:root {
  /* Default: dark mode */
  --bg: #0f0f1a;
  --bg-sidebar: #0a0a14;
  --bg-sidebar-hover: #141428;
  --bg-card: #1a1a2e;
  --bg-tldr: #1a1a3e;
  --bg-code: #16162a;
  --bg-callout: #2a2a1a;
  --text: #e2e8f0;
  --text-secondary: #a0a8b8;
  --text-sidebar: #c8c8d8;
  --text-sidebar-active: #ffffff;
  --accent: #6c3fba;
  --accent-light: #8b5cf6;
  --border: #2a2a4a;
  --border-sidebar: #2a2a4a;
  --shadow: 0 1px 3px rgba(0,0,0,.3);
  --shadow-lg: 0 4px 12px rgba(0,0,0,.4);
  --radius: 8px;
  --sidebar-w: 300px;
  --max-content: 820px;
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  --font-mono: 'JetBrains Mono', 'Fira Code', 'Consolas', monospace;
  --font-serif: 'Georgia', 'Times New Roman', serif;
}

[data-theme="light"] {
  --bg: #fafafa;
  --bg-card: #ffffff;
  --bg-tldr: #f0f7ff;
  --bg-code: #f6f8fa;
  --bg-callout: #fffbeb;
  --text: #1a1a2e;
  --text-secondary: #555;
  --border: #e2e8f0;
  --shadow: 0 1px 3px rgba(0,0,0,.08);
  --shadow-lg: 0 4px 12px rgba(0,0,0,.1);
  --bg-sidebar: #1a1a2e;
  --bg-sidebar-hover: #16213e;
}

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

html { font-size: 16px; scroll-behavior: smooth; }

body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.7;
  display: flex;
  min-height: 100vh;
}

/* ═══════════════════════════════════════════ */
/* SIDEBAR                                     */
/* ═══════════════════════════════════════════ */
.sidebar {
  width: var(--sidebar-w);
  background: var(--bg-sidebar);
  color: var(--text-sidebar);
  position: fixed;
  top: 0;
  left: 0;
  height: 100vh;
  overflow-y: auto;
  z-index: 100;
  transition: transform .3s ease;
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  padding: 20px 16px 12px;
  border-bottom: 1px solid var(--border-sidebar);
  flex-shrink: 0;
}

.sidebar-header h1 {
  font-size: 1.1rem;
  color: #fff;
  font-weight: 700;
  letter-spacing: -.02em;
}

.sidebar-header .subtitle {
  font-size: .7rem;
  color: var(--accent-light);
  text-transform: uppercase;
  letter-spacing: .1em;
  margin-top: 2px;
}

.sidebar-nav {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.sidebar-nav::-webkit-scrollbar { width: 4px; }
.sidebar-nav::-webkit-scrollbar-track { background: transparent; }
.sidebar-nav::-webkit-scrollbar-thumb { background: var(--border-sidebar); border-radius: 4px; }

/* Part group */
.nav-part {
  padding: 10px 16px 4px;
  font-size: .65rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .12em;
  color: var(--accent-light);
  opacity: .8;
}

/* Chapter link */
.nav-chapter {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 16px;
  font-size: .85rem;
  font-weight: 600;
  color: var(--text-sidebar);
  text-decoration: none;
  cursor: pointer;
  transition: background .15s, color .15s;
  user-select: none;
}

.nav-chapter:hover { background: var(--bg-sidebar-hover); color: #fff; }
.nav-chapter.active { color: var(--text-sidebar-active); background: var(--bg-sidebar-hover); }

.nav-chapter .ch-num {
  font-size: .7rem;
  font-weight: 700;
  color: var(--accent-light);
  min-width: 22px;
}

.nav-chapter .ch-arrow {
  margin-left: auto;
  font-size: .65rem;
  transition: transform .2s;
  opacity: .5;
}

.nav-chapter.expanded .ch-arrow { transform: rotate(90deg); }

/* Section links */
.nav-sections {
  display: none;
  padding: 0;
}

.nav-sections.open { display: block; }

.nav-section {
  display: block;
  padding: 4px 16px 4px 46px;
  font-size: .78rem;
  color: var(--text-sidebar);
  text-decoration: none;
  opacity: .7;
  transition: opacity .15s, color .15s, background .15s;
  line-height: 1.4;
}

.nav-section:hover { opacity: 1; background: var(--bg-sidebar-hover); }
.nav-section.active { opacity: 1; color: var(--accent-light); font-weight: 600; }

/* Sidebar toggle for mobile */
.sidebar-toggle {
  display: none;
  position: fixed;
  top: 12px;
  left: 12px;
  z-index: 200;
  background: var(--bg-sidebar);
  color: #fff;
  border: none;
  padding: 8px 12px;
  border-radius: 6px;
  font-size: 1.2rem;
  cursor: pointer;
  box-shadow: var(--shadow-lg);
}

/* ═══════════════════════════════════════════ */
/* MAIN CONTENT                                */
/* ═══════════════════════════════════════════ */
.main {
  margin-left: var(--sidebar-w);
  flex: 1;
  min-width: 0;
}

.content {
  max-width: var(--max-content);
  margin: 0 auto;
  padding: 40px 32px 80px;
}

/* Top bar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 32px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg);
  z-index: 50;
  margin-left: var(--sidebar-w);
}

.breadcrumb {
  font-size: .8rem;
  color: var(--text-secondary);
}

.breadcrumb a {
  color: var(--accent);
  text-decoration: none;
}

.breadcrumb a:hover { text-decoration: underline; }
.breadcrumb .sep { margin: 0 6px; opacity: .4; }

.topbar-actions { display: flex; gap: 8px; align-items: center; margin-right: 12px; }

.theme-toggle {
  background: none;
  border: 1px solid var(--border);
  padding: 4px 10px;
  border-radius: 6px;
  cursor: pointer;
  font-size: .85rem;
  color: var(--text-secondary);
  transition: background .15s;
}

.theme-toggle:hover { background: var(--border); }

/* ═══════════════════════════════════════════ */
/* TYPOGRAPHY                                  */
/* ═══════════════════════════════════════════ */
.content h1 {
  font-size: 2.2rem;
  font-weight: 800;
  color: var(--text);
  line-height: 1.2;
  margin-bottom: 8px;
  letter-spacing: -.03em;
}

.content h2 {
  font-size: 1.5rem;
  font-weight: 700;
  color: var(--text);
  margin-top: 48px;
  margin-bottom: 12px;
  letter-spacing: -.02em;
}

.content h3 {
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text);
  margin-top: 32px;
  margin-bottom: 8px;
}

.content h4 {
  font-size: 1rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-top: 24px;
  margin-bottom: 8px;
}

.content p {
  margin-bottom: 16px;
  font-size: 1rem;
  line-height: 1.8;
}

.content a { color: var(--accent); text-decoration: none; }
.content a:hover { text-decoration: underline; }

/* Chapter meta */
.chapter-meta {
  font-size: .85rem;
  color: var(--text-secondary);
  margin-bottom: 24px;
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
}

.chapter-meta .tag {
  background: var(--bg-card);
  border: 1px solid var(--border);
  padding: 2px 10px;
  border-radius: 20px;
  font-size: .75rem;
}

/* ═══════════════════════════════════════════ */
/* TLDR BOX                                    */
/* ═══════════════════════════════════════════ */
.tldr {
  background: var(--bg-tldr);
  border-left: 4px solid var(--accent);
  padding: 16px 20px;
  border-radius: 0 var(--radius) var(--radius) 0;
  margin: 20px 0 32px;
}

.tldr-label {
  font-size: .7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--accent);
  margin-bottom: 6px;
}

.tldr p {
  margin-bottom: 6px;
  font-size: .92rem;
  line-height: 1.6;
}

.tldr p:last-child { margin-bottom: 0; }

/* ═══════════════════════════════════════════ */
/* CALLOUT BOXES                               */
/* ═══════════════════════════════════════════ */
.callout {
  padding: 16px 20px;
  border-radius: var(--radius);
  margin: 20px 0;
  font-size: .92rem;
}

.callout-warning {
  background: var(--bg-callout);
  border-left: 4px solid #f59e0b;
}

.callout-info {
  background: var(--bg-tldr);
  border-left: 4px solid #3b82f6;
}

.callout-danger {
  background: #2a1a1a;
  border-left: 4px solid #ef4444;
}

[data-theme="light"] .callout-danger { background: #fef2f2; }

.callout-label {
  font-size: .7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .1em;
  margin-bottom: 6px;
}

/* ═══════════════════════════════════════════ */
/* CODE                                        */
/* ═══════════════════════════════════════════ */
code {
  font-family: var(--font-mono);
  font-size: .88em;
  background: var(--bg-code);
  padding: 2px 6px;
  border-radius: 4px;
}

pre {
  background: var(--bg-code);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  overflow-x: auto;
  margin: 16px 0;
  font-size: .85rem;
  line-height: 1.6;
}

pre code {
  background: none;
  padding: 0;
  font-size: inherit;
}

/* ═══════════════════════════════════════════ */
/* CARDS (Section listing on chapter pages)    */
/* ═══════════════════════════════════════════ */
.section-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin: 24px 0;
}

.section-card {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 14px 18px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--text);
  transition: box-shadow .15s, border-color .15s;
}

.section-card:hover {
  box-shadow: var(--shadow-lg);
  border-color: var(--accent);
  text-decoration: none;
}

.section-card .num {
  font-size: .75rem;
  font-weight: 700;
  color: var(--accent);
  min-width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  background: var(--bg-tldr);
  flex-shrink: 0;
}

.section-card .title {
  font-weight: 600;
  font-size: .95rem;
}

.section-card .count {
  margin-left: auto;
  font-size: .75rem;
  color: var(--text-secondary);
  flex-shrink: 0;
}

/* ═══════════════════════════════════════════ */
/* PREV/NEXT NAV                               */
/* ═══════════════════════════════════════════ */
.page-nav {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  margin-top: 48px;
  padding-top: 24px;
  border-top: 1px solid var(--border);
}

.page-nav a {
  display: flex;
  flex-direction: column;
  padding: 12px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--text);
  flex: 1;
  max-width: 48%;
  transition: border-color .15s;
}

.page-nav a:hover { border-color: var(--accent); }

.page-nav a .label {
  font-size: .7rem;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--text-secondary);
  margin-bottom: 2px;
}

.page-nav a .link-title {
  font-weight: 600;
  font-size: .9rem;
  color: var(--accent);
}

.page-nav .next { text-align: right; margin-left: auto; }

/* ═══════════════════════════════════════════ */
/* LANDING PAGE                                */
/* ═══════════════════════════════════════════ */
.hero {
  text-align: center;
  padding: 60px 0 40px;
}

.hero h1 {
  font-size: 3rem;
  letter-spacing: -.04em;
  margin-bottom: 12px;
}

.hero .tagline {
  font-size: 1.15rem;
  color: var(--text-secondary);
  max-width: 600px;
  margin: 0 auto;
}

.stats {
  display: flex;
  justify-content: center;
  gap: 32px;
  margin: 32px 0;
  flex-wrap: wrap;
}

.stat {
  text-align: center;
}

.stat .num {
  font-size: 2rem;
  font-weight: 800;
  color: var(--accent);
}

.stat .label {
  font-size: .75rem;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: .08em;
}

.part-group {
  margin: 32px 0 16px;
}

.part-group .part-title {
  font-size: .75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .1em;
  color: var(--accent);
  margin-bottom: 8px;
  padding-bottom: 4px;
  border-bottom: 2px solid var(--accent);
  display: inline-block;
}

.chapter-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 12px;
}

.chapter-card {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 14px 16px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  text-decoration: none;
  color: var(--text);
  transition: box-shadow .15s, border-color .15s;
}

.chapter-card:hover {
  box-shadow: var(--shadow-lg);
  border-color: var(--accent);
  text-decoration: none;
}

.chapter-card .ch-num {
  font-size: .75rem;
  font-weight: 800;
  color: var(--accent);
  min-width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: var(--bg-tldr);
  flex-shrink: 0;
}

.chapter-card .ch-info .ch-title {
  font-weight: 600;
  font-size: .9rem;
  line-height: 1.3;
}

.chapter-card .ch-info .ch-meta {
  font-size: .72rem;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* ═══════════════════════════════════════════ */
/* SVG diagrams                                */
/* ═══════════════════════════════════════════ */
.diagram {
  margin: 24px 0;
  text-align: center;
}

.diagram svg {
  max-width: 100%;
  height: auto;
}

.diagram figcaption {
  font-size: .8rem;
  color: var(--text-secondary);
  margin-top: 8px;
  font-style: italic;
}

/* ═══════════════════════════════════════════ */
/* TABLES                                      */
/* ═══════════════════════════════════════════ */
table {
  width: 100%;
  border-collapse: collapse;
  margin: 16px 0;
  font-size: .9rem;
}

th, td {
  padding: 8px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
}

th {
  font-weight: 600;
  font-size: .8rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  color: var(--text-secondary);
}

/* ═══════════════════════════════════════════ */
/* LISTS                                       */
/* ═══════════════════════════════════════════ */
.content ul, .content ol {
  margin: 12px 0 16px 24px;
}

.content li {
  margin-bottom: 6px;
  line-height: 1.7;
}

/* ═══════════════════════════════════════════ */
/* RESPONSIVE                                  */
/* ═══════════════════════════════════════════ */
@media (max-width: 900px) {
  .sidebar {
    transform: translateX(-100%);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-toggle { display: block; }

  .main { margin-left: 0; }
  .topbar { margin-left: 0; padding-left: 56px; }
  .content { padding: 24px 16px 60px; }

  .hero h1 { font-size: 2rem; }
  .chapter-grid { grid-template-columns: 1fr; }
}

/* ═══════════════════════════════════════════ */
/* PROGRESS BAR                                */
/* ═══════════════════════════════════════════ */
.progress-bar {
  position: fixed;
  top: 0;
  left: var(--sidebar-w);
  right: 0;
  height: 3px;
  z-index: 999;
  background: var(--border);
}

.progress-bar .fill {
  height: 100%;
  background: var(--accent);
  transition: width .1s;
  width: 0%;
}

@media (max-width: 900px) {
  .progress-bar { left: 0; }
}
