/* =========================================================================
   DVF Côtes-d'Armor — système de design
   Palette de séries validée (contrastes et séparation daltonienne) pour les
   deux modes ; les rôles ci-dessous sont les seules couleurs employées.
   ========================================================================= */

:root {
  color-scheme: light;
  --plane:        #f9f9f7;
  --surface:      #fcfcfb;
  --surface-2:    #f2f1ec;
  --ink:          #0b0b0b;
  --ink-2:        #52514e;
  --muted:        #898781;
  --grid:         #e1e0d9;
  --axis:         #c3c2b7;
  --border:       rgba(11, 11, 11, .10);
  --shadow:       0 1px 2px rgba(11, 11, 11, .05), 0 8px 24px rgba(11, 11, 11, .05);

  --s1: #2a78d6;  --s2: #eb6834;  --s3: #1baf7a;  --s4: #eda100;
  --s5: #e87ba4;  --s6: #008300;  --s7: #4a3aa7;  --s8: #e34948;

  /* Rampe séquentielle bleue, du clair au foncé (magnitude continue) */
  --r1: #cde2fb; --r2: #9ec5f4; --r3: #6da7ec; --r4: #3987e5;
  --r5: #256abf; --r6: #184f95; --r7: #0d366b;

  --good: #006300;  --bad: #d03b3b;  --warn: #fab219;
  --accent: var(--s1);
  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --plane: #0d0d0d;  --surface: #1a1a19;  --surface-2: #232322;
    --ink: #ffffff;    --ink-2: #c3c2b7;    --muted: #898781;
    --grid: #2c2c2a;   --axis: #383835;
    --border: rgba(255, 255, 255, .10);
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
    --s1: #3987e5;  --s2: #d95926;  --s3: #199e70;  --s4: #c98500;
    --s5: #d55181;  --s6: #008300;  --s7: #9085e9;  --s8: #e66767;
    --r1: #0d366b; --r2: #184f95; --r3: #256abf; --r4: #3987e5;
    --r5: #6da7ec; --r6: #9ec5f4; --r7: #cde2fb;
    --good: #0ca30c;  --bad: #d03b3b;
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --plane: #0d0d0d;  --surface: #1a1a19;  --surface-2: #232322;
  --ink: #ffffff;    --ink-2: #c3c2b7;    --muted: #898781;
  --grid: #2c2c2a;   --axis: #383835;
  --border: rgba(255, 255, 255, .10);
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
  --s1: #3987e5;  --s2: #d95926;  --s3: #199e70;  --s4: #c98500;
  --s5: #d55181;  --s6: #008300;  --s7: #9085e9;  --s8: #e66767;
  --r1: #0d366b; --r2: #184f95; --r3: #256abf; --r4: #3987e5;
  --r5: #6da7ec; --r6: #9ec5f4; --r7: #cde2fb;
  --good: #0ca30c;  --bad: #d03b3b;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  background: var(--plane);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* ---------------------------------------------------------------- entête */
header {
  position: sticky; top: 0; z-index: 900;
  display: flex; align-items: center; gap: 16px;
  padding: 12px 22px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.marque { display: flex; align-items: baseline; gap: 10px; }
.marque h1 { font-size: 16px; font-weight: 650; margin: 0; letter-spacing: -.01em; }
.marque span { font-size: 12px; color: var(--muted); }
.entete-droite { margin-left: auto; display: flex; align-items: center; gap: 8px; }

/* --------------------------------------------------------------- onglets */
nav.onglets {
  position: sticky; top: 53px; z-index: 890;
  display: flex; gap: 2px; padding: 0 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  overflow-x: auto; scrollbar-width: none;
}
nav.onglets::-webkit-scrollbar { display: none; }
nav.onglets button {
  flex: 0 0 auto;
  padding: 10px 14px 9px;
  background: none; border: 0; border-bottom: 2px solid transparent;
  color: var(--ink-2); font: inherit; font-size: 13px; font-weight: 500;
  cursor: pointer; white-space: nowrap;
  transition: color .12s, border-color .12s;
}
nav.onglets button:hover { color: var(--ink); }
nav.onglets button[aria-selected="true"] { color: var(--accent); border-bottom-color: var(--accent); }

/* ------------------------------------------------------- barre de filtres */
.filtres {
  display: flex; flex-wrap: wrap; align-items: flex-end; gap: 10px 14px;
  padding: 12px 22px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.champ { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.champ > label { font-size: 11px; font-weight: 600; color: var(--muted); letter-spacing: .02em; }
.champ-inline { display: flex; align-items: center; gap: 5px; }

input, select, button.btn {
  font: inherit; font-size: 13px;
  padding: 6px 9px;
  color: var(--ink); background: var(--surface);
  border: 1px solid var(--border); border-radius: 7px;
  transition: border-color .12s, box-shadow .12s;
}
input:focus, select:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 18%, transparent); }
input.mini { width: 74px; }
select { cursor: pointer; }

button.btn { cursor: pointer; font-weight: 500; }
button.btn:hover { border-color: var(--accent); color: var(--accent); }
button.btn.primaire { background: var(--accent); border-color: var(--accent); color: #fff; }
button.btn.primaire:hover { filter: brightness(1.08); color: #fff; }
button.icone { padding: 6px 8px; line-height: 1; }

.bascule { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--ink-2); cursor: pointer; user-select: none; }
.bascule input { accent-color: var(--accent); width: 15px; height: 15px; margin: 0; }

/* -------------------------------------------------- sélecteur de communes */
.jetons {
  display: flex; flex-wrap: wrap; align-items: center; gap: 4px;
  min-height: 32px; min-width: 240px; max-width: 460px;
  padding: 3px 6px;
  background: var(--surface); border: 1px solid var(--border); border-radius: 7px;
}
.jeton {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 2px 4px 2px 8px; border-radius: 20px;
  background: var(--surface-2); font-size: 12px;
}
.jeton b { font-weight: 500; }
.jeton button { border: 0; background: none; cursor: pointer; color: var(--muted); font-size: 14px; line-height: 1; padding: 0 2px; }
.jeton button:hover { color: var(--bad); }
.jetons input { border: 0; padding: 3px; min-width: 110px; flex: 1; background: none; }
.jetons input:focus { box-shadow: none; }
.suggestions {
  position: absolute; z-index: 950; max-height: 280px; overflow-y: auto;
  min-width: 240px; margin-top: 2px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; box-shadow: var(--shadow);
}
.suggestions div { padding: 6px 11px; cursor: pointer; font-size: 13px; display: flex; justify-content: space-between; gap: 14px; }
.suggestions div:hover, .suggestions div.actif { background: var(--surface-2); }
.suggestions em { color: var(--muted); font-style: normal; font-size: 11px; font-variant-numeric: tabular-nums; }

/* ----------------------------------------------------------------- pages */
main { padding: 18px 22px 60px; }
.page { display: none; }
.page.actif { display: block; animation: apparait .18s ease-out; }
@keyframes apparait { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.grille { display: grid; gap: 14px; grid-template-columns: repeat(12, 1fr); }
.carte {
  grid-column: span 12;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow);
  min-width: 0;
}
.c3 { grid-column: span 3; } .c4 { grid-column: span 4; }
.c6 { grid-column: span 6; } .c8 { grid-column: span 8; } .c12 { grid-column: span 12; }
@media (max-width: 1100px) { .c3 { grid-column: span 6; } .c4, .c6, .c8 { grid-column: span 12; } }
@media (max-width: 640px)  { .c3 { grid-column: span 12; } }

.carte h2 { margin: 0 0 2px; font-size: 13px; font-weight: 650; letter-spacing: -.005em; }
.carte .sous { margin: 0 0 14px; font-size: 12px; color: var(--muted); }
.entete-carte { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.entete-carte > div:first-child { flex: 1; min-width: 160px; }
.entete-carte .sous { margin: 0; }
.outils { display: flex; gap: 6px; flex-wrap: wrap; }
.outils select, .outils button.btn { padding: 4px 8px; font-size: 12px; }

/* ------------------------------------------------------- tuiles de chiffres */
.tuile .valeur {
  font-size: 30px; font-weight: 620; letter-spacing: -.02em;
  line-height: 1.15; margin: 4px 0 2px;
}
.tuile .etiquette { font-size: 11px; font-weight: 600; color: var(--muted); letter-spacing: .02em; }
.tuile .detail { font-size: 12px; color: var(--ink-2); }
.delta { display: inline-flex; align-items: center; gap: 3px; font-size: 12px; font-weight: 600; }
.delta.hausse { color: var(--good); }
.delta.baisse { color: var(--bad); }

/* Évolutions ventilées par département, dans une tuile de chiffres */
.lignes-dep { margin-top: 8px; display: flex; flex-direction: column; gap: 5px; }
.lignes-dep div {
  display: flex; align-items: baseline; gap: 7px;
  font-size: 12px; color: var(--ink-2);
}
.lignes-dep .nom { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lignes-dep .delta { font-size: 13px; }

/* --------------------------------------------------------------- tableaux */
.enveloppe-table { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 7px 10px; text-align: right; white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
th {
  position: sticky; top: 0;
  font-size: 11px; font-weight: 600; color: var(--muted); letter-spacing: .02em;
  background: var(--surface); border-bottom: 1px solid var(--axis);
  cursor: pointer; user-select: none;
}
th.nontri { cursor: default; }
th:hover:not(.nontri) { color: var(--ink); }
th .fleche { color: var(--accent); }
td { border-bottom: 1px solid var(--grid); font-variant-numeric: tabular-nums; }
tbody tr:hover td { background: var(--surface-2); }
td.texte { font-variant-numeric: normal; white-space: normal; }

/* Cellules du tableau croisé : rampe séquentielle à 7 classes.
   La couleur du texte est fixée par classe et non héritée : sur les pas foncés
   de la rampe, l'encre par défaut tomberait à 1,65:1 de contraste. Les valeurs
   retenues donnent au minimum 5,4:1 dans les deux modes (le sens de la rampe
   s'inverse en mode sombre, donc les seuils aussi). Les couleurs de texte sont
   écrites en dur : elles doivent suivre le fond, pas le thème. */
td.cel { font-weight: 500; text-align: center; }
td.cel-0 { background: var(--r1); } td.cel-1 { background: var(--r2); }
td.cel-2 { background: var(--r3); } td.cel-3 { background: var(--r4); }
td.cel-4 { background: var(--r5); } td.cel-5 { background: var(--r6); }
td.cel-6 { background: var(--r7); }

td.cel-0, td.cel-1, td.cel-2, td.cel-3 { color: #0b0b0b; }
td.cel-4, td.cel-5, td.cel-6 { color: #ffffff; }

:root[data-theme="dark"] td.cel-0,
:root[data-theme="dark"] td.cel-1,
:root[data-theme="dark"] td.cel-2 { color: #ffffff; }
:root[data-theme="dark"] td.cel-3,
:root[data-theme="dark"] td.cel-4,
:root[data-theme="dark"] td.cel-5,
:root[data-theme="dark"] td.cel-6 { color: #0b0b0b; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) td.cel-0,
  :root:not([data-theme="light"]) td.cel-1,
  :root:not([data-theme="light"]) td.cel-2 { color: #ffffff; }
  :root:not([data-theme="light"]) td.cel-3,
  :root:not([data-theme="light"]) td.cel-4,
  :root:not([data-theme="light"]) td.cel-5,
  :root:not([data-theme="light"]) td.cel-6 { color: #0b0b0b; }
}

/* ------------------------------------------------------------- graphiques */
.zone-graph { position: relative; height: 300px; }
.zone-graph.haute { height: 380px; }
.zone-graph.basse { height: 210px; }

.legende { display: flex; flex-wrap: wrap; gap: 6px 16px; margin-top: 12px; font-size: 12px; }
.legende span { display: inline-flex; align-items: center; gap: 6px; color: var(--ink-2); }
.pastille { width: 10px; height: 10px; border-radius: 3px; flex: 0 0 auto; }

/* Barres horizontales (classement) : une seule série, marque fine */
.barres { display: flex; flex-direction: column; gap: 9px; }
.barre-ligne { display: grid; grid-template-columns: 150px 1fr 84px; align-items: center; gap: 10px; font-size: 13px; }
.barre-ligne .nom { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--ink-2); }
/* display:block est indispensable : ces éléments sont des <span>, dont la
   largeur serait ignorée tant qu'ils restent en rendu inline. */
.barre-piste { display: block; height: 9px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.barre-jauge { display: block; height: 100%; background: var(--s1); border-radius: 4px; transition: width .35s cubic-bezier(.4,0,.2,1); }
.barre-ligne .val { text-align: right; font-variant-numeric: tabular-nums; font-weight: 550; }

/* Boîtes à moustaches, tracées en SVG */
.boites svg { width: 100%; display: block; }

/* ------------------------------------------------------------------ carte */
#carte-leaflet { height: 620px; border-radius: var(--radius); z-index: 1; }
.leaflet-container { background: var(--surface-2); font: inherit; }
.echelle-carte { display: flex; align-items: center; gap: 10px; margin-top: 12px; font-size: 12px; color: var(--ink-2); flex-wrap: wrap; }
.echelle-bandes { display: flex; height: 10px; border-radius: 3px; overflow: hidden; width: 220px; }
.echelle-bandes i { flex: 1; }
/* Pastilles de regroupement : encre neutre, elles comptent sans qualifier. */
.grappe-dvf {
  display: flex; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--ink) 72%, transparent);
  color: var(--surface); border: 2px solid var(--surface);
  border-radius: 50%; font-size: 12px; font-weight: 600;
  font-variant-numeric: tabular-nums; box-shadow: 0 1px 4px rgba(0, 0, 0, .28);
}
.grappe-dvf span { line-height: 1; }

.popup-dvf { font-family: var(--font); font-size: 13px; min-width: 190px; }
.popup-dvf b { display: block; font-size: 14px; margin-bottom: 3px; }
.popup-dvf table { font-size: 12px; }
.popup-dvf td { border: 0; padding: 1px 0; }
.popup-dvf td:first-child { color: var(--muted); padding-right: 12px; }

/* ------------------------------------------------------------- estimation */
.resultat-estim { text-align: center; padding: 8px 0 4px; }
.resultat-estim .principal { font-size: 40px; font-weight: 640; letter-spacing: -.025em; line-height: 1.1; }
.resultat-estim .fourchette { font-size: 13px; color: var(--ink-2); margin-top: 6px; }
.jauge-estim { margin: 18px 0 6px; }
.jauge-estim .piste { position: relative; height: 8px; border-radius: 4px; background: linear-gradient(90deg, var(--r2), var(--r4), var(--r2)); }
.jauge-estim .curseur { position: absolute; top: -5px; width: 3px; height: 18px; border-radius: 2px; background: var(--ink); transform: translateX(-50%); }
.jauge-estim .bornes { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); margin-top: 7px; font-variant-numeric: tabular-nums; }

/* ------------------------------------------------- avertissement légal */
.voile-avertissement {
  position: fixed; inset: 0; z-index: 1000;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  background: color-mix(in srgb, #0b0b0b 62%, transparent);
  backdrop-filter: blur(3px);
  overflow-y: auto;
}
.voile-avertissement[hidden] { display: none; }

.boite-avertissement {
  max-width: 620px; width: 100%; margin: auto;
  padding: 26px 30px 24px;
  background: var(--surface); color: var(--ink);
  border: 1px solid var(--border); border-top: 4px solid var(--warn);
  border-radius: var(--radius);
  box-shadow: 0 24px 60px rgba(0, 0, 0, .35);
}
.boite-avertissement h2 { margin: 0 0 12px; font-size: 18px; font-weight: 650; }
.boite-avertissement .chapeau {
  margin: 0 0 14px; font-size: 14px; line-height: 1.6;
  padding: 10px 13px; border-radius: 8px;
  background: color-mix(in srgb, var(--warn) 15%, var(--surface));
}
.boite-avertissement ul { margin: 0 0 14px; padding-left: 20px; }
.boite-avertissement li { margin-bottom: 9px; font-size: 13.5px; line-height: 1.6; color: var(--ink-2); }
.boite-avertissement li b, .boite-avertissement p b { color: var(--ink); }
.boite-avertissement p { margin: 0 0 14px; font-size: 13.5px; line-height: 1.6; color: var(--ink-2); }
.boite-avertissement .mention { font-size: 12px; color: var(--muted); }
.boite-avertissement button { width: 100%; padding: 10px; font-size: 14px; }

/* Bandeau permanent : l'avertissement reste visible après acceptation. */
.pied {
  display: flex; align-items: flex-start; gap: 12px;
  margin: 26px 22px 0; padding: 13px 16px 15px;
  border-top: 2px solid var(--warn);
  background: var(--surface);
  font-size: 12.5px; line-height: 1.6; color: var(--ink-2);
}
.etiquette-pied {
  flex: 0 0 auto; padding: 2px 8px; border-radius: 4px;
  background: color-mix(in srgb, var(--warn) 26%, var(--surface));
  color: var(--ink); font-size: 10.5px; font-weight: 700; letter-spacing: .04em;
}
.pied b { color: var(--ink); }
.lien-pied {
  border: 0; background: none; padding: 0; margin-left: 4px;
  color: var(--accent); font: inherit; text-decoration: underline; cursor: pointer;
}

/* ------------------------------------------------------- états et divers */
.vide, .chargement { padding: 34px 12px; text-align: center; color: var(--muted); font-size: 13px; }
.note {
  display: flex; gap: 9px; padding: 10px 13px; border-radius: 8px;
  background: var(--surface-2); font-size: 12.5px; color: var(--ink-2); line-height: 1.55;
}
.note b { color: var(--ink); font-weight: 600; }
.note.attention { background: color-mix(in srgb, var(--warn) 14%, var(--surface)); }
.pastille-compte {
  display: inline-block; padding: 1px 7px; border-radius: 20px;
  background: var(--surface-2); font-size: 11px; font-weight: 600;
  color: var(--ink-2); font-variant-numeric: tabular-nums;
}
.pagination { display: flex; align-items: center; justify-content: center; gap: 10px; margin-top: 14px; font-size: 13px; color: var(--ink-2); }
.pagination button { padding: 4px 11px; }
.pagination button:disabled { opacity: .4; cursor: not-allowed; }
.pagination button:disabled:hover { border-color: var(--border); color: var(--ink); }

.tourniquet {
  position: fixed; inset: 0 0 auto 0; height: 2px; z-index: 999;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  background-size: 40% 100%; background-repeat: no-repeat;
  animation: glisse 1s linear infinite; display: none;
}
.tourniquet.actif { display: block; }
@keyframes glisse { from { background-position: -40% 0; } to { background-position: 140% 0; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}
