:root {
  --paper: #0d1117;
  --panel: #161b22;
  --panel-border: #30363d;
  --ink: #f0f6fc;
  --ink-soft: #8b949e;
  --muted: #6e7681;
  --line: #21262d;
  --line-strong: #363b42;
  
  --accent: #38bdf8;
  --accent-glow: rgba(56, 189, 248, 0.25);
  --accent-tint: #0c2d48;
  
  --accent-purple: #a855f7;
  --accent-purple-tint: #2e1065;
  
  --ok: #34d399;
  --ok-tint: #064e3b;
  --bad: #f87171;
  --bad-tint: #4c1d1d;
  
  --shadow: 0 4px 20px rgba(0, 0, 0, 0.4);
  --radius: 8px;
  --font-mono: 'IBM Plex Mono', ui-monospace, SFMono-Regular, Consolas, monospace;
  --font-body: 'IBM Plex Sans', system-ui, -apple-system, sans-serif;
}

* { box-sizing: border-box; }
body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  margin: 0;
  padding: 0 20px 40px;
  -webkit-font-smoothing: antialiased;
}

.page {
  max-width: 1080px;
  margin-inline: auto;
}

/* Topbar */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: rgba(13, 17, 23, 0.9);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--panel-border);
  padding: 14px 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-logo { height: 42px; width: auto; }
.brand-logo-fallback {
  height: 40px; width: 40px; border-radius: 8px;
  background: var(--accent); color: var(--paper);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 14px;
}
.brand-text .school { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--accent); font-weight: 600; }
.brand-text h1 { font-size: 20px; font-weight: 700; margin: 0; color: var(--ink); }

/* Setup Layout */
.setup-screen {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 24px;
  margin-top: 24px;
}
@media (max-width: 820px) {
  .setup-screen { grid-template-columns: 1fr; }
}

.setup-main { display: flex; flex-direction: column; gap: 20px; }

.setup-card, .leaderboard-card, .reference-card {
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: var(--radius);
  padding: 24px;
  box-shadow: var(--shadow);
}

.setup-card h2, .leaderboard-card h3, .reference-card h3 { margin-top: 0; color: var(--ink); }
.setup-desc { font-size: 14px; color: var(--ink-soft); line-height: 1.5; }

/* Toggle Group / Slider */
.config-group { margin: 20px 0 16px; }
.config-label { display: block; font-size: 11px; font-weight: 700; text-transform: uppercase; color: var(--ink-soft); margin-bottom: 10px; }

.toggle-group {
  display: inline-flex;
  background: var(--paper);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 4px;
  gap: 4px;
  width: 100%;
}
.toggle-btn {
  flex: 1;
  border: 0; background: transparent;
  font-family: var(--font-body); font-size: 13px; font-weight: 600;
  color: var(--ink-soft); padding: 10px 16px; border-radius: 6px;
  cursor: pointer; transition: all .18s ease; text-align: center;
}
.toggle-btn.active {
  background: var(--accent-purple);
  color: #fff;
  box-shadow: 0 0 12px rgba(168, 85, 247, 0.4);
}

/* Dynamic Instruction Card */
.instruction-card {
  background: var(--paper);
  border: 1px solid var(--panel-border);
  border-left: 4px solid var(--accent-purple);
  padding: 16px;
  border-radius: var(--radius);
  font-size: 13px;
  color: var(--ink-soft);
  line-height: 1.5;
}
.instruction-card h3 { margin: 0 0 8px 0; font-size: 14px; color: var(--ink); }
.instruction-card ul { margin: 6px 0; padding-left: 20px; }

/* Reference Card */
.reference-card h3 { font-size: 15px; margin-bottom: 12px; }
.ref-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.ref-item { background: var(--paper); border: 1px solid var(--line); padding: 12px; border-radius: 6px; font-size: 12px; }
.ref-item h4 { margin: 0 0 6px 0; color: var(--accent); font-size: 13px; }
.ref-item p { margin: 0; color: var(--ink-soft); }

.stats-overview { display: flex; gap: 16px; margin: 20px 0; }
.stat-box { flex: 1; background: var(--paper); border: 1px solid var(--panel-border); padding: 12px 18px; border-radius: var(--radius); }
.stat-label { display: block; font-size: 10px; color: var(--muted); text-transform: uppercase; font-weight: 700; }
.stat-value { font-family: var(--font-mono); font-size: 22px; font-weight: 700; color: var(--accent); }

.btn-launch {
  width: 100%; font-size: 16px; padding: 14px; background: var(--ok); color: #0d1117; border: 0; border-radius: var(--radius); font-weight: 700; cursor: pointer; transition: all .2s ease;
  box-shadow: 0 0 15px rgba(52, 211, 153, 0.3);
}
.btn-launch:hover { background: #22c55e; transform: translateY(-1px); }

/* Leaderboard */
.leaderboard-box { margin-top: 12px; font-size: 13px; }
.leaderboard-table { width: 100%; border-collapse: collapse; }
.leaderboard-table td { padding: 10px 4px; border-bottom: 1px solid var(--line); }
.leaderboard-table .rank { font-family: var(--font-mono); font-weight: 700; color: var(--muted); width: 28px; }
.leaderboard-table .name { font-weight: 600; color: var(--ink); }
.leaderboard-table .score { font-family: var(--font-mono); text-align: right; font-weight: 700; color: var(--accent); }
.rejected-heading { margin: 20px 0 0; color: var(--ink); }
.rejected-table .name, .rejected-table .score { color: var(--muted); }
.rejected-table .reason { text-align: right; font-size: 11px; color: var(--muted); white-space: nowrap; }

/* Game HUD */
.game-hud {
  display: flex; align-items: center; justify-content: space-between;
  background: var(--panel); border: 1px solid var(--panel-border);
  padding: 14px 20px; border-radius: var(--radius); margin: 20px 0;
  gap: 16px; flex-wrap: wrap; box-shadow: var(--shadow);
}
.hud-item { display: flex; flex-direction: column; }
.relative-pos { position: relative; }
.hud-label { font-size: 10px; font-weight: 700; text-transform: uppercase; color: var(--muted); }
.hud-value { font-family: var(--font-mono); font-size: 22px; font-weight: 700; color: var(--ink); }
.hud-value.highlight { color: #f59e0b; }
.hud-tag { font-family: var(--font-mono); font-size: 14px; font-weight: 700; color: var(--accent-purple); }

.time-float {
  position: absolute; top: -12px; right: -24px; font-family: var(--font-mono);
  font-weight: 700; font-size: 14px; opacity: 0; pointer-events: none; transition: all .4s ease;
}
.time-float.pos { color: var(--ok); transform: translateY(-8px); opacity: 1; }
.time-float.neg { color: var(--bad); transform: translateY(8px); opacity: 1; }

.lives-row { display: flex; gap: 4px; margin-top: 4px; }
.life-icon { width: 20px; height: 20px; color: var(--bad); transition: opacity .2s ease; }
.life-icon.lost { opacity: .2; }

.hud-actions { display: flex; gap: 8px; }
.btn-hud { background: var(--paper); border: 1px solid var(--panel-border); color: var(--ink); border-radius: var(--radius); padding: 8px 14px; font-weight: 600; cursor: pointer; }
.btn-quit { color: var(--bad); border-color: var(--bad); }

/* Interactive Game Panel */
.panel {
  background: var(--panel); border: 1px solid var(--panel-border);
  border-radius: var(--radius); padding: 28px; box-shadow: var(--shadow);
}
.parity-instruction { font-size: 16px; color: var(--ink-soft); margin-bottom: 24px; }
.req-word { font-family: var(--font-mono); padding: 4px 12px; border-radius: 999px; background: var(--accent-tint); color: var(--accent); font-size: 14px; }
.req-word.odd { background: var(--accent-purple-tint); color: var(--accent-purple); }

/* Simple Parity */
.bitstream-row { display: flex; align-items: center; gap: 12px; margin-bottom: 28px; flex-wrap: wrap; }
.bitstream { display: flex; gap: 8px; }
.bit-static {
  width: 46px; height: 52px; border-radius: var(--radius); border: 1.5px solid var(--panel-border);
  background: var(--paper); display: flex; align-items: center; justify-content: center;
  font-family: var(--font-mono); font-size: 22px; font-weight: 700; color: var(--ink);
}
.bitstream-plus { font-family: var(--font-mono); font-size: 22px; color: var(--muted); }
.parity-slot {
  width: 52px; height: 52px; border-radius: var(--radius); border: 2px dashed var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-family: var(--font-mono); font-size: 22px; font-weight: 700; color: var(--accent);
}
.parity-slot.filled { border-style: solid; }
.parity-slot.correct { border-color: var(--ok); background: var(--ok-tint); color: var(--ok); }
.parity-slot.incorrect { border-color: var(--bad); background: var(--bad-tint); color: var(--bad); }

.choice-row { display: flex; align-items: center; gap: 16px; }
.choice-btn {
  width: 64px; height: 52px; border-radius: var(--radius); border: 1.5px solid var(--panel-border);
  background: var(--paper); font-family: var(--font-mono); font-size: 22px; font-weight: 700; color: var(--ink);
  cursor: pointer; transition: all .15s ease;
}
.choice-btn:hover:not(:disabled) { border-color: var(--accent); background: var(--accent-tint); color: var(--accent); }

/* Parity Block */
.block-scroll { overflow-x: auto; margin-bottom: 20px; }
table.parity-grid { border-collapse: collapse; font-family: var(--font-mono); }
table.parity-grid td {
  width: 44px; height: 44px; text-align: center; vertical-align: middle;
  border: 1px solid var(--line); padding: 0;
}
.cell-bit {
  width: 100%; height: 100%; background: var(--paper); border: none;
  font-family: var(--font-mono); font-size: 18px; font-weight: 700; cursor: pointer; color: var(--ink);
}
.cell-bit:hover:not(:disabled) { background: var(--accent-tint); color: var(--accent); }
td.rowparity .cell-bit, td.colparity .cell-bit { background: #1f2937; color: var(--accent); }
td.corner .cell-bit { background: var(--accent-purple-tint); color: var(--accent-purple); }

table.parity-grid tr td:nth-child(7) { border-right: 2px solid var(--panel-border); }
table.parity-grid tr:nth-child(7) td { border-bottom: 2px solid var(--panel-border); }

.rowcheck-cell, .colcheck-cell { font-family: var(--font-body); font-size: 16px; font-weight: 700; }
.rowcheck-cell.pass, .colcheck-cell.pass { color: var(--ok); }
.rowcheck-cell.fail, .colcheck-cell.fail { color: var(--bad); }

.block-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 20px; }
.block-hint { font-size: 13px; color: var(--muted); }

/* Feedback */
.feedback { border-radius: var(--radius); padding: 14px 18px; margin-top: 20px; font-weight: 600; font-size: 14px; }
.feedback.ok { background: var(--ok-tint); color: var(--ok); border: 1px solid var(--ok); }
.feedback.err { background: var(--bad-tint); color: var(--bad); border: 1px solid var(--bad); }

/* Buttons */
.btn { font-family: var(--font-body); font-weight: 600; font-size: 13px; border-radius: var(--radius); padding: 10px 18px; cursor: pointer; border: 1px solid transparent; }
.topbar-right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.btn-primary { background: var(--accent-purple); color: #fff; }
.btn-primary:hover { background: #9333ea; }
.btn-ghost { background: transparent; border-color: var(--panel-border); color: var(--ink-soft); }
.btn-ghost:hover { color: var(--ink); border-color: var(--ink-soft); }
.btn-danger { background: transparent; border: 1.5px solid var(--bad); color: var(--bad); }
.btn-danger:hover { background: var(--bad-tint); }

/* Overlay & Gameover */
.gameover-overlay {
  position: fixed; inset: 0; background: rgba(13, 17, 23, 0.85); backdrop-filter: blur(6px);
  display: flex; align-items: center; justify-content: center; z-index: 100;
  opacity: 0; pointer-events: none; transition: opacity .25s ease;
}
.gameover-overlay.visible { opacity: 1; pointer-events: auto; }
.gameover-card { background: var(--panel); border: 1px solid var(--panel-border); border-radius: var(--radius); padding: 32px; width: 90%; max-width: 460px; text-align: center; box-shadow: var(--shadow); }
.final-score-box { background: var(--paper); border: 1px solid var(--panel-border); padding: 16px; border-radius: var(--radius); margin: 16px 0; }
.final-score-val { font-family: var(--font-mono); font-size: 36px; font-weight: 700; color: var(--accent); display: block; }
.new-best-badge { color: #f59e0b; font-weight: 700; font-size: 13px; margin-top: 4px; }

/* Telemetry Box */
.telemetry-box { background: var(--paper); border: 1px solid var(--panel-border); border-radius: var(--radius); padding: 14px; margin-bottom: 16px; text-align: left; }
.telemetry-box h4 { margin: 0 0 10px 0; font-size: 11px; text-transform: uppercase; color: var(--muted); letter-spacing: 0.05em; }
.telemetry-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; font-size: 12px; color: var(--ink-soft); }
.telemetry-grid strong { color: var(--ink); font-family: var(--font-mono); }

.submit-input-group { display: flex; gap: 8px; margin-top: 8px; }
.submit-input-group input { font-family: var(--font-mono); font-size: 18px; text-transform: uppercase; text-align: center; width: 100px; border: 1.5px solid var(--panel-border); background: var(--paper); color: var(--ink); border-radius: var(--radius); }
.submit-status { font-size: 12px; margin-top: 8px; color: var(--muted); }

/* Glossary Modal */
dialog { border: 1px solid var(--panel-border); border-radius: var(--radius); padding: 0; max-width: 780px; width: 92vw; background: var(--panel); color: var(--ink); }
dialog::backdrop { background: rgba(0, 0, 0, 0.7); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.dialog-body { padding: 20px; max-height: 60vh; overflow-y: auto; }
.dialog-foot { padding: 14px 20px; display: flex; justify-content: flex-end; border-top: 1px solid var(--line); }
.close-x { border: 0; background: transparent; font-size: 22px; cursor: pointer; color: var(--muted); }

.terms { width: 100%; border-collapse: collapse; font-size: 13px; }
.terms th, .terms td { padding: 10px 8px; text-align: left; border-bottom: 1px solid var(--line); }
.terms th { color: var(--muted); font-size: 11px; text-transform: uppercase; }
.terms td.mi { color: var(--accent); }
.terms td.gap { color: var(--muted); font-style: italic; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--accent-purple); color: #fff; font-size: 13px; padding: 10px 20px; border-radius: 999px;
  opacity: 0; pointer-events: none; transition: all .2s ease; z-index: 200;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.hidden { display: none !important; }