:root {
  --bg: #f6f4ee;
  --panel: #ffffff;
  --ink: #221f1b;
  --muted: #5f5a50;
  --line: #ddd8cc;
  --grid: #e6e2d8;
  --chart-bg: #ffffff;
  --soot-band: rgba(55, 50, 45, 0.16);
  --dark-line: #c2410c;
  --allele: #2563eb;
  --accent: #7c3a10;
  --accent-ink: #ffffff;
  --focus: #2563eb;
  --g-CC: #1c1917;
  --g-Cc: #57534e;
  --g-cc: #d6cfbb;
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17150f;
    --panel: #221f19;
    --ink: #eeeae0;
    --muted: #b0a998;
    --line: #3a362d;
    --grid: #34302a;
    --chart-bg: #1c1a15;
    --soot-band: rgba(255, 250, 240, 0.10);
    --dark-line: #fb8a4c;
    --allele: #7aa7ff;
    --accent: #e6a26a;
    --accent-ink: #1a1208;
    --focus: #7aa7ff;
    --g-CC: #0b0a09;
    --g-Cc: #6f6a62;
    --g-cc: #e4ddc9;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
h1, h2, h3 { line-height: 1.2; margin: 0 0 .5rem; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }
h3 { font-size: 1rem; margin-top: 1rem; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

.topbar {
  display: flex; flex-wrap: wrap; align-items: baseline; justify-content: space-between;
  gap: .25rem 1rem;
  padding: 1rem clamp(16px, 3vw, 2rem) .5rem;
}
.whoami { margin: 0; color: var(--muted); }
.whoami strong { color: var(--ink); }
.save-state { display: block; font-size: .85rem; color: var(--dark-line); }

/* ---------- Student layout ---------- */

.student {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 320px;
  grid-template-areas:
    "scene controls"
    "chart controls"
    "log   about";
  gap: 1rem;
  padding: .5rem clamp(16px, 3vw, 2rem) 2rem;
  max-width: 1300px;
  margin: 0 auto;
}
.scene-panel { grid-area: scene; }
.controls { grid-area: controls; align-self: start; }
.chart-panel { grid-area: chart; }
.log-panel { grid-area: log; align-self: start; }
.about { grid-area: about; }

.scene-panel, .controls, .chart-panel, .log-panel, .about {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 1rem;
  min-width: 0;
}

.scene { position: relative; }
#scene {
  display: block; width: 100%; aspect-ratio: 16 / 7;
  border-radius: 6px; background: #8f8874;
}
.scene-badge {
  position: absolute; top: .5rem; left: .5rem;
  padding: .2rem .6rem; border-radius: 999px;
  font-size: .85rem; font-weight: 600;
  background: rgba(255, 255, 255, .88); color: #221f1b;
}
.scene-badge.sooty { background: rgba(0, 0, 0, .7); color: #fff; }
.caption { margin: .4rem 0 .75rem; font-size: .85rem; color: var(--muted); }

.stats {
  display: grid; grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: .5rem; margin: 0 0 .75rem;
}
.stats div { border-left: 3px solid var(--line); padding-left: .6rem; }
.stats dt { font-size: .8rem; color: var(--muted); }
.stats dd { margin: 0; font-size: 1.35rem; font-weight: 650; font-variant-numeric: tabular-nums; }
.stats small { font-size: .8rem; font-weight: 400; color: var(--muted); }

.geno-bar {
  display: flex; height: 14px; border-radius: 7px; overflow: hidden;
  background: var(--g-cc); border: 1px solid var(--line);
}
.geno-bar span { transition: width .15s; }
.g-CC { background: var(--g-CC); }
.g-Cc { background: var(--g-Cc); }
.g-cc { background: var(--g-cc); }
.geno-legend { list-style: none; display: flex; flex-wrap: wrap; gap: .25rem 1.25rem; padding: 0; margin: .4rem 0 0; font-size: .9rem; }
.geno-legend b { font-variant-numeric: tabular-nums; }
.sw { display: inline-block; width: .8rem; height: .8rem; border-radius: 3px; margin-right: .35rem; vertical-align: -1px; border: 1px solid var(--line); }

/* ---------- Controls ---------- */

.controls { display: flex; flex-direction: column; gap: 1rem; }
.btn-row { display: flex; gap: .5rem; }
.btn-row .btn { flex: 1; }

.btn {
  min-height: 44px; padding: .5rem .9rem;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg); color: var(--ink);
  font: inherit; font-weight: 600; cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--muted); background: var(--panel); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.1); background: var(--accent); }
.btn.subtle { background: transparent; font-weight: 500; }
.btn.danger { color: #b91c1c; }
@media (prefers-color-scheme: dark) { .btn.danger { color: #f87171; } }

fieldset { border: 0; padding: 0; margin: 0; }
legend { font-weight: 600; margin-bottom: .4rem; padding: 0; }
.bark-toggle { display: grid; grid-template-columns: 1fr 1fr; gap: .5rem; }
.bark-toggle legend { grid-column: 1 / -1; }
.bark-toggle label { position: relative; cursor: pointer; }
.bark-toggle input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.bark-toggle span {
  display: flex; align-items: center; justify-content: center; text-align: center;
  min-height: 52px; padding: .4rem; border-radius: 8px;
  border: 2px solid var(--line); font-weight: 600; font-size: .95rem;
}
.bark-toggle input[value="clean"] + span { background: #d8d2bd; color: #221f1b; }
.bark-toggle input[value="sooty"] + span { background: #2f2b27; color: #f3efe6; }
.bark-toggle label:hover span { border-color: var(--muted); }
.bark-toggle input:checked + span { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); }
.bark-toggle input:focus-visible + span { outline: 3px solid var(--focus); outline-offset: 2px; }

.field { display: flex; flex-direction: column; gap: .3rem; font-weight: 600; }
.field small { font-weight: 400; color: var(--muted); font-size: .82rem; }
.field output { font-variant-numeric: tabular-nums; }
input[type="range"] { width: 100%; min-height: 32px; accent-color: var(--accent); }
select {
  min-height: 44px; padding: .4rem .5rem; font: inherit; font-weight: 400;
  border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink);
  max-width: 100%;
}

/* ---------- Chart ---------- */

.chart { display: block; width: 100%; height: 300px; }
.legend { list-style: none; display: flex; flex-wrap: wrap; gap: .25rem 1.25rem; padding: 0; margin: 0 0 .5rem; font-size: .88rem; color: var(--muted); }
.legend i { display: inline-block; vertical-align: middle; margin-right: .4rem; }
.ln { width: 22px; height: 0; border-top: 3px solid var(--dark-line); }
.ln.allele { border-top: 2px dashed var(--allele); }
.band { width: 16px; height: 12px; background: var(--soot-band); border: 1px solid var(--line); }

/* ---------- Log + about ---------- */

.log { list-style: none; margin: 0; padding: 0; max-height: 260px; overflow-y: auto; font-size: .92rem; }
.log li { padding: .35rem 0; border-bottom: 1px solid var(--line); }
.log li:empty { display: none; }
.log:empty::before { content: "Nothing yet. Press Run to start."; color: var(--muted); }
.ev-gen { display: inline-block; min-width: 4.5rem; font-weight: 600; font-variant-numeric: tabular-nums; color: var(--muted); }
.ev-mutation .ev-gen, .ev-majority .ev-gen { color: var(--dark-line); }
.about { font-size: .95rem; }
.about p { margin: 0 0 .6rem; }
.about ul { margin: 0; padding-left: 1.2rem; }
.about li { margin-bottom: .3rem; }

@media (max-width: 860px) {
  .student {
    grid-template-columns: minmax(0, 1fr);
    grid-template-areas: "scene" "controls" "chart" "log" "about";
  }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #scene { aspect-ratio: 4 / 3; }
  .chart { height: 240px; }
}

/* ---------- Class board ---------- */

.board-page { font-size: 18px; }
.board-head {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: .75rem 1.5rem; padding: 1rem clamp(16px, 3vw, 2rem) .5rem;
}
.board-head h1 { font-size: 1.8rem; margin: 0; }
.join-hint { margin: 0; color: var(--muted); }
.join-hint code { font-size: 1.05em; color: var(--ink); font-weight: 650; }
.board { padding: .5rem clamp(16px, 3vw, 2rem) 2rem; }

.summary {
  display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); gap: 1rem; margin-bottom: 1rem;
}
.summary > div { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 1rem; min-width: 0; }
#overlay { display: block; width: 100%; height: 260px; }
.tallies { display: grid; grid-template-columns: 1fr 1fr; gap: .75rem 1rem; margin: 0; }
.tallies div { min-width: 0; }
.tallies dt { font-size: .85rem; color: var(--muted); }
.tallies dd { margin: 0; font-size: 2rem; font-weight: 700; font-variant-numeric: tabular-nums; line-height: 1.1; }
.tallies dd small { display: block; font-size: .8rem; font-weight: 400; color: var(--muted); }

.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: .75rem; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: .6rem .7rem .7rem; min-width: 0;
}
.card.stale { opacity: .55; }
.card-top { display: flex; justify-content: space-between; align-items: baseline; gap: .5rem; }
.card-name { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-pct { flex-shrink: 0; white-space: nowrap; font-size: 1.3rem; font-weight: 700; color: var(--dark-line); font-variant-numeric: tabular-nums; }
.card-pct small { font-size: .7rem; font-weight: 600; }
.card-meta { display: flex; flex-wrap: wrap; gap: .5rem; align-items: center; font-size: .8rem; color: var(--muted); margin: .1rem 0 .35rem; }
.chip { padding: 0 .45rem; border-radius: 999px; font-weight: 600; font-size: .75rem; }
.chip.clean { background: #d8d2bd; color: #221f1b; }
.chip.sooty { background: #2f2b27; color: #f3efe6; }
.chip.sample { border: 1px dashed var(--muted); }
.card canvas { display: block; width: 100%; height: 80px; border-radius: 4px; }
.empty { text-align: center; color: var(--muted); padding: 3rem 1rem; background: var(--panel); border: 1px dashed var(--line); border-radius: var(--radius); }

.teacher { position: relative; }
.teacher summary { cursor: pointer; min-height: 44px; display: flex; align-items: center; padding: 0 .9rem; border: 1px solid var(--line); border-radius: 8px; font-weight: 600; list-style: none; }
.teacher summary::-webkit-details-marker { display: none; }
.teacher summary:hover { border-color: var(--muted); }
.teacher-panel {
  position: absolute; right: 0; top: calc(100% + .4rem); z-index: 5; width: min(320px, calc(100vw - 32px));
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 1rem; display: flex; flex-direction: column; gap: .6rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .15); font-size: 16px;
}
.teacher-panel input[type="password"] { min-height: 44px; padding: .4rem .6rem; font: inherit; border: 1px solid var(--line); border-radius: 8px; background: var(--bg); color: var(--ink); width: 100%; }
.teacher-msg { margin: 0; font-size: .9rem; color: var(--muted); min-height: 1.3em; }

@media (max-width: 860px) {
  .board-page { font-size: 16px; }
  .summary { grid-template-columns: minmax(0, 1fr); }
  .teacher-panel { position: static; width: 100%; margin-top: .5rem; box-shadow: none; }
  .teacher[open] { flex-basis: 100%; }
}

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.ln.faint { opacity: .35; }
.board-head .btn-row { align-items: center; flex-wrap: wrap; }
