:root {
  --bg: #f4f7f5;
  --surface: #ffffff;
  --surface-2: #eef3f0;
  --text: #13201a;
  --muted: #5b6b63;
  --border: #dce5df;
  --accent: #15803d;
  --accent-strong: #166534;
  --accent-soft: #dcfce7;
  --e85: #0e7490;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --danger: #b91c1c;
  --shadow: 0 1px 2px rgb(16 40 28 / 6%), 0 4px 16px rgb(16 40 28 / 6%);
  --radius: 16px;
  --chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1.5 1.5 6 6l4.5-4.5' fill='none' stroke='%2315803d' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1411;
    --surface: #13201b;
    --surface-2: #1a2a23;
    --text: #e6efe9;
    --muted: #93a69c;
    --border: #25372f;
    --accent: #4ade80;
    --accent-strong: #86efac;
    --accent-soft: #14301f;
    --e85: #22d3ee;
    --warn: #fbbf24;
    --warn-soft: #3a2e10;
    --danger: #f87171;
    --shadow: none;
    --chevron: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 8'%3E%3Cpath d='M1.5 1.5 6 6l4.5-4.5' fill='none' stroke='%234ade80' stroke-width='1.8' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

.app-header,
main {
  max-width: 560px;
  margin: 0 auto;
  padding: 0 16px;
}

.app-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-top: 20px;
  padding-bottom: 8px;
}
.app-header img { border-radius: 10px; flex: none; }
.app-header h1 { margin: 0; font-size: 1.4rem; letter-spacing: -0.01em; }
.app-header p { margin: 0; color: var(--muted); font-size: 0.875rem; }

main { padding-bottom: 32px; }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-top: 14px;
}

h2 {
  font-size: 1rem;
  margin: 0 0 12px;
}
h2 small { color: var(--muted); font-weight: 500; }

.grid { display: grid; gap: 10px; }
.grid.prices { grid-template-columns: repeat(4, 1fr); }
.grid.two { grid-template-columns: 1fr 1fr; }
.grid.three { grid-template-columns: repeat(3, 1fr); }
/* Une colonne par champ affiché : litres de chaque carburant, puis montant. */
.grid.fill-amounts { grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); }
.grid.fill-when { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); margin-top: 12px; }
.grid.fill-when + .hint { display: block; margin-top: 6px; }
@media (max-width: 420px) {
  .grid.prices { grid-template-columns: 1fr 1fr; }
}

.field { display: flex; flex-direction: column; gap: 4px; margin-top: 12px; }
.grid .field { margin-top: 0; }
.field > span, .limit-head > span { font-size: 0.8125rem; color: var(--muted); font-weight: 500; }

input[type="text"],
input[type="date"],
.field select {
  width: 100%;
  min-width: 0;
  font: inherit;
  font-size: 1.0625rem;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  outline: none;
}
.field select { font-size: 1rem; }
/* Safari iOS centre la date et réduit la hauteur du champ natif. */
input[type="date"] { -webkit-appearance: none; appearance: none; min-height: 2.875rem; font-size: 1rem; }
input[type="date"]::-webkit-date-and-time-value { text-align: left; }
input[type="text"]:focus, input[type="date"]:focus, .field select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input[type="text"]:disabled { opacity: 0.6; }
.field-e85 input { border-color: color-mix(in srgb, var(--e85) 45%, var(--border)); }

.suffix { position: relative; }
.suffix input { padding-right: 34px; }
.suffix em {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  font-style: normal;
  color: var(--muted);
  pointer-events: none;
}

.hint { display: block; color: var(--muted); font-size: 0.8125rem; margin: 8px 0 0; }

.gauge-buttons { display: flex; gap: 8px; margin-top: 10px; }
.segmented { display: flex; gap: 8px; }
.gauge-buttons button,
.segmented button {
  flex: 1;
  font: inherit;
  font-size: 0.875rem;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 0;
  cursor: pointer;
}
.gauge-buttons button:active { background: var(--accent-soft); }
.segmented button[aria-pressed="true"] { color: var(--accent-strong); background: var(--accent-soft); border-color: transparent; font-weight: 600; }
#amount-wrap { margin-top: 6px; }

.limit-head { display: flex; justify-content: space-between; align-items: baseline; }
.limit-head output { font-weight: 700; font-size: 1.125rem; color: var(--accent); font-variant-numeric: tabular-nums; }
input[type="range"] { width: 100%; accent-color: var(--accent); margin: 6px 0 2px; }
input[type="range"]:disabled { opacity: 0.4; }

.check { display: flex; align-items: center; gap: 8px; font-size: 0.9375rem; margin: 6px 0; cursor: pointer; }
.check input { width: 18px; height: 18px; accent-color: var(--accent); margin: 0; }

/* Résultat */
.result { border: 2px solid var(--accent); }
.result.empty { border: 1px dashed var(--border); color: var(--muted); text-align: center; }
.result.empty p { margin: 4px 0; }
.result-head { display: flex; flex-direction: column; align-items: flex-start; gap: 6px; margin-bottom: 10px; }
.result-head h2 { font-size: 1.25rem; margin: 0; }
.badge {
  font-size: 0.75rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--accent-strong);
  background: var(--accent-soft);
  border-radius: 999px;
  padding: 2px 10px;
}

.steps { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; counter-reset: step; }
.steps li {
  counter-increment: step;
  display: grid;
  grid-template-columns: 28px 1fr auto;
  align-items: center;
  gap: 10px;
  background: var(--surface-2);
  border-radius: 12px;
  padding: 10px 12px;
}
.steps li::before {
  content: counter(step);
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--surface);
  font-weight: 700;
  font-size: 0.875rem;
}
.steps li.is-e85::before { background: var(--e85); }
.steps strong { display: block; font-size: 1.0625rem; }
.steps small { color: var(--muted); font-size: 0.8125rem; }
.steps .amount { font-size: 1.25rem; font-weight: 700; font-variant-numeric: tabular-nums; text-align: right; }
.steps .amount small { display: block; font-weight: 500; }

.ethanol { margin: 14px 0 4px; }
.ethanol p { margin: 6px 0 0; font-size: 0.875rem; color: var(--muted); }
.ethanol-bar { position: relative; height: 10px; border-radius: 999px; background: var(--surface-2); overflow: hidden; }
.ethanol-bar > div { height: 100%; background: linear-gradient(90deg, var(--accent), var(--e85)); border-radius: 999px; }
.ethanol-bar > i { position: absolute; top: 0; bottom: 0; width: 2px; background: var(--text); opacity: 0.55; }
.ethanol.over .ethanol-bar > div { background: var(--danger); }
.ethanol.over p { color: var(--danger); }

.figures { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 14px 0; }
.figures div { background: var(--surface-2); border-radius: 12px; padding: 8px 10px; }
.figures dt { font-size: 0.75rem; color: var(--muted); }
.figures dd { margin: 0; font-weight: 700; font-size: 1.0625rem; font-variant-numeric: tabular-nums; }
.figures .highlight { background: var(--accent-soft); }
.figures .highlight dd { color: var(--accent-strong); }

.warning, .tip { border-radius: 12px; padding: 10px 12px; font-size: 0.875rem; margin: 0 0 10px; }
.warning { background: var(--warn-soft); color: var(--warn); }
.tip { background: var(--surface-2); color: var(--muted); }

button.primary {
  width: 100%;
  font: inherit;
  font-weight: 600;
  color: #fff;
  background: #15803d;
  border: 0;
  border-radius: 12px;
  padding: 12px;
  cursor: pointer;
}
button.primary:active { background: #166534; }

/* Comparatif */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 0.875rem; font-variant-numeric: tabular-nums; }
th { text-align: right; color: var(--muted); font-weight: 500; font-size: 0.75rem; padding: 0 4px 6px; }
th:first-child, td:first-child { text-align: left; }
td { text-align: right; padding: 8px 4px; border-top: 1px solid var(--border); white-space: nowrap; }
td:first-child { white-space: normal; }
td small { display: block; color: var(--muted); }
#compare tr { cursor: pointer; }
#compare tr:hover td, #compare tr:focus-visible td { background: var(--surface-2); }
#compare tr:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
tr.is-best td { font-weight: 700; }
tr.is-best td:first-child { color: var(--accent); }
tr.is-over td { color: var(--danger); }
.gain-pos { color: var(--accent); }
.gain-neg { color: var(--danger); }

/* Réglages */
details > summary { list-style: none; cursor: pointer; display: flex; justify-content: space-between; align-items: center; }
details > summary::-webkit-details-marker { display: none; }
details > summary h2 { margin: 0; }
details > summary::after { content: "›"; font-size: 1.4rem; color: var(--muted); transform: rotate(90deg); transition: transform 0.15s; }
details[open] > summary::after { transform: rotate(-90deg); }
details[open] > summary { margin-bottom: 4px; }
details .grid { margin-top: 12px; }

/* Historique */
.history-head { display: flex; justify-content: space-between; align-items: baseline; }
.history-head h2 { margin: 0; }
button.link, a.link { font: inherit; font-size: 0.875rem; color: var(--muted); background: none; border: 0; cursor: pointer; text-decoration: underline; padding: 0; }
.total-row { display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 8px; margin: 8px 0; }
.total { margin: 0; font-size: 0.9375rem; }
.total strong { color: var(--accent); }
.history { list-style: none; margin: 0; padding: 0; font-size: 0.875rem; }
.history li { border-top: 1px solid var(--border); }
.history-item {
  width: 100%;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 0;
  font: inherit;
  color: inherit;
  text-align: left;
  background: none;
  border: 0;
  cursor: pointer;
}
.history-item small { display: block; color: var(--muted); }
.history-item small.conso-line { color: var(--e85); }
.history-item > span:last-child { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }

.conso .figures { margin: 10px 0 0; }
.conso .figures div { display: flex; flex-direction: column; justify-content: space-between; }
.conso .figures dd small { font-weight: 500; font-size: 0.75rem; color: var(--muted); }
.conso .hint { margin-bottom: 10px; }

/* Dépenses par mois : une colonne par mois, la barre part de la ligne de base. */
.monthly { margin: 4px 0 12px; }
.monthly h3 { margin: 0 0 6px; font-size: 0.8125rem; font-weight: 500; color: var(--muted); }
.months { display: flex; }
.month {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  font: inherit;
  color: var(--muted);
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}
.month-plot {
  width: 100%;
  height: 112px;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  gap: 4px;
  border-bottom: 1px solid var(--border);
}
.month-bar { width: 24px; max-width: 70%; border-radius: 4px 4px 0 0; background: color-mix(in srgb, var(--accent) 40%, var(--surface)); }
.month-value { font-size: 0.75rem; font-weight: 600; color: var(--text); white-space: nowrap; font-variant-numeric: tabular-nums; }
.month-label { font-size: 0.75rem; margin-top: 4px; }
.month[aria-pressed="true"] .month-bar { background: var(--accent); }
.month[aria-pressed="true"] .month-label { color: var(--text); font-weight: 600; }
#month-detail { margin-top: 6px; }

.disclaimer { color: var(--muted); font-size: 0.8125rem; margin: 18px 4px 0; }

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 0.875rem;
  width: max-content;
  max-width: calc(100vw - 32px);
  text-align: center;
  box-shadow: 0 6px 20px rgb(0 0 0 / 20%);
}

/* Stations proches */
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; }
.card-head h2 { margin: 0; white-space: nowrap; }
button.pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font: inherit;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--accent-strong);
  background: var(--accent-soft);
  border: 0;
  border-radius: 999px;
  padding: 7px 12px;
  cursor: pointer;
  white-space: nowrap;
}
button.pill:disabled { opacity: 0.6; cursor: progress; }

.selected-station {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-top: 10px;
  padding: 8px 12px;
  border-radius: 12px;
  background: var(--accent-soft);
  font-size: 0.875rem;
}
.selected-station strong { display: block; }
.selected-name { flex: 1; min-width: 0; }
.fav-toggle {
  flex: none;
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  margin: -6px -4px -6px -8px;
  color: var(--accent-strong);
  background: none;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
}
.fav-toggle svg { fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; }
.fav-toggle[aria-pressed="true"] svg { fill: currentColor; }

.favorites { margin-top: 12px; }
.favorites h3 { margin: 0 0 6px; font-size: 0.8125rem; font-weight: 500; color: var(--muted); }
.favorites .stations { margin: 0; }
.selected-station small { color: var(--muted); }
.selected-station.stale small { color: var(--warn); }
.selected-actions { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; white-space: nowrap; }

.station-search { display: grid; grid-template-columns: 1fr auto auto; gap: 8px; }
.station-search input,
.station-search select {
  min-width: 0;
  font: inherit;
  font-size: 1rem;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 10px;
}

/* Sélecteurs aux couleurs du thème, plutôt que l'apparence native (grise sur iOS). */
.field select,
.station-search select {
  -webkit-appearance: none;
  appearance: none;
  font-weight: 600;
  color: var(--accent-strong);
  border-color: color-mix(in srgb, var(--accent) 35%, transparent);
  background-image: var(--chevron);
  background-repeat: no-repeat;
  background-position: right 12px center;
  background-size: 12px 8px;
  padding-right: 34px;
  cursor: pointer;
  outline: none;
}
.station-search select:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
select option { color: var(--text); background: var(--surface); }
.station-search input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.sort { display: flex; gap: 6px; margin-top: 10px; }
.sort button {
  font: inherit;
  font-size: 0.8125rem;
  color: var(--muted);
  background: none;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 4px 10px;
  cursor: pointer;
}
.sort button[aria-pressed="true"] { color: var(--accent-strong); background: var(--accent-soft); border-color: transparent; font-weight: 600; }

#stations-status.error { color: var(--danger); }

.stations { list-style: none; margin: 8px 0 0; padding: 0; display: grid; gap: 8px; }
/* Itinéraire à côté de la station : un lien ne peut pas être dans le bouton. */
.stations li { display: flex; gap: 6px; }
.stations li .station { flex: 1; min-width: 0; }
.station-route {
  flex: none;
  width: 40px;
  display: grid;
  place-items: center;
  color: var(--accent-strong);
  background: var(--surface-2);
  border-radius: 12px;
}
.station-route:active { background: var(--accent-soft); }
.station {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: center;
  text-align: left;
  font: inherit;
  color: var(--text);
  background: var(--surface-2);
  border: 2px solid transparent;
  border-radius: 12px;
  padding: 10px 12px;
  cursor: pointer;
}
.station.is-selected { border-color: var(--accent); }
.station-main { min-width: 0; }
.station-main strong { display: block; font-size: 0.9375rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.station-meta { display: block; color: var(--muted); font-size: 0.8125rem; }
.station-meta.stale { color: var(--warn); }
.chips { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.chip { font-size: 0.75rem; font-variant-numeric: tabular-nums; background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 1px 6px; }
.chip-e85 { color: var(--e85); border-color: color-mix(in srgb, var(--e85) 40%, var(--border)); }
.station-cost { text-align: right; font-variant-numeric: tabular-nums; }
.station-cost strong { display: block; font-size: 1.0625rem; }
.station-cost small { display: block; color: var(--muted); font-size: 0.75rem; white-space: nowrap; }
.station.is-cheapest .station-cost strong { color: var(--accent); }
.station.is-cheapest .station-cost strong::before { content: "★ "; font-size: 0.8em; }
button.more { display: block; margin: 10px auto 0; }

/* Version et nouveautés */
.app-footer { text-align: center; color: var(--muted); font-size: 0.8125rem; margin-top: 16px; }

.sheet {
  width: min(480px, calc(100vw - 32px));
  max-height: min(80vh, 640px);
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
}
.sheet::backdrop { background: rgb(0 0 0 / 40%); }
.changelog section { padding: 10px 0; border-top: 1px solid var(--border); }
.changelog h3 { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; margin: 0 0 4px; font-size: 0.9375rem; }
.changelog h3 small { color: var(--muted); font-weight: 400; font-size: 0.8125rem; }
.changelog section.is-current h3 span { color: var(--accent); }
.changelog ul { margin: 0; padding-left: 18px; font-size: 0.875rem; }
.changelog li + li { margin-top: 2px; }

.notice h2 { margin: 0 0 8px; }
.notice ul { margin: 0; padding-left: 18px; font-size: 0.875rem; }
.notice li + li { margin-top: 6px; }
.notice button.primary { margin-top: 14px; }

.toast.clickable { cursor: pointer; }

/* Partage des économies */
.share #share-preview { margin: -4px 0 12px; white-space: pre-line; }
.share-targets { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.share-targets a, .share-targets button {
  font: inherit;
  font-size: 0.9375rem;
  font-weight: 600;
  text-align: center;
  text-decoration: none;
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 11px 8px;
  cursor: pointer;
}
.share-targets a:active, .share-targets button:active { background: var(--accent-soft); }

/* Enregistrement d'un plein */
.sheet form > .hint:first-of-type { margin: -4px 0 0; }
#fill-option-field:not([hidden]) + .hint { margin-top: 8px; }
.sheet .grid { margin-top: 12px; }
.sheet .check { margin-top: 12px; }
.sheet button.primary { margin-top: 14px; }
button.link.danger { display: block; margin: 12px auto 0; color: var(--danger); }

/* Tirer pour rafraîchir (appli installée) */
html.has-ptr,
html.has-ptr body { overscroll-behavior-y: none; }
.ptr {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 8px);
  left: 50%;
  z-index: 20;
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  color: var(--muted);
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: 0 4px 14px rgb(0 0 0 / 15%);
  opacity: 0;
  transform: translate(-50%, -56px);
  pointer-events: none;
}
.ptr.animating { transition: transform 0.2s ease, opacity 0.2s ease; }
.ptr.ready { color: var(--accent); }
.ptr.spinning svg { display: none; }
.ptr.spinning::after {
  content: "";
  width: 18px;
  height: 18px;
  border: 2.5px solid var(--accent-soft);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: ptr-spin 0.7s linear infinite;
}
@keyframes ptr-spin { to { transform: rotate(360deg); } }
