:root {
    --visual-viewport-top: 0px;
    --visual-viewport-left: 0px;
    --visual-viewport-right: 0px;
    --visual-viewport-width: 100vw;
    --visual-viewport-height: 100dvh;
}

body, html { margin: 0; padding: 0; height: 100%; width: 100%; overflow: hidden; font-family: Arial, sans-serif; }
#map { position: fixed; top: 0; left: 0; width: 100%; height: 100%; }

/* ─── Sidebar ─────────────────────────────────────────────────────────────── */
#controls {
    position: absolute; top: 10px; right: 10px; z-index: 1000;
    background: rgba(255, 255, 255, 0.55); padding: 50px 15px 15px; border-radius: 5px;
    backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px);
    box-shadow: 0 0 15px rgba(0,0,0,0.2); width: 280px;
    transition: transform 0.25s ease, opacity 0.25s ease;
    max-height: calc(100vh - 20px); overflow-y: auto; box-sizing: border-box;
}
#controls.collapsed {
    transform: translateX(320px);
    opacity: 0;
    pointer-events: none;
}

#controlsScrim { display: none; }

/* ─── Toggle button ───────────────────────────────────────────────────────── */
#toggleControls {
    position: fixed;
    top: calc(var(--visual-viewport-top) + 10px);
    right: calc(var(--visual-viewport-right) + 10px);
    z-index: 4001;
    width: 40px; height: 40px; padding: 0;
    background: #333; color: white; border: none; border-radius: 5px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
    font-size: 1.3rem; cursor: pointer; touch-action: manipulation;
    transform: translateZ(0);
    -webkit-transform: translateZ(0);
    backface-visibility: hidden;
}
#toggleControls:hover { background: #555; }

/* ─── Floating stats bar (visible when sidebar hidden) ────────────────────── */
#floatingStats {
    position: absolute; top: 10px; left: 50%; transform: translateX(-50%);
    z-index: 999; background: rgba(255,255,255,0.92); padding: 6px 14px;
    border-radius: 20px; box-shadow: 0 1px 6px rgba(0,0,0,0.15);
    font-size: 0.82rem; font-weight: 500; color: #333;
    white-space: nowrap; pointer-events: none; max-width: calc(100vw - 120px);
}

/* ─── Controls ────────────────────────────────────────────────────────────── */
h2 { margin-top: 0; font-size: 1.2rem; }
.control-group { margin-bottom: 15px; }
select, button { width: 100%; padding: 8px; margin-top: 5px; font-size: 0.95em; }
button { background-color: #007bff; color: white; border: none; border-radius: 3px; cursor: pointer; }
button:disabled { background-color: #cccccc; cursor: not-allowed; }
button:hover:not(:disabled) { background-color: #0056b3; }
input[type=range] { width: 100%; margin: 10px 0; }
#playBtn, #pauseBtn { padding: 6px; font-size: 0.9em; }
#status { margin-top: 10px; font-size: 0.9em; color: #555; }
.help-text { margin-top: 6px; font-size: 0.8rem; color: #666; line-height: 1.35; }

/* ─── Legend ───────────────────────────────────────────────────────────────── */
#legend { margin-top: 8px; }
.legend-item { display: flex; align-items: center; margin-bottom: 5px; font-size: 0.9rem; }
.color-box { width: 15px; height: 15px; display: inline-block; margin-right: 8px; border: 1px solid #ccc; }

/* ─── Population stats ────────────────────────────────────────────────────── */
#populationStats { margin-top: 15px; padding-top: 10px; border-top: 1px solid #eee; }
#populationStats h3 { font-size: 1rem; margin: 0 0 10px 0; }
.stat-row { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 4px; }
.stat-label { font-size: 0.85rem; color: #555; }
.stat-value { font-size: 1rem; font-weight: bold; }
.stat-bar-container { display: flex; height: 8px; border-radius: 4px; overflow: hidden; background: #eee; margin: 4px 0; }
.stat-bar-in { background: #007bff; transition: width 0.3s; }
.stat-bar-out { background: #ddd; transition: width 0.3s; }
.stat-detail { font-size: 0.78rem; color: #777; margin-bottom: 2px; }
.stat-source { font-size: 0.72rem; color: #aaa; margin-top: 10px; font-style: italic; }

/* ─── Misc ────────────────────────────────────────────────────────────────── */
.hidden { display: none !important; }
#loading { display: flex; align-items: center; margin-top: 10px; color: #007bff; }
.spinner { border: 3px solid #f3f3f3; border-top: 3px solid #007bff; border-radius: 50%; width: 15px; height: 15px; animation: spin 1s linear infinite; margin-right: 8px; }
@keyframes spin { 0% { transform: rotate(0deg); } 100% { transform: rotate(360deg); } }
.snap-marker { width: 14px; height: 14px; background: #007bff; border: 2px solid white; border-radius: 50%; box-shadow: 0 0 4px rgba(0,0,0,0.4); opacity: 0.8; }

/* ─── Mobile ──────────────────────────────────────────────────────────────── */
@media (max-width: 768px) {
    #controlsScrim {
        position: fixed;
        top: var(--visual-viewport-top);
        left: var(--visual-viewport-left);
        width: var(--visual-viewport-width);
        height: var(--visual-viewport-height);
        display: block;
        z-index: 999;
        background: rgba(15, 23, 42, 0.28);
        backdrop-filter: blur(2px);
        -webkit-backdrop-filter: blur(2px);
    }
    #controls {
        position: fixed;
        top: var(--visual-viewport-top);
        right: var(--visual-viewport-right);
        bottom: auto;
        left: auto;
        width: min(24rem, calc(var(--visual-viewport-width) - 56px));
        max-width: calc(var(--visual-viewport-width) - 56px);
        height: var(--visual-viewport-height);
        max-height: var(--visual-viewport-height);
        padding: calc(72px + env(safe-area-inset-top)) 16px calc(20px + env(safe-area-inset-bottom));
        border-radius: 20px 0 0 20px;
        background: rgba(255, 255, 255, 0.94);
        box-shadow: -18px 0 40px rgba(15, 23, 42, 0.24);
        font-size: 0.92em;
        overscroll-behavior: contain;
    }
    #controls.collapsed {
        transform: translateX(calc(100% + 12px));
    }
    #toggleControls {
        top: calc(var(--visual-viewport-top) + 10px + env(safe-area-inset-top));
        right: calc(var(--visual-viewport-right) + 10px);
        width: 48px;
        height: 48px;
        border-radius: 14px;
        font-size: 1.45rem;
        background: rgba(15, 23, 42, 0.92);
    }
    #floatingStats {
        top: calc(var(--visual-viewport-top) + 12px + env(safe-area-inset-top));
        left: 50%;
        right: auto;
        transform: translateX(-50%);
        white-space: nowrap;
        border-radius: 16px;
        max-width: calc(100vw - 80px);
    }
}
