/* ------------------------------------------------------------------ tokens
   Same structure as spm-math.com; accent shifted from teal to cyan-blue. */
:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --text: #16202a;
  --muted: #5b6875;
  --border: #d8dee5;
  --accent: #0e7490;
  --accent-strong: #155e75;
  --on-accent: #ffffff;
  --accent-soft: #d6f1f8;
  --focus: #1d4ed8;
  --good: #15803d;
  --warn: #b45309;
  --bad: #b91c1c;
  /* plot palette (colour-blind safe, distinguishable in both themes) */
  --c1: #0e7490;
  --c2: #c2410c;
  --c3: #7c3aed;
  --c4: #15803d;
  --c5: #be185d;
  --grid: #e3e8ee;
  --grid-strong: #c4ccd6;
  --axis: #3b4754;
  --plot-bg: #ffffff;
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.06), 0 6px 20px rgba(15, 23, 42, 0.06);
  --radius: 10px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1418; --surface: #182027; --surface-2: #212b34; --text: #e6ebf0; --muted: #9aa8b5; --border: #2d3944;
    --accent: #3cc4de; --accent-strong: #7ad9ec; --on-accent: #04222a; --accent-soft: #11373f; --focus: #7aa2ff;
    --good: #4ade80; --warn: #fbbf24; --bad: #f87171;
    --c1: #3cc4de; --c2: #fb923c; --c3: #a78bfa; --c4: #4ade80; --c5: #f472b6;
    --grid: #26313b; --grid-strong: #3a4753; --axis: #b6c2ce; --plot-bg: #131a20;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 20px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1418; --surface: #182027; --surface-2: #212b34; --text: #e6ebf0; --muted: #9aa8b5; --border: #2d3944;
  --accent: #3cc4de; --accent-strong: #7ad9ec; --on-accent: #04222a; --accent-soft: #11373f; --focus: #7aa2ff;
  --good: #4ade80; --warn: #fbbf24; --bad: #f87171;
  --c1: #3cc4de; --c2: #fb923c; --c3: #a78bfa; --c4: #4ade80; --c5: #f472b6;
  --grid: #26313b; --grid-strong: #3a4753; --axis: #b6c2ce; --plot-bg: #131a20;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 6px 20px rgba(0, 0, 0, 0.35);
  color-scheme: dark;
}

/* ------------------------------------------------------------------- base */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; scroll-padding-top: 76px; }
body { margin: 0; background: var(--bg); color: var(--text); font: 15px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent-strong); }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 4px; }
.skip { position: absolute; left: -999px; top: 8px; background: var(--surface); padding: 8px 12px; border-radius: 6px; z-index: 50; }
.skip:focus { left: 8px; }
h1, h2, h3, h4, p { margin: 0; }
.katex { font-size: 1.05em; }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } * { transition: none !important; animation: none !important; } }

/* ----------------------------------------------------------------- header */
.app-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--border); position: sticky; top: 0; z-index: 30; }
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-link { display: flex; align-items: center; gap: 12px; color: inherit; text-decoration: none; min-width: 0; }
.logo { color: var(--accent); display: grid; }
.brand h1 { font-size: 17px; line-height: 1.2; font-weight: 650; letter-spacing: -0.01em; }
.tag { font-size: 12.5px; color: var(--muted); }
.header-tools { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.icon-btn { width: 36px; height: 36px; display: grid; place-items: center; border-radius: 8px; background: var(--surface-2); border: 1px solid var(--border); }
.icon-btn:hover { border-color: var(--accent); }
.fb-btn { width: auto; display: flex; gap: 6px; padding: 0 12px; font-size: 13px; color: inherit; text-decoration: none; align-items: center; }
.menu-btn { display: none; }
.i-sun { display: none; }
:root[data-theme="dark"] .i-sun { display: block; }
:root[data-theme="dark"] .i-moon { display: none; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .i-sun { display: block; }
  :root:not([data-theme="light"]) .i-moon { display: none; }
}

/* segmented control (also used inside lessons) */
.seg { display: inline-flex; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 2px; gap: 2px; flex-wrap: wrap; max-width: 100%; }
.seg button { border: 0; background: transparent; padding: 6px 12px; border-radius: 6px; color: var(--muted); font-weight: 550; min-height: 32px; }
.seg button:hover { color: var(--text); }
.seg button[aria-pressed="true"] { background: var(--accent); color: var(--on-accent); }

/* ----------------------------------------------------------------- layout */
.layout { display: grid; grid-template-columns: minmax(260px, 320px) minmax(0, 1fr); gap: 20px; padding: 20px; max-width: 1500px; margin: 0 auto; align-items: start; }
.sidebar { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; position: sticky; top: 68px; max-height: calc(100vh - 88px); display: flex; flex-direction: column; min-height: 0; }
.side-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.side-head h2 { font-size: 15px; font-weight: 650; }
.count { font-size: 12.5px; color: var(--accent-strong); font-weight: 600; }
.search { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); }
.side-actions { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 8px 0 6px; }
.link-btn { border: 0; background: none; padding: 2px 0; color: var(--accent-strong); font-size: 12.5px; font-weight: 600; }
.link-btn:hover { text-decoration: underline; }
.main { min-width: 0; outline: none; }

/* sidebar tree */
.tree { overflow: auto; margin: 0 -6px; padding: 0 6px; flex: 1; min-height: 0; }
.tree details { margin: 0; }
.tree summary { list-style: none; display: flex; align-items: center; gap: 6px; padding: 5px 4px; border-radius: 6px; cursor: pointer; user-select: none; }
.tree summary::-webkit-details-marker { display: none; }
.tree summary:hover { background: var(--surface-2); }
.tree summary::before { content: ""; width: 7px; height: 7px; border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted); transform: rotate(-45deg); transition: transform 0.12s; margin: 0 4px 0 2px; flex-shrink: 0; }
.tree details[open] > summary::before { transform: rotate(45deg); }
.tree .form-sum { font-weight: 650; font-size: 14px; }
.tree .form-body { margin-left: 8px; border-left: 1px solid var(--border); padding-left: 6px; }
.tree a.row { display: flex; align-items: flex-start; gap: 7px; padding: 4px 6px; border-radius: 6px; font-size: 13.5px; line-height: 1.35; color: var(--text); text-decoration: none; }
.tree a.row:hover { background: var(--surface-2); }
.tree a.row[aria-current="page"] { background: var(--accent-soft); color: var(--accent-strong); font-weight: 600; }
.tree .no { color: var(--muted); font-variant-numeric: tabular-nums; min-width: 1.6em; flex-shrink: 0; }
.tree .secs { margin: 0 0 4px 30px; display: grid; gap: 1px; border-left: 1px solid var(--border); padding-left: 8px; }
.tree .secs a { font-size: 12.5px; color: var(--muted); text-decoration: none; padding: 2px 4px; border-radius: 4px; line-height: 1.35; }
.tree .secs a:hover { color: var(--text); background: var(--surface-2); }
.tree .empty { color: var(--muted); font-size: 13px; padding: 8px 4px; }
.tree .hide { display: none; }
.badge { font-size: 10.5px; padding: 0 6px; border-radius: 99px; border: 1px solid var(--border); color: var(--muted); white-space: nowrap; }
.scrim { display: none; }

/* ------------------------------------------------------------------ pages */
.page-head { margin-bottom: 16px; }
.crumbs { font-size: 12.5px; color: var(--muted); margin-bottom: 4px; }
.crumbs a { color: var(--muted); text-decoration: none; }
.crumbs a:hover { text-decoration: underline; }
.page-head h2 { font-size: 26px; line-height: 1.2; letter-spacing: -0.015em; font-weight: 700; }
.lede { color: var(--muted); font-size: 15.5px; margin-top: 6px; max-width: 70ch; }
.chapter-nav { display: flex; justify-content: space-between; gap: 12px; margin-top: 18px; flex-wrap: wrap; }
.chapter-nav a { display: inline-flex; flex-direction: column; gap: 2px; padding: 10px 14px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); text-decoration: none; color: var(--text); max-width: 48%; }
.chapter-nav a:hover { border-color: var(--accent); }
.chapter-nav small { color: var(--muted); font-size: 12px; }
.chapter-nav .next { text-align: right; margin-left: auto; }

.sec { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; margin-bottom: 16px; box-shadow: var(--shadow); }
.sec > h3 { font-size: 18px; font-weight: 650; letter-spacing: -0.01em; margin-bottom: 6px; }
.sec p { margin: 8px 0; max-width: 78ch; }
.sec .std { display: inline-block; font-size: 11.5px; color: var(--muted); border: 1px solid var(--border); border-radius: 99px; padding: 0 8px; margin-left: 8px; vertical-align: middle; font-weight: 500; }

.callout { border-left: 4px solid var(--accent); background: var(--accent-soft); border-radius: 0 8px 8px 0; padding: 10px 14px; margin: 12px 0; max-width: 78ch; }
.callout.warn { border-left-color: var(--warn); background: color-mix(in srgb, var(--warn) 12%, var(--surface)); }
.callout.good { border-left-color: var(--good); background: color-mix(in srgb, var(--good) 12%, var(--surface)); }
.callout > b:first-child { display: block; font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--accent-strong); margin-bottom: 2px; }
.callout.warn > b:first-child { color: var(--warn); }
.callout.good > b:first-child { color: var(--good); }
.callout p { margin: 4px 0; }
.formula { padding: 8px 12px; background: var(--surface-2); border-radius: 8px; margin: 10px 0; overflow-x: auto; max-width: 100%; }

/* ---------------------------------------------------- interactive widgets */
.lab { display: grid; grid-template-columns: minmax(0, 1.55fr) minmax(250px, 1fr); gap: 16px; align-items: start; margin: 12px 0; }
.lab.stack { grid-template-columns: minmax(0, 1fr); }
.lab > * { min-width: 0; }
.panel { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 12px 14px; display: grid; gap: 10px; align-content: start; }
.panel h4 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); font-weight: 650; }
.plot-wrap { position: relative; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--plot-bg); touch-action: pan-y; }
.plot-wrap svg { display: block; width: 100%; height: auto; user-select: none; -webkit-user-select: none; }
.plot-wrap.draggable svg { touch-action: none; }
.plot-tools { position: absolute; right: 6px; top: 6px; display: flex; gap: 4px; }
.plot-tools button { width: 28px; height: 28px; border-radius: 6px; border: 1px solid var(--border); background: var(--surface); color: var(--text); display: grid; place-items: center; font-weight: 700; line-height: 1; opacity: 0.85; }
.plot-tools button:hover { border-color: var(--accent); opacity: 1; }
.plot-cap { font-size: 12.5px; color: var(--muted); margin-top: 6px; }
.plot-hint { position: absolute; left: 8px; bottom: 6px; font-size: 11px; color: var(--muted); pointer-events: none; background: color-mix(in srgb, var(--plot-bg) 80%, transparent); padding: 0 4px; border-radius: 4px; }
.handle { cursor: grab; }
.handle:active { cursor: grabbing; }

.param { display: grid; grid-template-columns: auto minmax(0, 1fr) 5.2em; gap: 8px; align-items: center; }
.param label { font-weight: 600; min-width: 1.4em; }
.param input[type="range"] { width: 100%; accent-color: var(--accent); min-height: 28px; }
.param input[type="number"] { width: 100%; padding: 4px 6px; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); text-align: right; font-variant-numeric: tabular-nums; }
.field { display: grid; gap: 4px; }
.field > label, .field > .lbl { font-size: 12.5px; font-weight: 600; color: var(--muted); }
.field input[type="text"], .field input[type="number"], .field select { width: 100%; padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.field input.bad { border-color: var(--bad); }
.field .err { color: var(--bad); font-size: 12.5px; min-height: 1.2em; }
.row-wrap { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.check { display: flex; gap: 8px; align-items: center; font-size: 14px; cursor: pointer; }
.check input { accent-color: var(--accent); width: 16px; height: 16px; }
.btn { display: inline-flex; align-items: center; gap: 8px; padding: 7px 14px; border-radius: 8px; border: 1px solid var(--border); background: var(--surface); font-weight: 600; min-height: 36px; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--on-accent); border-color: var(--accent); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { border: 1px solid var(--border); background: var(--surface); border-radius: 99px; padding: 3px 10px; font-size: 13px; }
.chip:hover { border-color: var(--accent); }
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent-strong); font-weight: 600; }

.readout { display: grid; gap: 4px; font-variant-numeric: tabular-nums; }
.readout .r { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; padding: 3px 0; border-bottom: 1px dashed var(--border); }
.readout .r:last-child { border-bottom: 0; }
.readout .k { color: var(--muted); font-size: 13px; }
.readout .v { font-weight: 650; text-align: right; }
.eq-display { font-size: 1.15em; padding: 6px 2px; overflow-x: auto; }
.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.tbl { border-collapse: collapse; font-variant-numeric: tabular-nums; max-width: 100%; }
.tbl th, .tbl td { border: 1px solid var(--border); padding: 4px 10px; text-align: center; }
.tbl th { background: var(--surface-2); font-weight: 600; }
.tbl-wrap { overflow-x: auto; max-width: 100%; }
.tag-good { color: var(--good); font-weight: 650; }
.tag-bad { color: var(--bad); font-weight: 650; }

/* mini quiz */
.quiz { border: 1px dashed var(--border); border-radius: 8px; padding: 12px 14px; margin: 14px 0 4px; background: color-mix(in srgb, var(--accent-soft) 40%, var(--surface)); }
.quiz h4 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em; color: var(--accent-strong); margin-bottom: 6px; }
.quiz .q { margin-bottom: 8px; }
.quiz .opts { display: flex; flex-wrap: wrap; gap: 8px; }
.quiz .opts button { padding: 6px 14px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); min-height: 36px; }
.quiz .opts button:hover:not(:disabled) { border-color: var(--accent); }
.quiz .opts button.right { border-color: var(--good); background: color-mix(in srgb, var(--good) 15%, var(--surface)); }
.quiz .opts button.wrong { border-color: var(--bad); background: color-mix(in srgb, var(--bad) 12%, var(--surface)); }
.quiz .fb { margin-top: 8px; font-size: 14px; min-height: 1.4em; }
.quiz .ans-row { display: flex; gap: 8px; flex-wrap: wrap; }
.quiz .ans-row input { padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); width: 10em; }

/* home */
.hero { background: linear-gradient(135deg, var(--accent-soft), var(--surface)); border: 1px solid var(--border); border-radius: var(--radius); padding: 26px 24px; margin-bottom: 20px; }
.hero h2 { font-size: 30px; letter-spacing: -0.02em; line-height: 1.15; }
.hero p { margin-top: 8px; max-width: 62ch; color: var(--muted); font-size: 16px; }
.hero svg { max-width: 100%; height: auto; margin-top: 14px; }
.form-block { margin-bottom: 22px; }
.form-block > h3 { font-size: 18px; margin-bottom: 8px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 10px; }
.card { display: flex; flex-direction: column; gap: 4px; padding: 12px 14px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text); text-decoration: none; }
.card:hover { border-color: var(--accent); box-shadow: var(--shadow); }
.card .n { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.card .t { font-weight: 600; line-height: 1.3; }
.card .d { font-size: 13px; color: var(--muted); }
.soon { opacity: 0.6; }
.prose h3 { margin-top: 18px; }
.prose ul { padding-left: 20px; max-width: 78ch; }

.loading, .errbox { padding: 30px; text-align: center; color: var(--muted); }

/* sister-site links inside the drawer (header buttons are hidden on phones) */
.side-links { display: none; gap: 16px; padding-top: 10px; margin-top: 8px; border-top: 1px solid var(--border); font-size: 13px; }

/* --------------------------------------------------------------- responsive */
@media (max-width: 1000px) { .lab { grid-template-columns: minmax(0, 1fr); } }
@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); padding: 12px 16px; }
  .menu-btn { display: grid; }
  .sidebar { position: fixed; z-index: 40; left: 0; top: 0; bottom: 0; width: min(340px, 88vw); max-height: none; border-radius: 0 var(--radius) var(--radius) 0; transform: translateX(-102%); transition: transform 0.18s; }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .scrim { display: block; position: fixed; inset: 0; z-index: 35; background: rgba(0, 0, 0, 0.45); }
  .app-header { padding: 8px 16px; }
}
@media (max-width: 760px) {
  .fb-btn span { display: none; }
  .fb-btn { padding: 0; width: 36px; justify-content: center; }
  .tag { display: none; }
  .page-head h2 { font-size: 22px; }
}
@media (max-width: 520px) {
  .fb-btn.sister { display: none; }
  .side-links { display: flex; }
  .brand h1 { font-size: 15px; }
  .sec { padding: 14px 14px; }
  .param { grid-template-columns: auto minmax(0, 1fr) 4.6em; }
}
@media (max-width: 420px) {
  .header-tools { gap: 5px; }
  .header-tools .seg button { padding: 6px 8px; }
}
