:root {
  --ground: #EEF0EC;
  --surface: #FFFFFF;
  --surface-2: #F3F5F7;
  --ink: #141A22;
  --muted: #5B6573;
  --line: #DDE2E8;
  --line-strong: #C9CED4;
  --accent: #1B4FD8;
  --accent-ink: #FFFFFF;
  --ontime: #1F6FEB;
  --minor: #E8890C;
  --major: #C42B1C;
  --planned: #8A939E;
  --live-bg: #E7F0FE;
  --live-ink: #123E8C;
  --minor-bg: #FDF1E1;
  --minor-ink: #3D2300;
  --major-bg: #FBE5E3;
  --major-ink: #5C120B;
  --shadow: 0 2px 12px rgba(20, 26, 34, .14);
  --sans: 'IBM Plex Sans', system-ui, sans-serif;
  --mono: 'IBM Plex Mono', ui-monospace, monospace;
  color-scheme: light;
}

/* Dark: follows the device unless the switch picks light or dark. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --ground: #343B45;
    --surface: #1E242D;
    --surface-2: #262D37;
    --ink: #E6EAF0;
    --muted: #9AA4B1;
    --line: #2A313B;
    --line-strong: #3A424E;
    --accent: #8AB4FF;
    --accent-ink: #0B1B3A;
    --ontime: #4C8DFF;
    --minor: #F0A33A;
    --major: #F0635A;
    --planned: #7D8794;
    --live-bg: #1B2A45;
    --live-ink: #B5CEFF;
    --minor-bg: #3A2A12;
    --minor-ink: #F5C98A;
    --major-bg: #3D1A17;
    --major-ink: #F7B4AE;
    --shadow: 0 2px 14px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --ground: #343B45;
  --surface: #1E242D;
  --surface-2: #262D37;
  --ink: #E6EAF0;
  --muted: #9AA4B1;
  --line: #2A313B;
  --line-strong: #3A424E;
  --accent: #8AB4FF;
  --accent-ink: #0B1B3A;
  --ontime: #4C8DFF;
  --minor: #F0A33A;
  --major: #F0635A;
  --planned: #7D8794;
  --live-bg: #1B2A45;
  --live-ink: #B5CEFF;
  --minor-bg: #3A2A12;
  --minor-ink: #F5C98A;
  --major-bg: #3D1A17;
  --major-ink: #F7B4AE;
  --shadow: 0 2px 14px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: flex; flex-direction: column; overscroll-behavior: none; -webkit-tap-highlight-color: transparent;
  background: var(--ground); color: var(--ink);
  font: 15px/1.4 var(--sans);
}
button { font: inherit; color: inherit; cursor: pointer; }
.mono { font-family: var(--mono); }

/* Top bar */
.bar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 12px 20px;
  padding: 12px 20px; background: var(--surface); border-bottom: 1px solid var(--line);
  position: relative; z-index: 3;
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none; font-size: 18px; font-weight: 700; }
.search {
  position: relative; flex: 1 1 300px; max-width: 520px;
  display: flex; align-items: center; gap: 10px; height: 44px; padding: 0 14px;
  border-radius: 12px; background: var(--surface-2); border: 1px solid var(--line); color: var(--muted);
}
.search:focus-within { border-color: var(--accent); }
.search input { flex: 1; width: 0; min-width: 0; border: 0; background: transparent; font: inherit; font-size: 16px; color: var(--ink); outline: none; }
.search kbd { font-family: var(--mono); font-size: 12px; border: 1px solid var(--line); border-radius: 6px; padding: 1px 6px; }
.results {
  position: absolute; top: 50px; left: 0; right: 0; margin: 0; padding: 6px; list-style: none;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow);
  max-height: 60vh; overflow: auto;
}
.results li button {
  width: 100%; display: flex; align-items: center; gap: 10px; min-height: 44px; padding: 8px 10px;
  border: 0; border-radius: 8px; background: transparent; text-align: left;
}
.results li button:hover, .results li button:focus-visible { background: var(--surface-2); outline: none; }
.results .kind { font-size: 12px; color: var(--muted); margin-left: auto; }
.results .empty { padding: 10px; color: var(--muted); }

.theme-btn {
  width: 44px; height: 44px; flex: none; border-radius: 12px; display: grid; place-items: center;
  border: 1px solid var(--line); background: var(--surface-2); color: var(--ink);
}
.theme-btn:hover { border-color: var(--line-strong); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips button {
  height: 36px; padding: 0 14px; border-radius: 18px; font-size: 14px; font-weight: 500;
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--ink);
}
.chips button[aria-pressed="true"] { background: var(--ink); border-color: var(--ink); color: var(--surface); }

.legend {
  position: absolute; top: 16px; right: 16px; z-index: 2; margin: 0; padding: 10px 14px; list-style: none;
  display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted);
  background: var(--surface); border-radius: 12px; box-shadow: var(--shadow);
}
.legend li { display: flex; align-items: center; gap: 6px; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; }
.dot.ontime { background: var(--ontime); }
.dot.minor { background: var(--minor); }
.dot.major { background: var(--major); }
.dot.planned { border: 2px solid var(--planned); }

/* Map + panel */
.body { flex: 1; display: flex; min-height: 0; }
.mapwrap { position: relative; flex: 1; min-width: 0; }
#map { position: absolute; inset: 0; }
.panel {
  position: relative; width: 380px; flex: none; overflow-y: auto; padding: 24px;
  background: var(--surface); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; gap: 20px;
}
.panel[hidden] { display: none; }

.status {
  position: absolute; left: 16px; bottom: 16px; z-index: 2;
  display: flex; align-items: center; gap: 8px; padding: 9px 14px; border-radius: 12px;
  background: var(--surface); box-shadow: var(--shadow); font-size: 14px;
}
.status::before { content: ''; width: 8px; height: 8px; border-radius: 4px; background: var(--ontime); }
.status.error::before { background: var(--major); }

.following {
  position: absolute; top: 16px; left: 50%; transform: translateX(-50%); z-index: 2;
  display: flex; align-items: center; gap: 10px; height: 48px; padding: 0 6px 0 16px; border-radius: 24px;
  background: var(--surface); box-shadow: var(--shadow); color: var(--accent); white-space: nowrap;
}
.following[hidden] { display: none; }
.following span { color: var(--ink); }
.following strong { font-family: var(--mono); }
.following button { width: 38px; height: 38px; border-radius: 19px; border: 0; background: var(--surface-2); display: grid; place-items: center; color: var(--ink); }

/* Panel content */
.handle { display: none; }
.badges { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.badge { display: inline-flex; align-items: center; gap: 6px; padding: 3px 8px; border-radius: 6px; font-weight: 600; }
.badge.live { background: var(--live-bg); color: var(--live-ink); }
.badge.live::before { content: ''; width: 6px; height: 6px; border-radius: 3px; background: var(--ontime); }
.badge.planned { background: var(--surface-2); color: var(--muted); }
.badge.cancelled { background: var(--major-bg); color: var(--major-ink); }
.title { margin: 4px 0 2px; font: 600 28px/1.15 var(--mono); letter-spacing: -.02em; }
.route { font-size: 16px; font-weight: 500; }
.cards { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }
.card { padding: 12px 14px; border-radius: 14px; background: var(--surface-2); }
.card small { display: block; font-size: 13px; color: var(--muted); }
.card b { font: 600 26px/1.2 var(--mono); }
.card b span { font-size: 14px; color: var(--muted); }
.card.minor { background: var(--minor-bg); color: var(--minor-ink); }
.card.major { background: var(--major-bg); color: var(--major-ink); }
.card.minor small, .card.major small { color: inherit; opacity: .85; }
.next { display: flex; align-items: center; gap: 12px; padding: 14px 16px; border-radius: 14px; background: var(--surface-2); }
.next .grow { flex: 1; min-width: 0; }
.next small { display: block; font-size: 13px; color: var(--muted); }
.next strong { font-size: 16px; }
.countdown { text-align: right; font: 500 13px var(--mono); color: var(--muted); }
.countdown b { display: block; font-size: 17px; font-weight: 600; color: var(--ink); white-space: nowrap; }
.actions { display: flex; gap: 10px; }
.btn {
  flex: 1; height: 48px; border-radius: 12px; font-size: 15px; font-weight: 600;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px solid var(--line-strong); background: var(--surface);
}
.btn.primary { border: 0; background: var(--accent); color: var(--accent-ink); }
.btn.more { display: none; }

.stops h3 { margin: 0 0 10px; font-size: 13px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.stops ol { margin: 0; padding: 0; list-style: none; }
.stop { display: grid; grid-template-columns: 20px minmax(0, 1fr) auto; column-gap: 12px; }
.stop .rail { display: flex; flex-direction: column; align-items: center; }
.stop .rail i { width: 12px; height: 12px; margin-top: 4px; border-radius: 6px; border: 2px solid var(--ink); background: var(--surface); flex: none; }
.stop .rail u { flex: 1; width: 2px; min-height: 22px; background: var(--ink); }
.stop:last-child .rail u { display: none; }
.stop .name { padding-bottom: 16px; }
.stop .name small { display: block; font-size: 13px; color: var(--muted); }
.stop .time { text-align: right; font: 500 14px var(--mono); }
.stop .time s { display: block; font-size: 12px; color: var(--muted); }
.stop.passed .name, .stop.passed .time { color: var(--muted); }
.stop.passed .rail i { border-color: var(--planned); background: var(--planned); }
.stop.passed .rail u { background: var(--planned); }
.stop.is-next .name { font-weight: 600; }
.stop.is-next .rail i { width: 16px; height: 16px; margin-top: 2px; border: 0; background: var(--dotc, var(--ontime)); box-shadow: 0 0 0 4px color-mix(in srgb, var(--dotc, var(--ontime)) 25%, transparent); }
.stop.cancelled .name { text-decoration: line-through; }
.note { margin-top: auto; font-size: 12px; color: var(--muted); }
.close { position: absolute; top: 12px; right: 12px; width: 44px; height: 44px; border: 0; border-radius: 22px; background: transparent; display: grid; place-items: center; }
.close:hover { background: var(--surface-2); }

/* MapLibre controls in our colours */
.maplibregl-ctrl-group { background: var(--surface); border-radius: 12px; box-shadow: var(--shadow); overflow: hidden; }
.maplibregl-ctrl-group button { width: 44px; height: 44px; }
.maplibregl-ctrl-attrib { font-size: 11px; }

/* Phone: floating search, bottom sheet */
@media (max-width: 720px) {
  .bar {
    position: absolute; top: 0; left: 0; right: 0; padding: calc(env(safe-area-inset-top) + 12px) 16px 0;
    background: transparent; border: 0; pointer-events: none;
  }
  .bar > * { pointer-events: auto; }
  .brand, .search kbd { display: none; }
  .legend { display: none; }
  .theme-btn { width: 48px; height: 48px; border-radius: 24px; border: 0; background: var(--surface); box-shadow: var(--shadow); }
  .chips { flex-basis: 100%; }
  .search { flex: 1 1 0; min-width: 0; max-width: none; height: 48px; border-radius: 24px; background: var(--surface); border: 0; box-shadow: var(--shadow); }
  .chips { flex-wrap: nowrap; overflow-x: auto; }
  .chips button { height: 34px; padding: 0 12px; font-size: 13px; box-shadow: var(--shadow); border: 0; flex: none; }
  .following { top: calc(env(safe-area-inset-top) + 112px); }
  .panel {
    position: absolute; left: 0; right: 0; bottom: 0; z-index: 4; width: auto; max-height: 50vh;
    padding: 10px 20px calc(env(safe-area-inset-bottom) + 24px); gap: 16px;
    border: 0; border-radius: 28px 28px 0 0; box-shadow: 0 -4px 20px rgba(20, 26, 34, .16);
  }
  .panel.expanded { max-height: 88vh; }
  .handle { display: block; align-self: center; width: 40px; height: 5px; border: 0; padding: 0; border-radius: 3px; background: var(--line-strong); flex: none; }
  .close { display: none; }
  .title { font-size: 24px; }
  .btn.more { display: flex; }
  .panel:not(.expanded) .stops { display: none; }
  body.has-panel .status { display: none; }
}
