/* Sonata Dash — thème sombre style Tesla.
   Mise en page choisie par app.js (data-layout = compact | wide) d'après
   matchMedia('(min-width: 1000px) and (min-height: 600px)') ou le réglage « Format ».
   Les composants sont les mêmes partout : seul le placement change. */

:root {
  --bg: #000;
  --card: #0e1115;
  --card2: #161a20;
  --line: #242a32;
  --fg: #f3f4f6;
  --muted: #8a919b;
  --accent: #3e8bff;
  --ok: #30d158;
  --warn: #ffb020;
  --danger: #ff453a;
  --track: #222830;
  --red-zone: #4a1714;
  --bar-h: 64px;
  --gap: 10px;
  --radius: 18px;
  --dim: 0;
  color-scheme: dark;
}
html[data-theme="night"] {
  --card: #07090b;
  --card2: #0d1014;
  --line: #161a1f;
  --fg: #b9bdc4;
  --muted: #646a73;
  --accent: #2c66c4;
  --ok: #24a145;
  --warn: #c98a12;
  --danger: #cc3a30;
  --track: #15191e;
  --red-zone: #331210;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font: 500 18px/1.2 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  display: grid;
  grid-template-rows: var(--bar-h) minmax(0, 1fr);
  height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }

/* ---------- Boutons (cibles ≥ 64 px) ---------- */
.btn, .pill, #tabs button {
  min-height: 64px;
  min-width: 64px;
  padding: 0 18px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--card2);
  font-size: 19px;
  font-weight: 600;
}
.btn.icon { padding: 0; display: grid; place-items: center; }
.btn.icon svg { width: 28px; height: 28px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.danger { background: transparent; border-color: var(--danger); color: var(--danger); }
.btn:disabled { opacity: .4; cursor: default; }
.btn:active, .pill:active, #tabs button:active, .tile-btn:active { filter: brightness(1.3); }

/* ---------- Barre d'état ---------- */
#bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 var(--gap);
  min-width: 0;
}
.pill { display: inline-flex; align-items: center; gap: 10px; white-space: nowrap; min-width: 150px; }
.dot { width: 14px; height: 14px; border-radius: 50%; background: var(--muted); flex: none; }
.pill[data-status="connected"] .dot { background: var(--ok); }
.pill[data-status="connecting"] .dot, .pill[data-status="reconnecting"] .dot { background: var(--warn); }
.pill[data-status="error"] .dot { background: var(--danger); }
.pill[data-status="idle"], .pill[data-status="error"] { border-color: var(--accent); }

#tabs { display: flex; gap: 6px; }
#tabs button { padding: 0 14px; background: transparent; border-color: transparent; color: var(--muted); }
#tabs button[aria-selected="true"] { background: var(--card2); border-color: var(--line); color: var(--fg); }
html[data-layout="wide"] #tabs { display: none; }

#bar-info {
  flex: 1;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 14px;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
}
#clock { font-size: 24px; font-weight: 600; font-variant-numeric: tabular-nums; }
#ambient { color: var(--muted); font-size: 20px; }
html[data-layout="compact"] #bar-info { flex-direction: column; align-items: center; justify-content: center; gap: 0; }
html[data-layout="compact"] #ambient { font-size: 15px; }
html[data-layout="compact"] .pill { min-width: 0; }
html[data-layout="compact"] #tabs button { padding: 0 11px; }
#map-note { color: var(--muted); font-size: 16px; overflow: hidden; text-overflow: ellipsis; }
html[data-layout="compact"] #map-note { display: none !important; }

.chip { padding: 8px 12px; border-radius: 999px; font-size: 16px; font-weight: 600; white-space: nowrap; }
.chip.warn { background: color-mix(in srgb, var(--warn) 18%, transparent); color: var(--warn); }

/* ---------- Grille principale ---------- */
#dash {
  position: relative;
  display: grid;
  gap: var(--gap);
  padding: 0 var(--gap) var(--gap);
  min-height: 0;
  min-width: 0;
}
.panel {
  position: relative;
  background: var(--card);
  border-radius: var(--radius);
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}
#p-speed { grid-area: speed; }
#p-map { grid-area: map; }
#p-tiles { grid-area: tiles; min-width: 0; min-height: 0; gap: var(--gap); }

/* Compact (téléphone) : 3 vues */
html[data-layout="compact"] #dash { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); touch-action: none; }
html[data-layout="compact"] #dash[data-view="drive"] {
  grid-template-columns: minmax(0, 1.75fr) minmax(0, 1fr);
  grid-template-areas: "speed tiles";
}
html[data-layout="compact"] #dash[data-view="drive"] #p-map { position: absolute; inset: 0; visibility: hidden; pointer-events: none; }
html[data-layout="compact"] #dash[data-view="drive"] #p-tiles { display: flex; flex-direction: column; }
html[data-layout="compact"] #dash[data-view="drive"] #p-tiles > :not(.primary) { display: none; }
html[data-layout="compact"] #dash[data-view="drive"] .tile { flex: 1; }

html[data-layout="compact"] #dash[data-view="map"] { grid-template-areas: "map"; }
html[data-layout="compact"] #dash[data-view="map"] #p-speed,
html[data-layout="compact"] #dash[data-view="map"] #p-tiles { display: none; }

html[data-layout="compact"] #dash[data-view="engine"] { grid-template-areas: "tiles"; }
html[data-layout="compact"] #dash[data-view="engine"] #p-speed { display: none; }
html[data-layout="compact"] #dash[data-view="engine"] #p-map { position: absolute; inset: 0; visibility: hidden; pointer-events: none; }
html[data-layout="compact"] #dash[data-view="engine"] #p-tiles {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  grid-auto-rows: minmax(0, 1fr);
}

/* Large (tablette) : tout sur un écran */
html[data-layout="wide"] #dash {
  grid-template-columns: minmax(0, 2fr) minmax(0, 3fr);
  grid-template-rows: minmax(0, 1fr) minmax(0, 23%);
  grid-template-areas: "speed map" "tiles tiles";
}
html[data-layout="wide"] #p-tiles { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(0, 1fr); }
html[data-layout="wide"] .tile-debug { display: none; }
html[data-layout="wide"] #dash[data-map="off"] {
  grid-template-rows: minmax(0, 1fr);
  grid-template-areas: "speed tiles";
}
html[data-layout="wide"] #dash[data-map="off"] #p-map { position: absolute; inset: 0; visibility: hidden; pointer-events: none; }
html[data-layout="wide"] #dash[data-map="off"] #p-tiles {
  grid-auto-flow: row;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  grid-auto-rows: minmax(0, 1fr);
}

/* ---------- Vitesse + RPM ---------- */
#p-speed {
  container-type: size;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2cqh;
  padding: 3cqh 4cqw;
}
.speed { display: flex; flex-direction: column; align-items: center; line-height: .86; }
#speed {
  font-size: 40vmin; /* repli si pas de container queries */
  font-size: min(52cqh, 46cqw);
  font-weight: 600;
  letter-spacing: -0.045em;
  font-variant-numeric: tabular-nums;
}
#speed-unit { font-size: max(16px, min(8cqh, 6cqw)); color: var(--muted); font-weight: 500; margin-top: 1.5cqh; }

#src-badge {
  position: absolute;
  top: 12px;
  left: 14px;
  padding: 5px 10px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: .06em;
  color: var(--muted);
  border: 1px solid var(--line);
}
#src-badge[data-src="OBD"] { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 40%, transparent); }
#src-badge[data-src="GPS"] { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 40%, transparent); }
#src-badge[data-src="DÉMO"] { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 40%, transparent); }

.rpm { width: 100%; max-width: 900px; }
.rpm-track {
  position: relative;
  height: max(12px, 5cqh);
  border-radius: 999px;
  overflow: hidden;
  background: linear-gradient(to right, var(--track) 0 85.714%, var(--red-zone) 85.714%);
}
.rpm-fill {
  position: absolute;
  inset: 0;
  background: var(--fg);
  transform-origin: left center;
  transform: scaleX(0);
  will-change: transform;
}
.rpm-fill.red { background: var(--danger); }
.rpm-scale {
  display: flex;
  justify-content: space-between;
  color: var(--muted);
  font-size: max(12px, min(4.5cqh, 3.2cqw));
  margin-top: 4px;
  font-variant-numeric: tabular-nums;
}
.rpm-scale span:nth-child(7), .rpm-scale span:nth-child(8) { color: var(--danger); }
.rpm-read { text-align: center; color: var(--muted); font-size: max(14px, min(5.5cqh, 4cqw)); margin-top: 2px; }
#rpm-val { color: var(--fg); font-weight: 600; font-variant-numeric: tabular-nums; }

/* ---------- Tuiles ---------- */
.tile {
  container-type: size;
  position: relative;
  background: var(--card);
  border: 1px solid transparent;
  border-radius: var(--radius);
  padding: 8px 12px;
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}
.t-label {
  color: var(--muted);
  font-size: max(13px, min(15cqh, 11cqw));
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.t-value {
  font-size: max(22px, min(46cqh, 30cqw));
  font-weight: 600;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}
.t-unit { position: absolute; right: 12px; bottom: 10px; color: var(--muted); font-size: max(13px, min(15cqh, 11cqw)); }
.tile.stale .t-value { opacity: .45; }
.tile.alert { border-color: var(--danger); }
.tile.alert .t-value { color: var(--danger); }
.tile-btn { text-align: left; border-color: var(--line); background: var(--card2); }
.t-action { font-size: max(18px, min(28cqh, 16cqw)); font-weight: 600; color: var(--accent); }
#btn-diag.has-codes { border-color: var(--warn); }
#btn-diag.has-codes .t-action { color: var(--warn); }

/* ---------- Carte ---------- */
#map { position: absolute; inset: 0; }
#dash[data-map="off"] #map { visibility: hidden; }
#map-off {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  text-align: center;
  color: var(--muted);
  padding: 20px;
}
#map-off div { display: flex; flex-direction: column; gap: 8px; }
#map-off strong { color: var(--fg); font-size: 26px; font-weight: 600; }
#dash[data-map="on"] #map-off { display: none; }
#mini-speed {
  position: absolute;
  top: 12px;
  left: 12px;
  z-index: 2;
  display: none;
  align-items: baseline;
  gap: 6px;
  padding: 8px 16px;
  border-radius: 14px;
  background: color-mix(in srgb, var(--bg) 82%, transparent);
  border: 1px solid var(--line);
}
#mini-speed-val { font-size: 52px; font-weight: 600; font-variant-numeric: tabular-nums; line-height: 1; }
#mini-speed small { color: var(--muted); font-size: 18px; }
html[data-layout="compact"] #dash[data-view="map"] #mini-speed { display: flex; }

.car-marker { width: 44px; height: 44px; }
.car-marker svg { width: 100%; height: 100%; display: block; }
.maplibregl-ctrl-attrib { font-size: 11px; }
html[data-theme] .maplibregl-ctrl-attrib.maplibregl-compact,
html[data-theme] .maplibregl-ctrl-attrib { background: rgba(0, 0, 0, .55) !important; color: #aaa; }
html[data-theme] .maplibregl-ctrl-attrib a { color: #ccc !important; }

/* ---------- Alertes ---------- */
#alerts {
  position: absolute;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 5;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
  width: max-content;
  max-width: calc(100% - 2 * var(--gap));
}
.alert-item {
  pointer-events: auto;
  border: 0;
  border-radius: 14px;
  padding: 10px 18px;
  min-height: 48px;
  font-size: 20px;
  font-weight: 700;
  color: #fff;
  background: var(--danger);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.alert-item.warn { background: var(--warn); color: #1a1200; }

/* ---------- Toast ---------- */
#toast {
  position: fixed;
  left: 50%;
  bottom: calc(16px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 50;
  max-width: min(760px, calc(100vw - 32px));
  padding: 16px 20px;
  border-radius: 16px;
  background: var(--card2);
  border: 1px solid var(--line);
  font-size: 18px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .6);
}

/* ---------- Atténuation (luminosité) ---------- */
#dim { position: fixed; inset: 0; background: #000; opacity: var(--dim); pointer-events: none; z-index: 100; }

/* ---------- Dialogues ---------- */
dialog {
  width: min(760px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  overflow: auto;
  padding: 16px 20px 20px;
  border: 1px solid var(--line);
  border-radius: 20px;
  background: var(--card2);
  color: var(--fg);
}
dialog::backdrop { background: rgba(0, 0, 0, .72); }
dialog h2 { margin: 0; font-size: 26px; }
dialog h3 { margin: 22px 0 10px; font-size: 18px; color: var(--muted); font-weight: 600; }
dialog p { line-height: 1.4; }
.dlg-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 6px; }
.row { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.row > span { font-size: 19px; }
.seg { display: flex; gap: 4px; padding: 4px; border-radius: 16px; background: var(--bg); }
.seg button { min-height: 56px; min-width: 76px; padding: 0 14px; border: 0; border-radius: 12px; background: transparent; color: var(--muted); font-weight: 600; }
.seg button[aria-pressed="true"] { background: var(--accent); color: #fff; }
input[type="range"] { width: min(320px, 45vw); height: 56px; accent-color: var(--accent); }
.actions { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 16px; }

#dtc-list { list-style: none; margin: 10px 0 0; padding: 0; display: grid; gap: 8px; }
#dtc-list li { display: flex; gap: 14px; align-items: baseline; padding: 12px 14px; border-radius: 12px; background: var(--bg); }
#dtc-list b { font-size: 22px; font-variant-numeric: tabular-nums; color: var(--warn); min-width: 76px; }
#dtc-list li.none b { color: var(--ok); min-width: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chips span.chip-pid { padding: 6px 10px; border-radius: 8px; background: var(--bg); font-size: 15px; }
.chips span.chip-pid b { color: var(--accent); margin-right: 6px; font-variant-numeric: tabular-nums; }

pre { white-space: pre-wrap; word-break: break-word; font: 13px/1.45 ui-monospace, "Cascadia Mono", Consolas, monospace; margin: 10px 0 0; user-select: text; -webkit-user-select: text; }
#dbg-info { padding: 10px 12px; border-radius: 12px; background: var(--bg); color: var(--muted); }
#dbg-log { max-height: 46dvh; overflow: auto; padding: 10px 12px; border-radius: 12px; background: var(--bg); }
#btn-trace[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: #fff; }
