:root {
  --ink: #1d232a;
  --muted: #59636e;
  --line: #d5dbe1;
  --panel: #ffffff;
  --wash: #f3f5f7;
  --accent: #1f5fbf;
  --ok: #1a7f37;
  --wait: #8a5a00;
  --bad: #c62828;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --radius: 8px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  font-size: 16px;
  line-height: 1.45;
  color: var(--ink);
  background: var(--wash);
}

* {
  box-sizing: border-box;
}

/* Sections set their own display; hidden must still win. */
[hidden] {
  display: none !important;
}

body {
  margin: 0 auto;
  max-width: 1280px;
  padding: 0 1.25rem 2rem;
}

.masthead {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem 1.5rem;
  padding: 1rem 0;
  border-bottom: 1px solid var(--line);
}

h1 {
  margin: 0;
  font-size: 1.35rem;
}

.tabs {
  display: flex;
  gap: 0.25rem;
}

.tabs a {
  padding: 0.4rem 0.9rem;
  border-radius: 999px;
  color: var(--ink);
  text-decoration: none;
  font-weight: 600;
}

.tabs a:hover {
  background: var(--line);
}

.tabs a[aria-current="page"] {
  background: var(--accent);
  color: #fff;
}

.status {
  margin: 0 0 0 auto;
  font-size: 0.9rem;
  color: var(--wait);
}

.status[data-status="ready"] {
  color: var(--ok);
}

.status[data-status="error"] {
  color: var(--bad);
  font-weight: 600;
}

.intro h2 {
  margin: 1.25rem 0 0.25rem;
  font-size: 1.2rem;
}

.intro p {
  margin: 0 0 1rem;
  max-width: 70ch;
  color: var(--muted);
}

/* Settings on the left, the drawing on the right; stacked on narrow screens. */
.workspace {
  display: grid;
  grid-template-columns: minmax(15rem, 20rem) minmax(0, 1fr);
  gap: 1rem;
  align-items: start;
}

.controls,
.stage {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
}

.controls {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  padding: 1rem;
  min-height: 8rem;
}

.stage {
  position: relative;
  min-height: 28rem;
  overflow: hidden;
}

/* Both sizes are set here, so resizing the canvas's bitmap never changes its size on the page. */
.stage canvas {
  display: block;
  width: 100%;
  height: min(70vh, 34rem);
  min-height: 20rem;
}

fieldset {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  margin: 0;
  padding: 0;
  border: 0;
  min-width: 0;
}

legend {
  padding: 0;
  margin-bottom: 0.35rem;
  font-weight: 600;
}

.hint {
  margin: 0 0 0.25rem;
  font-size: 0.85rem;
  color: var(--muted);
}

/* A slider row: its name, the slider, and a box to type the value into. */
.slider {
  display: grid;
  grid-template-columns: 6rem minmax(0, 1fr) 4.75rem;
  align-items: center;
  gap: 0.5rem;
}

.slider label {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.95rem;
}

.slider input[type="range"] {
  width: 100%;
  margin: 0;
  accent-color: var(--accent);
}

.slider input[type="number"],
.playback button {
  font: inherit;
}

.slider input[type="number"] {
  width: 100%;
  padding: 0.2rem 0.35rem;
  border: 1px solid var(--line);
  border-radius: 4px;
  font-variant-numeric: tabular-nums;
}

/* A two-decimal value falls between the arrow keys' tenths, which is not a mistake. */
.slider input[type="number"]:invalid {
  box-shadow: none;
}

.swatch {
  flex: none;
  width: 0.8rem;
  height: 0.8rem;
  border-radius: 50%;
}

.playback {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 0.75rem;
}

.playback button,
.controls button {
  padding: 0.35rem 1rem;
  border: 1px solid var(--accent);
  border-radius: 999px;
  background: var(--panel);
  color: var(--accent);
  font-weight: 600;
  cursor: pointer;
}

.controls button:hover:not(:disabled) {
  background: var(--accent);
  color: #fff;
}

.controls button:disabled {
  opacity: 0.5;
  cursor: default;
}

.speed-label {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.speed {
  accent-color: var(--accent);
}

.speed-shown {
  font-size: 0.9rem;
  color: var(--muted);
}

.readout {
  display: flex;
  flex-direction: column;
  padding: 0.75rem 1rem 0;
}

.readout p {
  margin: 0;
}

.readout > p + p {
  margin-top: 0.5rem;
}

.grashof .rule {
  font-size: 0.9rem;
  color: var(--muted);
}

.warning {
  padding: 0.5rem 0.75rem;
  border-left: 4px solid var(--bad);
  border-radius: 4px;
  background: #fdecea;
  color: var(--bad);
  font-weight: 600;
}

/* Empty, the warning stays in place out of sight, so screen readers announce it as it fills. */
.warning:empty {
  margin-top: 0;
  padding: 0;
  border: 0;
}

.warning:empty:first-child + * {
  margin-top: 0;
}

.readout > p + table,
.readout > table + p {
  margin-top: 0.5rem;
}

.measures {
  align-self: flex-start;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}

.measures th,
.measures td {
  padding: 0.2rem 1.25rem 0.2rem 0;
  text-align: left;
}

.measures thead th {
  font-size: 0.85rem;
  color: var(--muted);
  font-weight: 600;
}

.measures tbody th {
  font-weight: 600;
}

.measures td:nth-child(2) {
  font-weight: 600;
}

.measures td:nth-child(3) {
  color: var(--muted);
}

.buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

/* A drop-down with its name beside it. */
#arena-mode .choice {
  display: grid;
  grid-template-columns: 6rem minmax(0, 1fr);
  align-items: center;
  gap: 0.5rem;
}

#arena-mode .choice select {
  width: 100%;
  padding: 0.2rem 0.35rem;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: var(--panel);
  font: inherit;
}

.notice {
  color: var(--wait);
  font-weight: 600;
}

.badge {
  align-self: flex-start;
  padding: 0.2rem 0.75rem;
  border-radius: 999px;
  background: #c2255c;
  color: #fff;
  font-weight: 600;
}

/* Templates: the print and download buttons, and the pages they print. */
.template {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.template .hint,
.template .warning {
  margin: 0;
}

.template-sheets {
  display: none;
}

@media print {
  /* No printer margins, so a 210 × 297 mm template page fills an A4 sheet unscaled. */
  @page {
    size: A4 portrait;
    margin: 0;
  }

  /* The page printed as itself keeps clear of the paper's edge, which printers cannot reach,
     on every sheet. */
  body:not([data-print="template"]) {
    padding: 12mm;
    -webkit-box-decoration-break: clone;
    box-decoration-break: clone;
  }

  :root {
    background: #fff;
  }

  /* While a template prints, its pages alone fill the sheets at actual size, one to each. */
  body[data-print="template"] {
    max-width: none;
    margin: 0;
    padding: 0;
  }

  body[data-print="template"] > :not(.template-sheets) {
    display: none !important;
  }

  body[data-print="template"] > .template-sheets {
    display: block;
  }

  .template-sheets > svg {
    display: block;
    width: 210mm;
    height: 297mm;
    break-after: page;
  }

  .template-sheets > svg:last-child {
    break-after: auto;
  }
}

@media (max-width: 800px) {
  .workspace {
    grid-template-columns: minmax(0, 1fr);
  }

  .stage canvas {
    height: auto;
    aspect-ratio: 1;
  }

  .status {
    margin-left: 0;
  }
}

/* Evolve mode */

#evolve-mode .controls .field {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
}

#evolve-mode .field > label,
#evolve-mode .field > legend {
  margin: 0;
  font-size: 0.92rem;
  font-weight: 600;
}

#evolve-mode .controls input[type="number"],
#evolve-mode .controls select {
  max-width: 100%;
  padding: 0.3rem 0.45rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--ink);
  font: inherit;
  font-size: 0.95rem;
  font-variant-numeric: tabular-nums;
}

#evolve-mode .field > input[type="number"] {
  width: 7rem;
}

#evolve-mode .field .hint {
  margin: 0;
}

/* A radio button or checkbox with its words, and sometimes a box, on one line. */
#evolve-mode .choice {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.95rem;
}

#evolve-mode .field > label.choice {
  font-weight: 400;
}

#evolve-mode .choice input[type="number"] {
  width: 6rem;
}

#evolve-mode .field.unused {
  opacity: 0.55;
}

#evolve-mode .buttons button {
  padding: 0.45rem 1.4rem;
  font-size: 1rem;
}

#evolve-mode .controls button.primary {
  background: var(--accent);
  color: #fff;
}

#evolve-mode .controls button.primary:hover:not(:disabled) {
  background: #194f9f;
}

.ev-error {
  margin: 0;
  font-size: 0.92rem;
  white-space: pre-wrap;
}

.ev-progress {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
}

#evolve-mode[data-status="error"] .ev-progress {
  color: var(--bad);
  font-weight: 600;
}

details.group {
  padding-top: 0.6rem;
  border-top: 1px solid var(--line);
}

details.group > summary {
  font-weight: 600;
  cursor: pointer;
}

details.group > * + * {
  margin-top: 0.9rem;
}

table.ingredients {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

table.ingredients th,
table.ingredients td {
  padding: 0.2rem 0.25rem 0.2rem 0;
  text-align: left;
  vertical-align: middle;
}

table.ingredients thead th {
  color: var(--muted);
  font-weight: 600;
}

table.ingredients tbody th {
  font-weight: 400;
}

#evolve-mode table.ingredients input[type="number"] {
  width: 5.2rem;
}

.genes {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: 0.2rem 0.4rem;
}

.gene {
  display: flex;
  align-items: center;
  gap: 0.15rem;
  font-family: var(--mono);
}

#evolve-mode .stage {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  padding: 1rem;
}

#evolve-mode .panel h3 {
  margin: 0;
  font-size: 1rem;
}

.panel-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.5rem 1rem;
  margin-bottom: 0.5rem;
}

.panel-head .ev-clear,
.panel-head .ev-reset-code {
  margin-left: auto;
}

#evolve-mode .stage button:not(:where(.cm-editor *)) {
  padding: 0.3rem 0.9rem;
  border: 1px solid var(--accent);
  border-radius: 999px;
  background: var(--panel);
  color: var(--accent);
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}

#evolve-mode .stage button:hover:not(:disabled, :where(.cm-editor *)) {
  background: var(--accent);
  color: #fff;
}

#evolve-mode .stage button:disabled:not(:where(.cm-editor *)) {
  opacity: 0.5;
  cursor: default;
}

.segmented {
  display: inline-flex;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 999px;
  font-size: 0.9rem;
}

.segmented label {
  padding: 0.25rem 0.8rem;
  cursor: pointer;
}

.segmented input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.segmented label:has(input:checked) {
  background: var(--accent);
  color: #fff;
}

.segmented label:has(input:focus-visible) {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

/* These sizes win over the shared canvas sizes, on narrow screens too. */
#evolve-mode .stage canvas.ev-graph {
  height: 19rem;
  min-height: 0;
  aspect-ratio: auto;
}

#evolve-mode .stage canvas.ev-beest {
  height: 17rem;
  min-height: 0;
  aspect-ratio: auto;
  border-radius: 6px;
  background: var(--wash);
}

/* The grid sets its own height, one row of cells at a time. */
#evolve-mode .stage canvas.ev-population {
  min-height: 0;
  aspect-ratio: auto;
}

.ev-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.25rem 1rem;
  margin: 0.5rem 0 0;
  padding: 0;
  font-size: 0.88rem;
  list-style: none;
}

.ev-legend li {
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.ev-legend .key {
  display: inline-block;
  width: 0.8rem;
  height: 0.8rem;
  border-radius: 3px;
}

.ev-styles {
  margin: 0.35rem 0 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.ev-styles .line {
  display: inline-block;
  width: 1.6rem;
  border-top: 2px solid var(--muted);
  vertical-align: middle;
}

.ev-styles .line.dashed {
  margin-left: 0.8rem;
  border-top-style: dashed;
}

.ev-readout {
  min-height: 1.3em;
  margin: 0.35rem 0 0;
  color: var(--muted);
  font-size: 0.85rem;
  white-space: pre-line;
}

/* Evolve's two best beests side by side, each a picture over its numbers; one column when
   the stage is narrow. */
.best-pair {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(17rem, 1fr));
  gap: 1rem;
  align-items: start;
  margin: 0.25rem 0 0.75rem;
}

#evolve-mode .best-beest h4 {
  margin: 0 0 0.4rem;
  font-size: 0.95rem;
}

.best-shown {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.best-panel > .hint {
  margin-bottom: 0.5rem;
}

.ev-facts p {
  margin: 0 0 0.5rem;
  font-size: 0.92rem;
}

.ev-facts .checks {
  display: flex;
  flex-direction: column;
  gap: 0.2rem;
  margin: 0 0 0.5rem;
  padding: 0;
  font-weight: 600;
  list-style: none;
}

.ev-facts .ok {
  color: var(--ok);
}

.ev-facts .bad {
  color: var(--bad);
}

.ev-facts .numbers {
  display: grid;
  grid-template-columns: auto auto;
  gap: 0.1rem 0.75rem;
  margin: 0 0 0.5rem;
  font-size: 0.9rem;
}

.ev-facts dt {
  color: var(--muted);
}

.ev-facts dd {
  margin: 0;
  font-variant-numeric: tabular-nums;
}

.ev-facts .lengths span {
  display: inline-block;
  margin-right: 0.5rem;
  font-variant-numeric: tabular-nums;
}

/* The code route's editor panel */

#evolve-mode code,
#evolve-mode pre,
.ev-editor .cm-scroller {
  font-family: var(--mono);
}

#evolve-mode code {
  font-size: 0.92em;
}

.code-panel > * + * {
  margin-top: 0.6rem;
}

.code-panel > .hint {
  margin-bottom: 0;
}

.ev-editor .cm-editor {
  max-height: 32rem;
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 0.9rem;
}

.ev-editor .cm-editor.cm-focused {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.ev-editor .cm-scroller {
  overflow: auto;
}

/* Wins over the editor's own active-line colour, and lets its selection show through. */
#evolve-mode .ev-editor .cm-line.error-line {
  background: color-mix(in srgb, var(--bad) 10%, transparent);
  box-shadow: inset 3px 0 0 var(--bad);
}

.ev-code-error {
  margin-bottom: 0;
  font-size: 0.92rem;
  white-space: pre-wrap;
}

.ev-traceback > summary,
.ev-help > summary {
  font-size: 0.92rem;
  font-weight: 600;
  cursor: pointer;
}

.ev-traceback pre {
  margin: 0.4rem 0 0;
  padding: 0.5rem 0.75rem;
  overflow-x: auto;
  border-radius: 6px;
  background: var(--wash);
  font-size: 0.85rem;
}

.ev-help dl {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  gap: 0.25rem 0.75rem;
  margin: 0.4rem 0 0;
  font-size: 0.88rem;
}

.ev-help dd {
  margin: 0;
  color: var(--muted);
}

@media (max-width: 800px) {
  .ev-help dl {
    grid-template-columns: minmax(0, 1fr);
  }
}
