/* ==========================================================
   Pathfinding Playground
   Design system: "Blueprint"
   A technical drawing: thin linework on a measured grid, one
   highlight colour for what matters (the frontier and the path).
   Flat fills and hairlines only: no gradients, glows or soft
   shadows. Values come from the tokens in section 1.
   ========================================================== */

/* ---------- 1. Tokens ---------- */
:root {
  /* colour: blueprint (default) */
  --paper: #0b2a4a;
  --paper-2: #123a61;
  --ink: #e6f0fa;
  --ink-2: #93b3cf;
  --spot: #ffb238;        /* highlight fills: frontier, path, primary button */
  --spot-text: #ffc766;   /* small highlighted text */
  --spot-ink: #1a1000;    /* text and lines on a highlight fill */
  --hair: color-mix(in srgb, var(--ink) 30%, transparent);
  --grid-minor: color-mix(in srgb, var(--ink) 6%, transparent);
  --grid-major: color-mix(in srgb, var(--ink) 12%, transparent);

  /* type */
  --f-display: "Space Grotesk", "Segoe UI", system-ui, sans-serif;
  --f-body: "Space Grotesk", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  --f-mono: "JetBrains Mono", ui-monospace, "SFMono-Regular", Consolas, monospace;

  --t-xs: .75rem;
  --t-sm: .875rem;
  --t-md: 1.0625rem;
  --t-lg: 1.25rem;
  --t-xl: 1.6rem;
  --t-h2: clamp(2rem, 4.6vw, 3.2rem);
  --t-hero: clamp(2.6rem, 8.4vw, 5.4rem);

  /* space (4px base) */
  --s-1: 4px;
  --s-2: 8px;
  --s-3: 12px;
  --s-4: 16px;
  --s-5: 24px;
  --s-6: 32px;
  --s-7: 48px;
  --s-8: 72px;

  /* lines */
  --lw: 1.5px;
  --r: 2px;

  /* motion */
  --dur: .14s;
  --ease: cubic-bezier(.2, .7, .2, 1);

  --mast-h: 60px;
  color-scheme: dark;
}

/* colour: drafting sheet (light) */
:root[data-theme="light"] {
  --paper: #eef3f7;
  --paper-2: #dde7ef;
  --ink: #12324f;
  --ink-2: #4a6a85;
  --spot: #e8590c;
  --spot-text: #b8440a;
  --spot-ink: #ffffff;
  --hair: color-mix(in srgb, var(--ink) 30%, transparent);
  --grid-minor: color-mix(in srgb, var(--ink) 6%, transparent);
  --grid-major: color-mix(in srgb, var(--ink) 11%, transparent);
  color-scheme: light;
}

/* ---------- 2. Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html { scroll-behavior: smooth; scroll-padding-top: var(--mast-h); -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background-color: var(--paper);
  /* drawing grid: 24px squares, a heavier line every fifth */
  background-image:
    linear-gradient(var(--grid-major) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-major) 1px, transparent 1px),
    linear-gradient(var(--grid-minor) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-minor) 1px, transparent 1px);
  background-size: 120px 120px, 120px 120px, 24px 24px, 24px 24px;
  color: var(--ink);
  font: 400 var(--t-md)/1.6 var(--f-body);
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}
img, svg, canvas { display: block; max-width: 100%; }
h1, h2, h3, h4, p, ul, ol, dl, dd, figure, fieldset { margin: 0; }
fieldset { padding: 0; border: 0; min-width: 0; }
ul, ol { padding: 0; list-style: none; }
a { color: inherit; }
button, select, input { font: inherit; color: inherit; }
strong, b { font-weight: 600; }
[hidden] { display: none !important; }
::selection { background: var(--spot); color: var(--spot-ink); }
:focus-visible { outline: 2px solid var(--spot); outline-offset: 3px; }

.wrap { width: min(1320px, 100% - 2 * var(--s-4)); margin-inline: auto; }
@media (min-width: 700px) { .wrap { width: min(1320px, 100% - 2 * var(--s-6)); } }

.skip {
  position: absolute; left: var(--s-4); top: -100px; z-index: 100;
  background: var(--spot); color: var(--spot-ink); padding: var(--s-2) var(--s-4);
  font: 600 var(--t-sm) var(--f-mono);
}
.skip:focus { top: var(--s-2); }

/* ---------- 3. Components ---------- */

/* Button: outlined linework. Variants: default, .btn-a (highlight). Sizes: default, .btn-sm */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s-2);
  padding: var(--s-3) var(--s-5);
  background: var(--paper); color: var(--ink);
  border: var(--lw) solid var(--ink); border-radius: var(--r);
  font: 600 var(--t-sm)/1 var(--f-mono); text-transform: uppercase; letter-spacing: .08em;
  text-decoration: none; cursor: pointer;
  transition: background var(--dur), color var(--dur), border-color var(--dur);
}
.btn:hover { background: var(--ink); color: var(--paper); }
.btn:active { transform: translateY(1px); }
.btn:disabled { cursor: not-allowed; opacity: .4; background: var(--paper); color: var(--ink); transform: none; }
.btn-a { background: var(--spot); border-color: var(--spot); color: var(--spot-ink); }
.btn-a:hover { background: var(--ink); border-color: var(--ink); color: var(--paper); }
.btn-sm { padding: var(--s-2) var(--s-3); font-size: .72rem; }

.sq-btn {
  display: inline-grid; place-items: center; width: 38px; height: 38px;
  background: transparent; border: var(--lw) solid var(--ink); border-radius: var(--r); cursor: pointer;
  transition: background var(--dur), color var(--dur);
}
.sq-btn:hover { background: var(--ink); color: var(--paper); }
/* the icon shows the theme you would switch to */
.i-moon { display: none; }
:root[data-theme="light"] .i-moon { display: block; }
:root[data-theme="light"] .i-sun { display: none; }
.btn-sm { min-height: 40px; }
.btn-quiet { border-color: var(--hair); background: transparent; }
.btn-quiet:hover { border-color: var(--ink); }

.sr-only {
  position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.field-label {
  display: block; padding: 0;
  font: 600 var(--t-xs)/1.4 var(--f-mono); text-transform: uppercase; letter-spacing: .12em; color: var(--ink-2);
}

/* Segmented control built from radio buttons */
.seg { display: flex; border: var(--lw) solid var(--ink); border-radius: var(--r); background: var(--paper); margin: 0; padding: 0; }
.seg label { position: relative; display: block; flex: 1 1 0; cursor: pointer; }
.seg label + label { border-left: var(--lw) solid var(--ink); }
.seg input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.seg span {
  display: flex; align-items: center; justify-content: center; min-height: 40px; padding: 0 var(--s-4);
  font: 700 .75rem/1 var(--f-mono); text-transform: uppercase; letter-spacing: .1em; white-space: nowrap;
}
.seg input:checked + span { background: var(--ink); color: var(--paper); }
.seg input:focus-visible + span { outline: 2px solid var(--spot); outline-offset: 2px; position: relative; z-index: 1; }

.select {
  min-height: 44px; padding: 0 36px 0 var(--s-3);
  background-color: var(--paper);
  /* chevron drawn with two hard-edged triangles */
  background-image: linear-gradient(45deg, transparent 50%, var(--spot) 50%), linear-gradient(135deg, var(--spot) 50%, transparent 50%);
  background-position: right 18px center, right 12px center;
  background-size: 6px 6px; background-repeat: no-repeat;
  border: var(--lw) solid var(--ink); border-radius: var(--r);
  font: 500 var(--t-sm)/1.4 var(--f-mono); appearance: none; cursor: pointer;
}
.select option { background: var(--paper); color: var(--ink); }

.check { display: flex; align-items: center; gap: var(--s-2); min-height: 44px; font-size: var(--t-sm); cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--spot); margin: 0; }

input[type="range"] { accent-color: var(--spot); margin: 0; height: 28px; cursor: pointer; }

/* ---------- 4. Header ---------- */
.masthead { position: sticky; top: 0; z-index: 50; background: var(--paper); border-bottom: var(--lw) solid var(--ink); }
.masthead-in {
  max-width: 1440px; margin-inline: auto; padding: var(--s-3) var(--s-6);
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: var(--s-3) var(--s-4);
}
.brand { display: flex; align-items: center; gap: var(--s-3); text-decoration: none; min-width: 0; }
.brand-mark {
  display: grid; place-items: center; flex: none; width: 38px; height: 38px;
  border: var(--lw) solid var(--spot); color: var(--spot);
  font: 700 .875rem/1 var(--f-mono);
}
.brand-txt { font: 700 var(--t-md)/1.2 var(--f-body); letter-spacing: -.01em; }
.brand-txt small { display: block; margin-top: 2px; font: 400 .68rem/1.3 var(--f-mono); text-transform: uppercase; letter-spacing: .1em; color: var(--ink-2); }
.mode-tabs { min-width: 240px; }
.masthead-act { display: flex; align-items: center; gap: var(--s-5); }
.nav-link { font: 600 .75rem/1 var(--f-mono); text-transform: uppercase; letter-spacing: .08em; text-decoration: none; }
.nav-link:hover { color: var(--spot-text); }
.sq-btn { width: 44px; height: 44px; }
@media (max-width: 860px) {
  .masthead { position: static; }
  .masthead-in { padding: var(--s-2) var(--s-4); }
  .mode-tabs { order: 3; flex: 1 1 100%; }
  .hide-sm { display: none; }
  .brand-txt small { display: none; }
}

/* ---------- 5. Workbench: tool rail + bench ---------- */
.workbench {
  max-width: 1440px; margin-inline: auto; padding: var(--s-5) var(--s-6) var(--s-7);
  display: flex; flex-wrap: wrap; align-items: flex-start; gap: var(--s-5);
}
.rail {
  flex: 1 1 210px; max-width: 240px;
  display: grid; gap: var(--s-5); padding: var(--s-4);
  border: var(--lw) solid var(--ink); background: var(--paper-2);
  position: sticky; top: calc(var(--mast-h) + var(--s-5));
}
.bench { flex: 999 1 640px; min-width: 0; display: grid; gap: var(--s-4); }

.rail-group { display: grid; gap: var(--s-2); margin: 0; padding: 0; border: 0; min-width: 0; }
.rail-group + .rail-group { padding-top: var(--s-4); border-top: 1px solid var(--hair); }
.rail-group > legend { margin-bottom: var(--s-2); }
.tools { display: grid; gap: var(--s-2); }
.tool { position: relative; display: block; cursor: pointer; }
.tool input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.tool span {
  display: flex; align-items: center; gap: var(--s-3); min-height: 44px; padding: 0 var(--s-3);
  border: var(--lw) solid var(--hair); border-radius: var(--r);
  font: 600 .95rem/1 var(--f-body);
}
.tool:hover span { border-color: var(--ink); }
.tool input:checked + span { background: var(--ink); color: var(--paper); border-color: var(--ink); }
.tool input:focus-visible + span { outline: 2px solid var(--spot); outline-offset: 2px; }
.tool canvas { width: 18px; height: 18px; flex: none; border: 1px solid var(--ink); }
.tool input:checked + span canvas { border-color: var(--paper); }
.tool kbd { margin-left: auto; font: 500 .7rem/1 var(--f-mono); color: inherit; opacity: .7; }
.rail-hint { margin-top: var(--s-1); font-size: var(--t-sm); line-height: 1.5; color: var(--ink-2); }
.rail-hint b { color: var(--ink); }
.kbd-help { display: block; margin-top: var(--s-2); font-size: .8rem; }
.board-actions { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--s-2); }
.board-actions .span-2 { grid-column: 1 / -1; }

/* Command bar */
.command { display: flex; flex-wrap: wrap; align-items: center; gap: var(--s-3) var(--s-4); }
.cmd-field { display: flex; align-items: center; gap: var(--s-3); margin: 0; padding: 0; border: 0; min-width: 0; }
.cmd-field > legend { float: left; margin-right: var(--s-3); }
.cmd-spacer { flex: 1 1 var(--s-5); }
.cmd-range input { width: 130px; }
.cmd-range output { min-width: 6.5em; font: 500 .8rem/1 var(--f-mono); color: var(--spot-text); white-space: nowrap; }
.cmd-buttons { display: flex; flex-wrap: wrap; gap: var(--s-2); margin-left: auto; }
.cmd-buttons .btn { min-height: 44px; padding: 0 var(--s-4); }
.cmd-buttons .btn-run { min-height: 48px; padding: 0 var(--s-5); font-size: var(--t-sm); font-weight: 700; }

.status {
  display: flex; align-items: center; gap: var(--s-3); min-height: 2.8em;
  padding: var(--s-2) var(--s-3); border: 1px solid var(--hair); border-left: 4px solid var(--ink); background: var(--paper);
  font: 500 var(--t-sm)/1.45 var(--f-mono);
}
.status[data-tone="good"] { border-left-color: var(--spot); }
.status[data-tone="bad"] { border-color: var(--spot); border-left-width: 4px; color: var(--spot-text); }

/* Boards */
.boards { display: grid; gap: var(--s-5); }
@media (min-width: 1200px) { .boards.is-compare { grid-template-columns: 1fr 1fr; } }
.board { display: grid; gap: var(--s-3); margin: 0; min-width: 0; }
.board-canvas {
  /* full width, but short enough that the controls above stay on screen */
  width: min(100% - 14px, calc((100svh - 270px) * var(--ar, 1.78))); min-width: min(100% - 14px, 300px);
  height: auto; aspect-ratio: 41 / 23; margin: 7px;
  border: var(--lw) solid var(--ink);
  outline: 1px dashed var(--hair); outline-offset: 6px;
  background: var(--paper); cursor: crosshair; touch-action: none;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.board-canvas:focus-visible { outline: 2px solid var(--spot); outline-offset: 6px; }

/* Title block under each board: figure name + stats (+ pad in Chase mode) */
.title-block { display: flex; flex-wrap: wrap; align-items: stretch; gap: var(--s-4); }
.tb-fig, .stats { border: var(--lw) solid var(--ink); background: var(--paper); }
.tb-fig {
  flex: 1 1 150px; display: flex; flex-direction: column; justify-content: center; gap: var(--s-1); padding: var(--s-2) var(--s-3);
  font: 400 .75rem/1.4 var(--f-mono); text-transform: uppercase; letter-spacing: .08em; color: var(--ink-2);
}
.tb-fig b { color: var(--spot-text); letter-spacing: .12em; }
.stats { flex: 999 1 420px; margin: 0; display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); }
.stats div { padding: var(--s-2) var(--s-3); min-width: 0; }
.stats div + div { border-left: 1px solid var(--hair); }
.stats dt { font: 500 .68rem/1.4 var(--f-mono); text-transform: uppercase; letter-spacing: .1em; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stats dd { margin: 0; font: 600 var(--t-lg)/1.3 var(--f-mono); letter-spacing: -.02em; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 760px) {
  .title-block { gap: 0; }
  .tb-fig { flex-basis: 100%; border-bottom: 0; flex-direction: row; justify-content: flex-start; gap: var(--s-3); }
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); flex-basis: 100%; }
  .stats div:nth-child(3) { border-left: 0; }
  .stats div:nth-child(n + 3) { border-top: 1px solid var(--hair); }
}

/* D-pad for moving the target */
.dpad {
  flex: none; display: grid; grid-template-columns: repeat(3, 48px); grid-template-rows: repeat(2, 48px);
  gap: 6px; justify-content: center; align-self: center;
}
.pad-btn { padding: 0; width: 48px; height: 48px; font-size: 1rem; touch-action: manipulation; }
.dpad [data-dir="up"] { grid-column: 2; grid-row: 1; }
.dpad [data-dir="left"] { grid-column: 1; grid-row: 2; }
.dpad [data-dir="down"] { grid-column: 2; grid-row: 2; }
.dpad [data-dir="right"] { grid-column: 3; grid-row: 2; }
@media (max-width: 760px) {
  .dpad { flex-basis: 100%; margin-top: var(--s-4); grid-template-columns: repeat(3, 56px); grid-template-rows: repeat(2, 56px); }
  .pad-btn { width: 56px; height: 56px; }
}

/* Legend strip */
.legend { display: flex; flex-wrap: wrap; gap: var(--s-2) var(--s-5); font: 400 .78rem/1.4 var(--f-mono); color: var(--ink-2); }
.legend li { display: flex; align-items: center; gap: var(--s-2); }
.legend canvas { width: 18px; height: 18px; flex: none; border: 1px solid var(--ink); }

/* Phone: grid first, tools after it, Run full width */
@media (max-width: 860px) {
  .workbench { padding: var(--s-4) var(--s-4) var(--s-7); gap: var(--s-4); }
  .rail { order: 2; flex-basis: 100%; max-width: none; position: static; }
  .tools { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .tool span { justify-content: center; gap: var(--s-2); padding: 0 var(--s-1); font-size: .875rem; white-space: nowrap; }
  .tool kbd { display: none; }
  .board-actions { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .board-actions .span-2 { grid-column: auto; }
  .kbd-help { display: none; }
  .cmd-spacer { display: none; }
  .command > .cmd-field:first-child { flex: 1 1 100%; }
  .command > .cmd-field:first-child .select { flex: 1 1 auto; }
  .cmd-range { flex: 1 1 100%; }
  .cmd-range input { flex: 1 1 auto; width: auto; }
  .cmd-buttons { flex: 1 1 100%; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .cmd-buttons .btn-run { grid-column: 1 / -1; order: -1; min-height: 52px; }
  .command[data-mode="chase"] .cmd-buttons { grid-template-columns: 1fr; }
}

/* ---------- 6. Manual ---------- */
.manual { border-top: var(--lw) solid var(--ink); background: var(--paper); }
.manual-in { max-width: 1440px; margin-inline: auto; padding: var(--s-7) var(--s-6) var(--s-8); display: grid; gap: var(--s-6); }
.manual-head {
  display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: var(--s-4);
  padding-bottom: var(--s-4); border-bottom: 1px solid var(--hair);
}
.manual-head h2 { font: 700 var(--t-h2)/1 var(--f-display); letter-spacing: -.03em; text-transform: uppercase; }
.folio { font: 500 var(--t-xs)/1 var(--f-mono); text-transform: uppercase; letter-spacing: .12em; color: var(--ink-2); }
.algos { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: var(--s-5); }
.algo { display: grid; align-content: start; gap: var(--s-3); padding: var(--s-5); border: var(--lw) solid var(--ink); }
.algo-no { justify-self: start; padding-top: var(--s-2); border-top: 2px solid var(--hair); font: 700 .85rem/1 var(--f-mono); color: var(--ink-2); }
.algo-hi .algo-no { border-color: var(--spot); color: var(--spot-text); }
.algo h3 { font: 700 var(--t-xl)/1.15 var(--f-display); letter-spacing: -.02em; }
.algo p, .notes p { color: var(--ink-2); }
.algo b, .notes b { color: var(--ink); }
.notes { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: var(--s-5); }
@media (max-width: 860px) { .manual-in { padding: var(--s-6) var(--s-4) var(--s-7); } }

/* ---------- 7. Colophon ---------- */
.colophon { border-top: var(--lw) solid var(--ink); background: var(--paper); font: 400 var(--t-xs)/1.5 var(--f-mono); text-transform: uppercase; letter-spacing: .08em; color: var(--ink-2); }
.colophon-in { max-width: 1440px; margin-inline: auto; padding: var(--s-5) var(--s-6); display: flex; justify-content: space-between; flex-wrap: wrap; gap: var(--s-2) var(--s-5); }
.colophon a { text-underline-offset: 4px; }
.colophon a:hover { color: var(--spot-text); }
@media (max-width: 860px) { .colophon-in { padding: var(--s-5) var(--s-4); } }

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .btn, .sq-btn { transition: none; }
}
