:root {
  --bg: #ffffff;
  --panel: rgba(255, 255, 255, 0.94);
  --text: #1d2130;
  --muted: #646b7a;
  --line: #e3e6ec;
  --hover: #f1f4f9;
  --accent: #2f5bc4;
  --freight: #9a6440;
  --rail: #8d9098;
  --shadow: 0 6px 24px rgba(20, 26, 40, 0.18);
  /* The detail panel's side view: a daytime sky here, night in dark mode. */
  --stage-bg: linear-gradient(180deg, #cfe7fa, #eef5fb);
  --stage-text: #4a5875;
  --stage-tag: rgba(20, 26, 40, 0.08);
  --stage-track: repeating-linear-gradient(90deg, #9c8a70 0 6px, transparent 6px 14px), #d8cebd;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1116;
    --panel: rgba(22, 25, 32, 0.94);
    --text: #e6e8ee;
    --muted: #9198a6;
    --line: #2a2f3a;
    --hover: #222733;
    --accent: #7ea2ff;
    --freight: #a67a57;
    --rail: #6f727c;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
    --stage-bg: linear-gradient(180deg, #1b2440, #0f1422);
    --stage-text: #b9c4e4;
    --stage-tag: rgba(255, 255, 255, 0.12);
    --stage-track: repeating-linear-gradient(90deg, #4d4335 0 6px, transparent 6px 14px), #2a2620;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1116;
  --panel: rgba(22, 25, 32, 0.94);
  --text: #e6e8ee;
  --muted: #9198a6;
  --line: #2a2f3a;
  --hover: #222733;
  --accent: #7ea2ff;
  --freight: #a67a57;
  --rail: #6f727c;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.5);
  --stage-bg: linear-gradient(180deg, #1b2440, #0f1422);
  --stage-text: #b9c4e4;
  --stage-tag: rgba(255, 255, 255, 0.12);
  --stage-track: repeating-linear-gradient(90deg, #4d4335 0 6px, transparent 6px 14px), #2a2620;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}
html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font:
    14px/1.4 system-ui,
    -apple-system,
    "Segoe UI",
    sans-serif;
}
#map {
  position: absolute;
  inset: 0;
}
button {
  font: inherit;
  color: inherit;
}

/* ---- Panel ---- */
#panel {
  position: absolute;
  top: 12px;
  left: 12px;
  bottom: 12px;
  width: 340px;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  backdrop-filter: blur(8px);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
  z-index: 2;
}
#panel header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}
.brand {
  display: flex;
  gap: 10px;
  align-items: center;
}
.logo {
  width: 28px;
  height: 28px;
  border-radius: 7px;
  image-rendering: pixelated; /* drawn on a 16×16 grid by src/icon.js */
}
h1 {
  font-size: 16px;
  margin: 0;
}
/* Train count and last update, as subtext on the "Show trains" toggle. */
#status {
  color: var(--muted);
}
#status::before {
  content: "· ";
}
.tools button {
  border: 0;
  background: transparent;
  width: 32px;
  height: 32px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 16px;
}
.tools button:hover {
  background: var(--hover);
}
.panel-body {
  flex: 1;
  min-height: 0;
  overflow: auto;
}
#search {
  width: calc(100% - 24px);
  margin: 12px;
  padding: 9px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
#search:focus {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}
.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 14px;
  list-style: none;
  margin: -4px 14px 8px;
  padding: 0;
  font-size: 12px;
  color: var(--muted);
}
.legend li {
  display: flex;
  align-items: center;
  gap: 6px;
}
.swatch {
  width: 18px;
  height: 4px;
  border-radius: 2px;
}
.swatch.amtrak {
  background: var(--accent);
}
.swatch.freight {
  background:
    repeating-linear-gradient(90deg, var(--freight) 0 3px, transparent 3px 5px),
    linear-gradient(var(--freight), var(--freight)) center / 100% 2px no-repeat;
  height: 6px;
}
.legend-toggle {
  flex-basis: 100%;
  flex-wrap: wrap;
  gap: 4px 16px;
}
.legend-toggle label {
  display: flex;
  white-space: nowrap;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  color: var(--text);
}
.speed-info {
  margin: 0 14px 10px;
  font-size: 12px;
  color: var(--muted);
}
.speed-period {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 6px;
}
.speed-period select {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 1px 4px;
}
.speed-info .ramp {
  height: 8px;
  border-radius: 4px;
}
.speed-info .ramp-labels {
  display: flex;
  justify-content: space-between;
  margin: 2px 0 4px;
  font-variant-numeric: tabular-nums;
}
.swatch.passenger {
  background: var(--rail);
  height: 2px;
}
#detail {
  display: none;
  padding: 0 14px 16px;
}
.has-detail #list-view {
  display: none;
}
.has-detail #detail {
  display: block;
}
.empty {
  color: var(--muted);
  padding: 8px 14px;
}

.train-row {
  display: flex;
  gap: 10px;
  align-items: center;
  width: 100%;
  text-align: left;
  border: 0;
  background: transparent;
  padding: 8px 14px;
  cursor: pointer;
}
.train-row:hover,
.train-row.active {
  background: var(--hover);
}
.row-main {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.row-title {
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.row-sub {
  font-size: 12px;
  color: var(--muted);
}
.badge {
  display: inline-block;
  min-width: 40px;
  padding: 2px 6px;
  border: 0;
  border-radius: 6px;
  font-weight: 700;
  font-size: 12px;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

/* ---- Detail ---- */
.back {
  border: 0;
  background: transparent;
  color: var(--accent);
  padding: 12px 0 4px;
  cursor: pointer;
}
.sprite-stage {
  position: relative;
  margin: 8px 0 4px;
  padding: 8px 0 0;
  border-radius: 10px;
  background: var(--stage-bg);
  overflow: hidden;
  text-align: center;
}
/* The tag has its own row, so the station roof and sign below can never run into it. The
   station stands ~23 px above the train and its sign ~19 px above that (at the 300 px max);
   that space is always reserved so the view keeps one height whether or not a station shows. */
.sprite-stage .train-wrap {
  margin-top: 48px;
}
/* Sits left of centre so the headlight beam has room ahead of the loco before the stage edge. */
.train-wrap {
  position: relative;
  width: 300px;
  max-width: 84%;
  margin: 0 auto -2px 4%;
  container-type: inline-size;
}
/* Train number on the first car behind the loco, over its window band (side sprite: 132 art px
   wide, 14 tall; that car spans columns 48–86, windows on rows 3–4). A white outline keeps it
   readable over the blue. */
/* Headlight beam from the loco's nose (side sprite: headlight at column 128 of 132, row ~6 of
   14), widening and fading ahead; the stage clips it at its edge. */
.side-beam {
  position: absolute;
  z-index: 0;
  left: 97%;
  top: 46%;
  width: 34%;
  height: 70%;
  transform: translateY(-50%);
  background: linear-gradient(90deg, rgba(255, 236, 160, 0.8), rgba(255, 226, 140, 0.25) 55%, transparent);
  clip-path: polygon(0 44%, 100% 0, 100% 100%, 0 56%);
  pointer-events: none;
}
.car-number {
  /* On the locomotive's white upper body (art columns 99–122 of 132, rows 2–4 of 14). */
  position: absolute;
  z-index: 2;
  left: 84%;
  top: 26%;
  transform: translate(-50%, -50%);
  color: #c8102e;
  font:
    900 4.6cqw/1 system-ui,
    sans-serif;
  letter-spacing: 0.01em;
  -webkit-text-stroke: 0.25px #c8102e;
  text-shadow:
    -1px 0 #fff,
    1px 0 #fff,
    0 -1px #fff,
    0 1px #fff;
}
.train-wrap img {
  display: block;
  image-rendering: pixelated;
}
.train-wrap .train {
  position: relative;
  z-index: 1;
  width: 100%;
}
/* Station building sits behind the front of the train, its platform level with the rails. */
.train-wrap .station {
  position: absolute;
  bottom: 0;
  left: 60%;
  transform: translateX(-50%);
}
.station-sign {
  position: absolute;
  left: 60%;
  transform: translate(-50%, -3px);
  max-width: 70%;
  padding: 1px 6px;
  border-radius: 3px;
  background: #102257;
  border: 1px solid #f5b800;
  color: #fff;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.stage-stops {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 5px 10px 6px;
  font-size: 11px;
  color: var(--stage-text);
}
.stage-stops .stop {
  display: flex;
  gap: 4px;
  min-width: 0;
  max-width: 50%;
  white-space: nowrap;
}
.stage-stops .name {
  overflow: hidden;
  text-overflow: ellipsis;
}
.stage-stops .time {
  flex-shrink: 0;
  font-variant-numeric: tabular-nums;
}
.track {
  height: 8px;
  background: var(--stage-track);
}
.track.moving {
  animation: ties 1.6s linear infinite;
}
@keyframes ties {
  to {
    background-position: -14px 0;
  }
}
/* New information slides in from the left (see slideInChanges in app.js). */
.slide-in {
  animation: slide-in 0.45s cubic-bezier(0.2, 0.8, 0.2, 1);
}
/* The station building and its sign arrive together, gliding in from the left like a platform
   coming into view; both keep their centring transform. */
.train-wrap .station.slide-in,
.station-sign.slide-in {
  animation-duration: 1.1s;
  animation-timing-function: cubic-bezier(0.15, 0.7, 0.25, 1);
}
.train-wrap .station.slide-in {
  animation-name: station-arrive;
}
.station-sign.slide-in {
  animation-name: station-sign-arrive;
}
@keyframes station-arrive {
  from {
    opacity: 0;
    transform: translateX(calc(-50% - 160px));
  }
}
@keyframes station-sign-arrive {
  from {
    opacity: 0;
    transform: translate(calc(-50% - 160px), -3px);
  }
}
@keyframes slide-in {
  from {
    opacity: 0;
    transform: translateX(-14px);
  }
}
@media (prefers-reduced-motion: reduce) {
  .track.moving,
  .slide-in {
    animation: none;
  }
}
.motion-tag {
  display: block;
  width: fit-content;
  margin-left: 10px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  padding: 2px 7px;
  border-radius: 999px;
  background: var(--stage-tag);
  color: var(--stage-text);
}
.motion-tag.moving {
  background: #f5b800;
  color: #141a28;
}
.motion-tag.arriving {
  background: #2fb36a;
  color: #08210f;
}
.motion-tag.stale {
  background: #6b2a26;
  color: #ffd9d4;
}
.facts .stale dd {
  color: #c8102e;
}
#detail h2 {
  font-size: 18px;
  margin: 12px 0 2px;
  display: flex;
  gap: 8px;
  align-items: center;
}
.od {
  margin: 0 0 10px;
  color: var(--muted);
}
.facts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 0 0 12px;
}
.facts div {
  background: var(--hover);
  border-radius: 8px;
  padding: 8px 10px;
}
.facts dt {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.facts dd {
  margin: 2px 0 0;
  font-weight: 600;
}
.alert {
  background: color-mix(in srgb, #e0a030 18%, transparent);
  border-radius: 8px;
  padding: 8px 10px;
  margin: 0 0 10px;
  font-size: 13px;
}
.timeline {
  list-style: none;
  margin: 0;
  padding: 0;
}
.timeline li {
  position: relative;
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 0 6px 22px;
  cursor: pointer;
}
.timeline li::before {
  content: "";
  position: absolute;
  left: 6px;
  top: 0;
  bottom: 0;
  width: 2px;
  background: var(--line);
}
.timeline .dot {
  position: absolute;
  left: 1px;
  top: 10px;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 3px solid;
  background: var(--bg);
}
.timeline li.done {
  color: var(--muted);
}
.timeline li.done .dot {
  background: var(--line);
}
.timeline li.next .stop-name {
  font-weight: 700;
}
.stop-time {
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
  font-size: 13px;
}
.stop-time s {
  color: var(--muted);
}
.code {
  color: var(--muted);
  font-size: 12px;
}
.muted {
  color: var(--muted);
}

/* ---- Small screens: bottom sheet ---- */
@media (max-width: 640px) {
  #panel {
    top: auto;
    left: 8px;
    right: 8px;
    bottom: 8px;
    width: auto;
    max-height: 55vh;
  }
  .panel-collapsed #panel .panel-body {
    display: none;
  }
}
@media (min-width: 641px) {
  .panel-collapsed #panel {
    bottom: auto;
  }
  .panel-collapsed #panel .panel-body {
    display: none;
  }
}

/* ---- MapLibre controls follow the theme ---- */
.maplibregl-ctrl-group,
.maplibregl-ctrl-attrib,
.maplibregl-ctrl-attrib.maplibregl-compact {
  background: var(--panel);
  color: var(--text);
}
.maplibregl-ctrl-attrib a {
  color: var(--text);
}
.maplibregl-ctrl-group button + button {
  border-top-color: var(--line);
}
:root[data-theme="dark"] .maplibregl-ctrl-group button .maplibregl-ctrl-icon,
:root[data-theme="dark"] .maplibregl-ctrl-attrib-button {
  filter: invert(1);
}

/* ---- Train links ---- */
.links {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 12px;
}
.links a,
.links .share {
  font-size: 13px;
  padding: 6px 10px;
  border-radius: 8px;
  background: var(--hover);
  color: var(--accent);
  text-decoration: none;
  border: 0;
  cursor: pointer;
}
.links a:hover,
.links .share:hover {
  text-decoration: underline;
}

/* ---- My trip ---- */
#trip {
  display: none;
  padding: 0 14px 16px;
}
.trip-mode #list-view,
.trip-mode #detail {
  display: none;
}
.trip-mode #trip {
  display: block;
}
#trip h2 {
  font-size: 18px;
  margin: 8px 0 2px;
  display: flex;
  gap: 8px;
  align-items: center;
}
.trip-start {
  display: block;
  width: 100%;
  margin: 0 0 10px;
  padding: 9px 12px;
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: var(--bg);
  font-weight: 600;
  cursor: pointer;
}
.trip-start:hover {
  filter: brightness(1.08);
}
.trip-resume {
  display: flex;
  gap: 10px;
  align-items: center;
  width: calc(100% - 24px);
  margin: 0 12px 8px;
  padding: 8px 10px;
  text-align: left;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: color-mix(in srgb, var(--accent) 10%, transparent);
  cursor: pointer;
}
.trip-hero {
  margin: 4px 0 10px;
  padding: 12px 14px;
  border-radius: 10px;
  background: var(--stage-bg);
  color: var(--text);
}
.trip-kicker {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--stage-text);
}
.trip-big {
  margin: 2px 0;
  font-size: 30px;
  font-weight: 700;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}
.trip-big span {
  font-size: 14px;
  font-weight: 500;
  color: var(--stage-text);
}
.trip-sub,
.trip-note,
.trip-me {
  font-size: 13px;
}
.trip-sub {
  font-weight: 600;
}
.trip-note,
.trip-me {
  color: var(--stage-text);
  margin-top: 4px;
}
.trip-me::before {
  content: "";
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 6px;
  border: 2px solid #fff;
  border-radius: 50%;
  background: #1a73e8;
  box-shadow: 0 0 0 1px rgba(26, 115, 232, 0.4);
}
.trip-progress {
  height: 6px;
  margin: 8px 0 2px;
  border-radius: 3px;
  background: var(--stage-tag);
  overflow: hidden;
}
.trip-progress span {
  display: block;
  height: 100%;
  border-radius: 3px;
  background: var(--accent);
  transition: width 1s ease;
}
.trip-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin: 0 0 12px;
}
.trip-actions button {
  font-size: 13px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--hover);
  color: var(--accent);
  cursor: pointer;
}
.trip-actions button[aria-pressed="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--bg);
}
.trip-stops li {
  cursor: default;
}
.trip-end {
  margin-left: 4px;
  padding: 1px 5px;
  border-radius: 4px;
  background: var(--hover);
  color: var(--accent);
  font-size: 11px;
  font-weight: 600;
}
.trip-edit {
  margin-top: 12px;
  font-size: 13px;
}
.trip-edit summary {
  color: var(--accent);
  cursor: pointer;
}
.trip-edit label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}
.trip-edit select {
  max-width: 190px;
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 3px 6px;
}
@media (prefers-reduced-motion: reduce) {
  .trip-progress span {
    transition: none;
  }
}

/* ---- Station departures board (styled after station display screens) ---- */
#board {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(5, 8, 15, 0.45);
}
#board[hidden] {
  display: none;
}
.board-card {
  width: min(760px, 100%);
  max-height: calc(100vh - 32px);
  overflow: auto;
  background: #0b1430;
  color: #f2f4f8;
  border-radius: 10px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
  border: 1px solid #1f2c55;
  font-family: "Helvetica Neue", Arial, system-ui, sans-serif;
}
.board-head {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 14px 18px;
  background: #102257;
  border-bottom: 3px solid #f5b800;
}
.board-head > div:first-child {
  flex: 1;
  min-width: 0;
}
.board-title {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 0.02em;
}
.board-station {
  font-size: 14px;
  color: #b9c4e4;
}
.board-station span {
  color: #f5b800;
  font-weight: 700;
  margin-left: 4px;
}
.board-clock {
  font:
    700 24px/1 ui-monospace,
    Menlo,
    monospace;
  color: #f5b800;
  font-variant-numeric: tabular-nums;
}
.board-close {
  border: 0;
  background: transparent;
  color: #b9c4e4;
  font-size: 18px;
  cursor: pointer;
  width: 32px;
  height: 32px;
  border-radius: 6px;
}
.board-close:hover {
  background: rgba(255, 255, 255, 0.1);
}
.board-tabs {
  display: flex;
  gap: 4px;
  padding: 8px 18px 0;
}
.board-tabs button {
  border: 0;
  background: transparent;
  color: #8f9cc4;
  padding: 6px 10px;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  font-weight: 600;
}
.board-tabs button[aria-selected="true"] {
  color: #fff;
  border-bottom-color: #f5b800;
}
.board-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 16px;
}
.board-table th {
  text-align: left;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #8f9cc4;
  padding: 10px 12px 6px;
  border-bottom: 1px solid #1f2c55;
}
.board-table td {
  padding: 10px 12px;
  border-bottom: 1px solid #16224a;
  vertical-align: middle;
}
.board-table tbody tr:nth-child(even) {
  background: #0e1938;
}
.board-table tbody tr[data-id] {
  cursor: pointer;
}
.board-table tbody tr[data-id]:hover {
  background: #17285c;
}
.board-table .time,
.board-table .trk,
.board-table .num {
  font-family: ui-monospace, Menlo, monospace;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.board-table .time {
  color: #f5b800;
}
.board-table .train .route {
  display: block;
  font-size: 12px;
  color: #b9c4e4;
}
.board-table .place {
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
}
.board-table .status {
  font-weight: 700;
  white-space: nowrap;
}
.board-table .status.ok {
  color: #5fd38a;
}
.board-table .status.warn {
  color: #f5b800;
}
.board-table .status.bad {
  color: #ff6b5e;
}
.board-table .status.muted,
.board-table tr.gone td {
  color: #6c789e;
}
.board-table .trk {
  text-align: center;
}
.board-table .none {
  text-align: center;
  color: #8f9cc4;
  padding: 28px;
}
.board-foot {
  padding: 10px 18px;
  font-size: 12px;
  color: #8f9cc4;
}
@media (max-width: 640px) {
  .board-table {
    font-size: 14px;
  }
  .board-table th:nth-child(5),
  .board-table td:nth-child(5),
  .board-table .train .route {
    display: none;
  }
  .board-title {
    font-size: 20px;
  }
}
