:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e2e8f0;
  --primary: #0f766e;
  --primary-hover: #115e59;
  --primary-soft: #ccfbf1;
  --danger: #b91c1c;
  --warn-bg: #fef3c7;
  --warn-text: #92400e;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 4px 12px rgba(15, 23, 42, .04);
  --topbar-h: 56px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #111827;
    --surface-2: #1a2333;
    --text: #e5e7eb;
    --muted: #94a3b8;
    --border: #273244;
    --primary: #14b8a6;
    --primary-hover: #2dd4bf;
    --primary-soft: #134e4a;
    --danger: #f87171;
    --warn-bg: #422006;
    --warn-text: #fcd34d;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; height: 100%; }

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.45;
  -webkit-text-size-adjust: 100%;
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 1000;
  height: var(--topbar-h);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 16px;
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-size: 1.3rem; }
.brand strong { color: var(--primary); }
.logo { font-size: 1.5rem; }
.topbar-actions { display: flex; gap: 4px; }

/* ---------- Layout ---------- */
.layout {
  display: grid;
  grid-template-columns: minmax(360px, 440px) 1fr;
  height: calc(100dvh - var(--topbar-h));
}
.panel {
  overflow-y: auto;
  padding: 16px;
  border-right: 1px solid var(--border);
}
.map-wrap { position: relative; }
#map { position: absolute; inset: 0; background: var(--surface-2); }

@media (max-width: 900px) {
  .layout {
    display: flex; flex-direction: column-reverse;
    height: auto;
  }
  /* Carte ancrée dans la page : elle défile avec le contenu. Épinglable via le bouton 📌. */
  .map-wrap {
    height: 42vh; min-height: 260px;
    position: relative; z-index: 500;
    border-bottom: 1px solid var(--border);
    scroll-margin-top: var(--topbar-h);
  }
  .map-wrap.pinned { position: sticky; top: var(--topbar-h); }
  .results { scroll-margin-top: calc(var(--topbar-h) + 8px); }
  .map-pinned .results { scroll-margin-top: calc(var(--topbar-h) + max(42vh, 260px) + 8px); }
  .panel { overflow: visible; border-right: 0; padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
  .hide-sm { display: none; }
}
@media (max-width: 900px) and (max-height: 500px) {
  .map-wrap.pinned { position: relative; top: 0; }
}

/* ---------- Cards / fields ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 12px 14px 14px;
  margin: 0 0 14px;
  min-width: 0;
}
legend, summary {
  font-weight: 600;
  padding: 0 4px;
}
fieldset.card legend { float: left; width: 100%; padding: 0 0 8px; }
fieldset.card legend + * { clear: both; }
.field { margin-bottom: 10px; }
.field:last-child { margin-bottom: 0; }
.field label { display: block; font-size: .85rem; color: var(--muted); margin-bottom: 4px; }
.field output { color: var(--text); font-weight: 600; float: right; }

input[type="text"], input[type="time"], input[type="number"] {
  width: 100%;
  font: inherit;
  font-size: 16px; /* évite le zoom auto sur iOS */
  color: var(--text);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 10px;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 25%, transparent); }
input.invalid { border-color: var(--danger); }
input[type="range"] { width: 100%; accent-color: var(--primary); }
input[type="checkbox"] { accent-color: var(--primary); width: 18px; height: 18px; margin: 0; flex: none; }

.check { display: flex; align-items: center; gap: 8px; cursor: pointer; font-size: .92rem; }

.settings summary { cursor: pointer; list-style: none; }
.settings summary::-webkit-details-marker { display: none; }
.settings summary::after { content: "▾"; float: right; color: var(--muted); }
.settings[open] summary { margin-bottom: 12px; }
.settings[open] summary::after { content: "▴"; }

/* ---------- Person ---------- */
.person {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  margin-bottom: 10px;
  background: var(--surface-2);
}
.person input[type="text"], .person input[type="number"] { background: var(--surface); }
.person-head { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.person-head .p-name { flex: 1; font-weight: 600; }
.person-dot { width: 12px; height: 12px; border-radius: 50%; background: var(--muted); flex: none; }
.person-car { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 8px; flex-wrap: wrap; min-height: 38px; }
.seats { display: flex; align-items: center; gap: 8px; font-size: .85rem; color: var(--muted); }
.seats input { width: 64px; text-align: center; padding: 6px; }
.seats[hidden] { display: none; }

/* ---------- Autocomplete ---------- */
.addr-field { position: relative; }
.addr-field input.located { background-image: linear-gradient(transparent, transparent); border-left: 3px solid var(--primary); }
.suggest {
  position: absolute; left: 0; right: 0; top: 100%; z-index: 2000;
  margin: 4px 0 0; padding: 4px; list-style: none;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .15);
  max-height: 260px; overflow-y: auto;
}
.suggest li { padding: 8px 10px; border-radius: 6px; cursor: pointer; font-size: .9rem; }
.suggest li small { display: block; color: var(--muted); }
.suggest li:hover, .suggest li.active { background: var(--primary-soft); }

/* ---------- Buttons ---------- */
.btn {
  font: inherit; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border-radius: 8px; border: 1px solid transparent;
  padding: 8px 12px; cursor: pointer;
  color: var(--text); background: transparent;
  text-decoration: none;
  transition: background .15s, border-color .15s;
  min-height: 38px;
}
.btn:disabled { opacity: .6; cursor: wait; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
@media (prefers-color-scheme: dark) { .btn-primary { color: #042f2e; } }
.btn-outline { border-color: var(--border); background: var(--surface); }
.btn-outline:hover { border-color: var(--primary); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-block { width: 100%; }
.btn-lg { padding: 12px; font-size: 1.05rem; }
.btn-sm { padding: 5px 9px; font-size: .82rem; min-height: 32px; }
.btn-icon {
  border: 0; background: transparent; color: var(--muted);
  width: 32px; height: 32px; border-radius: 6px; cursor: pointer; font-size: 1rem;
}
.btn-icon:hover { background: var(--border); color: var(--danger); }

.error { color: var(--danger); font-size: .9rem; margin: 10px 0 0; }

/* ---------- Results ---------- */
.results { margin-top: 18px; }
.results h2 { font-size: 1.1rem; margin: 0 0 4px; }
.summary { color: var(--muted); font-size: .9rem; margin: 0 0 12px; }
.notice {
  background: var(--warn-bg); color: var(--warn-text);
  border-radius: 8px; padding: 10px 12px; font-size: .88rem; margin-bottom: 12px;
}
.notice ul { margin: 4px 0 0; padding-left: 18px; }
.info { font-size: .8rem; color: var(--muted); margin-top: 8px; }

.vehicle { border-left: 5px solid var(--vcolor, var(--primary)); padding-top: 10px; }
.vehicle-head { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; flex-wrap: wrap; cursor: pointer; }
.vehicle-head h3 { margin: 0; font-size: 1rem; }
.vehicle-meta { font-size: .85rem; color: var(--muted); }
.vehicle-meta b { color: var(--text); }

.stops { list-style: none; margin: 10px 0; padding: 0; position: relative; }
.stops li { display: grid; grid-template-columns: 22px 1fr auto; gap: 8px; align-items: start; padding: 6px 0; position: relative; }
.stops li:not(:last-child)::before {
  content: ""; position: absolute; left: 10px; top: 24px; bottom: -6px;
  border-left: 2px dashed var(--vcolor, var(--border));
  opacity: .5;
}
.stop-ico {
  width: 22px; height: 22px; border-radius: 50%;
  display: grid; place-items: center;
  font-size: .72rem; font-weight: 700; color: #fff;
  background: var(--vcolor, var(--primary));
  z-index: 1;
}
.stop-name { font-weight: 600; font-size: .92rem; }
.stop-addr { font-size: .8rem; color: var(--muted); overflow-wrap: anywhere; }
.stop-time { font-variant-numeric: tabular-nums; font-weight: 600; font-size: .9rem; text-align: right; }
.stop-time small { display: block; font-weight: 400; color: var(--muted); font-size: .75rem; }
.badge { display: inline-block; font-size: .7rem; padding: 1px 6px; border-radius: 99px; background: var(--warn-bg); color: var(--warn-text); margin-left: 4px; vertical-align: middle; }

.exports { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.legs { margin-top: 8px; font-size: .82rem; }
.legs summary { cursor: pointer; color: var(--muted); font-weight: 500; }
.legs ol { padding-left: 20px; margin: 6px 0 0; }
.legs li { margin-bottom: 4px; }
.legs a { color: var(--primary); }

/* ---------- Map ---------- */
.map-loading {
  position: absolute; inset: 0; z-index: 800;
  display: flex; flex-direction: column; gap: 10px; align-items: center; justify-content: center;
  background: color-mix(in srgb, var(--bg) 70%, transparent);
  font-weight: 600;
}
.map-loading[hidden] { display: none; }
.spinner {
  width: 36px; height: 36px; border-radius: 50%;
  border: 4px solid var(--border); border-top-color: var(--primary);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.pin {
  width: 28px; height: 28px; border-radius: 50% 50% 50% 0;
  transform: rotate(-45deg);
  display: grid; place-items: center;
  border: 2px solid #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, .35);
}
.pin span { transform: rotate(45deg); color: #fff; font-size: 12px; font-weight: 700; }
.pin-dest { background: #111827; }
.leaflet-popup-content { font-size: .9rem; }
@media (prefers-color-scheme: dark) {
  .leaflet-tile-pane { filter: brightness(.8) contrast(1.1) saturate(.8); }
}

.map-pin-btn {
  display: none; place-items: center;
  position: absolute; top: 10px; right: 10px; z-index: 900;
  width: 36px; height: 36px; border-radius: 8px;
  border: 2px solid rgba(0, 0, 0, .2); background: #fff; color: #111;
  font-size: 1.05rem; cursor: pointer;
}
.map-pin-btn[aria-pressed="false"] { filter: grayscale(1); opacity: .75; }
.map-pin-btn[aria-pressed="true"] { background: #ccfbf1; border-color: #0f766e; }
@media (max-width: 900px) and (min-height: 501px) { .map-pin-btn { display: grid; } }

/* ---------- Toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(20px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  z-index: 3000;
  background: var(--text); color: var(--bg);
  padding: 10px 16px; border-radius: 99px; font-size: .9rem;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .25);
  max-width: calc(100vw - 32px);
}
