:root {
  /* Palette (design system Stations) */
  --color-primary: #2563EB;
  --color-on-primary: #FFFFFF;
  --color-secondary: #059669;
  --color-accent: #DC2626;
  --color-background: #F0F9FF;
  --color-foreground: #0F172A;
  --color-surface: #FFFFFF;
  --color-muted: #F1F5FD;
  --color-muted-fg: #64748B;
  --color-border: #E4ECFC;
  --color-ring: #2563EB;
  --color-cheap: #059669;
  --color-exp: #DC2626;
  --color-warn: #B45309;       /* ambre foncé (texte) */
  --color-warn-bg: #FEF3C7;    /* ambre clair (fond) */
  --color-warn-border: #FDE68A;

  /* Rayons & ombres */
  --r-sm: 8px;
  --r-md: 12px;
  --r-lg: 16px;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, .06), 0 1px 3px rgba(15, 23, 42, .08);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, .08), 0 2px 4px rgba(15, 23, 42, .05);
  --shadow-lg: 0 12px 32px rgba(15, 23, 42, .14);

  /* Espacement (rythme 8px) */
  --s1: 4px; --s2: 8px; --s3: 12px; --s4: 16px; --s5: 24px; --s6: 32px;

  --t-fast: 150ms cubic-bezier(.4, 0, .2, 1);
  --t-mid: 220ms cubic-bezier(.4, 0, .2, 1);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--color-background);
  color: var(--color-foreground);
  -webkit-font-smoothing: antialiased;
}

#app { display: flex; height: 100vh; height: 100dvh; overflow: hidden; }

button { font-family: inherit; }

/* ---------- Sidebar ---------- */
#sidebar {
  position: relative;
  width: 380px;
  min-width: 380px;
  background: var(--color-surface);
  border-right: 1px solid var(--color-border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  box-shadow: var(--shadow-md);
  z-index: 500;
}

.brand {
  display: flex;
  align-items: center;
  gap: var(--s3);
  padding: var(--s5) var(--s5) var(--s4);
}
.brand-logo {
  width: 40px; height: 40px;
  display: grid; place-items: center;
  background: linear-gradient(135deg, var(--color-primary), #4D7CFF);
  color: #fff;
  border-radius: var(--r-md);
  box-shadow: var(--shadow-sm);
  flex: 0 0 auto;
}
.brand-logo svg { width: 22px; height: 22px; }
.brand h1 { margin: 0; font-size: 20px; font-weight: 700; letter-spacing: -.02em; }
.subtitle { margin: 2px 0 0; color: var(--color-muted-fg); font-size: 12.5px; }

/* ---------- Controls ---------- */
.controls {
  padding: var(--s4) var(--s5);
  display: flex;
  flex-direction: column;
  gap: var(--s3);
  border-bottom: 1px solid var(--color-border);
}
.row { display: flex; gap: var(--s3); }
.row .field { flex: 1; min-width: 0; }
.row button { flex: 1; }

.field { display: flex; flex-direction: column; gap: var(--s1); }
.field > span { font-size: 12px; font-weight: 500; color: var(--color-muted-fg); }

.field select,
.field input {
  width: 100%;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  color: var(--color-foreground);
  padding: 10px 12px;
  border-radius: var(--r-sm);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color var(--t-fast), box-shadow var(--t-fast);
}
.field select { cursor: pointer; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%2364748B' stroke-width='2' stroke-linecap='round'%3E%3Cpath d='m6 9 6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center; padding-right: 32px;
}
.field select:focus,
.field input:focus {
  border-color: var(--color-ring);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .15);
}
.field select:disabled { opacity: .5; cursor: not-allowed; }

.input-wrap { position: relative; display: flex; align-items: center; }
.input-wrap .input-ico {
  position: absolute; left: 10px; width: 16px; height: 16px;
  color: var(--color-muted-fg); pointer-events: none;
}
.input-wrap input { padding-left: 34px; padding-right: 34px; }

.input-wrap .search-clear {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  width: 24px; height: 24px; min-width: 0; padding: 0; gap: 0;
  display: grid; place-items: center;
  background: var(--color-muted); color: var(--color-muted-fg);
  border: none; border-radius: 999px; cursor: pointer; box-shadow: none;
  transition: background var(--t-fast), color var(--t-fast);
}
.input-wrap .search-clear svg { width: 13px; height: 13px; }
.input-wrap .search-clear:hover { background: var(--color-border); color: var(--color-foreground); filter: none; }
.input-wrap .search-clear:active { transform: translateY(-50%) scale(.92); }

/* Autocomplétion BAN */
.suggestions {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0;
  margin: 0; padding: 4px; list-style: none;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--r-md);
  box-shadow: var(--shadow-lg);
  z-index: 1000;
  max-height: 280px; overflow-y: auto;
}
.suggestions li {
  padding: 9px 10px; border-radius: var(--r-sm);
  cursor: pointer; font-size: 13px; line-height: 1.35;
}
.suggestions li .s-label { font-weight: 500; color: var(--color-foreground); }
.suggestions li .s-context { font-size: 11.5px; color: var(--color-muted-fg); margin-top: 1px; }
.suggestions li:hover,
.suggestions li.active { background: var(--color-muted); }
.suggestions li.empty { color: var(--color-muted-fg); cursor: default; font-weight: 400; }
.suggestions li.empty:hover { background: transparent; }

/* Boutons */
.controls button {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: var(--color-primary);
  color: var(--color-on-primary);
  font-weight: 600;
  border: 1px solid transparent;
  padding: 10px 12px;
  border-radius: var(--r-sm);
  cursor: pointer;
  font-size: 13.5px;
  transition: filter var(--t-fast), transform var(--t-fast), box-shadow var(--t-fast);
}
.controls button .ico { width: 16px; height: 16px; }
.controls button:hover { filter: brightness(1.05); box-shadow: var(--shadow-sm); }
.controls button:active { transform: scale(.97); }
.controls button:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(37, 99, 235, .3); }
.controls button:disabled { opacity: .6; cursor: progress; }
.controls button.ghost {
  background: var(--color-surface);
  color: var(--color-foreground);
  border-color: var(--color-border);
}
.controls button.ghost:hover { background: var(--color-muted); }

/* Filtres */
.filters summary {
  display: flex; align-items: center; gap: 6px;
  cursor: pointer; font-size: 13px; font-weight: 500;
  color: var(--color-muted-fg);
  padding: var(--s1) 0; list-style: none;
}
.filters summary::-webkit-details-marker { display: none; }
.filters summary svg { width: 15px; height: 15px; }
.filters[open] summary { color: var(--color-foreground); }
#filters-box {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: var(--s2) var(--s3);
  padding: var(--s3) 0 var(--s1);
}
.chk { display: flex; align-items: center; gap: 7px; font-size: 13px; cursor: pointer; }
.chk input { width: 16px; height: 16px; accent-color: var(--color-primary); cursor: pointer; }

/* ---------- Stats ---------- */
.stats {
  padding: var(--s3) var(--s5);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: var(--s2);
  border-bottom: 1px solid var(--color-border);
}
.stats .stat {
  background: var(--color-muted);
  border-radius: var(--r-sm);
  padding: var(--s2) var(--s1);
  text-align: center;
}
.stats .stat .v {
  font-size: 16px; font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.stats .stat .l { font-size: 11px; color: var(--color-muted-fg); margin-top: 2px; }

/* ---------- Liste ---------- */
.list { flex: 1; overflow-y: auto; padding: var(--s3) var(--s4) var(--s4); }
.list .loading { color: var(--color-muted-fg); text-align: center; padding: var(--s6) 0; }

.station {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--r-md);
  padding: var(--s3) var(--s4);
  margin-bottom: var(--s2);
  cursor: pointer;
  transition: border-color var(--t-fast), box-shadow var(--t-fast), transform var(--t-fast);
}
.station:hover { border-color: var(--color-primary); box-shadow: var(--shadow-sm); }
.station:active { transform: scale(.99); }
.station .top { display: flex; justify-content: space-between; align-items: baseline; gap: var(--s2); }
.station .name { font-weight: 600; font-size: 14px; letter-spacing: -.01em; }
.station .price {
  font-weight: 700; font-size: 16px; white-space: nowrap;
  font-variant-numeric: tabular-nums; color: var(--color-foreground);
}
.station .addr { color: var(--color-muted-fg); font-size: 12px; margin-top: 3px; }
.station .rank { color: var(--color-secondary); font-size: 11px; font-weight: 500; margin-top: 4px; }

/* Badge d'avertissement « prix obsolète » */
.warn-badge {
  display: inline-flex; align-items: center; gap: 3px;
  margin-left: 6px; padding: 1px 6px 1px 4px;
  background: var(--color-warn-bg);
  color: var(--color-warn);
  border: 1px solid var(--color-warn-border);
  border-radius: 999px;
  font-size: 10.5px; font-weight: 600; line-height: 1.4;
  vertical-align: middle; cursor: help;
  white-space: nowrap;
}
.warn-badge svg { width: 11px; height: 11px; }
.warn-badge:focus-visible { outline: 2px solid var(--color-warn); outline-offset: 1px; }

/* Bandeau d'avertissement dans la fiche */
.warn-panel {
  display: flex; align-items: flex-start; gap: 8px;
  margin: 0 0 var(--s4);
  padding: 10px 12px;
  background: var(--color-warn-bg);
  border: 1px solid var(--color-warn-border);
  border-radius: var(--r-md);
  color: var(--color-warn);
  font-size: 12.5px; line-height: 1.45;
}
.warn-panel svg { width: 16px; height: 16px; flex: 0 0 auto; margin-top: 1px; }

/* Marqueur de cellule MAJ obsolète dans la table de prix */
.detail-prices td.maj.stale { color: var(--color-warn); font-weight: 600; }
.warn-inline { display: inline-flex; vertical-align: middle; margin-right: 3px; color: var(--color-warn); }
.warn-inline svg { width: 12px; height: 12px; }

/* ---------- Footer ---------- */
.credits {
  padding: var(--s3) var(--s5);
  font-size: 11px;
  color: var(--color-muted-fg);
  border-top: 1px solid var(--color-border);
  line-height: 1.5;
}
.credits a { color: var(--color-primary); text-decoration: none; }
.credits a:hover { text-decoration: underline; }

/* ---------- Carte ---------- */
#map { flex: 1; height: 100%; background: #dfe7f0; }

.price-marker {
  border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  border: 2px solid #fff;
  box-shadow: var(--shadow-md);
  width: 30px; height: 30px;
  display: flex; align-items: center; justify-content: center;
}
.price-marker span {
  transform: rotate(45deg);
  font-size: 9px; font-weight: 700; color: #fff;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 1px rgba(0,0,0,.25);
}

.search-pin {
  width: 22px; height: 22px; border-radius: 50%;
  background: var(--color-primary);
  border: 3px solid #fff;
  box-shadow: var(--shadow-md);
  position: relative;
}
.search-pin::after {
  content: ""; position: absolute; inset: -8px;
  border-radius: 50%; border: 2px solid var(--color-primary);
  opacity: .4;
}

.leaflet-popup-content-wrapper { border-radius: var(--r-md); }

/* ---------- Fiche station + historique ---------- */
.detail {
  position: absolute;
  inset: 0;
  background: var(--color-surface);
  z-index: 1200;
  overflow-y: auto;
  padding: var(--s5);
  animation: slideIn var(--t-mid);
}
@keyframes slideIn { from { opacity: 0; transform: translateX(12px); } to { opacity: 1; transform: none; } }

.detail-close {
  position: absolute; top: var(--s4); right: var(--s4);
  background: var(--color-muted); color: var(--color-foreground);
  border: 1px solid var(--color-border); border-radius: var(--r-sm);
  width: 34px; height: 34px; cursor: pointer;
  display: grid; place-items: center;
  transition: background var(--t-fast);
}
.detail-close svg { width: 18px; height: 18px; }
.detail-close:hover { background: var(--color-border); }

.detail h2 { margin: 0 40px 4px 0; font-size: 20px; font-weight: 700; letter-spacing: -.02em; }
.detail h3 {
  margin: var(--s5) 0 var(--s2); font-size: 12px; font-weight: 600;
  text-transform: uppercase; letter-spacing: .04em; color: var(--color-muted-fg);
}
.detail-addr { color: var(--color-muted-fg); font-size: 13px; margin: 0 0 var(--s4); line-height: 1.6; }

.detail-prices { width: 100%; border-collapse: collapse; }
.detail-prices td { padding: 9px 4px; border-bottom: 1px solid var(--color-border); font-size: 14px; }
.detail-prices td:first-child { font-weight: 500; }
.detail-prices td.p { font-weight: 700; text-align: right; font-variant-numeric: tabular-nums; }
.detail-prices td.maj { color: var(--color-muted-fg); font-size: 11px; text-align: right; width: 96px; }

.chips { display: flex; flex-wrap: wrap; gap: var(--s2); }
.chip {
  background: var(--color-muted); border: 1px solid var(--color-border);
  border-radius: 999px; padding: 5px 11px; font-size: 12px; color: var(--color-foreground);
}
.chip.on { background: var(--color-secondary); color: #fff; border-color: var(--color-secondary); font-weight: 600; }
.muted { color: var(--color-muted-fg); font-size: 13px; line-height: 1.5; }

.spark {
  margin-bottom: var(--s3);
  background: var(--color-muted);
  border: 1px solid var(--color-border);
  border-radius: var(--r-md);
  padding: var(--s3);
}
.spark-head { display: flex; justify-content: space-between; align-items: baseline; font-size: 11px; color: var(--color-muted-fg); }
.spark-head b { color: var(--color-foreground); font-size: 13px; }
.spark-foot { font-size: 11px; color: var(--color-muted-fg); margin-top: var(--s1); font-variant-numeric: tabular-nums; }

/* ---------- Bascule Liste / Carte (masquée sur PC) ---------- */
.view-switch { display: none; }

/* ---------- Responsive : mobile / tablette ---------- */
@media (max-width: 760px) {
  /* Le panneau de contrôle reste en haut ; la zone liste OU carte occupe
     le reste de l'écran (plein écran), commutée par la bascule du bas. */
  #app { display: block; position: relative; height: 100vh; height: 100dvh; }

  /* La carte est toujours présente, en plein écran, derrière le panneau. */
  #map { position: absolute; inset: 0; height: 100%; z-index: 1; }

  #sidebar {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;   /* vue Liste : plein écran */
    width: 100%; min-width: 0;
    border-right: none;
    border-bottom: 1px solid var(--color-border);
    box-shadow: var(--shadow-md);
    z-index: 600;
  }

  /* Vue Carte : le panneau se réduit aux seuls contrôles, la carte apparaît. */
  body.view-map #sidebar { bottom: auto; }
  body.view-map #stats,
  body.view-map .list,
  body.view-map .credits { display: none; }

  /* En-tête plus compact */
  .brand { padding: var(--s3) var(--s4) var(--s2); }
  .brand-logo { width: 34px; height: 34px; }
  .brand-logo svg { width: 19px; height: 19px; }
  .brand h1 { font-size: 18px; }
  .subtitle { display: none; }

  .controls { padding: var(--s3) var(--s4); gap: var(--s2); }

  /* Cibles tactiles confortables + 16px pour éviter le zoom auto iOS */
  .field select,
  .field input { font-size: 16px; padding: 12px 14px; }
  .field select { padding-right: 36px; }
  .input-wrap input { padding-left: 38px; padding-right: 40px; }
  .input-wrap .search-clear { width: 28px; height: 28px; min-height: 0; right: 8px; padding: 0; }

  .controls button { padding: 13px 12px; font-size: 14px; min-height: 46px; }
  .filters summary { padding: var(--s2) 0; font-size: 14px; }
  .chk { font-size: 14px; padding: 4px 0; }
  .chk input { width: 18px; height: 18px; }

  /* La liste laisse de la place à la bascule flottante */
  .list { padding-bottom: 84px; -webkit-overflow-scrolling: touch; }
  .station { padding: var(--s4); }
  .station .name { font-size: 15px; }
  .station .price { font-size: 17px; }

  /* La fiche station s'affiche en plein écran, quelle que soit la vue */
  .detail { position: fixed; inset: 0; z-index: 2000; padding: var(--s4); }
  .detail-close { width: 40px; height: 40px; }

  /* Bascule Liste / Carte : pilule flottante en bas */
  .view-switch {
    display: flex;
    position: fixed;
    bottom: calc(env(safe-area-inset-bottom, 0px) + 16px);
    left: 50%; transform: translateX(-50%);
    z-index: 1500;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: 999px;
    box-shadow: var(--shadow-lg);
    padding: 4px;
    gap: 2px;
  }
  .view-switch button {
    display: inline-flex; align-items: center; gap: 7px;
    border: none; background: transparent;
    color: var(--color-muted-fg);
    font-family: inherit; font-size: 14px; font-weight: 600;
    padding: 9px 18px; border-radius: 999px;
    cursor: pointer; min-height: 40px;
    transition: background var(--t-fast), color var(--t-fast);
  }
  .view-switch button svg { width: 17px; height: 17px; }
  .view-switch button.active {
    background: var(--color-primary);
    color: var(--color-on-primary);
    box-shadow: var(--shadow-sm);
  }
  /* La bascule est masquée quand la fiche plein écran est ouverte */
  body.detail-open .view-switch { display: none; }

  /* Contrôles Leaflet un peu plus gros pour le tactile */
  .leaflet-bar a { width: 34px !important; height: 34px !important; line-height: 34px !important; font-size: 20px !important; }
}

/* Très petits écrans : empile carburant / tri */
@media (max-width: 380px) {
  .controls .row:first-child { flex-direction: column; }
}

/* ---------- Accessibilité : mouvement réduit ---------- */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
