* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; font-family: system-ui, -apple-system, sans-serif; }
body {
  display: grid;
  grid-template-columns: minmax(260px, 320px) 1fr;
  height: 100dvh;
  color: #e2e8f0;
  background: #0f172a;
}

/* ------- PANNEAU LATÉRAL ------- */
#panel {
  background: #0f172a; padding: 16px; overflow-y: auto;
  border-right: 1px solid #1e293b;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
}
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
#panel h1 { font-size: clamp(16px, 2vw, 20px); margin: 0; flex: 1; }
#panel button#toggle-panel {
  width: 44px; min-width: 44px; height: 44px; min-height: 44px;
  padding: 0; margin: 0; flex: 0 0 auto;
  background: transparent; border: 1px solid #334155; color: #e2e8f0;
  border-radius: 6px; cursor: pointer; font-size: 16px;
}
#panel button#toggle-panel:hover,
#panel button#toggle-panel:focus-visible {
  background: #1e293b;
}
@media (min-width: 721px) { #toggle-panel { display: none; } }
#panel h2 {
  font-size: 13px; letter-spacing: 0.01em;
  color: #e2e8f0; margin: 16px 0 8px; font-weight: 600;
}
#panel h2.muted {
  font-size: 12px; text-transform: uppercase; letter-spacing: 0.05em;
  color: #94a3b8; font-weight: 500;
}
#panel section { padding-bottom: 10px; border-bottom: 1px solid #1e293b; }
/* Section « action » = action primaire, plus affordante */
#panel section.action {
  background: linear-gradient(180deg, #1a2440 0%, #141c30 100%);
  border: 1px solid #334155;
  border-radius: 10px;
  padding: 12px 14px;
  margin: 14px 0;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.25);
}
#panel section.action .action-title {
  font-size: 14px; color: #e0e7ff;
  margin: 0 0 4px; display: flex; align-items: center; gap: 6px;
}
#panel section.action .section-lead {
  color: #94a3b8; font-size: 11px; margin: 0 0 8px;
}
/* Sections repliables en secondaire (<details>) */
#panel details.collapsible {
  border-bottom: 1px solid #1e293b;
  padding-bottom: 10px;
}
#panel details.collapsible summary {
  cursor: pointer; list-style: none; outline: none;
  display: flex; align-items: center; gap: 6px;
  padding: 4px 0; user-select: none;
}
#panel details.collapsible summary::-webkit-details-marker { display: none; }
#panel details.collapsible summary::before {
  content: "▸"; color: #94a3b8; font-size: 10px;
  transition: transform 0.15s ease; display: inline-block;
}
#panel details.collapsible[open] summary::before { transform: rotate(90deg); }
#panel details.collapsible summary:hover h2,
#panel details.collapsible summary:focus-visible h2 { color: #e2e8f0; }
#panel h2.inline-h2 { margin: 0; }
#panel label { display: block; padding: 6px 0; font-size: 14px; }
#panel input, #panel select, #panel button {
  width: 100%; padding: 10px; margin: 4px 0; border-radius: 6px;
  border: 1px solid #334155; background: #1e293b; color: #e2e8f0;
  font-size: 14px; font-family: inherit;
}
#panel button {
  background: #2563eb; border: 0; cursor: pointer; font-weight: 500;
  min-height: 44px;    /* cible tactile confortable */
}
#panel button:hover, #panel button:focus-visible { background: #1d4ed8; }
#panel button.active { background: #16a34a; }
#panel button:disabled {
  background: #334155; color: #94a3b8; cursor: not-allowed; opacity: 0.7;
}
#panel input:focus-visible, #panel select:focus-visible {
  outline: 2px solid #60a5fa; outline-offset: 1px; border-color: #60a5fa;
}
#panel small { color: #94a3b8; font-size: 12px; display: block; margin-top: 4px; }
#panel label.inline {
  display: flex; align-items: center; gap: 8px;
  color: #cbd5e1; font-size: 13px; padding: 4px 0;
}
#panel label.inline input { width: auto; margin: 0; min-height: 0; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0;
  margin: -1px; overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}
#panel a {
  color: #60a5fa; font-size: 13px; line-height: 1.8;
  display: inline-block; padding: 2px 0;
}
/* Boutons d'export : grille 3 colonnes + compteur dynamique */
.export-grid {
  display: grid; grid-template-columns: repeat(3, 1fr);
  gap: 6px; margin: 8px 0 10px;
}
.export-btn {
  display: flex !important; flex-direction: column; align-items: center;
  justify-content: center; gap: 2px;
  padding: 10px 6px !important; min-height: 56px;
  background: #1e293b; border: 1px solid #334155; border-radius: 8px;
  color: #e2e8f0 !important; text-decoration: none; font-size: 12px;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.export-btn:hover, .export-btn:focus-visible {
  background: #2563eb; border-color: #2563eb; color: #fff !important;
}
.export-label { font-weight: 500; }
.export-count {
  font-size: 11px; color: #94a3b8;
  font-variant-numeric: tabular-nums;
}
.export-btn:hover .export-count,
.export-btn:focus-visible .export-count { color: #e0e7ff; }
.gpx-row { display: flex; gap: 6px; align-items: stretch; margin-top: 4px; }
.gpx-row input {
  flex: 1 1 auto; width: auto !important; margin: 0 !important;
}
.gpx-row button.ghost {
  flex: 0 0 auto; min-height: 42px !important; padding: 6px 14px !important;
  font-size: 13px !important;
}
.status {
  display: flex; align-items: center; gap: 8px; margin-bottom: 12px;
  flex-wrap: wrap;
}
.dot { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
.dot.on   { background: #22c55e; box-shadow: 0 0 8px #22c55e; }
.dot.off  { background: #ef4444; }
.dot.wait { background: #f59e0b; box-shadow: 0 0 8px #f59e0b;
  animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }
.sse-last {
  color: #94a3b8; font-size: 11px; margin-left: auto;
  font-variant-numeric: tabular-nums;
}
#log {
  list-style: none; padding: 0; margin: 0;
  max-height: 180px; overflow-y: auto; font-size: 12px;
}
#log li { padding: 4px 0; border-bottom: 1px solid #1e293b; color: #cbd5e1; }
#log li .t { color: #94a3b8; margin-right: 6px; }
#log li .k { color: #fbbf24; font-weight: 600; }
.log-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; margin: 16px 0 8px;
}
.log-head h2 {
  margin: 0; display: flex; align-items: center; gap: 8px;
}
.log-actions { display: flex; gap: 4px; }
.badge {
  background: #1e293b; color: #e2e8f0;
  border: 1px solid #334155; border-radius: 999px;
  padding: 1px 8px; font-size: 11px; font-weight: 600;
  min-width: 22px; text-align: center;
}
.badge.paused { background: #78350f; border-color: #a16207; color: #fde68a; }
#panel button.ghost {
  width: auto; min-height: 28px; padding: 2px 8px; margin: 0;
  background: transparent; color: #cbd5e1;
  border: 1px solid #334155; font-size: 11px; font-weight: 500;
}
#panel button.ghost:hover, #panel button.ghost:focus-visible {
  background: #1e293b; color: #e2e8f0;
}
#panel button.ghost[aria-pressed="true"] {
  background: #78350f; border-color: #a16207; color: #fde68a;
}

/* ------- CARTE ------- */
#map { height: 100dvh; min-height: 0; position: relative; }

/* Badge d'état en haut au centre de la carte (F12) */
.map-badge {
  position: absolute;
  top: 12px; left: 50%; transform: translateX(-50%);
  z-index: 500;
  display: inline-flex; align-items: center; gap: 8px;
  background: rgba(15, 23, 42, 0.92);
  border: 1px solid #334155; border-radius: 999px;
  padding: 6px 14px;
  color: #e2e8f0; font-size: 12px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.35);
  pointer-events: none;
  font-variant-numeric: tabular-nums;
}
.map-badge[hidden] { display: none; }
.map-badge-dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: #22c55e; box-shadow: 0 0 6px #22c55e;
}

/* Toasts : feedback persistant pour les erreurs/confirmations critiques (F7) */
.toasts {
  position: absolute;
  top: 12px; right: 12px;
  z-index: 600;
  display: flex; flex-direction: column; gap: 8px;
  max-width: min(340px, calc(100% - 24px));
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  display: flex; align-items: flex-start; gap: 10px;
  padding: 10px 12px;
  background: #0f172a;
  border: 1px solid #334155; border-left-width: 4px;
  border-radius: 8px;
  color: #e2e8f0; font-size: 13px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.4);
  animation: toast-in 0.18s ease-out;
}
.toast.ok      { border-left-color: #22c55e; }
.toast.info    { border-left-color: #60a5fa; }
.toast.warn    { border-left-color: #f59e0b; }
.toast.error   { border-left-color: #ef4444; }
.toast-body { flex: 1; min-width: 0; }
.toast-title { font-weight: 600; margin-bottom: 2px; }
.toast-msg   { color: #cbd5e1; font-size: 12px; }
.toast-actions { display: flex; gap: 6px; margin-top: 6px; }
.toast-actions button {
  background: #1e293b; color: #e2e8f0;
  border: 1px solid #334155; border-radius: 6px;
  padding: 4px 10px; font-size: 12px; cursor: pointer;
}
.toast-actions button:hover { background: #334155; }
.toast-close {
  background: transparent; border: 0; color: #94a3b8;
  cursor: pointer; font-size: 16px; padding: 0 2px; line-height: 1;
}
.toast-close:hover { color: #e2e8f0; }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(-6px); }
  to   { opacity: 1; transform: translateY(0); }
}
@media (max-width: 720px) {
  .toasts { top: 8px; right: 8px; left: 8px; max-width: none; }
  .map-badge { top: 8px; font-size: 11px; }
}

/* ------- MOBILE (<=720px) : panneau en tiroir du bas ------- */
@media (max-width: 720px) {
  body {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr auto;
  }
  #map {
    grid-row: 1;
    height: auto;
    min-height: 50dvh;    /* la carte garde toujours au moins la moitié */
  }
  #panel {
    grid-row: 2;
    max-height: 32dvh;    /* tiroir plus discret par défaut (était 48dvh) */
    border-right: 0;
    border-top: 1px solid #1e293b;
    position: relative;
    transition: max-height 0.22s ease;
    padding-top: 20px;    /* laisse la place au handle */
  }
  /* Handle de drag — grand, centré, cliquable (toggle collapse).
     Sélecteur sur-spécifié pour battre #panel button (1,0,1). */
  #panel button#panel-handle {
    position: absolute;
    top: 0; left: 0; right: 0;
    display: flex; align-items: center; justify-content: center;
    width: auto; height: 20px; min-height: 0;
    margin: 0; padding: 0;
    background: transparent;
    border: 0; border-radius: 0;
    cursor: pointer;
    color: inherit;
  }
  #panel button#panel-handle:hover,
  #panel button#panel-handle:focus-visible {
    background: transparent;
  }
  #panel button#panel-handle::before {
    content: "";
    display: block;
    width: 44px; height: 5px;
    background: #475569; border-radius: 3px;
    transition: background 0.15s ease, width 0.15s ease;
  }
  #panel button#panel-handle:hover::before,
  #panel button#panel-handle:focus-visible::before { background: #94a3b8; width: 56px; }
  /* État déplié : davantage d'espace quand l'utilisateur en demande plus */
  #panel.expanded { max-height: 70dvh; }
  /* État replié : juste la barre de statut */
  #panel.collapsed {
    max-height: 48px;
    overflow: hidden;
  }
  #panel h1 { font-size: 15px; margin: 0 0 8px; }
  #panel h2 { margin: 10px 0 6px; }
}
/* Sur desktop, le handle n'existe pas */
@media (min-width: 721px) {
  #panel-handle { display: none; }
}

/* ------- TRÈS PETIT (<=380px, Android compact) ------- */
@media (max-width: 380px) {
  #panel { padding: 12px; }
  #panel input, #panel select, #panel button { padding: 8px; font-size: 13px; }
}

/* ------- TABLETTE PAYSAGE / DESKTOP LARGE ------- */
@media (min-width: 1400px) {
  body { grid-template-columns: 360px 1fr; }
  #panel h1 { font-size: 22px; }
}

/* ------- PAYSAGE SUR TÉLÉPHONE : rebasculer en colonnes ------- */
@media (max-width: 920px) and (orientation: landscape) and (max-height: 500px) {
  body {
    grid-template-columns: 240px 1fr;
    grid-template-rows: 1fr;
  }
  #panel {
    grid-row: 1;
    max-height: none;
    border-right: 1px solid #1e293b;
    border-top: 0;
    padding-top: 16px;    /* pas de handle en paysage étroit */
  }
  #panel-handle { display: none; }
  #map { grid-row: 1; height: 100dvh; min-height: 0; }
}

/* ------- Contrôles Leaflet (plus gros au tactile) ------- */
.leaflet-touch .leaflet-bar a,
.leaflet-touch .leaflet-control-layers-toggle {
  width: 36px; height: 36px; line-height: 36px;
}
.leaflet-control-layers {
  background: rgba(15,23,42,0.92) !important;
  color: #e2e8f0;
  border: 1px solid #1e293b !important;
}
.leaflet-control-layers label { color: #e2e8f0; }
.leaflet-popup-content-wrapper, .leaflet-popup-tip {
  background: #0f172a; color: #e2e8f0;
}
.leaflet-popup-content { font-size: 13px; }

/* Attribution lisible sur fond sombre */
.leaflet-control-attribution {
  background: rgba(15,23,42,0.8) !important;
  color: #94a3b8 !important;
}
.leaflet-control-attribution a { color: #60a5fa !important; }
