/* Transformer Explainer — one stylesheet. Light/dark via [data-theme]; motion via .reduce-motion. */
:root {
  --bg: #f6f7fb; --surface: #ffffff; --surface-2: #f0f2f8; --text: #1b1f2e; --muted: #5a6178; --border: #dfe3ee;
  --accent-hsl: 250 78% 58%; --accent: hsl(var(--accent-hsl)); --accent-soft: hsl(var(--accent-hsl) / 0.12);
  --pos-hsl: 22 92% 52%; --neg-hsl: 214 86% 52%; --good: #14915a; --bad: #d1344a; --warn: #c27a00;
  --on-strong: #fff; --shadow: 0 1px 2px rgba(20, 24, 50, .06), 0 6px 20px rgba(20, 24, 50, .06);
  --chip-s: 82%; --chip-l: 88%; --chip-bl: 68%; --chip-fg: #1b1f2e;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace; --sans: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --t: 400ms; --radius: 14px; --header-h: 150px;
}
[data-theme="dark"] {
  --bg: #0e111a; --surface: #161a26; --surface-2: #1d2231; --text: #e7eaf5; --muted: #98a0b8; --border: #2a3042;
  --accent-hsl: 252 90% 72%; --pos-hsl: 24 95% 58%; --neg-hsl: 212 90% 62%; --good: #4cd08f; --bad: #ff6b82; --warn: #f0b429;
  --on-strong: #0e111a; --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  --chip-s: 55%; --chip-l: 26%; --chip-bl: 42%; --chip-fg: #eef0fa;
}
* { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: calc(var(--header-h) + 12px); }
html.reduce-motion { scroll-behavior: auto; }
html.reduce-motion *, html.reduce-motion *::before, html.reduce-motion *::after { transition-duration: 0ms !important; animation-duration: 0ms !important; animation-iteration-count: 1 !important; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.55 var(--sans); -webkit-font-smoothing: antialiased; }
h1, h2, h3 { line-height: 1.2; margin: 0; letter-spacing: -0.01em; }
code { font-family: var(--mono); font-size: .9em; background: var(--surface-2); padding: .1em .35em; border-radius: 5px; }
button, input, select { font: inherit; color: inherit; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 6px; }
.skip { position: absolute; left: -999px; top: 8px; background: var(--accent); color: var(--on-strong); padding: 8px 14px; border-radius: 8px; z-index: 100; }
.skip:focus { left: 8px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- header + pipeline map ---------- */
.topbar { position: sticky; top: 0; z-index: 40; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(12px); border-bottom: 1px solid var(--border); }
.topbar-row { display: flex; align-items: center; gap: 12px; padding: 8px 20px; max-width: 1240px; margin: 0 auto; flex-wrap: wrap; }
.brand { font-weight: 700; font-size: 1.05rem; display: flex; align-items: center; gap: 8px; white-space: nowrap; }
.brand .logo { width: 26px; height: 26px; border-radius: 8px; background: linear-gradient(135deg, hsl(var(--accent-hsl)), hsl(var(--pos-hsl))); display: inline-block; }
.sentence { flex: 1 1 280px; display: flex; align-items: center; gap: 8px; min-width: 220px; }
.sentence label { font-size: .8rem; color: var(--muted); white-space: nowrap; }
.sentence input { flex: 1; min-width: 0; padding: 7px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); font-size: 1rem; }
.sentence .note { font-size: .75rem; color: var(--warn); white-space: nowrap; }
.top-actions { display: flex; gap: 8px; align-items: center; }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); cursor: pointer; font-size: .85rem; white-space: nowrap; }
.pill:hover { border-color: var(--accent); }
.pill.on { background: var(--accent); color: var(--on-strong); border-color: var(--accent); }
.map-wrap { max-width: 1240px; margin: 0 auto; padding: 0 20px 8px; overflow-x: auto; scrollbar-width: thin; }
.map { display: flex; align-items: stretch; gap: 0; min-width: max-content; position: relative; padding: 12px 0 20px; }
.node { appearance: none; border: 1px solid var(--border); background: var(--surface); border-radius: 10px; padding: 5px 9px; cursor: pointer; font-size: .74rem; line-height: 1.2; text-align: center; transition: background var(--t), border-color var(--t), color var(--t), box-shadow var(--t); display: flex; flex-direction: column; gap: 2px; justify-content: center; min-width: 64px; }
.node b { font-weight: 600; font-size: .78rem; }
.node .shape { font-size: .62rem; }
.node:hover { border-color: var(--accent); transform: translateY(-1px); }
.node.active { background: var(--accent); color: var(--on-strong); border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.node.active .shape { color: inherit; background: rgba(255,255,255,.2); }
.node.done b::after { content: " ✓"; color: var(--good); }
.node.active.done b::after { color: inherit; }
.arrow { align-self: center; color: var(--muted); padding: 0 3px; font-size: .8rem; }
.block { border: 1.5px dashed var(--accent); border-radius: 14px; padding: 16px 8px 6px; position: relative; display: flex; align-items: stretch; background: var(--accent-soft); }
.block-label { position: absolute; top: -9px; left: 12px; background: var(--bg); padding: 0 8px; font-size: .7rem; font-weight: 700; color: var(--accent); border-radius: 6px; cursor: pointer; border: 0; }
.loop { position: absolute; left: 24px; right: 24px; bottom: 2px; height: 14px; border: 1.5px solid var(--muted); border-top: 0; border-radius: 0 0 12px 12px; opacity: .55; }
.loop::before { content: "↑"; position: absolute; left: -6px; top: -5px; font-size: .8rem; color: var(--muted); line-height: 1; }
.loop span { position: absolute; left: 50%; transform: translateX(-50%); bottom: -8px; background: var(--bg); padding: 0 8px; font-size: .68rem; color: var(--muted); white-space: nowrap; }

/* ---------- layout ---------- */
main { max-width: 1100px; margin: 0 auto; padding: 24px 20px 120px; }
.hero { padding: 24px 0 8px; }
.hero h1 { font-size: clamp(1.9rem, 4vw, 2.8rem); font-weight: 800; }
.hero p { max-width: 70ch; color: var(--muted); font-size: 1.05rem; }
.hero .cta { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 14px; }
.facts { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }
.fact { font-size: .8rem; padding: 4px 10px; background: var(--surface); border: 1px solid var(--border); border-radius: 999px; color: var(--muted); }
.fact b { color: var(--text); }
.chapter { margin: 56px 0; transition: opacity var(--t); }
.ch-head { display: flex; gap: 16px; align-items: flex-start; margin-bottom: 14px; }
.ch-num { flex: none; width: 44px; height: 44px; border-radius: 14px; background: var(--accent); color: var(--on-strong); display: grid; place-items: center; font-weight: 800; font-size: 1.2rem; }
.chapter.complete .ch-num { background: var(--good); }
.ch-titles { flex: 1; }
.ch-titles h2 { font-size: 1.6rem; }
.intuition { margin: 4px 0 0; font-size: 1.08rem; color: var(--muted); max-width: 75ch; }
.ch-status { font-weight: 700; color: var(--good); font-size: .9rem; white-space: nowrap; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 16px; margin-top: 14px; }
.card-title { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; flex-wrap: wrap; }
.tag { font-size: .7rem; text-transform: uppercase; letter-spacing: .08em; font-weight: 700; color: var(--accent); background: var(--accent-soft); padding: 3px 9px; border-radius: 999px; }
.pg-hint { color: var(--muted); font-size: .9rem; }
.empty { color: var(--muted); text-align: center; padding: 30px; }

/* ---------- viz player ---------- */
.viz-bar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 10px; }
.controls { display: flex; gap: 6px; flex-wrap: wrap; }
.speed { font-size: .85rem; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.speed select { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 4px 6px; }
.step-count { margin-left: auto; font-size: .85rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.viz-stage { min-height: 240px; overflow-x: auto; padding: 6px 2px 10px; position: relative; }
.viz-caption { background: var(--surface-2); border-radius: 10px; padding: 10px 14px; margin-top: 6px; border-left: 4px solid var(--accent); min-height: 3.2em; }
.cap-title { display: block; margin-bottom: 2px; }
.cap-text { color: var(--text); font-size: .95rem; }
.step-dots { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 10px; }
.dot { width: 12px; height: 12px; border-radius: 50%; border: 0; background: var(--border); cursor: pointer; padding: 0; transition: transform .2s, background var(--t); }
.dot.done { background: hsl(var(--accent-hsl) / .5); } .dot.on { background: var(--accent); transform: scale(1.35); }
.btn { appearance: none; border: 1px solid var(--border); background: var(--surface); border-radius: 10px; padding: 6px 13px; cursor: pointer; font-size: .88rem; font-weight: 500; transition: all .2s; }
.btn:hover:not(:disabled) { border-color: var(--accent); color: var(--accent); }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.btn.primary { background: var(--accent); color: var(--on-strong); border-color: var(--accent); }
.btn.primary:hover:not(:disabled) { color: var(--on-strong); filter: brightness(1.08); }
.btn.ghost { background: transparent; } .btn.small { padding: 3px 10px; font-size: .8rem; }

/* ---------- widgets ---------- */
.chip { --h: 210; display: inline-flex; align-items: baseline; gap: 5px; padding: 2px 9px; border-radius: 8px; font-family: var(--mono); font-size: .9rem; white-space: pre; color: var(--chip-fg);
  background: hsl(var(--h) var(--chip-s) var(--chip-l)); border: 1.5px solid hsl(var(--h) var(--chip-s) var(--chip-bl)); transition: background var(--t), border-color var(--t), box-shadow var(--t); }
.chip.small { padding: 0 6px; font-size: .78rem; border-radius: 6px; }
.chip-id { font-size: .68em; opacity: .75; font-weight: 700; }
.chip.dim { opacity: .35; } .chip.hl { box-shadow: 0 0 0 3px hsl(var(--h) 90% 55% / .45); }
.chip-btn { appearance: none; background: none; border: 2px solid transparent; border-radius: 12px; padding: 3px; cursor: pointer; display: inline-flex; flex-direction: column; align-items: center; gap: 1px; }
.chip-btn.sel { border-color: var(--accent); background: var(--accent-soft); }
.chip-btn .pos { font-size: .65rem; color: var(--muted); }
.picker { display: flex; flex-wrap: wrap; gap: 4px; align-items: center; }
.shape { font-family: var(--mono); font-size: .72rem; padding: 1px 6px; border-radius: 6px; background: var(--surface-2); color: var(--muted); border: 1px solid var(--border); white-space: nowrap; }
.tt { border-bottom: 1.5px dotted var(--accent); cursor: help; }
.tooltip { position: fixed; z-index: 200; max-width: 300px; background: #151829; color: #eef0fa; padding: 8px 11px; border-radius: 10px; font-size: .82rem; line-height: 1.4; pointer-events: none; opacity: 0; transition: opacity .15s; display: flex; flex-direction: column; gap: 2px; box-shadow: 0 8px 24px rgba(0,0,0,.35); }
.tooltip.show { opacity: 1; } .tooltip code { background: rgba(255,255,255,.14); color: inherit; }
[data-theme="dark"] .tooltip { background: #eef0fa; color: #151829; } [data-theme="dark"] .tooltip code { background: rgba(0,0,0,.12); }
.matrix { display: inline-block; vertical-align: top; }
.mgrid { display: grid; gap: 2px; align-items: center; }
.mcell { display: grid; place-items: center; font-family: var(--mono); font-size: .72rem; border-radius: 4px; transition: background var(--t), color var(--t); background: var(--surface-2); font-variant-numeric: tabular-nums; }
.mcell.masked { background: repeating-linear-gradient(45deg, var(--surface-2), var(--surface-2) 4px, var(--border) 4px, var(--border) 5px); color: var(--muted); }
.mcol { font-size: .68rem; color: var(--muted); text-align: center; font-family: var(--mono); overflow: hidden; }
.mrow { display: flex; justify-content: flex-end; padding-right: 4px; } .rowlab { font-size: .72rem; color: var(--muted); font-family: var(--mono); }
.slider { display: flex; align-items: center; gap: 10px; font-size: .9rem; min-width: 240px; flex: 1 1 260px; }
.slider .sl-label { min-width: 84px; color: var(--muted); } .slider input { flex: 1; accent-color: var(--accent); min-width: 80px; }
.sl-val { font-family: var(--mono); min-width: 3.2em; text-align: right; font-variant-numeric: tabular-nums; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; background: var(--surface-2); }
.seg-b { appearance: none; border: 0; background: none; padding: 5px 12px; cursor: pointer; font-size: .85rem; }
.seg-b.on { background: var(--accent); color: var(--on-strong); }
.row { display: flex; gap: 14px; flex-wrap: wrap; align-items: center; margin: 8px 0; }
.col { display: flex; flex-direction: column; gap: 6px; }
.lbl { font-size: .78rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .05em; margin-bottom: 4px; display: flex; align-items: center; gap: 6px; }
.flow { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.op { font-size: 1.4rem; color: var(--muted); font-weight: 300; padding: 0 2px; }
.stack { display: flex; flex-direction: column; gap: 4px; }
.box { border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; background: var(--surface-2); }
.kv { font-family: var(--mono); font-size: .8rem; color: var(--muted); }
.kv b { color: var(--text); }
svg text { font-family: var(--sans); fill: var(--text); } svg .mono { font-family: var(--mono); }
svg .muted { fill: var(--muted); }
.tokfill { fill: hsl(var(--h) var(--chip-s) var(--chip-l)); stroke: hsl(var(--h) var(--chip-s) var(--chip-bl)); stroke-width: 1.5; }
.tokstroke { stroke: hsl(var(--h) 70% 50%); fill: none; }
.tokbar { fill: hsl(var(--h) 70% 52%); }
.legend { font-size: .78rem; color: var(--muted); display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.sw { display: inline-block; width: 14px; height: 10px; border-radius: 3px; margin-right: 4px; vertical-align: middle; }
.sw.pos { background: hsl(var(--pos-hsl)); } .sw.neg { background: hsl(var(--neg-hsl)); } .sw.acc { background: var(--accent); }

/* ---------- deeper + quiz ---------- */
details.deeper { margin-top: 14px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
details.deeper summary { cursor: pointer; padding: 12px 16px; font-weight: 600; list-style: none; display: flex; align-items: center; gap: 8px; }
details.deeper summary::before { content: "▸"; transition: transform .2s; color: var(--accent); }
details.deeper[open] summary::before { transform: rotate(90deg); }
.deeper-body { padding: 0 18px 16px; font-size: .96rem; } .deeper-body h4 { margin: 16px 0 4px; }
.deeper-body table { border-collapse: collapse; margin: 8px 0; font-size: .88rem; } .deeper-body td, .deeper-body th { border: 1px solid var(--border); padding: 4px 10px; text-align: left; }
.deeper-body .katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; }
.quiz .q { margin: 8px 0 16px; } .q-text { margin: 0 0 8px; font-weight: 600; } .q-n { color: var(--accent); }
.opts { display: grid; gap: 6px; }
.opt { appearance: none; display: flex; gap: 10px; align-items: flex-start; text-align: left; background: var(--surface-2); border: 1.5px solid var(--border); border-radius: 10px; padding: 8px 12px; cursor: pointer; transition: all .2s; }
.opt:hover:not(:disabled) { border-color: var(--accent); } .opt:disabled { cursor: default; }
.opt-mark { flex: none; width: 22px; height: 22px; border-radius: 50%; background: var(--surface); border: 1px solid var(--border); display: grid; place-items: center; font-size: .75rem; font-weight: 700; }
.opt.correct { border-color: var(--good); background: color-mix(in srgb, var(--good) 14%, var(--surface)); } .opt.correct .opt-mark { background: var(--good); color: #fff; border-color: var(--good); }
.opt.wrong { border-color: var(--bad); background: color-mix(in srgb, var(--bad) 12%, var(--surface)); } .opt.wrong .opt-mark { background: var(--bad); color: #fff; border-color: var(--bad); }
.feedback { font-size: .9rem; margin-top: 6px; padding: 0; } .feedback.good, .feedback.bad { padding: 8px 12px; border-radius: 8px; }
.feedback.good { background: color-mix(in srgb, var(--good) 12%, transparent); } .feedback.bad { background: color-mix(in srgb, var(--bad) 10%, transparent); }
.quiz-score { margin-left: auto; font-weight: 600; font-size: .85rem; color: var(--muted); }

/* ---------- tour + panels ---------- */
.tourbar { position: fixed; left: 50%; bottom: 14px; transform: translate(-50%, 140%); width: min(860px, calc(100vw - 24px)); z-index: 60; background: var(--surface); border: 1.5px solid var(--accent); border-radius: 16px; box-shadow: var(--shadow), 0 10px 40px rgba(0,0,0,.2); padding: 12px 16px; visibility: hidden; transition: transform .45s cubic-bezier(.2,.8,.2,1), visibility 0s .45s; }
body.tour .tourbar { transform: translate(-50%, 0); visibility: visible; transition-delay: 0s; }
.tour-head { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; font-size: .8rem; color: var(--muted); }
.tour-head strong { color: var(--accent); } .tour-progress { flex: 1; height: 4px; background: var(--border); border-radius: 4px; overflow: hidden; } .tour-progress i { display: block; height: 100%; background: var(--accent); transition: width .4s; }
.tour-text { font-size: 1rem; min-height: 3em; } .tour-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 8px; align-items: center; }
.tour-actions .grow { flex: 1; font-size: .78rem; color: var(--muted); }
body.tour .chapter:not(.tour-on) { opacity: .28; } body.tour .chapter.tour-on { opacity: 1; }
.panel { position: fixed; right: 16px; top: calc(var(--header-h) - 4px); z-index: 70; width: min(380px, calc(100vw - 32px)); max-height: 70vh; overflow: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; box-shadow: var(--shadow), 0 12px 40px rgba(0,0,0,.25); padding: 14px 16px; display: none; }
.panel.open { display: block; } .panel h3 { font-size: 1rem; margin-bottom: 8px; }
.prog-row { display: flex; align-items: center; gap: 10px; padding: 4px 0; font-size: .9rem; border-bottom: 1px solid var(--border); }
.prog-row a { color: var(--text); text-decoration: none; flex: 1; } .prog-row a:hover { color: var(--accent); }
.prog-row .sc { font-family: var(--mono); font-size: .8rem; color: var(--muted); } .prog-row.done .sc { color: var(--good); font-weight: 700; }
.bar { height: 8px; background: var(--border); border-radius: 6px; overflow: hidden; margin: 6px 0 10px; } .bar i { display: block; height: 100%; background: var(--good); transition: width .5s; }
.kbd { font-family: var(--mono); font-size: .78rem; border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 6px; padding: 0 6px; background: var(--surface-2); }
.keys { display: grid; grid-template-columns: auto 1fr; gap: 6px 12px; font-size: .88rem; }
footer { max-width: 1100px; margin: 0 auto; padding: 20px; color: var(--muted); font-size: .85rem; border-top: 1px solid var(--border); }
footer .built-with { margin-top: 10px; overflow-wrap: anywhere; }
footer .built-with a { color: var(--accent); text-underline-offset: 2px; } footer .built-with a:hover { text-decoration-thickness: 2px; }

@media (max-width: 720px) {
  :root { --header-h: 190px; }
  .topbar-row { padding: 6px 12px; gap: 8px; } .brand span.name { display: none; } main { padding: 16px 12px 140px; }
  .ch-titles h2 { font-size: 1.3rem; } .ch-num { width: 36px; height: 36px; font-size: 1rem; } .node .shape { display: none; }
  .map-wrap { padding: 0 12px 6px; }
}
.chip.neutral { background: var(--surface-2); border-color: var(--border); color: var(--text); }
.chip.pop { box-shadow: 0 0 0 3px var(--accent-soft), 0 0 0 4px var(--accent); }
body.tour .chapter.tour-on .viz-caption { display: none; }
.wordgroup { display: inline-flex; gap: 3px; padding: 5px; border-radius: 12px; border: 1px dashed var(--border); }
.wordgroup.flat { border-color: transparent; padding: 5px 0; }
.chars { display: flex; flex-wrap: wrap; gap: 2px; }
.charbox { font-family: var(--mono); min-width: 1.5ch; padding: 1px 5px; border-radius: 5px; background: var(--surface-2); border: 1px solid var(--border); text-align: center; }
.rule { font-family: var(--mono); font-size: .85rem; padding: 4px 10px; border-radius: 8px; background: var(--accent-soft); display: inline-flex; gap: 8px; align-items: center; }
.merge-list { max-height: 180px; overflow: auto; font-family: var(--mono); font-size: .78rem; display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 2px 10px; padding: 8px; border: 1px solid var(--border); border-radius: 10px; }
.merge-list .used { color: var(--accent); font-weight: 700; }
.tok-table { border-collapse: collapse; font-size: .85rem; } .tok-table td, .tok-table th { padding: 3px 12px 3px 0; text-align: left; }
input.text, textarea.text { width: 100%; padding: 8px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); font-size: 1rem; }
.mspacer { display: block; }
.mcell.hl { outline: 2px solid var(--accent); outline-offset: -1px; }
.mcol .chip { max-width: 100%; }
.deeper-body { overflow-x: auto; }
.deeper-body table { display: block; overflow-x: auto; max-width: 100%; }
.pg-body, .viz-stage { max-width: 100%; }
.card { overflow-x: auto; }
@media (max-width: 720px) {
  .topbar { position: static; background: none; backdrop-filter: none; border-bottom: 0; }
  .map-wrap { position: sticky; top: 0; z-index: 40; background: color-mix(in srgb, var(--bg) 94%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--border); padding-top: 6px; }
  .panel { top: 8px; }
  .tourbar { bottom: 6px; padding: 10px 12px; }
}
