/* ── Reset & base ─────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:        #0f1923;
  --panel:     #16232e;
  --panel2:    #1c2e3a;
  --accent:    #00c9a7;
  --accent2:   #f7a600;
  --text:      #e8edf2;
  --muted:     #7a92a6;
  --border:    rgba(255,255,255,0.08);
  --radius:    10px;
  --shadow:    0 4px 24px rgba(0,0,0,0.45);
  --font:      'Inter', system-ui, sans-serif;
  --panel-w:   340px;
}

html, body { height: 100%; font-family: var(--font); background: var(--bg); color: var(--text); overflow: hidden; }

/* ── Layout ───────────────────────────────────────────── */
#app { display: flex; height: 100vh; position: relative; }

#sidebar {
  width: var(--panel-w);
  min-width: var(--panel-w);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  z-index: 1000;
  box-shadow: 4px 0 20px rgba(0,0,0,0.4);
  overflow: hidden;
  transition: transform 0.3s ease;
}

#map-container { flex: 1; position: relative; }
#map { width: 100%; height: 100%; }

/* ── Header ───────────────────────────────────────────── */
#header {
  background: linear-gradient(135deg, #0d2137 0%, #162b3e 100%);
  padding: 18px 20px 14px;
  border-bottom: 1px solid var(--border);
}

#header h1 {
  font-size: 1rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  line-height: 1.3;
  color: #fff;
}

#header .subtitle {
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 4px;
  line-height: 1.4;
}

.tag {
  display: inline-block;
  background: rgba(0,201,167,0.15);
  color: var(--accent);
  border: 1px solid rgba(0,201,167,0.3);
  border-radius: 4px;
  font-size: 0.65rem;
  font-weight: 700;
  padding: 1px 6px;
  margin-right: 6px;
  letter-spacing: 0.05em;
  vertical-align: middle;
}

/* ── Sidebar body ─────────────────────────────────────── */
#sidebar-body {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

/* ── Section titles ───────────────────────────────────── */
.section-title {
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  color: var(--muted);
  text-transform: uppercase;
  margin-bottom: 8px;
  margin-top: 18px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border);
}
.section-title:first-child { margin-top: 4px; }

/* ── Variable cards ───────────────────────────────────── */
.var-card {
  background: var(--panel2);
  border: 1.5px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 13px;
  margin-bottom: 6px;
  cursor: pointer;
  transition: all 0.18s ease;
  position: relative;
}

.var-card:hover { border-color: rgba(0,201,167,0.4); background: #1f3344; }
.var-card.active { border-color: var(--accent); background: rgba(0,201,167,0.08); }
.var-card.active-bivar { border-color: var(--accent2); background: rgba(247,166,0,0.08); }

.var-card .var-name {
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text);
}
.var-card .var-desc {
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 3px;
  line-height: 1.4;
}
.var-card .var-unit {
  font-size: 0.68rem;
  color: var(--accent);
  margin-top: 4px;
  font-weight: 600;
}

.badge {
  position: absolute;
  top: 8px; right: 10px;
  font-size: 0.6rem;
  font-weight: 700;
  border-radius: 4px;
  padding: 2px 6px;
  letter-spacing: 0.05em;
  text-transform: uppercase;
}
.badge-bivar { background: rgba(247,166,0,0.2); color: var(--accent2); border: 1px solid rgba(247,166,0,0.3); }

/* ── Mode toggle ──────────────────────────────────────── */
#mode-toggle {
  display: flex;
  background: var(--bg);
  border-radius: 8px;
  padding: 3px;
  margin-bottom: 14px;
}

.mode-btn {
  flex: 1;
  background: none;
  border: none;
  color: var(--muted);
  font-size: 0.75rem;
  font-weight: 600;
  padding: 7px 8px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.18s;
  font-family: var(--font);
}
.mode-btn.active { background: var(--panel2); color: var(--text); box-shadow: 0 1px 6px rgba(0,0,0,0.3); }

/* ── Info panel (commune) ─────────────────────────────── */
#info-panel {
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 14px;
  display: none;
}
#info-panel.visible { display: block; }

#info-name {
  font-size: 0.95rem;
  font-weight: 700;
  margin-bottom: 2px;
}
#info-insee {
  font-size: 0.7rem;
  color: var(--muted);
  margin-bottom: 10px;
}

.info-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 0;
  border-bottom: 1px solid var(--border);
}
.info-row:last-child { border-bottom: none; }
.info-row .lbl { font-size: 0.73rem; color: var(--muted); }
.info-row .val { font-size: 0.8rem; font-weight: 700; color: var(--accent); }
.info-row .val.highlight { color: var(--accent2); }

/* ── Search ───────────────────────────────────────────── */
#search-box {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 0.82rem;
  padding: 9px 13px;
  margin-bottom: 14px;
  outline: none;
  font-family: var(--font);
  transition: border-color 0.18s;
}
#search-box:focus { border-color: var(--accent); }
#search-box::placeholder { color: var(--muted); }

#search-results {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-top: -10px;
  margin-bottom: 12px;
  max-height: 160px;
  overflow-y: auto;
  display: none;
}
.search-item {
  padding: 8px 13px;
  font-size: 0.8rem;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  transition: background 0.12s;
}
.search-item:last-child { border-bottom: none; }
.search-item:hover { background: var(--panel2); }
.search-item .dept { color: var(--muted); font-size: 0.7rem; }

/* ── Floating legend on map ───────────────────────────── */
#map-legend {
  position: absolute;
  bottom: 28px;
  right: 12px;
  z-index: 800;
  background: rgba(15, 25, 35, 0.92);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  min-width: 190px;
  max-width: 230px;
  box-shadow: var(--shadow);
  pointer-events: none; /* laisse passer les clics carte */
  transition: opacity 0.2s ease;
}


.legend-title {
  font-size: 0.71rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 3px;
  line-height: 1.3;
}

.legend-method {
  font-size: 0.62rem;
  color: var(--muted);
  font-style: italic;
  margin-bottom: 9px;
  line-height: 1.4;
  padding-bottom: 7px;
  border-bottom: 1px solid var(--border);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 7px;
  margin-bottom: 3px;
}

.legend-swatch {
  display: inline-block;
  width: 16px;
  height: 12px;
  border-radius: 2px;
  flex-shrink: 0;
}

.legend-label {
  font-size: 0.69rem;
  color: var(--text);
}

/* Bivariate legend */
#bivar-legend {
  display: none;
  margin-top: 8px;
}
#bivar-legend.visible { display: block; }

/* ── Responsive legend (mobile) ──────────────────────── */
@media (max-width: 720px) {
  #map-legend {
    bottom: 12px;
    left: 8px;
    right: 8px;
    max-width: none;
    min-width: 0;
    padding: 9px 12px;
  }

  /* Items en grille 2 colonnes pour compacité */
  .legend-items-wrap {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 3px 12px;
  }
}

/* ── Stats bar ────────────────────────────────────────── */
#stats-bar {
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 13px;
  margin-bottom: 14px;
  display: none;
}
#stats-bar.visible { display: block; }
.stats-title { font-size: 0.68rem; color: var(--muted); font-weight: 600; margin-bottom: 6px; text-transform: uppercase; letter-spacing: 0.06em; }
.stats-grid { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; }
.stat-item { text-align: center; }
.stat-item .sval { font-size: 0.9rem; font-weight: 700; color: var(--accent); }
.stat-item .slbl { font-size: 0.62rem; color: var(--muted); }

/* ── Map controls ─────────────────────────────────────── */
.leaflet-control-zoom {
  border: none !important;
  box-shadow: var(--shadow) !important;
}
.leaflet-control-zoom a {
  background: var(--panel) !important;
  color: var(--text) !important;
  border-color: var(--border) !important;
}
.leaflet-control-zoom a:hover { background: var(--panel2) !important; }

/* ── Tooltip ──────────────────────────────────────────── */
.commune-tooltip {
  background: var(--panel) !important;
  border: 1px solid var(--border) !important;
  color: var(--text) !important;
  border-radius: 6px !important;
  font-family: var(--font) !important;
  font-size: 0.78rem !important;
  padding: 6px 10px !important;
  box-shadow: var(--shadow) !important;
  white-space: nowrap;
}
.commune-tooltip .tt-name { font-weight: 700; }
.commune-tooltip .tt-val { color: var(--accent); font-weight: 600; }

/* ── Mobile burger ────────────────────────────────────── */
#burger {
  display: none;
  position: fixed;
  top: 12px; right: 12px;
  z-index: 2000;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 1.2rem;
  width: 44px; height: 44px;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow);
}

/* ── Footer ───────────────────────────────────────────── */
#footer {
  padding: 10px 16px;
  background: var(--bg);
  border-top: 1px solid var(--border);
  font-size: 0.62rem;
  color: var(--muted);
  text-align: center;
  line-height: 1.5;
}
#footer a { color: var(--accent); text-decoration: none; }

/* ── Loading overlay ──────────────────────────────────── */
#loading {
  position: fixed;
  inset: 0;
  background: var(--bg);
  z-index: 9999;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  transition: opacity 0.5s ease;
}
#loading.hidden { opacity: 0; pointer-events: none; }
.spinner {
  width: 48px; height: 48px;
  border: 3px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.9s linear infinite;
  margin-bottom: 18px;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-text { font-size: 0.85rem; color: var(--muted); }

/* ── Responsive ───────────────────────────────────────── */
@media (max-width: 720px) {
  :root { --panel-w: min(300px, 88vw); }

  #sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    transform: translateX(-100%);
    z-index: 1500;
  }
  #sidebar.open { transform: translateX(0); }

  #burger { display: flex; }

  #map-container { width: 100%; }

  /* Zoom Leaflet : descendre pour ne pas être masqué par le burger (haut droite) */
  .leaflet-top.leaflet-left {
    top: 12px;
    left: 12px;
  }

  /* Pas de conflit burger/zoom : burger est à droite, zoom reste à gauche */
}

/* Overlay fermeture sidebar */
#overlay-close {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 1400; /* sous sidebar (1500), au-dessus de la carte */
  cursor: pointer;
}
