:root {
  --bg: #ffffff;
  --fg: #16201b;
  --muted: #64706a;
  --line: #e3e8e5;
  --accent: #016a42;
  --accent-fg: #ffffff;
  --live: #10a35a;
  --late: #c2410c;
  --shadow: 0 6px 24px rgba(0, 0, 0, .18);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171c1a;
    --fg: #e6ece9;
    --muted: #9aa8a1;
    --line: #2a322e;
    --accent: #2fae74;
    --accent-fg: #06140d;
    --live: #3ddc84;
    --late: #fb923c;
    --shadow: 0 6px 24px rgba(0, 0, 0, .5);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--fg);
  background: var(--bg);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

#map { position: fixed; inset: 0; }
.maplibregl-ctrl-top-right { top: calc(var(--safe-t) + 64px); right: calc(var(--safe-r) + 4px); }

/* ---------- top bar ---------- */
.top {
  position: fixed;
  top: calc(var(--safe-t) + 10px);
  left: calc(var(--safe-l) + 10px);
  right: calc(var(--safe-r) + 10px);
  display: flex;
  gap: 8px;
  z-index: 5;
}
.search { position: relative; flex: 1; min-width: 0; }
.search input {
  width: 100%;
  height: 44px;
  padding: 0 14px;
  border: 0;
  border-radius: 12px;
  background: var(--bg);
  color: var(--fg);
  box-shadow: var(--shadow);
  font-size: 16px;
}
.results {
  position: absolute;
  top: 50px; left: 0; right: 0;
  background: var(--bg);
  border-radius: 12px;
  box-shadow: var(--shadow);
  overflow: hidden;
  max-height: 50vh;
  overflow-y: auto;
}
.results button {
  display: flex; justify-content: space-between; gap: 8px;
  width: 100%; padding: 12px 14px;
  border: 0; border-bottom: 1px solid var(--line);
  background: none; text-align: left;
}
.chip {
  flex: none;
  height: 44px;
  padding: 0 14px;
  border: 0;
  border-radius: 12px;
  background: var(--bg);
  box-shadow: var(--shadow);
  font-weight: 600;
  max-width: 40vw;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.chip.on { background: var(--accent); color: var(--accent-fg); }

/* ---------- bottom sheet ---------- */
.sheet {
  position: fixed;
  left: 0; right: 0; bottom: 0;
  z-index: 4;
  background: var(--bg);
  border-radius: 18px 18px 0 0;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  max-height: 55vh;
  padding-bottom: var(--safe-b);
  transition: max-height .2s ease;
}
.sheet.collapsed { max-height: calc(78px + var(--safe-b)); }
.sheet.collapsed .sheet-body { overflow: hidden; }
.handle {
  flex: none;
  height: 22px;
  border: 0;
  background: none;
  position: relative;
}
.handle::after {
  content: "";
  position: absolute; left: 50%; top: 8px;
  width: 40px; height: 5px; margin-left: -20px;
  border-radius: 3px; background: var(--line);
}
.sheet-body {
  overflow-y: auto;
  padding: 0 max(14px, var(--safe-r)) 10px max(14px, var(--safe-l));
  -webkit-overflow-scrolling: touch;
}
.fabs {
  position: absolute;
  right: calc(var(--safe-r) + 12px);
  top: -60px;
  display: flex;
  gap: 8px;
}
.fab {
  height: 48px; min-width: 48px; padding: 0 12px;
  border: 0; border-radius: 24px;
  background: var(--bg); color: var(--fg);
  box-shadow: var(--shadow);
  font-weight: 700;
  display: grid; place-items: center;
}

@media (min-width: 820px) {
  .sheet {
    left: calc(var(--safe-l) + 12px); right: auto; bottom: 12px;
    width: 400px; border-radius: 18px; max-height: calc(100vh - 100px);
  }
  .fabs { right: auto; left: 412px; top: auto; bottom: 0; flex-direction: column; }
}

/* ---------- content ---------- */
.tabs { display: flex; gap: 6px; padding: 2px 0 10px; }
.tabs button {
  flex: 1; height: 38px; border: 0; border-radius: 10px;
  background: var(--line); font-weight: 600;
}
.tabs button.on { background: var(--accent); color: var(--accent-fg); }

.head { display: flex; align-items: center; gap: 10px; padding: 2px 0 10px; }
.head h2 { font-size: 18px; margin: 0; flex: 1; min-width: 0; line-height: 1.25; }
.head .sub { display: block; font-size: 13px; font-weight: 400; color: var(--muted); }
.icon-btn {
  flex: none; width: 40px; height: 40px; border: 0; border-radius: 10px;
  background: var(--line); font-size: 20px; display: grid; place-items: center;
}
.icon-btn.star.on { color: #e0a800; }

.list { display: flex; flex-direction: column; }
.row {
  display: flex; align-items: center; gap: 10px;
  width: 100%; padding: 11px 2px;
  border: 0; border-bottom: 1px solid var(--line);
  background: none; text-align: left;
}
.row:last-child { border-bottom: 0; }
.row .main { flex: 1; min-width: 0; }
.row .name { font-weight: 600; }
.row .code, .muted { color: var(--muted); font-size: 13px; }
.row .headsign { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row .right { flex: none; text-align: right; font-variant-numeric: tabular-nums; }
.row .when { font-weight: 700; font-size: 16px; }
.row .when.now { color: var(--live); }
.row .delay { font-size: 12px; color: var(--late); }
.row.passed { opacity: .45; }
.row.next { background: color-mix(in srgb, var(--accent) 10%, transparent); border-radius: 8px; }
.next-line { display: flex; flex-wrap: wrap; gap: 6px 10px; margin-top: 4px; font-size: 13px; font-variant-numeric: tabular-nums; }
.next-line span.item { display: inline-flex; align-items: center; gap: 4px; }

.badge {
  display: inline-block; min-width: 30px; padding: 2px 6px;
  border-radius: 6px; text-align: center; font-weight: 800; font-size: 13px;
  flex: none;
}
.live {
  display: inline-block; width: 7px; height: 7px; border-radius: 50%;
  background: var(--live); margin-right: 4px; vertical-align: middle;
  animation: pulse 2s infinite;
}
@keyframes pulse { 50% { opacity: .3; } }
@media (prefers-reduced-motion: reduce) { .live { animation: none; } .sheet { transition: none; } }

.empty { padding: 18px 4px; color: var(--muted); text-align: center; }
.empty .btn { margin-top: 10px; }
.btn {
  height: 42px; padding: 0 16px; border: 0; border-radius: 10px;
  background: var(--line); font-weight: 600;
}
.btn.primary { background: var(--accent); color: var(--accent-fg); }

/* ---------- dialog / toast ---------- */
.dlg {
  position: fixed; inset: 0; z-index: 20;
  background: rgba(0, 0, 0, .4);
  display: grid; place-items: center;
  padding: 16px;
}
.dlg-box {
  width: 100%; max-width: 380px;
  background: var(--bg); border-radius: 16px; padding: 18px;
  box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 10px;
}
.dlg-box h2 { margin: 0; font-size: 18px; }
.dlg-box p { margin: 0; }
.dlg-box input {
  height: 44px; padding: 0 12px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--bg); color: var(--fg); font-size: 16px;
}
.dlg-actions { display: flex; gap: 8px; justify-content: flex-end; }

.toast {
  position: fixed; left: 50%; top: calc(var(--safe-t) + 66px); transform: translateX(-50%);
  z-index: 30; background: var(--fg); color: var(--bg);
  padding: 10px 16px; border-radius: 10px; font-size: 14px; box-shadow: var(--shadow);
}
