:root {
  --bg: #f7f7f5;
  --surface: #ffffff;
  --text: #1d1f23;
  --muted: #5c6370;
  --border: #e2e2de;
  --accent: #5b3fd1;
  --accent-soft: #ece8fb;
  --ok: #1f7a4d;
  --warn: #a15c00;
  --code-bg: #1e1f26;
  --code-text: #e6e6e6;
  --diagram-bg: #f1f0ec;
  --tok-kw: #c792ea;
  --tok-type: #7fd1c7;
  --tok-str: #c3e88d;
  --tok-num: #f78c6c;
  --tok-com: #7f848e;
  --radius: 8px;
  --mono: ui-monospace, "Cascadia Code", "SF Mono", Consolas, "Liberation Mono", monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15161a;
    --surface: #1c1d22;
    --text: #e4e4e7;
    --muted: #9ca0aa;
    --border: #2d2f36;
    --accent: #a594f9;
    --accent-soft: #2a2542;
    --ok: #5fcf95;
    --warn: #e0a24a;
    --code-bg: #111216;
    --diagram-bg: #202127;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
}

.sr-only {
  position: absolute; width: 1px; height: 1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap;
}

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 820px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.brand {
  font-weight: 650; color: var(--text); text-decoration: none; white-space: nowrap;
  font-family: var(--mono); font-size: 15px;
}
.brand .dim { color: var(--muted); }
.topbar-right { display: flex; gap: 8px; align-items: center; min-width: 0; }
#jump {
  font: inherit; font-size: 14px; max-width: 220px; min-width: 0;
  padding: 5px 8px; border-radius: 6px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.progress { height: 3px; background: var(--border); }
.progress-fill { height: 100%; background: var(--accent); width: 0; transition: width .25s ease; }

/* ---------- buttons ---------- */
.btn {
  font: inherit; font-size: 14px; font-weight: 550;
  padding: 7px 14px; border-radius: 6px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  transition: background .15s, border-color .15s, opacity .15s;
}
.btn:hover:not(:disabled) { border-color: var(--accent); }
.btn:focus-visible, select:focus-visible, summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #fff; }
@media (prefers-color-scheme: dark) { .btn-primary { color: #15161a; } }
.btn-ghost { background: transparent; }
.btn-small { padding: 4px 10px; font-size: 13px; }
.btn-code { font-family: var(--mono); font-size: 13px; }

/* ---------- lesson ---------- */
.lesson {
  max-width: 820px; margin: 0 auto; padding: 28px 16px 120px;
  outline: none;
  transition: opacity .15s ease;
}
.lesson.fading { opacity: 0; }

.eyebrow {
  font-family: var(--mono); font-size: 13px; color: var(--accent);
  letter-spacing: .02em; margin: 0 0 4px;
}
.lesson h1 { font-size: 28px; line-height: 1.25; margin: 0 0 14px; letter-spacing: -.01em; }
.lesson h2 {
  font-size: 13px; text-transform: uppercase; letter-spacing: .08em;
  color: var(--muted); margin: 30px 0 8px; font-weight: 650;
}
.lesson p { margin: 0 0 12px; }
.lesson ul { margin: 0 0 12px; padding-left: 22px; }
.lesson li { margin-bottom: 4px; }
.intro { font-size: 17px; }

code {
  font-family: var(--mono); font-size: .88em;
  background: var(--accent-soft); padding: 1px 5px; border-radius: 4px;
}

.code-label {
  font-family: var(--mono); font-size: 12px; color: var(--muted); margin: 16px 0 4px;
}
pre.code {
  margin: 0 0 12px; padding: 14px 16px;
  background: var(--code-bg); color: var(--code-text);
  border-radius: var(--radius); overflow-x: auto;
  font-family: var(--mono); font-size: 14px; line-height: 1.55;
  tab-size: 4;
}
pre.code code { background: none; padding: 0; font-size: inherit; color: inherit; }
.tok-kw { color: var(--tok-kw); }
.tok-type { color: var(--tok-type); }
.tok-str { color: var(--tok-str); }
.tok-num { color: var(--tok-num); }
.tok-com { color: var(--tok-com); font-style: italic; }

pre.diagram {
  margin: 0 0 12px; padding: 14px 16px;
  background: var(--diagram-bg); border: 1px solid var(--border);
  border-radius: var(--radius); overflow-x: auto;
  font-family: var(--mono); font-size: 14px; line-height: 1.45;
}

.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px; margin: 0 0 12px;
}
.card p:last-child, .card ul:last-child { margin-bottom: 0; }
.card.before { border-left: 3px solid var(--accent); }

/* ---------- predict / reveal ---------- */
.predict { display: grid; gap: 10px; }
.predict .q { font-weight: 550; margin: 0 0 8px; }
.predict .answer {
  margin-top: 10px; padding-top: 10px; border-top: 1px dashed var(--border);
}
.predict .answer[hidden] { display: none; }

details.more {
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); margin: 12px 0;
}
details.more summary {
  cursor: pointer; padding: 10px 14px; font-weight: 550; font-size: 15px;
  list-style-position: inside;
}
details.more[open] summary { border-bottom: 1px solid var(--border); }
details.more .more-body { padding: 12px 14px 4px; }

/* ---------- walker widget ---------- */
.widget {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 14px 16px; margin: 0 0 12px;
}
.widget-title { font-size: 13px; color: var(--muted); margin: 0 0 10px; }
.walker + .walker { margin-top: 16px; padding-top: 14px; border-top: 1px dashed var(--border); }
.walker-name { font-family: var(--mono); font-size: 13px; font-weight: 650; margin-bottom: 6px; }
.cells { display: flex; gap: 6px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 8px; }
.cell {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-family: var(--mono);
}
.cell .box {
  min-width: 48px; padding: 6px 8px; text-align: center;
  border: 1px solid var(--border); border-radius: 6px; background: var(--diagram-bg);
  transition: background .15s, border-color .15s;
}
.cell.slot .box { border-style: dashed; color: var(--muted); font-size: 12px; min-width: 56px; }
.cell .idx { font-size: 11px; color: var(--muted); }
.cell .caret { height: 16px; font-size: 13px; color: var(--accent); line-height: 16px; }
.cell.here .box { border-color: var(--accent); background: var(--accent-soft); font-weight: 650; }
.walker-state { font-family: var(--mono); font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.walker-state b { color: var(--text); font-weight: 600; }
.walker-controls { display: flex; flex-wrap: wrap; gap: 6px; }
.log {
  font-family: var(--mono); font-size: 13px; margin-top: 8px; min-height: 1.5em;
}
.log .t { color: var(--ok); }
.log .f { color: var(--warn); }

/* loop trace */
.trace-code { margin-bottom: 10px; }
.trace-code .line { display: block; padding: 0 6px; margin: 0 -6px; border-radius: 4px; }
.trace-code .line.active { background: rgba(165, 148, 249, .22); }
.trace-note { font-size: 15px; margin: 8px 0; min-height: 3em; }
.console {
  font-family: var(--mono); font-size: 13px;
  background: var(--code-bg); color: var(--code-text);
  border-radius: 6px; padding: 8px 12px; margin: 8px 0; min-height: 2.4em;
  white-space: pre;
}
.console .prompt { color: var(--tok-com); }

/* ---------- pager ---------- */
.pager {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  background: var(--surface); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.pager-inner {
  max-width: 820px; margin: 0 auto; padding: 10px 16px;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
}
.counter { font-family: var(--mono); font-size: 13px; color: var(--muted); white-space: nowrap; }

.noscript { text-align: center; padding: 40px 16px; }

/* ---------- mobile ---------- */
@media (max-width: 600px) {
  .lesson { padding-top: 20px; }
  .lesson h1 { font-size: 23px; }
  .intro { font-size: 16px; }
  pre.code, pre.diagram { font-size: 12.5px; padding: 12px; }
  .brand { font-size: 13px; }
  #jump { max-width: 120px; }
  #reset { padding: 5px 8px; font-size: 12px; }
  .cell .box { min-width: 40px; padding: 5px 6px; }
  .cell.slot .box { min-width: 44px; font-size: 11px; }
}
@media (max-width: 380px) {
  .brand .dim { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

#reset { white-space: nowrap; }
@media (max-width: 600px) {
  .hide-sm { display: none; }
}

.lesson a { color: var(--accent); text-underline-offset: 2px; }
