/* ── R6 — CSS Design Tokens ──────────────────────────────────── */

:root {
  /* Colors */
  --color-bg-primary: #ffffff;
  --color-bg-surface: rgba(255, 255, 255, 0.96);
  --color-bg-hover: #f1f5f9;
  --color-bg-muted: #f8fafc;
  --color-text-primary: #1e293b;
  --color-text-secondary: #64748b;
  --color-text-muted: #94a3b8;
  --color-text-body: #374151;
  --color-accent: #1D4ED8;
  --color-accent-light: #3b82f6;
  --color-accent-bg: #eff6ff;
  --color-border: #e2e8f0;
  --color-border-light: #f1f5f9;
  --color-error: #dc2626;
  --color-success: #16a34a;
  --color-warning: #f59e0b;

  /* Spacing */
  --space-xs: 4px;
  --space-sm: 8px;
  --space-md: 12px;
  --space-lg: 16px;
  --space-xl: 24px;
  --space-2xl: 32px;

  /* Border radius */
  --radius-sm: 4px;
  --radius-md: 6px;
  --radius-lg: 8px;
  --radius-xl: 10px;
  --radius-2xl: 12px;
  --radius-full: 9999px;

  /* Typography */
  --font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-size-xs: 10px;
  --font-size-sm: 11px;
  --font-size-base: 13px;
  --font-size-md: 14px;
  --font-size-lg: 16px;
  --font-size-xl: 22px;

  /* Shadows */
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.15);
  --shadow-md: 0 2px 14px rgba(0, 0, 0, 0.18);
  --shadow-lg: 0 4px 20px rgba(0, 0, 0, 0.25);

  /* MapLibre popup */
  --popup-bg: var(--color-bg-primary);
  --popup-text: var(--color-text-primary);
}

/* ── R4 — Dark mode tokens ──────────────────────────────────── */

[data-theme="dark"] {
  --color-bg-primary: #0f172a;
  --color-bg-surface: rgba(30, 41, 59, 0.96);
  --color-bg-hover: #334155;
  --color-bg-muted: #1e293b;
  --color-text-primary: #f1f5f9;
  --color-text-secondary: #94a3b8;
  --color-text-muted: #64748b;
  --color-text-body: #cbd5e1;
  --color-accent-bg: #1e293b;
  --color-border: #334155;
  --color-border-light: #1e293b;
  --color-error: #f87171;
  --color-success: #34d399;
  --color-warning: #fbbf24;
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.4);
  --shadow-md: 0 2px 14px rgba(0, 0, 0, 0.5);
  --shadow-lg: 0 4px 20px rgba(0, 0, 0, 0.6);
  --popup-bg: #1e293b;
  --popup-text: #f1f5f9;
}

/* ── Reset ───────────────────────────────────────────────────── */

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  height: 100%;
  font-family: var(--font-family);
  background: var(--color-bg-primary);
  color: var(--color-text-primary);
}

#map {
  height: 100vh;
  width: 100vw;
}

/* ── R2 — Loading overlay ────────────────────────────────────── */

#loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  background: linear-gradient(135deg, #0f172a 0%, #1e3a5f 50%, #0f2444 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.5s ease;
}

#loading-overlay.fade-out {
  opacity: 0;
  pointer-events: none;
}

.loading-content {
  text-align: center;
  color: white;
  max-width: 320px;
  width: 90%;
}

.loading-icon {
  font-size: 48px;
  margin-bottom: var(--space-lg);
  animation: loading-pulse 1.5s ease-in-out infinite;
}

@keyframes loading-pulse {
  0%, 100% { transform: scale(1); opacity: 1; }
  50% { transform: scale(1.1); opacity: 0.8; }
}

.loading-title {
  font-size: 20px;
  font-weight: 700;
  margin-bottom: var(--space-xl);
  letter-spacing: -0.3px;
}

.loading-step {
  font-size: var(--font-size-base);
  opacity: 0.7;
  margin-bottom: var(--space-md);
  min-height: 18px;
}

.loading-bar-track {
  width: 100%;
  height: 4px;
  background: rgba(255, 255, 255, 0.15);
  border-radius: var(--radius-full);
  overflow: hidden;
}

.loading-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #3b82f6, #60a5fa);
  border-radius: var(--radius-full);
  transition: width 0.3s ease;
}

/* ── Panel (top-left) ──────────────────────────────────────── */

#panel {
  position: absolute;
  top: 10px;
  left: 0;
  z-index: 1000;
  background: var(--color-bg-surface);
  border-radius: 0 var(--radius-xl) var(--radius-xl) 0;
  padding: var(--space-md) 14px;
  box-shadow: var(--shadow-md);
  min-width: 210px;
}

.panel-title {
  font-size: var(--font-size-lg);
  font-weight: 700;
  color: var(--color-text-primary);
  margin-bottom: 2px;
}

.panel-sub {
  font-size: var(--font-size-base);
  color: var(--color-text-secondary);
  margin-bottom: 2px;
}

/* Drive mode — shift normal panel to the right of the drive sidebar */
.drive-mode-active #panel {
  left: calc(25vw + 6px);
}

/* D-2 — data freshness indicator */
#data-freshness {
  font-size: 12px;
  color: var(--color-text-muted);
  margin-bottom: var(--space-sm);
  min-height: 13px;
}

/* ── Route planning ─────────────────────────────────────────── */

.panel-divider {
  border: none;
  border-top: 1px solid var(--color-border);
  margin: 10px 0;
}

.route-label {
  font-size: var(--font-size-sm);
  font-weight: 700;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 6px;
}

.route-input {
  display: block;
  width: 100%;
  padding: var(--space-md) 14px;
  border: 1.5px solid var(--color-border);
  border-radius: var(--radius-md);
  font-size: var(--font-size-lg);
  outline: none;
  box-sizing: border-box;
  margin-bottom: 5px;
  font-family: inherit;
  color: var(--color-text-primary);
  background: var(--color-bg-primary);
}
.route-input:focus { border-color: var(--color-accent-light); }
.route-input::placeholder { color: var(--color-text-muted); }

.route-actions {
  display: flex;
  gap: 5px;
  margin-bottom: var(--space-xs);
}

.route-btn {
  flex: 1;
  padding: 5px 10px;
  border: none;
  border-radius: var(--radius-md);
  cursor: pointer;
  font-size: var(--font-size-md);
  min-height: 44px;
  min-width: 44px;
  font-weight: 600;
  background: var(--color-accent);
  color: white;
  transition: opacity 0.15s;
  font-family: inherit;
}
.route-btn:hover:not(:disabled) { opacity: 0.85; }
.route-btn:disabled { opacity: 0.5; cursor: default; }

.route-btn-clear {
  flex: none;
  background: var(--color-bg-hover);
  color: var(--color-text-secondary);
}
.route-btn-clear:hover:not(:disabled) { background: var(--color-border); opacity: 1; }

/* F-5 — share button */
.route-btn-share {
  flex: none;
  background: var(--color-bg-hover);
  color: var(--color-text-secondary);
  min-width: 44px;
  min-height: 44px;
  font-size: var(--font-size-md);
}
.route-btn-share:hover:not(:disabled) { background: var(--color-border); opacity: 1; }

.route-stat     { font-size: var(--font-size-sm); color: var(--color-accent); font-weight: 600; }
.route-error    { font-size: var(--font-size-md); color: var(--color-error); }
.route-progress { font-size: var(--font-size-sm); color: var(--color-text-secondary); }

/* Route calculation progress bar */
.route-progress-bar {
  width: 100%;
  height: 4px;
  background: var(--color-border);
  border-radius: var(--radius-full);
  margin-top: var(--space-xs);
  overflow: hidden;
}
.route-progress-bar-fill {
  height: 100%;
  background: var(--color-accent);
  border-radius: var(--radius-full);
  transition: width 0.15s ease-out;
  width: 0%;
}

/* F-9a — network recommendation */
#network-recommendation {
  margin-top: 6px;
  padding: 6px var(--space-sm);
  background: var(--color-accent-bg);
  border: 1px solid #bae6fd;
  border-radius: var(--radius-md);
  font-size: var(--font-size-base);
  color: #0c4a6e;
  line-height: 1.4;
}
[data-theme="dark"] #network-recommendation {
  background: #1e293b;
  border-color: #334155;
  color: #93c5fd;
}
#network-recommendation .net-rec-title {
  font-weight: 600;
  margin-bottom: 2px;
}
#network-recommendation .net-rec-item {
  display: flex;
  align-items: center;
  gap: 5px;
  margin-top: 2px;
}
#network-recommendation .net-rec-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
#network-recommendation .net-rec-score {
  color: var(--color-text-secondary);
  font-size: 12px;
}

/* ── Legend (bottom-right) ─────────────────────────────────── */

#legend {
  position: absolute;
  bottom: 28px;
  right: 10px;
  z-index: 1000;
  background: var(--color-bg-surface);
  border-radius: var(--radius-xl);
  padding: var(--space-md) 14px;
  box-shadow: var(--shadow-md);
}

.legend-title {
  font-size: var(--font-size-md);
  font-weight: 700;
  color: var(--color-text-primary);
  margin-bottom: var(--space-sm);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.legend-chevron {
  display: none;
  font-size: var(--font-size-sm);
  margin-left: var(--space-xs);
  transition: transform 0.2s;
}

.legend-item {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  margin-bottom: 5px;
  font-size: var(--font-size-base);
  color: var(--color-text-body);
  min-width: 170px;
}

.legend-name { flex: 1; }

.legend-count {
  font-size: var(--font-size-sm);
  font-weight: 700;
  color: var(--color-text-muted);
  min-width: 20px;
  text-align: right;
}

.legend-dot {
  width: 11px;
  height: 11px;
  border-radius: 50%;
  border: 1.5px solid rgba(0, 0, 0, 0.15);
  flex-shrink: 0;
}
[data-theme="dark"] .legend-dot {
  border-color: rgba(255, 255, 255, 0.2);
}

.legend-hint {
  font-size: var(--font-size-sm);
  color: #9ca3af;
  margin-top: 9px;
  text-align: center;
  font-style: italic;
}

.legend-dot-cheap {
  background: transparent !important;
  border: 2px dashed;
}


/* ── Filter checkbox (panel) ───────────────────────────────── */

.filter-check {
  display: flex;
  align-items: center;
  gap: 7px;
  font-size: 12px;
  color: var(--color-text-body);
  cursor: pointer;
  user-select: none;
}

.filter-check input[type="checkbox"] {
  width: 14px;
  height: 14px;
  cursor: pointer;
  accent-color: var(--color-accent);
  flex-shrink: 0;
}

/* ── R8 — Popup redesign ──────────────────────────────────── */

.maplibregl-popup-content {
  background: var(--popup-bg) !important;
  color: var(--popup-text) !important;
  border-radius: var(--radius-xl) !important;
  padding: var(--space-lg) !important;
  box-shadow: var(--shadow-lg) !important;
}

.maplibregl-popup-tip {
  border-top-color: var(--popup-bg) !important;
}

.popup-card {
  min-width: 240px;
}

.popup-card-header {
  display: flex;
  align-items: center;
  gap: var(--space-sm);
  flex-wrap: wrap;
  margin-bottom: var(--space-sm);
}

.popup-station,
.popup-card-name {
  font-size: 17px;
  font-weight: 700;
  color: var(--popup-text);
  line-height: 1.3;
  margin-bottom: 2px;
}

.popup-card-addr {
  font-size: var(--font-size-base);
  color: var(--color-text-secondary);
  margin-bottom: var(--space-sm);
  line-height: 1.3;
}

.popup-operator-badge {
  display: inline-block;
  font-size: var(--font-size-base);
  font-weight: 600;
  color: white;
  padding: 3px 10px;
  border-radius: var(--radius-full);
  white-space: nowrap;
}

.popup-card-badges {
  display: flex;
  gap: var(--space-xs);
  flex-wrap: wrap;
}

.popup-badge {
  display: inline-block;
  font-size: var(--font-size-sm);
  font-weight: 600;
  color: var(--color-text-secondary);
  background: var(--color-bg-muted);
  padding: 2px var(--space-sm);
  border-radius: var(--radius-full);
  white-space: nowrap;
  border: 1px solid var(--color-border);
}

.popup-grid {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: var(--space-xs) 10px;
  font-size: var(--font-size-md);
  color: var(--color-text-body);
  margin-top: var(--space-sm);
}

.popup-label { color: var(--color-text-muted); white-space: nowrap; }

.popup-power {
  font-size: var(--font-size-lg);
  font-weight: 800;
  color: var(--popup-text);
}

.popup-avail-cell {
  display: flex;
  align-items: center;
  gap: 5px;
}

.popup-avail { color: var(--color-text-muted); }
.popup-avail-ok   { font-weight: 700; color: var(--color-success); }
.popup-avail-none { font-weight: 700; color: var(--color-error); }
.popup-avail-err  { font-size: var(--font-size-sm); color: var(--color-warning); cursor: help; }

@keyframes avail-spin {
  to { transform: rotate(360deg); }
}
.popup-avail-spinner {
  display: inline-block;
  animation: avail-spin 0.9s linear infinite;
}

.popup-avail-refresh {
  background: none;
  border: none;
  cursor: pointer;
  font-size: var(--font-size-base);
  color: #cbd5e1;
  padding: var(--space-sm);
  min-width: 44px;
  min-height: 44px;
  line-height: 1;
  transition: color 0.15s;
  flex-shrink: 0;
}
.popup-avail-refresh:hover { color: var(--color-accent-light); }

.maplibregl-popup-close-button {
  width: 36px !important;
  height: 36px !important;
  font-size: 24px !important;
  line-height: 36px !important;
  color: var(--color-text-muted) !important;
}

.popup-cheap-badge {
  display: inline-block;
  font-size: var(--font-size-sm);
  font-weight: 600;
  color: #065f46;
  background: #d1fae5;
  padding: 2px var(--space-sm);
  border-radius: var(--radius-2xl);
  margin-left: var(--space-xs);
}
[data-theme="dark"] .popup-cheap-badge {
  color: #34d399;
  background: #064e3b;
}

/* ── Autocomplete ──────────────────────────────────────────── */

.autocomplete-wrap {
  position: relative;
  margin-bottom: 5px;
}

.autocomplete-wrap .route-input {
  margin-bottom: 0;
}

.autocomplete-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: var(--color-bg-primary);
  border: 1.5px solid var(--color-accent-light);
  border-top: none;
  border-radius: 0 0 var(--radius-md) var(--radius-md);
  z-index: 2000;
  max-height: 180px;
  overflow-y: auto;
  box-shadow: 0 4px 10px rgba(0,0,0,0.12);
}

.autocomplete-item {
  padding: var(--space-md) 14px;
  font-size: var(--font-size-md);
  min-height: 44px;
  color: var(--color-text-primary);
  cursor: pointer;
  border-bottom: 1px solid var(--color-border-light);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.autocomplete-item:last-child { border-bottom: none; }
.autocomplete-item:hover,
.autocomplete-item-active { background: var(--color-accent-bg); }

/* F-6 — Recent routes items in the Arrivée dropdown */
.autocomplete-recent-header {
  font-size: var(--font-size-xs);
  font-weight: 600;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  background: var(--color-bg-muted);
  cursor: default;
  padding: 5px var(--space-sm) 3px;
}
.autocomplete-recent-header:hover { background: var(--color-bg-muted); }

.autocomplete-recent-item {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.autocomplete-recent-from {
  font-size: var(--font-size-xs);
  color: var(--color-text-muted);
}

/* ── Zoom control (below settings button) ─────────────────── */

.maplibregl-ctrl-top-right {
  top: 64px; /* settings btn: 10px top + 44px height + 10px gap */
}

/* ── Settings button (top-right) ──────────────────────────── */

#settings-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  z-index: 1000;
  width: 44px;
  height: 44px;
  background: var(--color-bg-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: var(--font-size-lg);
  cursor: pointer;
  user-select: none;
  color: var(--color-text-secondary);
  transition: background 0.15s, color 0.15s;
}

#settings-btn:hover {
  background: var(--color-bg-hover);
  color: var(--color-text-primary);
}

/* R4 — Theme toggle button (top-right, next to settings) */

#theme-toggle-btn {
  position: absolute;
  top: 10px;
  right: 60px;
  z-index: 1000;
  width: 44px;
  height: 44px;
  background: var(--color-bg-surface);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  cursor: pointer;
  user-select: none;
  color: var(--color-text-secondary);
  transition: background 0.15s, color 0.15s;
}
#theme-toggle-btn:hover {
  background: var(--color-bg-hover);
  color: var(--color-text-primary);
}

#settings-menu {
  position: absolute;
  top: 60px;
  right: 10px;
  z-index: 1001;
  background: var(--color-bg-surface);
  border-radius: var(--radius-xl);
  padding: 6px;
  box-shadow: var(--shadow-md);
  min-width: 160px;
}

.settings-section-label {
  font-size: var(--font-size-xs);
  font-weight: 700;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: var(--space-xs) 10px 2px;
}

.settings-radio-item {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 10px var(--space-md);
  border-radius: var(--radius-md);
  font-size: var(--font-size-md);
  color: var(--color-text-body);
  cursor: pointer;
  transition: background 0.12s;
}

.settings-radio-item:hover { background: var(--color-bg-hover); }

.settings-radio-item input[type="radio"] {
  accent-color: var(--color-accent);
  cursor: pointer;
  flex-shrink: 0;
}

.settings-divider {
  border: none;
  border-top: 1px solid var(--color-border);
  margin: 5px 0;
}

/* F-9b — preferred network checkboxes */
.settings-checkbox-item {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 10px var(--space-md);
  border-radius: var(--radius-md);
  font-size: var(--font-size-md);
  color: var(--color-text-body);
  cursor: pointer;
  transition: background 0.12s;
}
.settings-checkbox-item:hover { background: var(--color-bg-hover); }
.settings-checkbox-item input[type="checkbox"] {
  accent-color: var(--color-accent);
  cursor: pointer;
  flex-shrink: 0;
}
.settings-net-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
  display: inline-block;
}

.settings-item {
  display: block;
  width: 100%;
  padding: 7px 10px;
  border: none;
  border-radius: var(--radius-md);
  background: transparent;
  font-size: 12px;
  font-family: inherit;
  color: var(--color-text-body);
  cursor: pointer;
  text-align: left;
  transition: background 0.12s;
}

.settings-item:hover {
  background: #fee2e2;
  color: var(--color-error);
}
[data-theme="dark"] .settings-item:hover {
  background: #450a0a;
}

.settings-api-help {
  margin-left: var(--space-xs);
  font-size: 9px;
  font-weight: 700;
  color: var(--color-accent-light);
  text-decoration: none;
  border: 1px solid #93c5fd;
  border-radius: 50%;
  padding: 0 3px;
}
.settings-api-help:hover { background: var(--color-accent-bg); }

.settings-api-row {
  padding: 3px var(--space-sm) 5px;
  display: flex;
  align-items: center;
}

.settings-hint {
  font-size: var(--font-size-xs);
  color: var(--color-text-muted);
}

.settings-api-input {
  width: 100%;
  box-sizing: border-box;
  padding: 5px var(--space-sm);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  font-size: var(--font-size-sm);
  font-family: monospace;
  color: var(--color-text-body);
  background: var(--color-bg-muted);
  outline: none;
  transition: border-color 0.15s;
}
.settings-api-input:focus { border-color: var(--color-accent-light); background: var(--color-bg-primary); }
.settings-api-input::placeholder { color: #cbd5e1; font-family: inherit; }

/* ── Corridor row ──────────────────────────────────────────── */

#corridor-row {
  margin-bottom: var(--space-xs);
  position: relative;
}

/* U-1 — tooltip above slider thumb during drag */
#corridor-slider-tooltip {
  position: absolute;
  bottom: calc(100% + 3px);
  transform: translateX(-50%);
  background: var(--color-accent);
  color: #fff;
  border-radius: var(--radius-sm);
  font-size: var(--font-size-xs);
  font-weight: 600;
  padding: 2px 5px;
  pointer-events: none;
  white-space: nowrap;
  display: none;
  z-index: 10;
}

#corridor-label {
  font-size: var(--font-size-sm);
  color: var(--color-text-secondary);
  cursor: pointer;
  user-select: none;
  min-height: 44px;
  display: flex;
  align-items: center;
}

#corridor-label strong {
  color: var(--color-accent);
}

#corridor-label:hover strong {
  text-decoration: underline;
}

#corridor-slider {
  display: block;
  width: 100%;
  margin-top: var(--space-xs);
  accent-color: var(--color-accent);
}

#corridor-slider::-webkit-slider-thumb {
  width: 28px;
  height: 28px;
}

/* ── R3 — Bottom sheet handle (mobile) ───────────────────────── */

.bs-handle {
  display: none;
}

/* ── Mobile: hide legend entirely ──────────────────────────── */

@media (max-width: 640px) {
  #legend {
    display: none;
  }
}

/* ── R3 — Mobile: bottom sheet panel ─────────────────────────── */

.panel-chevron {
  display: none;
  font-size: var(--font-size-xs);
  margin-left: 6px;
  transition: transform 0.2s;
  vertical-align: middle;
}

@media (max-width: 640px) {
  /* R3 — Convert panel to bottom sheet */
  #panel {
    position: fixed;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    border-radius: var(--space-lg) var(--space-lg) 0 0;
    max-height: 85vh;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    overflow: hidden;
    padding-top: 6px;
  }

  .bs-handle {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    width: 100%;
    height: 28px;
    cursor: grab;
  }
  .bs-handle::before,
  .bs-handle::after {
    content: '';
    display: block;
    width: 32px;
    height: 3px;
    border-radius: var(--radius-full);
    background: var(--color-text-muted);
    opacity: 0.55;
  }
  .bs-handle:active { cursor: grabbing; }

  /* Bottom sheet states */
  #panel.bs-collapsed {
    transform: translateY(calc(100% - 64px));
  }

  #panel.bs-half {
    transform: translateY(calc(100% - 300px));
  }

  #panel.bs-full {
    transform: translateY(0);
    overflow-y: auto;
  }

  /* When collapsed, hide everything after the title */
  #panel.bs-collapsed .panel-sub,
  #panel.bs-collapsed #data-freshness,
  #panel.bs-collapsed #route-section {
    display: none;
  }

  /* After route calculation */
  #panel.route-calculated .panel-chevron {
    display: inline;
  }

  #panel.route-calculated .panel-title {
    cursor: pointer;
    user-select: none;
  }

  /* Legacy: keep route-calculated/panel-expanded for backwards compat */
  #panel.route-calculated:not(.panel-expanded):not(.bs-full) .panel-sub,
  #panel.route-calculated:not(.panel-expanded):not(.bs-full) .route-label,
  #panel.route-calculated:not(.panel-expanded):not(.bs-full) .autocomplete-wrap,
  #panel.route-calculated:not(.panel-expanded):not(.bs-full) .route-actions,
  #panel.route-calculated:not(.panel-expanded):not(.bs-full) #route-info,
  #panel.route-calculated:not(.panel-expanded):not(.bs-full) .panel-divider {
    display: none;
  }

  #panel.route-calculated:not(.panel-expanded) #route-section {
    margin-top: var(--space-sm);
  }

  #panel.route-calculated.panel-expanded .panel-chevron {
    transform: rotate(90deg);
  }

  #panel.route-calculated.panel-expanded .panel-title {
    margin-bottom: 2px;
  }

  /* R1 — Don't shift panel when drive mode is active on mobile */
  .drive-mode-active #panel {
    left: 0;
  }

  /* R9 — Settings as bottom sheet on mobile */
  #settings-menu {
    position: fixed;
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    border-radius: var(--space-lg) var(--space-lg) 0 0;
    max-height: 70vh;
    overflow-y: auto;
    z-index: 2001;
    padding: var(--space-lg) var(--space-md);
    box-shadow: var(--shadow-lg);
  }

  /* Larger touch targets on mobile settings */
  .settings-radio-item,
  .settings-checkbox-item {
    min-height: 48px;
  }

  .settings-radio-item input[type="radio"],
  .settings-checkbox-item input[type="checkbox"] {
    width: 20px;
    height: 20px;
  }

  /* R9 — Settings close button on mobile */
  .settings-close-mobile {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    padding: var(--space-md);
    font-size: var(--font-size-md);
    font-weight: 600;
    color: var(--color-text-secondary);
    background: var(--color-bg-muted);
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    margin-top: var(--space-sm);
    font-family: inherit;
  }
  .settings-close-mobile:hover { background: var(--color-bg-hover); }
}

/* Settings close button and backdrop hidden on non-mobile */
@media (min-width: 641px) {
  .settings-close-mobile {
    display: none;
  }
}

/* Brand icon markers — remove default DivIcon white box */
.cheap-brand-marker {
  background: none !important;
  border: none !important;
  transition: opacity 200ms ease-out;
}

/* ── Geolocation arrow marker ──────────────────────────────── */

.geoloc-marker-outer {
  background: none !important;
  border: none !important;
}

.geoloc-marker {
  filter: drop-shadow(0 1px 3px rgba(0, 0, 0, 0.45));
}

/* ── Locate control button ─────────────────────────────────── */

.maplibregl-ctrl-locate {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 29px;
  height: 29px;
  background: var(--color-bg-primary);
  border: none;
  border-radius: var(--radius-sm);
  padding: 0;
  cursor: pointer;
  color: var(--color-text-body);
  font-family: inherit;
}

.maplibregl-ctrl-locate:hover:not(:disabled) {
  background: var(--color-bg-hover);
}

.maplibregl-ctrl-locate:disabled {
  color: #bbb;
  cursor: default;
}

.maplibregl-ctrl-locate.geoloc-retry {
  color: var(--color-warning);
}

/* MapLibre controls dark mode */
[data-theme="dark"] .maplibregl-ctrl-group {
  background: var(--color-bg-muted);
  box-shadow: var(--shadow-sm);
}
[data-theme="dark"] .maplibregl-ctrl-group button {
  background-color: transparent;
  color: var(--color-text-primary);
}
[data-theme="dark"] .maplibregl-ctrl-group button + button {
  border-top-color: var(--color-border);
}

/* ── U-2 — Route results panel ─────────────────────────────── */

#route-results {
  margin-top: var(--space-sm);
  border-top: 1px solid var(--color-border);
  padding-top: var(--space-xs);
}

.route-results-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: var(--font-size-sm);
  font-weight: 600;
  color: var(--color-text-secondary);
  cursor: pointer;
  user-select: none;
  padding: 10px 0;
}
.route-results-header:hover { color: var(--color-text-primary); }

.route-results-chevron {
  font-size: var(--font-size-xs);
  transition: transform 0.2s;
}
#route-results.open .route-results-chevron { transform: rotate(90deg); }

#route-results-list {
  display: none;
  max-height: 200px;
  overflow-y: auto;
  margin-top: var(--space-xs);
}
#route-results.open #route-results-list { display: block; }

.route-result-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 10px var(--space-xs);
  font-size: var(--font-size-base);
  border-bottom: 1px solid var(--color-border-light);
}
.route-result-item:last-child { border-bottom: none; }

.route-result-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.route-result-name {
  flex: 1;
  color: var(--color-text-primary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.route-result-meta {
  color: var(--color-text-muted);
  font-size: var(--font-size-xs);
  white-space: nowrap;
  flex-shrink: 0;
}

/* ── U-7 drive mode button ──────────────────────────────────── */

.route-btn-drive {
  flex: none;
  background: #059669;
  color: white;
  font-size: var(--font-size-md);
  min-height: 44px;
  min-width: 44px;
  padding: 5px var(--space-sm);
}
.route-btn-drive:hover:not(:disabled) { background: #047857; opacity: 1; }

.route-btn-emergency {
  flex: none;
  background: #ef4444;
  color: white;
  font-size: var(--font-size-lg);
  min-height: 44px;
  min-width: 44px;
  padding: 5px var(--space-sm);
}
.route-btn-emergency:hover:not(:disabled) { background: var(--color-error); opacity: 1; }

/* ── U-7 — Drive mode panel ─────────────────────────────────── */

#drive-panel {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: 25vw;
  min-width: 240px;
  z-index: 2000;
  background: var(--color-bg-surface);
  padding: 10px var(--space-md);
  box-shadow: var(--shadow-md);
  border-radius: 0 var(--radius-xl) var(--radius-xl) 0;
  flex-direction: column;
  gap: var(--space-sm);
}

.dm-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}

.dm-title {
  font-size: var(--font-size-md);
  font-weight: 700;
  color: var(--color-text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.dm-exit {
  background: none;
  border: none;
  color: var(--color-text-secondary);
  font-size: 24px;
  cursor: pointer;
  padding: 6px var(--space-sm);
  line-height: 1;
  font-family: inherit;
  border-radius: var(--radius-md);
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.dm-exit:hover { color: var(--color-text-primary); background: var(--color-bg-hover); }

.dm-refresh {
  background: none;
  border: none;
  color: var(--color-text-secondary);
  font-size: 20px;
  cursor: pointer;
  padding: 6px var(--space-sm);
  line-height: 1;
  font-family: inherit;
  border-radius: var(--radius-md);
  min-width: 44px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.dm-refresh:hover { color: var(--color-text-primary); background: var(--color-bg-hover); }

#dm-cards {
  display: flex;
  flex-direction: column-reverse;
  flex: 1;
  min-height: 0;
  gap: 6px;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-color: var(--color-border) transparent;
  scrollbar-width: thin;
}
#dm-cards::-webkit-scrollbar { width: 6px; }
#dm-cards::-webkit-scrollbar-track { background: transparent; }
#dm-cards::-webkit-scrollbar-thumb { background: var(--color-border); border-radius: var(--radius-full); }

.dm-card {
  background: var(--color-bg-hover);
  border-radius: var(--radius-lg);
  padding: 10px var(--space-md);
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  gap: var(--space-md);
  border: 2px solid transparent;
}

.dm-card-left {
  flex-shrink: 0;
  min-width: 56px;
  text-align: right;
}

.dm-card-right {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.dm-card--next { border-color: #0ea5e9; }
.dm-card--preferred { border-left: 3px solid #f59e0b; }
.dm-card--error, .dm-card--done {
  color: var(--color-text-secondary);
  font-size: var(--font-size-base);
  align-items: center;
  justify-content: center;
  min-width: 160px;
}

.dm-distance {
  font-size: 26px;
  font-weight: 800;
  color: var(--color-text-primary);
  line-height: 1;
}
.dm-unit { font-size: var(--font-size-md); font-weight: 400; }

.dm-operator {
  font-size: var(--font-size-md);
  color: var(--color-text-body);
  display: flex;
  align-items: center;
  gap: 5px;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.dm-op-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}

.dm-name  { font-size: var(--font-size-base); color: var(--color-text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.dm-power { font-size: var(--font-size-md); color: var(--color-text-muted); }

.dm-avail { font-size: var(--font-size-sm); min-height: 14px; }

.dm-card[data-drive-idx],
.dm-card[data-emergency-idx] { cursor: pointer; }
.dm-card[data-drive-idx]:active,
.dm-card[data-emergency-idx]:active { opacity: 0.75; }

.dm-preferred-star { color: #f59e0b; flex-shrink: 0; }

/* ── R1 — Mobile drive/emergency mode ────────────────────────── */

@media (max-width: 640px) {
  #drive-panel {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    min-width: unset;
    max-height: 50vh;
    border-radius: var(--space-lg) var(--space-lg) 0 0;
    box-shadow: var(--shadow-lg);
  }

  /* Don't shift panel when drive mode active on mobile */
  .drive-mode-active #panel {
    left: 0;
  }

  /* Emergency mode on mobile: full screen */
  .emergency-mobile #drive-panel {
    max-height: 100vh;
    border-radius: 0;
  }

  .emergency-mobile #drive-panel .emergency-header {
    font-size: var(--font-size-md);
    padding: var(--space-md) var(--space-lg);
  }

  #dm-cards {
    flex-direction: column;
  }
}

/* ── Route polyline draw animation (piste 4.4) ─────────────── */

@keyframes drawRoute {
  from { stroke-dashoffset: 5000; }
  to   { stroke-dashoffset: 0; }
}

.route-polyline {
  stroke-dasharray: 5000;
  stroke-dashoffset: 5000;
  animation: drawRoute 0.8s ease-out forwards;
}

/* ── P1 — Navigation button ──────────────────────────────────── */

.nav-btn {
  display: inline-block;
  min-height: 44px;
  min-width: 44px;
  padding: 10px var(--space-lg);
  background: var(--color-accent);
  color: white;
  border-radius: var(--radius-lg);
  text-decoration: none;
  font-weight: 600;
  font-size: var(--font-size-md);
  text-align: center;
  line-height: 24px;
  margin-top: var(--space-sm);
}

.nav-btn:hover { opacity: 0.85; }

/* Override MapLibre popup link color which can override .nav-btn color */
.maplibregl-popup-content .nav-btn,
.maplibregl-popup-content .nav-btn:visited,
.maplibregl-popup-content .nav-btn:hover {
  color: white;
  text-decoration: none;
}

.nav-btn-sm {
  min-height: 32px;
  min-width: auto;
  padding: var(--space-xs) 10px;
  font-size: var(--font-size-sm);
  line-height: 24px;
  margin-top: 0;
  flex-shrink: 0;
  border-radius: var(--radius-md);
}

.nav-btn-drive {
  width: 44px;
  height: 44px;
  min-height: 44px;
  min-width: 44px;
  padding: 0;
  font-size: 20px;
  line-height: 44px;
  margin-top: var(--space-xs);
  text-align: center;
  border-radius: var(--radius-xl);
}

/* ── P2 — Gap warnings and Plan B ────────────────────────────── */

.route-gap-next {
  font-size: var(--font-size-sm);
  color: var(--color-text-muted);
  padding: 2px var(--space-xs) 2px 22px;
}

.route-gap-warning {
  padding: var(--space-xs) var(--space-sm);
  margin: 2px 0;
  background: #fff7ed;
  border: 1px solid #fed7aa;
  border-radius: var(--radius-sm);
  font-size: 12px;
  color: #9a3412;
  font-weight: 600;
}
[data-theme="dark"] .route-gap-warning {
  background: #431407;
  border-color: #9a3412;
  color: #fdba74;
}

.dm-planb {
  font-size: var(--font-size-sm);
  color: var(--color-text-secondary);
  margin-top: 2px;
}

.dm-planb-link {
  color: #0891B2;
  text-decoration: none;
  font-weight: 600;
  padding: var(--space-xs);
  min-width: 44px;
  min-height: 44px;
  display: inline-block;
  text-align: center;
}
.dm-planb-link:hover { color: #06b6d4; }

.dm-planb-last {
  font-style: italic;
  color: var(--color-text-muted);
}

/* ── Emergency mode — bornes à proximité ─────────────────────── */

.emergency-header {
  background: #fef2f2;
  border-bottom: 2px solid #ef4444;
  padding: var(--space-sm) var(--space-md);
  font-size: 12px;
  color: #991b1b;
  font-weight: 600;
}
[data-theme="dark"] .emergency-header {
  background: #450a0a;
  color: #fca5a5;
}


/* ── P3 — Pricing tiers ──────────────────────────────────────── */

.popup-price {
  display: block;
  font-size: var(--font-size-base);
  margin-bottom: 6px;
}

.price-tier {
  font-weight: 700;
  font-size: var(--font-size-base);
}
.price-tier-1 { color: #10b981; }
.price-tier-2 { color: #f59e0b; }
.price-tier-3 { color: #ef4444; }

.price-note {
  color: var(--color-text-muted);
}

.price-disclaimer {
  font-size: var(--font-size-sm);
  color: var(--color-text-muted);
  text-align: center;
  margin-top: var(--space-sm);
  padding-top: 6px;
  border-top: 1px solid var(--color-border-light);
}

/* ── P4 — Alert badges ──────────────────────────────────────── */

.alert-badge {
  display: inline-block;
  background: #fef3c7;
  border: 1px solid #fcd34d;
  border-radius: var(--radius-sm);
  padding: 2px var(--space-sm);
  font-size: 12px;
  margin-top: var(--space-xs);
  margin-right: var(--space-xs);
}
[data-theme="dark"] .alert-badge {
  background: #451a03;
  border-color: #92400e;
  color: #fbbf24;
}

/* ── R9 — Tesla viewport: simplified settings ────────────────── */
.settings-tesla-hidden {
  display: none !important;
}

/* ── Settings overlay backdrop (mobile) ──────────────────────── */
.settings-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 2000;
}
.settings-backdrop.active { display: block; }

/* ── Print ──────────────────────────────────────────────────── */
@page { margin: 0; }
@media print {
  #panel, #legend, #settings-btn, #theme-toggle-btn, #settings-menu, #loading-overlay { display: none !important; }
}

/* ── Drive/emergency panel handle (hidden on desktop) ──────── */

.dm-handle { display: none; }

@media (max-width: 640px) {
  #drive-panel {
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    min-width: unset;
    max-height: 70vh;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -4px 20px rgba(0,0,0,0.4);
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  }

  #drive-panel.dm-collapsed {
    transform: translateY(calc(100% - 80px));
  }

  .dm-handle {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    width: 100%;
    height: 24px;
    cursor: grab;
  }
  .dm-handle::before,
  .dm-handle::after {
    content: '';
    display: block;
    width: 32px;
    height: 3px;
    border-radius: 9999px;
    background: #64748b;
    opacity: 0.55;
  }
  .dm-handle:active { cursor: grabbing; }

  /* Drive mode: don't shift main panel on mobile */
  .drive-mode-active #panel { left: 0; }
}
