:root {
  --bg: #eef2f7;
  --surface: #ffffff;
  --surface-2: #f6f8fb;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --accent: #2563eb;
  --green: #16a34a;
  --yellow: #eab308;
  --red: #dc2626;
  --radius: 14px;
  --shadow: 0 6px 24px rgba(15, 23, 42, 0.14);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b1220;
    --surface: #111a2e;
    --surface-2: #17223a;
    --text: #e5ebf5;
    --muted: #93a1b8;
    --border: #22304d;
    --accent: #60a5fa;
    --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0b1220;
  --surface: #111a2e;
  --surface-2: #17223a;
  --text: #e5ebf5;
  --muted: #93a1b8;
  --border: #22304d;
  --accent: #60a5fa;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}
body[data-view="trip"] { overflow: hidden; overscroll-behavior: none; }

:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 60%, transparent); outline-offset: 2px; }
svg { flex: none; }

.view { max-width: 720px; margin: 0 auto; min-height: 100dvh; padding-bottom: calc(24px + env(safe-area-inset-bottom)); }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 6px;
  padding: calc(8px + env(safe-area-inset-top)) 8px 8px 12px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}
.topbar > a.icon-btn { margin-left: -6px; }
.topbar-title { flex: 1; min-width: 0; font-weight: 700; font-size: 17px; }
.topbar-sub { font-size: 13px; color: var(--muted); font-weight: 400; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.app-title { font-size: 21px; margin: 0; letter-spacing: -0.01em; }
.title-route { display: flex; gap: 10px; align-items: center; min-width: 0; }
.title-route > span:last-child { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 15px; }

.icon-btn {
  width: 44px; height: 44px; flex: none;
  display: grid; place-items: center;
  border: 0; border-radius: 12px; background: transparent; color: inherit; cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); }

/* ---------- search ---------- */
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin: 12px 16px 0; padding: 4px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 14px; }
.tabs button {
  display: flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 42px; border: 0; border-radius: 10px; background: transparent;
  color: var(--muted); font: inherit; font-weight: 700; cursor: pointer;
}
.tabs button svg { width: 20px; height: 20px; }
.tabs button[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: 0 1px 4px rgba(15, 23, 42, 0.12); }

.search-box {
  margin: 12px 16px; display: flex; align-items: center; gap: 8px;
  padding: 0 12px; background: var(--surface); color: var(--muted);
  border: 1px solid var(--border); border-radius: 12px;
}
.search-box:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent); }
.search-box input { flex: 1; min-width: 0; border: 0; outline: none; background: transparent; color: var(--text); font: inherit; font-size: 17px; padding: 12px 0; }
.stop-tools { padding: 0 16px 4px; }

.section { font-size: 12px; text-transform: uppercase; letter-spacing: 0.07em; color: var(--muted); margin: 20px 16px 8px; font-weight: 700; }

/* ---------- list rows ---------- */
.row {
  display: flex; align-items: center; gap: 12px;
  width: 100%; min-height: 60px; padding: 10px 16px;
  background: var(--surface); color: inherit; text-decoration: none; text-align: left;
  border: 0; border-bottom: 1px solid var(--border); font: inherit; cursor: pointer;
}
.row:hover { background: var(--surface-2); }
.row:active { background: color-mix(in srgb, var(--accent) 8%, var(--surface)); }
.section + .row { border-top: 1px solid var(--border); }
.row-main { flex: 1; min-width: 0; }
.row-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-sub { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.row-icon { color: var(--muted); display: grid; place-items: center; width: 32px; }
.row-badges { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; align-items: center; font-size: 12px; }

.badge {
  display: inline-grid; place-items: center; flex: none;
  min-width: 50px; height: 32px; padding: 0 8px; border-radius: 8px;
  background: var(--bc, #3b82f6); color: var(--bt, #fff);
  font-weight: 800; font-size: 16px; font-variant-numeric: tabular-nums; letter-spacing: -0.01em;
}
.badge-sm { min-width: 30px; height: 22px; padding: 0 5px; border-radius: 6px; font-size: 12px; }

.trip-time { font-size: 20px; font-weight: 750; font-variant-numeric: tabular-nums; min-width: 60px; }
.trip-row.past { opacity: 0.5; }
.pill { font-size: 12px; font-weight: 700; padding: 3px 9px; border-radius: 999px; white-space: nowrap; }
.pill-live { background: color-mix(in srgb, var(--green) 16%, var(--surface)); color: var(--green); }
.rel { font-weight: 700; color: var(--accent); font-variant-numeric: tabular-nums; white-space: nowrap; }

.chips { display: flex; gap: 8px; padding: 12px 16px 4px; overflow-x: auto; scrollbar-width: none; }
.chip {
  flex: none; padding: 8px 14px; border-radius: 999px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface); color: inherit; font: inherit; font-weight: 600; white-space: nowrap;
}
.chip[aria-pressed="true"] { background: var(--text); color: var(--bg); border-color: var(--text); }

.suggest { margin: 12px 16px 0; border: 2px solid var(--accent); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.suggest-label { display: flex; align-items: center; gap: 6px; padding: 7px 12px; background: var(--accent); color: #fff; font-size: 13px; font-weight: 700; }
.suggest-label svg { width: 16px; height: 16px; }
.suggest .row { border-bottom: 0; }

.empty, .loading { padding: 40px 24px; text-align: center; color: var(--muted); }
.muted { color: var(--muted); font-weight: 400; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 10px 18px; border: 0; border-radius: 12px;
  background: var(--accent); color: #fff; font: inherit; font-weight: 700; text-decoration: none; cursor: pointer;
}
.btn:disabled { opacity: 0.6; }
.btn svg { width: 20px; height: 20px; }
.btn-secondary { width: 100%; background: var(--surface); color: var(--text); border: 1px solid var(--border); }
.btn-ghost { background: transparent; color: var(--text); padding: 6px 10px; min-height: 40px; font-weight: 600; }

/* ---------- tracker ---------- */
.tracker-view { position: fixed; inset: 0; max-width: none; padding: 0; overflow: hidden; background: var(--bg); }
.tracker-view .map { position: absolute; inset: 0; }
.tracker-view .map .empty { position: absolute; top: 35%; left: 0; right: 0; }

.glass {
  background: color-mix(in srgb, var(--surface) 90%, transparent);
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
  box-shadow: var(--shadow); border-radius: 14px; color: var(--text);
}
.glass.icon-btn:hover { background: var(--surface); }

.tracker-top {
  position: absolute; z-index: 5; left: 0; right: 0; top: 0;
  display: flex; gap: 8px; align-items: center;
  padding: calc(10px + env(safe-area-inset-top)) 12px 0;
  pointer-events: none;
}
.tracker-top > * { pointer-events: auto; }
.tracker-title { flex: 1; min-width: 0; display: flex; align-items: center; gap: 10px; padding: 6px 12px 6px 6px; min-height: 44px; }
.tracker-title-text { min-width: 0; }
.headsign { font-weight: 750; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tracker-sub { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.map-buttons { position: absolute; z-index: 5; right: 12px; top: calc(68px + env(safe-area-inset-top)); display: flex; flex-direction: column; gap: 8px; }
.tracker-view .maplibregl-ctrl-bottom-right { top: calc(66px + env(safe-area-inset-top)); bottom: auto; right: auto; left: 4px; }

.card {
  position: absolute; z-index: 6; left: 0; right: 0; bottom: 0;
  max-width: 560px; margin: 0 auto; max-height: 78dvh;
  display: flex; flex-direction: column;
  padding: 12px 14px calc(8px + env(safe-area-inset-bottom));
  background: var(--surface); border-radius: 22px 22px 0 0;
  box-shadow: 0 -8px 30px rgba(2, 6, 23, 0.2);
}
.card-main { display: grid; grid-template-columns: minmax(150px, auto) 1fr; gap: 14px; }

.delay-box {
  display: flex; flex-direction: column; justify-content: center; text-align: center;
  padding: 14px 14px 12px; border-radius: 18px; transition: background-color 0.3s, color 0.3s;
}
.delay-value { font-size: clamp(46px, 14vw, 66px); font-weight: 800; line-height: 1; font-variant-numeric: tabular-nums; letter-spacing: -0.03em; }
.delay-label { margin-top: 6px; font-size: 13px; font-weight: 750; text-transform: uppercase; letter-spacing: 0.05em; }
.state-green { background: var(--green); color: #fff; }
.state-yellow { background: var(--yellow); color: #1c1917; }
.state-red { background: var(--red); color: #fff; }
.state-none { background: var(--surface-2); color: var(--muted); box-shadow: inset 0 0 0 1px var(--border); }
.state-none .delay-value { color: var(--text); }

.metrics { display: flex; flex-direction: column; justify-content: center; gap: 10px; min-width: 0; }
.metric-k { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; font-weight: 700; }
.metric-v { font-size: 24px; font-weight: 750; font-variant-numeric: tabular-nums; line-height: 1.15; }
.metric-name { font-size: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.metric-s { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

.card-actions { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 10px; padding-top: 4px; border-top: 1px solid var(--border); }
.card-actions [aria-expanded] svg { transition: transform 0.2s; width: 20px; height: 20px; }
.card-actions [aria-expanded="true"] svg { transform: rotate(180deg); }
.method-tag { font-size: 12px; color: var(--muted); text-align: center; flex: 1; }

.banner { margin: 0 0 10px; padding: 8px 12px; border-radius: 10px; font-size: 14px; font-weight: 600; }
.banner-warn { background: color-mix(in srgb, var(--yellow) 24%, var(--surface)); color: var(--text); }
.banner-info { background: color-mix(in srgb, var(--accent) 14%, var(--surface)); color: var(--accent); }
.banner-action { cursor: pointer; }
.banner-action::after { content: ' ↻'; }

.stoplist { list-style: none; margin: 4px 0 0; padding: 0; overflow-y: auto; min-height: 0; flex: 1; overscroll-behavior: contain; }
.stop-row {
  display: grid; grid-template-columns: 18px minmax(0, 1fr) auto 104px; gap: 10px; align-items: center;
  padding: 9px 2px; border-bottom: 1px solid var(--border); font-size: 15px; cursor: pointer;
}
.stop-name { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stop-dot { justify-self: center; width: 12px; height: 12px; border-radius: 50%; border: 2.5px solid var(--accent); background: var(--surface); }
.stop-plan, .stop-real { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
.stop-real { color: var(--muted); }
.stop-row.passed { color: var(--muted); }
.stop-row.passed .stop-dot { background: var(--muted); border-color: var(--muted); }
.stop-row.next { font-weight: 750; }
.stop-row.next .stop-dot { background: var(--accent); width: 15px; height: 15px; }
.delta { font-size: 11px; font-weight: 750; padding: 1px 5px; border-radius: 5px; }

.user-marker { width: 48px; height: 48px; filter: drop-shadow(0 2px 3px rgba(0, 0, 0, 0.35)); }
.stop-popup { font: 600 14px/1.3 system-ui, sans-serif; color: #0f172a; }

@media (max-width: 359px) {
  .card-main { grid-template-columns: 1fr; }
}

/* ---------- settings sheet ---------- */
.sheet-backdrop { position: fixed; inset: 0; z-index: 100; display: flex; align-items: flex-end; justify-content: center; background: rgba(2, 6, 23, 0.5); }
.sheet {
  width: 100%; max-width: 560px; max-height: 90dvh; overflow-y: auto;
  padding: 6px 18px calc(18px + env(safe-area-inset-bottom));
  background: var(--surface); color: var(--text); border-radius: 22px 22px 0 0;
}
@media (min-width: 700px) {
  .sheet-backdrop { align-items: center; }
  .sheet { border-radius: 22px; }
}
.sheet-head { position: sticky; top: 0; z-index: 1; display: flex; align-items: center; justify-content: space-between; padding: 6px 0; background: var(--surface); }
.sheet-head h2 { margin: 0; font-size: 21px; }
.sheet fieldset { border: 0; padding: 0; margin: 14px 0; }
.sheet legend { padding: 0; margin-bottom: 8px; font-size: 12px; text-transform: uppercase; letter-spacing: 0.07em; color: var(--muted); font-weight: 700; }
.radio-row { display: flex; align-items: flex-start; gap: 12px; padding: 11px 12px; margin-bottom: 8px; border: 1px solid var(--border); border-radius: 12px; cursor: pointer; }
.radio-row:has(input:checked) { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 8%, var(--surface)); }
.radio-row input { width: 20px; height: 20px; margin: 1px 0 0; accent-color: var(--accent); flex: none; }
.radio-label { display: block; font-weight: 650; }
.radio-help { display: block; margin-top: 2px; font-size: 13px; color: var(--muted); }
.switch-row, .select-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; cursor: pointer; }
.switch-row input { width: 22px; height: 22px; accent-color: var(--accent); flex: none; }
.sheet select { font: inherit; padding: 8px 10px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface-2); color: inherit; }
.sheet details { border-top: 1px solid var(--border); margin-top: 8px; }
.sheet summary { padding: 12px 0; cursor: pointer; font-weight: 650; color: var(--muted); }
.data-info { font-size: 13px; margin: 16px 0 0; }
