:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --text: #16181d;
  --muted: #5d6470;
  --border: #e3e6eb;
  --route: #7b3fa0;
  --out: #1f6feb;
  --in: #d9480f;
  --station: #8a919c;
  --ok: #1a7f37;
  --warn: #b35900;
  --bad: #c62828;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .1);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --text: #e8eaee;
    --muted: #9aa1ad;
    --border: #2a2f38;
    --route: #b58ad1;
    --out: #58a6ff;
    --in: #ff8a4c;
    --station: #8b939f;
    --ok: #3fb950;
    --warn: #e3a008;
    --bad: #f85149;
    --shadow: 0 1px 3px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.layout {
  display: grid;
  grid-template-columns: 380px 1fr;
  height: 100%;
}

#map { grid-column: 2; grid-row: 1; height: 100%; }

.panel {
  grid-column: 1; grid-row: 1;
  overflow-y: auto;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; grid-template-rows: 55vh 1fr; }
  #map { grid-column: 1; grid-row: 1; }
  .panel { grid-column: 1; grid-row: 2; border-right: 0; border-top: 1px solid var(--border); }
}

.panel-head { padding: 16px; border-bottom: 1px solid var(--border); }
.route-switch {
  display: flex; gap: 4px; padding: 3px; margin-bottom: 14px; border-radius: 10px;
  background: color-mix(in srgb, var(--muted) 12%, transparent);
}
.route-switch button {
  flex: 1; padding: 6px 10px; border: 0; border-radius: 8px; cursor: pointer;
  background: transparent; color: var(--muted); font: inherit; font-size: 13px; font-weight: 600;
}
.route-switch button[aria-checked="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.route-switch button:focus-visible { outline: 2px solid var(--route); outline-offset: 1px; }
.route-switch .code { color: var(--route); margin-right: 6px; }
.subtitle a { margin-left: 8px; color: var(--route); }
.title-row { display: flex; gap: 12px; align-items: center; }
.route-badge {
  flex: none;
  display: grid; place-items: center;
  width: 44px; height: 44px; border-radius: 10px;
  background: var(--route); color: #fff;
  font-weight: 700; font-size: 18px;
}
h1 { margin: 0; font-size: 17px; }
.subtitle { margin: 2px 0 0; color: var(--muted); font-size: 12.5px; }

.status {
  margin-top: 12px;
  display: inline-flex; align-items: center; gap: 8px;
  padding: 4px 10px; border-radius: 999px;
  font-size: 12.5px; font-weight: 500;
  background: color-mix(in srgb, var(--muted) 12%, transparent);
}
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.status--live { color: var(--ok); background: color-mix(in srgb, var(--ok) 12%, transparent); }
.status--live .dot { background: var(--ok); animation: pulse 2s infinite; }
.status--stale { color: var(--warn); background: color-mix(in srgb, var(--warn) 14%, transparent); }
.status--stale .dot { background: var(--warn); }
.status--down { color: var(--bad); background: color-mix(in srgb, var(--bad) 12%, transparent); }
.status--down .dot { background: var(--bad); }
@keyframes pulse { 50% { opacity: .35; } }
@media (prefers-reduced-motion: reduce) { .status--live .dot { animation: none; } }

.section { padding: 14px 16px; border-bottom: 1px solid var(--border); }
h2 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.count { font-weight: 500; }
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; flex-wrap: wrap; }
.toggle { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); cursor: pointer; margin-bottom: 10px; }
.toggle input { accent-color: var(--route); margin: 0; }
.empty { margin: 0; color: var(--muted); }

.bus-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.bus-card {
  display: grid; grid-template-columns: 28px 1fr auto; gap: 10px; align-items: center;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--surface); cursor: pointer; text-align: left; width: 100%;
  font: inherit; color: inherit;
}
.bus-card:hover, .bus-card:focus-visible { border-color: var(--route); outline: none; }
.bus-card .arrow { width: 28px; height: 28px; }
.bus-card .line1 { font-weight: 600; }
.bus-card .line2 { color: var(--muted); font-size: 12.5px; }
.bus-card .age { font-size: 12px; color: var(--muted); text-align: right; white-space: nowrap; }
.bus-card.is-lost { opacity: .55; }
.tag {
  display: inline-block; margin-left: 6px; padding: 0 6px; border-radius: 4px;
  font-size: 11px; font-weight: 600; vertical-align: 1px;
  background: color-mix(in srgb, var(--muted) 15%, transparent); color: var(--muted);
}

.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-bottom: 10px; font-size: 12px; color: var(--muted); }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 5px; vertical-align: -1px; }
.swatch--out { background: var(--out); }
.swatch--in { background: var(--in); }
.swatch--station { background: var(--station); }

.stop-strip { list-style: none; margin: 0; padding: 0; position: relative; }
.stop-strip::before {
  content: ""; position: absolute; left: 7px; top: 8px; bottom: 8px; width: 2px; background: var(--route); opacity: .35;
}
.stop {
  position: relative; padding: 3px 0 3px 26px; font-size: 13px; cursor: pointer;
}
.stop::before {
  content: ""; position: absolute; left: 3px; top: 8px; width: 10px; height: 10px; border-radius: 50%;
  background: var(--surface); border: 2px solid var(--route);
}
.stop--terminus::before { background: var(--route); }
.stop .num { color: var(--muted); font-variant-numeric: tabular-nums; margin-right: 6px; }
.stop-divider { padding: 8px 0 4px 26px; font-size: 11.5px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; padding: 2px 0 4px 26px; }
.chip {
  font-size: 11.5px; font-weight: 600; padding: 1px 7px; border-radius: 999px; color: #fff; cursor: pointer; border: 0;
}
.chip--outbound { background: var(--out); }
.chip--inbound { background: var(--in); }
.chip--station, .chip--possible { background: var(--station); }

.panel-foot { padding: 14px 16px; color: var(--muted); font-size: 12px; margin-top: auto; }
.diagnostics { margin-top: 12px; }
.diagnostics summary { cursor: pointer; font-weight: 600; color: var(--text); }
.diagnostics table { width: 100%; margin: 8px 0; border-collapse: collapse; font-size: 11.5px; }
.diagnostics th { text-align: left; vertical-align: top; padding: 2px 8px 2px 0; white-space: nowrap; color: var(--text); font-weight: 600; }
.diagnostics td { padding: 2px 0; overflow-wrap: anywhere; }
.diagnostics pre {
  max-height: 220px; overflow: auto; margin: 0; padding: 8px; border-radius: 6px;
  background: color-mix(in srgb, var(--muted) 10%, transparent);
  font: 10.5px/1.45 ui-monospace, SFMono-Regular, Menlo, monospace; white-space: pre-wrap;
}

/* Map */
.base-tiles { filter: saturate(.55) contrast(.95); }
@media (prefers-color-scheme: dark) {
  .base-tiles { filter: invert(1) hue-rotate(180deg) brightness(.85) saturate(.5); }
  .leaflet-container { background: #1b1e24; }
}

/* Map markers */
.bus-marker { transition: opacity .3s; }
.bus-marker.is-lost { opacity: .45; }
.bus-marker svg { display: block; filter: drop-shadow(0 1px 1px rgba(0, 0, 0, .35)); }
.stop-label { font-size: 11px; font-weight: 600; }
.leaflet-popup-content { font: 13px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif; }
.leaflet-popup-content b { font-size: 14px; }
.leaflet-popup-content .muted { color: #5d6470; }
