:root {
    --bg: #0f1419;
    --panel: #1a2332;
    --panel-2: #243044;
    --text: #e8edf4;
    --muted: #8fa3bf;
    --accent: #e53935;
    --accent-2: #3d8bfd;
    --danger: #e85d5d;
    --ok: #3ecf8e;
    --border: #2d3a4f;
    --topbar-h: 68px;
    --mobile-tabs-h: 52px;
}

* { box-sizing: border-box; }
html, body {
    margin: 0;
    min-height: 100%;
    font-family: "Segoe UI", system-ui, sans-serif;
    background: var(--bg);
    color: var(--text);
    -webkit-text-size-adjust: 100%;
}

.topbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1.25rem;
    border-bottom: 1px solid var(--border);
    background: linear-gradient(180deg, #162030, var(--bg));
    position: sticky;
    top: 0;
    z-index: 1000;
}
.brand {
    display: flex;
    gap: 0.75rem;
    align-items: center;
    min-width: 0;
    flex: 1;
}
.brand h1 {
    margin: 0;
    font-size: 1.15rem;
    letter-spacing: 0.02em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}
.brand h1 span { color: var(--accent); }
.brand p {
    margin: 0.15rem 0 0;
    color: var(--muted);
    font-size: 0.82rem;
    line-height: 1.35;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.logo { filter: drop-shadow(0 0 8px rgba(229,57,53,0.35)); flex-shrink: 0; }

.lang-switch {
    display: flex;
    flex-shrink: 0;
    gap: 0.25rem;
}
.lang-switch a {
    color: var(--muted);
    text-decoration: none;
    padding: 0.35rem 0.55rem;
    border: 1px solid transparent;
    border-radius: 6px;
    font-size: 0.85rem;
}
.lang-switch a.active,
.lang-switch a:hover {
    color: var(--text);
    border-color: var(--border);
    background: var(--panel);
}

.mobile-tabs {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 1001;
    background: var(--panel);
    border-top: 1px solid var(--border);
    padding: 0.35rem;
    gap: 0.35rem;
    box-shadow: 0 -4px 20px rgba(0,0,0,0.35);
}
.mobile-tabs button {
    flex: 1;
    background: var(--panel-2);
    color: var(--muted);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.55rem 0.4rem;
    font-size: 0.82rem;
    cursor: pointer;
    touch-action: manipulation;
}
.mobile-tabs button.active {
    color: var(--text);
    border-color: var(--accent);
    background: #3a2228;
}

.layout {
    display: grid;
    grid-template-columns: minmax(240px, 280px) 1fr minmax(300px, 360px);
    height: calc(100dvh - var(--topbar-h));
    min-height: 0;
}

.sidebar,
.detail-panel {
    background: var(--panel);
    overflow: auto;
    -webkit-overflow-scrolling: touch;
    min-height: 0;
}
.sidebar {
    border-right: 1px solid var(--border);
}
.detail-panel {
    border-left: 1px solid var(--border);
    padding: 1rem;
}

.mode-tabs { display: flex; gap: 0.5rem; padding: 0.75rem; }
.mode-tabs button {
    flex: 1;
    background: var(--panel-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 0.55rem 0.5rem;
    cursor: pointer;
    font-size: 0.88rem;
    touch-action: manipulation;
}
.mode-tabs button.active {
    background: #3a2228;
    border-color: var(--accent);
}

.search-box { padding: 0 0.75rem 0.75rem; }
.search-box input {
    width: 100%;
    background: var(--bg);
    border: 1px solid var(--border);
    color: var(--text);
    border-radius: 8px;
    padding: 0.65rem 0.75rem;
    font-size: 16px;
}

.zone-list { list-style: none; margin: 0; padding: 0 0.5rem 1rem; }
.zone-list li {
    padding: 0.6rem 0.65rem;
    border-radius: 8px;
    cursor: pointer;
    margin-bottom: 0.25rem;
    display: flex;
    justify-content: space-between;
    gap: 0.5rem;
    font-size: 0.92rem;
    touch-action: manipulation;
}
.zone-list li:hover,
.zone-list li.active { background: var(--panel-2); }
.zone-list .idx { color: var(--accent); font-variant-numeric: tabular-nums; flex-shrink: 0; }
.zone-list li.hint {
    color: var(--muted);
    font-size: 0.82rem;
    cursor: default;
    font-style: italic;
}
.zone-list li.hint:hover { background: transparent; }

.map-panel {
    position: relative;
    min-height: 300px;
    min-width: 0;
}
#map {
    width: 100%;
    height: 100%;
    background: #121820;
    touch-action: pan-x pan-y;
}
#map.loading { opacity: 0.55; pointer-events: none; }

.map-toolbar {
    position: absolute;
    top: 12px;
    left: 12px;
    right: 12px;
    z-index: 500;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
    background: rgba(18,24,32,0.92);
    border: 1px solid var(--border);
    padding: 0.45rem 0.65rem;
    border-radius: 8px;
    font-size: 0.8rem;
    max-width: 320px;
}
.map-toolbar label { color: var(--muted); white-space: nowrap; }
.map-toolbar select {
    flex: 1;
    min-width: 120px;
    background: var(--panel-2);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 6px;
    padding: 0.35rem 0.4rem;
    font-size: 16px;
}

.map-legend {
    position: absolute;
    bottom: 12px;
    left: 12px;
    right: 12px;
    z-index: 500;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: rgba(18,24,32,0.92);
    padding: 0.4rem 0.6rem;
    border-radius: 8px;
    font-size: 0.75rem;
    color: var(--muted);
    border: 1px solid var(--border);
    max-width: 280px;
}
.legend-gradient {
    flex: 1;
    min-width: 80px;
    max-width: 120px;
    height: 8px;
    border-radius: 4px;
    background: linear-gradient(90deg, #2d5a4a, #5a8f7b, #d4a017, #c0392b);
}

.zone-tip {
    background: rgba(18,24,32,0.95) !important;
    border: 1px solid var(--border) !important;
    color: var(--text) !important;
    font-size: 0.8rem !important;
    box-shadow: none !important;
}

.detail-panel h2 { margin: 0 0 0.25rem; font-size: 1.2rem; line-height: 1.3; }
.detail-panel h3 { margin: 1rem 0 0.5rem; font-size: 0.95rem; color: var(--muted); }
.zone-meta { color: var(--muted); margin: 0 0 1rem; font-size: 0.9rem; line-height: 1.45; }

.indices-grid {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.5rem;
    margin-bottom: 1rem;
}
.index-card {
    background: var(--panel-2);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.65rem 0.75rem;
}
.index-card .label {
    color: var(--muted);
    font-size: 0.75rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}
.index-card .value {
    font-size: 1.35rem;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
}
.index-card.high .value { color: var(--danger); }
.index-card.low .value { color: var(--ok); }
.index-card.mid .value { color: #d4a017; }

.metrics-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.88rem;
    margin-bottom: 1rem;
    display: block;
    overflow-x: auto;
}
.metrics-table tbody { display: table; width: 100%; }
.metrics-table td {
    padding: 0.45rem 0;
    border-bottom: 1px solid var(--border);
    vertical-align: top;
}
.metrics-table td:first-child { padding-right: 0.5rem; }
.metrics-table td:last-child {
    text-align: right;
    font-variant-numeric: tabular-nums;
    white-space: nowrap;
}

#history-chart {
    width: 100% !important;
    max-height: 200px;
}

.footer-meta {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    color: var(--muted);
    margin-top: 1rem;
    padding-bottom: 0.5rem;
    font-size: 0.8rem;
    line-height: 1.4;
}

.leaflet-popup-content-wrapper {
    background: var(--panel);
    color: var(--text);
    border: 1px solid var(--border);
}
.leaflet-popup-tip { background: var(--panel); }
.leaflet-control-zoom a {
    width: 34px !important;
    height: 34px !important;
    line-height: 34px !important;
}

@media (min-width: 961px) {
    .sidebar,
    .map-panel,
    .detail-panel {
        display: block !important;
        position: static !important;
        visibility: visible !important;
        opacity: 1 !important;
        pointer-events: auto !important;
        height: auto !important;
    }
    .map-panel { display: block !important; }
    #map { height: 100% !important; }
    .mobile-tabs { display: none !important; }
}

/* Tablette paysage / petit desktop */
@media (max-width: 1200px) {
    .layout {
        grid-template-columns: 260px 1fr 320px;
    }
}

/* Tablette portrait + mobile */
@media (max-width: 960px) {
    :root {
        --topbar-h: 60px;
        --mobile-tabs-h: 54px;
    }

    html, body {
        height: 100%;
        overflow: hidden;
    }

    .layout {
        position: relative;
        display: block;
        height: calc(100dvh - var(--topbar-h) - var(--mobile-tabs-h));
        min-height: 280px;
        overflow: hidden;
    }

    .mobile-tabs {
        display: flex;
        padding-bottom: max(0.35rem, env(safe-area-inset-bottom));
    }

    /* Panneaux empilés : la carte garde toujours une taille (fix Leaflet) */
    .sidebar,
    .map-panel,
    .detail-panel {
        position: absolute;
        inset: 0;
        width: 100%;
        height: 100%;
        display: block !important;
        visibility: hidden;
        pointer-events: none;
        opacity: 0;
        z-index: 1;
        border: 0;
    }

    .sidebar.panel-active,
    .map-panel.panel-active,
    .detail-panel.panel-active {
        visibility: visible;
        pointer-events: auto;
        opacity: 1;
        z-index: 2;
    }

    .sidebar,
    .detail-panel {
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 1rem;
    }

    .detail-panel { padding: 1rem; }

    .map-panel {
        display: flex !important;
        flex-direction: column;
        min-height: 0;
    }

    #map {
        flex: 1 1 auto;
        width: 100% !important;
        height: 100% !important;
        min-height: 240px;
    }

    .map-toolbar {
        top: 8px;
        left: 8px;
        right: 8px;
        max-width: none;
    }

    .map-legend {
        bottom: max(8px, env(safe-area-inset-bottom));
        left: 8px;
        right: 8px;
        max-width: none;
    }

    .footer-meta { padding-bottom: 1rem; }

    .leaflet-control-zoom {
        margin-bottom: max(10px, env(safe-area-inset-bottom)) !important;
    }

}

/* Mobile */
@media (max-width: 600px) {
    :root {
        --topbar-h: 64px;
    }

    .topbar {
        flex-wrap: wrap;
        padding: 0.65rem 0.85rem;
    }

    .brand h1 { font-size: 1rem; }
    .brand p {
        font-size: 0.78rem;
        -webkit-line-clamp: 1;
    }

    .logo { width: 32px; height: 32px; }

    .indices-grid {
        grid-template-columns: 1fr 1fr;
        gap: 0.4rem;
    }

    .index-card .value { font-size: 1.15rem; }

    .metrics-table { font-size: 0.84rem; }

    .mode-tabs button { font-size: 0.82rem; padding: 0.5rem; }
}

@media (max-width: 380px) {
    .indices-grid { grid-template-columns: 1fr; }
    .mobile-tabs button { font-size: 0.76rem; padding: 0.5rem 0.25rem; }
}