:root {
  color-scheme: light;
  /* OnTrack brand colors, sampled from the logo files */
  --maroon: #83272d;
  --gray: #808080;
  /* Interface tokens (light) */
  --accent: var(--maroon);          /* decorative borders and fills */
  --focus: var(--maroon);           /* focus rings and active borders (needs 3:1) */
  --accent-text: var(--maroon);     /* maroon used as text */
  --accent-tint: #f6ecec;
  --ink: #2a2a2a;
  --muted: #6b6b6b;
  --paper: #f7f7f7;
  --panel: #fff;
  --field: #fff;
  --field-border: #bbb;
  --button: #f4f4f4;
  --line: #dcdcdc;
  --status-bg: rgba(255,255,255,.85);
  /* Canvas tokens, read by app.js on every draw */
  --cv-board: #fff;
  --cv-grid: rgba(0,0,0,.06);
  --cv-board-edge: #808080;
  --cv-track: #3d3d3d;
  --cv-other: rgba(120,120,120,.25);
  --cv-select: #83272d;
  --cv-end-fill: #fff;
  --cv-roadbed: #a9a69f;
  --cv-roadbed-edge: #8d8a83;
  --cv-tie: #6f6a62;
  --cv-rail-shadow: #4b4b4b;
  --cv-rail: #dcdcdc;
  --cv-joint: #5f5c56;
  --cv-machine: #7a7670;
  --cv-bad: #c76a00;               /* drag preview that will not fit */
}
/* Dark theme: used when the viewer picks Dark, or picks System and the device is dark. */
:root[data-theme="dark"] {
  color-scheme: dark;
  --accent: var(--maroon);          /* brand maroon for decorative bars and borders */
  --focus: #be3941;                 /* darkest brand-hue red with 3:1 on the panel */
  --accent-text: #d1646b;           /* darkest brand-hue red with 4.5:1 for text */
  --accent-tint: #33201f;
  --ink: #e6e6e6;
  --muted: #a3a3a3;
  --paper: #161616;
  --panel: #1f1f1f;
  --field: #2a2a2a;
  --field-border: #555;
  --button: #2c2c2c;
  --line: #383838;
  --status-bg: rgba(31,31,31,.88);
  --cv-board: #232323;
  --cv-grid: rgba(255,255,255,.06);
  --cv-board-edge: #8a8a8a;
  --cv-track: #d0d0d0;
  --cv-other: rgba(180,180,180,.25);
  --cv-select: #c33a43;
  --cv-end-fill: #232323;
  --cv-roadbed: #6e6b65;
  --cv-roadbed-edge: #57544f;
  --cv-tie: #48443e;
  --cv-rail-shadow: #1c1c1c;
  --cv-rail: #c9c9c9;
  --cv-joint: #2e2c29;
  --cv-machine: #4f4b45;
  --cv-bad: #f0a030;
}
.logo-dark { display: none; }
:root[data-theme="dark"] .logo-light { display: none; }
:root[data-theme="dark"] .logo-dark { display: inline; }

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; font: 14px/1.4 system-ui, sans-serif; color: var(--ink); background: var(--paper); }
body { display: grid; grid-template-columns: 340px 1fr; }

.side {
  overflow-y: auto; border-right: 1px solid var(--line); padding: 12px 14px 16px; background: var(--panel);
  display: flex; flex-direction: column;
}
.brand { border-bottom: 3px solid var(--accent); padding-bottom: 8px; margin-bottom: 4px; }
.brand h1 { margin: 2px 0 2px; line-height: 0; }
.brand h1 img { width: 100%; max-width: 280px; height: auto; }
.tagline { margin: 0; color: var(--muted); font-size: 12px; letter-spacing: .02em; }
.side h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--accent-text); margin: 14px 0 6px; }
.side section { border-top: 1px solid var(--line); padding-top: 4px; }
.credits {
  margin-top: auto; padding-top: 14px; border-top: 1px solid var(--line);
  color: var(--muted); font-size: 11px; text-align: center;
}
.credits .version { color: var(--accent-text); font-weight: 600; font-size: 12px; }

label { display: block; margin: 4px 0; }
label.check { display: inline-flex; align-items: center; gap: 4px; margin-right: 10px; }
input, select { width: 100%; padding: 4px 6px; border: 1px solid var(--field-border); border-radius: 4px; font: inherit; background: var(--field); color: var(--ink); }
input:focus, select:focus { outline: 2px solid var(--focus); outline-offset: -1px; border-color: var(--focus); }
input[type=checkbox], input[type=radio] { width: auto; accent-color: var(--focus); }
.row { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin: 6px 0; }
.row label { margin: 0; }
[hidden] { display: none !important; }

button, label.button {
  display: inline-block; padding: 5px 10px; border: 1px solid var(--gray); border-radius: 4px;
  background: var(--button); color: var(--ink); font: inherit; cursor: pointer;
}
button:hover, label.button:hover { background: var(--accent-tint); border-color: var(--focus); }
button:focus-visible, label.button:focus-within { outline: 2px solid var(--focus); outline-offset: 1px; }
button:disabled { opacity: .5; cursor: default; }

.hint { color: var(--muted); font-size: 12px; margin: 4px 0 8px; }
.tabs { display: flex; gap: 2px; margin: 8px 0 0; border-bottom: 2px solid var(--accent); }
.tabs .tab {
  flex: 1; border: 1px solid var(--line); border-bottom: none; border-radius: 4px 4px 0 0;
  padding: 4px 2px; font-size: 12px; background: var(--button);
}
.tabs .tab[aria-selected="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
.tabs .tab .count { display: block; font-size: 11px; font-weight: 600; line-height: 1.1; }
.tabs .tab .count:empty { display: none; }
.palette .piece.incompatible { opacity: .4; }
.palette .piece.incompatible:hover { opacity: .65; }
.palette { display: grid; gap: 4px; margin: 6px 0; max-height: 360px; overflow-y: auto; }
.palette .piece {
  display: flex; align-items: center; gap: 8px; text-align: left; font-size: 12px;
  padding: 3px 6px; cursor: grab; touch-action: pan-y; user-select: none; -webkit-user-select: none;
}
.palette .piece:active { cursor: grabbing; }
.palette .piece .thumb { flex: none; }
.palette .piece b { display: block; font-size: 12px; }
.palette .piece span span { color: var(--muted); }
.tools button { padding: 4px 8px; font-size: 12px; }

table { width: 100%; border-collapse: collapse; font-size: 12px; margin-bottom: 6px; }
th, td { border-bottom: 1px solid var(--line); padding: 3px 4px; text-align: left; }

.stage { position: relative; min-width: 0; min-height: 0; }
#canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; cursor: crosshair; touch-action: none; }
#msg {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
  background: var(--accent-tint); color: var(--ink); border: 1px solid var(--accent); border-left-width: 4px; border-radius: 6px; padding: 6px 12px;
  max-width: 80%; font-size: 13px; pointer-events: none;
}
#msg:empty { display: none; }
#status {
  position: absolute; bottom: 0; left: 0; right: 0; padding: 4px 10px;
  background: var(--status-bg); border-top: 1px solid var(--line); font-size: 12px; color: var(--muted);
}

@media (max-width: 760px) {
  body { grid-template-columns: 1fr; grid-template-rows: 45vh 1fr; }
  .side { order: 2; border-right: none; border-top: 3px solid var(--accent); }
  .stage { order: 1; }
}
