html, body {
    margin: 0;
    padding: 0;
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: #101418;
    color: #e6e8ea;
    overflow: hidden;
}

#map {
    position: absolute;
    inset: 0;
    background: #0b0e11;
}

/* Force Leaflet controls into the dark theme */
.leaflet-container {
    background: #0b0e11;
    outline: none;
}
.leaflet-control-attribution {
    background: rgba(20, 24, 28, 0.75) !important;
    color: #8891a0 !important;
}
.leaflet-control-attribution a { color: #b3bcc7 !important; }
.leaflet-bar a {
    background-color: #1a1f26 !important;
    color: #e6e8ea !important;
    border-bottom-color: #2b323b !important;
}
.leaflet-bar a:hover { background-color: #232a33 !important; }

#ui-overlay {
    position: absolute;
    top: 16px;
    left: 16px;
    z-index: 1000;
    max-width: 340px;
    pointer-events: none;
}

.panel {
    background: rgba(20, 24, 28, 0.92);
    border: 1px solid #2b323b;
    border-radius: 12px;
    padding: 18px 20px 16px 20px;
    backdrop-filter: blur(10px);
    box-shadow: 0 6px 20px rgba(0,0,0,0.5);
    pointer-events: auto;
}

h1 {
    margin: 0 0 4px 0;
    font-size: 20px;
    font-weight: 600;
    letter-spacing: -0.01em;
}

.panel-top {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    margin-bottom: 6px;
}

.back-link {
    display: inline-block;
    font-size: 12px;
    color: #8891a0;
    text-decoration: none;
    white-space: nowrap;
}
.back-link:hover { color: #e6e8ea; }

/* "More settings" / "Collapse" toggle — mobile only */
#panel-toggle { display: none; }

.subtitle {
    margin: 0 0 14px 0;
    font-size: 12px;
    color: #8891a0;
}

.search-row {
    display: flex;
    gap: 6px;
    margin-bottom: 14px;
}

#search-input {
    flex: 1;
    background: #0f1318;
    border: 1px solid #2b323b;
    color: #e6e8ea;
    padding: 8px 10px;
    border-radius: 6px;
    font-size: 13px;
    outline: none;
}
#search-input:focus { border-color: #4a90e2; }

#search-btn {
    background: #4a90e2;
    color: #fff;
    border: none;
    padding: 0 14px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
}
#search-btn:hover { background: #5aa0f2; }
#search-btn:disabled { opacity: 0.5; cursor: wait; }

.slider-row {
    margin-bottom: 12px;
}

.slider-row label {
    display: block;
    font-size: 13px;
    color: #b3bcc7;
    margin-bottom: 8px;
}
#time-value {
    color: #4a90e2;
    font-weight: 600;
    font-size: 15px;
    margin: 0 3px;
}

#time-slider {
    width: 100%;
    -webkit-appearance: none;
    appearance: none;
    height: 4px;
    background: #2b323b;
    border-radius: 2px;
    outline: none;
}
#time-slider::-webkit-slider-thumb {
    -webkit-appearance: none;
    appearance: none;
    width: 16px;
    height: 16px;
    background: #4a90e2;
    border-radius: 50%;
    cursor: pointer;
    box-shadow: 0 0 0 4px rgba(74, 144, 226, 0.15);
}
#time-slider::-moz-range-thumb {
    width: 16px;
    height: 16px;
    background: #4a90e2;
    border-radius: 50%;
    cursor: pointer;
    border: none;
}

.tick-labels {
    display: flex;
    justify-content: space-between;
    font-size: 10px;
    color: #6b7482;
    margin-top: 4px;
}

.status {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 12px;
    color: #8891a0;
    padding: 8px 0;
    border-top: 1px solid #2b323b;
    margin-top: 6px;
}
#stats { color: #b3bcc7; font-variant-numeric: tabular-nums; }

.legend {
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid #2b323b;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 4px 12px;
    font-size: 11px;
    color: #b3bcc7;
}
.legend-row { display: flex; align-items: center; gap: 6px; }
.swatch {
    width: 18px;
    height: 3px;
    border-radius: 2px;
    flex-shrink: 0;
}
.swatch.walk {
    background: repeating-linear-gradient(90deg, #6b7482 0 3px, transparent 3px 6px);
    height: 2px;
}
.dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    flex-shrink: 0;
    border: 1.5px solid #fff;
    box-sizing: border-box;
}

.basemap-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 12px;
    font-size: 12px;
    color: #b3bcc7;
}
#basemap-select {
    flex: 1;
    background: #0f1318;
    color: #e6e8ea;
    border: 1px solid #2b323b;
    border-radius: 6px;
    padding: 6px 8px;
    font-size: 12px;
    outline: none;
}
#basemap-select:focus { border-color: #4a90e2; }

.station-tt {
    background: rgba(20, 24, 28, 0.95) !important;
    color: #e6e8ea !important;
    border: 1px solid #2b323b !important;
    font-size: 11px !important;
    padding: 4px 8px !important;
}
.station-tt::before { border-top-color: rgba(20, 24, 28, 0.95) !important; }

#loading {
    position: absolute;
    top: 16px;
    left: 50%;
    transform: translateX(-50%);
    background: rgba(74, 144, 226, 0.9);
    color: #fff;
    padding: 8px 16px;
    border-radius: 20px;
    font-size: 13px;
    z-index: 1001;
    pointer-events: none;
    box-shadow: 0 4px 12px rgba(0,0,0,0.4);
}
#loading.hidden { display: none; }

.systems-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px 10px;
    margin-top: 10px;
    padding-top: 10px;
    border-top: 1px solid #2b323b;
    font-size: 11px;
    color: #b3bcc7;
}
.systems-label {
    color: #6b7482;
    font-size: 11px;
    text-transform: uppercase;
    letter-spacing: 0.06em;
}
.sys-toggle {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    cursor: pointer;
    user-select: none;
}
.sys-toggle input {
    accent-color: #4a90e2;
    margin: 0;
}
.sys-toggle.off {
    opacity: 0.45;
}
.mode-note {
    color: #6b7482;
    font-size: 11px;
    margin-left: auto;
    font-style: italic;
}
.sys-swatch {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    border: 1.5px solid #fff;
    box-sizing: border-box;
    flex-shrink: 0;
}

.toggle-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 6px;
    padding-top: 10px;
    border-top: 1px solid #2b323b;
}
#heatmap-btn, #filter-used-btn, .detail-btn {
    background: #232a33;
    color: #e6e8ea;
    border: 1px solid #2b323b;
    padding: 7px 12px;
    border-radius: 6px;
    cursor: pointer;
    font-size: 12px;
    font-weight: 500;
    transition: background 0.15s, border-color 0.15s;
}
#heatmap-btn:hover, #filter-used-btn:hover, .detail-btn:hover { background: #2b323b; }
#heatmap-btn[aria-pressed="true"], #filter-used-btn[aria-pressed="true"],
.detail-btn[aria-pressed="true"] {
    background: #4a90e2;
    border-color: #4a90e2;
    color: #fff;
}
#heatmap-btn:disabled, #filter-used-btn:disabled {
    opacity: 0.4;
    cursor: not-allowed;
}
#heatmap-note, #filter-used-note, #detail-note {
    font-size: 11px;
    color: #6b7482;
}
.detail-btns { display: inline-flex; gap: 4px; }
.detail-btn { padding: 6px 10px; }

/* ── Mobile: top-left collapsible panel ───────────────────────────────── */
@media (max-width: 640px) {
    #ui-overlay {
        top: calc(10px + env(safe-area-inset-top));
        left: 10px;
        right: 10px;
        max-width: 380px;
    }
    .panel {
        padding: 12px 14px;
        /* Expanded panel scrolls rather than running off a short screen */
        max-height: calc(100vh - 40px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }

    .panel-top { margin-bottom: 0; }
    #ui-overlay:not(.collapsed) .panel-top { margin-bottom: 8px; }

    #panel-toggle {
        display: inline-block;
        background: #232a33;
        color: #e6e8ea;
        border: 1px solid #2b323b;
        padding: 8px 12px;
        border-radius: 6px;
        cursor: pointer;
        font-size: 13px;
        font-weight: 500;
        white-space: nowrap;
        touch-action: manipulation;
    }
    #panel-toggle[aria-expanded="true"] {
        background: #4a90e2;
        border-color: #4a90e2;
        color: #fff;
    }

    /* Collapsed: just the top row — back link + "More settings" */
    #ui-overlay.collapsed h1,
    #ui-overlay.collapsed .subtitle,
    #ui-overlay.collapsed .search-row,
    #ui-overlay.collapsed .basemap-row,
    #ui-overlay.collapsed .slider-row,
    #ui-overlay.collapsed .systems-row,
    #ui-overlay.collapsed .toggle-row,
    #ui-overlay.collapsed .status,
    #ui-overlay.collapsed .legend { display: none; }
    #ui-overlay.collapsed { right: auto; }
    #ui-overlay.collapsed .panel { padding: 10px 12px; }

    h1 { font-size: 17px; }
    .back-link { font-size: 13px; padding: 4px 0; }
    /* 16px stops iOS Safari auto-zooming the page when the input focuses */
    #search-input { font-size: 16px; padding: 10px 12px; }
    #search-btn { padding: 0 18px; font-size: 15px; }
    #basemap-select { font-size: 14px; padding: 9px 10px; }
    #time-slider { height: 6px; }
    #time-slider::-webkit-slider-thumb { width: 26px; height: 26px; }
    #time-slider::-moz-range-thumb { width: 26px; height: 26px; }
    .sys-toggle { padding: 4px 2px; font-size: 13px; }
    .sys-toggle input { width: 18px; height: 18px; }
    #heatmap-btn, #filter-used-btn { padding: 10px 14px; font-size: 14px; }
    .status { font-size: 13px; }
    button, select, input, .sys-toggle { touch-action: manipulation; }

    /* Zoom control sits under the panel top-left — move it below the pill */
    .leaflet-top.leaflet-left { top: calc(60px + env(safe-area-inset-top)); }

    /* Loading pill clear of the notch and the panel */
    #loading {
        top: auto;
        bottom: calc(24px + env(safe-area-inset-bottom));
    }
}

/* ── Intro overlay: loading screen + feature guide ───────────────────── */
#intro-overlay {
    position: absolute;
    inset: 0;
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: rgba(8, 11, 14, 0.78);
    backdrop-filter: blur(8px);
    -webkit-backdrop-filter: blur(8px);
    opacity: 1;
    transition: opacity 0.35s ease;
}
#intro-overlay[hidden] { display: none; }
#intro-overlay.closing { opacity: 0; pointer-events: none; }

.intro-card {
    width: 100%;
    max-width: 440px;
    background: rgba(20, 24, 28, 0.97);
    border: 1px solid #2b323b;
    border-radius: 14px;
    padding: 26px 28px 18px;
    box-shadow: 0 16px 50px rgba(0, 0, 0, 0.65);
    text-align: center;
}

.intro-dots {
    display: flex;
    justify-content: center;
    gap: 7px;
    margin-bottom: 18px;
}
.intro-dots span {
    width: 8px;
    height: 8px;
    border-radius: 50%;
    background: #2b323b;
    transition: background 0.2s, transform 0.2s;
}
.intro-dots span.done   { background: #33547a; }
.intro-dots span.active { background: #4a90e2; transform: scale(1.3); }

.intro-icon {
    font-size: 40px;
    line-height: 1;
    margin-bottom: 10px;
}

.intro-card h2 {
    margin: 0 0 8px;
    font-size: 21px;
    font-weight: 600;
    letter-spacing: -0.01em;
}

#intro-text {
    margin: 0 0 20px;
    font-size: 13.5px;
    line-height: 1.55;
    color: #b3bcc7;
    min-height: 84px;
}

.intro-nav {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}
.intro-nav-right { display: flex; gap: 8px; }

.intro-nav button {
    border: 1px solid #2b323b;
    background: #232a33;
    color: #e6e8ea;
    padding: 9px 16px;
    border-radius: 7px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 500;
    transition: background 0.15s, border-color 0.15s;
}
.intro-nav button:hover { background: #2b323b; }
.intro-nav button.primary {
    background: #4a90e2;
    border-color: #4a90e2;
    color: #fff;
}
.intro-nav button.primary:hover { background: #5aa0f2; }
#intro-skip {
    background: none;
    border-color: transparent;
    color: #8891a0;
}
#intro-skip:hover { color: #e6e8ea; background: none; }

.intro-load {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    margin-top: 18px;
    padding-top: 14px;
    border-top: 1px solid #2b323b;
    font-size: 11.5px;
    color: #8891a0;
}
.intro-spinner {
    width: 12px;
    height: 12px;
    flex-shrink: 0;
    border: 2px solid #2b323b;
    border-top-color: #4a90e2;
    border-radius: 50%;
    animation: intro-spin 0.9s linear infinite;
}
@keyframes intro-spin { to { transform: rotate(360deg); } }
.intro-spinner.done {
    animation: none;
    border-color: #22c55e;
    position: relative;
}
.intro-spinner.error {
    animation: none;
    border-color: #dc2626;
}

.intro-bar {
    margin-top: 10px;
    height: 4px;
    border-radius: 2px;
    background: #2b323b;
    overflow: hidden;
}
#intro-bar-fill {
    width: 0%;
    height: 100%;
    border-radius: 2px;
    background: #4a90e2;
    transition: width 0.25s ease;
}
.intro-bar.done #intro-bar-fill { background: #22c55e; }

/* Compact mode (returning visitors): just title, spinner, and two buttons */
#intro-overlay.compact .intro-dots,
#intro-overlay.compact .intro-icon,
#intro-overlay.compact #intro-back { display: none; }
#intro-overlay.compact #intro-text { min-height: 0; }
#intro-overlay.compact .intro-card { max-width: 360px; }

@media (max-width: 640px) {
    .intro-card { padding: 20px 18px 14px; }
    #intro-text { font-size: 14px; min-height: 110px; }
    .intro-nav button { padding: 11px 16px; font-size: 14px; touch-action: manipulation; }
}

/* Custom marker */
.origin-marker {
    width: 20px;
    height: 20px;
    background: #4a90e2;
    border: 3px solid #fff;
    border-radius: 50%;
    box-shadow: 0 0 0 4px rgba(74, 144, 226, 0.3), 0 0 20px rgba(74, 144, 226, 0.6);
    animation: pulse 2s infinite;
}
@keyframes pulse {
    0% { box-shadow: 0 0 0 4px rgba(74, 144, 226, 0.3), 0 0 20px rgba(74, 144, 226, 0.6); }
    50% { box-shadow: 0 0 0 10px rgba(74, 144, 226, 0.1), 0 0 30px rgba(74, 144, 226, 0.8); }
    100% { box-shadow: 0 0 0 4px rgba(74, 144, 226, 0.3), 0 0 20px rgba(74, 144, 226, 0.6); }
}
