@font-face { font-family: 'Atkinson Hyperlegible'; src: url('../fonts/atkinson-hyperlegible-latin-400-normal.woff2') format('woff2'); font-weight: 400; font-display: swap; }
@font-face { font-family: 'Atkinson Hyperlegible'; src: url('../fonts/atkinson-hyperlegible-latin-400-italic.woff2') format('woff2'); font-weight: 400; font-style: italic; font-display: swap; }
@font-face { font-family: 'Atkinson Hyperlegible'; src: url('../fonts/atkinson-hyperlegible-latin-700-normal.woff2') format('woff2'); font-weight: 700; font-display: swap; }
@font-face { font-family: 'Newsreader'; src: url('../fonts/newsreader-latin-wght-normal.woff2') format('woff2'); font-weight: 200 800; font-display: swap; }
@font-face { font-family: 'Newsreader'; src: url('../fonts/newsreader-latin-wght-italic.woff2') format('woff2'); font-weight: 200 800; font-style: italic; font-display: swap; }

:root {
  --paper: #fbf8f1; --paper-2: #f3eee2; --line: #e0dacb; --ink: #1d2327; --ink-2: #565c60;
  --accent: #c63d2c; --accent-ink: #fff; --ok: #25803c; --no: #c63d2c; --ok-bg: #e4f2e4; --no-bg: #fae6e1;
  --side: #14201f; --side-2: #1e2d2b; --side-line: #2c3f3c; --side-ink: #eeebe0; --side-ink-2: #9daaa6; --chalk: #f1d163;
  --serif: 'Newsreader', Georgia, serif;
  --sans: 'Atkinson Hyperlegible', system-ui, sans-serif;
  --side-w: 288px;
}
:root[data-theme='dark'] {
  --paper: #14201f; --paper-2: #1b2a28; --line: #2c3f3c; --ink: #f0ede3; --ink-2: #a6b1ad;
  --accent: #ff8d78; --accent-ink: #14201f; --ok: #7edb8c; --no: #ff8d78; --ok-bg: #1c3a2a; --no-bg: #43251f;
  --side: #0e1716; --side-2: #18242a; --side-line: #24332f;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body { margin: 0; background: var(--paper); color: var(--ink); font: 16px/1.5 var(--sans); display: flex; overflow: hidden; }
a { color: inherit; }
h1, h2, h3, p { margin: 0; }
ul, ol { margin: 0; padding: 0; list-style: none; }
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; padding: 0; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- sidebar ---- */
#sidebar { width: var(--side-w); flex: none; height: 100dvh; background: var(--side); color: var(--side-ink); display: flex; flex-direction: column; }
.brand { display: flex; align-items: center; gap: 10px; padding: 18px 20px 14px; font: 600 23px/1 var(--serif); text-decoration: none; }
.brand-mark { color: var(--chalk); font-size: 28px; line-height: 1; font-style: italic; }
.nav { flex: 1; overflow-y: auto; padding: 0 10px 12px; scrollbar-width: thin; scrollbar-color: var(--side-line) transparent; }
.nav-group { font: 700 11.5px/1 var(--sans); letter-spacing: .08em; text-transform: uppercase; color: var(--side-ink-2); padding: 16px 10px 6px; }
.course-link { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; padding: 7px 10px; border-radius: 6px; text-decoration: none; font-weight: 700; font-size: 15px; }
.course-link:hover { background: var(--side-2); }
.course.open > .course-link { background: var(--side-2); color: var(--chalk); }
.course-count { font-weight: 400; font-size: 12.5px; color: var(--side-ink-2); font-variant-numeric: tabular-nums; white-space: nowrap; }
.course-count.done { color: var(--chalk); }
.lesson-list { margin: 3px 0 8px 6px; border-left: 1px solid var(--side-line); }
.lesson-link { display: flex; gap: 9px; align-items: baseline; padding: 4px 8px 4px 10px; font-size: 14px; line-height: 1.3; text-decoration: none; color: var(--side-ink-2); border-radius: 0 6px 6px 0; }
.lesson-link:hover { color: var(--side-ink); background: var(--side-2); }
.lesson-link.current { color: var(--side-ink); background: var(--side-2); box-shadow: inset 2px 0 0 var(--chalk); }
.mark { flex: none; width: 13px; font-size: 12px; text-align: center; color: var(--chalk); position: relative; }
.mark.seen::before { content: ''; position: absolute; left: 4px; top: -8px; width: 5px; height: 5px; border-radius: 50%; background: var(--side-ink-2); }
.side-foot { display: flex; flex-wrap: wrap; gap: 4px 14px; padding: 12px 20px; border-top: 1px solid var(--side-line); font-size: 13px; color: var(--side-ink-2); }
.side-foot a, .side-foot button { color: inherit; text-decoration: none; }
.side-foot a:hover, .side-foot button:hover { color: var(--side-ink); }

#nav-toggle, #nav-scrim { display: none; }

/* ---- main + lesson ---- */
#main { flex: 1; min-width: 0; height: 100dvh; position: relative; overflow: auto; }
.lesson { height: 100%; display: flex; flex-direction: column; position: relative; overflow: hidden; }
.lesson-head { display: grid; grid-template-columns: minmax(170px, 250px) minmax(0, 1fr) auto; gap: 8px 32px; padding: 14px 24px 12px; border-bottom: 1px solid var(--line); background: var(--paper); align-items: start; max-height: 34vh; overflow-y: auto; }
.crumb { font-size: 12.5px; color: var(--ink-2); }
.lesson-title h1 { font: 600 27px/1.12 var(--serif); margin-top: 2px; letter-spacing: -.005em; }
.tutorial { font-size: 15.5px; line-height: 1.5; max-width: 76ch; }
.tutorial p + p, .tutorial ul + p, .tutorial p + ul { margin-top: 6px; }
.tutorial ul { padding-left: 18px; list-style: disc; }
.tutorial .katex-display { margin: 6px 0; text-align: left; }
.tutorial .katex-display > .katex { text-align: left; }
.try { color: var(--ink-2); margin-top: 6px; }
.try b { color: var(--accent); margin-right: 3px; }
.lesson-actions { display: flex; flex-direction: column; gap: 8px; align-items: stretch; min-width: 150px; }
.pager { display: flex; justify-content: space-between; font-size: 14px; }
.pager a { color: var(--ink-2); text-decoration: none; }
.pager a:hover { color: var(--ink); text-decoration: underline; }
.cta { display: inline-block; text-align: center; background: var(--accent); color: var(--accent-ink); font-weight: 700; font-size: 15px; padding: 8px 16px; border-radius: 6px; text-decoration: none; }
.cta:hover { filter: brightness(1.08); }
.ghost { padding: 8px 16px; border: 1px solid var(--line); border-radius: 6px; font-weight: 700; font-size: 15px; }
.ghost:hover { background: var(--paper-2); }

.stage { flex: 1; min-height: 200px; position: relative; overflow: hidden; }
.stage canvas { display: block; position: absolute; inset: 0; touch-action: none; cursor: default; outline: none; }
canvas.over-point { cursor: grab; }
canvas.dragging { cursor: grabbing; }

.bar { display: flex; justify-content: space-between; align-items: center; gap: 12px 32px; flex-wrap: wrap; padding: 9px 24px; min-height: 54px; border-top: 1px solid var(--line); background: var(--paper-2); }
.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 20px; flex: 1 1 340px; }
.readouts { display: flex; flex-wrap: wrap; gap: 2px 22px; font-size: 16px; justify-content: flex-end; flex: 0 1 auto; font-variant-numeric: tabular-nums; }
.readout { white-space: nowrap; }
.ctl { display: flex; align-items: center; gap: 8px; font-size: 14.5px; }
.ctl-label { color: var(--ink-2); white-space: nowrap; }
.ctl output { min-width: 40px; font-variant-numeric: tabular-nums; font-size: 14px; }
.ctl-check { display: flex; align-items: center; gap: 6px; cursor: pointer; white-space: nowrap; }
.ctl-check input { accent-color: var(--accent); width: 16px; height: 16px; margin: 0; }
.ctl-btn { padding: 4px 12px; border: 1px solid var(--line); border-radius: 6px; background: var(--paper); font-size: 14px; white-space: nowrap; }
.ctl-btn:hover { border-color: var(--ink-2); }
.ctl-reset { color: var(--ink-2); }
.ctl-play { width: 26px; height: 26px; line-height: 24px; text-align: center; font-size: 10px; border: 1px solid var(--line); border-radius: 50%; background: var(--paper); }
.ctl-seg { display: inline-flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; background: var(--paper); }
.ctl-seg button { padding: 4px 11px; font-size: 14px; white-space: nowrap; }
.ctl-seg button + button { border-left: 1px solid var(--line); }
.ctl-seg button.on { background: var(--ink); color: var(--paper); }

input[type='range'] { -webkit-appearance: none; appearance: none; width: 150px; height: 22px; background: transparent; margin: 0; cursor: pointer; }
input[type='range']::-webkit-slider-runnable-track { height: 4px; border-radius: 2px; background: linear-gradient(to right, var(--accent) var(--fill, 0%), var(--line) var(--fill, 0%)); }
input[type='range']::-moz-range-track { height: 4px; border-radius: 2px; background: var(--line); }
input[type='range']::-moz-range-progress { height: 4px; border-radius: 2px; background: var(--accent); }
input[type='range']::-webkit-slider-thumb { -webkit-appearance: none; width: 16px; height: 16px; border-radius: 50%; background: var(--paper); border: 2.5px solid var(--accent); margin-top: -6px; }
input[type='range']::-moz-range-thumb { width: 11px; height: 11px; border-radius: 50%; background: var(--paper); border: 2.5px solid var(--accent); }

/* ---- exercise drawer ---- */
.drawer { position: absolute; top: 0; right: 0; bottom: 0; width: min(420px, 100%); background: var(--paper); border-left: 1px solid var(--line); box-shadow: -12px 0 32px rgb(0 0 0 / .14); display: flex; flex-direction: column; z-index: 5; animation: slide .18s ease-out; }
@keyframes slide { from { transform: translateX(24px); opacity: 0; } }
@media (prefers-reduced-motion: reduce) { .drawer { animation: none; } }
.drawer-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 22px; border-bottom: 1px solid var(--line); }
.drawer-head h2 { font: 600 21px/1 var(--serif); }
.drawer-close { font-size: 26px; line-height: 1; color: var(--ink-2); width: 32px; height: 32px; }
.drawer-body { padding: 18px 22px 26px; overflow-y: auto; flex: 1; }
.q-count { font-size: 13px; color: var(--ink-2); }
.q-text { font-size: 18px; line-height: 1.4; margin: 6px 0 16px; }
.q-options { display: flex; flex-direction: column; gap: 8px; }
.q-option { text-align: left; padding: 10px 14px; border: 1.5px solid var(--line); border-radius: 8px; background: var(--paper); line-height: 1.35; }
.q-option:not(:disabled):hover { border-color: var(--ink-2); background: var(--paper-2); }
.q-option:disabled { cursor: default; opacity: .65; }
.q-option.right { border-color: var(--ok); background: var(--ok-bg); opacity: 1; }
.q-option.wrong { border-color: var(--no); background: var(--no-bg); opacity: 1; }
.q-feedback { margin-top: 16px; }
.q-verdict { font-weight: 700; }
.q-verdict.ok { color: var(--ok); }
.q-verdict.no { color: var(--no); }
.q-explain { margin: 3px 0 14px; color: var(--ink-2); }
.result { padding-top: 8px; }
.score { font: 500 64px/1 var(--serif); }
.score span { font-size: 30px; color: var(--ink-2); }
.result h3 { font: 600 24px/1.2 var(--serif); margin: 12px 0 4px; }
.result-actions { display: flex; gap: 10px; margin-top: 20px; flex-wrap: wrap; }

/* ---- home ---- */
.home { max-width: 1040px; margin: 0 auto; padding: 44px 40px 40px; }
.hero { display: grid; grid-template-columns: minmax(0, 5fr) minmax(0, 7fr); gap: 36px; align-items: center; margin-bottom: 20px; }
.hero h1 { font: 500 clamp(36px, 4.4vw, 54px)/1.04 var(--serif); letter-spacing: -.015em; }
.hero p { font-size: 18px; color: var(--ink-2); margin: 14px 0 20px; max-width: 42ch; }
.hero-viz { position: relative; height: 270px; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.hero-viz canvas { position: absolute; inset: 0; touch-action: none; outline: none; }
.home-group { margin-top: 30px; }
.home-group h2 { font: 700 12.5px/1 var(--sans); letter-spacing: .08em; text-transform: uppercase; color: var(--ink-2); padding-bottom: 8px; border-bottom: 1px solid var(--ink); }
.home-courses a { display: grid; grid-template-columns: minmax(180px, 2fr) minmax(0, 4fr) 90px 80px; gap: 18px; align-items: baseline; padding: 11px 0; border-bottom: 1px solid var(--line); text-decoration: none; }
.home-courses a:hover .hc-title { color: var(--accent); }
.hc-title { font: 600 21px/1.2 var(--serif); }
.hc-blurb { color: var(--ink-2); font-size: 15.5px; }
.hc-meter { height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; align-self: center; }
.hc-meter i { display: block; height: 100%; background: var(--accent); }
.hc-count { text-align: right; font-size: 13.5px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.home-note { margin-top: 26px; color: var(--ink-2); font-size: 14px; }
.notfound { padding: 60px 40px; }
.notfound h1 { font: 500 34px/1.1 var(--serif); margin-bottom: 10px; }

/* ---- small screens ---- */
@media (max-width: 1100px) {
  .lesson-head { grid-template-columns: minmax(0, 1fr) auto; }
  .tutorial { grid-column: 1 / -1; grid-row: 2; }
  .lesson-actions { flex-direction: row; align-items: center; min-width: 0; gap: 16px; }
  .pager { gap: 14px; }
}
@media (max-width: 820px) {
  body { display: block; }
  #sidebar { position: fixed; z-index: 20; top: 0; left: 0; transform: translateX(-100%); transition: transform .2s ease; }
  body.nav-open #sidebar { transform: none; }
  #nav-scrim { position: fixed; inset: 0; background: rgb(0 0 0 / .45); z-index: 15; }
  body.nav-open #nav-scrim { display: block; }
  #nav-toggle { display: block; position: fixed; z-index: 10; top: 10px; left: 10px; width: 38px; height: 38px; border-radius: 8px; background: var(--side); color: var(--side-ink); font-size: 18px; }
  .lesson-head { padding: 10px 16px 10px 58px; max-height: 40dvh; }
  .tutorial { margin-left: -42px; font-size: 15px; }
  .lesson-title h1 { font-size: 22px; }
  .lesson-actions { grid-column: 1 / -1; grid-row: 3; margin-left: -42px; justify-content: space-between; }
  .stage { min-height: 240px; }
  .bar { padding: 8px 16px; }
  .readouts { justify-content: flex-start; }
  .controls { flex-basis: 100%; }
  input[type='range'] { width: 120px; flex: 1; }
  .ctl-slider { flex: 1 1 100%; }
  .home { padding: 60px 16px 30px; }
  .hero { grid-template-columns: 1fr; gap: 20px; }
  .home-courses a { grid-template-columns: minmax(0, 1fr) auto; gap: 2px 12px; }
  .hc-title { grid-column: 1; }
  .hc-count { grid-column: 2; grid-row: 1; }
  .hc-blurb { grid-column: 1 / -1; }
  .hc-meter { display: none; }
}
