:root {
    --bg: #e8edf2;
    --panel: #f7f9fb;
    --panel-2: #ffffff;
    --ink: #14181f;
    --muted: #4b5563;
    --faint: #6b7280;
    --line: #d5dde6;
    --accent: #0b5cab;
    --accent-ink: #ffffff;
    --ok: #0f6b38;
    --warn: #9a3412;
    --bad: #b42318;
    --purple: #6a1b9a;
    --shadow: 0 12px 40px rgba(20, 24, 31, 0.14);
    --radius: 14px;
    --topbar: 64px;
    --sheet-peek: 148px;
    --sheet-mid: 48dvh;
    --sheet-full: calc(100dvh - var(--topbar) - 8px);
    --safe-b: env(safe-area-inset-bottom, 0px);
    --font: "Segoe UI", "Helvetica Neue", ui-sans-serif, system-ui, sans-serif;
    --mono: "ui-monospace", "SFMono-Regular", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg: #0c0f13;
        --panel: #141920;
        --panel-2: #1b222b;
        --ink: #eef2f6;
        --muted: #b4bcc6;
        --faint: #8b95a1;
        --line: #2a3340;
        --accent: #6cb0ff;
        --accent-ink: #071018;
        --ok: #4ade80;
        --warn: #fdba74;
        --bad: #fca5a5;
        --purple: #e9d5ff;
        --shadow: 0 16px 44px rgba(0, 0, 0, 0.45);
    }
}

* { box-sizing: border-box; }

html, body {
    margin: 0;
    height: 100%;
    background: var(--bg);
    color: var(--ink);
    font-family: var(--font);
    font-size: 16px;
    line-height: 1.45;
}

body {
    min-height: 100dvh;
    overflow: hidden;
}

.sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
}

.skip-link {
    position: absolute;
    left: 12px;
    top: -48px;
    z-index: 80;
    background: var(--accent);
    color: var(--accent-ink);
    padding: 8px 12px;
    border-radius: 8px;
    font-weight: 650;
}

.skip-link:focus {
    top: 12px;
}

:focus-visible {
    outline: 3px solid var(--accent);
    outline-offset: 2px;
}

button, select, input {
    font: inherit;
    color: inherit;
}

button {
    min-height: 44px;
    min-width: 44px;
}

.error-banner {
    position: sticky;
    top: 0;
    z-index: 60;
    background: #7f1d1d;
    color: #fff;
    padding: 10px 16px;
}

.topbar {
    position: relative;
    z-index: 30;
    display: grid;
    grid-template-columns: minmax(140px, auto) minmax(0, 1fr) auto;
    gap: 12px;
    align-items: center;
    min-height: var(--topbar);
    padding: 10px 16px;
    padding-top: max(10px, env(safe-area-inset-top));
    background: color-mix(in srgb, var(--panel) 88%, transparent);
    border-bottom: 1px solid var(--line);
    backdrop-filter: blur(16px);
}

.brand h1 {
    margin: 0;
    font-size: 1.05rem;
    letter-spacing: -0.03em;
}

.live {
    margin: 2px 0 0;
    color: var(--muted);
    font-size: 0.95rem;
}

.live-dot {
    display: inline-block;
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--faint);
    margin-right: 6px;
    vertical-align: middle;
}

.live-dot[data-state="ok"] { background: var(--ok); }
.live-dot[data-state="stale"] { background: #ca8a04; }
.live-dot[data-state="err"] { background: var(--bad); }

.omni { position: relative; }

#omni-input {
    width: 100%;
    min-height: 44px;
    border: 1px solid var(--line);
    border-radius: 999px;
    padding: 0 16px;
    background: var(--panel-2);
}

.omni-results {
    position: absolute;
    left: 0;
    right: 0;
    top: calc(100% + 6px);
    max-height: min(360px, 50dvh);
    overflow: auto;
    background: var(--panel-2);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: var(--shadow);
    z-index: 40;
}

.omni-item {
    display: flex;
    justify-content: space-between;
    gap: 12px;
    width: 100%;
    text-align: left;
    padding: 10px 12px;
    border: 0;
    background: transparent;
    cursor: pointer;
}

.omni-item[aria-selected="true"],
.omni-item:hover {
    background: color-mix(in srgb, var(--accent) 12%, var(--panel-2));
}

.omni-kind {
    color: var(--faint);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.ghost-btn, .back-btn, .map-btn {
    border: 1px solid var(--line);
    background: var(--panel-2);
    border-radius: 10px;
    padding: 0 12px;
    cursor: pointer;
}

#follow-btn.is-follow,
#follow-btn[aria-pressed="true"] {
    background: #0b5cab;
    border-color: #0b5cab;
    color: #fff;
}

.back-btn { font-weight: 650; }

.workspace {
    height: calc(100dvh - var(--topbar));
    max-height: calc(100dvh - var(--topbar));
    min-height: 0;
    overflow: hidden;
    display: grid;
    grid-template-columns: minmax(320px, 400px) 1fr;
    grid-template-rows: minmax(0, 1fr);
}

.sheet {
    display: flex;
    flex-direction: column;
    min-width: 0;
    min-height: 0;
    height: 100%;
    overflow: hidden;
    background: var(--panel);
    border-right: 1px solid var(--line);
}

.sheet-handle { display: none; }

.sheet-chrome {
    padding: 12px 14px 8px;
    border-bottom: 1px solid var(--line);
    background: var(--panel);
}

.nav-row {
    display: flex;
    flex-direction: column;
    gap: 10px;
}

.tabs {
    display: flex;
    gap: 6px;
}

.tabs [role="tab"] {
    flex: 1;
    border: 1px solid var(--line);
    background: var(--panel-2);
    border-radius: 999px;
    cursor: pointer;
}

.tabs [role="tab"][aria-selected="true"] {
    background: var(--ink);
    color: var(--bg);
    border-color: var(--ink);
}

.field {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.field-label {
    font-size: 0.85rem;
    color: var(--muted);
    font-weight: 650;
}

.field select {
    min-height: 44px;
    width: 100%;
    border: 1px solid var(--line);
    border-radius: 10px;
    padding: 0 10px;
    background: var(--panel-2);
}

.crumbs {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 10px;
    font-size: 0.95rem;
}

.crumb {
    border: 0;
    background: transparent;
    color: var(--accent);
    padding: 0;
    min-width: 0;
    min-height: 0;
    cursor: pointer;
    text-decoration: underline;
}

.crumb[aria-current="page"] {
    color: var(--ink);
    text-decoration: none;
    font-weight: 700;
    cursor: default;
}

.crumb-sep { color: var(--faint); }

.sheet-body {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.results-wrap {
    position: relative;
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.sheet-body.has-detail .results-wrap {
    display: none;
}

.sheet-body.has-detail .detail:not([hidden]) {
    flex: 1;
    min-height: 0;
}

.results {
    flex: 1;
    min-height: 0;
    overflow: auto;
    padding: 6px 28px 16px 0;
}

.line-index {
    position: absolute;
    top: 4px;
    right: 0;
    bottom: 8px;
    z-index: 4;
    width: 34px;
    padding: 0 0 0 8px;
    overflow: hidden;
    touch-action: none;
    user-select: none;
    -webkit-user-select: none;
}

.line-index[hidden] { display: none; }

.line-index-item {
    position: absolute;
    left: 8px;
    right: 0;
    top: calc(var(--t, 0) * (100% - 12px) + 6px);
    transform: translateY(-50%);
    display: flex;
    align-items: center;
    justify-content: center;
    border: 0;
    background: transparent;
    min-height: 0;
    min-width: 0;
    padding: 0;
    font-size: 0.65rem;
    font-weight: 750;
    line-height: 1;
    color: var(--accent);
    pointer-events: none;
}

.line-index-item.is-active {
    color: var(--ink);
    font-weight: 800;
}

.line-index-hud {
    position: absolute;
    left: 50%;
    top: 42%;
    transform: translate(-50%, -50%);
    z-index: 5;
    min-width: 72px;
    padding: 10px 16px;
    border-radius: 14px;
    background: color-mix(in srgb, var(--ink) 88%, transparent);
    color: var(--bg);
    font-size: 1.8rem;
    font-weight: 800;
    letter-spacing: -0.03em;
    text-align: center;
    pointer-events: none;
}

.line-index-hud[hidden] { display: none; }

.line-group { position: relative; }

.group-h {
    position: sticky;
    top: 0;
    z-index: 1;
    padding: 10px 28px 6px 16px;
    background: color-mix(in srgb, var(--panel) 92%, transparent);
    color: var(--muted);
    font-size: 0.85rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.item {
    display: block;
    width: 100%;
    text-align: left;
    border: 0;
    border-bottom: 1px solid var(--line);
    background: transparent;
    padding: 12px 16px;
    cursor: pointer;
}

.item:hover,
.item[aria-selected="true"] {
    background: color-mix(in srgb, var(--accent) 10%, var(--panel));
}

.item.is-detour {
    box-shadow: inset 4px 0 0 var(--bad);
}

.item-title {
    font-size: 1.05rem;
    font-weight: 700;
    letter-spacing: -0.02em;
}

.item-meta, .item-sub {
    color: var(--muted);
    font-size: 0.95rem;
}

.item-sub {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 4px;
}

.item-id { font-family: var(--mono); font-size: 0.9rem; }

.progress-bar {
    height: 10px;
    margin-top: 8px;
    border-radius: 99px;
    background: color-mix(in srgb, var(--ink) 12%, transparent);
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    background: var(--ok);
}

.progress-unknown,
.progress-disabled {
    height: 100%;
    width: 100%;
    background: color-mix(in srgb, var(--faint) 45%, transparent);
}

.deviation {
    margin-top: 6px;
    color: var(--bad);
    font-weight: 700;
    font-size: 0.95rem;
}

.empty {
    padding: 20px 16px;
    color: var(--muted);
}

.detail {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
    background: var(--panel-2);
    border-top: 1px solid var(--line);
}

.detail[hidden] { display: none; }

.detail-head {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 14px;
    border-bottom: 1px solid var(--line);
}

.detail-head-text {
    flex: 1;
    min-width: 0;
}

.detail-head h2 {
    margin: 0;
    font-size: 1.1rem;
}

.headsign {
    position: relative;
    height: 46px;
    border-radius: 8px;
    background:
        linear-gradient(180deg, rgba(255, 255, 255, 0.04), transparent 40%),
        #0b0d11;
    box-shadow:
        inset 0 0 0 1px #2c323c,
        inset 0 8px 16px rgba(0, 0, 0, 0.35);
    overflow: hidden;
}

.headsign-viewport {
    height: 46px;
    overflow: hidden;
}

.headsign-track {
    display: flex;
    flex-direction: column;
}

.headsign.is-flip.pages-2 .headsign-track {
    animation: headsign-flip-2 8s steps(1, end) infinite;
}

.headsign.is-flip.pages-3 .headsign-track {
    animation: headsign-flip-3 14s steps(1, end) infinite;
}

.headsign-page {
    box-sizing: border-box;
    height: 46px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 10px;
    padding: 0 12px;
    color: #ffc14d;
    font-family: var(--mono);
    font-weight: 800;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    text-shadow: 0 0 10px rgba(255, 176, 48, 0.45);
}

.headsign-route { font-size: 1.35rem; }
.headsign-dir {
    font-size: 0.78rem;
    letter-spacing: 0.14em;
    opacity: 0.82;
}

.headsign-dest {
    overflow: hidden;
}

.headsign-scroll {
    display: flex;
    gap: 48px;
    width: max-content;
    flex-shrink: 0;
    white-space: nowrap;
}

.headsign-copy { font-size: 1.05rem; }
.headsign-vehicle { font-size: 1.05rem; }

.headsign-dest.is-scroll {
    justify-content: flex-start;
}

.headsign-dest.is-scroll .headsign-scroll {
    animation: headsign-marquee 8s linear infinite;
}

@keyframes headsign-flip-2 {
    0%, 36% { transform: translateY(0); }
    40%, 96% { transform: translateY(-50%); }
    100% { transform: translateY(0); }
}

@keyframes headsign-flip-3 {
    0%, 12% { transform: translateY(0); }
    16%, 72% { transform: translateY(-33.333%); }
    76%, 96% { transform: translateY(-66.666%); }
    100% { transform: translateY(0); }
}

@keyframes headsign-marquee {
    0% { transform: translateX(12px); }
    100% { transform: translateX(calc(-50% - 24px)); }
}

.headsign::after {
    content: "";
    pointer-events: none;
    position: absolute;
    inset: 0;
    background: repeating-linear-gradient(
        to bottom,
        transparent 0 2px,
        rgba(0, 0, 0, 0.16) 2px 3px
    );
}

@media (prefers-reduced-motion: reduce) {
    .headsign,
    .headsign-viewport { height: auto; }
    .headsign-track {
        animation: none !important;
        transform: none !important;
    }
    .headsign.has-dest .headsign-page {
        height: auto;
        min-height: 28px;
        padding: 6px 12px;
        justify-content: flex-start;
    }
    .headsign-dest.is-scroll .headsign-scroll { animation: none !important; }
}

.detail-scroll {
    flex: 1;
    overflow: auto;
    padding: 14px;
}

.meta-block {
    display: grid;
    gap: 6px;
    margin-bottom: 14px;
    font-size: 1rem;
}

.meta-block strong { color: var(--muted); font-weight: 650; }

.eta-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 8px 12px;
    align-items: center;
    padding: 10px 12px;
    margin-bottom: 14px;
    border: 1px solid var(--line);
    border-radius: 10px;
    font-size: 0.95rem;
    color: var(--muted);
}

.eta-info {
    display: inline-flex;
    align-items: center;
    gap: 0.4em;
    padding: 5px 10px;
    border-radius: 8px;
    font-size: 0.95rem;
    line-height: 1.35;
    font-weight: 700;
    white-space: nowrap;
    background: #e5e7eb;
    color: #111827;
}

.eta-info.historical { background: #0d47a1; color: #fff; }
.eta-info.path-timing { background: #dbeafe; color: #0d47a1; }
.eta-info.range { background: #ffedd5; color: #9a3412; }
.eta-info.skipped { background: #b91c1c; color: #fff; }
.eta-info.turnaround { background: #6b21a8; color: #fff; }
.eta-info.fallback { background: #334155; color: #fff; }
.eta-info.arrived, .eta-info.arriving { background: #166534; color: #fff; }
.eta-info.passed { background: #475569; color: #fff; }
.eta-info.nearby { background: #c2410c; color: #fff; }
.eta-info.smoothed { box-shadow: inset 3px 0 0 #2563eb; }

.eta-flip {
    display: inline-block;
    height: 1.35em;
    overflow: hidden;
    vertical-align: middle;
}

.eta-flip-track {
    display: flex;
    flex-direction: column;
    animation: eta-flip 8s steps(1, end) infinite;
    animation-delay: calc(var(--eta-phase, 0) * 1s);
}

.eta-flip-page {
    height: 1.35em;
    line-height: 1.35em;
}

@keyframes eta-flip {
    0%, 58% { transform: translateY(0); }
    62%, 96% { transform: translateY(-50%); }
    100% { transform: translateY(0); }
}

.timeline-wrap {
    position: relative;
    margin: 8px 0;
}

.timeline-line,
.timeline-fill {
    position: absolute;
    left: 9px;
    top: 12px;
    width: 4px;
    border-radius: 99px;
}

.timeline-line {
    bottom: 12px;
    background: var(--line);
}

.timeline-fill { background: var(--ok); }

#stops-container { position: relative; z-index: 1; }
#stops-container.is-axis .stop-row { position: absolute; left: 0; right: 0; }

.stop-row {
    display: grid;
    grid-template-columns: 22px minmax(0, 1fr);
    gap: 10px;
    padding: 8px 8px 8px 0;
    border: 0;
    background: transparent;
    width: 100%;
    text-align: left;
    border-radius: 10px;
    cursor: pointer;
}

.stop-row:hover { background: color-mix(in srgb, var(--accent) 8%, transparent); }
.stop-row.current { background: color-mix(in srgb, var(--accent) 14%, transparent); }
.stop-row.passed .stop-title { color: var(--ok); }

.stop-marker {
    width: 12px;
    height: 12px;
    margin: 7px auto 0;
    border-radius: 50%;
    background: var(--panel-2);
    border: 2px solid #6b7280;
}

.stop-marker.active { background: var(--ok); border-color: var(--ok); box-shadow: 0 0 0 2px color-mix(in srgb, var(--ok) 35%, transparent); }
.stop-marker.passed { background: var(--ok); border-color: var(--ok); }
.stop-marker.skipped { background: #b91c1c; border-color: #b91c1c; }
.stop-marker.turnaround { background: #e9d5ff; border-color: #6b21a8; }

.stop-title { font-size: 1.05rem; font-weight: 650; }
.stop-id { color: var(--faint); font-family: var(--mono); font-size: 0.85rem; font-weight: 500; }
.stop-chips { display: flex; flex-direction: column; align-items: flex-start; gap: 6px; margin-top: 6px; }

.turnaround-divider {
    margin: 18px 0 10px 24px;
    padding: 8px 10px;
    background: #f3e8ff;
    color: #6b21a8;
    border-radius: 8px;
    font-size: 0.95rem;
    font-weight: 700;
}

@media (prefers-color-scheme: dark) {
    .turnaround-divider { background: #3b0764; color: #f3e8ff; }
}

.arrival-row {
    display: flex;
    flex-direction: column;
    gap: 8px;
    width: 100%;
    text-align: left;
    padding: 12px 0;
    border: 0;
    border-bottom: 1px solid var(--line);
    background: transparent;
    cursor: pointer;
    font-size: 1rem;
}

.arrival-row-head { display: flex; align-items: center; gap: 8px; font-size: 1.05rem; }
.arrival-row.passed { opacity: 0.7; }

.line-pill {
    display: inline-block;
    background: var(--accent);
    color: var(--accent-ink);
    border-radius: 8px;
    padding: 4px 8px;
    font-size: 0.95rem;
    font-weight: 700;
}

.map-stage {
    position: relative;
    min-width: 0;
    min-height: 0;
    height: 100%;
    overflow: hidden;
    isolation: isolate;
    transform: translateZ(0);
}
#map { position: absolute; inset: 0; }

.map-controls {
    position: absolute;
    right: 12px;
    top: 12px;
    display: flex;
    flex-direction: column;
    gap: 8px;
    z-index: 5;
}

.map-controls-bottom {
    top: auto;
    right: auto;
    left: 12px;
    bottom: 96px;
}

#locate-btn {
    width: 44px;
    min-height: 44px;
    padding: 0;
    display: grid;
    place-items: center;
}

#locate-btn[aria-busy="true"] {
    opacity: 0.55;
}

.map-btn[aria-pressed="true"] {
    background: var(--ink);
    color: var(--bg);
}

.inspect {
    position: absolute;
    right: 12px;
    bottom: 18px;
    width: min(360px, calc(100% - 24px));
    background: var(--panel-2);
    border: 1px solid var(--line);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    z-index: 6;
    overflow: hidden;
}

.inspect-body { padding: 12px 14px 4px; }
.inspect-actions {
    display: flex;
    gap: 8px;
    padding: 8px 12px 12px;
}

.inspect-actions #inspect-open {
    flex: 1;
    border: 0;
    border-radius: 10px;
    background: var(--ink);
    color: var(--bg);
    cursor: pointer;
}

.hover-card-title { font-weight: 800; margin-bottom: 8px; font-size: 1.1rem; }
.hover-card-row { display: flex; justify-content: space-between; gap: 12px; font-size: 1rem; }
.hover-card-row .k { color: var(--muted); }
.hover-card-row .v { font-weight: 700; text-align: right; }
.hover-card-warn { color: var(--bad); font-weight: 800; margin-top: 8px; }
.hover-card-etas { margin-top: 8px; padding-top: 8px; border-top: 1px solid var(--line); }

.hover-float {
    position: fixed;
    z-index: 50;
    pointer-events: none;
    max-width: 280px;
    background: var(--panel-2);
    border-radius: 12px;
    box-shadow: var(--shadow);
    padding: 10px 12px;
}

.help-dialog {
    border: 1px solid var(--line);
    border-radius: 16px;
    background: var(--panel-2);
    color: var(--ink);
    padding: 20px;
    width: min(420px, 92vw);
}

.help-dialog kbd {
    font-family: var(--mono);
    border: 1px solid var(--line);
    border-radius: 6px;
    padding: 1px 6px;
    background: var(--panel);
}

.maplibregl-ctrl-attrib { font-size: 10px; }

@media (max-width: 799px) {
    :root {
        --topbar: calc(56px + env(safe-area-inset-top, 0px));
        --sheet-full: calc(100dvh - var(--topbar) - 16px);
    }

    .topbar {
        grid-template-columns: auto minmax(0, 1fr);
        gap: 8px;
        min-height: 0;
        padding: 6px 12px;
        padding-top: max(6px, env(safe-area-inset-top));
        position: absolute;
        left: 0;
        right: 0;
        top: 0;
        background: color-mix(in srgb, var(--panel) 78%, transparent);
    }

    .brand h1 { display: none; }
    .live {
        margin: 0;
        font-size: 0.85rem;
        white-space: nowrap;
    }
    .top-actions { display: none; }
    .omni { grid-column: auto; }

    .workspace {
        display: block;
        height: 100dvh;
    }

    .map-stage {
        position: absolute;
        inset: 0;
    }

    .map-controls { top: calc(var(--topbar) + 12px); }

    .map-controls-bottom {
        top: auto;
        right: auto;
        left: 12px;
        bottom: calc(var(--sheet-mid) + 12px);
    }

    .workspace:has(.sheet[data-snap="peek"]) .map-controls-bottom {
        bottom: calc(var(--sheet-peek) + var(--safe-b) + 12px);
    }

    .workspace:has(.sheet[data-snap="full"]) .map-controls-bottom {
        top: calc(var(--topbar) + 108px);
        right: 12px;
        left: auto;
        bottom: auto;
    }

    .sheet {
        position: absolute;
        left: 0;
        right: 0;
        bottom: 0;
        z-index: 20;
        height: var(--sheet-h, var(--sheet-mid));
        max-height: var(--sheet-full);
        border: 0;
        border-radius: 18px 18px 0 0;
        box-shadow: var(--shadow);
        padding-bottom: var(--safe-b);
        contain: layout paint;
        transform: translateZ(0);
    }

    .sheet[data-snap="peek"] { --sheet-h: calc(var(--sheet-peek) + var(--safe-b)); }
    .sheet[data-snap="mid"] { --sheet-h: var(--sheet-mid); }
    .sheet[data-snap="full"] { --sheet-h: var(--sheet-full); }

    .sheet-handle {
        display: block;
        width: 56px;
        height: 28px;
        margin: 6px auto 0;
        border: 0;
        background: transparent;
        position: relative;
        z-index: 2;
    }

    .sheet-handle::before {
        content: "";
        display: block;
        width: 42px;
        height: 5px;
        margin: 8px auto 0;
        border-radius: 99px;
        background: #94a3b8;
    }

    .inspect { display: none; }

    body.is-map-panning .results-wrap {
        content-visibility: hidden;
        contain-intrinsic-size: auto 50vh;
        pointer-events: none;
    }
}

@media (prefers-reduced-motion: reduce) {
    *, *::before, *::after {
        scroll-behavior: auto !important;
        transition: none !important;
        animation: none !important;
    }
}
