:root {
  --paper: #EAEEF1;
  --panel: #FFFFFF;
  --panel-2: #F4F7F9;
  --ink: #0F1820;
  --ink-soft: #4B5C69;
  --muted: #7D8D99;
  --line: #CFD8DF;
  --line-strong: #E2E9EE;
  --accent-deep: #002D5B;
  --on-accent: #FFFFFF;
  --brand-grey: #6D6E71;
  --logo-plate: transparent;
  --logo-pad: 0px;
  --accent: #0F6FA8;
  --accent-tint: #DCEBF5;
  --accent-b: #B8551D;
  --accent-b-tint: #F7E7DA;
  --bad: #A81E28;
  --bad-tint: #F6E0E1;
  --ok: #2C6E52;
  --ok-tint: #DCEEE4;
  --warn: #9A6A12;
  --warn-tint: #F6ECD6;
  --hint: #3C6FA8;
  --hint-tint: #E1ECF7;
  --accent-b-deep: #8A3F14;
  --shadow: 0 1px 2px rgba(15,24,32,.06), 0 8px 24px -16px rgba(15,24,32,.28);
  --font-mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, monospace;
  --font-display: "Archivo", "Helvetica Neue", Arial, sans-serif;
  --font-body: "Source Sans 3", -apple-system, "Segoe UI", Roboto, sans-serif;

  --stage-ink: #C7D2E3;
  --stage-bg: #10171E;
  --gold: #FFC94A;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #0C1218;
    --panel: #141D25;
    --panel-2: #1A242D;
    --ink: #E4ECF2;
    --ink-soft: #9DAEBA;
    --muted: #71838F;
    --line: #2A3742;
    --line-strong: #212C35;
    --accent-deep: #8FBBDF;
    --brand-grey: #9AA3A9;
    --logo-plate: transparent;
    --logo-pad: 7px;
    --accent: #5CADDF;
    --accent-tint: #13303F;
    --accent-b: #E08B52;
    --accent-b-tint: #3A2617;
    --bad: #E4757C;
    --bad-tint: #3A1A1D;
    --ok: #6FBF97;
    --ok-tint: #173226;
    --warn: #E0A94A;
    --warn-tint: #3A2B12;
    --hint: #6FA6DF;
    --hint-tint: #182A3E;
    --accent-b-deep: #EFB186;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px -16px rgba(0,0,0,.8);
  }
}

:root[data-theme="dark"] {
  --paper: #0C1218;
  --panel: #141D25;
  --panel-2: #1A242D;
  --ink: #E4ECF2;
  --ink-soft: #9DAEBA;
  --muted: #71838F;
  --line: #2A3742;
  --line-strong: #212C35;
  --accent-deep: #8FBBDF;
  --on-accent: #0C1218;
  --brand-grey: #9AA3A9;
  --logo-plate: transparent;
  --logo-pad: 7px;
  --accent: #5CADDF;
  --accent-tint: #13303F;
  --accent-b: #E08B52;
  --accent-b-tint: #3A2617;
  --bad: #E4757C;
  --bad-tint: #3A1A1D;
  --ok: #6FBF97;
  --ok-tint: #173226;
  --warn: #E0A94A;
  --warn-tint: #3A2B12;
  --hint: #6FA6DF;
  --hint-tint: #182A3E;
  --accent-b-deep: #EFB186;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 8px 24px -16px rgba(0,0,0,.8);
}

* { box-sizing: border-box; }

body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: 16px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  margin: 0;
}

.wrap {
  max-width: 1180px;
  margin: 0 auto;
  padding: 28px 20px 64px;
  display: flex;
  flex-direction: column;
  gap: 26px;
}

/* ---------- topbar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  background: var(--paper);
  border-bottom: 2px solid var(--accent-deep);
  padding-block: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.brand { display: flex; flex-direction: row; align-items: center; gap: 14px; }
.brand-logo {
  flex: none;
  height: 56px;
  width: auto;
  border-radius: 5px;
  background: var(--logo-plate);
  padding: var(--logo-pad);
  box-sizing: content-box;
}
.brand-text { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.school {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: .14em;
  text-transform: uppercase;
  color: var(--brand-grey);
  margin: 0;
}
h1 {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: clamp(24px,3.6vw,34px);
  line-height: 1.05;
  letter-spacing: -.025em;
  margin: 0;
  text-wrap: balance;
  color: var(--accent-deep);
}
.topbar-right { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.btn {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 13px;
  border-radius: 4px;
  padding: 8px 14px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: transform .05s ease;
}
.btn:active { transform: translateY(1px); }
.btn-primary { background: var(--accent); color: #ffffff; }
.btn-primary:hover { background: var(--accent-deep); }
.btn-ghost {
  background: transparent;
  border-color: var(--line-strong);
  color: var(--ink-soft);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}
.btn-ghost:hover { border-color: var(--accent); color: var(--accent); }
.site-intro { margin: 0; max-width: 72ch; color: var(--ink-soft); font-size: 16px; }
.objective { font-size: 14.5px; color: var(--ink-soft); max-width: 74ch; margin: 0 0 10px; }
.objective strong { color: var(--ink); }

/* ---------- glossary / dialog ---------- */
dialog {
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  padding: 0;
  max-width: 760px;
  width: 94vw;
  background: var(--panel);
  color: var(--ink);
  box-shadow: var(--shadow);
}
dialog::backdrop { background: rgba(15,21,29,.5); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--line); }
.dialog-head h3 { margin: 0; font-size: 16px; font-family: var(--font-display); }
.dialog-body { padding: 16px 18px 20px; max-height: 65vh; overflow: auto; }
.close-x { border: 0; background: transparent; color: var(--muted); font-size: 20px; line-height: 1; cursor: pointer; padding: 4px; }
.close-x:hover { color: var(--ink); }
.tablewrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 13.5px; }
th, td { text-align: left; padding: 7px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-family: var(--font-display); font-size: 11px; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
td.mi .gap { color: var(--muted); font-style: italic; font-size: 12.5px; }
.tnote { font-size: 12px; color: var(--muted); margin-top: 14px; }
.tnote a { color: var(--muted); }

/* ---------- accordion ---------- */
details.acc { border: 1px solid var(--line); border-radius: 6px; background: var(--panel); box-shadow: var(--shadow); }
details.acc > summary {
  cursor: pointer;
  padding: 13px 16px;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 16.5px;
  display: flex;
  align-items: center;
  gap: 10px;
  list-style: none;
  color: var(--ink);
}
details.acc > summary::-webkit-details-marker { display: none; }
details.acc > summary::before {
  content: "";
  flex: none;
  width: 0;
  height: 0;
  border-left: 6px solid var(--accent-deep);
  border-top: 4.5px solid transparent;
  border-bottom: 4.5px solid transparent;
  transition: transform .15s;
}
details.acc[open] > summary::before { transform: rotate(90deg); }
details.acc > summary:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
details.acc > summary .hint {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--muted);
  font-weight: 400;
  margin-left: auto;
  text-align: right;
}
.acc-body { padding: 0 16px 18px; display: flex; flex-direction: column; gap: 10px; }
.acc-body ul { margin: 0; padding-left: 20px; }
.acc-body li { margin-bottom: 4px; }

/* ---------- generic panel ---------- */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 6px; box-shadow: var(--shadow); }
h2 { font-family: var(--font-display); font-weight: 600; font-size: 19px; letter-spacing: -.01em; margin: 0; }

/* ---------- setup screen ---------- */
.setup { padding: 24px; display: flex; flex-direction: column; gap: 22px; }
.setup-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; max-width: 800px; }
.setup-group { display: flex; flex-direction: column; gap: 8px; }
.setup-group label.group-label {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: .1em;
  text-transform: uppercase;
  color: var(--muted);
}
.toggle-row { display: flex; gap: 8px; flex-wrap: wrap; }
.toggle-btn {
  flex: 1;
  min-width: 120px;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 14.5px;
  padding: 12px 14px;
  border: 1.5px solid var(--line-strong);
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink-soft);
  cursor: pointer;
  text-align: center;
  transition: border-color .1s, color .1s, background .1s;
}
.toggle-btn:hover { border-color: var(--accent); }
.toggle-btn.active { border-color: var(--accent); color: var(--accent-deep); background: var(--accent-tint); }
.toggle-btn .sub { display: block; font-family: var(--font-mono); font-weight: 400; font-size: 11px; color: var(--muted); margin-top: 2px; }
.setup-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 14px;
  border-top: 1px solid var(--line);
  padding-top: 18px;
}
.best-score { font-family: var(--font-mono); font-size: 13px; color: var(--ink-soft); }
.best-score b { color: var(--ink); }
.launch-btn { font-size: 15.5px; padding: 12px 26px; }
.howto { font-size: 13.5px; color: var(--ink-soft); margin: 0; padding-left: 20px; }
.howto li { margin-bottom: 5px; }
.howto .kbd {
  font-family: var(--font-mono);
  font-size: 12px;
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 3px;
  padding: 1px 6px;
  color: var(--ink);
}

/* Leaderboard Styling */
.leaderboard-panel {
  background: var(--paper);
  border: 1px solid var(--line-strong);
  border-radius: 6px;
  padding: 10px 14px;
  font-family: var(--font-mono);
  font-size: 13px;
}
.leaderboard-table { width: 100%; border-collapse: collapse; }
.leaderboard-table td { padding: 4px 0; border: none; }
.leaderboard-table .rank { color: var(--muted); width: 24px; }
.leaderboard-table .name { color: var(--accent-deep); font-weight: 700; letter-spacing: 1px; }
.leaderboard-table .score { text-align: right; font-weight: 700; color: var(--ink); }
.rejected-table .name, .rejected-table .score { color: var(--muted); }
.rejected-table .reason { text-align: right; padding-left: 10px; font-size: 11px; color: var(--muted); white-space: nowrap; }
#rejectedBox { margin-top: 0; }

/* ---------- loading-screen primer ---------- */
.primer { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.primer-card { background: var(--paper); border: 1px solid var(--line); border-radius: 6px; padding: 16px 18px; }
.primer-card h2 { margin-bottom: 8px; color: var(--accent-deep); }
.primer-card p { margin: 0 0 10px; font-size: 14.5px; color: var(--ink-soft); }
.primer-card .small { font-size: 13.5px; }
.primer-card code, .rtn-log code, .isa-table code {
  font-family: var(--font-mono); font-size: 12.5px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 3px; padding: 0 5px; color: var(--ink); white-space: nowrap;
}
.kbd {
  font-family: var(--font-mono); font-size: 12px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 3px; padding: 1px 6px; color: var(--ink); white-space: nowrap;
}
.reg-list { display: grid; grid-template-columns: max-content 1fr; gap: 5px 12px; margin: 0 0 10px; font-size: 14px; }
.reg-list dt { font-family: var(--font-mono); font-weight: 700; color: var(--accent-deep); }
.reg-list dd { margin: 0; color: var(--ink-soft); }
.rtn-steps { margin: 0 0 10px; padding-left: 22px; font-size: 14px; }
.rtn-steps li { margin-bottom: 6px; }
.rtn-steps li span { display: block; color: var(--ink-soft); font-size: 13px; }
.isa-table { font-size: 13px; }
.isa-table th, .isa-table td { padding: 5px 8px; }
.isa-table td.bin { font-family: var(--font-mono); white-space: nowrap; }
.isa-table td.mn { font-family: var(--font-mono); font-weight: 700; white-space: nowrap; color: var(--accent-deep); }
.isa-table.compact { font-size: 12px; }
.isa-table.compact th, .isa-table.compact td { padding: 3px 6px; }
.teach-note p { font-family: var(--font-body); font-size: 13.5px; color: var(--ink-soft); margin: 0 0 8px; }
.teach-note p:last-child { margin-bottom: 0; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- game stage (every colour comes from the theme tokens) ---------- */
.stage-wrap { position: relative; padding: 0; overflow: hidden; }
.stage { position: relative; background: var(--panel); padding: 10px 12px 14px; }
.stage:fullscreen { overflow: auto; width: 100vw; height: 100vh; }
.hud { display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }
.hud-left, .hud-right, .teach-controls { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.teach-controls[hidden], [data-play-only][hidden] { display: none; }
.ov-chip {
  font-family: var(--font-mono); font-size: 11.5px; letter-spacing: .03em; color: var(--ink-soft);
  background: var(--paper); border: 1px solid var(--line); border-radius: 4px;
  padding: 4px 8px; display: flex; align-items: center; gap: 5px;
}
.ov-chip b { color: var(--ink); font-weight: 700; }
.score-chip b { color: var(--accent-b-deep); font-size: 14px; }
.hud-btn { padding: 5px 12px; }
.hud-select {
  font-family: var(--font-body); font-size: 13px; color: var(--ink); background: var(--paper);
  border: 1px solid var(--line); border-radius: 4px; padding: 4px 6px; max-width: 220px;
}
.icon-btn {
  width: 26px; height: 26px; border-radius: 4px; border: 1px solid var(--line);
  background: var(--paper); color: var(--ink-soft); cursor: pointer; font-size: 13px; line-height: 1;
  display: flex; align-items: center; justify-content: center;
}
.icon-btn:hover { border-color: var(--accent); color: var(--accent); }
.msg {
  margin: 8px 0; min-height: 48px; font-family: var(--font-mono); font-size: 13px; color: var(--ink);
  padding: 5px 10px; border-left: 3px solid var(--line-strong); background: var(--paper); border-radius: 0 4px 4px 0;
}
.msg.ok { border-left-color: var(--ok); }
.msg.bad { border-left-color: var(--bad); color: var(--bad); }
.msg.teach { border-left-color: var(--accent); font-family: var(--font-body); font-size: 14.5px; min-height: 5.6em; }
.msg.teach b { font-family: var(--font-mono); color: var(--accent-deep); }
.msg .hint { display: block; color: var(--accent-deep); margin-top: 2px; }
.msg.teach .hint { color: var(--ink-soft); font-size: 14px; }

.board { border-radius: 8px; overflow: hidden; border: 1px solid var(--line); }
.board svg { display: block; width: 100%; height: auto; touch-action: none; user-select: none; -webkit-user-select: none; }

/* interactive parts of the diagram */
#cpuSvg [data-src] { cursor: grab; }
#cpuSvg .btn-svg { cursor: pointer; }
#cpuSvg .btn-svg:hover rect { filter: brightness(1.15); }
#cpuSvg .btn-svg.hidden { display: none; }
#cpuSvg .selected > .frame { stroke: var(--accent); stroke-width: 4; }
#cpuSvg .drop-hover > .frame { stroke: var(--ok); stroke-width: 4; }
#cpuSvg .flash-ok > .frame { stroke: var(--ok); stroke-width: 5; }
#cpuSvg .flash-bad > .frame { stroke: var(--bad); stroke-width: 5; }
#cpuSvg .alu-zone > .frame { fill: transparent; stroke: transparent; }
#cpuSvg .alu-zone.drop-hover > .frame, #cpuSvg .alu-zone.flash-ok > .frame { fill: var(--ok-tint); stroke: var(--ok); }
#cpuSvg .alu-zone.flash-bad > .frame { fill: var(--bad-tint); stroke: var(--bad); }
#cpuSvg .mem-row.at-mar > .frame { fill: var(--warn-tint); stroke: var(--warn); stroke-width: 3; }
#cpuSvg .opd-chip { cursor: grab; }
#cpuSvg .op-chip { cursor: help; }
#cpuSvg .phase-on { fill: var(--accent); font-weight: 700; }
#cpuSvg .bus-on { stroke-width: 8; filter: drop-shadow(0 0 4px currentColor); }
#cpuSvg .teach-src > .frame, #cpuSvg .teach-src.frame { stroke: var(--accent); stroke-width: 4; stroke-dasharray: 7 4; }
#cpuSvg .teach-dst > .frame, #cpuSvg .teach-dst.frame { stroke: var(--ok); stroke-width: 4; }
@keyframes tick-pulse { from { opacity: 1; } to { opacity: .3; } }
#cpuSvg .tick-pulse { animation: tick-pulse .25s ease-out; }

.below { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-top: 12px; }
.side-panel { background: var(--paper); border: 1px solid var(--line); border-radius: 6px; padding: 10px 14px; min-width: 0; }
.side-panel h3 { margin: 0 0 6px; font-family: var(--font-display); font-size: 14px; color: var(--ink); }
.rtn-log { margin: 0; padding-left: 4px; list-style: none; counter-reset: step; max-height: 190px; overflow-y: auto; font-size: 13px; color: var(--ink-soft); }
.rtn-log li { margin-bottom: 3px; }
.rtn-log li:not(.cycle) { counter-increment: step; }
.rtn-log li:not(.cycle)::before { content: counter(step) ". "; color: var(--muted); font-family: var(--font-mono); font-size: 11px; }
.rtn-log li.bad { color: var(--bad); }
.rtn-log li.cycle {
  counter-reset: step; font-family: var(--font-mono); font-size: 11px; letter-spacing: .08em;
  text-transform: uppercase; color: var(--muted); border-top: 1px solid var(--line); padding-top: 4px; margin-top: 6px;
}

.stage-toast {
  position: absolute; left: 50%; top: 42%; transform: translate(-50%,-50%); z-index: 8; display: none;
  background: var(--panel); border: 2px solid var(--warn); border-radius: 8px; padding: 18px 26px;
  text-align: center; color: var(--ink); font-family: var(--font-display); box-shadow: var(--shadow);
}
.stage-toast.visible { display: block; }
.stage-toast b { display: block; font-size: 22px; color: var(--accent-b-deep); margin-bottom: 4px; }
.stage-toast span { display: block; font-family: var(--font-mono); font-size: 13px; color: var(--ink-soft); }

.stage-overlay {
  position: absolute; inset: 0; display: none; flex-direction: column; align-items: center; justify-content: center;
  gap: 14px; background: rgba(8,12,18,.9); text-align: center; padding: 20px; z-index: 10;
}
.stage-overlay.visible { display: flex; }
.stage-overlay h3 { font-family: var(--font-display); font-weight: 700; font-size: clamp(22px,4vw,32px); color: #fff; margin: 0; }
.stage-overlay p { color: #C7D2E3; margin: 0; font-size: 14.5px; max-width: 48ch; }
.stage-overlay .score-final { font-family: var(--font-mono); font-size: 15px; color: #fff; }
.stage-overlay .score-final b { font-size: 26px; color: #FFC94A; }
.overlay-btns { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; }
.initials-input {
  font-family: var(--font-mono); font-size: 18px; font-weight: 700; text-align: center; text-transform: uppercase;
  width: 70px; padding: 6px; border: 1.5px solid #5CADDF; background: rgba(5,8,16,.9); color: #7CF7C4;
  border-radius: 4px; outline: none;
}
.submit-status { font-family: var(--font-mono); font-size: 12px; color: #7CF7C4; }

/* drag ghost and tooltip sit inside whatever is fullscreen (see the fullscreenchange handler) */
.ghost {
  position: fixed; z-index: 1000; pointer-events: none; display: none; transform: translate(-50%, -140%);
  font-family: var(--font-mono); font-size: 13px; font-weight: 700; color: var(--paper); background: var(--accent);
  border-radius: 5px; padding: 4px 9px; box-shadow: var(--shadow); white-space: nowrap;
}
.tip {
  position: fixed; z-index: 1001; pointer-events: none; display: none; max-width: 340px;
  background: var(--ink); color: var(--panel); border-radius: 6px; padding: 9px 12px; font-size: 13px; line-height: 1.45;
  box-shadow: var(--shadow);
}
.tip b { color: var(--paper); }
.tip code { font-family: var(--font-mono); font-size: 12px; color: var(--accent-tint); }
.tip ol { margin: 4px 0 0; padding-left: 18px; }

footer {
  border-top: 1px solid var(--line); padding-top: 14px; font-size: 13px; color: var(--muted);
  display: flex; flex-wrap: wrap; gap: 6px 18px;
}
footer a { color: var(--muted); }

@media (max-width:860px) {
  .setup-grid, .primer, .below { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) {
  .toggle-btn, .btn { transition-duration: .001ms !important; }
  #cpuSvg .tick-pulse { animation: none; }
}
