:root {
  --bg: #f3f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --ink: #172033;
  --muted: #5a6579;
  --line: #dde3ec;
  --accent: #2458d6;
  --accent-soft: #e7eefc;
  --phys: #2458d6;
  --phys-soft: #e7eefc;
  --chem: #0b8a67;
  --chem-soft: #e2f5ee;
  --tool: #7c3aed;
  --tool-soft: #f1eafe;
  --danger: #c62828;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 6px 16px rgba(16, 24, 40, .06);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; background: var(--bg); color: var(--ink); font-size: 15px; line-height: 1.55; }
a { color: var(--accent); }
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; }
p { margin: 0 0 .75em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
kbd { font: 12px ui-monospace, Consolas, monospace; background: var(--surface-2); border: 1px solid var(--line); border-bottom-width: 2px; border-radius: 4px; padding: 0 5px; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  padding: 10px 20px; background: var(--surface); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 20;
}
.brand { display: flex; align-items: center; gap: 9px; font-weight: 750; font-size: 1.1rem; color: var(--ink); text-decoration: none; }
.logo {
  width: 30px; height: 30px; border-radius: 8px; display: grid; place-items: center;
  background: linear-gradient(135deg, #2458d6, #0b8a67); color: #fff; font-size: 15px;
}
.home-link { font-size: .85rem; font-weight: 600; color: var(--muted); text-decoration: none; padding-right: 14px; border-right: 1px solid var(--line); }
.home-link:hover { color: var(--ink); }
.topbar .crumb { color: var(--muted); font-weight: 600; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.spacer { flex: 1; }
.name-field { display: flex; align-items: center; gap: 6px; font-size: .9rem; color: var(--muted); }
.name-field input { width: 170px; }

/* ---------- Controls ---------- */
input[type=text], input[type=number], input:not([type]), select, textarea {
  font: inherit; color: inherit; background: var(--surface);
  border: 1px solid var(--line); border-radius: 8px; padding: 6px 9px;
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
textarea { width: 100%; min-height: 84px; resize: vertical; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  font: inherit; font-weight: 600; font-size: .9rem; cursor: pointer; white-space: nowrap;
  border: 1px solid var(--line); background: var(--surface); color: var(--ink);
  border-radius: 8px; padding: 7px 12px; text-decoration: none;
}
.btn:hover { background: var(--surface-2); border-color: #c9d1dd; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.on { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.icon-btn { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 13px; padding: 4px 7px; border-radius: 6px; }
.icon-btn:hover { background: #fdecec; color: var(--danger); }

/* ---------- Library page ---------- */
.hero { max-width: 1180px; margin: 0 auto; padding: 44px 20px 18px; }
.hero h1 { font-size: clamp(1.7rem, 3.5vw, 2.4rem); letter-spacing: -.02em; }
.hero p { font-size: 1.05rem; color: var(--muted); max-width: 720px; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 18px; }
.chip { border: 1px solid var(--line); background: var(--surface); border-radius: 999px; padding: 6px 14px; font: inherit; font-weight: 600; font-size: .9rem; cursor: pointer; color: var(--muted); }
.chip.on { background: var(--ink); border-color: var(--ink); color: #fff; }

.grid { max-width: 1180px; margin: 0 auto; padding: 12px 20px 40px; display: grid; gap: 18px; grid-template-columns: repeat(auto-fill, minmax(290px, 1fr)); }
.lab-card {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
  overflow: hidden; display: flex; flex-direction: column; text-decoration: none; color: inherit; transition: transform .12s, box-shadow .12s;
}
.lab-card:hover { transform: translateY(-2px); box-shadow: 0 2px 4px rgba(16,24,40,.06), 0 12px 28px rgba(16,24,40,.1); }
.lab-card canvas, .lab-card .thumb { width: 100%; aspect-ratio: 16 / 9; display: block; background: #222; }
.lab-card .body { padding: 14px 16px 16px; display: flex; flex-direction: column; gap: 6px; flex: 1; }
.lab-card h3 { margin: 0; font-size: 1.05rem; }
.lab-card p { margin: 0; color: var(--muted); font-size: .92rem; }
.tag { align-self: flex-start; font-size: .72rem; font-weight: 700; letter-spacing: .04em; text-transform: uppercase; padding: 2px 8px; border-radius: 999px; }
.tag.Physics { background: var(--phys-soft); color: var(--phys); }
.tag.Chemistry { background: var(--chem-soft); color: var(--chem); }
.tag.Tool { background: var(--tool-soft); color: var(--tool); }
.topic { font-size: .82rem; color: var(--muted); font-weight: 600; }
.thumb.tool-thumb { display: grid; place-items: center; background: linear-gradient(135deg, #312e81, #7c3aed); color: #fff; font-size: 54px; }

.teacher { max-width: 1180px; margin: 0 auto 60px; padding: 0 20px; }
.teacher .card { padding: 22px 24px; }
.teacher ol, .teacher ul { padding-left: 1.2em; margin: 0; }
.teacher li { margin-bottom: 6px; }
.cols { display: grid; gap: 22px; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); }

/* ---------- Lab page ---------- */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; }
.lab-layout {
  max-width: 1500px; margin: 0 auto; padding: 18px 20px 60px;
  display: grid; gap: 18px; grid-template-columns: minmax(0, 1.25fr) minmax(0, 1fr); align-items: start;
}
.stage-col { position: sticky; top: 68px; display: flex; flex-direction: column; gap: 14px; }
.work-col { display: flex; flex-direction: column; gap: 18px; min-width: 0; }
.stage-card { padding: 12px; }
.stage-wrap { position: relative; border-radius: 8px; overflow: hidden; background: #111; }
#stage { width: 100%; aspect-ratio: 16 / 9; display: block; touch-action: none; }
#stage.mark-mode { cursor: crosshair; }

.controls { display: flex; align-items: center; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.controls input[type=range] { flex: 1; min-width: 120px; accent-color: var(--accent); }
.time-readout { font: 600 14px ui-monospace, Consolas, monospace; min-width: 118px; text-align: right; }
.play-btn { width: 42px; height: 36px; font-size: 15px; }

.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--line); }
.toolbar label.inline { display: flex; align-items: center; gap: 6px; font-size: .85rem; color: var(--muted); }
.hint { font-size: .85rem; color: var(--muted); margin-top: 8px; min-height: 1.3em; }
.hint b { color: var(--ink); }

.params { display: flex; flex-direction: column; gap: 10px; }
.param-label { font-size: .8rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; margin-bottom: 4px; }
.seg { display: flex; flex-wrap: wrap; gap: 6px; }
.seg button { font: inherit; font-size: .9rem; font-weight: 600; padding: 6px 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; }
.seg button:hover { border-color: #b9c3d2; }
.seg button[aria-checked=true] { background: var(--ink); color: #fff; border-color: var(--ink); }

.intro-card .subject-line { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.intro-card ol { padding-left: 1.25em; margin: .25em 0 0; }
.intro-card li { margin-bottom: 4px; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.section-head h2 { margin: 0; }
.step-num { display: inline-grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: var(--accent); color: #fff; font-size: .8rem; margin-right: 8px; vertical-align: 2px; }

/* Data table */
.table-scroll { overflow-x: auto; border: 1px solid var(--line); border-radius: 8px; max-height: 420px; overflow-y: auto; }
table.data { border-collapse: collapse; width: 100%; font-size: .9rem; }
table.data th { position: sticky; top: 0; background: var(--surface-2); text-align: left; font-weight: 700; padding: 8px; border-bottom: 1px solid var(--line); white-space: nowrap; z-index: 1; }
table.data th .unit { font-weight: 500; color: var(--muted); }
table.data th .calc { margin-left: 5px; font-style: italic; color: var(--accent); font-family: Georgia, serif; }
table.data td { padding: 3px 5px; border-bottom: 1px solid #edf1f6; }
table.data td.idx { color: var(--muted); text-align: center; width: 34px; font-size: .8rem; }
table.data td input { width: 100%; min-width: 64px; border-color: transparent; background: transparent; padding: 5px 6px; }
table.data td input:hover { border-color: var(--line); }
table.data td input:focus { background: #fff; }
table.data td.computed { color: var(--accent); font-variant-numeric: tabular-nums; padding-left: 11px; }
table.data tr.empty td { text-align: center; color: var(--muted); padding: 22px; }
.row-btns { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 10px; }

/* Graph */
.graph-controls { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; margin-bottom: 10px; }
.graph-controls label { display: flex; flex-direction: column; gap: 3px; font-size: .78rem; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .03em; }
.graph-controls select { font-size: .9rem; text-transform: none; letter-spacing: 0; font-weight: 500; color: var(--ink); }
.graph-canvas-wrap { border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.graph-canvas-wrap canvas { display: block; width: 100%; }
.fit-results { margin-top: 10px; display: flex; flex-direction: column; gap: 6px; font-size: .9rem; }
.fit-row { background: var(--surface-2); border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; font-variant-numeric: tabular-nums; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: 0; }

/* Questions */
.question { padding: 12px 0; border-top: 1px solid var(--line); }
.question:first-of-type { border-top: 0; padding-top: 0; }
.question .prompt { font-weight: 600; margin-bottom: 8px; }
.question .qn { color: var(--accent); margin-right: 4px; }
.choices { display: flex; flex-direction: column; gap: 6px; }
.choices label { display: flex; gap: 8px; align-items: flex-start; padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; }
.choices label:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.saved-note { font-size: .8rem; color: var(--muted); }

/* Video analysis */
.video-drop { aspect-ratio: 16/9; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; text-align: center; padding: 20px; border: 2px dashed #c3ccda; border-radius: 8px; background: var(--surface-2); }
.video-drop.drag { border-color: var(--accent); background: var(--accent-soft); }
.video-wrap { position: relative; background: #000; border-radius: 8px; overflow: hidden; }
.video-wrap video, .video-wrap canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.video-wrap canvas { touch-action: none; }
.scale-form { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 10px; padding: 10px; background: #fff8e1; border: 1px solid #f3d27a; border-radius: 8px; font-size: .9rem; }
.scale-form input { width: 90px; }

/* Practice */
.practice-main { max-width: 900px; margin: 0 auto; padding: 18px 20px 60px; }
.practice-wrap { display: flex; flex-direction: column; gap: 18px; }
.quiz-meta { font-size: .85rem; color: var(--muted); font-weight: 600; margin-bottom: 8px; display: flex; gap: 10px; align-items: center; }
.quiz-q { font-size: 1.05rem; font-weight: 600; margin-bottom: 12px; }
.quiz-choices { display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px; }
.choice-btn { text-align: left; font: inherit; padding: 10px 14px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); cursor: pointer; }
.choice-btn:hover:not(:disabled) { border-color: var(--accent); background: var(--accent-soft); }
.choice-btn:disabled { cursor: default; color: var(--muted); }
.choice-btn.ok { border-color: #16a34a; background: #dcfce7; color: #14532d; font-weight: 600; }
.choice-btn.bad { border-color: #dc2626; background: #fee2e2; color: #7f1d1d; }
.feedback { border-radius: 8px; padding: 10px 12px; margin: 8px 0 10px; font-size: .93rem; }
.feedback.ok { background: #dcfce7; border: 1px solid #86efac; }
.feedback.bad { background: #fef2f2; border: 1px solid #fecaca; }
.feedback.hint { background: #fffbeb; border: 1px solid #fde68a; }
.feedback.sol { background: var(--accent-soft); border: 1px solid #bfd1f7; }
.hearts { color: #dc2626; letter-spacing: 2px; font-size: 1rem; }
.hearts .lost { color: #e5e7eb; }
.quiz-end { text-align: center; padding: 10px 0; }
.quiz-end .big { font-size: 2.4rem; font-weight: 800; color: var(--accent); }
.problem { border-top: 1px solid var(--line); padding: 14px 0; }
.problem:first-child { border-top: 0; padding-top: 0; }
.problem-head { display: flex; justify-content: space-between; gap: 10px; }
.problem-text { margin: 6px 0 10px; }
.answer-row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.answer-row input { width: 150px; }
.unit-label { color: var(--muted); font-weight: 600; min-width: 30px; }
.hub { max-width: 1180px; margin: 0 auto; padding: 0 20px 60px; display: grid; gap: 18px; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); align-items: start; }
.hub-row { display: grid; grid-template-columns: 1fr; padding: 10px 4px; border-top: 1px solid var(--line); text-decoration: none; color: inherit; }
.hub-row:hover .hub-title { color: var(--accent); }
.hub-title { font-weight: 700; }
.hub-stats { font-size: .82rem; color: var(--muted); }
.next-steps { display: flex; gap: 10px; flex-wrap: wrap; }
.mc-check { margin-top: 8px; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.mc-check .feedback { margin: 0; flex: 1 1 100%; }
.topnav { display: flex; gap: 8px; flex-wrap: wrap; }

/* Teacher guide */
.teacher-main { max-width: 1300px; margin: 0 auto; padding: 18px 20px 60px; }
table.guide { border-collapse: collapse; width: 100%; font-size: .9rem; }
table.guide th { text-align: left; background: var(--surface-2); padding: 8px; border-bottom: 1px solid var(--line); }
table.guide td { padding: 8px; border-bottom: 1px solid #edf1f6; vertical-align: top; }
table.guide .key { display: none; color: #14532d; }
body.show-keys table.guide .key { display: block; }
.ngss { font: 600 12px ui-monospace, Consolas, monospace; background: var(--surface-2); border: 1px solid var(--line); border-radius: 4px; padding: 1px 5px; white-space: nowrap; }

/* Report (print only) */
#report { display: none; }
@media print {
  body { background: #fff; }
  body:not(.print-all) > *:not(#report) { display: none !important; }
  body.print-all .topbar { display: none !important; }
  #report { display: block; font-size: 12pt; }
  #report h1 { font-size: 18pt; }
  #report img { max-width: 100%; border: 1px solid #ccc; }
  #report table { border-collapse: collapse; margin: 8px 0 16px; }
  #report th, #report td { border: 1px solid #999; padding: 3px 8px; }
  #report .ans { border-left: 3px solid #999; padding-left: 10px; margin: 4px 0 14px; white-space: pre-wrap; }
  #report section { break-inside: avoid; }
}

@media (max-width: 980px) {
  .lab-layout { grid-template-columns: minmax(0, 1fr); padding: 12px 16px 40px; }
  .stage-col { position: static; }
}
@media (max-width: 560px) {
  .topbar { padding: 8px 16px; }
  .name-field input { width: 130px; }
  .home-link { padding-right: 10px; }
  .hero { padding: 28px 16px 10px; }
  .grid, .teacher { padding-left: 16px; padding-right: 16px; }
  .card { padding: 14px; }
}
