/* Corewars design system.
 *
 * Two themes, both from the chipdiplo platforms:
 *   Grey  - the dark slate analyst theme with a copper accent;
 *   Solar - the warm paper theme with pine and amber.
 * "System" follows prefers-color-scheme. Everything is a token on :root;
 * components only use tokens. Contrast targets WCAG 2.2 AA (4.5:1 for
 * text, 3:1 for UI boundaries and focus indicators).
 */

:root {
  color-scheme: dark;
  --bg: #12151a;
  --surface: #1a1f27;
  --surface-raised: #212733;
  --border: #2c3340;
  --border-strong: #3a4354;
  --text: #e7ebf1;
  --text-muted: #9aa4b5;
  --text-faint: #7d8799;
  --accent: #d98a4f;
  --accent-strong: #f0a868;
  --accent-contrast: #1a1006;
  --good: #4fae7a;
  --warn: #dba53f;
  --bad: #f2766b;
  --info: #5aa9e2;
  --focus: #f0a868;
  /* Accent used as text or links: must reach 4.5:1 on surfaces. */
  --accent-text: #f0a868;
  --link: #f0a868;
  /* Arena: empty memory and Puzzle data. */
  --cell-empty: #171b22;
  --cell-data: #4b5363;
  --cell-goal: #e7ebf1;
  --radius: 6px;
  --font-sans: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, "SFMono-Regular", "JetBrains Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --text-scale: 1;
}

:root[data-theme="solar"],
:root[data-theme="system"] {
  /* overridden below for system dark */
}

:root[data-theme="solar"] {
  color-scheme: light;
  --bg: #f5f3ec;
  --surface: #fffefa;
  --surface-raised: #f0efe6;
  --border: #d9ddd7;
  --border-strong: #b9c2bb;
  --text: #16211f;
  --text-muted: #4f5d59;
  --text-faint: #66736f;
  --accent: #174f43;
  --accent-strong: #0f3a31;
  --accent-contrast: #ffffff;
  --good: #1f6f4a;
  --warn: #8a5a0f;
  --bad: #8f332d;
  --info: #1f5f8f;
  --focus: #c26a19;
  --accent-text: #174f43;
  --link: #174f43;
  --cell-empty: #ebe8dc;
  --cell-data: #9aa39e;
  --cell-goal: #16211f;
}

@media (prefers-color-scheme: light) {
  :root[data-theme="system"] {
    color-scheme: light;
    --bg: #f5f3ec;
    --surface: #fffefa;
    --surface-raised: #f0efe6;
    --border: #d9ddd7;
    --border-strong: #b9c2bb;
    --text: #16211f;
    --text-muted: #4f5d59;
    --text-faint: #66736f;
    --accent: #174f43;
    --accent-strong: #0f3a31;
    --accent-contrast: #ffffff;
    --good: #1f6f4a;
    --warn: #8a5a0f;
    --bad: #8f332d;
    --info: #1f5f8f;
    --focus: #c26a19;
    --accent-text: #174f43;
    --link: #174f43;
    --cell-empty: #ebe8dc;
    --cell-data: #9aa39e;
    --cell-goal: #16211f;
  }
}

/* High contrast: stronger borders and text, for either theme. */
:root[data-contrast="high"] {
  --border: var(--border-strong);
  --text-muted: var(--text);
  --text-faint: var(--text);
}

*, *::before, *::after { box-sizing: border-box; }
html { font-size: calc(15px * var(--text-scale)); -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
::selection { background: var(--accent); color: var(--accent-contrast); }

a { color: var(--link); text-underline-offset: 2px; }
a:hover { text-decoration-thickness: 2px; }
/* Programmatic focus target after navigation: no ring for the whole page. */
main:focus { outline: none; }

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

h1, h2, h3 { line-height: 1.25; letter-spacing: -0.01em; margin: 0 0 .5rem; }
h1 { font-size: 1.6rem; }
h2 { font-size: 1.2rem; margin-top: 1.6rem; }
h3 { font-size: 1rem; margin-top: 1.2rem; }
p { margin: 0 0 .9rem; max-width: 72ch; }
code, kbd, pre, .mono { font-family: var(--font-mono); }
code {
  font-size: .9em;
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: .05em .35em;
}
pre {
  background: var(--surface-raised);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: .75rem .9rem;
  overflow-x: auto;
  font-size: .88rem;
}
pre code { background: none; border: 0; padding: 0; }
kbd {
  font-size: .8em;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: 4px;
  padding: .05em .35em;
  background: var(--surface);
}
table { border-collapse: collapse; width: 100%; font-size: .9rem; }
th, td { text-align: left; padding: .45rem .6rem; border-bottom: 1px solid var(--border); vertical-align: top; }
thead th { color: var(--text-muted); font-size: .75rem; text-transform: uppercase; letter-spacing: .04em; font-weight: 700; }
tbody th { font-weight: 600; }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.skip-link {
  position: absolute; left: .5rem; top: -3rem; z-index: 100;
  background: var(--accent); color: var(--accent-contrast);
  padding: .5rem .8rem; border-radius: var(--radius);
}
.skip-link:focus { top: .5rem; }

/* Shell */
.site-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border-strong);
}
.site-header-inner {
  max-width: 1440px; margin: 0 auto; padding: 0 1.25rem;
  display: flex; align-items: center; gap: 1.5rem; min-height: 52px; flex-wrap: wrap;
}
.brand {
  display: flex; align-items: center; gap: .55rem;
  font-family: var(--font-mono); font-weight: 700; font-size: .95rem;
  letter-spacing: .08em; text-transform: uppercase;
  color: var(--text); text-decoration: none;
}
.brand-mark {
  width: 12px; height: 12px; flex: none;
  background:
    linear-gradient(var(--accent), var(--accent)) center / 100% 2px no-repeat,
    linear-gradient(var(--accent), var(--accent)) center / 2px 100% no-repeat;
}
.site-nav { display: flex; flex-wrap: wrap; gap: .15rem; flex: 1; }
.site-nav a {
  color: var(--text-muted); text-decoration: none; font-size: .9rem; font-weight: 500;
  padding: .45rem .65rem; border-radius: 5px;
}
.site-nav a:hover { color: var(--text); background: var(--surface-raised); }
.site-nav a[aria-current="page"] { color: var(--text); background: var(--surface-raised); box-shadow: inset 0 -2px 0 var(--accent); }
.site-user { display: flex; align-items: center; gap: .5rem; font-size: .85rem; color: var(--text-muted); }

main { max-width: 1440px; margin: 0 auto; padding: 1.5rem 1.25rem 4rem; }
main.narrow { max-width: 900px; }
.page-head { display: flex; align-items: baseline; justify-content: space-between; gap: 1rem; flex-wrap: wrap; margin-bottom: 1rem; }
.page-head p { color: var(--text-muted); margin: 0; }
.eyebrow { color: var(--accent-text); font-size: .72rem; font-weight: 800; letter-spacing: .12em; text-transform: uppercase; }
.muted { color: var(--text-muted); }
.small { font-size: .85rem; }

/* Controls */
button, .button {
  font: inherit; font-size: .9rem; font-weight: 600; cursor: pointer;
  display: inline-flex; align-items: center; gap: .4rem;
  background: var(--surface-raised); color: var(--text);
  border: 1px solid var(--border-strong); border-radius: var(--radius);
  padding: .45rem .85rem; text-decoration: none; min-height: 2.25rem;
}
button:hover, .button:hover { border-color: var(--text-faint); }
button.primary, a.button.primary { background: var(--accent); color: var(--accent-contrast); border-color: var(--accent); }
a.button { color: var(--text); }
button.primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }
button.danger { color: var(--bad); }
button.ghost { background: transparent; border-color: transparent; }
button.ghost:hover { background: var(--surface-raised); }
button:disabled { opacity: .55; cursor: not-allowed; }
button[aria-pressed="true"] { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }

label { display: block; font-size: .85rem; font-weight: 600; color: var(--text-muted); margin-bottom: .9rem; }
input, select, textarea {
  display: block; width: 100%; margin-top: .3rem;
  font: inherit; font-size: .95rem; color: var(--text);
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--radius);
  padding: .5rem .6rem;
}
input[type="checkbox"], input[type="radio"] { display: inline-block; width: auto; margin: 0 .4rem 0 0; accent-color: var(--accent); }
input[type="range"] { padding: 0; accent-color: var(--accent); }
textarea { font-family: var(--font-mono); font-size: .85rem; min-height: 10rem; resize: vertical; }
.check { display: flex; align-items: center; font-weight: 500; color: var(--text); }
fieldset { border: 1px solid var(--border); border-radius: var(--radius); padding: .8rem 1rem; margin: 0 0 1rem; }
legend { font-weight: 700; padding: 0 .3rem; }
.row { display: flex; gap: .6rem; align-items: center; flex-wrap: wrap; }
.row > label { margin-bottom: 0; }
.stack > * + * { margin-top: .8rem; }
.grid-2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 22rem), 1fr)); gap: 1rem; }

/* Cards, panels, badges */
.panel {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1rem 1.1rem;
}
.panel > h2:first-child, .panel > h3:first-child { margin-top: 0; }
.badge {
  display: inline-flex; align-items: center; gap: .3rem;
  font-family: var(--font-mono); font-size: .72rem; font-weight: 700; letter-spacing: .02em;
  padding: .1rem .45rem; border-radius: 4px; border: 1px solid var(--border-strong);
  color: var(--text-muted); background: var(--surface-raised); white-space: nowrap;
}
.badge.good { color: var(--good); border-color: currentColor; background: color-mix(in srgb, var(--good) 12%, transparent); }
.badge.bad { color: var(--bad); border-color: currentColor; background: color-mix(in srgb, var(--bad) 12%, transparent); }
.badge.warn { color: var(--warn); border-color: currentColor; background: color-mix(in srgb, var(--warn) 12%, transparent); }
.badge.accent { color: var(--accent-text); border-color: currentColor; }
.notice { border: 1px solid var(--border-strong); border-left-width: 4px; border-radius: var(--radius); padding: .6rem .8rem; background: var(--surface); margin: .8rem 0; }
.notice.bad { border-left-color: var(--bad); }
.notice.good { border-left-color: var(--good); }
.notice.warn { border-left-color: var(--warn); }
.notice.info { border-left-color: var(--info); }
.empty { color: var(--text-muted); padding: 1.5rem; text-align: center; border: 1px dashed var(--border-strong); border-radius: var(--radius); }

/* Landing */
.landing { display: grid; grid-template-columns: minmax(0, 1.2fr) minmax(0, 1fr); gap: 3rem; align-items: center; min-height: calc(100vh - 8rem); }
.landing h1 { font-size: clamp(2rem, 4vw, 3.2rem); font-weight: 750; letter-spacing: -.02em; margin-bottom: 1rem; }
.landing .lede { font-size: 1.1rem; color: var(--text-muted); max-width: 52ch; }
.landing .points { list-style: none; padding: 0; margin: 1.5rem 0 0; display: grid; gap: .6rem; }
.landing .points li { display: flex; gap: .7rem; align-items: baseline; }
.landing .points li::before { content: ""; width: 8px; height: 8px; flex: none; background: var(--accent); transform: translateY(-2px); }
.landing-art { font-family: var(--font-mono); font-size: .8rem; line-height: 1.35; color: var(--text-muted); white-space: pre; overflow: hidden; margin-top: 2rem; }
.auth-card { max-width: 420px; width: 100%; justify-self: end; }
.tabs { display: flex; gap: .25rem; border-bottom: 1px solid var(--border); margin-bottom: 1rem; flex-wrap: wrap; }
.tabs a.button.ghost, .tabs [role="tab"] { background: none; border: 0; border-bottom: 2px solid transparent; border-radius: 0; color: var(--text-muted); padding: .5rem .8rem; }
.tabs a.button.ghost[aria-current="page"], .tabs [role="tab"][aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }

/* Campaign */
.chapter { margin-bottom: 2rem; }
.chapter-head { display: flex; align-items: baseline; gap: .8rem; flex-wrap: wrap; margin-bottom: .6rem; }
.chapter-head h2 { margin: 0; }
.puzzle-list { list-style: none; margin: 0; padding: 0; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); overflow: hidden; }
.puzzle-list li { border-bottom: 1px solid var(--border); }
.puzzle-list li:last-child { border-bottom: 0; }
.puzzle-row { display: grid; grid-template-columns: 2.5rem minmax(0, 1fr) auto; gap: .8rem; align-items: center; padding: .7rem .9rem; color: var(--text); text-decoration: none; }
a.puzzle-row { color: var(--text); }
a.puzzle-row:hover { background: var(--surface-raised); }
.puzzle-row .num { font-family: var(--font-mono); color: var(--text-faint); font-size: .85rem; }
.puzzle-row .title { font-weight: 600; }
.puzzle-row .concept { color: var(--text-muted); font-size: .85rem; }
.puzzle-row.locked { color: var(--text-faint); }
.status-icon { font-family: var(--font-mono); font-weight: 700; }

/* Puzzle workspace */
.workspace {
  display: grid;
  grid-template-columns: minmax(18rem, 26rem) minmax(0, 1fr) minmax(20rem, 34rem);
  gap: 1rem;
  align-items: start;
}
.workspace > section { min-width: 0; }
.brief { max-height: calc(100vh - 9rem); overflow: auto; position: sticky; top: 1rem; }
.brief h1 { font-size: 1.35rem; }
.chips { display: flex; flex-wrap: wrap; gap: .3rem; margin: .4rem 0 .8rem; }
.toolbar { display: flex; gap: .5rem; align-items: center; flex-wrap: wrap; margin-bottom: .5rem; }
.toolbar .spacer { flex: 1; }
.file-tabs { display: flex; gap: .2rem; flex-wrap: wrap; }
.file-tabs button { font-family: var(--font-mono); font-size: .8rem; padding: .25rem .55rem; min-height: 1.9rem; }
.editor-host { border: 1px solid var(--border-strong); border-radius: var(--radius); overflow: hidden; background: var(--surface); }
.editor-host .cm-editor { height: min(62vh, 44rem); font-size: calc(.92rem * var(--editor-scale, 1)); }
.editor-host .cm-editor.cm-focused { outline: 3px solid var(--focus); outline-offset: -1px; }
.cm-cost { color: var(--text-muted); font-family: var(--font-mono); font-size: .75rem; padding-left: 1.5rem; }
.cm-heat { font-family: var(--font-mono); font-size: .72rem; text-align: right; min-width: 3.5rem; padding-right: .3rem; }
.cm-current-line-run { background: color-mix(in srgb, var(--focus) 12%, transparent); box-shadow: inset 3px 0 0 var(--focus); }
.status-line { font-family: var(--font-mono); font-size: .78rem; color: var(--text-muted); display: flex; gap: 1rem; flex-wrap: wrap; padding: .3rem .1rem; }

/* Results */
.runs { display: grid; gap: .35rem; margin: .6rem 0; }
.run { display: flex; gap: .6rem; align-items: baseline; font-size: .9rem; }
.metrics { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .5rem; margin: .8rem 0; }
.metric { border: 1px solid var(--border); border-radius: var(--radius); padding: .5rem .6rem; background: var(--surface); }
.metric .value { font-family: var(--font-mono); font-size: 1.3rem; font-weight: 700; }
.metric .label { font-size: .72rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: .05em; }
.metric .par { font-size: .78rem; color: var(--text-muted); }
.histogram svg { width: 100%; height: auto; max-height: 5.5rem; display: block; }
.histogram .bar { fill: var(--border-strong); }
.histogram .bar.mine { fill: var(--accent); }
.histogram text { fill: var(--text-muted); font-size: 10px; font-family: var(--font-mono); }
.diag { font-family: var(--font-mono); font-size: .82rem; }
.diag li { margin-bottom: .25rem; }

/* Arena */
.arena { display: grid; gap: .5rem; }
.arena { min-width: 0; }
.arena canvas.overview { display: block; max-width: 100%; cursor: crosshair; touch-action: none; }
.zoom-head { display: flex; align-items: center; gap: .4rem; font-size: .85rem; }
.zoom-head .spacer { flex: 1; }
.zoom-head button { padding: .2rem .5rem; font-size: .8rem; }
.zoom { overflow-x: auto; border-radius: 4px; }
.zoom:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.zoom table { border-collapse: separate; border-spacing: 2px; table-layout: fixed; font-family: var(--font-mono); font-size: .74rem; min-width: 26rem; }
.zoom th, .zoom td { padding: .12rem 0; border: 0; text-align: center; vertical-align: middle; }
.zoom thead th { font-size: .7rem; font-weight: 400; text-transform: none; letter-spacing: 0; }
.zoom th:first-child { width: 3.6rem; }
.zoom tbody th { text-align: right; padding-right: .35rem; font-weight: 400; color: var(--text-muted); }
.zoom td { background: var(--cell-empty); color: var(--text-muted); border-radius: 2px; cursor: pointer; }
.zoom td.off { background: transparent; cursor: default; }
.zoom td.goal { outline: 2px dashed var(--cell-goal); outline-offset: -2px; }
.zoom td.pc { box-shadow: inset 0 0 0 2px var(--text); }
.zoom td.fresh { font-weight: 800; text-decoration: underline; }
.zoom td.cursor { outline: 3px solid var(--focus); outline-offset: -1px; }
.transport { display: flex; gap: .35rem; align-items: center; flex-wrap: wrap; }
.transport button { min-width: 2.4rem; justify-content: center; padding: .35rem .5rem; }
.transport input[type="range"] { flex: 1; min-width: 8rem; margin: 0; }
.transport .clock { font-family: var(--font-mono); font-size: .8rem; color: var(--text-muted); min-width: 9rem; }
.legend { display: flex; flex-wrap: wrap; gap: .4rem .9rem; font-size: .8rem; }
.legend .swatch { display: inline-block; width: .8rem; height: .8rem; border-radius: 2px; margin-right: .35rem; vertical-align: -1px; border: 1px solid var(--border-strong); }
.inspector { font-family: var(--font-mono); font-size: .8rem; color: var(--text-muted); min-height: 1.3rem; }
.registers { display: grid; grid-template-columns: repeat(auto-fill, minmax(10.5rem, 1fr)); gap: .1rem .8rem; font-family: var(--font-mono); font-size: .78rem; }
.registers .reg { display: flex; justify-content: space-between; gap: .4rem; padding: .05rem .2rem; border-radius: 3px; }
.registers .reg .name { color: var(--text-muted); }
.registers .reg.changed { background: color-mix(in srgb, var(--accent) 18%, transparent); font-weight: 700; }
.registers .reg.changed .name::after { content: " *"; }
.step-info { font-family: var(--font-mono); font-size: .8rem; }

/* Toasts */
.toasts { position: fixed; right: 1rem; bottom: 1rem; z-index: 50; display: grid; gap: .5rem; max-width: min(24rem, calc(100vw - 2rem)); }
.toast { background: var(--surface); border: 1px solid var(--border-strong); border-left: 4px solid var(--accent); border-radius: var(--radius); padding: .7rem .9rem; box-shadow: 0 8px 28px rgb(0 0 0 / 25%); display: grid; grid-template-columns: 1fr auto; gap: .5rem; }
.toast strong { display: block; }

/* Achievements */
.achievements { display: grid; grid-template-columns: repeat(auto-fill, minmax(15rem, 1fr)); gap: .7rem; }
.achievement { border: 1px solid var(--border); border-radius: var(--radius); padding: .7rem .8rem; background: var(--surface); }
.achievement.earned { border-color: var(--accent); }
.achievement .title { font-weight: 700; }
.achievement.locked { color: var(--text-muted); }

/* Prose from the server */
.prose { max-width: 75ch; }
.prose table { margin: .8rem 0; }
.prose h1 { font-size: 1.5rem; }

/* Teacher progress matrix */
.matrix td, .matrix th { text-align: center; padding: .35rem .4rem; }
.matrix th.rot { writing-mode: vertical-rl; transform: rotate(180deg); white-space: nowrap; text-transform: none; letter-spacing: 0; font-weight: 600; }
.matrix td.name, .matrix th.name { text-align: left; white-space: nowrap; }
.cell-solved { color: var(--good); font-weight: 700; }
.cell-tried { color: var(--warn); }

/* Diff view */
.diff { font-family: var(--font-mono); font-size: .8rem; white-space: pre; overflow-x: auto; }
.diff .add { color: var(--good); }
.diff .del { color: var(--bad); }

.progress { height: .5rem; background: var(--surface-raised); border-radius: 999px; overflow: hidden; border: 1px solid var(--border); }
.progress > span { display: block; height: 100%; background: var(--accent); }

@media (max-width: 1200px) {
  .workspace { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); }
  .workspace .brief { grid-column: 1 / -1; position: static; max-height: none; }
}
@media (max-width: 820px) {
  .workspace { grid-template-columns: minmax(0, 1fr); }
  .landing { grid-template-columns: minmax(0, 1fr); gap: 1.5rem; }
  .auth-card { justify-self: stretch; max-width: none; }
  .site-nav { order: 3; flex: 1 1 100%; overflow-x: auto; flex-wrap: nowrap; }
  .metrics { grid-template-columns: minmax(0, 1fr); }
}

/* Motion: transitions only when the user has not asked for less. */
@media (prefers-reduced-motion: no-preference) {
  :root:not([data-motion="reduce"]) a,
  :root:not([data-motion="reduce"]) button { transition: background-color .12s ease, border-color .12s ease, color .12s ease; }
}
