/*
 * css/race.css -- the race page (simulation2/index.html). Shares the app.css token names (--bg, --panel, --ink,
 * --muted, --line, --accent, --accent-ink, --canvas-bg, --warn) so the page matches debug.html's light/dark
 * theming, and adds the layout + the Pop/DNA-library rules those modules need (copied from tools/index.html's
 * "/* Library search", "/* Pop" and toast sections, re-skinned to these variables instead of hard-coded colors).
 */
:root {
  --bg: #f4f6f2;
  --panel: #ffffff;
  --ink: #1f2a1c;
  --muted: #66735f;
  --line: #d6ddd0;
  --accent: #3f7d20;
  --accent-ink: #ffffff;
  --canvas-bg: #eaf3e2;
  --warn: #b3261e;
  --good: #2f8f4e;
  --chip-bg: #e4ecdc;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161a14;
    --panel: #1f251c;
    --ink: #e6ecdf;
    --muted: #9aa892;
    --line: #34402e;
    --accent: #7fc257;
    --accent-ink: #10150d;
    --canvas-bg: #1b2417;
    --warn: #ff8a80;
    --good: #7fc257;
    --chip-bg: #2a3324;
  }
}
* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}
a { color: var(--accent); }
h1 { margin: 0; font-size: 20px; }
h2 { margin: 0; font-size: 15px; display: flex; align-items: center; gap: 0.4rem; }
h3 { margin: 0 0 6px; font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; }
.muted { color: var(--muted); font-weight: normal; }

header.top {
  padding: 12px 20px;
  display: flex;
  align-items: baseline;
  gap: 14px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
header.top .debug-link { margin-left: auto; font-size: 13px; }

/* Three-column layout: field | race | sidebar. Stacks to one column when the viewport can't fit all three
   (this page targets 1280px+ widths per spec, but stacking avoids horizontal scrolling on anything smaller). */
main.layout {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr) 320px;
  gap: 14px;
  padding: 14px;
  align-items: start;
}
@media (max-width: 1100px) {
  main.layout { grid-template-columns: minmax(0, 1fr); }
}

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 12px 14px; }
.panel + .panel { margin-top: 14px; }

button, select, textarea, input[type="number"], input[type="text"] {
  font: inherit; color: var(--ink); background: var(--panel);
  border: 1px solid var(--line); border-radius: 6px; padding: 4px 9px;
}
input[type="number"] { width: 6em; padding: 4px 6px; }
button:not(:disabled) { cursor: pointer; }
button:not(:disabled):hover { border-color: var(--accent); }
button:disabled, select:disabled, input:disabled { opacity: 0.5; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.ghost { background: transparent; }

/* ---------- Field (left) ---------- */
.field-list { display: flex; flex-direction: column; gap: 8px; margin-bottom: 10px; }
.field-row {
  display: grid;
  grid-template-columns: 56px 1fr auto;
  grid-template-areas: "preview main move";
  gap: 8px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
}
.field-row canvas.preview { grid-area: preview; width: 56px; height: 56px; object-fit: contain; image-rendering: pixelated; background: var(--canvas-bg); border-radius: 4px; }
.field-row .main { grid-area: main; min-width: 0; display: flex; flex-direction: column; gap: 4px; }
.field-row .name-status { display: flex; flex-wrap: wrap; gap: 4px 6px; }
.field-row .name-status input[type="text"] { flex: 1 1 100%; min-width: 0; }
.field-row .name-status select { width: 6.5em; }
.field-row .name-status input[type="number"] { width: 3.6em; }
.field-row .traits { display: flex; flex-wrap: wrap; gap: 3px; }
.trait-chip { font-size: 10px; padding: 1px 6px; border-radius: 10px; background: var(--chip-bg); color: var(--muted); }
.field-row .move { grid-area: move; display: flex; flex-direction: column; gap: 2px; }
.field-row .move button { padding: 1px 6px; font-size: 11px; line-height: 1.4; }
/* drag to reorder (page.js startRowDrag): touch drags from the preview picture only */
.field-row { cursor: grab; position: relative; }
.field-row input, .field-row select, .field-row button { cursor: auto; }
.field-row canvas.preview { touch-action: none; }
.field-row.shifting { transition: transform 0.15s ease; }
.field-row.dragging { z-index: 2; cursor: grabbing; box-shadow: 0 4px 14px rgba(0,0,0,0.25); opacity: 0.95; user-select: none; }
.field-empty { color: var(--muted); font-size: 12px; font-style: italic; padding: 6px 0; }
#pasteBox { width: 100%; min-height: 70px; resize: vertical; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; margin-bottom: 6px; }
.field-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.field-actions button { flex: 1 1 0; }
.field-actions + .field-actions { margin-top: 6px; }

/* ---------- Race (centre) ---------- */
.race-panel { display: flex; flex-direction: column; gap: 10px; }
.race-stage { position: relative; }
#raceCanvas { display: block; width: 100%; height: 420px; background: var(--canvas-bg); border-radius: 6px; cursor: grab; touch-action: none; }
#raceCanvas:active { cursor: grabbing; }

/* Fullscreen: the stage covers the whole screen and the canvas fills it; overlay buttons are shown/hidden by
   page.js (fullscreen state + race state both matter for the Start overlay, so CSS alone can't gate it).
   Separate rules: a browser that does not know one of the selectors would drop a combined rule entirely. The
   .is-fs class (set by page.js) covers the rest. */
#raceStage:fullscreen { width: 100vw; height: 100vh; background: #000; }
#raceStage:-webkit-full-screen { width: 100vw; height: 100vh; background: #000; }
#raceStage.is-fs { width: 100vw; height: 100vh; background: #000; }
#raceStage:fullscreen #raceCanvas { width: 100%; height: 100%; border-radius: 0; }
#raceStage:-webkit-full-screen #raceCanvas { width: 100%; height: 100%; border-radius: 0; }
#raceStage.is-fs #raceCanvas { width: 100vw; height: 100vh; border-radius: 0; }
.fs-overlay { display: none; position: absolute; }
.fs-start {
  top: 50%; left: 50%; transform: translate(-50%, -50%);
  font-size: 28px; padding: 16px 44px; border-radius: 10px;
  background: var(--accent); color: var(--accent-ink); border: none; font-weight: 600;
}
.fs-exit {
  top: 14px; right: 14px; width: 36px; height: 36px; border-radius: 50%;
  background: rgba(0,0,0,0.55); color: #fff; border: none; font-size: 16px; line-height: 1; padding: 0;
}
.control-bar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.control-bar label { display: flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
#statusLine { font-size: 12px; color: var(--muted); min-height: 1.4em; }
#statusLine .end { color: var(--warn); font-weight: 600; }

details.options-panel { border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; }
details.options-panel summary { cursor: pointer; font-weight: 600; font-size: 13px; }
.options-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(160px, 1fr)); gap: 8px 12px; margin-top: 10px; }
.options-grid label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.mechanics-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 4px 10px; margin-top: 8px; }
.mechanics-grid label { display: flex; align-items: center; gap: 5px; font-size: 12px; font-weight: normal; flex-direction: row; }

table.results-table { width: 100%; border-collapse: collapse; font-size: 12px; }
table.results-table th, table.results-table td { text-align: left; padding: 4px 6px; border-bottom: 1px solid var(--line); }
table.results-table tbody tr:first-child td { font-weight: 600; }
.dnf { color: var(--warn); }
.replay-row { display: flex; gap: 6px; flex-wrap: wrap; }
#replayOutput { width: 100%; min-height: 60px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 11px; display: none; }

/* ---------- Sidebar (right): Pop + DNA library, from tools/index.html ---------- */
.sidebar { display: flex; flex-direction: column; gap: 14px; }

/* Library search (js/dna-library.js) */
.library-search { display: flex; gap: 0.4rem; }
.library-search input {
  flex: 1; min-width: 0;
  border: 1px solid var(--line); border-radius: 0.5rem;
  padding: 0.35rem 0.6rem; background: var(--bg); color: var(--ink); font-size: 0.8rem;
}
.library-search input:focus { outline: none; border-color: var(--accent); }
.library-search button { padding: 0.2rem 0.6rem; }
.lib-match { background: color-mix(in srgb, var(--accent) 35%, transparent); color: inherit; border-radius: 2px; padding: 0 1px; }
.library-empty { list-style: none; padding: 0.5rem; color: var(--muted); font-size: 0.8rem; font-style: italic; }
#saveDnaBtn:disabled { opacity: 0.5; cursor: not-allowed; }

.category-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 0.4rem; }
.category-item {
  background: var(--bg); border-radius: 0.6rem; padding: 0.4rem 0.6rem;
  display: flex; flex-direction: column; gap: 0.3rem;
  transition: background 0.2s, border-color 0.2s; border: 2px solid transparent;
}
.category-item.selected { border-color: var(--accent); }
.category-header { display: flex; align-items: center; gap: 0.5rem; cursor: pointer; }
.category-name { flex: 1; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.category-actions { display: flex; gap: 0.2rem; align-items: center; }
.category-actions button { background: transparent; border: none; color: var(--muted); cursor: pointer; padding: 0.2rem; font-size: 0.8rem; }
.category-actions button:hover { color: var(--ink); }
.entry-list { list-style: none; padding-left: 1rem; margin: 0; display: flex; flex-direction: column; gap: 0.2rem; }
.entry-item {
  display: flex; align-items: center; gap: 0.4rem; padding: 0.2rem 0.4rem;
  background: var(--panel); border-radius: 0.4rem; cursor: pointer; transition: background 0.2s;
}
.entry-item:hover { background: var(--chip-bg); }
.entry-name { flex: 1; font-size: 0.85rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry-actions { display: flex; gap: 0.2rem; align-items: center; }
.entry-actions button { background: transparent; border: none; color: var(--muted); padding: 0.1rem; font-size: 0.7rem; }
.entry-actions button:hover { color: var(--ink); }
.drag-handle { cursor: grab; color: var(--muted); font-size: 0.9rem; user-select: none; }
.drag-handle:active { cursor: grabbing; }
.category-item.dragging, .entry-item.dragging { opacity: 0.4; }
.sidebar-controls { display: flex; gap: 0.5rem; flex-wrap: nowrap; }
.sidebar-controls button { flex: 1 1 0; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.import-export-btns { display: flex; gap: 0.5rem; margin-top: 0.5rem; }

/* Pop (js/pop-presets.js) */
.pop-toggle { cursor: pointer; user-select: none; }
.pop-toggle .chevron { font-size: 0.7rem; color: var(--muted); transition: transform 0.15s; }
.pop-toggle.collapsed .chevron { transform: rotate(-90deg); }
#popBody { display: flex; flex-direction: column; gap: 0.5rem; }
.preset-list { max-height: 220px; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.preset-row {
  display: flex; align-items: center; gap: 0.4rem; padding: 0.3rem 0.5rem;
  background: var(--bg); border-radius: 0.5rem; cursor: pointer; font-size: 0.8rem;
}
.preset-row:hover { background: var(--chip-bg); }
.preset-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.preset-dot { color: var(--good); font-size: 0.75rem; }
.preset-dot.invalid { color: var(--muted); }
.presets-empty { color: var(--muted); font-size: 0.8rem; font-style: italic; padding: 0.3rem; }

/* Public build: hides elements marked data-public-only in the dev page; the build script removes both
   this rule and the attributes for the public deploy. */

/* ---------- Public footer (the Horsey Game blurb): small and muted, below everything ---------- */
.promo { max-width: 720px; margin: 28px auto 0; padding: 14px 16px 0; border-top: 1px solid var(--line); color: var(--muted); font-size: 12px; line-height: 1.55; }
.promo p { margin: 0 0 8px; }
.promo ul { margin: 0 0 8px; padding-left: 20px; columns: 2; column-gap: 24px; }
.promo a { color: inherit; text-decoration: underline; }
.promo a:hover { color: var(--accent); }
/* inside the shortest panel (page.js places it): full panel width, one list column in the narrow side panels */
.promo.in-panel { max-width: none; margin: 14px 0 0; padding: 10px 0 0; }
.field-panel .promo.in-panel ul, .sidebar .promo.in-panel ul { columns: 1; }

/* ---------- Toast ---------- */
.toast-container { position: fixed; top: 1rem; right: 1rem; display: flex; flex-direction: column; gap: 0.5rem; z-index: 1000; }
.toast {
  background: var(--panel); color: var(--ink); padding: 0.7rem 1rem; border-radius: 0.5rem;
  box-shadow: 0 8px 20px rgba(0,0,0,0.35); font-size: 0.85rem; display: flex; align-items: center; gap: 0.5rem;
  animation: slideIn 0.3s ease; min-width: 200px; border-left: 4px solid var(--line);
}
.toast.success { border-left-color: var(--good); }
.toast.error { border-left-color: var(--warn); }
@keyframes slideIn { from { transform: translateX(100%); opacity: 0; } to { transform: translateX(0); opacity: 1; } }

/* ---------- Player control (Phase 12): the fullscreen stand-in and the four on-screen buttons ---------- */
/* Where the Fullscreen API is missing (iPhone Safari) the stage fills the window instead. */
.race-stage.fs-fill { position: fixed; inset: 0; z-index: 9999; background: #000; }
.race-stage#raceStage.fs-fill, .race-stage#boutStage.fs-fill { width: auto; height: auto; }
.race-stage.fs-fill #raceCanvas, .race-stage.fs-fill #boutCanvas { width: 100%; height: 100%; border-radius: 0; }
body.fs-fill-on { overflow: hidden; }
.pc-pad { position: absolute; inset: 0; pointer-events: none; }
.pc-cluster { position: absolute; bottom: calc(18px + env(safe-area-inset-bottom)); display: flex; flex-direction: column; gap: 12px; }
.pc-back { left: calc(18px + env(safe-area-inset-left)); }
.pc-front { right: calc(18px + env(safe-area-inset-right)); }
.pc-mirror .pc-back { left: auto; right: calc(18px + env(safe-area-inset-right)); }
.pc-mirror .pc-front { right: auto; left: calc(18px + env(safe-area-inset-left)); }
.pc-row .pc-back { flex-direction: row; }
.pc-row .pc-front { flex-direction: row-reverse; }
.pc-row.pc-mirror .pc-back { flex-direction: row-reverse; }
.pc-row.pc-mirror .pc-front { flex-direction: row; }
.pc-btn {
  pointer-events: auto; width: 64px; height: 64px; border-radius: 50%; padding: 0;
  background: rgba(255,255,255,0.25); color: #fff; border: 2px solid rgba(255,255,255,0.6);
  font: 600 13px system-ui, sans-serif; text-shadow: 0 1px 2px rgba(0,0,0,0.6);
  touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent;
}
.pc-btn.pc-down { background: rgba(255,255,255,0.75); color: #000; text-shadow: none; }
.pc-btn.pc-off { opacity: 0.3; }

/* ---------- public build (tools/build-deploy.js): clear the Ko-fi floating button ---------- */
body { padding-bottom: 120px; }
