/* Sparks & Circuits — shared lesson styles */
:root {
  --bg: #fbfaf7; --surface: #ffffff; --surface2: #f3f1ec; --ink: #1d2026; --muted: #5f6672; --line: #e3e0d8;
  --accent: #e8590c; --accent2: #ffe8d6; --accent-ink: #ffffff; --link: #1864ab;
  --c1: #1c7ed6; --c2: #f08c00; --c3: #2f9e44; --c4: #e03131; --c5: #7048e8; --c6: #0c8599;
  --good: #2b8a3e; --good-bg: #ebfbee; --bad: #c92a2a; --bad-bg: #fff5f5;
  --wire: #2b2f36; --scope-bg: #0b1a12; --scope-grid: #1d3a28; --scope-ink: #b2f2bb;
  --radius: 14px; --shadow: 0 1px 2px rgba(0,0,0,.05), 0 6px 20px rgba(0,0,0,.06);
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --serif: Charter, "Bitstream Charter", "Sitka Text", Cambria, Georgia, serif;
  --mono: ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas, monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #131519; --surface: #1b1e24; --surface2: #23272f; --ink: #e8eaed; --muted: #a2a9b5; --line: #30353e;
    --accent: #ff8a3d; --accent2: #3a2516; --link: #74c0fc;
    --c1: #4dabf7; --c2: #ffa94d; --c3: #69db7c; --c4: #ff6b6b; --c5: #9775fa; --c6: #3bc9db;
    --good: #69db7c; --good-bg: #14271a; --bad: #ff8787; --bad-bg: #2c1618; --wire: #d3d7dd;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 20px rgba(0,0,0,.25); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #131519; --surface: #1b1e24; --surface2: #23272f; --ink: #e8eaed; --muted: #a2a9b5; --line: #30353e;
  --accent: #ff8a3d; --accent2: #3a2516; --link: #74c0fc;
  --c1: #4dabf7; --c2: #ffa94d; --c3: #69db7c; --c4: #ff6b6b; --c5: #9775fa; --c6: #3bc9db;
  --good: #69db7c; --good-bg: #14271a; --bad: #ff8787; --bad-bg: #2c1618; --wire: #d3d7dd;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 6px 20px rgba(0,0,0,.25); color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-padding-top: 70px; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 17px/1.65 var(--font); }
a { color: var(--link); }
img, svg, canvas { max-width: 100%; }
h1, h2, h3, h4 { line-height: 1.25; margin: 1.6em 0 .5em; }
h1 { font-size: clamp(1.9rem, 4.5vw, 2.6rem); letter-spacing: -.02em; margin-top: .2em; }
h2 { font-size: 1.55rem; letter-spacing: -.01em; padding-top: .4em; border-top: 1px solid var(--line); }
h3 { font-size: 1.2rem; }
p, li { max-width: 72ch; }
code { font: .9em var(--mono); background: var(--surface2); padding: .1em .35em; border-radius: 5px; }
pre { position: relative; background: var(--surface2); border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px; overflow-x: auto; font-size: .86rem; line-height: 1.5; }
pre code { background: none; padding: 0; }
.copy-btn { position: absolute; top: 6px; right: 6px; font: 12px var(--font); padding: 3px 8px; border-radius: 6px; border: 1px solid var(--line); background: var(--surface); color: var(--muted); cursor: pointer; }
table { border-collapse: collapse; width: 100%; margin: 1em 0; font-size: .95rem; display: block; overflow-x: auto; }
th, td { border-bottom: 1px solid var(--line); padding: 8px 10px; text-align: left; vertical-align: top; }
th { background: var(--surface2); }
hr { border: 0; border-top: 1px solid var(--line); margin: 2em 0; }
kbd { font: .85em var(--mono); border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 5px; padding: 0 .35em; background: var(--surface); }

/* ---------- top bar ---------- */
.topbar { position: sticky; top: 0; z-index: 50; display: flex; align-items: center; gap: 12px; padding: 8px 16px; background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.tb-home { font-weight: 600; text-decoration: none; color: var(--ink); white-space: nowrap; }
.tb-title { color: var(--muted); font-size: .9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.tb-stats { display: flex; align-items: center; gap: 6px; }
.tb-chip { background: var(--surface2); border-radius: 999px; padding: 2px 10px; font-size: .9rem; white-space: nowrap; }
.tb-btn { border: 0; background: none; font-size: 1.05rem; cursor: pointer; padding: 4px 6px; border-radius: 8px; color: var(--ink); }
.tb-btn:hover { background: var(--surface2); }
.tb-progress { position: absolute; left: 0; right: 0; bottom: -1px; height: 3px; }
.tb-progress i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--c3), var(--accent)); transition: width .5s; }
@media (max-width: 560px) { .tb-title { display: none; } .topbar { justify-content: space-between; } }

/* ---------- layout ---------- */
main.chapter { max-width: 780px; margin: 0 auto; padding: 24px 16px 100px; }
.chapter-head { margin: 24px 0 8px; }
.kicker { text-transform: uppercase; letter-spacing: .12em; font-size: .78rem; font-weight: 700; color: var(--accent); margin: 0; }
.lede { font-size: 1.2rem; color: var(--muted); font-family: var(--serif); }
.inpage-toc { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 20px; margin: 20px 0 28px; font-size: .95rem; }
.inpage-toc ol { margin: 6px 0 0; padding-left: 1.3em; columns: 2; column-gap: 28px; }
@media (max-width: 600px) { .inpage-toc ol { columns: 1; } }
.inpage-toc a { text-decoration: none; }
section { margin-bottom: 2em; }

/* ---------- callouts ---------- */
.callout { border: 1px solid var(--line); border-left: 4px solid var(--muted); background: var(--surface); border-radius: 10px; padding: 12px 16px; margin: 1.4em 0; }
.callout > :first-child { margin-top: 0; } .callout > :last-child { margin-bottom: 0; }
.callout .ct { display: block; font-weight: 700; margin-bottom: 4px; }
.callout.tip { border-left-color: var(--c3); }
.callout.warn { border-left-color: var(--c4); background: var(--bad-bg); }
.callout.dev { border-left-color: var(--c5); }
.callout.physics { border-left-color: var(--c1); }
.callout.math { border-left-color: var(--c6); }
.callout.lab { border-left-color: var(--c2); }
.callout.ref { border-left-color: var(--muted); }
.callout.myth { border-left-color: var(--c4); }
details.deep { border: 1px dashed var(--line); border-radius: 10px; padding: 10px 16px; margin: 1.2em 0; background: var(--surface); }
details.deep > summary { cursor: pointer; font-weight: 600; color: var(--c6); }
.formula { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 18px 10px; margin: 1.4em 0; text-align: center; }
.formula p { margin: 0 auto; color: var(--muted); font-size: .92rem; }
.katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; }

/* ---------- figures & widgets ---------- */
figure { margin: 1.6em 0; }
figure.widget { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); }
@media (min-width: 1000px) { figure.wide { margin-left: -90px; margin-right: -90px; } }
figcaption { font-size: .9rem; color: var(--muted); margin-top: 10px; }
figcaption b:first-child { color: var(--ink); }
.w-plot, .w-canvas { display: block; border-radius: 8px; }
.w-plot-wrap, .w-canvas-wrap { width: 100%; }
.w-controls { display: flex; flex-wrap: wrap; gap: 8px 18px; align-items: center; margin: 10px 0 4px; }
.w-slider { display: grid; grid-template-columns: minmax(80px, max-content) 1fr 78px; gap: 10px; align-items: center; flex: 1 1 280px; font-size: .92rem; }
.w-slider input { width: 100%; accent-color: var(--accent); }
.w-slider output { font: .88rem var(--mono); text-align: right; white-space: nowrap; }
.w-readout { display: inline-flex; gap: 6px; align-items: baseline; background: var(--surface2); border-radius: 8px; padding: 4px 10px; font-size: .92rem; }
.w-readout b { font-family: var(--mono); font-weight: 600; }
.w-readout.good { background: var(--good-bg); color: var(--good); } .w-readout.bad { background: var(--bad-bg); color: var(--bad); }
.w-toggle, .w-select { display: inline-flex; gap: 6px; align-items: center; font-size: .92rem; cursor: pointer; }
.w-select select { font: inherit; padding: 4px 8px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--ink); }
.w-seg { display: inline-flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.w-seg button { border: 0; background: var(--surface); color: var(--ink); padding: 6px 12px; font: inherit; font-size: .9rem; cursor: pointer; border-right: 1px solid var(--line); }
.w-seg button:last-child { border-right: 0; }
.w-seg button.on { background: var(--accent); color: var(--accent-ink); }
.w-grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; align-items: center; }
@media (max-width: 640px) { .w-grid2 { grid-template-columns: 1fr; } }

/* schematics */
.w-sch { display: block; width: 100%; height: auto; margin: 0 auto; overflow: visible; }
.w-sch line, .w-sch polyline, .w-sch path, .w-sch circle, .w-sch polygon, .w-sch rect { stroke: var(--wire); stroke-width: 2; fill: none; stroke-linecap: round; stroke-linejoin: round; }
.w-sch .fill { fill: var(--wire); }
.w-sch .glow { stroke: none; transition: opacity .2s; }
.w-sch .p-LED .body { stroke-width: 2; }
.w-sch .bulb { transition: fill .2s; }
.w-sch .hit { stroke: none; fill: transparent; }
.w-sch .clickable { cursor: pointer; }
.w-sch .clickable:hover .lever { stroke: var(--accent); }
.w-sch .lever line { stroke-width: 2.5; }
.w-sch line.lever { stroke-width: 2.5; }
.w-sch text { fill: var(--ink); stroke: none; font: 13px var(--font); }
.w-sch text.sym { font-size: 13px; font-weight: 700; }
.w-sch text.sym.big { font-size: 15px; }
.w-sch text.pin { font-size: 10.5px; fill: var(--muted); font-family: var(--mono); }
.w-sch text.icname { font-weight: 700; }
.w-sch .icbox { fill: var(--surface2); }
.w-sch .flowdot { stroke: none; }
.w-sch .tag-r { fill: var(--surface2); stroke: var(--line); stroke-width: 1; }
.w-sch .tag-t { font: 12px var(--mono); }
.w-sch .anno line, .w-sch .anno polygon { stroke: currentColor; } .w-sch .anno .fill { fill: currentColor; }
.w-sch .anno { color: var(--c4); } .w-sch .anno-t { fill: var(--c4); font-weight: 600; }
.w-sch .wire.hot { stroke: var(--c4); } .w-sch .wire.gndw { stroke: var(--c1); }
.w-bulb .glass { fill: color-mix(in srgb, var(--surface2) 70%, transparent); stroke: var(--wire); stroke-width: 3; transition: fill .2s; }
.w-bulb .filament { fill: none; stroke: var(--muted); stroke-width: 2.5; }
.w-bulb .base rect, .w-bulb .base path { fill: var(--muted); stroke: none; }

/* ---------- buttons ---------- */
.btn { font: 600 .95rem var(--font); border: 0; border-radius: 12px; padding: 10px 18px; background: var(--accent); color: var(--accent-ink); cursor: pointer; box-shadow: 0 3px 0 color-mix(in srgb, var(--accent) 60%, #000); transition: transform .06s, box-shadow .06s; text-decoration: none; display: inline-block; }
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 color-mix(in srgb, var(--accent) 60%, #000); }
.btn:disabled { background: var(--line); color: var(--muted); box-shadow: none; cursor: not-allowed; }
.btn.small { padding: 5px 12px; font-size: .85rem; border-radius: 9px; box-shadow: 0 2px 0 color-mix(in srgb, var(--accent) 60%, #000); }
.btn.ghost { background: transparent; color: var(--muted); box-shadow: none; border: 1px solid var(--line); }
.btn.big { font-size: 1.1rem; padding: 14px 28px; }
.btn.continue { margin: 10px 0 20px; }

/* ---------- exercises ---------- */
.ex { position: relative; background: var(--surface); border: 2px solid var(--line); border-radius: var(--radius); padding: 16px 18px 14px; margin: 1.8em 0; box-shadow: var(--shadow); }
.ex.solved { border-color: color-mix(in srgb, var(--good) 55%, var(--line)); }
.ex-badge { display: inline-block; font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .08em; color: var(--accent); background: var(--accent2); padding: 2px 9px; border-radius: 999px; margin-bottom: 8px; }
.ex.solved .ex-badge::after { content: " ✓"; }
.ex .q { font-weight: 500; }
.ex .q > :first-child { margin-top: 0; }
.ex .opts { list-style: none; padding: 0; margin: 12px 0 4px; display: grid; gap: 8px; }
.ex .opts > li { border: 2px solid var(--line); border-bottom-width: 4px; border-radius: 12px; padding: 9px 14px; cursor: pointer; background: var(--surface); transition: border-color .1s, background .1s, transform .06s; max-width: none; }
.ex .opts > li:hover { border-color: color-mix(in srgb, var(--c1) 50%, var(--line)); }
.ex .opts > li:active { transform: translateY(2px); }
.ex .opts > li.sel { border-color: var(--c1); background: color-mix(in srgb, var(--c1) 10%, var(--surface)); }
.ex .opts > li.wrong { border-color: var(--bad); background: var(--bad-bg); }
.ex .opts > li.right { border-color: var(--good); background: var(--good-bg); }
.ex.solved .opts > li { cursor: default; }
.ex .opts.grid2 { grid-template-columns: 1fr 1fr; }
.ex .opts.grid3 { grid-template-columns: repeat(3, 1fr); }
@media (max-width: 520px) { .ex .opts.grid3 { grid-template-columns: 1fr 1fr; } }
.ex-input { display: flex; align-items: center; gap: 8px; margin: 12px 0 4px; }
.ex-input input { font: 1.05rem var(--mono); padding: 9px 12px; border-radius: 10px; border: 2px solid var(--line); background: var(--bg); color: var(--ink); width: min(260px, 100%); }
.ex-input input:focus { outline: none; border-color: var(--c1); }
.ex-input .unit { color: var(--muted); font-weight: 600; }
.ex .hint, .ex .explain { display: none; margin-top: 12px; padding: 10px 14px; border-radius: 10px; font-size: .95rem; }
.ex .hint { background: color-mix(in srgb, var(--c2) 12%, var(--surface)); }
.ex .hint::before { content: "💡 Hint: "; font-weight: 700; }
.ex .explain { background: var(--good-bg); }
.ex .explain::before { content: "Why: "; font-weight: 700; color: var(--good); }
.ex.hinted .hint { display: block; }
.ex.solved .explain { display: block; }
.ex-fb { min-height: 1.4em; font-weight: 600; margin-top: 8px; }
.ex-fb.good { color: var(--good); } .ex-fb.bad { color: var(--bad); }
.ex-foot { display: flex; gap: 10px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.ex-retry { display: none; margin-left: auto; }
.ex.solved .ex-retry { display: inline-block; }
.ex.solved .ex-check { display: none; }
.ex.shake { animation: shake .4s; }
.ex.pop { animation: pop .5s; }
@keyframes shake { 0%,100% { transform: translateX(0); } 20% { transform: translateX(-8px); } 40% { transform: translateX(7px); } 60% { transform: translateX(-5px); } 80% { transform: translateX(3px); } }
@keyframes pop { 0% { transform: scale(1); } 40% { transform: scale(1.02); } 100% { transform: scale(1); } }
.ex-task .q::before { content: "🎯 "; }
/* order */
.ord-line { min-height: 54px; border: 2px dashed var(--line); border-radius: 12px; padding: 8px; display: flex; flex-wrap: wrap; gap: 8px; margin: 12px 0 8px; align-items: center; }
.ord-line:empty::before { content: attr(data-empty); color: var(--muted); font-size: .9rem; padding: 0 6px; }
.ord-bank { display: flex; flex-wrap: wrap; gap: 8px; min-height: 40px; }
.ord-line li, .ord-bank li, .sort-pool li, .bin-drop li { list-style: none; border: 2px solid var(--line); border-bottom-width: 4px; border-radius: 10px; padding: 6px 12px; background: var(--surface); cursor: pointer; user-select: none; max-width: none; }
.ord-line { counter-reset: ord; }
.ord-line li::before { counter-increment: ord; content: counter(ord) ". "; color: var(--muted); font-weight: 700; }
.ord-line li.wrong, .bin-drop li.wrong { border-color: var(--bad); background: var(--bad-bg); }
.ex.solved .ord-line li, .ex.solved .bin-drop li { border-color: var(--good); background: var(--good-bg); cursor: default; }
/* sort */
.sort-pool { display: flex; flex-wrap: wrap; gap: 8px; padding: 0; margin: 12px 0; min-height: 44px; }
.sort-pool li.picked { border-color: var(--c1); transform: translateY(-2px); box-shadow: 0 4px 10px rgba(0,0,0,.15); }
.ex .bins { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.ex .bins > [data-bin] { border: 2px dashed var(--line); border-radius: 12px; padding: 8px; min-height: 90px; cursor: pointer; background: var(--surface2); }
.ex .bins > [data-bin].over { border-color: var(--c1); }
.bin-label { font-weight: 700; text-align: center; margin-bottom: 6px; }
.bin-drop { display: flex; flex-wrap: wrap; gap: 6px; padding: 0; margin: 0; }

/* xp, toast, confetti */
.xp-float { position: absolute; transform: translateX(-50%); font-weight: 800; color: var(--c2); pointer-events: none; animation: floatup 1.2s ease-out forwards; z-index: 90; font-size: 1.1rem; }
@keyframes floatup { from { opacity: 1; margin-top: 0; } to { opacity: 0; margin-top: -60px; } }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 30px); opacity: 0; background: var(--ink); color: var(--bg); padding: 12px 20px; border-radius: 12px; font-weight: 600; z-index: 100; transition: all .3s; max-width: 90vw; text-align: center; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.win { background: linear-gradient(90deg, var(--c5), var(--accent)); color: #fff; }
.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 99; }

/* chapter nav */
.chap-nav { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 60px; }
.chap-nav a { border: 1px solid var(--line); border-radius: var(--radius); padding: 14px 16px; text-decoration: none; color: var(--ink); background: var(--surface); display: flex; flex-direction: column; }
.chap-nav a:hover { border-color: var(--accent); }
.chap-nav .next { text-align: right; }
.chap-nav small { color: var(--muted); }

/* TOC */
.toc-list { list-style: none; padding: 0; display: grid; gap: 10px; }
.toc-list li { max-width: none; }
.toc-list a { display: grid; grid-template-columns: 48px 1fr 44px; gap: 14px; align-items: center; padding: 14px 16px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); text-decoration: none; color: var(--ink); transition: border-color .15s, transform .15s; }
.toc-list a:hover { border-color: var(--accent); transform: translateY(-1px); }
.toc-num { width: 44px; height: 44px; border-radius: 50%; display: grid; place-items: center; font-weight: 800; background: var(--accent2); color: var(--accent); font-size: 1.1rem; }
.toc-list a.done .toc-num { background: var(--good-bg); color: var(--good); }
.toc-body { display: flex; flex-direction: column; }
.toc-desc { color: var(--muted); font-size: .9rem; }
.toc-bar { height: 4px; background: var(--surface2); border-radius: 4px; margin-top: 6px; overflow: hidden; }
.toc-bar i { display: block; height: 100%; background: var(--c3); }
.toc-pct { color: var(--good); font-weight: 700; text-align: right; }

@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation-duration: .001s !important; transition-duration: .001s !important; } }
@media print { .topbar, .chap-nav, .ex-foot { display: none !important; } }
[hidden] { display: none !important; }
.w-select select { max-width: 100%; }
.w-select { max-width: 100%; flex-wrap: wrap; }
.w-seg { flex-wrap: wrap; max-width: 100%; }
main table, .katex-display, .formula { position: relative; }
main p code, main li code { overflow-wrap: anywhere; }
.w-grid2 > * { min-width: 0; }
.w-slider { grid-template-columns: minmax(70px, max-content) minmax(80px, 1fr) minmax(64px, max-content); }
@media (max-width: 480px) { .w-slider { grid-template-columns: 1fr auto; } .w-slider input { grid-column: 1 / -1; grid-row: 2; } }
