:root {
  --bg: #0d0f14;
  --panel: #171a22;
  --panel2: #1f2330;
  --line: #2c3142;
  --text: #e9ecf3;
  --muted: #8d94a8;
  --accent: #ff5a1f;
  --accent2: #ffc21a;
  --good: #33d17a;
  --bad: #ff4d5e;
  --blue: #3fa9ff;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--bad); min-height: 1.2em; margin: 6px 0 0; }
h2 { margin: 0 0 12px; font-size: 20px; }
h3 { margin: 0 0 8px; font-size: 16px; }

.logo { font-weight: 900; font-style: italic; letter-spacing: 1px; font-size: 40px; line-height: 1; margin: 0; }
.logo span { color: var(--accent); margin-left: 6px; }
.logo.small { font-size: 20px; }

.btn {
  background: var(--panel2); color: var(--text); border: 1px solid var(--line);
  padding: 9px 16px; border-radius: 8px; font: inherit; font-weight: 600; cursor: pointer;
}
.btn:hover:not(:disabled) { border-color: var(--muted); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.good { background: var(--good); border-color: var(--good); color: #062; }
.btn.danger { color: var(--bad); }
.btn.big { padding: 13px 22px; font-size: 17px; width: 100%; }
.link { background: none; border: none; color: var(--muted); cursor: pointer; text-decoration: underline; font: inherit; }

input, select {
  width: 100%; background: var(--bg); color: var(--text); border: 1px solid var(--line);
  border-radius: 8px; padding: 9px 10px; font: inherit; margin-top: 4px;
}
label { display: block; margin-bottom: 12px; font-size: 13px; color: var(--muted); }
label.check { display: flex; gap: 8px; align-items: center; }
label.check input { width: auto; margin: 0; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; }

/* Entrada */
#screen-login { min-height: 100%; display: grid; place-items: center; padding: 16px;
  background: radial-gradient(circle at 50% 0%, #3a1a0c 0%, var(--bg) 60%); }
.login-box { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 28px 24px; }
.login-box .logo { font-size: 34px; margin-bottom: 6px; }

/* Barra superior */
.topbar { display: flex; align-items: center; gap: 16px; padding: 10px 16px; background: var(--panel);
  border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; flex-wrap: wrap; }
#tabs { display: flex; gap: 4px; flex: 1; overflow-x: auto; }
#tabs button { background: none; border: none; color: var(--muted); padding: 8px 12px; border-radius: 8px;
  font: inherit; font-weight: 600; cursor: pointer; white-space: nowrap; }
#tabs button.active { background: var(--panel2); color: var(--text); }
.wallet { white-space: nowrap; }
.wallet b { color: var(--accent2); }
main { max-width: 1100px; margin: 0 auto; padding: 20px 16px 60px; }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 16px; margin-bottom: 16px; }
.empty { text-align: center; padding: 50px 16px; }

/* Garaje */
.cars { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.car-card { background: var(--panel); border: 2px solid var(--line); border-radius: 12px; overflow: hidden; display: flex; flex-direction: column; }
.car-card.active { border-color: var(--accent); }
.car-photo { aspect-ratio: 16/8; background: #000 center/contain no-repeat; position: relative; }
.car-photo.flip { transform: scaleX(-1); }
.car-card .body { padding: 12px 14px; flex: 1; display: flex; flex-direction: column; gap: 8px; }
.car-card .name { font-weight: 700; font-size: 16px; }
.badge { display: inline-block; background: var(--accent); color: #fff; font-size: 11px; font-weight: 800;
  padding: 2px 7px; border-radius: 5px; vertical-align: middle; margin-left: 6px; }
.specs { display: grid; grid-template-columns: 1fr 1fr; gap: 2px 10px; font-size: 13px; color: var(--muted); }
.specs b { color: var(--text); }
.car-card .row { margin-top: auto; }

/* Taller */
.upgrades { display: grid; grid-template-columns: repeat(auto-fill, minmax(300px, 1fr)); gap: 12px; }
.upg { background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 14px; display: flex; flex-direction: column; gap: 8px; }
.pips { display: flex; gap: 4px; }
.pips i { flex: 1; height: 8px; border-radius: 3px; background: var(--line); }
.pips i.on { background: var(--accent); }
.statline { display: flex; gap: 24px; flex-wrap: wrap; }
.stat { display: flex; flex-direction: column; }
.stat span { font-size: 12px; color: var(--muted); }
.stat b { font-size: 22px; }

/* Transmisión */
.gear-row { display: grid; grid-template-columns: 90px 1fr 60px; gap: 12px; align-items: center; margin-bottom: 6px; }
.gear-row input[type=range] { margin: 0; padding: 0; accent-color: var(--accent); }
.gear-row output { text-align: right; font-variant-numeric: tabular-nums; font-weight: 700; }
#gear-chart { width: 100%; height: 240px; background: var(--bg); border-radius: 8px; display: block; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* Multijugador */
.list { display: flex; flex-direction: column; gap: 6px; }
.list .item { display: flex; justify-content: space-between; align-items: center; background: var(--panel2);
  border-radius: 8px; padding: 8px 12px; gap: 10px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--good); display: inline-block; margin-right: 8px; }
.code { font-family: ui-monospace, Consolas, monospace; font-size: 40px; font-weight: 900; letter-spacing: 8px; color: var(--accent2); }
.versus { display: grid; grid-template-columns: 1fr auto 1fr; gap: 12px; align-items: center; }
.versus .vs { font-size: 28px; font-weight: 900; font-style: italic; color: var(--accent); }
.slot { background: var(--panel2); border-radius: 10px; padding: 12px; text-align: center; min-height: 160px; }
.slot .car-photo { border-radius: 8px; margin-bottom: 8px; }
.ready-tag { font-weight: 800; color: var(--good); }
.wait-tag { color: var(--muted); }

table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-weight: 600; font-size: 13px; }

/* Carrera */
#screen-race { position: fixed; inset: 0; background: #000; z-index: 20; }
#race-canvas, #race-3d { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
#race-3d.hidden { display: none; }
.mute-btn.gfx-btn { left: 58px; font: 800 13px system-ui, sans-serif; padding: 7px 9px; }
.race-help { position: absolute; top: 10px; left: 50%; transform: translateX(-50%); color: #fff9;
  font-size: 13px; background: #0008; padding: 5px 12px; border-radius: 8px; white-space: nowrap; }
.touch-controls { position: absolute; bottom: 16px; left: 16px; right: 16px; display: none; justify-content: space-between; gap: 10px; pointer-events: none; }
.tbtn { pointer-events: auto; border: none; border-radius: 14px; font: 800 16px system-ui, sans-serif; color: #fff;
  padding: 22px 0; width: 30%; max-width: 160px; opacity: .85; touch-action: none; user-select: none; -webkit-user-select: none; }
.tbtn.gas { background: #2f8f4e; }
.tbtn.nitro { background: #2c6fd6; }
.tbtn.shift { background: var(--accent); }
.tbtn:active { opacity: 1; transform: scale(.97); }
@media (pointer: coarse) {
  .touch-controls { display: flex; }
  .race-help { display: none; }
}
.race-result { position: absolute; inset: 0; display: grid; place-items: center; background: #000b; padding: 16px; }
.result-box { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 22px; width: 100%; max-width: 560px; }
.result-title { font-size: 30px; font-weight: 900; font-style: italic; margin: 0 0 12px; }
.result-title.win { color: var(--good); }
.result-title.lose { color: var(--bad); }
.shift-list { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.chip { font-size: 12px; padding: 3px 8px; border-radius: 6px; background: var(--panel2); }
.chip.perfecto { background: #1d5d3a; color: #9ff0c0; }
.chip.bueno { background: #2d4a1d; color: #cbe8a3; }
.chip.tarde, .chip.temprano { background: #5a2a20; color: #ffb4a0; }

dialog { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 14px;
  padding: 22px; width: min(560px, calc(100vw - 32px)); max-height: calc(100vh - 32px); }
dialog::backdrop { background: #000a; }
.photo-preview { background: #000; border-radius: 8px; margin-bottom: 10px; text-align: center; }
.photo-preview img { max-width: 100%; max-height: 160px; display: block; margin: 0 auto; }
.photo-preview img:not([src]), .photo-preview img[src=""] { display: none; }

.mute-btn { position: absolute; top: 44px; left: 12px; background: #0008; border: 1px solid #fff3; color: #fff;
  border-radius: 8px; font-size: 18px; padding: 4px 8px; cursor: pointer; }
dialog.wide { width: min(760px, calc(100vw - 32px)); }
.look-stage { background: repeating-conic-gradient(#20242f 0% 25%, #181b23 0% 50%) 0 0 / 20px 20px; border-radius: 8px; }
#look-canvas { width: 100%; display: block; touch-action: none; cursor: grab; }
.car-photo.processed { background-size: 92% auto; }

.car-photo.has-bg::after {
  content: 'Foto con fondo · cámbiala en "Editar datos"'; position: absolute; left: 0; right: 0; bottom: 0;
  background: #000b; color: #ffc21a; font-size: 11px; font-weight: 700; text-align: center; padding: 3px;
}
details.tip { background: var(--panel2); border-radius: 8px; padding: 8px 12px; margin: 4px 0 10px; font-size: 13px; }
details.tip summary { cursor: pointer; font-weight: 700; color: var(--accent2); }
details.tip ol { margin: 8px 0; padding-left: 20px; }
.results-grid { display: grid; grid-template-columns: 1fr; gap: 10px; }
.results-grid .panel { margin-bottom: 0; }

/* Celulares: evitar el zoom automático de Safari y el zoom por doble toque */
html { touch-action: manipulation; -webkit-text-size-adjust: 100%; }
input, select { font-size: 16px; }

/* Pantallas bajitas (celular acostado): todo más compacto y con scroll */
.race-result { overflow-y: auto; }
@media (max-height: 500px) {
  .race-result { place-items: start center; padding: 10px; }
  .result-box { padding: 14px; max-width: 760px; }
  .result-title { font-size: 22px; margin-bottom: 8px; }
  .results-grid { grid-template-columns: 1fr 1fr; }
  .results-grid .panel { padding: 10px; }
  .result-box .stat b { font-size: 17px; }
  .result-box .statline { gap: 14px; }
  .tbtn { padding: 14px 0; font-size: 14px; }
  .touch-controls { bottom: 10px; }
  .mute-btn { top: 36px; }
  dialog { padding: 14px; }
}
@supports (padding: env(safe-area-inset-left)) {
  .touch-controls { left: max(16px, env(safe-area-inset-left)); right: max(16px, env(safe-area-inset-right)); }
  .mute-btn { margin-left: env(safe-area-inset-left); }
}

.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--panel2);
  border: 1px solid var(--line); padding: 10px 18px; border-radius: 10px; z-index: 50; max-width: calc(100vw - 32px); }
.toast.bad { border-color: var(--bad); }

@media (max-width: 720px) {
  .two-col, .grid2 { grid-template-columns: 1fr; }
  .logo { font-size: 30px; }
  .versus { grid-template-columns: 1fr; }
  .versus .vs { text-align: center; }
  .code { font-size: 32px; }
}
