/* KartingAR : base d'interface (lot C). Base reprise de vol-de-nid (réglages tactiles, overlays),
   habillée avec les images des packs Kenney copiées dans public/assets/{ui,pixel,controls,medals}.
   Aucun texte ici : les libellés (en français) sont dans le HTML. Aperçu : gallery.html.

   Classes fournies
   - Global      .hidden  .copyable  #view (canvas three.js plein écran)  #camera (vidéo AR, sous le canvas)
   - Écrans      .screen (overlay plein écran défilable, zones sûres)  .screen.dim (fond assombri)
                 .panel (cadre beige)  .panel.dark (cadre brun, texte clair)  .panel.small
                 .title (bandeau rouge : titre de l'accueil ou du podium)  .subtitle  .help  .error
                 .row (ligne flexible, retour à la ligne)  .stack (colonne)
   - Boutons     .btn (beige)  .btn.primary (rouge)  .btn.ghost (gris)  .btn.big  .btn.icon (rond, une icône
                 .ico-*)  :disabled  — cibles ≥ 48 px
   - Champs      .field > label + input/select    input.code (code de salon, majuscules)
                 input[type=checkbox].check
   - Chargement  .bar > .fill (style="width: 40%")
   - Salon       .players > .player (.me, .bot, .host, .off) > .car-dot (--c: couleur) + .name + .tag
                 .cars > .car (.sel, .taken) contenant une image ou un canvas de la voiture
                 .code-big (code de partie en gros)
   - HUD         #hud (couche plein écran, ne capte pas le toucher)
                 .hud-top (en haut, zones sûres) > .hud-pos (badge position : <b>2</b><small>/8</small>)
                 .hud-lap (bandeau tour : <b>2/3</b>)  .hud-time (chrono)  .hud-item (case objet)
                 .hud-item > img | canvas (vignette de l'objet tenu, rendue par le jeu) ; .hud-item.empty
                 .countdown (grand chiffre central, .go pour le départ)  .toast (message bref en haut)
                 .ar-lost (alerte « marqueur perdu »)  .coins (compteur de pièces)
   - Tactile     .touch (couche des commandes ; affichée seulement avec body.touch)
                 .tbtn (bouton) + .t-left .t-right .t-gas .t-brake .t-item .t-pause ; .tbtn.on = appuyé
                 Portrait : direction en bas à gauche ; accélérateur en bas à droite, frein au-dessus,
                 objet à gauche du frein ; pause en haut à droite. Paysage : tout sur la ligne du bas.
   - Fin         .podium > .step.p1 | .p2 | .p3 > .medal + .name + .block ; .results (tableau des places)
                 .medal.gold | .silver | .bronze | .best (meilleur tour)
*/

:root {
  --ink: #2b1d0e;          /* texte sur beige : contraste > 12:1 */
  --paper: #fff1d6;
  --brown: #a66a32;
  --brown-dark: #6b4220;
  --red: #d9534a;
  --hud-bg: rgba(28, 22, 16, 0.86);
  --hud-ink: #fff;
  --accent: #ffcf3a;
  --shadow: 0 6px 18px rgba(20, 12, 4, 0.35);
  --outline: 0 2px 0 #000, 0 -2px 0 #000, 2px 0 0 #000, -2px 0 0 #000, 0 3px 6px rgba(0, 0, 0, 0.6);
  /* zones sûres (encoche, barre d'accueil) + marge minimale */
  --sa-t: max(10px, env(safe-area-inset-top));
  --sa-r: max(10px, env(safe-area-inset-right));
  --sa-b: max(10px, env(safe-area-inset-bottom));
  --sa-l: max(10px, env(safe-area-inset-left));
  font-family: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;
  font-weight: 700;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; color: var(--ink); background: #35424f; }
/* Mobile : pas de zoom au double-tap, pas de sélection / loupe / menu à l'appui long, pas de surbrillance
   au toucher, pas de rebond. Champs et .copyable restent sélectionnables. */
html, body {
  touch-action: manipulation; overscroll-behavior: none;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%; text-size-adjust: 100%;
}
body { position: fixed; inset: 0; width: 100%; height: 100%; height: 100dvh; }
input, textarea, select, .copyable { -webkit-user-select: text; user-select: text; -webkit-touch-callout: default; }
img, canvas { -webkit-user-drag: none; user-select: none; }
#camera { position: fixed; inset: 0; width: 100%; height: 100%; object-fit: cover; background: #000; }
#view { position: fixed; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
.hidden { display: none !important; }
button { font: inherit; cursor: pointer; color: inherit; }
input, select, textarea { font: inherit; font-size: 16px; } /* ≥ 16 px : pas de zoom iOS */

/* ----- Écrans (accueil, salon, périmètre, fin) ----- */
.screen {
  position: fixed; inset: 0; z-index: 10; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  display: grid; grid-template-columns: minmax(0, 1fr); justify-items: center;
  padding: var(--sa-t) var(--sa-r) var(--sa-b) var(--sa-l);
}
.screen > * { margin-block: auto; } /* centré, sans couper le haut quand le contenu dépasse */
.screen.dim { background: radial-gradient(ellipse at center, rgba(20, 12, 4, 0.25), rgba(20, 12, 4, 0.7)); }
.panel {
  width: 100%; max-width: 720px; padding: 18px 20px; text-align: center;
  border: 14px solid transparent; border-image: url(../assets/ui/panel_brown.png) 28 fill / 14px round;
  filter: drop-shadow(0 8px 14px rgba(20, 12, 4, 0.45));
  color: var(--ink); font-size: 18px; line-height: 1.35;
}
.panel.small { max-width: 420px; }
.panel.dark { border-image-source: url(../assets/ui/panel_brown_dark.png); color: #fff; text-shadow: 0 1px 0 rgba(0, 0, 0, 0.5); }
.panel > :first-child { margin-top: 0; }
.panel > :last-child { margin-bottom: 0; }
.title {
  margin: 0 auto 12px; max-width: 100%; padding: 14px 28px 18px; min-height: 64px;
  border: 0 solid transparent; border-width: 0 24px;
  border-image: url(../assets/ui/banner_hanging.png) 0 48 fill / 0 24px stretch;
  color: #fff; font-size: clamp(26px, 7vw, 44px); font-weight: 900; line-height: 1.1; letter-spacing: 0.5px;
  text-shadow: 0 3px 0 #8b2620, 0 0 2px #8b2620;
}
.subtitle { margin: 4px 0 12px; font-size: 18px; color: var(--brown-dark); }
.help { font-size: 15px; font-weight: 600; opacity: 0.85; margin: 12px 0 0; }
.error { color: #a1170f; font-weight: 800; margin: 8px 0; }
.row { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; align-items: center; }
.stack { display: flex; flex-direction: column; gap: 10px; align-items: stretch; }
h1, h2, h3 { margin: 0 0 8px; line-height: 1.15; }

/* ----- Boutons (images button_*.png, 9 tranches) ----- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 52px; min-width: 52px; padding: 2px 10px 4px; margin: 0;
  border: 12px solid transparent; border-bottom-width: 14px;
  border-image: url(../assets/ui/button_brown.png) 16 16 18 fill / 12px 12px 14px stretch;
  background: none; color: var(--ink); font-size: 20px; font-weight: 900; line-height: 1.1;
  filter: drop-shadow(0 3px 0 rgba(20, 12, 4, 0.35));
  transition: transform 0.06s;
}
.btn.primary { border-image-source: url(../assets/ui/button_red.png); color: #fff; text-shadow: 0 2px 0 #7a1d17; }
.btn.ghost { border-image-source: url(../assets/ui/button_grey.png); color: #fff; text-shadow: 0 2px 0 #2c3846; }
.btn.big { min-height: 64px; font-size: 24px; padding-left: 30px; padding-right: 30px; }
.btn:active, .btn.on { transform: translateY(3px); filter: none; }
.btn:focus-visible, .tbtn:focus-visible, .car:focus-visible { outline: 4px solid var(--accent); outline-offset: 2px; }
.btn:disabled { border-image-source: url(../assets/ui/button_grey.png); color: #d6dbe0; text-shadow: none; filter: grayscale(0.6); opacity: 0.7; cursor: default; }
.btn.icon {
  width: 56px; height: 56px; padding: 0; border: 0; border-image: none; border-radius: 50%;
  background: url(../assets/ui/round_brown.png) center / 100% 100% no-repeat;
}
.btn.icon > i { width: 28px; height: 28px; filter: brightness(0.25); }
[class^="ico-"], [class*=" ico-"] { display: inline-block; width: 1.3em; height: 1.3em; flex: none; background: center / contain no-repeat; }
.ico-play { background-image: url(../assets/controls/icon_play.png); }
.ico-pause { background-image: url(../assets/controls/icon_pause.png); }
.ico-cog { background-image: url(../assets/controls/icon_cog.png); }
.ico-check { background-image: url(../assets/controls/icon_checkmark.png); }
.ico-cross { background-image: url(../assets/controls/icon_cross.png); }
.ico-retry { background-image: url(../assets/controls/icon_arrow_rotate.png); }
.ico-wheel { background-image: url(../assets/controls/icon_steering_wheel.png); }
.ico-star { background-image: url(../assets/controls/icon_star.png); }
.btn.primary [class^="ico-"], .btn.ghost [class^="ico-"] { filter: drop-shadow(0 2px 0 rgba(0, 0, 0, 0.4)); }
.btn:not(.primary):not(.ghost):not(.icon) [class^="ico-"] { filter: brightness(0.25); }

/* ----- Champs ----- */
.field { display: block; text-align: left; margin: 10px auto; max-width: 380px; font-size: 17px; }
.field input, .field select, input.code {
  display: block; width: 100%; margin-top: 4px; min-height: 48px; padding: 8px 12px;
  border: 3px solid var(--brown); border-radius: 10px; background: #fff; color: var(--ink); font-size: 18px; font-weight: 700;
}
input.code { display: inline-block; width: 7.5em; text-align: center; text-transform: uppercase; letter-spacing: 6px; font-size: 24px; font-weight: 900; }
.field input:focus, .field select:focus, input.code:focus { outline: 3px solid var(--accent); outline-offset: 1px; }
input[type=checkbox].check {
  -webkit-appearance: none; appearance: none; width: 40px; height: 40px; margin: 0 6px 0 0; vertical-align: middle; cursor: pointer;
  background: url(../assets/ui/checkbox_brown_empty.png) center / contain no-repeat;
}
input[type=checkbox].check:checked { background-image: url(../assets/ui/checkbox_brown_checked.png); }

/* ----- Barre de chargement (progress_*.png, pilules étirées) ----- */
.bar {
  height: 28px; margin: 12px 0 6px; position: relative;
  border: 0 solid transparent; border-width: 0 14px; border-image: url(../assets/ui/progress_transparent.png) 32 16 fill / 0 14px stretch;
}
.bar > .fill {
  position: absolute; top: 0; bottom: 0; left: -14px; width: 0; min-width: 28px; max-width: calc(100% + 28px);
  border: 0 solid transparent; border-width: 0 14px; border-image: url(../assets/ui/progress_green.png) 32 16 fill / 0 14px stretch;
  transition: width 0.2s;
}

/* ----- Salon ----- */
.code-big { font-size: clamp(40px, 12vw, 64px); font-weight: 900; letter-spacing: 8px; color: var(--red); text-shadow: 0 3px 0 #7a1d17; }
.players { list-style: none; margin: 8px 0; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 8px; text-align: left; }
.player {
  display: flex; align-items: center; gap: 10px; min-height: 52px; padding: 6px 10px; min-width: 0;
  background: #fff; border: 3px solid #e2c9a0; border-radius: 12px;
}
.player.me { border-color: var(--red); }
.player.bot { opacity: 0.7; }
.player.off { opacity: 0.5; font-style: italic; }
.player .car-dot { width: 26px; height: 26px; border-radius: 50%; flex: none; background: var(--c, #888); border: 3px solid var(--ink); }
.player .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 18px; }
.tag { flex: none; font-size: 13px; font-weight: 900; padding: 2px 8px; border-radius: 8px; background: #e2c9a0; color: var(--ink); }
.player.host .tag, .tag.hot { background: var(--red); color: #fff; }
.cars { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 8px 0; }
.car {
  position: relative; aspect-ratio: 1; padding: 6px; display: grid; place-items: center; cursor: pointer;
  border: 0; background: url(../assets/ui/round_brown.png) center / 100% 100% no-repeat;
}
.car > img, .car > canvas { width: 100%; height: 100%; object-fit: contain; pointer-events: none; }
.car.sel { background-image: url(../assets/ui/round_grey_dark.png); outline: 4px solid var(--accent); outline-offset: -2px; border-radius: 50%; }
.car.taken { opacity: 0.4; filter: grayscale(1); cursor: default; }

/* ----- HUD de course ----- */
#hud { position: fixed; inset: 0; z-index: 5; pointer-events: none; color: var(--hud-ink); }
.hud-top {
  position: absolute; top: var(--sa-t); left: var(--sa-l); right: var(--sa-r);
  display: flex; align-items: flex-start; gap: 8px;
}
.pix { image-rendering: pixelated; }
.hud-pos {
  width: 84px; height: 84px; flex: none; display: flex; align-items: baseline; justify-content: center; padding-top: 16px;
  background: url(../assets/pixel/badge_round.png) center / 100% 100% no-repeat; image-rendering: pixelated;
  color: var(--ink); text-shadow: none;
}
.hud-pos b { font-size: 40px; font-weight: 900; line-height: 1; }
.hud-pos small { font-size: 18px; font-weight: 900; }
.hud-lap {
  height: 64px; min-width: 150px; padding: 0 44px; display: flex; align-items: center; justify-content: center;
  background: url(../assets/pixel/banner_left.png) left center / 64px 64px no-repeat,
              url(../assets/pixel/banner_right.png) right center / 64px 64px no-repeat,
              url(../assets/pixel/banner_mid.png) center / 64px 64px repeat-x;
  background-clip: border-box; image-rendering: pixelated;
  font-size: 30px; font-weight: 900; text-shadow: 0 3px 0 #7a1d17, 0 0 3px #7a1d17;
}
.hud-time, .coins {
  min-height: 48px; padding: 6px 14px; display: flex; align-items: center; gap: 6px;
  background: var(--hud-bg); border: 3px solid rgba(255, 255, 255, 0.85); border-radius: 12px;
  font-size: 24px; font-weight: 900; font-variant-numeric: tabular-nums; text-shadow: 0 2px 0 #000;
}
.hud-time { margin-left: auto; }
.coins::before { content: ""; width: 26px; height: 26px; background: url(../assets/controls/icon_money.png) center / contain no-repeat; }
.hud-item {
  width: 84px; height: 84px; flex: none; display: grid; place-items: center;
  background: url(../assets/pixel/slot_hex.png) center / 100% 100% no-repeat; image-rendering: pixelated;
}
.hud-item > img, .hud-item > canvas { width: 58%; height: 58%; object-fit: contain; image-rendering: auto; }
.hud-item.empty > * { visibility: hidden; }
.countdown {
  position: absolute; top: 45%; left: 50%; transform: translate(-50%, -50%);
  font-size: clamp(90px, 30vmin, 180px); font-weight: 900; line-height: 1; color: #fff; text-shadow: var(--outline);
  animation: pop 0.9s ease-out both;
}
.countdown.go { color: var(--accent); }
@keyframes pop { from { transform: translate(-50%, -50%) scale(1.6); opacity: 0; } 25% { opacity: 1; } to { transform: translate(-50%, -50%) scale(1); } }
.toast {
  position: absolute; left: 50%; top: calc(var(--sa-t) + 96px); transform: translateX(-50%); max-width: calc(100% - 32px);
  padding: 10px 20px; border-radius: 14px; background: var(--hud-bg); border: 3px solid var(--accent);
  font-size: 22px; font-weight: 900; text-align: center; text-shadow: 0 2px 0 #000;
}
.ar-lost {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); max-width: calc(100% - 32px);
  padding: 14px 22px; border-radius: 14px; background: rgba(161, 23, 15, 0.92); border: 3px solid #fff;
  font-size: 22px; font-weight: 900; text-align: center; animation: blink 1s steps(2, jump-none) infinite;
}
@keyframes blink { from { opacity: 1; } to { opacity: 0.6; } }

/* ----- Commandes tactiles (mobile-controls, style A + surbrillance A) ----- */
/* :not(body) : la classe body.touch (appareil tactile) ne doit pas masquer ni figer toute la page (lot G) */
.touch:not(body) { display: none; position: fixed; inset: 0; z-index: 6; pointer-events: none; }
body.touch .touch { display: block; }
.touch:not(body), .touch:not(body) * { touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.tbtn {
  --s: 88px;
  position: absolute; width: var(--s); height: var(--s); padding: 0; border: 0; pointer-events: auto;
  background: var(--icon) center / 52% no-repeat, var(--base) center / 100% 100% no-repeat;
  filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.55));
}
.tbtn.on { background: var(--hl) center / 100% 100% no-repeat, var(--icon) center / 52% no-repeat, var(--base) center / 100% 100% no-repeat; transform: scale(0.94); }
.tbtn.t-left, .tbtn.t-right { --s: 96px; background: var(--base) center / 100% 100% no-repeat; }
.tbtn.t-left.on, .tbtn.t-right.on { background: var(--hl) center / 100% 100% no-repeat, var(--base) center / 100% 100% no-repeat; }
.t-left { --base: url(../assets/controls/direction_left.png); --hl: url(../assets/controls/direction_left_highlight.png); left: var(--sa-l); bottom: var(--sa-b); }
.t-right { --base: url(../assets/controls/direction_right.png); --hl: url(../assets/controls/direction_right_highlight.png); left: calc(var(--sa-l) + 108px); bottom: var(--sa-b); }
.t-gas, .t-brake, .t-item { --base: url(../assets/controls/button_circle.png); --hl: url(../assets/controls/button_circle_highlight.png); }
.t-gas { --s: 104px; --icon: url(../assets/controls/icon_pedal.png); right: var(--sa-r); bottom: var(--sa-b); }
.t-brake { --s: 76px; --icon: url(../assets/controls/icon_pedal_brake.png); right: calc(var(--sa-r) + 14px); bottom: calc(var(--sa-b) + 116px); }
.t-item { --s: 84px; --icon: url(../assets/controls/icon_star.png); right: calc(var(--sa-r) + 96px); bottom: calc(var(--sa-b) + 124px); }
.t-pause {
  --s: 56px; --base: url(../assets/controls/button_square.png); --hl: url(../assets/controls/button_square_highlight.png);
  --icon: url(../assets/controls/icon_pause.png); right: var(--sa-r); top: calc(var(--sa-t) + 92px);
}
.t-item.empty { opacity: 0.45; }
/* paysage sur téléphone : boutons un peu plus petits, objet à côté du frein */
@media (orientation: landscape) and (max-height: 500px) {
  .tbtn.t-left, .tbtn.t-right { --s: 84px; }
  .t-right { left: calc(var(--sa-l) + 94px); }
  .t-gas { --s: 92px; }
  .t-brake { --s: 70px; right: calc(var(--sa-r) + 102px); bottom: var(--sa-b); }
  .t-item { --s: 70px; right: calc(var(--sa-r) + 182px); bottom: var(--sa-b); }
  .t-pause { top: var(--sa-t); right: calc(var(--sa-r) + 96px); }
  .hud-pos, .hud-item { width: 68px; height: 68px; padding-top: 12px; }
  .hud-pos b { font-size: 32px; }
  .hud-lap { height: 52px; font-size: 24px; background-size: 52px 52px; }
  .countdown { font-size: clamp(80px, 40vmin, 150px); }
}

/* ----- Fin de course : podium et classement ----- */
.podium { display: flex; justify-content: center; align-items: flex-end; gap: 10px; margin: 8px 0 14px; }
.step { width: 31%; max-width: 170px; display: flex; flex-direction: column; align-items: center; min-width: 0; }
.step.p1 { order: 2; } .step.p2 { order: 1; } .step.p3 { order: 3; }
.step .name { font-size: 17px; font-weight: 900; max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.step .block {
  width: 100%; display: grid; place-items: start center; padding-top: 6px;
  border: 10px solid transparent; border-image: url(../assets/ui/panel_brown_dark.png) 24 fill / 10px round;
  color: #fff; font-size: 34px; font-weight: 900; text-shadow: 0 2px 0 #000;
}
.step.p1 .block { height: 130px; } .step.p2 .block { height: 96px; } .step.p3 .block { height: 70px; }
.medal { display: inline-block; width: 56px; height: 84px; background: center / contain no-repeat; flex: none; }
.step .medal { width: 64px; height: 96px; margin-bottom: 2px; }
.medal.gold { background-image: url(../assets/medals/shaded_medal4.png); }
.medal.silver { background-image: url(../assets/medals/shaded_medal3.png); }
.medal.bronze { background-image: url(../assets/medals/shaded_medal2.png); }
.medal.best { background-image: url(../assets/medals/shaded_medal8.png); }
.results { width: 100%; border-collapse: collapse; font-size: 17px; text-align: left; }
.results td, .results th { padding: 8px 6px; border-bottom: 2px solid #e2c9a0; }
.results .num { text-align: right; font-variant-numeric: tabular-nums; }
.results tr.me { background: rgba(217, 83, 74, 0.15); }

/* ----- Petits écrans ----- */
@media (max-width: 480px) {
  .panel { padding: 12px 10px; font-size: 17px; border-width: 12px; }
  .cars { grid-template-columns: repeat(4, 1fr); gap: 4px; }
  .players { grid-template-columns: 1fr; }
  .hud-lap { min-width: 120px; padding: 0 36px; height: 56px; font-size: 26px; background-size: 56px 56px; }
  .hud-time { font-size: 20px; padding: 4px 10px; }
  .btn.big { width: 100%; }
}
@media (prefers-reduced-motion: reduce) {
  .countdown, .ar-lost { animation: none; }
  .btn, .bar > .fill { transition: none; }
}
