/* Theme surfaces are separate from content and progress. No global SVG rules. */
:root { color-scheme: light; }
.theme-toggle {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  flex: 0 0 auto; min-height: 38px; padding: 6px 11px; white-space: nowrap;
}
.theme-toggle[hidden] { display: none; }
.theme-toggle > svg { width: 18px; height: 18px; flex: 0 0 18px; }
.theme-toggle .theme-moon { display: none; }
[data-theme="dark"] .theme-toggle .theme-sun { display: none; }
[data-theme="dark"] .theme-toggle .theme-moon { display: block; }
.theme-status { position: fixed; bottom: 18px; right: 20px; z-index: 150; max-width: 340px; }
.theme-status:empty { display: none; }
.theme-status:not(:empty) { background: #193b52; color: #e3eef5; padding: 12px 18px; border-radius: 8px; font-size: 14px; }
.topbar .theme-toggle { margin-left: auto; }
@media (max-width: 1120px) { .topbar .top-meta { display: none; } }

@media (max-width: 600px) {
  .topbar .header-progress { display: none; }
  .theme-toggle [data-theme-label] { display: none; }
}

@media screen {
  :root[data-theme="dark"] {
    color-scheme: dark;
    --bg: #101820; --ink: #e2eaf0; --muted: #a8bac8; --line: #425664;
    --teal: #80dace; --blue: #95b7ff; --orange: #f1bc80;
    --white: #192630; --light: #203b38;
    --surface: #192630; --raised: #22323f; --soft: #20313f;
    --green-surface: #1c3832; --green-ink: #a5e5c5;
    --warm-surface: #3a2f23; --warm-ink: #f1c48d;
    --blue-surface: #20334c; --blue-ink: #afcbff;
    --chart-teal: #80dace; --chart-blue: #9bbcff; --chart-orange: #f3b680;
  }
  [data-theme="dark"] body { background: var(--bg); color: var(--ink); }
  [data-theme="dark"] :is(.topbar,.dialog-head) { background: #15212b; }
  [data-theme="dark"] .sidebar { background: #122331; border-right: 1px solid #304552; }
  [data-theme="dark"] :is(#topic-nav,#past-exam-nav) a.active { background: #244a44; color: #bcefe0; }
  [data-theme="dark"] .sidebar #topic-nav a.active.topic-done .number:after { color: #bcefe0; }
  [data-theme="dark"] :is(.panel,.problem,.resource,.map-item,.preparation,.table-wrap,
    .practice-empty,.review-item,.cheat-card,.diagnostic-card,.past-paper-card,
    .vg-player-panel,.vg-metadata span,.vg-chapters a,.vg-symbols div,dialog,.skip) {
    background: var(--surface); color: var(--ink); border-color: var(--line);
  }
  [data-theme="dark"] :is(.problem-head,th,.constants-box:not(.on-dark),.notation-guide,
    .before-video,.vg-terms-row span) { background: var(--raised); color: var(--ink); border-color: var(--line); }
  [data-theme="dark"] :is(.concept p,.vocabulary-chips span,.glossary-item h3 span,
    .diagnostic-card small,.exam-date span,.completion-panel span,.vg-source-link,
    .solution-diagram-note,.solution-diagram > figcaption > span,.header-progress) { color: var(--muted); }
  [data-theme="dark"] :is(.formula,.vg-formula,.textbook-bridge,.problem-learning-note,
    .solution-diagram-formulas,.vg-visual) { background: var(--soft); color: var(--ink); border-color: #64849b; }
  [data-theme="dark"] .step-no { background: #294254; color: #d5e7f2; }
  [data-theme="dark"] .hint-reveal { background: var(--blue-surface); border-color: #95b7ff; }
  [data-theme="dark"] .hint-reveal small { color: var(--blue-ink); }
  [data-theme="dark"] :is(.answer,.check-panel,.vg-check,.vg-watch-task,.term-explanation,
    .vocabulary-panel,.resume-card,.visual-feedback,.phase-readout,.vg-empty,.exam-scope.confirmed) {
    background: var(--green-surface); color: var(--ink); border-color: #578a7e;
  }
  [data-theme="dark"] .answer strong { color: var(--green-ink); }
  [data-theme="dark"] :is(.notice-box,.problem-note,.exam-scope,.past-paper-note,.past-source-note,
    .vg-link-row a,.vg-homework-wrap > summary) {
    background: var(--warm-surface); color: var(--warm-ink); border-color: #aa8151;
  }
  [data-theme="dark"] :is(.vg-link-row strong,.vg-bridge-label,.kind-homework,
    .problem-homework .problem-label,.problem-label.textbook) { color: var(--warm-ink); }
  [data-theme="dark"] :is(.kind-worksheet,.problem-worksheet .problem-label) { color: var(--teal); }
  [data-theme="dark"] .problem-homework { border-left-color: #f1bc80; }
  [data-theme="dark"] .problem-worksheet { border-left-color: #80dace; }
  [data-theme="dark"] .problem-lecture { border-left-color: #b9a9eb; }
  [data-theme="dark"] .problem-exam { border-left-color: #9bbcff; }
  [data-theme="dark"] .problem-exam .problem-label { color: #afcbff; }
  [data-theme="dark"] :is(.vg-time,.vg-values strong,.solved-button,.hint-reset) { color: var(--teal); }
  [data-theme="dark"] .cheatsheet-jumps { background: #101820f5; }
  [data-theme="dark"] :is(.related,.glossary-item,.visual-results,.visual-assumptions) { border-color: var(--line); }

  /* Controls keep distinct resting, selected, correct and incorrect states. */
  [data-theme="dark"] button { background: var(--raised); color: var(--ink); border-color: #718797; }
  [data-theme="dark"] button:hover { background: #2b444d; border-color: var(--teal); }
  [data-theme="dark"] :is(.visual-controls select,#glossary-search,.cheatsheet-jumps a,.exam-jumps a) {
    color: var(--ink); background: var(--raised); border-color: #718797;
  }
  [data-theme="dark"] :is(.vocabulary-chips button,.vg-symbols div) { color: var(--ink); }
  [data-theme="dark"] :is(.formula-term.selected,.solution-toggle[aria-expanded="true"],
    .diagnostic-card button[aria-pressed="true"],.vg-check button[aria-pressed="true"],
    .vg-chapters a.active,.solved-button.done,.complete-button.done,.choices .correct) {
    background: #264b40; color: #d3f5e3; border-color: #80dace;
  }
  [data-theme="dark"] .choices .incorrect { background: #4c2c2a; color: #ffded5; border-color: #e7a18c; }
  [data-theme="dark"] :is(.vg-tabs button[aria-pressed="true"],.vg-clip-tabs button[aria-pressed="true"],
    .primary,.vg-next,.complete-button) { background: #295d59; color: #effbf8; border-color: #80bcb1; }
  [data-theme="dark"] :is(.primary,.vg-next,.complete-button):hover { background: #356f66; color: #fff; }
  [data-theme="dark"] .complete-button.done { background: #264b40; color: #d3f5e3; }
  [data-theme="dark"] :is(.original-link,.hint-reset,.dialog-head button,.sidebar-toggle) { background: transparent; color: var(--teal); }
  [data-theme="dark"] :is(.vocab-inline:hover,.vocab-inline:focus,.pdf-link:hover) { background: #264b40; color: #d3f5e3; }
  [data-theme="dark"] .video-launch { background: #102b43; color: #e3eef5; }
  [data-theme="dark"] .video-launch:hover { background: #1c4359; }
  [data-theme="dark"] .lab { background: #102b43; color: #e3eef5; }
  [data-theme="dark"] .lab button { background: #25465c; border-color: #648295; color: #e1f4f0; }
  [data-theme="dark"] .lab button:hover { background: #315b70; }
  [data-theme="dark"] :focus-visible { outline-color: #f1bd70; }
  [data-theme="dark"] .header-progress progress::-webkit-progress-bar { background: #344c50; }
  [data-theme="dark"] .header-progress progress::-webkit-progress-value { background: #80dace; }
  [data-theme="dark"] .header-progress progress::-moz-progress-bar { background: #80dace; }
  [data-theme="dark"] ::selection { background: #356f66; color: #fff; }

  /* Analytic charts, scoped so MathJax keeps its own currentColor and size. */
  [data-theme="dark"] .vg-plot text { fill: var(--ink); }
  [data-theme="dark"] .vg-plot .grid { stroke: #718797; }
  [data-theme="dark"] .vg-plot .curve { stroke: var(--chart-teal); }
  [data-theme="dark"] .vg-plot .second { stroke: var(--chart-orange); }
  [data-theme="dark"] .vg-visual input[type="range"] { accent-color: var(--chart-teal); }
  [data-theme="dark"] .vg-plot [fill="#087579"] { fill: var(--chart-teal); }
  [data-theme="dark"] .vg-plot [stroke="#087579"] { stroke: var(--chart-teal); }
  [data-theme="dark"] .vg-plot [fill="#9b5d27"] { fill: var(--chart-orange); }
  [data-theme="dark"] .vg-plot [fill="#173d53"] { fill: #d6ebfa; }
  [data-theme="dark"] .teaching-chart .grid-line { stroke: #435969; }
  [data-theme="dark"] .teaching-chart :is(.axis,.reference-path,.cursor-line) { stroke: #92a9b9; }
  [data-theme="dark"] .teaching-chart :is(.active-path,.cold-curve) { stroke: var(--chart-teal); }
  [data-theme="dark"] .teaching-chart .hot-curve { stroke: var(--chart-orange); }
  [data-theme="dark"] .teaching-chart :is(.current-state,.cold-point) { fill: var(--chart-teal); stroke: var(--surface); }
  [data-theme="dark"] .teaching-chart .hot-point { fill: var(--chart-orange); stroke: var(--surface); }
  [data-theme="dark"] .teaching-chart .piston { stroke: #c6d8e5; }
  [data-theme="dark"] .solution-diagram { background: var(--surface); color: var(--ink); border-color: var(--line); }
  [data-theme="dark"] .solution-diagram-swatch[data-leg="teal"] { background: var(--chart-teal); }
  [data-theme="dark"] .solution-diagram-swatch[data-leg="blue"] { background: var(--chart-blue); }
  [data-theme="dark"] .solution-diagram-swatch[data-leg="orange"] { background: var(--chart-orange); }
  [data-theme="dark"] .solution-plot [fill="#19394d"] { fill: var(--ink); }
  [data-theme="dark"] .solution-plot [stroke="#19394d"] { stroke: var(--ink); }
  [data-theme="dark"] .solution-plot [fill="#557080"] { fill: var(--muted); }
  [data-theme="dark"] .solution-plot [stroke="#d9e3e7"] { stroke: #607889; }
  [data-theme="dark"] .solution-plot [fill="#087f83"] { fill: var(--chart-teal); }
  [data-theme="dark"] .solution-plot [stroke="#087f83"] { stroke: var(--chart-teal); }
  [data-theme="dark"] .solution-plot [fill="#356db1"] { fill: var(--chart-blue); }
  [data-theme="dark"] .solution-plot [stroke="#356db1"] { stroke: var(--chart-blue); }
  [data-theme="dark"] .solution-plot [fill="#c85f21"] { fill: var(--chart-orange); }
  [data-theme="dark"] .solution-plot [stroke="#c85f21"] { stroke: var(--chart-orange); }

  /* Source scans retain their real colors; keep a neutral paper frame. */
  [data-theme="dark"] :is(.question-figure button,.problem-image-button,.source-gallery button) {
    background: #d9dfe2; border-color: #718797; color: #19394d;
  }
  [data-theme="dark"] .source-gallery span { color: #19394d; }
  [data-theme="dark"] #notice { background: #264b40; color: #effbf8; border: 1px solid #80bcb1; }
}
@media print {
  :root { color-scheme: light; }
  .theme-toggle,.theme-status { display: none!important; }
}
