/* ===== RESET & BASE ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --sidebar-w: 280px;

  /* Light theme */
  --bg:        #f0f2f7;
  --surface:   #ffffff;
  --surface2:  #f7f8fc;
  --border:    #e3e6ef;
  --text:      #1a1d2e;
  --text2:     #5a607a;
  --text3:     #8e95ae;
  --accent:    #3a5bde;
  --accent2:   #2445c7;
  --accent-bg: #eef1ff;
  --badge-bg:  #dde3ff;
  --badge-txt: #2a41c0;
  --warn:      #f59e0b;
  --danger:    #ef4444;
  --ok:        #10b981;
  --shadow-sm: 0 1px 3px rgba(0,0,0,.08);
  --shadow:    0 4px 20px rgba(0,0,0,.09);
  --shadow-lg: 0 8px 40px rgba(0,0,0,.12);
  --radius:    14px;
  --radius-sm: 8px;
  --sidebar-bg: #1a1d2e;
  --sidebar-text: rgba(255,255,255,.85);
  --sidebar-text2: rgba(255,255,255,.5);
  --sidebar-active: rgba(255,255,255,.1);
  --sidebar-hover: rgba(255,255,255,.06);
  --sidebar-border: rgba(255,255,255,.08);
}

[data-theme="dark"] {
  --bg:        #0d0f1a;
  --surface:   #161929;
  --surface2:  #1f2236;
  --border:    #2a2f4a;
  --text:      #e8ecf4;
  --text2:     #9ba4c0;
  --text3:     #5c6480;
  --accent:    #6b8aff;
  --accent2:   #8ba3ff;
  --accent-bg: #1a2150;
  --badge-bg:  #1e2860;
  --badge-txt: #8ba3ff;
  --shadow-sm: 0 1px 3px rgba(0,0,0,.3);
  --shadow:    0 4px 20px rgba(0,0,0,.4);
  --shadow-lg: 0 8px 40px rgba(0,0,0,.5);
  --sidebar-bg: #0a0c18;
  --sidebar-text: rgba(255,255,255,.8);
  --sidebar-text2: rgba(255,255,255,.4);
  --sidebar-active: rgba(107,138,255,.15);
  --sidebar-hover: rgba(255,255,255,.04);
  --sidebar-border: rgba(255,255,255,.06);
}

html { scroll-behavior: smooth; }

body {
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.65;
  transition: background .3s, color .3s;
  overflow-x: hidden;
}

/* ===== LAYOUT ===== */
.layout {
  display: flex;
  min-height: 100vh;
}

/* ===== SIDEBAR ===== */
.sidebar {
  width: var(--sidebar-w);
  min-height: 100vh;
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  gap: 0;
  position: fixed;
  left: 0; top: 0; bottom: 0;
  z-index: 100;
  transition: transform .3s cubic-bezier(.4,0,.2,1);
  overflow-y: auto;
  overflow-x: hidden;
}

.sidebar-logo {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 24px 20px 16px;
  border-bottom: 1px solid var(--sidebar-border);
}

.logo-icon { font-size: 26px; flex-shrink: 0; }

.logo-text {
  display: flex;
  flex-direction: column;
}
.logo-title {
  font-size: .9rem;
  font-weight: 700;
  color: #fff;
  letter-spacing: .01em;
  line-height: 1.2;
}
.logo-sub {
  font-size: .72rem;
  color: var(--sidebar-text2);
  margin-top: 2px;
}

.sidebar-meta {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 20px;
}
.meta-badge {
  font-size: .72rem;
  font-weight: 600;
  background: rgba(107,138,255,.2);
  color: #8ba3ff;
  padding: 3px 9px;
  border-radius: 20px;
  letter-spacing: .02em;
}
.meta-year {
  font-size: .72rem;
  color: var(--sidebar-text2);
}

.nav-section { padding: 8px 12px; }

.nav-label {
  font-size: .68rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--sidebar-text2);
  padding: 0 8px;
  margin-bottom: 6px;
}

.nav-list { list-style: none; display: flex; flex-direction: column; gap: 2px; }

.nav-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 11px 12px;
  background: transparent;
  border: none;
  border-radius: var(--radius-sm);
  color: var(--sidebar-text);
  cursor: pointer;
  transition: background .15s, color .15s;
  text-align: left;
  font-family: inherit;
}

.nav-item:hover { background: var(--sidebar-hover); }
.nav-item.active { background: var(--sidebar-active); }
.nav-item.active .nav-icon { filter: drop-shadow(0 0 6px rgba(107,138,255,.6)); }

.nav-icon { font-size: 1.2rem; flex-shrink: 0; }

.nav-text {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 1px;
}
.nav-text strong { font-size: .875rem; font-weight: 600; color: #fff; }
.nav-text small { font-size: .72rem; color: var(--sidebar-text2); }
.nav-item.active .nav-text strong { color: #9db5ff; }

.nav-arrow {
  font-size: 1.1rem;
  color: var(--sidebar-text2);
  transition: transform .2s;
}
.nav-item.active .nav-arrow { color: #8ba3ff; transform: translateX(2px); }

.sidebar-info {
  margin: auto 0 0;
  padding: 16px 20px;
  border-top: 1px solid var(--sidebar-border);
}
.info-card { }
.info-title { font-size: .68rem; text-transform: uppercase; letter-spacing: .06em; color: var(--sidebar-text2); margin-bottom: 2px; }
.info-val { font-size: .8rem; font-weight: 600; color: var(--sidebar-text); margin-bottom: 0; }

.theme-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 12px 20px 20px;
  padding: 10px 14px;
  background: var(--sidebar-hover);
  border: 1px solid var(--sidebar-border);
  border-radius: var(--radius-sm);
  color: var(--sidebar-text2);
  font-size: .8rem;
  font-family: inherit;
  cursor: pointer;
  transition: all .2s;
}
.theme-btn:hover { background: var(--sidebar-active); color: var(--sidebar-text); }

/* ===== MAIN WRAP ===== */
.main-wrap {
  margin-left: var(--sidebar-w);
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
  position: relative;
}

/* ===== TOPBAR ===== */
.topbar {
  height: 64px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 24px;
  position: sticky;
  top: 0;
  z-index: 50;
  box-shadow: var(--shadow-sm);
  transition: background .3s;
}

.menu-btn {
  display: none;
  background: none;
  border: none;
  color: var(--text2);
  cursor: pointer;
  padding: 6px;
  border-radius: var(--radius-sm);
  transition: background .15s;
}
.menu-btn:hover { background: var(--border); }

.topbar-center {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.topbar-badge {
  font-size: .7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .06em;
  background: var(--badge-bg);
  color: var(--badge-txt);
  padding: 3px 9px;
  border-radius: 20px;
  white-space: nowrap;
  flex-shrink: 0;
}

.topbar-title {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-search {
  position: relative;
  display: flex;
  align-items: center;
}
.search-icon {
  position: absolute;
  left: 10px;
  color: var(--text3);
  pointer-events: none;
}
.search-input {
  padding: 8px 12px 8px 34px;
  border: 1px solid var(--border);
  border-radius: 40px;
  background: var(--bg);
  color: var(--text);
  font-size: .85rem;
  font-family: inherit;
  width: 220px;
  transition: all .2s;
  outline: none;
}
.search-input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(58,91,222,.15);
  width: 260px;
}

/* ===== SEARCH RESULTS BAR ===== */
.search-results-bar {
  background: var(--accent-bg);
  border-bottom: 1px solid var(--border);
  padding: 8px 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: .82rem;
  color: var(--accent);
  gap: 12px;
  flex-wrap: wrap;
}
.search-nav { display: flex; align-items: center; gap: 6px; }
.search-nav button {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--accent);
  font-size: .9rem;
  width: 28px; height: 28px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all .15s;
  display: flex; align-items: center; justify-content: center;
}
.search-nav button:hover { background: var(--accent); color: #fff; }
#match-counter { font-size: .8rem; color: var(--text2); min-width: 50px; text-align: center; }

/* ===== CONTENT ===== */
.content-area {
  flex: 1;
  padding: 36px 40px;
  max-width: 860px;
  width: 100%;
  margin: 0 auto;
  position: relative;
}

/* ===== SPINNER ===== */
.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
  margin: 60px auto;
  display: none;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== PROSE (Markdown) ===== */
.prose {
  animation: fadeUp .35s ease-out;
}
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(14px); }
  to   { opacity: 1; transform: translateY(0); }
}

.prose h1 {
  font-size: 1.9rem;
  font-weight: 800;
  color: var(--text);
  line-height: 1.2;
  margin-bottom: 8px;
  letter-spacing: -.02em;
}

.prose h2 {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text);
  margin-top: 2.5em;
  margin-bottom: .75em;
  padding-bottom: .5em;
  border-bottom: 2px solid var(--border);
  letter-spacing: -.01em;
}

.prose h3 {
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--text);
  margin-top: 1.8em;
  margin-bottom: .5em;
}

.prose h4 {
  font-size: .95rem;
  font-weight: 600;
  color: var(--text2);
  margin-top: 1.4em;
  margin-bottom: .4em;
  text-transform: uppercase;
  letter-spacing: .04em;
}

.prose p {
  margin-bottom: 1em;
  color: var(--text);
}

.prose em { color: var(--text2); font-style: italic; }

.prose strong { font-weight: 600; color: var(--text); }

.prose ul, .prose ol {
  padding-left: 1.6em;
  margin-bottom: 1em;
}
.prose li { margin-bottom: .4em; }

.prose a {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: border-color .15s;
  font-weight: 500;
}
.prose a:hover { border-color: var(--accent); }

.prose hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 2em 0;
}

.prose blockquote {
  border-left: 3px solid var(--accent);
  padding: .75em 1.25em;
  background: var(--accent-bg);
  border-radius: 0 var(--radius-sm) var(--radius-sm) 0;
  margin: 1.2em 0;
  color: var(--text2);
  font-size: .9rem;
}

/* Tables */
.prose table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.2em 0;
  font-size: .88rem;
  border-radius: var(--radius-sm);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
}
.prose thead { background: var(--accent); color: #fff; }
.prose thead th {
  padding: 10px 14px;
  text-align: left;
  font-weight: 600;
  font-size: .8rem;
  text-transform: uppercase;
  letter-spacing: .05em;
}
.prose tbody tr { border-bottom: 1px solid var(--border); transition: background .12s; }
.prose tbody tr:last-child { border-bottom: none; }
.prose tbody tr:hover { background: var(--surface2); }
.prose td { padding: 9px 14px; color: var(--text); vertical-align: top; }
.prose tbody tr:nth-child(even) td { background: var(--surface2); }

/* Code */
.prose code {
  background: var(--surface2);
  border: 1px solid var(--border);
  padding: .15em .4em;
  border-radius: 4px;
  font-size: .85em;
  font-family: 'Fira Mono', monospace;
  color: var(--accent);
}

/* Search highlight */
mark.search-mark {
  background: #fde047;
  color: #1a1a1a;
  border-radius: 3px;
  padding: 1px 2px;
}
mark.search-mark.current {
  background: #f97316;
  color: #fff;
}

/* ===== BACK TO TOP ===== */
.back-top {
  position: fixed;
  bottom: 28px;
  right: 28px;
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  box-shadow: var(--shadow);
  display: flex; align-items: center; justify-content: center;
  opacity: 0;
  transform: translateY(12px);
  pointer-events: none;
  transition: opacity .25s, transform .25s;
}
.back-top.visible {
  opacity: 1;
  transform: translateY(0);
  pointer-events: all;
}
.back-top:hover { background: var(--accent2); transform: translateY(-2px); }

/* ===== OVERLAY ===== */
.overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 90;
}
.overlay.open { display: block; }

/* ===== RESPONSIVE ===== */
@media (max-width: 900px) {
  .content-area { padding: 28px 24px; }
  .search-input { width: 160px; }
  .search-input:focus { width: 200px; }
}

@media (max-width: 680px) {
  .sidebar {
    transform: translateX(calc(-1 * var(--sidebar-w)));
    box-shadow: var(--shadow-lg);
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .main-wrap { margin-left: 0; }
  .menu-btn { display: flex; }
  .topbar { gap: 10px; padding: 0 16px; }
  .topbar-title { font-size: .9rem; }
  .topbar-search { display: none; }
  .content-area { padding: 20px 16px; }
  .prose h1 { font-size: 1.5rem; }
  .prose h2 { font-size: 1.1rem; }
  .back-top { bottom: 20px; right: 16px; }
}
