/* Base variables & themes - 3Blue1Brown Dark Math Cinema (Default) + Academic Paper (Light) */
:root {
  --bg: #0c0e14;
  --bg-elev: #121621;
  --surface: #171c29;
  --surface-2: #1e2436;
  --border: #283047;
  --border-strong: #374363;
  --text: #e7ecf5;
  --text-muted: #98a4bd;
  --text-dim: #6f7c99;

  /* 3b1b Manim Semantic Colors */
  --c-i: #83C167;
  --c-j: #FC6255;
  --c-k: #58C4DD;
  --c-y: #F7D154;
  --c-p: #C586C0;

  --accent: #58C4DD;
  --accent-bg: rgba(88, 196, 221, 0.12);
  --gold: #F7D154;
  --gold-bg: rgba(247, 209, 84, 0.10);
  --danger: #FC6255;
  --danger-bg: rgba(252, 98, 85, 0.10);
  --success: #83C167;
  --success-bg: rgba(131, 193, 103, 0.12);

  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.35);
  --shadow-md: 0 8px 28px rgba(0, 0, 0, 0.45);
  --shadow-lg: 0 18px 48px rgba(0, 0, 0, 0.65);

  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --nav-h: 62px;
  --font-sans: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", -apple-system, BlinkMacSystemFont, sans-serif;
  --font-mono: "JetBrains Mono", "Fira Code", Consolas, "Courier New", monospace;
}

[data-theme="light"] {
  --bg: #f6f8fb;
  --bg-elev: #ffffff;
  --surface: #ffffff;
  --surface-2: #eef2f8;
  --border: #dce3ee;
  --border-strong: #bac6da;
  --text: #161d2b;
  --text-muted: #4b5873;
  --text-dim: #73829e;

  --c-i: #2b8a3e;
  --c-j: #c92a2a;
  --c-k: #0b7285;
  --c-y: #b87400;
  --c-p: #862e9c;

  --accent: #0c7792;
  --accent-bg: rgba(12, 119, 146, 0.09);
  --gold: #b87400;
  --gold-bg: rgba(184, 116, 0, 0.08);
  --danger: #c92a2a;
  --danger-bg: rgba(201, 42, 42, 0.08);
  --success: #2b8a3e;
  --success-bg: rgba(43, 138, 62, 0.10);

  --shadow-sm: 0 2px 8px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 8px 24px rgba(15, 23, 42, 0.08);
  --shadow-lg: 0 18px 44px rgba(15, 23, 42, 0.14);
}

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

html {
  scroll-behavior: smooth;
  scroll-padding-top: calc(var(--nav-h) + 20px);
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 16px;
  line-height: 1.75;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

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

img {
  max-width: 100%;
  display: block;
}

/* Manim semantic colors in prose */
.t-i { color: var(--c-i); font-weight: 600; }
.t-j { color: var(--c-j); font-weight: 600; }
.t-k { color: var(--c-k); font-weight: 600; }
.t-y { color: var(--c-y); font-weight: 600; }
.t-p { color: var(--c-p); font-weight: 600; }

/* KaTeX subtle spacing & scroll safety */
.katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  padding: 6px 0;
  margin: 0.6em 0;
}

/* Top Navbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 90;
  height: var(--nav-h);
  background: rgba(12, 14, 20, 0.88);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
[data-theme="light"] .topbar {
  background: rgba(255, 255, 255, 0.90);
}
.topbar-inner {
  max-width: 1320px;
  margin: 0 auto;
  height: 100%;
  padding: 0 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}
.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 700;
  font-size: 1.04rem;
  color: var(--text);
  text-decoration: none !important;
  white-space: nowrap;
}
.brand-logo {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  background: linear-gradient(135deg, rgba(88,196,221,0.22), rgba(131,193,103,0.22));
  border: 1px solid var(--border-strong);
  display: grid;
  place-items: center;
  color: var(--c-k);
  font-family: var(--font-mono);
  font-size: 0.86rem;
  font-weight: 700;
}
.nav-links {
  display: flex;
  align-items: center;
  gap: 6px;
}
.nav-link {
  padding: 6px 12px;
  border-radius: 8px;
  color: var(--text-muted);
  font-size: 0.92rem;
  font-weight: 500;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
}
.nav-link:hover, .nav-link.active {
  background: var(--surface-2);
  color: var(--text);
  text-decoration: none;
}
.nav-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 14px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.88rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.16s ease;
  font-family: inherit;
  text-decoration: none !important;
  white-space: nowrap;
}
.btn:hover {
  border-color: var(--border-strong);
  background: var(--surface-2);
}
.btn-primary {
  background: var(--accent-bg);
  border-color: var(--accent);
  color: var(--accent);
}
.btn-primary:hover {
  background: var(--accent);
  color: #0b0e14;
}
.btn-success {
  background: var(--success-bg);
  border-color: var(--success);
  color: var(--success);
}
.btn-sm {
  padding: 5px 10px;
  font-size: 0.82rem;
}
.kbd-hint {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-dim);
}

[data-theme="light"] .btn {
  background: #ffffff;
  color: #161d2b;
  border-color: #cbd5e4;
}
[data-theme="light"] .btn:hover {
  background: #eef2f8;
  border-color: #94a3b8;
}
[data-theme="light"] .btn-primary {
  background: rgba(12, 119, 146, 0.10);
  color: #0b657d;
  border-color: #0c7792;
}
[data-theme="light"] .btn-primary:hover {
  background: #0c7792;
  color: #ffffff;
}
[data-theme="light"] .btn-success {
  background: rgba(43, 138, 62, 0.12);
  color: #216e30;
  border-color: #2b8a3e;
}

/* Hero Section on Home */
.hero {
  position: relative;
  overflow: hidden;
  border-bottom: 1px solid var(--border);
  padding: 54px 20px 44px;
  background: radial-gradient(circle at 20% 20%, rgba(88,196,221,0.10), transparent 45%),
              radial-gradient(circle at 80% 70%, rgba(131,193,103,0.08), transparent 50%),
              var(--bg);
}
#hero-grid-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0.42;
  pointer-events: none;
}
.hero-inner {
  position: relative;
  z-index: 2;
  max-width: 1200px;
  margin: 0 auto;
  display: grid;
  grid-template-columns: 1.3fr 0.9fr;
  gap: 32px;
  align-items: center;
}
.hero-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 12px;
  border-radius: 999px;
  border: 1px solid var(--border-strong);
  background: var(--surface);
  font-size: 0.82rem;
  color: var(--accent);
  margin-bottom: 14px;
}
.hero h1 {
  margin: 0 0 14px;
  font-size: clamp(1.9rem, 3.6vw, 2.85rem);
  line-height: 1.2;
  letter-spacing: -0.02em;
}
.hero-sub {
  margin: 0 0 22px;
  color: var(--text-muted);
  font-size: 1.02rem;
  max-width: 640px;
}
.hero-cta {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.progress-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 20px 22px;
  box-shadow: var(--shadow-md);
}
.progress-header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 10px;
}
.progress-bar-track {
  height: 10px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
  margin-bottom: 14px;
}
.progress-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--c-k), var(--c-i), var(--c-y));
  border-radius: 999px;
  transition: width 0.35s ease;
}
.progress-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-top: 12px;
}
.stat-box {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px;
  text-align: center;
}
.stat-val {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--text);
  font-family: var(--font-mono);
}
.stat-lbl {
  font-size: 0.76rem;
  color: var(--text-dim);
}

/* Main Container */
.container {
  max-width: 1240px;
  margin: 0 auto;
  padding: 36px 20px 64px;
}
.section-head {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-end;
  gap: 12px;
  margin: 0 0 18px;
}
.section-head h2 {
  margin: 0;
  font-size: 1.45rem;
}
.section-head p {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.92rem;
}

/* Knowledge Map SVG */
.map-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 16px;
  margin-bottom: 42px;
  box-shadow: var(--shadow-sm);
  overflow-x: auto;
  max-width: 100%;
}
.map-svg {
  width: 100%;
  min-width: 860px;
  height: auto;
  display: block;
}
.map-node {
  cursor: pointer;
  transition: transform 0.16s ease;
}
.map-node rect {
  fill: var(--bg-elev);
  stroke: var(--border-strong);
  stroke-width: 1.5;
  rx: 8;
  transition: stroke 0.15s, fill 0.15s;
}
.map-node:hover rect {
  stroke: var(--c-k);
  fill: var(--surface-2);
}
.map-node.done rect {
  stroke: var(--c-i);
}
.map-node text {
  fill: var(--text);
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 600;
}
.map-node .node-num {
  fill: var(--c-k);
  font-family: var(--font-mono);
  font-size: 10.5px;
}
.map-edge {
  fill: none;
  stroke: var(--border-strong);
  stroke-width: 1.4;
  opacity: 0.7;
}
.map-stage-lbl {
  fill: var(--c-k);
  font-size: 12px;
  font-weight: 700;
}
[data-theme="light"] .map-node rect {
  fill: #f8fafd;
  stroke: #c4cfdf;
}
[data-theme="light"] .map-node:hover rect {
  fill: #eaf1fa;
  stroke: var(--accent);
}
[data-theme="light"] .map-node.done rect {
  fill: #eef8f0;
  stroke: var(--c-i);
}
[data-theme="light"] .map-node text {
  fill: #161d2b;
}
[data-theme="light"] .map-node .node-num {
  fill: #0c7792;
}

/* Stage Groups & Episode Cards */
.stage-block {
  margin-bottom: 36px;
}
.stage-title-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.stage-pill {
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 700;
  font-family: var(--font-mono);
  background: var(--accent-bg);
  color: var(--accent);
  border: 1px solid rgba(88,196,221,0.3);
}
.stage-desc {
  color: var(--text-muted);
  font-size: 0.9rem;
}
.cards-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
  gap: 16px;
}
.ep-card {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: transform 0.16s ease, border-color 0.16s ease, box-shadow 0.16s ease;
  text-decoration: none !important;
  color: var(--text);
}
.ep-card:hover {
  transform: translateY(-3px);
  border-color: var(--accent);
  box-shadow: var(--shadow-md);
}
.ep-card.is-done {
  border-color: rgba(131, 193, 103, 0.55);
}
.ep-thumb-wrap {
  position: relative;
  aspect-ratio: 16 / 9;
  background: #090b10;
  overflow: hidden;
  border-bottom: 1px solid var(--border);
}
.ep-thumb-wrap img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.ep-num-badge {
  position: absolute;
  top: 10px;
  left: 10px;
  padding: 2px 8px;
  border-radius: 6px;
  background: rgba(12, 14, 20, 0.85);
  border: 1px solid rgba(255,255,255,0.16);
  color: #f4f7fc;
  font-family: var(--font-mono);
  font-size: 0.78rem;
  font-weight: 700;
}
.ep-done-badge {
  display: none;
  position: absolute;
  top: 10px;
  right: 10px;
  padding: 2px 8px;
  border-radius: 6px;
  background: rgba(24, 52, 30, 0.92);
  border: 1px solid #83C167;
  color: #b2f29b;
  font-size: 0.74rem;
  font-weight: 600;
}
.ep-card.is-done .ep-done-badge {
  display: inline-flex;
}
.ep-body {
  padding: 14px 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
}
.ep-title {
  margin: 0;
  font-size: 1.05rem;
  line-height: 1.35;
  color: var(--text);
}
.ep-intuition {
  margin: 0;
  font-size: 0.86rem;
  color: var(--text-muted);
  line-height: 1.55;
  flex: 1;
}
.ep-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  font-size: 0.76rem;
  color: var(--text-dim);
  padding-top: 6px;
  border-top: 1px dashed var(--border);
}

/* Chapter Reader Layout */
.chapter-shell {
  max-width: 1320px;
  margin: 0 auto;
  padding: 28px 20px 80px;
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  gap: 32px;
  align-items: start;
}
.chapter-sidebar {
  position: sticky;
  top: calc(var(--nav-h) + 20px);
  max-height: calc(100vh - var(--nav-h) - 40px);
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px;
}
.sidebar-title {
  font-size: 0.8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-dim);
  margin: 0 0 10px;
}
.toc-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.toc-link {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 7px;
  color: var(--text-muted);
  font-size: 0.86rem;
  line-height: 1.35;
  border-left: 2px solid transparent;
  text-decoration: none !important;
}
.toc-link:hover {
  background: var(--surface-2);
  color: var(--text);
}
.toc-link.active {
  background: var(--accent-bg);
  color: var(--accent);
  border-left-color: var(--accent);
  font-weight: 600;
}
.toc-time {
  font-family: var(--font-mono);
  font-size: 0.72rem;
  color: var(--text-dim);
  flex-shrink: 0;
}

.chapter-main {
  min-width: 0;
}

.chapter-header {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 26px 28px;
  margin-bottom: 24px;
  box-shadow: var(--shadow-sm);
}
.breadcrumbs {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  font-size: 0.84rem;
  color: var(--text-dim);
  margin-bottom: 10px;
}
.chapter-header h1 {
  margin: 0 0 6px;
  font-size: clamp(1.55rem, 2.6vw, 2.15rem);
  line-height: 1.25;
}
.chapter-en-title {
  color: var(--text-dim);
  font-size: 0.94rem;
  margin: 0 0 14px;
}
.one-liner {
  background: var(--accent-bg);
  border-left: 3px solid var(--accent);
  padding: 10px 14px;
  border-radius: 0 8px 8px 0;
  font-size: 0.98rem;
  margin-bottom: 14px;
}
.chapter-overview {
  color: var(--text-muted);
  margin: 0 0 18px;
}
.chapter-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

/* Prereq / Leads-to & Key Formulas Summary */
.meta-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(260px, 1fr));
  gap: 14px;
  margin-bottom: 24px;
}
.info-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 16px 18px;
}
.info-card h3 {
  margin: 0 0 10px;
  font-size: 0.95rem;
  color: var(--text);
}
.rel-item {
  font-size: 0.88rem;
  margin-bottom: 6px;
  color: var(--text-muted);
}
.formula-summary-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 12px;
  margin-bottom: 26px;
}
.formula-chip {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  overflow-x: auto;
}
.formula-chip-lbl {
  font-size: 0.8rem;
  color: var(--accent);
  font-weight: 600;
  margin-bottom: 4px;
}

/* Lesson Section Card */
.lesson-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px 26px;
  margin-bottom: 24px;
  box-shadow: var(--shadow-sm);
}
.sec-head {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  padding-bottom: 12px;
  margin-bottom: 16px;
  border-bottom: 1px solid var(--border);
}
.sec-head h2 {
  margin: 0;
  font-size: 1.28rem;
  line-height: 1.35;
}
.sec-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.time-pill {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  padding: 3px 9px;
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

/* Blocks */
.sec-blocks p {
  margin: 0 0 13px;
}
.sec-list {
  margin: 0 0 14px;
  padding-left: 22px;
}
.sec-list li {
  margin-bottom: 6px;
}
.callout {
  border-radius: var(--radius-md);
  padding: 13px 16px;
  margin: 14px 0;
  border: 1px solid var(--border);
}
.callout-title {
  font-weight: 700;
  font-size: 0.9rem;
  margin-bottom: 4px;
}
.callout-formula {
  background: var(--bg-elev);
  border-left: 4px solid var(--c-k);
}
.callout-formula .callout-title { color: var(--c-k); }
.callout-intuition {
  background: var(--gold-bg);
  border-color: rgba(247, 209, 84, 0.3);
  border-left: 4px solid var(--c-y);
}
.callout-intuition .callout-title { color: var(--c-y); }
.callout-pitfall {
  background: var(--danger-bg);
  border-color: rgba(252, 98, 85, 0.3);
  border-left: 4px solid var(--c-j);
}
.callout-pitfall .callout-title { color: var(--c-j); }
.callout-crosslink {
  background: var(--success-bg);
  border-color: rgba(131, 193, 103, 0.3);
  border-left: 4px solid var(--c-i);
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

/* Keyframes Gallery inside Section */
.keyframes-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 14px;
  margin-top: 18px;
}
.kf-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  cursor: zoom-in;
  transition: border-color 0.16s, transform 0.16s;
}
.kf-card:hover {
  border-color: var(--accent);
  transform: translateY(-2px);
}
.kf-img-box {
  position: relative;
  aspect-ratio: 16 / 9;
  background: #080a0f;
  overflow: hidden;
}
.kf-img-box img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
/* Dark theme adaptation for 15-quick-eigen light SVGs */
.kf-img-box.svg-frame,
.ep-thumb-wrap.svg-frame {
  background: #ffffff;
}
html:not([data-theme="light"]) .light-svg-figure {
  filter: invert(0.925) hue-rotate(180deg) saturate(1.18);
}
html:not([data-theme="light"]) .kf-img-box.svg-frame,
html:not([data-theme="light"]) .ep-thumb-wrap.svg-frame {
  background: #131313;
}
.kf-ts {
  position: absolute;
  bottom: 8px;
  right: 8px;
  padding: 2px 7px;
  border-radius: 5px;
  background: rgba(10, 12, 18, 0.84);
  color: #e7ecf5;
  font-family: var(--font-mono);
  font-size: 0.74rem;
  border: 1px solid rgba(255,255,255,0.16);
}
.kf-cap {
  padding: 10px 12px;
  font-size: 0.86rem;
  color: var(--text-muted);
  line-height: 1.5;
}
.kf-focus {
  display: block;
  margin-top: 4px;
  font-size: 0.76rem;
  color: var(--text-dim);
}

/* Bilingual Transcript Drawer */
.transcript-drawer {
  margin-top: 16px;
  border-top: 1px dashed var(--border);
  padding-top: 12px;
}
.transcript-list {
  display: none;
  margin-top: 10px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  max-height: 440px;
  overflow-y: auto;
}
.transcript-drawer.open .transcript-list {
  display: block;
}
.tr-row {
  display: grid;
  grid-template-columns: 74px 1fr;
  gap: 12px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 0.88rem;
}
.tr-row:last-child {
  border-bottom: none;
}
.tr-time {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  color: var(--accent);
  white-space: nowrap;
  padding-top: 2px;
}
.tr-zh {
  color: var(--text);
  margin-bottom: 3px;
}
.tr-en {
  color: var(--text-dim);
  font-size: 0.81rem;
  line-height: 1.45;
}

/* Interactive Lab Container */
.lab-panel {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  padding: 22px 24px;
  margin: 26px 0;
  box-shadow: var(--shadow-md);
}
.lab-head {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 14px;
}
.lab-head h3 {
  margin: 0;
  font-size: 1.15rem;
}
.lab-workspace {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(250px, 0.85fr);
  gap: 18px;
  align-items: start;
}
.lab-canvas-wrap {
  position: relative;
  background: #090b10;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
  aspect-ratio: 4 / 3;
}
.lab-canvas-wrap canvas {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
  cursor: crosshair;
}
.lab-controls {
  display: flex;
  flex-direction: column;
  gap: 12px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 14px;
}
.lab-ctrl-group label {
  display: flex;
  justify-content: space-between;
  font-size: 0.84rem;
  color: var(--text-muted);
  margin-bottom: 4px;
}
.lab-ctrl-group input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}
.lab-presets {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.lab-readout {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-family: var(--font-mono);
  font-size: 0.82rem;
  line-height: 1.65;
  color: var(--text);
}

/* Quiz Section */
.quiz-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 24px 26px;
  margin: 26px 0;
}
.quiz-item {
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  margin-bottom: 12px;
  background: var(--bg-elev);
  overflow: hidden;
}
.quiz-q {
  padding: 13px 16px;
  font-weight: 600;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}
.quiz-a {
  display: none;
  padding: 0 16px 14px;
  border-top: 1px dashed var(--border);
  padding-top: 12px;
  color: var(--text-muted);
}
.quiz-item.open .quiz-a {
  display: block;
}

/* Chapter Bottom Prev/Next Nav */
.chapter-pager {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
  margin-top: 28px;
}
.pager-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 14px 18px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  text-decoration: none !important;
  transition: border-color 0.16s, background 0.16s;
}
.pager-card:hover {
  border-color: var(--accent);
  background: var(--surface-2);
}
.pager-dir {
  font-size: 0.78rem;
  color: var(--text-dim);
}
.pager-title {
  font-weight: 600;
  color: var(--text);
}
.pager-next {
  text-align: right;
}

/* Cheatsheet */
.cheat-table-wrap {
  overflow-x: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  margin-bottom: 28px;
}
.cheat-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}
.cheat-table th, .cheat-table td {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
}
.cheat-table th {
  background: var(--surface-2);
  color: var(--text);
  font-weight: 600;
}

/* Search Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(6, 8, 12, 0.72);
  backdrop-filter: blur(5px);
  display: none;
  align-items: flex-start;
  justify-content: center;
  padding: 72px 16px 24px;
}
.modal-backdrop.open {
  display: flex;
}
.search-box {
  width: min(720px, 100%);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}
.search-input-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.search-input-row input {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text);
  font-size: 1rem;
  font-family: inherit;
}
.search-results {
  max-height: 60vh;
  overflow-y: auto;
  padding: 8px;
}
.search-item {
  display: block;
  padding: 10px 12px;
  border-radius: 9px;
  text-decoration: none !important;
  color: var(--text);
}
.search-item:hover {
  background: var(--surface-2);
}
.search-item-meta {
  font-size: 0.76rem;
  color: var(--accent);
  margin-bottom: 2px;
}
.search-item-snippet {
  font-size: 0.84rem;
  color: var(--text-muted);
}

/* Lightbox Modal */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 220;
  background: rgba(5, 7, 10, 0.92);
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.lightbox.open {
  display: flex;
}
.lightbox-img {
  max-width: 92vw;
  max-height: 76vh;
  border-radius: 10px;
  border: 1px solid rgba(255,255,255,0.16);
  background: #0b0e14;
}
.lightbox-img.svg-light {
  background: #ffffff;
}
html:not([data-theme="light"]) .lightbox-img.svg-light {
  filter: invert(0.925) hue-rotate(180deg) saturate(1.18);
  background: #131313;
}
.lightbox-bar {
  margin-top: 14px;
  display: flex;
  align-items: center;
  gap: 14px;
  color: #e7ecf5;
  max-width: 900px;
  text-align: center;
}

/* Footer */
.site-footer {
  border-top: 1px solid var(--border);
  padding: 28px 20px;
  text-align: center;
  color: var(--text-dim);
  font-size: 0.86rem;
}

/* Mobile Responsive */
@media (max-width: 960px) {
  .hero-inner {
    grid-template-columns: 1fr;
  }
  .chapter-shell {
    grid-template-columns: 1fr;
  }
  .chapter-sidebar {
    position: static;
    max-height: none;
  }
  .lab-workspace {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .topbar {
    height: auto;
    min-height: var(--nav-h);
  }
  .topbar-inner {
    padding: 8px 12px;
    flex-wrap: wrap;
    gap: 8px;
  }
  .brand {
    font-size: 0.94rem;
    flex: 1 1 auto;
    min-width: 0;
  }
  .brand span:last-child {
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .nav-links {
    order: 3;
    width: 100%;
    overflow-x: auto;
    padding-bottom: 2px;
    gap: 4px;
  }
  .nav-link {
    padding: 4px 10px;
    font-size: 0.84rem;
  }
  .nav-actions {
    flex-shrink: 0;
    gap: 6px;
  }
  .nav-actions .btn {
    padding: 5px 8px;
    font-size: 0.78rem;
  }
  .nav-link span.hide-sm, .hide-sm, .kbd-hint {
    display: none;
  }
  .hero {
    padding: 28px 14px 26px;
  }
  .container {
    padding: 24px 14px 48px;
  }
  .chapter-shell {
    padding: 16px 12px 60px;
    gap: 18px;
  }
  .chapter-header, .lesson-section, .lab-panel, .quiz-card {
    padding: 16px 14px;
  }
  .keyframes-grid, .cards-grid, .meta-grid, .formula-summary-grid {
    grid-template-columns: 1fr;
  }
  .chapter-pager {
    grid-template-columns: 1fr;
  }
  .tr-row {
    grid-template-columns: 1fr;
    gap: 4px;
  }
}

/* Print Styles for Cheatsheet & Chapters */
@media print {
  .topbar, .chapter-sidebar, .chapter-toolbar, .transcript-drawer, .lab-panel, .chapter-pager, .site-footer {
    display: none !important;
  }
  body {
    background: #fff;
    color: #000;
  }
  .lesson-section, .chapter-header, .info-card, .cheat-table-wrap {
    box-shadow: none;
    border-color: #ccc;
    break-inside: avoid;
  }
}
