:root {
  --ground: #e9edf2; --panel: #f3f6fa; --panel-2: #ffffff;
  --ink: #18202e; --ink-soft: #56617a; --ink-faint: #8b95a8;
  --line: #d3dae4; --line-soft: #e2e7ee;
  --accent: #a9690f; --accent-soft: #b57d12;
  --sea: #c3d3e6; --land: #cdd0c8; --land-hi: #b7bcae; --impass: #d5d9e0;
  --good: #3f8f5a; --cargo: #b57d12;
  --shadow: 0 1px 2px rgba(24,32,46,.08), 0 8px 24px rgba(24,32,46,.10);
}
@media (prefers-color-scheme: dark) {
  :root {
    --ground: #0d1119; --panel: #141a26; --panel-2: #1a2130;
    --ink: #e7ecf4; --ink-soft: #9aa6ba; --ink-faint: #6b7688;
    --line: #232c3c; --line-soft: #1b2331;
    --accent: #e6b04a; --accent-soft: #e0a83a;
    --sea: #16273f; --land: #2a3446; --land-hi: #384660; --impass: #171b24;
    --good: #5ab27a; --cargo: #e0a83a;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 10px 30px rgba(0,0,0,.4);
  }
}
:root[data-theme="light"] {
  --ground: #e9edf2; --panel: #f3f6fa; --panel-2: #ffffff;
  --ink: #18202e; --ink-soft: #56617a; --ink-faint: #8b95a8;
  --line: #d3dae4; --line-soft: #e2e7ee;
  --accent: #a9690f; --accent-soft: #b57d12;
  --sea: #c3d3e6; --land: #cdd0c8; --land-hi: #b7bcae; --impass: #d5d9e0;
  --good: #3f8f5a; --cargo: #b57d12;
  --shadow: 0 1px 2px rgba(24,32,46,.08), 0 8px 24px rgba(24,32,46,.10);
}
:root[data-theme="dark"] {
  --ground: #0d1119; --panel: #141a26; --panel-2: #1a2130;
  --ink: #e7ecf4; --ink-soft: #9aa6ba; --ink-faint: #6b7688;
  --line: #232c3c; --line-soft: #1b2331;
  --accent: #e6b04a; --accent-soft: #e0a83a;
  --sea: #16273f; --land: #2a3446; --land-hi: #384660; --impass: #171b24;
  --good: #5ab27a; --cargo: #e0a83a;
  --shadow: 0 1px 2px rgba(0,0,0,.3), 0 10px 30px rgba(0,0,0,.4);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0; background: var(--ground); color: var(--ink);
  font-family: system-ui, "Segoe UI", Roboto, sans-serif;
  font-size: 14px; line-height: 1.5; -webkit-font-smoothing: antialiased;
}
.serif { font-family: "Constantia", "Cambria", Georgia, "Times New Roman", serif; }
.mono { font-family: "Cascadia Code", "Consolas", ui-monospace, monospace; font-variant-numeric: tabular-nums; }

.app {
  display: grid; grid-template-columns: 1fr 392px; gap: 0;
  height: 100vh; width: 100%;
}
/* ---------- MAP ---------- */
/* the map hero stays dark in both UI themes — the baked terrain is dark-tinted,
   so the letterbox void around it is a matching near-black, and everything laid
   over the map (title, legend, transport, tooltip) is pinned to the dark palette
   regardless of the UI theme the rail follows. */
.stage {
  position: relative; overflow: hidden; background: #090d14; cursor: grab;
  --panel: #141a26; --panel-2: #1a2130;
  --ink: #e7ecf4; --ink-soft: #9aa6ba; --ink-faint: #6b7688;
  --line: #232c3c; --line-soft: #1b2331; --accent: #e6b04a;
  color: var(--ink);   /* descendants without their own colour inherit light-on-dark */
}
.stage.grabbing { cursor: grabbing; }
.stage::after {   /* soft vignette for depth over the raster */
  content: ""; position: absolute; inset: 0; pointer-events: none; z-index: 2;
  box-shadow: inset 0 0 140px 10px rgba(4,7,12,.55);
}
canvas#map { position: absolute; inset: 0; width: 100%; height: 100%; display: block; z-index: 1; }

.titleblock {
  position: absolute; top: 22px; left: 26px; max-width: 60%; z-index: 3;
  pointer-events: none; text-shadow: 0 1px 12px color-mix(in srgb, var(--ground) 70%, transparent);
}
.eyebrow {
  font-size: 11px; letter-spacing: .18em; text-transform: uppercase;
  color: var(--accent); font-weight: 600; margin: 0 0 6px;
}
h1.title { font-size: clamp(22px, 3vw, 34px); margin: 0; font-weight: 600; line-height: 1.05; text-wrap: balance; letter-spacing: -.01em; }
.subtitle { color: var(--ink-soft); margin: 8px 0 0; font-size: 13px; max-width: 44ch; }

.legend {
  position: absolute; left: 26px; bottom: 92px; z-index: 3;
  display: flex; flex-direction: column; gap: 3px;
  background: color-mix(in srgb, var(--panel) 78%, transparent);
  backdrop-filter: blur(8px); border: 1px solid var(--line);
  border-radius: 10px; padding: 10px 12px; box-shadow: var(--shadow);
}
.legend .lg-h { font-size: 10px; letter-spacing: .14em; text-transform: uppercase; color: var(--ink-faint); margin-bottom: 3px; }
.legrow {
  display: flex; align-items: center; gap: 9px; padding: 3px 6px; border-radius: 6px;
  cursor: pointer; border: 1px solid transparent; font-size: 12.5px; color: var(--ink-soft);
  background: none; text-align: left; width: 100%; transition: background .12s, color .12s;
}
.legrow:hover { background: var(--line-soft); color: var(--ink); }
.legrow[aria-pressed="true"] { background: var(--line-soft); color: var(--ink); border-color: var(--line); }
.legrow .dot { width: 11px; height: 3px; border-radius: 2px; flex: none; }
.legrow.reset { color: var(--ink-faint); }
.legrow .km { margin-left: auto; font-size: 11px; color: var(--ink-faint); font-variant-numeric: tabular-nums; }
.heatkey { margin-top: 5px; padding-top: 7px; border-top: 1px solid var(--line-soft); }
.hk-bar { height: 8px; border-radius: 4px; margin-top: 2px;
  background: linear-gradient(90deg, rgba(236,178,96,.30), rgba(228,96,56,.95)); }
.hk-scale { display: flex; justify-content: space-between; font-size: 10px; color: var(--ink-faint);
  margin-top: 3px; font-variant-numeric: tabular-nums; }

/* ---------- TRANSPORT ---------- */
.transport {
  position: absolute; left: 26px; right: 26px; bottom: 20px; z-index: 4;
  display: flex; align-items: center; gap: 16px;
  background: color-mix(in srgb, var(--panel) 82%, transparent);
  backdrop-filter: blur(10px); border: 1px solid var(--line);
  border-radius: 12px; padding: 12px 16px; box-shadow: var(--shadow);
}
.playbtn {
  flex: none; width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--accent); background: color-mix(in srgb, var(--accent) 14%, transparent);
  color: var(--accent); cursor: pointer; display: grid; place-items: center;
  transition: background .12s, transform .1s;
}
.playbtn:hover { background: color-mix(in srgb, var(--accent) 24%, transparent); }
.playbtn:active { transform: scale(.94); }
.playbtn svg { width: 16px; height: 16px; fill: currentColor; }
.tl-wrap { flex: 1; display: flex; flex-direction: column; gap: 5px; }
.tl-dates { display: flex; justify-content: space-between; font-size: 11px; color: var(--ink-faint); }
.datepill {
  color: var(--ink); font-weight: 600; font-size: 13px;
  font-family: "Cascadia Code", "Consolas", ui-monospace, monospace;
}
input[type="range"].scrub {
  -webkit-appearance: none; appearance: none; width: 100%; height: 5px; border-radius: 3px;
  background: var(--line); outline: none; cursor: pointer;
}
input[type="range"].scrub::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none; width: 15px; height: 15px; border-radius: 50%;
  background: var(--accent); border: 2px solid var(--panel-2); box-shadow: 0 1px 4px rgba(0,0,0,.3);
}
input[type="range"].scrub::-moz-range-thumb {
  width: 15px; height: 15px; border-radius: 50%; background: var(--accent);
  border: 2px solid var(--panel-2); box-shadow: 0 1px 4px rgba(0,0,0,.3);
}
.speed { flex: none; display: flex; gap: 2px; background: var(--line-soft); border-radius: 8px; padding: 3px; }
.speed button {
  border: none; background: none; color: var(--ink-soft); cursor: pointer;
  font-size: 11px; padding: 4px 8px; border-radius: 6px; font-variant-numeric: tabular-nums;
}
.speed button[aria-pressed="true"] { background: var(--panel-2); color: var(--ink); box-shadow: var(--shadow); }

/* province tooltip */
.tip {
  position: absolute; z-index: 6; pointer-events: none; opacity: 0; transform: translateY(-4px);
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  padding: 7px 10px; box-shadow: var(--shadow); font-size: 12px; max-width: 220px;
  transition: opacity .1s;
}
.tip.on { opacity: 1; }
.tip b { font-weight: 600; }
.tip .r { color: var(--ink-faint); font-size: 11px; }

/* ---------- RAIL ---------- */
.rail {
  background: var(--panel); border-left: 1px solid var(--line);
  overflow-y: auto; padding: 22px 22px 40px; display: flex; flex-direction: column; gap: 20px;
}
.runmeta .rm-title { font-size: 12px; letter-spacing: .04em; color: var(--ink); font-weight: 600; }
.runmeta .rm-sub { color: var(--ink-faint); font-size: 12px; margin-top: 2px; }
.metagrid { display: grid; grid-template-columns: 1fr 1fr; gap: 1px; margin-top: 14px; background: var(--line); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.metacell { background: var(--panel-2); padding: 10px 12px; }
.metacell .k { font-size: 10px; letter-spacing: .1em; text-transform: uppercase; color: var(--ink-faint); }
.metacell .v { font-size: 18px; font-weight: 600; margin-top: 2px; font-variant-numeric: tabular-nums; }
.metacell .v small { font-size: 11px; color: var(--ink-soft); font-weight: 400; }

.sectlabel { font-size: 11px; letter-spacing: .14em; text-transform: uppercase; color: var(--ink-faint); margin: 0 0 2px; }

/* comparison table */
table.cmp { width: 100%; border-collapse: collapse; font-size: 12.5px; }
table.cmp th { text-align: left; font-weight: 500; color: var(--ink-faint); font-size: 10.5px; letter-spacing: .06em; text-transform: uppercase; padding: 0 6px 6px 0; }
table.cmp th.num, table.cmp td.num { text-align: right; font-variant-numeric: tabular-nums; }
table.cmp td { padding: 7px 6px 7px 0; border-top: 1px solid var(--line-soft); }
table.cmp tr.click { cursor: pointer; }
table.cmp tr.click:hover td { background: var(--line-soft); }
.destcell { display: flex; align-items: center; gap: 8px; }
.destcell .dot { width: 9px; height: 9px; border-radius: 50%; flex: none; }

/* detail card */
.detail { display: flex; flex-direction: column; gap: 18px; }
.backbtn { align-self: flex-start; border: 1px solid var(--line); background: var(--panel-2); color: var(--ink-soft);
  font-size: 12px; padding: 5px 11px; border-radius: 7px; cursor: pointer; display: flex; gap: 6px; align-items: center; }
.backbtn:hover { color: var(--ink); border-color: var(--ink-faint); }
.d-head { display: flex; align-items: baseline; gap: 10px; }
.d-head .route-dot { width: 12px; height: 12px; border-radius: 50%; flex: none; align-self: center; }
.d-head h2 { font-size: 20px; margin: 0; font-weight: 600; }
.statrow { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.stat { background: var(--panel-2); border: 1px solid var(--line); border-radius: 9px; padding: 9px 11px; }
.stat .k { font-size: 10px; letter-spacing: .08em; text-transform: uppercase; color: var(--ink-faint); }
.stat .v { font-size: 17px; font-weight: 600; margin-top: 3px; font-variant-numeric: tabular-nums; }

.chart { background: var(--panel-2); border: 1px solid var(--line); border-radius: 10px; padding: 12px 12px 8px; }
.chart .c-head { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 4px; }
.chart .c-title { font-size: 12px; font-weight: 600; }
.chart .c-now { font-size: 12px; font-variant-numeric: tabular-nums; }
.chart svg { width: 100%; height: 96px; display: block; overflow: visible; }

.cargo-list { display: flex; flex-direction: column; gap: 7px; }
.cargo-item { display: grid; grid-template-columns: 92px 1fr auto; gap: 9px; align-items: center; font-size: 12px; }
.cargo-item .cname { color: var(--ink-soft); text-transform: capitalize; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cargo-bar { height: 7px; border-radius: 4px; background: var(--line); overflow: hidden; }
.cargo-bar > i { display: block; height: 100%; border-radius: 4px; background: var(--cargo); }
.cargo-item .cval { font-variant-numeric: tabular-nums; color: var(--ink); font-size: 11.5px; }
.cargo-more { color: var(--ink-faint); font-size: 11.5px; }

.footnote { color: var(--ink-faint); font-size: 11px; line-height: 1.5; border-top: 1px solid var(--line-soft); padding-top: 14px; }
.footnote code { font-family: "Cascadia Code","Consolas",ui-monospace,monospace; color: var(--ink-soft); }

.themebtn {
  position: absolute; top: 22px; right: 22px; z-index: 5; width: 34px; height: 34px; border-radius: 8px;
  border: 1px solid var(--line); background: color-mix(in srgb, var(--panel) 80%, transparent);
  color: var(--ink-soft); cursor: pointer; display: grid; place-items: center; backdrop-filter: blur(6px);
}
.themebtn:hover { color: var(--ink); }

/* ---------- ZOOM CONTROL ---------- */
.zoomctl {
  position: absolute; right: 22px; bottom: 92px; z-index: 4;
  display: flex; flex-direction: column; gap: 4px;
}
.zoomctl button {
  width: 34px; height: 34px; border-radius: 8px; border: 1px solid var(--line);
  background: color-mix(in srgb, var(--panel) 80%, transparent); color: var(--ink-soft);
  cursor: pointer; display: grid; place-items: center; backdrop-filter: blur(6px);
  font-size: 17px; line-height: 1;
}
.zoomctl button:hover { color: var(--ink); }
.zoomctl button svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 2; }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 62vh 1fr; height: auto; }
  .rail { border-left: none; border-top: 1px solid var(--line); }
  .titleblock { max-width: 76%; }
  .legend { bottom: 96px; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
