/* One Question Math
   Layout: one narrow column on faint graph paper. The current question is the only big thing on
   screen; tools (stuck, calculator) sit quietly below it; the learner's own findings collect last. */

@font-face { font-family: "Figtree"; src: url("../fonts/figtree-latin-400-normal.woff2") format("woff2"); font-weight: 400; font-style: normal; font-display: swap; }
@font-face { font-family: "Figtree"; src: url("../fonts/figtree-latin-500-normal.woff2") format("woff2"); font-weight: 500; font-style: normal; font-display: swap; }
@font-face { font-family: "Figtree"; src: url("../fonts/figtree-latin-600-normal.woff2") format("woff2"); font-weight: 600; font-style: normal; font-display: swap; }
@font-face { font-family: "Figtree"; src: url("../fonts/figtree-latin-700-normal.woff2") format("woff2"); font-weight: 700; font-style: normal; font-display: swap; }
@font-face { font-family: "STIX Two Text"; src: url("../fonts/stix-two-text-latin-400-normal.woff2") format("woff2"); font-weight: 400 500; font-style: normal; font-display: swap; }
@font-face { font-family: "STIX Two Text"; src: url("../fonts/stix-two-text-latin-600-normal.woff2") format("woff2"); font-weight: 600 700; font-style: normal; font-display: swap; }
@font-face { font-family: "STIX Two Text"; src: url("../fonts/stix-two-text-latin-400-italic.woff2") format("woff2"); font-weight: 400 700; font-style: italic; font-display: swap; }

:root {
  --paper: #F5F7FA;
  --surface: #FFFFFF;
  --ink: #18202C;
  --muted: #566073;
  --line: #DCE1E9;
  --grid: rgba(31, 92, 153, 0.055);
  --accent: #1F5C99;
  --accent-ink: #FFFFFF;
  --accent-soft: #E7EFF8;
  --good: #1B7149;
  --good-bg: #E5F2EA;
  --warn: #87500F;
  --warn-bg: #FAEEDD;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 8px 24px -12px rgba(16, 24, 40, 0.14);
  --font-math: "STIX Two Text", "Cambria Math", Cambria, Georgia, serif;
  --font-ui: "Figtree", system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --paper: #10141B; --surface: #171C25; --ink: #E6E9EF; --muted: #9AA4B5; --line: #29303D;
    --grid: rgba(127, 176, 230, 0.05); --accent: #82B3E8; --accent-ink: #0D1320; --accent-soft: #1B2638;
    --good: #72CB9E; --good-bg: #15281E; --warn: #E7B46E; --warn-bg: #2A2116;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 8px 24px -12px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  min-height: 100vh;
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 24px 24px;
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 16px;
  line-height: 1.5;
}
[hidden] { display: none !important; }
a { color: var(--accent); }
var { font-family: var(--font-math); font-style: italic; }
/* Exponents and subscripts (7¹⁰⁰, H₂O) without disturbing line spacing. */
sup, sub { font-size: 0.68em; line-height: 0; position: relative; vertical-align: baseline; font-style: normal; }
sup { top: -0.48em; }
sub { bottom: -0.22em; }
button { font: inherit; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.skip {
  position: absolute; left: 12px; top: -48px; z-index: 20;
  background: var(--accent); color: var(--accent-ink); padding: 8px 14px; border-radius: 8px; font-weight: 600;
}
.skip:focus { top: 12px; }

/* Header */
.site-header {
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--paper) 88%, transparent);
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}
.site-header-inner {
  max-width: 580px; margin: 0 auto; padding-block: 12px;
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
}
.brand { display: inline-flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none; font-weight: 700; font-size: 17px; letter-spacing: -0.01em; }
.brand-mark { width: 28px; height: 28px; flex: none; }
.brand-mark rect { fill: var(--accent); }
.brand-mark path { fill: none; stroke: var(--accent-ink); stroke-width: 2.4; stroke-linecap: round; }
.brand-mark circle { fill: var(--accent-ink); }
.site-header nav a { color: var(--muted); text-decoration: none; font-weight: 600; font-size: 15px; padding: 6px 2px; }
.site-header nav a:hover, .site-header nav a[aria-current="page"] { color: var(--ink); }

/* Page column */
.wrap {
  max-width: 580px; margin: 0 auto;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  padding-block: 28px 64px;
  display: flex; flex-direction: column; gap: 22px;
}
.wrap:focus { outline: none; }
.wrap > * { min-width: 0; }

.label { font-size: 12px; font-weight: 600; letter-spacing: 0.08em; text-transform: uppercase; color: var(--muted); }
.notice {
  margin: 0; padding: 12px 14px; border-radius: 10px;
  background: var(--warn-bg); color: var(--warn); font-size: 15px;
}

/* Library */
.intro { display: flex; flex-direction: column; gap: 8px; }
.intro h1 { font-family: var(--font-math); font-weight: 500; font-size: clamp(1.7rem, 5.5vw, 2.2rem); line-height: 1.2; margin: 0; text-wrap: balance; }
.intro p { margin: 0; color: var(--muted); max-width: 52ch; }
.wrap.wide { max-width: 980px; }
body:has(.wrap.wide) .site-header-inner { max-width: 948px; }
.wrap.wide .intro { max-width: 640px; }
.subject-jump {
  position: sticky; top: 0; z-index: 5;
  display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none;
  margin-inline: -4px; padding: 10px 4px;
  background: color-mix(in srgb, var(--paper) 92%, transparent); backdrop-filter: blur(6px);
}
.subject-jump::-webkit-scrollbar { display: none; }
.subject-jump a {
  flex: none; display: inline-flex; align-items: center; gap: 8px;
  padding: 7px 14px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); text-decoration: none; font-size: 15px; font-weight: 600;
}
.subject-jump a:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.subject-jump a span { color: var(--muted); font-weight: 500; font-size: 13px; }
.subjects { display: flex; flex-direction: column; gap: 40px; }
.subject { scroll-margin-top: 64px; }
.subject:focus { outline: none; }
.subject-heading { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; padding-bottom: 8px; border-bottom: 1px solid var(--line); }
.subject-heading h2 { margin: 0; font-size: 22px; font-weight: 700; letter-spacing: -0.02em; }
.subject-heading span { color: var(--muted); font-size: 14px; white-space: nowrap; }
.subject-note { margin: 10px 0 14px; color: var(--muted); font-size: 15px; }
.problem-list { list-style: none; margin: 0; padding: 0; display: grid; grid-template-columns: 1fr; gap: 12px; }
@media (min-width: 760px) {
  .problem-list { grid-template-columns: 1fr 1fr; gap: 14px; }
}
.problem-list li { display: flex; }
.problem-link {
  position: relative; flex: 1;
  display: flex; flex-direction: column; gap: 8px;
  padding: 18px 20px 18px 58px; border-radius: 14px; border: 1px solid var(--line);
  background: var(--surface); color: var(--ink); text-decoration: none; box-shadow: var(--shadow);
  transition: border-color .15s;
}
.problem-link:hover { border-color: color-mix(in srgb, var(--accent) 45%, var(--line)); }
.problem-link:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 2px; }
.problem-num {
  position: absolute; left: 18px; top: 18px;
  width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center;
  background: color-mix(in srgb, var(--accent) 12%, var(--surface)); color: var(--accent);
  font-size: 13px; font-weight: 700;
}
.problem-link h3 { margin: 0; font-size: 18px; font-weight: 700; letter-spacing: -0.01em; line-height: 26px; }
.problem-link .q { margin: 0; font-family: var(--font-math); font-size: 1.04rem; line-height: 1.45; flex: 1; }
.problem-meta { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 14px; color: var(--muted); }
.problem-meta b { font-family: var(--font-math); font-weight: 600; color: var(--ink); font-size: 1.02rem; }
.problem-meta .go { margin-left: auto; }
@media (max-width: 520px) {
  .subject-jump { position: static; flex-wrap: wrap; overflow: visible; padding-block: 0; background: none; backdrop-filter: none; }
  .subject-jump a { padding: 6px 12px; font-size: 14px; }
  .subject-jump a span { display: none; }
  .problem-link { padding-left: 20px; }
  .problem-num { display: none; }
}
.go { color: var(--accent); font-weight: 600; }

/* Problem header */
.problem { display: flex; flex-direction: column; gap: 10px; }
.back-link { align-self: flex-start; font-size: 14px; font-weight: 600; text-decoration: none; color: var(--muted); }
.back-link:hover { color: var(--ink); }
.problem-q { font-family: var(--font-math); font-size: 1.15rem; line-height: 1.45; margin: 0; text-wrap: pretty; }
.problem-a { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 10px; font-size: 15px; color: var(--muted); }
.problem-a strong { font-family: var(--font-math); font-size: 1.15rem; color: var(--ink); font-weight: 600; }

/* Progress: one segment per top-level question, because the path really is a sequence */
.progress { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 5px; }
.seg { height: 4px; border-radius: 2px; background: var(--line); transition: background .3s; }
.seg.done { background: var(--accent); }
.seg.now { background: var(--muted); }

/* The one question */
.card {
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  padding: 26px 24px 20px; display: flex; flex-direction: column; gap: 16px; box-shadow: var(--shadow);
}
.lead { margin: 0; font-size: 15px; color: var(--muted); }
.depth { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 13px; color: var(--muted); }
.chip {
  font-size: 11px; font-weight: 700; letter-spacing: 0.06em; text-transform: uppercase;
  padding: 3px 8px; border-radius: 999px; background: var(--warn-bg); color: var(--warn);
}
.depth .for { min-width: 0; overflow-wrap: anywhere; }
.question { font-family: var(--font-math); font-size: clamp(1.45rem, 4.6vw, 1.85rem); line-height: 1.3; margin: 0; text-wrap: balance; }
.question:focus { outline: none; }
.answer-row { display: flex; gap: 10px; flex-wrap: wrap; }
.field {
  flex: 1 1 200px; min-width: 0; display: flex; align-items: center;
  border: 1.5px solid var(--line); border-radius: 10px; background: var(--paper);
}
.field:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
.field input {
  flex: 1; min-width: 0; border: 0; background: transparent; color: var(--ink);
  font-family: var(--font-math); font-size: 1.35rem; padding: 10px 14px; font-variant-numeric: tabular-nums;
}
.field input:focus { outline: none; }
.unit { padding-right: 14px; font-family: var(--font-math); font-size: 1.1rem; color: var(--muted); white-space: nowrap; }

.btn {
  font-family: var(--font-ui); font-size: 15px; font-weight: 600; line-height: 1.2;
  border-radius: 10px; padding: 12px 20px; cursor: pointer; border: 1.5px solid transparent;
}
.btn:focus-visible, .key:focus-visible, .val:focus-visible, .tool:focus-visible, .icon-btn:focus-visible {
  outline: 3px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 2px;
}
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { filter: brightness(1.06); }
.btn-secondary { background: transparent; color: var(--ink); border-color: var(--line); }
.btn-secondary:hover { border-color: var(--muted); }
.btn:disabled, .tool:disabled { opacity: .5; cursor: default; }
.tools { display: flex; gap: 8px; flex-wrap: wrap; }
.tool {
  display: inline-flex; align-items: center; gap: 7px; cursor: pointer;
  background: transparent; color: var(--muted); border: 1.5px solid var(--line); border-radius: 10px;
  font-family: var(--font-ui); font-size: 14px; font-weight: 600; padding: 8px 14px;
}
.tool:hover:not(:disabled) { color: var(--ink); border-color: var(--muted); }
.tool[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
.tool svg { width: 16px; height: 16px; flex: none; }
.feedback { min-height: 1.5em; margin: 0; font-size: 15px; font-weight: 500; }
.feedback.good { color: var(--good); }
.feedback.bad { color: var(--warn); }
/* Hints and shown answers: help, not a scolding. */
.feedback.hint {
  color: var(--ink); background: var(--accent-soft); border-radius: 10px; padding: 10px 12px;
  border-left: 3px solid var(--accent);
}
.field input[readonly] { color: var(--accent); }

/* Diagrams (public/js/viz.js). Semantic fills: over the curve = too much (warn),
   under the curve = too little (accent), exact area = good. */
.viz-wrap { margin: 0; }
.viz { display: block; width: 100%; max-width: 420px; height: auto; margin: 0 auto; overflow: visible; }
.viz-small .viz { max-width: 300px; margin: 0; }
.viz-grid { stroke: var(--line); stroke-width: 1; }
.viz-axis { stroke: var(--muted); stroke-width: 1.5; }
.viz-tick { fill: var(--muted); font-family: var(--font-ui); font-size: 11px; font-variant-numeric: tabular-nums; }
.viz-name { fill: var(--accent); font-family: var(--font-math); font-size: 13px; font-style: italic; }
.viz-curve { fill: none; stroke: var(--accent); stroke-width: 2.5; stroke-linejoin: round; }
.viz-area { fill: color-mix(in srgb, var(--good) 22%, transparent); }
.viz-rect-over { fill: color-mix(in srgb, var(--warn) 18%, transparent); stroke: var(--warn); stroke-width: 1.2; }
.viz-rect-under { fill: color-mix(in srgb, var(--accent) 24%, transparent); stroke: var(--accent); stroke-width: 1.2; }
.viz-rect-over.thin, .viz-rect-under.thin { stroke-width: 0.4; }
.viz-box { fill: none; stroke: var(--muted); stroke-width: 1.2; stroke-dasharray: 4 4; }
.viz-cut { stroke: var(--accent); stroke-width: 2; stroke-dasharray: 6 4; }
.viz-mark { stroke: var(--ink); stroke-width: 1.5; stroke-dasharray: 4 3; }
.viz-dot { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.viz-value { fill: var(--ink); font-family: var(--font-math); font-size: 14px; font-weight: 600; font-variant-numeric: tabular-nums; }

/* Matrix diagrams: the row and column being multiplied share one highlight; their result cell gets a stronger one. */
.viz-hl { fill: var(--accent-soft); stroke: color-mix(in srgb, var(--accent) 45%, transparent); stroke-width: 1; }
.viz-hl-cell { fill: color-mix(in srgb, var(--accent) 22%, transparent); stroke: var(--accent); stroke-width: 1.5; }
.viz-cell { fill: var(--ink); font-family: var(--font-math); font-size: 18px; font-variant-numeric: tabular-nums; }
.viz-cell.q { fill: var(--accent); font-weight: 600; }
.viz-cell.empty { fill: var(--line); }
.viz-bracket { fill: none; stroke: var(--ink); stroke-width: 1.6; }
.viz-op { fill: var(--muted); font-family: var(--font-math); font-size: 22px; }
.viz.viz-matrix { max-width: 380px; }
.viz.viz-arrows { max-width: 320px; }
.viz-arrow { stroke: var(--accent); stroke-width: 2.5; stroke-linecap: round; }
.viz-arrow.ghost { stroke: var(--muted); stroke-dasharray: 5 4; }
.viz-arrowhead { fill: var(--accent); }
.viz-arrowhead.ghost { fill: var(--muted); }
.viz-halo { paint-order: stroke; stroke: var(--surface, #fff); stroke-width: 4px; stroke-linejoin: round; }
/* Seesaw diagrams */
.viz-plank { fill: var(--muted); }
.viz-pivot { fill: var(--line); stroke: var(--muted); stroke-width: 1.2; }
.viz-load { fill: var(--accent-soft); stroke: var(--accent); stroke-width: 1.5; }
.viz-load.unknown { fill: none; stroke-dasharray: 5 4; }
.viz-load-label { fill: var(--ink); font-family: var(--font-ui); font-size: 13px; font-weight: 600; }
.viz-dim { stroke: var(--muted); stroke-width: 1.2; }
.viz-value.q { fill: var(--accent); }

/* Calculator */
.calc {
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  padding: 14px; display: flex; flex-direction: column; gap: 10px;
}
.calc-display {
  display: flex; align-items: center; gap: 4px;
  background: var(--paper); border: 1.5px solid var(--line); border-radius: 10px;
}
.calc-display:focus-within { border-color: var(--accent); }
.calc-display input {
  flex: 1; min-width: 0; border: 0; background: transparent; color: var(--ink);
  font-family: var(--font-math); font-size: 1.3rem; padding: 9px 12px; text-align: right; font-variant-numeric: tabular-nums;
}
.calc-display input:focus { outline: none; }
.icon-btn { display: inline-grid; place-items: center; background: transparent; color: var(--muted); border: 0; border-radius: 8px; padding: 8px 10px; cursor: pointer; }
.icon-btn:hover { color: var(--ink); }
.icon-btn svg { width: 20px; height: 20px; }
.calc-result { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; min-height: 38px; }
.calc-result .eq { font-family: var(--font-math); font-size: 1.25rem; font-variant-numeric: tabular-nums; min-width: 0; overflow-wrap: anywhere; }
.calc-result .eq.placeholder { font-family: var(--font-ui); font-size: 14px; color: var(--muted); }
.use { background: var(--good-bg); color: var(--good); font-size: 14px; padding: 8px 14px; }
.keys { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.key {
  font-family: var(--font-math); font-size: 1.2rem; font-weight: 500; cursor: pointer;
  padding: 10px 0; border-radius: 9px; background: var(--paper); color: var(--ink); border: 1px solid var(--line);
}
.key:hover { border-color: var(--muted); }
.key.op { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.key.fn { color: var(--muted); font-family: var(--font-ui); font-size: 15px; font-weight: 600; }
.key.eq-key { background: var(--accent); color: var(--accent-ink); border-color: transparent; }

/* What you've found */
.found { display: flex; flex-direction: column; gap: 8px; }
.found-head { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; flex-wrap: wrap; }
.found-head small { font-size: 13px; color: var(--muted); }
.found ol { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; }
.found li {
  display: flex; justify-content: space-between; gap: 16px; align-items: center;
  padding: 5px 0; border-bottom: 1px dashed var(--line); font-size: 14.5px;
}
.found li > span { color: var(--muted); min-width: 0; }
.found li.empty { color: var(--muted); border: 0; }
.val {
  font-family: var(--font-math); font-size: 1.05rem; font-variant-numeric: tabular-nums; white-space: nowrap; cursor: pointer;
  background: transparent; color: var(--ink); padding: 3px 8px; border-radius: 7px; border: 1px solid transparent;
}
.val:hover { background: var(--accent-soft); border-color: color-mix(in srgb, var(--accent) 30%, transparent); }

/* Finale */
.finale { display: flex; flex-direction: column; gap: 14px; }
.finale h2 { margin: 0; font-size: 15px; font-weight: 600; color: var(--muted); }
.big { font-family: var(--font-math); font-size: 3rem; line-height: 1; margin: 0; color: var(--good); }
.finale p { margin: 0; }
.stats { display: flex; gap: 24px; flex-wrap: wrap; padding-top: 4px; }
.stat { display: flex; flex-direction: column; }
.stat b { font-family: var(--font-math); font-size: 1.6rem; font-variant-numeric: tabular-nums; }
.stat span { font-size: 13px; color: var(--muted); }
.actions { display: flex; gap: 10px; flex-wrap: wrap; }

/* Stages, recap and Now you try */
.stage { margin: 0 0 6px; font-size: 13px; font-weight: 600; color: var(--accent); }
.found li.stage-head { display: block; border: 0; padding: 12px 0 2px; font-size: 12px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--accent); }
.found li.stage-head:first-child { padding-top: 0; }
.recap { display: flex; flex-direction: column; gap: 6px; padding: 12px 14px; border-radius: 10px; background: var(--accent-soft); }
.recap ol { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 4px; }
.try-next, .practice-head { display: flex; flex-direction: column; gap: 4px; padding: 12px 14px; border-radius: 10px; border: 1px dashed color-mix(in srgb, var(--accent) 45%, var(--line)); }
.try-next p, .practice-head p { margin: 0; font-family: var(--font-math); font-size: 1.08rem; }
.practice-head { margin-bottom: 12px; }

/* Loading, error, empty, not found */
.state { display: flex; flex-direction: column; gap: 12px; align-items: flex-start; }
.state h1 { font-family: var(--font-math); font-weight: 500; font-size: 1.7rem; margin: 0; }
.state p { margin: 0; color: var(--muted); max-width: 52ch; }
.skeleton { display: flex; flex-direction: column; gap: 14px; }
.sk { background: var(--line); border-radius: 8px; animation: pulse 1.4s ease-in-out infinite; }
.sk-line { height: 18px; }
.w40 { width: 40%; } .w90 { width: 90%; }
.sk-card { height: 220px; border-radius: 14px; }
@keyframes pulse { 50% { opacity: .55; } }

.enter { animation: rise .32s ease-out; }
@keyframes rise { from { opacity: .35; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.shake { animation: shake .3s; }
@keyframes shake { 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }
.flash { animation: flash .6s; }
@keyframes flash { from { background: var(--accent-soft); } }
@media (prefers-reduced-motion: reduce) {
  .enter, .shake, .flash, .sk { animation: none; }
  .seg, .problem-link { transition: none; }
}
@media (max-width: 420px) {
  .card { padding: 20px 16px 16px; }
  .answer-row .btn-primary { flex: 1 1 100%; }
}
