*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:           #FFFFFF;
  --bg-sidebar:   #F9F9F9;
  --bg-card:      #FFFFFF;
  --bg-card-hover:#F9F9F9;
  --border:       #E5E5E5;
  --border-light: #EEEEEE;
  --text-primary: #000000;
  --text-secondary:#6E6E73;
  --text-muted:   #8E8E93;
  --accent:       #FFD52E;
  --accent-light: #FFCC00;
  --accent-bg:    #FFF9E6;
  --tag-bg:       #F2F2F2;
  --tag-text:     #6E6E73;
  --tag-active-bg:#FFD52E;
  --tag-active:   #000000;
  --link:         #007AFF;
  --shadow-sm:    0 1px 3px rgba(0,0,0,.06);
  --shadow-md:    0 4px 16px rgba(0,0,0,.08);
  --radius:       10px;
  --font-ui:      -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'SF Pro Display', 'Helvetica Neue', sans-serif;
  --font-body:    -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'SF Pro Display', 'Helvetica Neue', sans-serif;
  --font-mono:    'SF Mono', ui-monospace, 'Menlo', 'Consolas', monospace;
  --sidebar-w:    280px;
  --transition:   .18s ease;
}


html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text-primary);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* LAYOUT */

.layout {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* SIDEBAR */

.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: transform var(--transition);
}

.sidebar-header {
  padding: 28px 20px 16px;
  border-bottom: 1px solid var(--border-light);
}

.site-title {
  font-family: var(--font-ui);
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -.01em;
  color: var(--text-primary);
  line-height: 1.2;
  margin-bottom: 4px;
}

.site-description {
  font-size: .8rem;
  color: var(--text-muted);
  line-height: 1.4;
}

.search-wrap {
  padding: 14px 16px 10px;
  border-bottom: 1px solid var(--border-light);
}

.search-input {
  width: 100%;
  padding: 8px 12px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: .85rem;
  font-family: var(--font-ui);
  color: var(--text-primary);
  outline: none;
  transition: border-color var(--transition), box-shadow var(--transition);
}

.search-input::placeholder { color: var(--text-muted); }

.search-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(255, 213, 46, .25);
}

.tags-section {
  flex: 1;
  overflow-y: auto;
  padding: 12px 12px 16px;
}

.tags-label {
  font-size: .7rem;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 4px 8px 10px;
}

.tag-item {
  display: block;
  width: 100%;
  padding: 6px 10px;
  background: none;
  border: none;
  border-radius: 8px;
  text-align: left;
  font-size: .84rem;
  font-family: var(--font-ui);
  color: var(--text-secondary);
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
  line-height: 1.4;
}

.tag-item:hover {
  background: var(--border-light);
  color: var(--text-primary);
}

.tag-item.active {
  background: var(--accent);
  color: var(--tag-active);
  font-weight: 600;
}

.sidebar-footer {
  padding: 10px 16px 14px;
  border-top: 1px solid var(--border-light);
  font-size: .75rem;
  color: var(--text-muted);
}

/* MAIN */

.main {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}

.main-header {
  padding: 20px 32px 14px;
  border-bottom: 1px solid var(--border-light);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 10;
}

.main-header-title {
  font-family: var(--font-ui);
  font-size: 1.1rem;
  font-weight: 600;
  color: var(--text-primary);
  text-decoration: none;
  cursor: pointer;
}

.main-header-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.note-count-badge {
  font-size: .78rem;
  color: var(--text-muted);
  background: var(--tag-bg);
  padding: 2px 8px;
  border-radius: 20px;
  white-space: nowrap;
}

.view-toggle {
  display: flex;
  gap: 2px;
  background: var(--tag-bg);
  border-radius: var(--radius);
  padding: 2px;
}

.view-toggle button {
  background: none;
  border: none;
  padding: 4px 8px;
  border-radius: 4px;
  cursor: pointer;
  font-size: .78rem;
  color: var(--text-muted);
  font-family: var(--font-ui);
  transition: background var(--transition), color var(--transition);
}

.view-toggle button.active {
  background: var(--bg-card);
  color: var(--text-primary);
  box-shadow: var(--shadow-sm);
}

/* NOTES LIST — shared */

.notes-list {
  padding: 20px 32px 40px;
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.note-card {
  padding: 16px 18px;
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background var(--transition), box-shadow var(--transition), border-color var(--transition);
  margin-bottom: 8px;
  position: relative;
}

.note-card:hover {
  background: var(--bg-card-hover);
  border-color: var(--border);
  box-shadow: var(--shadow-sm);
}

.note-card-title {
  font-family: var(--font-ui);
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-primary);
  margin-bottom: 6px;
  line-height: 1.3;
}

.note-card-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
}

.note-card-date {
  font-size: .78rem;
  color: var(--text-muted);
}

.tag-pill {
  display: inline-block;
  font-size: .72rem;
  padding: 3px 10px;
  background: var(--tag-bg);
  color: var(--tag-text);
  border-radius: 20px;
  line-height: 1.5;
  font-weight: 500;
}

.note-card-excerpt {
  font-size: .84rem;
  color: var(--text-secondary);
  line-height: 1.55;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

/* LIST VIEW — thumbnail on the left */

.notes-list.view-list .note-card {
  display: flex;
  gap: 16px;
  align-items: stretch;
}

.notes-list.view-list .note-card-thumb {
  flex-shrink: 0;
  width: 80px;
  border-radius: 4px;
  overflow: hidden;
  background: var(--border-light);
}

.notes-list.view-list .note-card-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.notes-list.view-list .note-card-content {
  flex: 1;
  min-width: 0;
}

.notes-list.view-list .note-card-image { display: none; }

/* GRID VIEW */

.notes-list.view-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}

.notes-list.view-grid .note-card {
  margin-bottom: 0;
  display: flex;
  flex-direction: column;
}

.notes-list.view-grid .note-card-thumb { display: none; }

.notes-list.view-grid .note-card-image {
  margin: -16px -18px 16px;
  border-radius: var(--radius) var(--radius) 0 0;
  overflow: hidden;
  aspect-ratio: 16 / 9;
  background: var(--border-light);
}

.notes-list.view-grid .note-card-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
  transition: transform .4s ease;
}

.notes-list.view-grid .note-card:hover .note-card-image img {
  transform: scale(1.03);
}

.notes-list.view-grid .note-card-content {
  flex: 1;
}

/* Hide elements by default when not in the active view */
.notes-list:not(.view-list):not(.view-grid) .note-card-thumb { display: none; }
.notes-list:not(.view-grid) .note-card-image { display: none; }

.note-view-hero {
  margin: -28px -32px 32px;
  aspect-ratio: 16 / 6;
  overflow: hidden;
  background: var(--border-light);
}

.note-view-hero img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

@media (max-width: 720px) {
  .note-view-hero { margin: -16px -16px 24px; }
}

/* NOTE VIEW */

.note-view {
  padding: 28px 32px 60px;
  max-width: 760px;
  width: 100%;
}

.back-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: none;
  border: 1px solid var(--border);
  color: var(--text-secondary);
  font-size: .82rem;
  font-family: var(--font-ui);
  padding: 6px 14px;
  border-radius: var(--radius);
  cursor: pointer;
  margin-bottom: 32px;
  transition: background var(--transition), color var(--transition), border-color var(--transition);
}

.back-btn:hover {
  background: var(--border-light);
  color: var(--text-primary);
  border-color: var(--border);
}

.note-view-title {
  font-family: var(--font-ui);
  font-size: 1.7rem;
  font-weight: 700;
  line-height: 1.2;
  color: var(--text-primary);
  margin-bottom: 12px;
  letter-spacing: -.02em;
}

.note-view-meta {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  padding-bottom: 24px;
  border-bottom: 1px solid var(--border-light);
  margin-bottom: 28px;
}

.note-view-date {
  font-size: .82rem;
  color: var(--text-muted);
}

/* Note body — Apple Notes style */
.note-body {
  font-family: var(--font-body);
  font-size: 1rem;
  line-height: 1.7;
  color: var(--text-primary);
}

.note-body p, .note-body div { margin-bottom: .8em; }

.note-body h1, .note-body h2, .note-body h3 {
  font-family: var(--font-ui);
  margin: 1.4em 0 .5em;
  line-height: 1.25;
  color: var(--text-primary);
}

.note-body h1 { font-size: 1.5rem; font-weight: 700; }
.note-body h2 { font-size: 1.2rem; font-weight: 600; }
.note-body h3 { font-size: 1.05rem; font-weight: 600; }

.note-body ul, .note-body ol {
  padding-left: 1.6em;
  margin-bottom: .8em;
}

.note-body li { margin-bottom: .25em; }

.note-body a {
  color: var(--link);
  text-decoration: none;
}

.note-body a:hover {
  text-decoration: underline;
}

.note-body blockquote {
  border-left: 3px solid var(--accent);
  margin: 1em 0;
  padding: .5em 1.2em;
  color: var(--text-secondary);
}

.note-body code, .note-body pre {
  font-family: var(--font-mono);
  font-size: .88em;
  background: var(--accent-bg);
  border-radius: 6px;
}

.note-body code { padding: .15em .4em; }
.note-body pre { padding: 1em 1.2em; overflow-x: auto; margin-bottom: .8em; }
.note-body pre code { background: none; padding: 0; }

.note-body table {
  border-collapse: collapse;
  width: 100%;
  margin-bottom: .8em;
  font-size: .9em;
}

.note-body th, .note-body td {
  border: 1px solid var(--border-light);
  padding: .5em .8em;
  text-align: left;
}

.note-body th { background: var(--accent-bg); font-weight: 600; }
.note-body img { max-width: 100%; border-radius: 8px; }
.note-body hr { border: none; border-top: 1px solid var(--border-light); margin: 1.5em 0; }

/* EMPTY / ERROR STATES */

.state-message {
  padding: 60px 32px;
  text-align: center;
}

.state-message-icon {
  font-size: 2rem;
  margin-bottom: 16px;
  opacity: .4;
}

.state-message h2 {
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: 1.2rem;
  color: var(--text-secondary);
  margin-bottom: 10px;
}

.state-message p {
  font-size: .88rem;
  color: var(--text-muted);
  max-width: 400px;
  margin: 0 auto 16px;
  line-height: 1.6;
}

.error-code {
  display: inline-block;
  background: var(--accent-bg);
  border: 1px solid var(--border-light);
  border-radius: 8px;
  padding: 10px 18px;
  font-family: var(--font-mono);
  font-size: .82rem;
  color: var(--text-secondary);
  text-align: left;
  line-height: 1.8;
  margin: 6px 0;
  width: 100%;
  max-width: 420px;
}

/* LOADING SPINNER */

.note-loading {
  padding: 60px 32px;
  text-align: center;
  color: var(--text-muted);
}

.note-loading .spinner {
  display: inline-block;
  width: 24px;
  height: 24px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .6s linear infinite;
  margin-bottom: 12px;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* MOBILE TOGGLE */

.mobile-toggle {
  display: none;
  background: none;
  border: none;
  padding: 6px 8px;
  font-size: 1.2rem;
  cursor: pointer;
  color: var(--text-secondary);
  line-height: 1;
  flex-shrink: 0;
}

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.3);
  z-index: 50;
}

@media (max-width: 720px) {
  html { -webkit-text-size-adjust: 100%; }

  .layout { display: block; height: auto; overflow: visible; }

  .sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    z-index: 60;
    transform: translateX(-100%);
    box-shadow: var(--shadow-md);
    height: 100dvh;
    width: min(280px, 85vw);
  }

  .sidebar.open { transform: translateX(0); }
  .sidebar-overlay.visible { display: block; }
  .mobile-toggle { display: block; }

  .main { min-height: 100dvh; }

  .main-header {
    padding: 14px 12px 12px;
    gap: 8px;
    position: relative;
  }

  .main-header-title {
    font-size: .95rem;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
  }

  /* Larger touch targets for tags */
  .tag-item { padding: 10px 12px; font-size: .9rem; }

  /* Search input — prevent iOS zoom */
  .search-input { font-size: 16px; }

  /* Notes list */
  .notes-list { padding: 12px 12px 40px; }

  /* Grid: 2 columns on tablets, 1 on small phones */
  .notes-list.view-grid {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 10px;
  }

  .notes-list.view-grid .note-card { padding: 12px; }

  .notes-list.view-grid .note-card-image {
    margin: -12px -12px 10px;
  }

  .notes-list.view-grid .note-card-title { font-size: .92rem; }
  .notes-list.view-grid .note-card-excerpt { display: none; }

  /* List view */
  .notes-list.view-list .note-card { padding: 14px 12px; gap: 12px; }
  .notes-list.view-list .note-card-thumb { width: 64px; }

  /* Note view */
  .note-view {
    padding: 16px 16px 60px;
    padding-top: env(safe-area-inset-top, 16px);
  }

  .note-view-title { font-size: 1.4rem; }

  .note-view-hero { margin: -16px -16px 24px; aspect-ratio: 16 / 9; }

  .back-btn {
    position: sticky;
    top: 0;
    z-index: 5;
    background: var(--bg);
    margin-bottom: 20px;
  }

  .note-body { font-size: .95rem; line-height: 1.7; }
  .note-body img { border-radius: 4px; }

  /* Prevent horizontal overflow from wide content */
  .note-body table { display: block; overflow-x: auto; }
  .note-body pre { max-width: calc(100vw - 32px); }
  .note-body img { max-width: 100%; height: auto; }
}

/* Very small phones */
@media (max-width: 380px) {
  .notes-list.view-grid { grid-template-columns: 1fr; }
  .note-count-badge { display: none; }
  .main-header { padding-left: 8px; }
}

/* ANIMATIONS */

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.note-card { animation: fadeIn .2s ease both; }
.note-card:nth-child(1)  { animation-delay: .00s; }
.note-card:nth-child(2)  { animation-delay: .02s; }
.note-card:nth-child(3)  { animation-delay: .04s; }
.note-card:nth-child(4)  { animation-delay: .06s; }
.note-card:nth-child(5)  { animation-delay: .08s; }
.note-card:nth-child(n+6){ animation-delay: .10s; }

[hidden] { display: none !important; }
