:root {
    --bg: #f5f7fb;
    --text: #0f172a;
    --muted: #5f6b7c;
    --border: #d4dbe8;
    --card-bg: #ffffff;
    --panel-w: 380px;
    --topbar-h: 52px;
    --radius: 12px;
    --shadow: 0 4px 24px rgba(0,0,0,0.10);
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
    font-family: "Inter", "Segoe UI", system-ui, -apple-system, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    overflow: hidden;
    height: 100dvh;
}

/* ── Map ── */
#map {
    position: fixed;
    inset: 0;
    z-index: 0;
}

/* ── Top bar ── */
.topbar {
    position: fixed;
    top: 0; left: 0; right: 0;
    height: var(--topbar-h);
    background: #fff;
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0 1rem;
    z-index: 500;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
}

.topbar-logo {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    text-decoration: none;
    color: var(--text);
    font-weight: 700;
    font-size: 1rem;
    white-space: nowrap;
}

.topbar-logo-img { height: 28px; width: auto; }
.topbar-logo-text { font-size: 0.95rem; }

.line-tabs {
    display: flex;
    gap: 0.3rem;
    overflow-x: auto;
    flex: 1;
    scrollbar-width: none;
}
.line-tabs::-webkit-scrollbar { display: none; }

.line-tab {
    padding: 0.25rem 0.65rem;
    border-radius: 999px;
    border: 1.5px solid var(--lc, #888);
    background: transparent;
    color: var(--lc, #333);
    font-size: 0.8rem;
    font-weight: 700;
    cursor: pointer;
    white-space: nowrap;
    transition: background 0.15s, color 0.15s;
}
.line-tab:hover, .line-tab.active {
    background: var(--lc, #333);
    color: #fff;
}

.topbar-actions {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    white-space: nowrap;
}

.crowd-legend {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.78rem;
    color: var(--muted);
}
.legend-dot {
    display: inline-block;
    width: 10px; height: 10px;
    border-radius: 50%;
}
.legend-dot.low  { background: #22c55e; }
.legend-dot.mod  { background: #f59e0b; }
.legend-dot.high { background: #ef4444; }

.topbar-btn {
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.3rem 0.6rem;
    background: #fff;
    color: var(--text);
    font-size: 1rem;
    cursor: pointer;
    transition: background 0.15s;
}
.topbar-btn:hover { background: var(--bg); }

/* ── Panel ── */
.panel {
    position: fixed;
    top: var(--topbar-h);
    right: 0;
    width: var(--panel-w);
    height: calc(100dvh - var(--topbar-h));
    background: var(--card-bg);
    border-left: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    z-index: 400;
    box-shadow: -4px 0 20px rgba(0,0,0,0.07);
    transition: transform 0.3s ease;
}
.panel.hidden {
    transform: translateX(100%);
}

.panel-header {
    display: flex;
    align-items: center;
    border-bottom: 1px solid var(--border);
    padding: 0 0.5rem 0 0;
    flex-shrink: 0;
}

.panel-tabs {
    display: flex;
    flex: 1;
    gap: 0.5rem;
    overflow-x: auto;
    scrollbar-width: none;
    padding: 0.35rem 0.35rem 0.25rem 0.35rem;
}
.panel-tabs::-webkit-scrollbar { display: none; }

.ptab-group {
    display: flex;
    gap: 0.25rem;
    border-radius: 10px;
    padding: 0.2rem;
}
.ptab-group-selected {
    background: #eff6ff;
    border: 1px solid #bfdbfe;
}
.ptab-group-all {
    background: #f8fafc;
    border: 1px solid #e2e8f0;
}

.ptab {
    padding: 0.45rem 0.65rem;
    min-width: 104px;
    border: none;
    border-radius: 8px;
    border-bottom: 2px solid transparent;
    background: transparent;
    color: var(--muted);
    font-size: 0.82rem;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    display: inline-flex;
    flex-direction: column;
    align-items: center;
    gap: 0.15rem;
    transition: color 0.15s, border-color 0.15s;
}
.ptab:hover { color: var(--text); }
.ptab.active {
    color: var(--text);
    border-bottom-color: var(--text);
    background: #ffffff;
}
.ptab-title {
    line-height: 1.05;
}
.ptab-tag {
    font-size: 0.62rem;
    font-weight: 700;
    letter-spacing: 0.03em;
    color: #475569;
    background: #e2e8f0;
    border-radius: 999px;
    padding: 0.04rem 0.36rem;
    line-height: 1.2;
}
.ptab.active .ptab-tag {
    background: #0f172a;
    color: #fff;
}

.station-name-label {
    font-size: 10px !important;
    line-height: 1.1 !important;
    padding: 1px 4px !important;
    border-radius: 4px;
    border: 1px solid #cbd5e1;
    background: rgba(255, 255, 255, 0.8);
    box-shadow: none;
}

.panel-close {
    border: none;
    background: none;
    color: var(--muted);
    font-size: 1rem;
    cursor: pointer;
    padding: 0.5rem;
    border-radius: 6px;
    flex-shrink: 0;
}
.panel-close:hover { background: var(--bg); color: var(--text); }

.panel-body {
    flex: 1;
    overflow-y: auto;
}

.tab-pane { display: none; padding: 1rem; }
.tab-pane.active { display: block; }

.panel-open-btn {
    position: fixed;
    bottom: 1.5rem;
    right: 1rem;
    background: #0f172a;
    color: #fff;
    border: none;
    border-radius: 999px;
    padding: 0.6rem 1.1rem;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    z-index: 401;
    box-shadow: var(--shadow);
    display: none;
}
.panel-open-btn.visible { display: block; }

/* ── Pane loading state ── */
.pane-loading {
    color: var(--muted);
    font-size: 0.9rem;
    padding: 1rem 0;
    text-align: center;
}

/* ── Alerts tab ── */
.alerts-summary {
    border-radius: var(--radius);
    padding: 0.9rem 1rem;
    margin-bottom: 1rem;
    font-weight: 600;
    font-size: 0.95rem;
}
.alerts-summary.ok       { background: #dcfce7; color: #15803d; }
.alerts-summary.disrupted { background: #fee2e2; color: #b91c1c; }

.line-section { margin-bottom: 1.25rem; }

.line-section-header {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    margin-bottom: 0.6rem;
}
.line-badge {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 42px;
    height: 28px;
    padding: 0 0.5rem;
    border-radius: 6px;
    font-size: 0.78rem;
    font-weight: 800;
    color: #fff;
    letter-spacing: 0.04em;
}
.line-section-name { font-weight: 600; font-size: 0.9rem; }

.status-pill {
    padding: 0.15rem 0.65rem;
    border-radius: 999px;
    font-size: 0.78rem;
    font-weight: 600;
    margin-left: auto;
}
.status-pill.ok       { background: #dcfce7; color: #15803d; }
.status-pill.disrupted { background: #fee2e2; color: #b91c1c; }
.status-pill.unknown  { background: #f1f5f9; color: var(--muted); }

.advisory-card {
    background: var(--bg);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.75rem;
    margin-top: 0.5rem;
    font-size: 0.85rem;
}
.advisory-card.disrupted {
    border-color: rgba(239,68,68,0.4);
    background: #fff5f5;
}
.advisory-dir { font-weight: 600; margin-bottom: 0.25rem; }
.advisory-msg { color: var(--muted); margin-bottom: 0.4rem; }
.advisory-stations {
    display: flex;
    flex-wrap: wrap;
    gap: 0.3rem;
    margin-top: 0.3rem;
}
.stn-chip {
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 999px;
    padding: 0.15rem 0.55rem;
    font-size: 0.78rem;
}

/* ── Crowd tab ── */
.crowd-list { display: flex; flex-direction: column; gap: 0.4rem; }

.crowd-row {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.55rem 0.75rem;
    background: var(--bg);
    border-radius: 8px;
    font-size: 0.85rem;
    cursor: pointer;
    transition: background 0.15s;
}
.crowd-row:hover { background: var(--border); }

.crowd-stn-code {
    font-weight: 700;
    font-size: 0.8rem;
    min-width: 40px;
    color: var(--muted);
}
.crowd-stn-name { flex: 1; font-weight: 500; }
.crowd-dot {
    width: 12px; height: 12px;
    border-radius: 50%;
    flex-shrink: 0;
}
.crowd-label {
    font-size: 0.78rem;
    font-weight: 600;
    min-width: 30px;
    text-align: right;
}
.crowd-dot.low,  .crowd-label.low  { background: #22c55e; color: #15803d; }
.crowd-dot.mod,  .crowd-label.mod  { background: #f59e0b; color: #92400e; }
.crowd-dot.high, .crowd-label.high { background: #ef4444; color: #991b1b; }
.crowd-dot.na,   .crowd-label.na   { background: #cbd5e1; color: var(--muted); }

.crowd-time {
    font-size: 0.75rem;
    color: var(--muted);
    margin-top: 0.25rem;
    padding: 0 0.25rem;
}

/* ── Forecast tab ── */
.forecast-station-label {
    font-size: 0.85rem;
    color: var(--muted);
    margin-bottom: 0.75rem;
    font-style: italic;
}
.forecast-content { display: flex; flex-direction: column; gap: 0.3rem; }

.forecast-row {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-size: 0.82rem;
}
.forecast-time { min-width: 52px; color: var(--muted); font-size: 0.78rem; }
.forecast-bar-wrap { flex: 1; height: 18px; background: #f1f5f9; border-radius: 4px; overflow: hidden; }
.forecast-bar { height: 100%; border-radius: 4px; transition: width 0.3s; }
.forecast-bar.low  { background: #22c55e; }
.forecast-bar.mod  { background: #f59e0b; }
.forecast-bar.high { background: #ef4444; }
.forecast-bar.na   { background: #cbd5e1; }
.forecast-lbl { min-width: 32px; font-size: 0.78rem; font-weight: 600; text-align: right; }
.forecast-lbl.low  { color: #15803d; }
.forecast-lbl.mod  { color: #92400e; }
.forecast-lbl.high { color: #991b1b; }
.forecast-lbl.na   { color: var(--muted); }

/* ── History tab ── */
.history-controls {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 1rem;
    align-items: center;
}
.history-select {
    flex: 1 1 10rem;
    min-width: 0;
    padding: 0.4rem 0.6rem;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: #fff;
    font-size: 0.85rem;
    color: var(--text);
}
#loadHistoryBtn {
    flex: 0 0 auto;
    white-space: nowrap;
}
.history-content { display: flex; flex-direction: column; gap: 0.4rem; }

.history-row {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    font-size: 0.82rem;
}
.history-rank { min-width: 22px; color: var(--muted); font-size: 0.75rem; text-align: right; }
.history-code { min-width: 72px; font-weight: 700; font-size: 0.8rem; color: var(--muted); }
.history-bar-wrap { flex: 1; height: 16px; background: #f1f5f9; border-radius: 4px; overflow: hidden; }
.history-bar { height: 100%; background: #5E35B1; border-radius: 4px; }
.history-vol { min-width: 52px; font-size: 0.78rem; color: var(--muted); text-align: right; }

.history-daytype {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 0.75rem;
}
.daytype-btn {
    padding: 0.25rem 0.75rem;
    border: 1.5px solid var(--border);
    border-radius: 999px;
    background: #fff;
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.15s, color 0.15s;
}
.daytype-btn.active {
    background: #0f172a;
    color: #fff;
    border-color: #0f172a;
}

/* ── Leaflet popup overrides ── */
.stn-popup { min-width: 160px; }
.stn-popup-name { font-weight: 700; font-size: 0.95rem; margin-bottom: 0.25rem; }
.stn-popup-crowd { font-size: 0.85rem; margin-bottom: 0.4rem; }
.stn-popup-btn {
    display: inline-block;
    margin-top: 0.25rem;
    padding: 0.3rem 0.7rem;
    background: #0f172a;
    color: #fff;
    border-radius: 6px;
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
    border: none;
    width: 100%;
}

/* ── Utilities ── */
.hidden { display: none !important; }

/* ── Mobile ── */
@media (max-width: 768px) {
    :root { --panel-w: 100vw; }

    .panel {
        top: auto;
        bottom: 0;
        right: 0;
        left: 0;
        width: 100%;
        height: 60dvh;
        border-left: none;
        border-top: 1px solid var(--border);
        border-radius: var(--radius) var(--radius) 0 0;
        box-shadow: 0 -4px 20px rgba(0,0,0,0.10);
    }
    .panel.hidden { transform: translateY(100%); }

    .crowd-legend { display: none; }

    .topbar-logo-text { display: none; }

    .panel-open-btn { right: 50%; transform: translateX(50%); }

    .history-controls {
        align-items: stretch;
    }
    .history-select {
        flex: 1 1 100%;
    }
    #loadHistoryBtn {
        width: 100%;
        text-align: center;
    }
}
