/* Réinitialisation simple : on enlève les marges par défaut du navigateur */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* La page occupe toute la hauteur de la fenêtre */
html,
body {
  height: 100%;
  font-family: Arial, Helvetica, sans-serif;
}

/* La carte occupe tout l'écran (plein écran) */
#map {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
}

/* Barre de recherche d'une commune, flottante à côté du zoom Leaflet */
#recherche-ville {
  position: absolute;
  top: 10px;
  left: 52px;
  z-index: 1100;
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: calc(100vw - 64px);
  background: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
  padding: 6px;
}

#recherche-ville #champ-recherche {
  width: 280px;
  max-width: calc(100vw - 190px);
  padding: 8px 10px;
  font-size: 14px;
  border: 1px solid #ccc;
  border-radius: 6px;
}

#recherche-ville #champ-recherche.erreur {
  border-color: #c0392b;
}

#recherche-ville #btn-recherche {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 38px;
  height: 38px;
  padding: 0;
  border: none;
  border-radius: 6px;
  background: #2980b9;
  color: #ffffff;
  cursor: pointer;
}

#recherche-ville #btn-recherche:hover {
  background: #2471a3;
}

#recherche-ville #recherche-message {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin-top: 4px;
  padding: 6px 10px;
  background: #ffffff;
  border-radius: 6px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
  font-size: 12px;
  color: #c0392b;
}

#suggestions-recherche {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  z-index: 1200;
  max-height: 280px;
  overflow-y: auto;
  background: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.28);
  padding: 4px;
}

.suggestion-recherche {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  min-height: 40px;
  padding: 8px 10px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: #ffffff;
  cursor: pointer;
  text-align: left;
}

.suggestion-recherche:hover,
.suggestion-recherche:focus,
.suggestion-recherche.suggestion-active {
  background: #eef6fc;
  outline: none;
}

.suggestion-nom {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: #2c3e50;
  font-size: 14px;
  font-weight: 600;
}

.suggestion-meta {
  flex: none;
  color: #7f8c8d;
  font-size: 12px;
  white-space: nowrap;
}

#btn-controles-mobile,
#btn-recherche-mobile,
#btn-recentrer-mobile {
  display: none;
}

.icone-bouton {
  width: 20px;
  height: 20px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.25;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.texte-visuellement-cache {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Panneau flottant en haut à droite, au-dessus de la carte */
#panneau {
  display: none; /* panneau masqué : on ne l'affiche plus */
  position: absolute;
  top: 15px;
  right: 15px;
  z-index: 1000; /* au-dessus de la carte */
  width: 240px;
  background: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
  padding: 16px;
}

/* Titre du panneau */
#panneau h1 {
  font-size: 18px;
  margin-bottom: 12px;
  color: #ffffff;
}

/* Texte du compteur de zones */
#panneau .compteur {
  font-size: 14px;
  margin-bottom: 12px;
  color: #34495e;
}

/* Le compteur lui-même en gras */
#panneau .compteur span {
  font-weight: bold;
}

/* Boutons du panneau */
#panneau button {
  display: block;
  width: 100%;
  padding: 8px 10px;
  margin-bottom: 8px;
  border: none;
  border-radius: 6px;
  background: #2980b9;
  color: #ffffff;
  font-size: 14px;
  cursor: pointer;
}

/* Couleur au survol des boutons */
#panneau button:hover {
  background: #2471a3;
}

/* On enlève la marge du dernier bouton */
#panneau button:last-of-type {
  margin-bottom: 0;
}

/* Barre d'outils de dessin (Leaflet.draw) masquée : on ne la voit plus */
.leaflet-draw {
  display: none;
}

/* Étiquettes des noms de villes (en français) */
.label-ville {
  /* Texte plus petit que les régions, et de couleur plus sombre */
  font-size: 11px;
  font-weight: normal;
  color: #2c3e50;
  white-space: nowrap; /* le nom reste sur une seule ligne */
  /* Halo blanc pour la lisibilité */
  text-shadow: none;
  width: auto !important;
  height: auto !important;
  /* Alignement vertical du texte avec le point */
  display: flex;
  align-items: center;
}

.label-ville * {
  color: inherit;
}

.leaflet-marker-icon.label-ville {
  color: #ffffff !important;
}

/* Petit point rond affiché juste avant le nom de la ville.
   La couleur ET la taille réelles sont définies en ligne (voir
   habillerPastille() dans app.js) : la couleur selon le niveau de sécurité,
   la taille selon la population et le zoom. Les valeurs ci-dessous ne sont
   qu'un repli pour les communes sans score. */
.point-ville {
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: 4px;
  background: #7f8c8d; /* gris neutre (repli) */
  border-radius: 50%; /* cercle */
  /* Petit contour blanc pour bien ressortir */
  box-shadow: 0 0 0 1px #fff;
  /* Grossissement fluide lors des changements de zoom */
  transition: width 0.15s ease, height 0.15s ease;
}

/* Pastille d'une commune disposant d'un détail : cliquable pour ouvrir le
   panneau. Le calque des labels ignore les clics (pointer-events:none) ;
   on les réactive uniquement sur cette pastille. */
.point-ville-cliquable {
  pointer-events: auto;
  cursor: pointer;
}
.point-ville-cliquable:hover {
  box-shadow: 0 0 0 2px #fff;
}

/* Titre (nom de la commune) en tête du panneau de détail d'une ville. */
.detail-titre-ville {
  font-size: 15px;
  font-weight: 600;
  color: #2c3e50;
  margin-bottom: 6px;
}

/* Épingle posée sur la commune trouvée par la recherche.
   On dessine une goutte (pin) dont la pointe est en bas. */
.epingle-recherche {
  width: 24px !important;
  height: 24px !important;
}

.epingle-recherche span {
  display: block;
  width: 20px;
  height: 20px;
  margin: 0 auto;
  background: #2563eb;
  border: 2px solid #fff;
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.4);
}

/* =============================================================
   Carte de sécurité : voxels, contrôles, légende et détail
   ============================================================= */

/* Le canvas qui dessine les carrés colorés.
   On le laisse transparent aux clics (gérés par la carte). */
.couche-voxels {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
}

/* Barre de contrôle en haut à droite */
#controles-securite {
  position: absolute;
  top: 15px;
  right: 15px;
  z-index: 1000;
  width: 220px;
  background: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
  padding: 14px;
}

#controles-securite h2 {
  font-size: 16px;
  margin-bottom: 10px;
  color: #2c3e50;
}

#controles-securite label {
  display: block;
  font-size: 12px;
  color: #34495e;
  margin-bottom: 4px;
}

#controles-securite select {
  width: 100%;
  padding: 6px;
  font-size: 14px;
  border: 1px solid #ccc;
  border-radius: 6px;
  margin-bottom: 12px;
}

/* Cases pour activer/désactiver les malus */
#controles-malus {
  margin-bottom: 4px;
}

#controles-securite label.controle-toggle {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #34495e;
  margin-bottom: 6px;
  cursor: pointer;
}

#controles-securite label.controle-toggle input {
  margin-right: 8px;
  flex-shrink: 0;
  cursor: pointer;
}

/* Sélecteur de scénario temporel (présent / +5 ans / +10 ans) */
#controles-scenario {
  margin: 8px 0 4px;
  border-top: 1px solid #eee;
  padding-top: 8px;
}

.boutons-scenario {
  display: flex;
  gap: 4px;
}

.btn-scenario {
  flex: 1;
  font-size: 11px;
  padding: 5px 4px;
  border: 1px solid #d0d7de;
  background: #f6f8fa;
  color: #34495e;
  border-radius: 4px;
  cursor: pointer;
}

.btn-scenario:hover {
  background: #eaeef2;
}

.btn-scenario.actif {
  background: #34495e;
  border-color: #34495e;
  color: #fff;
}

#controles-mode {
  margin-top: 6px;
}

.btn-mode {
  flex: 1;
  font-size: 11px;
  padding: 5px 4px;
  border: 1px solid #d0d7de;
  background: #f6f8fa;
  color: #34495e;
  border-radius: 4px;
  cursor: pointer;
}

.btn-mode:hover {
  background: #eaeef2;
}

.btn-mode.actif {
  background: #b06a00;
  border-color: #b06a00;
  color: #fff;
}

.note-scenario {
  margin: 6px 0 0;
  font-size: 10px;
  color: #b06a00;
  line-height: 1.3;
}

/* Légende : une ligne par couleur */
#legende {
  border-top: 1px solid #eee;
  padding-top: 10px;
}

.legende-ligne {
  display: flex;
  align-items: center;
  font-size: 12px;
  color: #34495e;
  margin-bottom: 5px;
}

.legende-pastille {
  display: inline-block;
  width: 16px;
  height: 16px;
  border-radius: 3px;
  margin-right: 8px;
  flex-shrink: 0;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.15);
}

.source {
  margin-top: 8px;
  font-size: 10px;
  color: #95a5a6;
  line-height: 1.3;
}

/* Panneau de détail au clic sur un carré (en bas à gauche) */
#detail-voxel {
  position: absolute;
  bottom: 20px;
  left: 15px;
  z-index: 1000;
  width: 260px;
  background: #ffffff;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.3);
  padding: 14px 16px;
}

#detail-voxel h3 {
  font-size: 15px;
  color: #2c3e50;
  margin-bottom: 8px;
  padding-right: 18px;
}

#btn-fermer-detail {
  position: absolute;
  top: 8px;
  right: 10px;
  border: none;
  background: none;
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  color: #7f8c8d;
}

#btn-fermer-detail:hover {
  color: #2c3e50;
}

/* En-tête du détail : niveau de risque + score */
.detail-niveau {
  display: flex;
  align-items: center;
  font-size: 13px;
  font-weight: bold;
  margin-bottom: 8px;
}

.detail-niveau .legende-pastille {
  width: 18px;
  height: 18px;
}

.detail-info {
  font-size: 12px;
  color: #34495e;
  margin-bottom: 8px;
}

/* Tableau des indicateurs */
.detail-indic {
  width: 100%;
  border-collapse: collapse;
  font-size: 11px;
}

.detail-indic td {
  padding: 2px 0;
  color: #34495e;
}

.detail-indic td:last-child {
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: #2c3e50;
  font-weight: bold;
}

/* Ligne d'un malus désactivé via les cases à cocher */
.detail-indic tr.detail-malus-inactif td {
  opacity: 0.45;
  text-decoration: line-through;
}

/* Bloc équation de sécurité */
.detail-equation td {
  padding: 3px 0;
}

.detail-equation tr.detail-equation-total td {
  border-top: 1px solid #d0d7de;
  padding-top: 5px;
  font-weight: bold;
  color: #1a1a1a;
}

/* Pastille de niveau d'un indicateur */
.detail-indic td.indic-valeur {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
}

.indic-niveau {
  font-size: 10px;
  font-weight: 600;
  padding: 1px 6px;
  border-radius: 10px;
  color: #fff;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  /* Largeur fixe pour que tous les badges s'alignent verticalement,
     quelle que soit la longueur du libellé ou du nombre à droite. */
  min-width: 52px;
  box-sizing: border-box;
  text-align: center;
  flex: none;
}

/* Le nombre occupe une colonne de largeur fixe, alignée à droite : il ne
   repousse donc jamais le badge, qui reste aligné d'une ligne à l'autre. */
.indic-chiffre {
  min-width: 24px;
  text-align: right;
  flex: none;
}

/* Emplacement de badge vide (indicateur sans seuil) : invisible mais garde
   la largeur pour préserver l'alignement du nombre. */
.indic-niveau.indic-vide {
  background: transparent;
}

.indic-niveau.indic-faible {
  background: #1a9850;
}

.indic-niveau.indic-moyen {
  background: #f08000;
}

.indic-niveau.indic-eleve {
  background: #d73027;
}

@media (max-width: 700px) {
  html,
  body {
    min-height: 100%;
    overflow: hidden;
  }

  #map {
    position: fixed;
    inset: 0;
  }

  .leaflet-control-zoom {
    display: none;
  }

  #btn-controles-mobile,
  #btn-recherche-mobile,
  #btn-recentrer-mobile {
    position: fixed;
    top: 10px;
    z-index: 1200;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    padding: 0;
    border: none;
    border-radius: 8px;
    background: #2980b9;
    color: #ffffff;
    font-size: 14px;
    font-weight: 600;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
  }

  #btn-recentrer-mobile {
    left: 10px;
  }

  #btn-controles-mobile {
    right: 10px;
  }

  #btn-recherche-mobile {
    right: 64px;
  }

  body.recherche-ouverte #btn-recherche-mobile,
  body.controles-ouverts #btn-controles-mobile {
    background: #2471a3;
  }

  #recherche-ville {
    position: fixed;
    top: 66px;
    left: 10px;
    right: 10px;
    z-index: 1100;
    width: auto;
    max-width: none;
    align-items: stretch;
    gap: 6px;
    padding: 6px;
    transform: translateY(-8px);
    opacity: 0;
    pointer-events: none;
    transition: transform 0.18s ease, opacity 0.18s ease;
  }

  body.recherche-ouverte #recherche-ville {
    transform: translateY(0);
    opacity: 1;
    pointer-events: auto;
  }

  #suggestions-recherche {
    max-height: min(42dvh, 320px);
  }

  .suggestion-recherche {
    min-height: 46px;
  }

  #recherche-ville #champ-recherche {
    width: auto;
    min-width: 0;
    max-width: none;
    flex: 1;
    min-height: 44px;
    font-size: 16px;
  }

  #recherche-ville #btn-recherche {
    width: 44px;
    height: 44px;
    min-height: 44px;
  }

  #controles-securite {
    position: fixed;
    top: 122px;
    right: 10px;
    z-index: 1150;
    width: min(92vw, 340px);
    max-height: calc(100dvh - 142px);
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 14px;
    transform: translateX(calc(100% + 18px));
    opacity: 0;
    pointer-events: none;
    transition: transform 0.18s ease, opacity 0.18s ease;
  }

  body.controles-ouverts #controles-securite {
    transform: translateX(0);
    opacity: 1;
    pointer-events: auto;
  }

  #controles-securite h2 {
    font-size: 17px;
  }

  #controles-securite label.controle-toggle {
    min-height: 38px;
    margin-bottom: 2px;
    font-size: 14px;
  }

  #controles-securite label.controle-toggle input {
    width: 18px;
    height: 18px;
  }

  .boutons-scenario {
    gap: 6px;
  }

  .btn-scenario,
  .btn-mode {
    min-height: 44px;
    padding: 8px 6px;
    font-size: 12px;
  }

  .legende-ligne {
    min-height: 28px;
    font-size: 13px;
  }

  .source,
  .note-scenario {
    font-size: 11px;
  }

  #detail-voxel {
    position: fixed;
    right: 10px;
    bottom: 10px;
    left: 10px;
    z-index: 1120;
    width: auto;
    max-height: min(48dvh, 390px);
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 14px 16px 16px;
  }

  #detail-voxel h3 {
    min-height: 32px;
    padding-right: 46px;
    font-size: 16px;
  }

  #btn-fermer-detail {
    top: 6px;
    right: 8px;
    width: 44px;
    height: 44px;
    font-size: 28px;
  }

  .detail-niveau {
    font-size: 14px;
  }

  .detail-info {
    font-size: 13px;
  }

  .detail-indic {
    font-size: 12px;
  }

  .detail-indic td {
    padding: 4px 0;
  }
}

@media (max-width: 420px) {
  #recherche-ville {
    top: 64px;
  }

  #recherche-ville #btn-recherche {
    max-width: none;
  }

  #controles-securite {
    top: 120px;
    right: 8px;
    width: calc(100vw - 16px);
    max-height: calc(100dvh - 140px);
  }

  #detail-voxel {
    right: 8px;
    bottom: 8px;
    left: 8px;
  }
}
