:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #eef1f7;
  --text: #1c2130;
  --muted: #5d6679;
  --border: #dde2ec;
  --accent: #4f46e5;
  --accent-soft: #e6e5ff;
  --accent-text: #ffffff;
  --good: #16803c;
  --good-soft: #dcf5e4;
  --bad: #c42b2b;
  --bad-soft: #fde2e2;
  --warn: #b45309;
  --target: #f59e0b;
  --blank: #e9ecf5;
  --blank-text: #3b4256;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(20, 24, 40, .06), 0 4px 16px rgba(20, 24, 40, .06);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #11141c;
    --surface: #1a1e29;
    --surface-2: #232838;
    --text: #e8ebf3;
    --muted: #9aa3b8;
    --border: #2e3446;
    --accent: #8b85ff;
    --accent-soft: #2c2a55;
    --accent-text: #11141c;
    --good: #4ade80;
    --good-soft: #173323;
    --bad: #f87171;
    --bad-soft: #3a1c1f;
    --warn: #fbbf24;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

button { font: inherit; color: inherit; cursor: pointer; }

main { max-width: 1100px; margin: 0 auto; padding: 16px 16px 48px; outline: none; }

h1, h2, h3 { line-height: 1.2; margin: 0 0 .5em; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.15rem; }
p { margin: 0 0 1em; }
.muted { color: var(--muted); }

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
  padding: 8px 16px;
}
.brand {
  display: flex; align-items: center; gap: 8px;
  background: none; border: 0; padding: 4px 0;
  font-weight: 700; font-size: 1.05rem;
}
.brand-mark { font-size: 1.3rem; }
.topbar { display: flex; align-items: center; gap: 8px; }
a.brand { color: inherit; text-decoration: none; }
.crumb-sep { color: var(--muted); }
.crumb { background: none; border: 0; padding: 4px 0; font-weight: 600; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.crumb:hover { color: var(--text); }

/* ---------- Unit hub ---------- */
.unit-list { display: grid; gap: 16px; max-width: 760px; }
.unit-card { display: flex; gap: 16px; align-items: center; color: inherit; text-decoration: none; }
.unit-card:hover { border-color: var(--accent); }
.unit-card img { width: 96px; height: 96px; object-fit: contain; background: #fff; border-radius: 10px; flex: none; }
.unit-card h2 { margin-bottom: 4px; }
.unit-card p { margin: 0; font-size: .92rem; }

/* ---------- Cards & buttons ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  margin-bottom: 16px;
}

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 44px; padding: 10px 18px;
  border-radius: 10px; border: 1px solid var(--border);
  background: var(--surface); font-weight: 600;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { background: color-mix(in srgb, var(--accent) 88%, black); }
.btn-ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn-ghost:hover { background: var(--surface-2); color: var(--text); }
.btn-block { width: 100%; }
.btn:disabled { opacity: .5; cursor: default; }
.btn-row { display: flex; flex-wrap: wrap; gap: 8px; }
.link { background: none; border: 0; padding: 0; color: var(--muted); text-decoration: underline; font-size: .9rem; }

/* ---------- Home ---------- */
.hero { margin: 8px 0 20px; }
.hero p { color: var(--muted); max-width: 60ch; }

.home-grid { display: grid; gap: 16px; grid-template-columns: 1fr; }
@media (min-width: 860px) { .home-grid { grid-template-columns: 1.25fr 1fr; align-items: start; } }

.study-tiles { display: grid; grid-template-columns: repeat(2, 1fr); gap: 10px; }
.tile-group { font-size: .8rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 18px 0 8px; }
.tile-group:first-of-type { margin-top: 4px; }
@media (min-width: 560px) { .study-tiles { grid-template-columns: repeat(4, 1fr); } }
.tile {
  display: flex; flex-direction: column; gap: 6px; text-align: left;
  border: 1px solid var(--border); border-radius: 12px; background: var(--surface);
  padding: 8px; overflow: hidden;
}
.tile:hover { border-color: var(--accent); }
.tile-img { background: #fff; border-radius: 8px; height: 110px; display: grid; place-items: center; overflow: hidden; }
.tile-img img { max-width: 100%; max-height: 110px; }
.tile-name { font-weight: 600; font-size: .92rem; padding: 0 2px; }
.tile-progress { font-size: .8rem; color: var(--muted); padding: 0 2px; }
.bar { height: 5px; background: var(--surface-2); border-radius: 99px; overflow: hidden; margin: 0 2px 2px; }
.bar > span { display: block; height: 100%; background: var(--good); border-radius: 99px; }

.field { margin-bottom: 16px; }
.field-label { font-weight: 600; font-size: .9rem; margin-bottom: 6px; display: block; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  min-height: 40px; padding: 8px 14px; border-radius: 99px;
  border: 1px solid var(--border); background: var(--surface); font-weight: 500;
}
.chip[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--text); }
.chip small { color: var(--muted); font-weight: 400; }
.mode-desc { font-size: .85rem; color: var(--muted); margin-top: 6px; min-height: 1.3em; }

.weak-list { display: flex; flex-wrap: wrap; gap: 6px; margin: 10px 0 14px; }
.weak-tag { font-size: .82rem; padding: 3px 10px; border-radius: 99px; background: var(--bad-soft); color: var(--bad); }

/* ---------- Diagram figure ---------- */
.figure-wrap { background: #fff; border-radius: var(--radius); border: 1px solid var(--border); padding: 10px; overflow-x: auto; }
.figure {
  position: relative; margin: 0 auto;
  container-type: inline-size;
  min-width: var(--minw, 0);
  user-select: none; -webkit-user-select: none;
}
.figure img { display: block; width: 100%; height: auto; }
@media (min-width: 860px) {
  .figure { max-width: calc((100vh - 120px) * var(--ar)); }
}

.blank {
  position: absolute;
  display: flex; align-items: center; justify-content: center;
  padding: 0; margin: 0;
  border: 1.5px solid #b7bfd3; border-radius: 4px;
  background: var(--blank); color: var(--blank-text);
  font-size: var(--fs); font-weight: 700; line-height: 1.05;
  text-align: center; white-space: normal; overflow: visible;
  scroll-margin: 80px 0 240px;
  transition: background .15s, border-color .15s, transform .15s;
}
.blank.vertical { writing-mode: vertical-rl; }
.blank.single { white-space: nowrap; padding: 0 .25em; }
.blank.clickable:hover { border-color: var(--accent); background: #dcdcff; }
.blank.target {
  background: #fff4d6; border-color: var(--target); color: #7a4a00;
  box-shadow: 0 0 0 3px rgba(245, 158, 11, .35);
  animation: pulse 1.4s ease-in-out infinite;
}
.blank.revealed { background: #fff; border-color: #cfd6e6; color: #1c2130; font-weight: 700; }
.blank.correct { background: #e3f7ea; border-color: #22a355; color: #0f5d2c; }
.blank.wrong { background: #fde6e6; border-color: #d94141; color: #8f1d1d; }
.blank.flash { animation: flash .9s ease-out 2; }
.blank.prev-good { background: #eefaf2; border-color: #8fd3a8; color: #17663a; }
.blank.prev-retry { background: #fff4e3; border-color: #f2b866; color: #8a4b00; }
.blank.prev-missed { background: #fdeaea; border-color: #eb9a9a; color: #a33; }

.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; justify-content: center; margin-top: 8px; font-size: .78rem; color: #5d6679; }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend .sw { width: 12px; height: 12px; border-radius: 3px; border: 1.5px solid; }
.legend .sw.prev-good { background: #eefaf2; border-color: #8fd3a8; }
.legend .sw.prev-retry { background: #fff4e3; border-color: #f2b866; }
.legend .sw.prev-missed { background: #fdeaea; border-color: #eb9a9a; }
.retry-tag { color: var(--warn); }
.quiz-head .chip { min-height: 36px; padding: 6px 12px; font-size: .85rem; white-space: nowrap; }
.blank .num { font-weight: 700; }

@keyframes pulse { 50% { box-shadow: 0 0 0 6px rgba(245, 158, 11, .15); } }
@keyframes flash { 0% { transform: scale(1.25); box-shadow: 0 0 0 6px rgba(79, 70, 229, .45); } 100% { transform: scale(1); } }
@media (prefers-reduced-motion: reduce) { .blank.target, .blank.flash { animation: none; } }

/* ---------- Quiz layout ---------- */
.quiz-head { display: flex; align-items: center; gap: 12px; margin-bottom: 12px; }
.progress { flex: 1; }
.progress-text { display: flex; justify-content: space-between; gap: 8px; font-size: .88rem; color: var(--muted); margin-bottom: 4px; }
@media (max-width: 520px) {
  .progress-text span:last-child { display: none; }
  .quiz-head { gap: 8px; }
  .quiz-head .btn-ghost { padding: 10px 8px; }
}
.progress .bar { height: 8px; margin: 0; }
.progress .bar > span { background: var(--accent); }

/* minmax(0, …) lets a wide diagram scroll inside its card instead of widening the page. */
.quiz-layout { display: grid; gap: 16px; grid-template-columns: minmax(0, 1fr); }
.quiz-layout.has-figure { grid-template-columns: minmax(0, 1fr); }
.study-layout { grid-template-columns: minmax(0, 1fr); }
@media (min-width: 860px) {
  .quiz-layout.has-figure { grid-template-columns: minmax(0, 2fr) minmax(290px, 1fr); align-items: start; }
  .quiz-layout.has-figure .panel { position: sticky; top: 72px; }
}
@media (max-width: 859px) {
  .quiz-layout.has-figure .panel {
    position: sticky; bottom: 0; z-index: 10;
    margin: 0 -16px -48px; border-radius: var(--radius) var(--radius) 0 0;
    border-bottom: 0; padding-bottom: calc(18px + env(safe-area-inset-bottom));
    box-shadow: 0 -6px 24px rgba(20, 24, 40, .12);
  }
}
.quiz-layout:not(.has-figure) { max-width: 640px; margin: 0 auto; }

.panel { margin-bottom: 0; }
.panel .topic-tag { font-size: .78rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; margin-bottom: 4px; }
.prompt { font-size: 1.15rem; font-weight: 600; margin-bottom: 14px; }
.prompt strong { color: var(--accent); }

.answer-row { display: flex; gap: 8px; }
.answer-input {
  flex: 1; min-width: 0; min-height: 46px; padding: 10px 12px;
  border: 1.5px solid var(--border); border-radius: 10px;
  background: var(--surface); color: var(--text); font-size: 1rem;
}
.answer-input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.choices { display: grid; gap: 8px; }
.choice {
  display: flex; align-items: center; gap: 10px; text-align: left;
  min-height: 46px; padding: 10px 12px;
  border: 1.5px solid var(--border); border-radius: 10px; background: var(--surface);
}
.choice:hover:not(:disabled) { border-color: var(--accent); }
.choice .key {
  flex: none; display: grid; place-items: center; width: 24px; height: 24px;
  border-radius: 6px; background: var(--surface-2); font-size: .8rem; font-weight: 700; color: var(--muted);
}
.choice.correct { border-color: var(--good); background: var(--good-soft); }
.choice.wrong { border-color: var(--bad); background: var(--bad-soft); }
.choice:disabled { cursor: default; }

.feedback { margin-top: 14px; padding: 12px 14px; border-radius: 10px; font-size: .95rem; }
.feedback.good { background: var(--good-soft); }
.feedback.bad { background: var(--bad-soft); }
.feedback .verdict { font-weight: 700; }
.feedback.good .verdict { color: var(--good); }
.feedback.bad .verdict { color: var(--bad); }
.feedback .note { color: var(--warn); font-size: .88rem; margin-top: 4px; }
.panel-actions { display: flex; gap: 8px; margin-top: 14px; }
.panel-actions .btn { flex: 1; }
.hint { font-size: .8rem; color: var(--muted); margin-top: 10px; text-align: center; }
@media (hover: none) { .hint { display: none; } }

/* ---------- Slide notes ---------- */
.slide-title { font-size: .92rem; font-weight: 600; color: var(--muted); margin-bottom: 6px; }
.fill-sentence { line-height: 1.6; font-weight: 500; }
.fill-sentence strong { color: inherit; font-weight: 700; }
.fill-blank {
  display: inline-block; min-width: 4.5em; padding: 0 .4em; margin: 0 .1em; text-align: center;
  border-bottom: 2.5px solid var(--accent); background: var(--accent-soft); border-radius: 4px; color: var(--accent);
}
.fill-blank.good { border-color: var(--good); background: var(--good-soft); color: var(--good); }
.fill-blank.bad { border-color: var(--bad); background: var(--bad-soft); color: var(--bad); }
.nowrap { white-space: nowrap; }
.notes-btn small { color: var(--muted); font-weight: 500; margin-left: 4px; }
.slide-list { display: grid; gap: 12px; }
@media (min-width: 860px) { .slide-list { grid-template-columns: 1fr 1fr; align-items: start; } }
.slide-card { margin: 0; }
.slide-card h3 { font-size: 1rem; }
.slide-card ul { margin: 0; padding-left: 1.2em; }
.slide-card li { margin: 6px 0; line-height: 1.7; }
.note-blank {
  font: inherit; font-weight: 600; padding: 0 .35em; margin: 0 .1em; border: 0; border-radius: 4px;
  border-bottom: 2px solid var(--accent); background: var(--accent-soft); color: var(--text);
}
.note-blank.shown { background: transparent; color: var(--accent); }

/* ---------- Results ---------- */
.score-big { font-size: 3rem; font-weight: 800; line-height: 1; margin: 6px 0; }
.missed-list { list-style: none; padding: 0; margin: 0 0 16px; }
.missed-list li { padding: 10px 0; border-bottom: 1px solid var(--border); }
.missed-list li:last-child { border-bottom: 0; }
.missed-list .t { font-weight: 600; }
.missed-list .m { color: var(--muted); font-size: .92rem; }

/* ---------- Study ---------- */
.diagram-picker { display: flex; gap: 8px; margin-bottom: 14px; }
.diagram-picker select {
  flex: 1; min-width: 0; min-height: 44px; padding: 8px 12px; font: inherit;
  border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text);
}
.diagram-picker .btn { padding: 10px 14px; font-size: 1.2rem; line-height: 1; }
.tabs { display: flex; gap: 6px; overflow-x: auto; margin-bottom: 14px; padding-bottom: 2px; scrollbar-width: none; }
.tab { flex: none; min-height: 40px; padding: 8px 14px; border-radius: 99px; border: 1px solid var(--border); background: var(--surface); font-weight: 500; }
.tab[aria-selected="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.study-layout { display: grid; gap: 16px; }
@media (min-width: 860px) { .study-layout { grid-template-columns: minmax(0, 2fr) minmax(290px, 1fr); align-items: start; } }
.study-tools { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 12px; align-items: center; }
.glossary { list-style: none; margin: 0; padding: 0; }
.glossary li button {
  width: 100%; text-align: left; display: grid; grid-template-columns: 2em 1fr; gap: 4px 8px;
  padding: 8px 6px; border: 0; border-bottom: 1px solid var(--border); background: none; border-radius: 0;
}
.glossary li button:hover { background: var(--surface-2); }
.glossary .n { color: var(--muted); font-weight: 600; }
.glossary .t { font-weight: 600; }
.glossary .m { grid-column: 2; color: var(--muted); font-size: .9rem; margin-top: -4px; }

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