:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --text: #1d1d1b;
  --muted: #6b6a65;
  --line: #dddbd4;
  --accent: #1f5fbf;
  --accent-text: #ffffff;
  --good: #1f7a3d;
  --good-bg: #e3f3e8;
  --bad: #b3261e;
  --bad-bg: #fbe9e7;
  --warn-bg: #fff4d6;
  --bar: #9db8e3;
  --bar-correct: #5fae78;
  --radius: 10px;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161615;
    --surface: #222220;
    --text: #ecebe6;
    --muted: #a3a29b;
    --line: #3a3936;
    --accent: #6ea2f0;
    --accent-text: #0e1726;
    --good: #7fd197;
    --good-bg: #1d3325;
    --bad: #f2a29b;
    --bad-bg: #3d2120;
    --warn-bg: #3a3218;
    --bar: #3f6aa8;
    --bar-correct: #3f8f5a;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 17px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
main { padding: 0 16px 80px; }
h1 { font-size: 1.6rem; margin: 1.2rem 0 0.8rem; }
h2 { font-size: 1.2rem; margin: 1.6rem 0 0.6rem; }
h3 { font-size: 1.05rem; margin: 0 0 0.6rem; }
a { color: var(--accent); }
p { margin: 0.4rem 0; }

.narrow { max-width: 440px; margin: 0 auto; }
.page { max-width: 820px; margin: 0 auto; }
.page.wide { max-width: 1100px; }
.worksheet { max-width: 720px; margin: 0 auto; }
.muted { color: var(--muted); }
.small { font-size: 0.85rem; }
.error { color: var(--bad); }
.grow { flex: 1; min-width: 0; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin: 12px 0;
}
.link-card { text-decoration: none; color: inherit; }
.link-card:hover { border-color: var(--accent); }
.notice { background: var(--warn-bg); }

/* ---- Controls ---- */
button, .button {
  font: inherit;
  font-size: 0.95rem;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  text-decoration: none;
  min-height: 40px;
}
button:hover:not(:disabled), .button:hover { border-color: var(--accent); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary, .button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.danger { color: var(--bad); border-color: var(--bad); }
button.danger.big { background: var(--bad); color: #fff; }
button.big, .button.big { font-size: 1.1rem; padding: 12px 22px; font-weight: 600; }
button.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.link { border: 0; background: none; padding: 4px; min-height: 0; color: var(--accent); }
button.small { min-height: 34px; padding: 4px 10px; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

input, textarea, select {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  width: 100%;
}
textarea { resize: vertical; min-height: 64px; overflow: hidden; }
select { width: auto; max-width: 100%; }
input[type="radio"], input[type="checkbox"] { width: 20px; height: 20px; margin: 0; flex: none; accent-color: var(--accent); }
.field { margin-bottom: 14px; }
.field label { display: block; font-weight: 600; margin-bottom: 4px; }
.code-input { text-transform: uppercase; letter-spacing: 0.2em; font-size: 1.4rem; font-weight: 700; }

/* ---- Header bars ---- */
.bar {
  display: flex;
  gap: 12px;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
  margin-bottom: 8px;
}
.bar h1 { margin: 0; }
.bar.sticky { position: sticky; top: 0; background: var(--bg); z-index: 5; }
.bar-right { display: flex; align-items: center; gap: 8px; }
.status { font-size: 0.85rem; padding: 3px 10px; border-radius: 99px; }
.status.good { color: var(--good); background: var(--good-bg); }
.status.busy { color: var(--muted); background: var(--surface); }
.status.bad { color: var(--bad); background: var(--bad-bg); }
.code { font-size: 1.8rem; font-weight: 800; letter-spacing: 0.15em; font-variant-numeric: tabular-nums; }
.small-code { font-size: 1.2rem; }
.join-info { display: flex; flex-direction: column; align-items: center; }

/* ---- Student worksheet ---- */
.section { color: var(--muted); font-size: 0.95rem; text-transform: uppercase; letter-spacing: 0.04em; }
.stage { margin-bottom: 24px; }
.stage.new { animation: arrive 2.5s ease-out; border-radius: var(--radius); }
@keyframes arrive { from { background: var(--warn-bg); } to { background: transparent; } }
@media (prefers-reduced-motion: reduce) { .stage.new { animation: none; } }
.question {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin: 10px 0;
}
.question.locked { opacity: 0.85; }
.q-head { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.q-id { font-size: 0.8rem; font-weight: 700; color: var(--muted); }
.prompt { font-weight: 600; margin: 6px 0 10px; }
.badge { font-size: 0.75rem; padding: 2px 8px; border-radius: 99px; background: var(--warn-bg); }
.badge.lock { background: var(--line); }
.badge.plain { background: none; border: 1px solid var(--line); }
.choices { display: flex; flex-direction: column; gap: 6px; }
.choices.inline { flex-direction: row; flex-wrap: wrap; }
.choice {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  cursor: pointer;
}
.choice:has(input:checked) { border-color: var(--accent); }
.choice.correct { border-color: var(--good); background: var(--good-bg); }
.tick { color: var(--good); font-weight: 700; }
.check { display: flex; gap: 12px; align-items: center; font-weight: 600; cursor: pointer; padding: 4px 0; }
.check input { width: 26px; height: 26px; }
.match { display: flex; flex-direction: column; gap: 8px; }
.match-row { display: flex; gap: 10px; align-items: center; justify-content: space-between; flex-wrap: wrap; }
.order { padding-left: 1.6em; margin: 0; }
.order li { padding: 4px 0; }
.order li > * { vertical-align: middle; }
.order-text { display: inline-block; min-width: 50%; margin-right: 8px; }
.saved { font-size: 0.8rem; color: var(--muted); min-height: 1.2em; margin-top: 4px; }
.saved.bad { color: var(--bad); }
.showback { border-left: 3px solid var(--line); padding: 4px 10px; margin: 8px 0; }
.answer { border-left: 3px solid var(--good); background: var(--good-bg); padding: 8px 12px; margin-top: 10px; border-radius: 0 8px 8px 0; }
.answer-label { font-size: 0.75rem; font-weight: 700; text-transform: uppercase; color: var(--good); }
.answer ol, .answer ul { margin: 4px 0; }
.class-tally { margin-top: 10px; }
.toast {
  position: fixed;
  left: 50%;
  bottom: 20px;
  transform: translateX(-50%);
  background: var(--accent);
  color: var(--accent-text);
  border: 0;
  box-shadow: 0 4px 18px rgb(0 0 0 / 0.25);
  font-weight: 600;
  z-index: 20;
  max-width: calc(100vw - 32px);
}
.error-toast { background: var(--bad); color: #fff; padding: 10px 16px; border-radius: 8px; }

/* ---- Tallies ---- */
.tally { display: flex; flex-direction: column; gap: 6px; margin: 8px 0; }
.bar-row { display: grid; grid-template-columns: minmax(0, 14em) 1fr 2.5em; gap: 10px; align-items: center; }
.bar-row:has(.bar-names) { grid-template-columns: minmax(0, 14em) minmax(60px, 1fr) 2em minmax(0, 1.4fr); }
.bar-label { overflow-wrap: anywhere; }
.bar-row.correct .bar-label { color: var(--good); font-weight: 600; }
.bar-track { height: 16px; background: var(--bg); border-radius: 4px; overflow: hidden; border: 1px solid var(--line); }
.bar-fill { display: block; height: 100%; background: var(--bar); transition: width 0.3s; }
.bar-row.correct .bar-fill { background: var(--bar-correct); }
.bar-count { font-variant-numeric: tabular-nums; text-align: right; font-weight: 600; }
@media (max-width: 560px) {
  .bar-row, .bar-row:has(.bar-names) { grid-template-columns: 1fr 2.5em; }
  .bar-label, .bar-names { grid-column: 1 / -1; }
}

/* ---- Instructor control screen ---- */
.people { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0 4px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0; }
.chip { font-size: 0.85rem; padding: 3px 10px; border-radius: 99px; border: 1px solid var(--line); background: var(--surface); min-height: 0; }
.chip.here { border-color: var(--good); color: var(--good); }
.person .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--line); display: inline-block; }
.person.here .dot { background: var(--good); }
.stage-card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); margin: 8px 0; }
.stage-card.next { border: 2px solid var(--accent); }
.stage-card.unlocked { border-left: 4px solid var(--good); }
.stage-head { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 10px 14px; flex-wrap: wrap; }
.stage-title { font-weight: 700; font-size: 1.05rem; color: var(--text) !important; text-align: left; }
.caret { width: 1em; display: inline-block; color: var(--muted); }
.stage-body { padding: 0 14px 10px; }
.iq { border-top: 1px solid var(--line); padding: 10px 0; }
.note { font-size: 0.85rem; font-style: italic; color: var(--muted); }
.note::before { content: "Note: "; font-weight: 600; }
.key { font-size: 0.9rem; }
.answers { margin: 6px 0; padding-left: 1.2em; }
.answers li { margin: 2px 0; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 6px; }
.pill { font-size: 0.8rem; padding: 3px 10px; border-radius: 99px; }
.pill.good { color: var(--good); background: var(--good-bg); }
.issues { background: var(--bad-bg); border-radius: 8px; padding: 8px 12px; margin-top: 10px; font-size: 0.9rem; }
.issues ul { margin: 4px 0; padding-left: 1.2em; }
.overlay { position: fixed; inset: 0; background: rgb(0 0 0 / 0.45); display: grid; place-items: center; z-index: 30; padding: 16px; }
.dialog { max-width: 460px; width: 100%; }
body.offline .bar.sticky { box-shadow: inset 0 -3px 0 var(--bad); }

/* ---- Projector ---- */
body.projector { font-size: 24px; }
.proj { max-width: 1200px; margin: 0 auto; padding: 20px 0; }
.proj-head { display: flex; justify-content: space-between; gap: 20px; flex-wrap: wrap; color: var(--muted); font-size: 0.8em; }
.proj-section { color: var(--muted); margin-top: 1.5em; }
.proj h1 { font-size: 2.6em; margin: 0.2em 0 0.6em; }
.proj-tally h2 { font-size: 1.2em; }
.proj .bar-track { height: 28px; }
.proj .bar-row { grid-template-columns: minmax(0, 16em) 1fr 3em; }
.worksheet-bar { max-width: 720px; margin: 0 auto 8px; }
