:root {
  --pix: 'Press Start 2P', 'Courier New', monospace;
  --panel: rgba(12, 12, 32, 0.92);
  --gold: #ffd23f;
  --red: #e8322e;
  --blue: #2a7fff;
  --cyan: #3ff0ff;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  height: 100%;
  background: #05050c;
  overflow: hidden;
  font-family: var(--pix);
  color: #fff;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}
body { touch-action: none; }
#wrap { position: fixed; left: 50%; top: 50%; transform: translate(-50%, -50%); width: 384px; height: 216px; }
#screen {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
}
canvas.pix { image-rendering: pixelated; image-rendering: crisp-edges; }

/* ---------- UI ---------- */
#ui { position: fixed; inset: 0; pointer-events: none; font-size: clamp(9px, 0.9vmin + 5px, 14px); line-height: 1.6; }
#ui .screen {
  position: absolute;
  inset: 0;
  pointer-events: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-y: auto;
  padding: 1.2em 1em 2em;
  gap: 1.2em;
  background: rgba(4, 4, 14, 0.55);
  touch-action: pan-y;
}
#ui .screen.dim { background: rgba(4, 4, 14, 0.82); }
.btn {
  font-family: var(--pix);
  font-size: 1em;
  color: #fff;
  background: #2c2c52;
  border: 0;
  padding: 0.9em 1.3em;
  cursor: pointer;
  text-transform: uppercase;
  box-shadow: inset -3px -3px 0 rgba(0, 0, 0, 0.45), inset 3px 3px 0 rgba(255, 255, 255, 0.22), 0 0 0 3px #000;
  line-height: 1.3;
}
.btn:hover, .btn:focus-visible { background: #40407a; outline: none; }
.btn:active { transform: translateY(2px); }
.btn.primary { background: var(--red); }
.btn.primary:hover, .btn.primary:focus-visible { background: #ff4f49; }
.btn.gold { background: #d9a514; color: #2a1800; }
.btn.gold:hover { background: #f5c542; }
.btn.small { font-size: 0.8em; padding: 0.6em 0.8em; }
.btn.big { font-size: 1.35em; padding: 1em 2em; }
.btn.sel { background: var(--blue); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }

.topbar { width: 100%; max-width: 1100px; display: flex; justify-content: space-between; align-items: center; gap: 1em; flex-wrap: wrap; }
.wallet { display: flex; gap: 1.4em; align-items: center; background: rgba(0, 0, 0, 0.6); padding: 0.6em 1em; box-shadow: 0 0 0 3px #000; }
.ico-crys { display: inline-block; width: 0.8em; height: 0.8em; background: var(--cyan); transform: rotate(45deg); box-shadow: 0 0 0 2px #0b5a6a; margin-right: 0.6em; }
.ico-coin { display: inline-block; width: 0.95em; height: 0.95em; border-radius: 50%; background: var(--gold); box-shadow: inset -2px -2px 0 #c99a10, 0 0 0 2px #5a3a00; margin-right: 0.5em; vertical-align: -0.1em; }
h2.title { font-size: 1.6em; color: var(--gold); text-shadow: 3px 3px 0 #000; text-align: center; }
.logo { text-align: center; color: var(--gold); font-size: 2.8em; line-height: 1.25; text-shadow: 4px 4px 0 var(--red), 8px 8px 0 #000; margin-top: 0.6em; }
.logo small { display: block; font-size: 0.32em; color: #fff; text-shadow: 2px 2px 0 #000; margin-top: 1em; letter-spacing: 0.1em; }
.menu { display: flex; flex-direction: column; gap: 1em; align-items: stretch; min-width: 16em; }
.help { font-size: 0.75em; color: #cfd6e4; text-align: center; max-width: 60em; background: rgba(0, 0, 0, 0.6); padding: 0.8em 1em; line-height: 2; }
.panel { background: var(--panel); box-shadow: 0 0 0 3px #000, inset 0 0 0 3px #34346a; padding: 1.1em; }
.row { display: flex; gap: 1em; flex-wrap: wrap; justify-content: center; align-items: center; }
.muted { color: #9aa3b8; }
.r0 { color: #c8d0dc; } .r1 { color: #5a9cff; } .r2 { color: #c47aff; } .r3 { color: #ffa53a; }
.bg-r0 { box-shadow: 0 0 0 3px #000, inset 0 0 0 3px #7a8290 !important; }
.bg-r1 { box-shadow: 0 0 0 3px #000, inset 0 0 0 3px #3f8cff !important; }
.bg-r2 { box-shadow: 0 0 0 3px #000, inset 0 0 0 3px #b65cff !important; }
.bg-r3 { box-shadow: 0 0 0 3px #000, inset 0 0 0 3px #ff9a1a !important; }

/* Choix du circuit */
.tracks { display: flex; gap: 1.2em; flex-wrap: wrap; justify-content: center; }
.tcard { width: 15em; cursor: pointer; text-align: center; display: flex; flex-direction: column; gap: 0.6em; align-items: center; }
.tcard canvas { width: 9em; height: 9em; }
.tcard.sel { box-shadow: 0 0 0 3px #000, inset 0 0 0 4px var(--gold); }
.tcard .lvl { font-size: 0.8em; }
.tcard .rec { font-size: 0.7em; color: #bfe8ff; }

/* Garage */
.garage { display: flex; gap: 1.2em; width: 100%; max-width: 1100px; flex-wrap: wrap; align-items: flex-start; justify-content: center; }
.g-left { flex: 1 1 18em; max-width: 26em; display: flex; flex-direction: column; gap: 1em; }
.g-right { flex: 2 1 22em; display: flex; flex-direction: column; gap: 1em; min-width: 0; }
.g-bike { width: 100%; aspect-ratio: 4 / 3; background: #1a1a34; }
.stat { display: grid; grid-template-columns: auto 1fr 3em; gap: 0.6em; align-items: center; font-size: 0.7em; margin: 0.35em 0; }
.stat > span:first-child { white-space: nowrap; min-width: 10.5em; }
.bar { height: 0.9em; background: #000; position: relative; box-shadow: 0 0 0 2px #34346a; overflow: hidden; }
.bar i { position: absolute; top: 0; bottom: 0; left: 0; display: block; }
.bar .base { background: #3fd16b; }
.bar .up { background: #a8ff6a; }
.bar .down { background: #e8322e; }
.sets div { font-size: 0.7em; margin: 0.3em 0; }
.slots { display: grid; grid-template-columns: repeat(auto-fill, minmax(7.5em, 1fr)); gap: 0.6em; }
.slot { font-family: var(--pix); font-size: 0.65em; color: #fff; background: #22223f; border: 0; padding: 0.6em 0.4em; cursor: pointer; display: flex; flex-direction: column; align-items: center; gap: 0.4em; box-shadow: 0 0 0 3px #000; line-height: 1.4; text-align: center; }
.slot.sel { background: #3a3a74; box-shadow: 0 0 0 3px #000, inset 0 0 0 3px var(--gold); }
.slot canvas { width: 32px; height: 32px; }
.slot .eq { font-size: 0.85em; color: #9aa3b8; min-height: 2.8em; }
.inv { display: flex; flex-direction: column; gap: 0.7em; }
.item { display: grid; grid-template-columns: 52px 1fr auto; gap: 0.8em; align-items: center; background: #1a1a34; padding: 0.7em; cursor: pointer; }
.item.sel { background: #2a2a54; }
.item canvas { width: 48px; height: 48px; background: #0d0d1d; }
.item .nm { font-size: 0.85em; }
.item .meta { font-size: 0.65em; color: #9aa3b8; margin-top: 0.3em; }
.item .st { font-size: 0.65em; color: #a8ff6a; margin-top: 0.3em; }
.item .st .neg { color: #ff6a6a; }
.item .desc { font-size: 0.62em; color: #ffd9a0; margin-top: 0.3em; }
.item .acts { display: flex; flex-direction: column; gap: 0.5em; }
.empty { font-size: 0.75em; color: #9aa3b8; text-align: center; padding: 1.5em; line-height: 2; }

/* Coffres */
.chests { display: grid; grid-template-columns: repeat(auto-fill, minmax(12em, 1fr)); gap: 1.2em; width: 100%; max-width: 1100px; }
.chest { display: flex; flex-direction: column; align-items: center; gap: 0.6em; text-align: center; }
.chest canvas { width: 10em; height: 7.5em; }
.chest .nm { font-size: 0.85em; color: var(--gold); }
.odds { font-size: 0.62em; line-height: 1.9; }
.opening { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1em; background: rgba(2, 2, 8, 0.94); pointer-events: auto; padding: 1em; }
.opening canvas { width: min(90vw, 96vh); height: auto; aspect-ratio: 4 / 3; max-height: 62vh; }
.reveal { text-align: center; display: flex; flex-direction: column; gap: 0.6em; align-items: center; min-height: 8em; }
.reveal .big { font-size: 1.4em; }
.reveal .tag { font-size: 0.9em; color: var(--gold); }

/* Résultats */
table.res { border-collapse: collapse; font-size: 0.72em; }
table.res td { padding: 0.45em 0.8em; }
table.res tr:nth-child(odd) { background: rgba(255, 255, 255, 0.05); }
table.res tr.me { background: rgba(255, 210, 63, 0.25); color: var(--gold); }
.gains { font-size: 1.1em; display: flex; gap: 2em; flex-wrap: wrap; justify-content: center; }
.toast { position: fixed; left: 50%; bottom: 1.5em; transform: translateX(-50%); background: #000; padding: 0.8em 1.2em; box-shadow: 0 0 0 3px var(--gold); font-size: 0.8em; pointer-events: none; z-index: 10; }

/* ---------- Boutons tactiles ---------- */
#touch { position: fixed; inset: 0; pointer-events: none; z-index: 5; --ab: max(21vmin, 76px); --sb: max(15vmin, 56px); --gap: 2.5vmin; }
#touch.hidden { display: none; }
#touch button, #touch .t-steer {
  pointer-events: auto;
  position: absolute;
  border: 3px solid rgba(255, 255, 255, 0.6);
  background: rgba(0, 0, 0, 0.3);
  color: #fff;
  font-family: var(--pix);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}
#touch button { border-radius: 50%; font-size: clamp(16px, 4vmin, 30px); }
#touch button.on { background: rgba(255, 255, 255, 0.4); }
#touch .t-steer { left: 3vmin; bottom: 4vmin; width: max(42vmin, 150px); height: var(--sb); border-radius: 999px; display: flex; overflow: hidden; }
#touch .t-steer span { flex: 1; display: flex; align-items: center; justify-content: center; font-size: clamp(16px, 4.5vmin, 32px); }
#touch .t-steer .l { border-right: 2px solid rgba(255, 255, 255, 0.4); }
#touch .t-steer span.on { background: rgba(255, 255, 255, 0.4); }
#touch .t-up { right: 3vmin; bottom: 4vmin; width: var(--ab); height: var(--ab); background: rgba(63, 209, 107, 0.35); }
#touch .t-down { right: calc(3vmin + (var(--ab) - var(--sb)) / 2); bottom: calc(4vmin + var(--ab) + var(--gap)); width: var(--sb); height: var(--sb); background: rgba(232, 50, 46, 0.35); }
#touch .t-turbo { right: calc(3vmin + var(--ab) + var(--gap)); bottom: 4vmin; width: var(--sb); height: var(--sb); font-size: clamp(8px, 1.7vmin, 13px); background: rgba(255, 122, 26, 0.45); }
#touch .t-pause { left: 3vmin; top: 3vmin; width: 11vmin; height: 11vmin; min-width: 40px; min-height: 40px; font-size: clamp(10px, 2.4vmin, 18px); }

/* Message « tourne ton téléphone » en portrait sur écran tactile */
#rotate { display: none; position: fixed; inset: 0; z-index: 20; background: #05050c; color: var(--gold); align-items: center; justify-content: center; text-align: center; font-size: 14px; line-height: 2.2; padding: 2em; }
@media (orientation: portrait) and (pointer: coarse) { #rotate { display: flex; } }

.lvlrow { width: 100%; max-width: 1100px; display: flex; flex-direction: column; align-items: center; gap: 0.6em; }
.lvlname { font-size: 0.9em; }
.score { font-size: 0.75em; }
.score b { color: var(--gold); }

/* ---------- Mode en ligne ---------- */
.form { display: flex; flex-direction: column; gap: 0.9em; min-width: min(30em, 92vw); }
.form label { display: flex; flex-direction: column; gap: 0.4em; font-size: 0.75em; }
.form .row label { font-size: 0.7em; }
#ui input, #ui select {
  font-family: var(--pix); font-size: 1em; color: #fff; background: #0d0d1d; border: 0;
  padding: 0.7em 0.8em; box-shadow: 0 0 0 3px #000, inset 0 0 0 2px #34346a; outline: none;
  -webkit-user-select: text; user-select: text;
}
#ui input:focus, #ui select:focus { box-shadow: 0 0 0 3px #000, inset 0 0 0 2px var(--gold); }
.err { color: #ff6a6a; font-size: 0.7em; min-height: 1.2em; }
.acct { font-size: 0.75em; }
.players { display: flex; flex-wrap: wrap; gap: 1em; justify-content: center; }
.pl { display: flex; align-items: center; gap: 0.6em; background: #1a1a34; padding: 0.5em 0.8em 0.5em 0.4em; font-size: 0.8em; }
.pl canvas { width: 64px; height: 48px; }
