/* Book-to-course player styles. Light + dark, responsive, motion respects prefers-reduced-motion. */
:root {
  --bg: #f7f7fb; --surface: #ffffff; --surface-2: #f0f1f8; --text: #1d2033; --muted: #636b86;
  --border: #dfe2ee; --accent: #3b5bdb; --accent-ink: #ffffff; --accent-soft: #e4e9ff;
  --ok: #1f9d55; --ok-soft: #e1f6ea; --bad: #d64545; --bad-soft: #fde8e8; --warn: #c27c0e; --warn-soft: #fff3da;
  --code-bg: #14172a; --code-text: #e7e9f6; --code-head: #1e2240;
  --tok-kw: #ff7ab2; --tok-str: #8fd18f; --tok-num: #f6b26b; --tok-com: #7f87a8; --tok-fn: #7fb7ff; --tok-lit: #c79bff; --tok-bi: #6ad6d6;
  --shadow: 0 1px 2px rgba(20,24,60,.06), 0 6px 20px rgba(20,24,60,.06);
  --radius: 14px; --sidebar-w: 304px; --topbar-h: 56px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}
:root[data-theme="dark"] {
  --bg: #0f1120; --surface: #181b30; --surface-2: #20243d; --text: #e8eaf8; --muted: #9aa2c4;
  --border: #2b3052; --accent: #7c93ff; --accent-ink: #0f1120; --accent-soft: #232a55;
  --ok: #4cd08a; --ok-soft: #14301f; --bad: #ff7a7a; --bad-soft: #3a1b1f; --warn: #f0b34a; --warn-soft: #382c10;
  --code-bg: #0b0d1a; --code-head: #151932; --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px rgba(0,0,0,.3);
}
* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body { margin: 0; background: var(--bg); color: var(--text); font-family: var(--font); line-height: 1.65; font-size: 17px; -webkit-font-smoothing: antialiased; }
a { color: var(--accent); }
button { font: inherit; color: inherit; }
code { font-family: var(--mono); font-size: .9em; background: var(--surface-2); padding: .12em .4em; border-radius: 6px; }

/* Top bar */
.topbar { position: sticky; top: 0; z-index: 30; height: var(--topbar-h); display: flex; align-items: center; gap: 14px; padding: 0 16px; background: color-mix(in srgb, var(--surface) 92%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--border); }
.brand { font-weight: 700; text-decoration: none; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 38vw; }
.topbar-progress { flex: 1; display: flex; align-items: center; gap: 10px; min-width: 80px; max-width: 460px; margin-left: auto; font-size: .85rem; color: var(--muted); }
.bar { flex: 1; height: 8px; background: var(--surface-2); border-radius: 99px; overflow: hidden; }
.bar-fill { height: 100%; width: 0; background: linear-gradient(90deg, var(--accent), #8a6bff); border-radius: 99px; transition: width .8s cubic-bezier(.2,.8,.2,1); }
.save-state { font-size: .78rem; color: var(--muted); white-space: nowrap; }
.icon-btn { border: 1px solid var(--border); background: var(--surface); border-radius: 10px; width: 38px; height: 38px; cursor: pointer; font-size: 1.1rem; }
.icon-btn:hover { background: var(--surface-2); }
#menuBtn { display: none; }

/* Layout */
.shell { display: grid; grid-template-columns: var(--sidebar-w) minmax(0, 1fr); min-height: calc(100vh - var(--topbar-h)); }
.sidebar { position: sticky; top: var(--topbar-h); align-self: start; height: calc(100vh - var(--topbar-h)); overflow-y: auto; padding: 16px 12px 40px; border-right: 1px solid var(--border); background: var(--surface); }
.content { padding: 32px clamp(16px, 4vw, 56px) 96px; outline: none; }
.content > * { max-width: 820px; margin-left: auto; margin-right: auto; }
.scrim { display: none; }

/* Sidebar */
.nav-home, .nav-gloss { display: flex; gap: 8px; align-items: center; padding: 8px 10px; border-radius: 10px; text-decoration: none; color: var(--text); font-weight: 600; font-size: .95rem; }
.nav-home:hover, .nav-gloss:hover, .nav-lesson:hover { background: var(--surface-2); }
.nav-home.active, .nav-gloss.active { background: var(--accent-soft); }
.nav-ch { margin-top: 10px; }
.nav-ch > summary { cursor: pointer; list-style: none; padding: 8px 10px; border-radius: 10px; font-weight: 700; font-size: .92rem; display: flex; gap: 8px; align-items: baseline; }
.nav-ch > summary::-webkit-details-marker { display: none; }
.nav-ch > summary::before { content: "▸"; color: var(--muted); transition: transform .2s; display: inline-block; }
.nav-ch[open] > summary::before { transform: rotate(90deg); }
.nav-ch .ch-pct { margin-left: auto; font-weight: 500; color: var(--muted); font-size: .8rem; }
.nav-lesson { display: flex; gap: 10px; align-items: flex-start; padding: 6px 10px 6px 26px; border-radius: 10px; text-decoration: none; color: var(--text); font-size: .9rem; line-height: 1.4; }
.nav-lesson.active { background: var(--accent-soft); font-weight: 600; }
.nav-lesson .st { flex: none; width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--border); margin-top: 2px; display: grid; place-items: center; font-size: .7rem; color: var(--accent-ink); }
.nav-lesson.done .st { background: var(--ok); border-color: var(--ok); }
.nav-lesson.started .st { border-color: var(--accent); background: linear-gradient(90deg, var(--accent) 50%, transparent 50%); }
.nav-lesson.is-test .t::after { content: " 🏁"; }

/* Content typography */
h1, h2, h3, h4, h5 { line-height: 1.25; margin: 1.6em 0 .5em; }
h1 { font-size: clamp(1.7rem, 3.4vw, 2.3rem); margin-top: 0; letter-spacing: -.01em; }
.crumb { color: var(--muted); font-size: .88rem; margin-bottom: 8px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; margin-bottom: 8px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px; border-radius: 99px; background: var(--surface-2); color: var(--muted); font-size: .8rem; font-weight: 600; }
.chip.ok { background: var(--ok-soft); color: var(--ok); }
.chip.bad { background: var(--bad-soft); color: var(--bad); }
.chip.accent { background: var(--accent-soft); color: var(--accent); }
.lead { font-size: 1.12rem; color: var(--muted); margin-top: 6px; margin-bottom: 24px; }
.block { margin: 22px auto; }
.block > h3:first-child, .block > h4:first-child { margin-top: 0; }
.text p, .text li { margin: .6em 0; }
.text ul, .text ol { padding-left: 1.4em; }
.text blockquote, .frombook blockquote { margin: .8em 0; padding: .2em 1em; border-left: 4px solid var(--accent); color: var(--muted); }

/* Math */
math { line-height: 1.2; }
/* overflow-x alone makes overflow-y compute to auto, and tall formulas (roots, fractions) overflow by a few px */
.mathblock { overflow-x: auto; overflow-y: hidden; padding: .3em 0; margin: 1rem 0; }
.math-err { font-family: var(--mono); color: var(--bad); border-bottom: 1px dotted var(--bad); cursor: help; }

/* Scroll reveal */
.rv { opacity: 0; transform: translateY(16px); transition: opacity .55s ease, transform .55s ease; }
.rv.in { opacity: 1; transform: none; }

/* Callouts */
.callout { padding: 14px 18px; border-radius: var(--radius); border: 1px solid var(--border); border-left-width: 5px; background: var(--surface); box-shadow: var(--shadow); }
.callout .c-title { font-weight: 700; margin-bottom: 2px; display: flex; gap: 8px; align-items: center; }
.callout p { margin: .4em 0; }
.callout.tip { border-left-color: var(--ok); } .callout.note { border-left-color: var(--accent); }
.callout.warning { border-left-color: var(--warn); background: var(--warn-soft); } .callout.analogy { border-left-color: #a45bd8; }
.callout.key { border-left-color: var(--bad); } .callout.example { border-left-color: #1ba5b8; }

/* Code */
.code-wrap { background: var(--code-bg); color: var(--code-text); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.code-head { display: flex; justify-content: space-between; align-items: center; background: var(--code-head); padding: 6px 12px; font-family: var(--mono); font-size: .78rem; color: #aab1d6; }
.code-wrap pre { margin: 0; padding: 14px 0; overflow-x: auto; font-family: var(--mono); font-size: .9rem; line-height: 1.55; tab-size: 4; }
.code-wrap code { background: none; padding: 0; font-size: inherit; color: inherit; }
.ln { display: block; min-height: 1.55em; padding: 0 16px; white-space: pre; border-left: 3px solid transparent; transition: background .3s, border-color .3s; }
.ln.hl { background: rgba(124,147,255,.18); border-left-color: #7c93ff; }
.tk-kw { color: var(--tok-kw); } .tk-str { color: var(--tok-str); } .tk-num { color: var(--tok-num); } .tk-com { color: var(--tok-com); font-style: italic; }
.tk-fn { color: var(--tok-fn); } .tk-lit { color: var(--tok-lit); } .tk-bi { color: var(--tok-bi); }
.copy-btn { background: transparent; border: 1px solid #3a4070; color: #c6cbe9; border-radius: 8px; padding: 2px 10px; cursor: pointer; font-size: .75rem; }
.copy-btn:hover { background: #2a3060; }
.code-caption { color: var(--muted); font-size: .88rem; margin: 6px 4px 0; }
.term { background: #0b0d1a; color: #cfe8cf; font-family: var(--mono); font-size: .85rem; padding: 12px 16px; border-radius: 10px; overflow-x: auto; white-space: pre-wrap; margin: 8px 0 0; max-height: 360px; }
.out-label { font-size: .8rem; color: var(--muted); margin: 10px 0 2px; font-weight: 600; }

/* Buttons */
.btn { border: 1px solid var(--accent); background: var(--accent); color: var(--accent-ink); padding: 8px 16px; border-radius: 10px; cursor: pointer; font-weight: 600; transition: transform .1s, filter .15s; }
.btn:hover:not(:disabled) { filter: brightness(1.08); } .btn:active:not(:disabled) { transform: scale(.97); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.ghost { background: transparent; color: var(--accent); }
.btn.sm { padding: 4px 12px; font-size: .88rem; }
.btn.ok { background: var(--ok); border-color: var(--ok); }
.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }

/* Stepper */
.stepper, .flow, .quiz, .exercise, .flash, .reveal, .summary, .svgfig, .tablewrap, .frombook { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; box-shadow: var(--shadow); }
.block-title { margin: 0 0 10px; font-size: 1.05rem; display: flex; gap: 8px; align-items: center; }
.stepper-stage { min-height: 120px; }
.stepper-stage.anim-next { animation: slideNext .35s ease; } .stepper-stage.anim-prev { animation: slidePrev .35s ease; }
@keyframes slideNext { from { opacity: 0; transform: translateX(24px); } to { opacity: 1; transform: none; } }
@keyframes slidePrev { from { opacity: 0; transform: translateX(-24px); } to { opacity: 1; transform: none; } }
.step-title { font-weight: 700; font-size: 1.05rem; margin: 0 0 6px; }
.dots { display: flex; gap: 6px; margin: 12px 0; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: var(--border); transition: background .25s, transform .25s; border: 0; padding: 0; cursor: pointer; }
.dot.on { background: var(--accent); transform: scale(1.35); } .dot.past { background: color-mix(in srgb, var(--accent) 45%, var(--border)); }

/* Flow */
.flow-track { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin: 12px 0; }
.flow-node { padding: 10px 14px; border-radius: 12px; border: 2px solid var(--border); background: var(--surface-2); color: var(--muted); font-weight: 600; transition: all .35s; cursor: pointer; }
.flow-node.on { border-color: var(--accent); background: var(--accent-soft); color: var(--text); transform: translateY(-2px); box-shadow: var(--shadow); }
.flow-node.cur { animation: pulse 1.2s ease 2; }
.flow-arrow { color: var(--border); font-size: 1.3rem; transition: color .35s; } .flow-arrow.on { color: var(--accent); }
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 0 rgba(59,91,219,.4); } 50% { box-shadow: 0 0 0 8px rgba(59,91,219,0); } }
.flow-detail { min-height: 3em; padding: 10px 14px; border-radius: 10px; background: var(--surface-2); }

/* Reveal / from book / table / svg */
.reveal .prompt { font-weight: 600; } .reveal .answer { margin-top: 10px; animation: fadeUp .4s ease; }
@keyframes fadeUp { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.frombook { border-left: 5px solid #a45bd8; } .fb-label { font-size: .82rem; font-weight: 700; color: #a45bd8; text-transform: uppercase; letter-spacing: .04em; }
.tablewrap { overflow-x: auto; padding: 0; } table { border-collapse: collapse; width: 100%; font-size: .95rem; }
th, td { padding: 10px 14px; text-align: left; border-bottom: 1px solid var(--border); vertical-align: top; } th { background: var(--surface-2); font-weight: 700; }
.svgfig { text-align: center; } .svgfig svg { max-width: 100%; height: auto; }
.svgfig figcaption { color: var(--muted); font-size: .9rem; margin-top: 8px; }

/* Quiz */
.question { padding: 14px 0; border-top: 1px dashed var(--border); } .question:first-of-type { border-top: 0; }
.q-head { display: flex; gap: 12px; align-items: flex-start; } .q-text p { margin: 0 0 .3em; } .q-text { font-weight: 600; }
.q-num { flex: none; width: 28px; height: 28px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 700; font-size: .85rem; }
.opts { display: grid; gap: 8px; margin: 12px 0 8px 40px; }
.opt { display: flex; gap: 10px; align-items: center; padding: 9px 12px; border: 2px solid var(--border); border-radius: 10px; cursor: pointer; background: var(--surface); transition: border-color .2s, background .2s; }
.opt:hover { border-color: var(--accent); } .opt input { accent-color: var(--accent); }
.opt.sel { border-color: var(--accent); background: var(--accent-soft); }
.opt.good { border-color: var(--ok); background: var(--ok-soft); } .opt.bad { border-color: var(--bad); background: var(--bad-soft); }
.fill-in { margin: 12px 0 8px 40px; } .fill-in input { width: min(420px, 100%); padding: 9px 12px; border-radius: 10px; border: 2px solid var(--border); background: var(--surface); color: var(--text); font: inherit; font-family: var(--mono); }
.fill-in input:focus { outline: none; border-color: var(--accent); } .fill-in.good input { border-color: var(--ok); } .fill-in.bad input { border-color: var(--bad); }
.order-list { list-style: none; margin: 12px 0 8px 40px; padding: 0; display: grid; gap: 8px; }
.order-list li { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border: 2px solid var(--border); border-radius: 10px; background: var(--surface); transition: transform .25s, border-color .2s; }
.order-list li .txt { flex: 1; } .order-list.good li { border-color: var(--ok); } .order-list.bad li { border-color: var(--bad); }
.q-actions { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0 0 40px; }
.q-feedback { margin: 10px 0 0 40px; } .q-feedback .fb { padding: 10px 14px; border-radius: 10px; animation: fadeUp .35s ease; }
.fb.good { background: var(--ok-soft); } .fb.bad { background: var(--bad-soft); } .fb p { margin: .3em 0; }
.hints { margin: 8px 0 0 40px; } .hint { background: var(--warn-soft); padding: 8px 12px; border-radius: 10px; margin-top: 6px; } .pop { animation: fadeUp .3s ease; }
.scorebar { margin-top: 16px; padding: 14px 16px; border-radius: 12px; background: var(--surface-2); animation: fadeUp .4s ease; }
.scorebar.pass { background: var(--ok-soft); } .scorebar.fail { background: var(--bad-soft); }
.scorebar .big { font-size: 1.4rem; font-weight: 800; }
.quiz-foot { margin-top: 14px; display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }

/* Exercise */
.exercise { border-top: 5px solid var(--accent); }
.exercise h4 { margin: 14px 0 6px; font-size: .95rem; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.exercise details { margin: 10px 0; border: 1px solid var(--border); border-radius: 10px; padding: 8px 12px; } .exercise summary { cursor: pointer; font-weight: 600; }
.exercise details > .code-wrap { margin: 10px 0; }
.cmdline { display: flex; gap: 8px; align-items: center; background: var(--surface-2); border-radius: 10px; padding: 8px 12px; font-family: var(--mono); font-size: .88rem; overflow-x: auto; }
.cmdline code { background: none; flex: 1; white-space: pre; }
.checklist { list-style: none; padding: 0; margin: 8px 0; } .checklist li { margin: 6px 0; } .checklist label { display: flex; gap: 10px; cursor: pointer; } .checklist input { accent-color: var(--ok); width: 18px; height: 18px; margin-top: 4px; }
.run-status { margin-top: 10px; font-weight: 600; } .run-status.good { color: var(--ok); } .run-status.bad { color: var(--bad); }
.note-small { color: var(--muted); font-size: .88rem; }

/* Flashcards */
.flash-stage { perspective: 1000px; margin: 12px 0; }
.card { position: relative; height: 190px; cursor: pointer; transform-style: preserve-3d; transition: transform .6s cubic-bezier(.2,.8,.2,1); }
.card.flipped { transform: rotateY(180deg); }
.card .face { position: absolute; inset: 0; backface-visibility: hidden; display: grid; place-items: center; padding: 18px; text-align: center; border-radius: 14px; border: 2px solid var(--border); background: var(--surface-2); font-size: 1.1rem; overflow: auto; }
.card .back { transform: rotateY(180deg); background: var(--accent-soft); border-color: var(--accent); }
.flash-meta { color: var(--muted); font-size: .9rem; margin-bottom: 4px; }

/* Summary / terms */
.summary { background: linear-gradient(135deg, var(--accent-soft), var(--surface)); }
.summary li { margin: .5em 0; list-style: none; position: relative; padding-left: 28px; opacity: 0; transform: translateX(-10px); }
.summary.in li { animation: itemIn .45s ease forwards; } .summary li::before { content: "✓"; position: absolute; left: 0; color: var(--ok); font-weight: 800; }
@keyframes itemIn { to { opacity: 1; transform: none; } }
.summary ul { padding: 0; margin: 8px 0 0; }
.terms { border-top: 1px solid var(--border); margin-top: 36px; padding-top: 4px; } .terms dt { font-weight: 700; margin-top: 10px; } .terms dd { margin: 2px 0 0 0; color: var(--muted); }

/* Completion + nav */
.finish { margin-top: 36px; padding: 20px; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--border); box-shadow: var(--shadow); }
.finish h3 { margin: 0 0 8px; } .pager { display: flex; justify-content: space-between; gap: 12px; margin-top: 24px; flex-wrap: wrap; }
.pager a { flex: 1 1 220px; display: block; padding: 12px 16px; border-radius: 12px; border: 1px solid var(--border); background: var(--surface); text-decoration: none; color: var(--text); transition: border-color .2s, transform .15s; }
.pager a:hover { border-color: var(--accent); transform: translateY(-2px); } .pager small { color: var(--muted); display: block; } .pager .nx { text-align: right; }

/* Home */
.hero { display: flex; gap: 28px; align-items: center; flex-wrap: wrap; margin-bottom: 28px; }
.hero-text { flex: 1 1 320px; } .hero-text h1 { margin-bottom: 6px; }
.ring { position: relative; width: 132px; height: 132px; } .ring svg { transform: rotate(-90deg); } .ring .bg { stroke: var(--surface-2); } .ring .fg { stroke: var(--accent); stroke-linecap: round; transition: stroke-dashoffset 1.1s cubic-bezier(.2,.8,.2,1); }
.ring .pct { position: absolute; inset: 0; display: grid; place-items: center; font-weight: 800; font-size: 1.6rem; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin-top: 18px; margin-bottom: 28px; }
.stat { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); } .stat b { font-size: 1.5rem; display: block; } .stat span { color: var(--muted); font-size: .88rem; }
.ch-card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 20px; margin-top: 14px; margin-bottom: 14px; box-shadow: var(--shadow); }
.ch-card h3 { margin: 0 0 4px; display: flex; gap: 10px; align-items: center; } .ch-num { background: var(--accent); color: var(--accent-ink); border-radius: 10px; padding: 0 10px; font-size: .9rem; }
.ch-card ul { list-style: none; padding: 0; margin: 10px 0 0; } .ch-card li a { display: flex; gap: 10px; padding: 5px 0; text-decoration: none; color: var(--text); } .ch-card li a:hover { color: var(--accent); }
.ch-card .st { color: var(--ok); width: 1.2em; }
.footer-tools { margin-top: 40px; display: flex; gap: 12px; flex-wrap: wrap; align-items: center; color: var(--muted); font-size: .88rem; }

/* Glossary */
.gloss-search { width: 100%; padding: 10px 14px; border-radius: 12px; border: 2px solid var(--border); background: var(--surface); color: var(--text); font: inherit; margin-top: 10px; margin-bottom: 20px; }
.gloss-item { padding: 12px 0; border-bottom: 1px solid var(--border); } .gloss-item b { font-size: 1.05rem; } .gloss-item a { font-size: .82rem; margin-left: 8px; }

/* Confetti */
.confetti { position: fixed; left: 0; top: 0; pointer-events: none; z-index: 100; font-size: 1.6rem; will-change: transform, opacity; }

/* Responsive */
@media (max-width: 900px) {
  .shell { display: block; } #menuBtn { display: inline-block; }
  /* align-self: auto undoes the desktop "start": newer Chrome applies it to fixed boxes, so the drawer grows to its content and cannot scroll */
  .sidebar { position: fixed; top: var(--topbar-h); left: 0; bottom: 0; width: min(88vw, 340px); height: auto; align-self: auto; overscroll-behavior: contain; z-index: 40; transform: translateX(-102%); transition: transform .28s ease; box-shadow: var(--shadow); }
  body.nav-open .sidebar { transform: none; } body.nav-open .scrim { display: block; position: fixed; inset: var(--topbar-h) 0 0 0; background: rgba(10,12,30,.45); z-index: 35; }
  .save-state { display: none; } .opts, .fill-in, .order-list, .q-actions, .q-feedback, .hints { margin-left: 0; } body { font-size: 16px; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; scroll-behavior: auto !important; }
  .rv { opacity: 1; transform: none; } .summary li { opacity: 1; transform: none; }
}
