/* ===== Reset & Base (Mobile-First) ===== */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --bg: #f5f6fa;
    --surface: #ffffff;
    --text: #2d3436;
    --text-muted: #636e72;
    --border: #dfe6e9;
    --primary: #0984e3;
    --primary-hover: #0767b3;
    --delay-ok: #00b894;
    --delay-warn: #fdcb6e;
    --delay-bad: #d63031;
    --panel-shadow: 0 -4px 20px rgba(0,0,0,0.15);
    --radius: 8px;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    line-height: 1.5;
    min-height: 100vh;
    overflow-x: hidden;
}

/* ===== Header ===== */
header {
    background: var(--surface);
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
    text-align: center;
}

header h1 {
    font-size: 1.25rem;
    font-weight: 700;
    color: var(--text);
}

.subtitle {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin-top: 2px;
}

/* ===== Filter Bar ===== */
.filter-bar {
    display: flex;
    gap: 6px;
    padding: 10px 16px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    justify-content: center;
    flex-wrap: wrap;
}

.filter-btn {
    border: 1px solid var(--border);
    background: var(--surface);
    color: var(--text-muted);
    padding: 8px 14px;
    border-radius: 20px;
    font-size: 0.85rem;
    font-weight: 600;
    cursor: pointer;
    min-width: 40px;
    min-height: 40px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.15s ease;
    -webkit-tap-highlight-color: transparent;
}

.filter-btn:hover {
    border-color: var(--primary);
    color: var(--primary);
}

.filter-btn.active {
    background: var(--primary);
    color: white;
    border-color: var(--primary);
}

/* ===== Content Area ===== */
.content-area {
    position: relative;
    flex: 1;
}

/* ===== Map ===== */
#map-container {
    width: 100%;
}

#map {
    width: 100%;
    height: 42vh;
    min-height: 250px;
    background: #e8ecef;
}

/* ===== Legend ===== */
#legend {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    background: var(--surface);
    border-top: 1px solid var(--border);
    justify-content: center;
}

.legend-label {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-weight: 600;
}

.legend-bar {
    width: 150px;
    height: 10px;
    border-radius: 5px;
    background: linear-gradient(to right,
        #d73027, #f46d43, #fdae61, #fee08b, #d9ef8b, #a6d96a, #66bd63, #1a9850
    );
}

/* ===== Detail Panel (Mobile: Bottom Sheet) ===== */
.panel {
    background: var(--surface);
    border-top: 1px solid var(--border);
    box-shadow: var(--panel-shadow);
    overflow-y: auto;
    transition: transform 0.3s ease;
    max-height: 50vh;
    padding: 16px;
}

.panel.hidden {
    display: none;
}

.panel-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 12px;
    position: sticky;
    top: 0;
    background: var(--surface);
    padding-bottom: 8px;
    border-bottom: 1px solid var(--border);
}

.panel-header h2 {
    font-size: 1.1rem;
    font-weight: 700;
}

.close-btn {
    background: none;
    border: none;
    font-size: 1.5rem;
    color: var(--text-muted);
    cursor: pointer;
    padding: 4px 8px;
    min-width: 44px;
    min-height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius);
}

.close-btn:hover {
    background: var(--bg);
    color: var(--text);
}

/* ===== Ward Stats Cards ===== */
.ward-stats {
    display: flex;
    gap: 8px;
    margin-bottom: 16px;
    flex-wrap: wrap;
}

.stat {
    flex: 1;
    min-width: 80px;
    background: var(--bg);
    border-radius: var(--radius);
    padding: 10px;
    text-align: center;
}

.stat-value {
    display: block;
    font-size: 1.3rem;
    font-weight: 700;
    color: var(--text);
}

.stat-label {
    display: block;
    font-size: 0.7rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
    margin-top: 2px;
}

/* ===== Route Table ===== */
.data-note {
    font-size: 0.75rem;
    color: var(--text-muted);
    margin-bottom: 10px;
    font-style: italic;
}

.routes-header {
    font-size: 0.9rem;
    font-weight: 600;
    color: var(--text-muted);
    margin-bottom: 8px;
}

.table-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    margin: 0 -16px;
    padding: 0 16px;
}

.route-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.85rem;
    min-width: 320px;
}

.route-table th {
    text-align: left;
    padding: 8px 10px;
    border-bottom: 2px solid var(--border);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    color: var(--text-muted);
    font-weight: 600;
    white-space: nowrap;
}

.route-table td {
    padding: 10px;
    border-bottom: 1px solid var(--border);
    white-space: nowrap;
}

.route-table tr:last-child td {
    border-bottom: none;
}

.route-table tbody tr:active {
    background: var(--bg);
}

.route-id {
    font-weight: 700;
    color: var(--primary);
}

.delay-ok { color: var(--delay-ok); font-weight: 600; }
.delay-warn { color: #e17055; font-weight: 600; }
.delay-bad { color: var(--delay-bad); font-weight: 600; }

.no-data {
    padding: 24px 0;
    text-align: center;
    color: var(--text-muted);
    font-size: 0.9rem;
}

/* ===== Info Bar ===== */
#info-bar {
    display: flex;
    justify-content: space-between;
    padding: 8px 16px;
    font-size: 0.7rem;
    color: var(--text-muted);
    background: var(--surface);
    border-top: 1px solid var(--border);
    flex-wrap: wrap;
    gap: 4px;
}

/* ===== Ward % On-Time Label (permanent tooltip) ===== */
.ward-pct-label {
    background: rgba(255,255,255,0.88) !important;
    color: var(--text) !important;
    font-size: 0.72rem !important;
    font-weight: 700 !important;
    padding: 2px 6px !important;
    border-radius: 4px !important;
    border: none !important;
    box-shadow: 0 1px 3px rgba(0,0,0,0.2) !important;
    pointer-events: none !important;
    white-space: nowrap !important;
}

.ward-pct-label::before {
    display: none !important;
}

/* Ensure tooltip pane doesn't block clicks */
.leaflet-tooltip-pane {
    pointer-events: none !important;
}

/* ===== Leaflet Tooltip Override ===== */
.ward-tooltip {
    background: rgba(0,0,0,0.8);
    color: white;
    border: none;
    border-radius: 4px;
    padding: 4px 8px;
    font-size: 0.8rem;
    font-weight: 600;
}

/* ===== Footer ===== */
footer {
    background: var(--surface);
    border-top: 1px solid var(--border);
    padding: 16px;
    text-align: center;
    font-size: 0.75rem;
    color: var(--text-muted);
    line-height: 1.6;
}

footer a {
    color: var(--primary);
    text-decoration: none;
}

footer a:hover {
    text-decoration: underline;
}

.footer-sources {
    margin-top: 4px;
    font-size: 0.7rem;
}

/* ===== Loading State ===== */
.loading {
    text-align: center;
    padding: 32px;
    color: var(--text-muted);
}

/* =========================================
   Desktop Layout (768px+)
   ========================================= */
@media (min-width: 768px) {
    header h1 {
        font-size: 1.5rem;
    }

    .filter-btn {
        padding: 8px 18px;
    }

    .content-area {
        display: flex;
        height: calc(100vh - 140px);
    }

    #map-container {
        flex: 1;
        min-width: 0;
    }

    #map {
        height: calc(100% - 34px);
        min-height: 400px;
    }

    /* Panel becomes a side panel on desktop */
    .panel {
        width: 380px;
        max-height: none;
        height: 100%;
        border-top: none;
        border-left: 1px solid var(--border);
        box-shadow: -4px 0 20px rgba(0,0,0,0.08);
    }

    .table-wrap {
        margin: 0;
        padding: 0;
    }

    #info-bar {
        z-index: 500;
    }
}

/* ===== Large Desktop (1200px+) ===== */
@media (min-width: 1200px) {
    .panel {
        width: 440px;
    }
}
