:root {
    --emerald: #059669;
    --emerald-bright: #10b981;
    --teal: #0d9488;
    --teal-bright: #14b8a6;
    --on-time: #10b981;
    --minor: #f59e0b;
    --major: #ef4444;
    --slate-50: #f8fafc;
    --slate-200: #e2e8f0;
    --slate-300: #cbd5e1;
    --slate-400: #94a3b8;
    --slate-500: #64748b;
    --slate-600: #475569;
    --slate-700: #334155;
    --slate-800: #1e293b;
    --slate-900: #0f172a;
    --slate-950: #020617;
    --radius: 22px;
    --nav-w: 228px;
    --tab-h: calc(64px + env(safe-area-inset-bottom));
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

html[data-theme="dark"] {
    --bg: var(--slate-900);
    --card: var(--slate-800);
    --field: rgba(30, 41, 59, 0.92);
    --border: var(--slate-700);
    --ink: #ffffff;
    --muted: var(--slate-400);
    --brand: var(--emerald-bright);
    --nav: rgba(2, 6, 23, 0.96);
    --shadow: 0 10px 28px rgba(0, 0, 0, 0.28);
    --cross: #ffffff;
    color-scheme: dark;
}

html[data-theme="light"] {
    --bg: var(--slate-50);
    --card: #ffffff;
    --field: #f8fafc;
    --border: var(--slate-200);
    --ink: var(--slate-900);
    --muted: var(--slate-500);
    --brand: var(--emerald);
    --nav: rgba(255, 255, 255, 0.96);
    --shadow: 0 10px 28px rgba(15, 23, 42, 0.08);
    --cross: var(--emerald);
    color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
    font-family: var(--font);
    background: var(--bg);
    color: var(--ink);
    -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
button:disabled { cursor: default; }
img, svg { display: block; }
a { color: var(--brand); }

.shell {
    min-height: 100vh;
    min-height: 100dvh;
}

.stage {
    width: min(860px, 100%);
    margin: 0 auto;
    padding: calc(12px + env(safe-area-inset-top)) 16px calc(var(--tab-h) + 18px);
}
.stage.map-stage {
    width: 100%;
    max-width: none;
    padding: 0 0 var(--tab-h);
    min-height: 100dvh;
}

.tabs {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 30;
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 2px;
    background: var(--nav);
    border-top: 1px solid var(--border);
    padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
    backdrop-filter: blur(16px);
}
.tabs button {
    min-height: 52px;
    border-radius: 14px;
    color: var(--muted);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    font-size: 11px;
    font-weight: 650;
}
.tabs button.active { color: var(--brand); background: color-mix(in srgb, var(--brand) 12%, transparent); }
.tabs svg { width: 22px; height: 22px; }
.side-brand { display: none; }

.top {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    margin-bottom: 16px;
}
.top h1 {
    margin: 2px 0 0;
    font-size: clamp(28px, 4vw, 34px);
    letter-spacing: -0.03em;
}
.grow { flex: 1; min-width: 0; }
.clock {
    font-variant-numeric: tabular-nums;
    font-weight: 650;
    color: var(--muted);
    padding-top: 8px;
}
.brand {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    color: var(--brand);
    font-size: 12px;
    font-weight: 800;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}
.cross { width: 10px; height: 10px; color: var(--cross); }

.card {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: var(--radius);
    box-shadow: var(--shadow);
    padding: 14px;
}
.card + .card, .block + .block { margin-top: 14px; }
.section-label {
    margin: 18px 2px 8px;
    color: var(--muted);
    font-size: 12px;
    font-weight: 800;
    letter-spacing: 0.08em;
    text-transform: uppercase;
}

.field {
    width: 100%;
    display: flex;
    align-items: center;
    gap: 12px;
    text-align: left;
    background: var(--field);
    border: 1px solid color-mix(in srgb, var(--border) 80%, transparent);
    border-radius: 16px;
    padding: 10px 12px;
    min-height: 58px;
}
.field + .field { margin-top: 8px; }
.dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    box-shadow: 0 0 0 4px color-mix(in srgb, currentColor 18%, transparent);
    flex: none;
}
.field small { display: block; color: var(--muted); font-size: 11px; font-weight: 600; }
.field b { display: block; font-size: 15px; font-weight: 650; }
.field b.dim { color: var(--muted); font-weight: 600; }
.field .x, .icon-btn {
    width: 36px;
    height: 36px;
    border-radius: 12px;
    color: var(--muted);
    display: grid;
    place-items: center;
    flex: none;
}
.icon-btn {
    background: var(--field);
    border: 1px solid var(--border);
    color: var(--brand);
}
.icon-btn.quiet { color: var(--muted); }
.swap-wrap { position: relative; padding-right: 44px; }
.swap {
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: var(--slate-600);
    color: white;
    display: grid;
    place-items: center;
    z-index: 2;
    border: 1px solid rgba(255, 255, 255, 0.18);
}
.swap:disabled { opacity: 0.4; }
.swap svg, .icon-btn svg, .field .x svg { width: 16px; height: 16px; }

.row-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 12px;
    flex-wrap: wrap;
}
.chip, .pill {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--field);
    color: var(--muted);
    font-size: 13px;
    font-weight: 650;
    padding: 8px 12px;
    min-height: 40px;
}
.chip svg { width: 15px; height: 15px; color: var(--brand); }
.pill.on, .chip.on {
    background: var(--emerald);
    color: white;
    border-color: transparent;
}
.filters { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 2px; }
.filters::-webkit-scrollbar { display: none; }

.primary {
    width: 100%;
    margin-top: 12px;
    min-height: 50px;
    border-radius: 16px;
    font-weight: 800;
    color: var(--slate-900);
    background: linear-gradient(90deg, var(--emerald-bright), var(--teal-bright));
    box-shadow: 0 8px 18px rgba(16, 185, 129, 0.28);
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
}
.primary:disabled { background: var(--slate-600); color: var(--slate-300); box-shadow: none; }
.primary svg { width: 18px; height: 18px; }
.primary.narrow { width: auto; padding: 0 18px; }

.history {
    display: grid;
    grid-template-columns: 1fr;
    gap: 8px;
}
.history button.tile, .nearby-row, .result, .dep, .companion {
    width: 100%;
    text-align: left;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 10px 12px;
}
.tile {
    display: flex;
    gap: 10px;
    align-items: center;
}
.tile i, .mark {
    width: 34px;
    height: 34px;
    border-radius: 10px;
    display: grid;
    place-items: center;
    background: color-mix(in srgb, var(--brand) 16%, transparent);
    color: var(--brand);
    flex: none;
}
.tile b, .tile span { display: block; font-size: 13px; font-weight: 650; line-height: 1.25; }
.tile span { color: var(--muted); font-weight: 600; }
.nearby-head, .spread {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
}
.live-dot {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: var(--on-time);
    box-shadow: 0 0 0 4px rgba(16, 185, 129, 0.16);
}
.nearby-row, .dep {
    display: grid;
    grid-template-columns: auto 1fr auto;
    gap: 10px;
    align-items: center;
    margin-top: 8px;
    background: var(--field);
}
.nearby-row b, .dep b, .result b { font-weight: 700; }
.nearby-row > span:nth-child(2), .dep .nearby-row > span:nth-child(2) { min-width: 0; }
.nearby-row b { display: block; }
.nearby-row .sub { display: flex; flex-wrap: wrap; gap: 4px 8px; }
.sub { color: var(--muted); font-size: 12px; font-weight: 600; }
.right { text-align: right; }
.mono { font-variant-numeric: tabular-nums; }

.badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 42px;
    max-width: 92px;
    padding: 4px 7px;
    border-radius: 6px;
    font-size: 12px;
    font-weight: 800;
    letter-spacing: -0.02em;
    line-height: 1.15;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.delay {
    display: inline-flex;
    border-radius: 999px;
    padding: 2px 7px;
    font-size: 12px;
    font-weight: 800;
    background: color-mix(in srgb, var(--tone, var(--on-time)) 16%, transparent);
    color: var(--tone, var(--on-time));
}
.delay.minor { --tone: var(--minor); }
.delay.major { --tone: var(--major); }
.platform s { color: var(--major); margin-right: 4px; }
.cancelled { text-decoration: line-through; opacity: 0.7; }

.result { display: block; margin-bottom: 10px; }
    .result .times {
        display: grid;
        grid-template-columns: 1fr auto 1fr;
        align-items: center;
        gap: 8px;
    }
    .result .times strong { font-size: 26px; letter-spacing: -0.03em; }
    .result .times > span:last-child { text-align: right; }
.lines { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; align-items: center; }
.page-btn {
    width: 100%;
    margin: 4px 0 12px;
    min-height: 46px;
    border-radius: 14px;
    border: 1px solid var(--border);
    background: var(--field);
    font-weight: 700;
}

.back {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--field);
    border: 1px solid var(--border);
    color: var(--muted);
    display: grid;
    place-items: center;
    flex: none;
}
.head-line {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}
.head-line h2 {
    margin: 0;
    font-size: 15px;
    display: flex;
    gap: 6px;
    align-items: center;
    min-width: 0;
}
.head-line h2 span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.arrow { color: var(--brand); flex: none; }

.leg {
    display: grid;
    grid-template-columns: 18px 1fr;
    gap: 10px;
    margin-top: 4px;
}
.rail { position: relative; }
.rail::before {
    content: "";
    position: absolute;
    left: 7px;
    top: 8px;
    bottom: 8px;
    width: 3px;
    border-radius: 3px;
    background: var(--brand);
}
.rail.walk::before {
    background: repeating-linear-gradient(to bottom, #e879f9 0 6px, transparent 6px 10px);
}
.rail i {
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background: var(--card);
    border: 3px solid var(--brand);
    display: block;
    position: relative;
    z-index: 1;
    margin: 6px auto;
}
.stop-line { display: flex; gap: 8px; align-items: baseline; flex-wrap: wrap; }
.stop-line .name { font-weight: 750; }
.mid {
    margin: 8px 0 8px 28px;
    border-left: 2px solid var(--border);
    padding-left: 12px;
}
.mid div { display: flex; gap: 8px; padding: 4px 0; font-size: 13px; color: var(--muted); }
.walk-note {
    margin: 6px 0 6px 28px;
    color: var(--muted);
    font-size: 13px;
    font-weight: 650;
}
.occ { display: flex; gap: 10px; font-size: 12px; color: var(--muted); font-weight: 700; }
.occ i {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    display: inline-block;
    background: color-mix(in srgb, var(--muted) 35%, transparent);
    margin-left: 2px;
}
.occ i.on.l1 { background: var(--on-time); }
.occ i.on.l2 { background: var(--minor); }
.occ i.on.l3 { background: var(--major); }

.formation { margin: 8px 0 8px 28px; overflow-x: auto; }
.formation-head, .spread-inline { display: flex; justify-content: space-between; gap: 8px; font-size: 12px; font-weight: 700; }
.coach-row, .sector-row { display: flex; gap: 2px; margin-top: 6px; }
.coach {
    width: 34px;
    min-height: 28px;
    border-radius: 4px;
    background: var(--slate-500);
    color: white;
    font-size: 10px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: none;
}
.coach.first { background: #facc15; color: #111; }
.coach.wr { background: #dc2626; }
.coach.wl { background: #7c3aed; }
.coach.d { background: #92400e; }
.sector { font-size: 10px; text-align: center; color: var(--muted); font-weight: 800; }

.alert {
    border: 1px solid color-mix(in srgb, var(--major) 45%, var(--border));
    background: color-mix(in srgb, var(--major) 10%, var(--card));
    border-radius: 14px;
    padding: 10px 12px;
    margin-bottom: 10px;
}
.alert button { text-align: left; font-weight: 700; color: var(--major); }

.map-wrap { height: calc(100dvh - var(--tab-h)); position: relative; }
#netmap, #tripmap { height: 100%; min-height: 280px; background: var(--slate-800); }
#tripmap { height: 320px; border-radius: 16px; overflow: hidden; margin-top: 12px; border: 1px solid var(--border); }
.map-tools {
    position: absolute;
    z-index: 500;
    left: 12px;
    right: 12px;
    top: calc(12px + env(safe-area-inset-top));
    display: flex;
    gap: 8px;
}
.map-tools input, .searchbox input, .sheet input, .sheet select {
    width: 100%;
    min-height: 46px;
    border-radius: 14px;
    border: 1px solid var(--border);
    background: var(--card);
    padding: 0 12px;
}
.map-card {
    position: absolute;
    z-index: 500;
    left: 12px;
    right: 12px;
    bottom: 12px;
}

.overlay {
    position: fixed;
    inset: 0;
    z-index: 40;
    background: rgba(2, 6, 23, 0.55);
    display: flex;
    align-items: flex-end;
    justify-content: center;
}
.sheet {
    width: min(560px, 100%);
    max-height: min(88dvh, 760px);
    overflow: auto;
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 24px 24px 0 0;
    padding: 14px 16px calc(16px + env(safe-area-inset-bottom));
    box-shadow: var(--shadow);
}
.sheet h2 { margin: 4px 0 12px; font-size: 20px; }
.hit {
    width: 100%;
    text-align: left;
    padding: 12px 4px;
    border-bottom: 1px solid var(--border);
    font-weight: 650;
}
.hit small { display: block; color: var(--muted); font-weight: 600; }
.toggle {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding: 12px 0;
    border-bottom: 1px solid var(--border);
    text-align: left;
    font-weight: 650;
}
.switch {
    width: 44px;
    height: 26px;
    border-radius: 99px;
    background: var(--slate-600);
    position: relative;
    flex: none;
}
.switch.on { background: var(--emerald); }
.switch i {
    position: absolute;
    top: 3px;
    left: 3px;
    width: 20px;
    height: 20px;
    border-radius: 50%;
    background: white;
    transition: transform 0.15s ease;
}
.switch.on i { transform: translateX(18px); }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.seg button, .choice {
    min-height: 40px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--field);
    font-weight: 700;
}
.seg button.on, .choice.on { background: var(--emerald); color: white; border-color: transparent; }
.stepper { display: flex; align-items: center; gap: 10px; }
.stepper button {
    width: 36px;
    height: 36px;
    border-radius: 10px;
    border: 1px solid var(--border);
    background: var(--field);
    font-size: 20px;
}

.empty {
    min-height: 46vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    text-align: center;
    gap: 8px;
    color: var(--muted);
    padding: 24px;
}
.empty h2 { color: var(--ink); margin: 0; }
.empty svg { width: 42px; height: 42px; color: var(--brand); }
.spinner {
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: 3px solid color-mix(in srgb, var(--brand) 25%, transparent);
    border-top-color: var(--brand);
    animation: spin 0.8s linear infinite;
    margin: 28px auto;
}
@keyframes spin { to { transform: rotate(360deg); } }
.toast {
    position: fixed;
    left: 50%;
    bottom: calc(var(--tab-h) + 12px);
    transform: translateX(-50%);
    z-index: 50;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 10px 16px;
    font-size: 13px;
    font-weight: 700;
    box-shadow: var(--shadow);
    max-width: min(420px, calc(100% - 32px));
}
.err { color: var(--major); font-weight: 650; margin: 8px 0; }
.tools { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.ghost {
    min-height: 40px;
    padding: 0 12px;
    border-radius: 12px;
    border: 1px solid var(--border);
    background: var(--field);
    font-weight: 700;
    color: var(--ink);
}
.star.on { color: var(--minor); }

.leaflet-container { font-family: var(--font); background: var(--slate-800); }
.leaflet-marker-icon.stop-pin {
    background: transparent;
    border: 0;
}
.stop-pin-inner {
    display: flex;
    align-items: center;
    gap: 5px;
    transform: translate(-7px, -10px);
    width: max-content;
}
.stop-pin-inner i {
    width: 14px;
    height: 14px;
    border-radius: 50%;
    background: #059669;
    border: 2px solid #fff;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
    flex: none;
}
.stop-pin.selected .stop-pin-inner i {
    background: #10b981;
    box-shadow: 0 0 0 4px rgba(16, 185, 129, 0.35);
}
.stop-pin-inner b {
    background: var(--card);
    color: var(--ink);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 2px 7px;
    font-size: 12px;
    font-weight: 750;
    line-height: 1.2;
    white-space: nowrap;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.16);
}

@media (min-width: 760px) {
    .history { grid-template-columns: 1fr 1fr; }
    .overlay { align-items: center; padding: 24px; }
    .sheet { border-radius: 24px; }
    .stage { padding-top: 28px; }
}

@media (min-width: 1080px) {
    .shell { display: grid; grid-template-columns: var(--nav-w) 1fr; }
    .tabs {
        position: sticky;
        top: 0;
        bottom: auto;
        height: 100vh;
        height: 100dvh;
        width: var(--nav-w);
        display: flex;
        flex-direction: column;
        align-items: stretch;
        gap: 4px;
        border-top: 0;
        border-right: 1px solid var(--border);
        padding: 22px 12px;
    }
    .side-brand { display: flex; margin: 4px 8px 18px; }
    .tabs button {
        flex-direction: row;
        justify-content: flex-start;
        gap: 10px;
        min-height: 46px;
        padding: 0 12px;
        font-size: 15px;
    }
    .stage, .stage.map-stage { padding-bottom: 28px; }
    .stage.map-stage { min-height: 100dvh; }
    .map-wrap { height: 100dvh; }
    .toast { bottom: 24px; }
}
