/* BigMap — front-end stylesheet.
   Mobile first: the map takes the top, the settings the bottom, within thumb
   reach. On a wide screen the panel moves left to free up the map. */

:root {
  --green: #12603c;
  --green-light: #e7f2ec;
  --grey-excluded: #d6d8db;
  --text: #1b1d1f;
  --muted: #5f666c;
  --border: #e6e8ea;
  --panel-height: 48vh;
  --radius: 10px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0; height: 100%; overflow: hidden;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--text); background: #eceef0;
  -webkit-text-size-adjust: 100%;
}

#map { position: absolute; inset: 0 0 var(--panel-height) 0; }
body.panel-collapsed { --panel-height: 84px; }

/* ------------------------------------------------------------------ panel */
#panel {
  position: absolute; left: 0; right: 0; bottom: 0; height: var(--panel-height);
  background: #fff; border-radius: var(--radius) var(--radius) 0 0;
  box-shadow: 0 -2px 16px rgba(0, 0, 0, .16);
  display: flex; flex-direction: column; overflow: hidden;
  transition: height .2s ease;
}

#summary {
  padding: 2px 14px 9px; border-bottom: 1px solid var(--border);
  display: flex; flex-direction: column; gap: 7px;
}
.grip {
  align-self: center;
  width: 44px; height: 18px; border: 0; background: none; padding: 0;
  cursor: pointer;
}
.grip::before {
  content: ''; display: block; width: 38px; height: 4px; margin: 7px auto;
  border-radius: 2px; background: #cdd2d6;
}
.count { display: flex; align-items: baseline; gap: 7px; }
#count {
  font-size: 22px; font-weight: 650; color: var(--green);
  font-variant-numeric: tabular-nums;
}
#count-caption { font-size: 13px; color: var(--muted); line-height: 1.25; }

.actions { display: flex; gap: 6px; }
.actions button { flex: 1; }
.actions button {
  font: inherit; font-size: 13px; padding: 8px 12px; border-radius: 8px;
  border: 1px solid var(--green); background: var(--green); color: #fff;
  cursor: pointer; touch-action: manipulation;
}
.actions button.subdued { background: #fff; color: var(--green); }

/* --------------------------------------------------------------- criteria */
#criteria {
  flex: 1 1 auto; min-height: 0;
  overflow-y: auto; padding: 2px 14px 18px;
  -webkit-overflow-scrolling: touch;
  padding-bottom: calc(18px + env(safe-area-inset-bottom));
}

/* The twenty-seven criteria are grouped by family: laid flat, the sheet became
   a wall. A folded family costs one line, and its counter says what it holds
   active without having to open it. */
.family { border-bottom: 1px solid #e6e8ea; }
.family:last-child { border-bottom: 0; }

.family-header {
  display: flex; align-items: center; gap: 10px; width: 100%;
  padding: 13px 2px; background: none; border: 0; cursor: pointer;
  font: inherit; font-weight: 600; color: inherit; text-align: left;
}
.family-title { flex: 1; }
.family-count { font-size: 12px; font-weight: 500; color: var(--green); }
.family:not(.has-active) .family-title { font-weight: 500; color: #55606a; }

.chevron {
  width: 8px; height: 8px; flex: none; margin-left: 2px;
  border-right: 2px solid var(--muted); border-bottom: 2px solid var(--muted);
  transform: rotate(-45deg); transition: transform .18s ease;
}
.family.open .chevron { transform: rotate(45deg); }

.family-criteria { display: none; padding-bottom: 6px; }
.family.open .family-criteria { display: block; }

.criterion { padding: 10px 0; border-bottom: 1px solid #f1f2f3; }
.criterion:last-child { border-bottom: 0; }

/* The header wraps rather than clips. `white-space: nowrap` on the threshold
   was written for "≤ 30 km"; the density grid reads "Ceinture urbaine – Rural à
   habitat très dispersé", which was cut off mid-word — and a scale spelled out
   in words exists precisely so it can be read. */
.criterion-header {
  display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap;
}
.switch { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.switch input {
  width: 22px; height: 22px; accent-color: var(--green); flex: none; margin: 0;
}
.criterion-name { font-size: 14px; }
.threshold {
  font-size: 13px; color: var(--green); font-weight: 600;
  font-variant-numeric: tabular-nums;
  margin-left: auto; text-align: right;
}

.settings { margin-top: 4px; }

/* Sliders drawn by hand rather than left to `accent-color`.
   The native thumb is ~16 px: on a phone, a finger misses it. Here it is 26 px,
   inside a 36 px tall touch area — height is what rescues the most missed
   gestures, not diameter. The price is repainting the travelled portion
   ourselves, which `accent-color` gave for free: hence --fill, kept up to date
   by refreshPanel(). */
.criterion input[type=range] {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 36px; margin: 0; background: none; cursor: pointer;
}

.criterion input[type=range]::-webkit-slider-runnable-track {
  height: 6px; border-radius: 3px;
  background: linear-gradient(to right, var(--green) var(--fill, 0%),
                              #e3e6e8 var(--fill, 0%));
}
.criterion input[type=range]::-moz-range-track {
  height: 6px; border-radius: 3px;
  background: linear-gradient(to right, var(--green) var(--fill, 0%),
                              #e3e6e8 var(--fill, 0%));
}

.criterion input[type=range]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 26px; height: 26px; margin-top: -10px; border-radius: 50%;
  background: var(--green); border: 3px solid #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, .3);
}
.criterion input[type=range]::-moz-range-thumb {
  width: 26px; height: 26px; border-radius: 50%; border: 3px solid #fff;
  background: var(--green); box-shadow: 0 1px 4px rgba(0, 0, 0, .3);
}

.criterion input[type=range]:focus-visible { outline: none; }
.criterion input[type=range]:focus-visible::-webkit-slider-thumb {
  box-shadow: 0 1px 4px rgba(0, 0, 0, .3), 0 0 0 4px var(--green-light);
}
.criterion input[type=range]:focus-visible::-moz-range-thumb {
  box-shadow: 0 1px 4px rgba(0, 0, 0, .3), 0 0 0 4px var(--green-light);
}

/* A range is ONE rail carrying TWO thumbs.
   It used to be two half-width sliders side by side, each spanning the whole
   scale: the gesture read as two unrelated controls, and the precision was
   halved exactly where it was needed — aiming at two million inhabitants on
   a half rail landed on the last notch, which is above the largest commune.

   The technique: both inputs stretched over the same box, transparent, with
   pointer events off — only their thumbs take the finger. The rail and the
   selected segment are painted by the container, which is the only element
   that knows both bounds. */
.sliders { position: relative; height: 36px; --low: 0%; --high: 100%; }

.sliders::before {
  content: ''; position: absolute; left: 0; right: 0; top: 15px; height: 6px;
  border-radius: 3px;
  background: linear-gradient(to right,
    #e3e6e8 var(--low), var(--green) var(--low),
    var(--green) var(--high), #e3e6e8 var(--high));
}

.sliders input[type=range] {
  position: absolute; left: 0; top: 0; width: 100%;
  pointer-events: none;          /* the rail itself is not a target */
}
.sliders input[type=range]::-webkit-slider-runnable-track { background: none; }
.sliders input[type=range]::-moz-range-track { background: none; }
/* …but the thumbs are, otherwise nothing could be dragged at all. */
.sliders input[type=range]::-webkit-slider-thumb { pointer-events: auto; }
.sliders input[type=range]::-moz-range-thumb { pointer-events: auto; }

.roles {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap; margin-top: 2px;
}
.segments { display: inline-flex; border: 1px solid var(--border); border-radius: 7px; overflow: hidden; }
.segments button {
  font: inherit; font-size: 12px; padding: 5px 10px; border: 0;
  background: #fff; color: var(--muted); cursor: pointer;
}
.segments button.chosen { background: var(--green-light); color: var(--green); font-weight: 600; }

/* The weight applies to both roles: an eliminating criterion also ranks the
   communes it lets through. */
.weight { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); }
/* Wide enough for the five steps to stay reachable with the enlarged thumb:
   five positions across 88 px left no room. */
.weight input { width: 116px; }
.weight-value { color: var(--green); letter-spacing: -1px; min-width: 44px; }

/* A criterion that is off stays legible but recedes visually. */
.criterion.inactive .criterion-name, .criterion.inactive .threshold { opacity: .45; }
.criterion.inactive .settings { opacity: .4; pointer-events: none; }

/* --------------------------------------------------------- drawers, alerts */
#perf {
  position: absolute; top: 8px; right: 8px; z-index: 3;
  background: rgba(20, 22, 25, .72); color: #fff; border-radius: 6px;
  padding: 3px 8px; font-size: 11px; font-variant-numeric: tabular-nums;
}

#error, #message {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%);
  z-index: 20; margin: 0; padding: 10px 14px; border-radius: 8px;
  font-size: 13px; max-width: min(92vw, 520px); text-align: center;
  box-shadow: 0 2px 10px rgba(0, 0, 0, .2);
}
#error { background: #fdecea; color: #8c1d18; border: 1px solid #f3b8b2; }
#message { background: #1b1d1f; color: #fff; }

#sources {
  position: absolute; inset: 12px; z-index: 30; overflow: auto;
  background: #fff; border-radius: var(--radius); padding: 16px 18px;
  box-shadow: 0 4px 24px rgba(0, 0, 0, .25); font-size: 13.5px; line-height: 1.5;
}
#sources h2 { margin: 0 0 6px; font-size: 17px; }
#sources h3 { margin: 18px 0 6px; font-size: 14px; }
#sources table { border-collapse: collapse; width: 100%; margin-top: 10px; }
#sources th { text-align: left; font-weight: 600; padding: 6px 8px 2px 0; vertical-align: top; }
#sources td { padding: 6px 0 2px; color: var(--muted); }
#sources td.complete { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
#sources tr.method td { padding: 0 0 8px; font-size: 12px; border-bottom: 1px solid var(--border); }
#sources .datasets { padding-left: 18px; }
#sources .datasets .date { color: var(--muted); }
.close {
  float: right; border: 0; background: none; font-size: 24px; line-height: 1;
  color: var(--muted); cursor: pointer; padding: 0 4px;
}

/* ------------------------------------------------------------------ fiche
   Carte flottante plutôt que bulle ancrée au clic : une bulle déborde sous le
   panneau et se retrouve tronquée dès que la commune touchée est en bas. */
#sheet {
  position: absolute; top: 10px; left: 10px; right: 10px; z-index: 15;
  max-height: calc(100% - 20px - var(--panel-height)); overflow: auto;
  background: #fff; border-radius: var(--radius); padding: 12px 14px;
  font-size: 13px; box-shadow: 0 3px 18px rgba(0, 0, 0, .22);
}
#sheet h2 { margin: 0 0 2px; font-size: 16px; padding-right: 28px; }
#sheet .subtitle { margin: 0 0 8px; color: var(--muted); font-size: 12px; }
#sheet table { border-collapse: collapse; width: 100%; }
#sheet th {
  text-align: left; font-weight: 400; padding: 3px 8px 3px 0; color: var(--text);
}
#sheet td {
  text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap;
}
#sheet .absent { color: #98a0a6; font-size: 12px; }
.badge { display: inline-block; padding: 1px 7px; border-radius: 20px; font-size: 11px; }
.badge.kept { background: var(--green-light); color: var(--green); }
.badge.excluded { background: #f1f2f3; color: var(--muted); }
.warning {
  margin: 6px 0; padding: 6px 8px; background: #fff8e6; border-radius: 6px;
  font-size: 12px; color: #7a5b00;
}
.provenance { margin: 10px 0 0; font-size: 11.5px; color: var(--muted); }
.loading { margin: 8px 0 0; font-size: 12px; color: var(--muted); font-style: italic; }

/* ------------------------------------------------------------ wide screen */
@media (min-width: 900px) {
  :root { --panel-height: 0px; }
  #map { inset: 0 0 0 380px; }
  #panel {
    top: 0; right: auto; width: 380px; height: 100%;
    border-radius: 0; box-shadow: 2px 0 16px rgba(0, 0, 0, .12);
  }
  .grip { display: none; }
  body.panel-collapsed #map { inset: 0; }
  body.panel-collapsed #panel { width: 0; }
  #sources { inset: 24px 24px 24px 404px; }
}

/* -------------------------------------------------- tabs and results list */
#tabs {
  display: flex; gap: 4px; padding: 0 14px; border-bottom: 1px solid var(--border);
  flex: none;
}
#tabs button {
  font: inherit; font-size: 14px; font-weight: 600; padding: 10px 4px;
  margin-right: 16px; background: none; border: 0; cursor: pointer;
  color: var(--muted); border-bottom: 2px solid transparent;
}
#tabs button[aria-selected=true] { color: var(--green); border-bottom-color: var(--green); }

#results {
  flex: 1 1 auto; min-height: 0;
  overflow-y: auto; padding: 12px 14px 18px;
  padding-bottom: calc(18px + env(safe-area-inset-bottom));
  -webkit-overflow-scrolling: touch;
}

/* The search covers the whole of France, not the current view: you look up a
   commune you know without knowing where it is on screen. */
.search { position: relative; }
#search {
  width: 100%; font: inherit; font-size: 15px; padding: 10px 12px;
  border: 1px solid var(--border); border-radius: 9px; background: #fff;
}
#search:focus { outline: 2px solid var(--green-light); border-color: var(--green); }

#suggestions {
  position: absolute; z-index: 5; left: 0; right: 0; top: calc(100% + 4px);
  margin: 0; padding: 4px 0; list-style: none; max-height: 46vh; overflow-y: auto;
  background: #fff; border: 1px solid var(--border); border-radius: 9px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .14);
}
#suggestions li {
  display: flex; align-items: baseline; gap: 8px;
  padding: 9px 12px; cursor: pointer;
}
#suggestions li:hover, #suggestions li:active { background: var(--green-light); }
#suggestions small, .commune-name small { color: var(--muted); font-size: 12px; }

#list-scope { margin: 14px 0 4px; font-size: 12.5px; color: var(--muted); }
#list-hint { margin: 0 0 8px; font-size: 12px; color: var(--muted); opacity: .8; }

#ranking { margin: 0; padding: 0; list-style: none; }
#ranking li {
  display: flex; align-items: baseline; gap: 10px;
  padding: 10px 2px; border-bottom: 1px solid #f1f2f3; cursor: pointer;
}
#ranking li:active { background: var(--green-light); }
/* The picked row takes the blue of the outline drawn on the map: it is the
   same object pointed at in two places, it must carry the same colour. */
#ranking li.picked {
  background: #eaf1fe; box-shadow: inset 3px 0 0 #1d6ff2;
}
#ranking li.picked .commune-name { font-weight: 600; }
.rank {
  min-width: 26px; font-size: 12px; color: var(--muted);
  font-variant-numeric: tabular-nums;
}
.commune-name { flex: 1; display: flex; align-items: baseline; gap: 7px; }
/* The score shown is the raw one, the same that colours the map: the list and
   the colour must tell the same story. */
.score {
  font-weight: 700; color: var(--green); font-variant-numeric: tabular-nums;
  font-size: 15px;
}
