:root {
  --bg: #0d0f12;
  --panel: rgba(16, 19, 24, 0.92);
  --line: rgba(255, 255, 255, 0.12);
  --text: #f3f4f6;
  --muted: #a3a9b4;
  --accent: #ffd60a;
  --accent-ink: #1a1500;
  --good: #34d399;
  --bad: #f87171;
  --gas: #22c55e;
  --brake: #ef4444;
  --hb: #f59e0b;
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text);
  font-family: Barlow, system-ui, sans-serif; overscroll-behavior: none;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
}
button { font: inherit; color: inherit; cursor: pointer; }
[hidden] { display: none !important; }

#map { position: fixed; inset: 0; background: #1d2416; }

.maplibregl-ctrl-attrib { font-size: 10px; opacity: .7; }
.maplibregl-ctrl-bottom-right { bottom: calc(2px + var(--safe-b)); top: auto; right: auto; left: 50%; transform: translateX(-50%); z-index: 1; }
.maplibregl-ctrl-attrib.maplibregl-compact { background: rgba(0,0,0,.35); }
.maplibregl-ctrl-attrib-inner { color: #ddd; font-size: 9px; }
.maplibregl-ctrl-attrib a { color: #ddd; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; font-family: 'Barlow Condensed', sans-serif; }
.hud-top {
  position: absolute; top: calc(8px + var(--safe-t)); left: calc(10px + var(--safe-l)); right: calc(10px + var(--safe-r));
  display: flex; justify-content: space-between; align-items: flex-start; gap: 12px;
}
.timer-box, .speed-box {
  background: rgba(10, 12, 15, .78); border: 1px solid var(--line); border-radius: 12px;
  padding: 6px 14px 8px; min-width: 128px; backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.speed-box { text-align: right; min-width: 104px; }
#timer, #speed { font-size: 34px; font-weight: 700; line-height: 1; font-variant-numeric: tabular-nums; letter-spacing: .5px; }
#split { font-size: 16px; font-weight: 600; margin-top: 4px; min-height: 18px; font-variant-numeric: tabular-nums; }
#split.good { color: var(--good); } #split.bad { color: var(--bad); }
.unit { font-size: 14px; color: var(--muted); margin-top: 4px; letter-spacing: .5px; text-transform: uppercase; }

#notes { display: flex; align-items: flex-start; gap: 8px; }
.note {
  display: flex; align-items: center; gap: 8px; background: rgba(10, 12, 15, .82);
  border: 1px solid var(--line); border-radius: 12px; padding: 6px 12px 6px 8px; color: var(--text);
  opacity: 0; transform: translateY(-6px); transition: opacity .15s, transform .15s;
}
.note.show { opacity: 1; transform: none; }
.note svg { width: 46px; height: 46px; flex: none; }
.note .n-num { font-size: 40px; font-weight: 800; line-height: 1; color: var(--accent); }
.note .n-mod { font-size: 15px; font-weight: 600; text-transform: uppercase; letter-spacing: .6px; line-height: 1.1; }
.note .n-gap { font-size: 14px; color: var(--muted); font-weight: 600; }
.note.next { transform: scale(.78); transform-origin: top left; }
.note.next.show { opacity: .8; transform: scale(.78); }
.note.next svg { width: 38px; height: 38px; }
.note.next .n-num { font-size: 32px; }
.note.warn .n-num { color: #ff7a59; }

.progress {
  position: absolute; left: 50%; transform: translateX(-50%); top: calc(84px + var(--safe-t));
  width: min(46vw, 420px); height: 6px; border-radius: 3px; background: rgba(0, 0, 0, .55); overflow: hidden;
}
#progBar { height: 100%; width: 0; background: var(--accent); }
#progGhost { position: absolute; top: -2px; width: 3px; height: 10px; background: #8ecbff; left: 0; display: none; }
#km {
  position: absolute; left: 50%; transform: translateX(-50%); top: calc(94px + var(--safe-t));
  font-size: 14px; font-weight: 600; color: #e5e7eb; text-shadow: 0 1px 3px #000; font-variant-numeric: tabular-nums;
}
#pauseBtn {
  position: absolute; top: calc(8px + var(--safe-t)); left: 50%; transform: translateX(-50%);
  pointer-events: auto; display: none;
}
#banner {
  position: absolute; left: 50%; top: 38%; transform: translate(-50%, -50%);
  font-size: clamp(30px, 7vw, 64px); font-weight: 800; letter-spacing: 1px; text-transform: uppercase;
  text-shadow: 0 3px 18px rgba(0, 0, 0, .8); white-space: nowrap; opacity: 0; transition: opacity .2s;
}
#banner.show { opacity: 1; }
#banner.red { color: #ff6b5b; } #banner.yellow { color: var(--accent); } #banner.green { color: var(--good); }
#lights {
  position: absolute; left: 50%; top: 30%; transform: translateX(-50%); display: flex; gap: 12px;
  background: rgba(10, 12, 15, .85); padding: 12px 16px; border-radius: 16px; border: 1px solid var(--line);
}
#lights i { width: 34px; height: 34px; border-radius: 50%; background: #2a2f37; box-shadow: inset 0 2px 6px rgba(0,0,0,.6); }
#lights i.on { background: #ff3b30; box-shadow: 0 0 18px #ff3b30; }
#lights.go i { background: var(--good); box-shadow: 0 0 18px var(--good); }

/* ---------- comandos ---------- */
#controls { position: fixed; inset: 0; touch-action: none; }
#steerZone { position: absolute; left: 0; top: 30%; bottom: 0; width: 50%; }
.zone-hint {
  position: absolute; left: calc(196px + var(--safe-l)); bottom: calc(24px + var(--safe-b));
  font: 600 13px 'Barlow Condensed', sans-serif; letter-spacing: 1px; text-transform: uppercase;
  color: rgba(255,255,255,.55); text-shadow: 0 1px 3px #000; transition: opacity .4s;
}
#wheel {
  position: absolute; left: calc(18px + var(--safe-l)); bottom: calc(14px + var(--safe-b));
  width: 168px; height: 168px; pointer-events: none; filter: drop-shadow(0 6px 14px rgba(0,0,0,.55));
}
#wheelSvg { width: 100%; height: 100%; display: block; transition: none; }
#wheel.active #wheelSvg { filter: brightness(1.15); }

#pedals {
  position: absolute; right: calc(16px + var(--safe-r)); bottom: calc(16px + var(--safe-b));
  display: flex; flex-direction: column; align-items: flex-end; gap: 12px;
}
.pedal-row { display: flex; gap: 12px; align-items: flex-end; }
.pedal {
  display: flex; align-items: center; justify-content: center; text-align: center;
  font: 700 13px/1.1 'Barlow Condensed', sans-serif; letter-spacing: 1px; color: #fff;
  border-radius: 18px; border: 2px solid rgba(255,255,255,.35); text-shadow: 0 1px 2px rgba(0,0,0,.6);
  transition: transform .06s, filter .06s;
}
.pedal span { pointer-events: none; }
#gas { width: 104px; height: 150px; background: rgba(34, 197, 94, .55); }
#brake { width: 92px; height: 116px; background: rgba(239, 68, 68, .55); }
#hb { width: 208px; height: 60px; background: rgba(245, 158, 11, .5); border-radius: 30px; }
.pedal.on { transform: scale(.95); filter: brightness(1.45); border-color: #fff; }

/* ---------- ecrãs ---------- */
.screen {
  position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  padding: calc(16px + var(--safe-t)) calc(16px + var(--safe-r)) calc(16px + var(--safe-b)) calc(16px + var(--safe-l));
  background: linear-gradient(180deg, rgba(8,10,12,.35), rgba(8,10,12,.75)); overflow-y: auto;
}
.panel {
  width: min(440px, 100%); background: var(--panel); border: 1px solid var(--line); border-radius: 20px;
  padding: 22px; display: flex; flex-direction: column; gap: 14px; margin: auto;
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.panel.wide { width: min(560px, 100%); }
.panel-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.eyebrow { font: 600 13px 'Barlow Condensed', sans-serif; letter-spacing: 2px; text-transform: uppercase; color: var(--accent); }
h1, h2 { font-family: 'Barlow Condensed', sans-serif; font-weight: 800; margin: 0; letter-spacing: .3px; line-height: 1.05; }
h1 { font-size: 34px; margin-top: 6px; } h2 { font-size: 28px; }
.meta { color: var(--muted); font-size: 14px; margin-top: 8px; }
.field { display: flex; flex-direction: column; gap: 8px; }
.field > span { font-size: 13px; color: var(--muted); font-weight: 500; }
.field input {
  font: 500 17px Barlow, sans-serif; color: var(--text); background: rgba(255,255,255,.06);
  border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; outline: none; -webkit-user-select: text; user-select: text;
}
.field input:focus { border-color: var(--accent); }
.seg { display: flex; background: rgba(255,255,255,.06); border-radius: 12px; padding: 4px; gap: 4px; }
.seg button { flex: 1; border: 0; background: none; padding: 10px 8px; border-radius: 9px; font-weight: 600; font-size: 14px; color: var(--muted); }
.seg button.on { background: var(--accent); color: var(--accent-ink); }
.primary {
  border: 0; background: var(--accent); color: var(--accent-ink); font: 800 20px 'Barlow Condensed', sans-serif;
  letter-spacing: 1px; text-transform: uppercase; padding: 15px; border-radius: 14px; margin-top: 4px;
}
.primary:active { transform: scale(.98); }
.ghost-btn { border: 1px solid var(--line); background: rgba(255,255,255,.04); padding: 11px 12px; border-radius: 12px; font-weight: 600; font-size: 14px; }
.row-btns { display: flex; gap: 8px; flex-wrap: wrap; }
.row-btns .ghost-btn { flex: 1; min-width: 110px; }
.rotate-hint { margin: 0; color: var(--muted); font-size: 13px; text-align: center; }
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px; border: 1px solid var(--line); background: rgba(10,12,15,.78);
  display: grid; place-items: center; padding: 0;
}
.icon-btn svg { width: 20px; height: 20px; fill: #fff; stroke: #fff; stroke-width: 0; }
.icon-btn.close svg { fill: none; stroke-width: 2.2; stroke-linecap: round; }

.board { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 2px; max-height: 52vh; overflow-y: auto; }
.board li {
  display: grid; grid-template-columns: 34px 1fr auto; gap: 10px; align-items: center;
  padding: 9px 10px; border-radius: 10px; background: rgba(255,255,255,.03); font-size: 15px;
}
.board li.me { background: rgba(255, 214, 10, .16); outline: 1px solid rgba(255, 214, 10, .5); }
.board li.real { background: rgba(142, 203, 255, .1); }
.board .pos { font: 700 17px 'Barlow Condensed', sans-serif; color: var(--muted); text-align: center; }
.board .who { min-width: 0; }
.board .who b { display: block; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.board .who small { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; display: block; }
.board .t { font: 700 18px 'Barlow Condensed', sans-serif; font-variant-numeric: tabular-nums; }
.board .empty { display: block; text-align: center; color: var(--muted); padding: 22px 10px; }
.board.compact { max-height: 34vh; }
.help { margin: 0; padding-left: 18px; display: flex; flex-direction: column; gap: 10px; color: #d6d9de; line-height: 1.45; font-size: 15px; }
.help b { color: var(--text); }
.fin-time { font: 800 64px/1 'Barlow Condensed', sans-serif; font-variant-numeric: tabular-nums; color: var(--accent); }
.fin-info { color: #d6d9de; line-height: 1.45; }
.fin-info b { color: var(--text); }

#toast {
  position: fixed; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translateX(-50%) translateY(20px);
  background: #1f242c; border: 1px solid var(--line); padding: 10px 16px; border-radius: 12px; font-size: 14px;
  opacity: 0; transition: .2s; pointer-events: none; z-index: 10;
}
#toast.show { opacity: 1; transform: translateX(-50%); }

/* telemóvel na vertical: comandos mais compactos */
@media (orientation: portrait) {
  #steerZone { top: 55%; }
  #wheel { width: 138px; height: 138px; }
  #gas { width: 84px; height: 128px; } #brake { width: 74px; height: 100px; } #hb { width: 170px; height: 52px; }
  .hud-top { flex-wrap: wrap; }
  #notes { order: 3; width: 100%; justify-content: center; }
  .progress { top: calc(150px + var(--safe-t)); width: 70vw; }
  #km { top: calc(160px + var(--safe-t)); }
  #timer, #speed { font-size: 28px; }
}
/* ecrãs baixos (telemóvel deitado) */
@media (max-height: 440px) {
  .panel { padding: 16px; gap: 10px; }
  #menu .panel { width: min(760px, 100%); display: grid; grid-template-columns: 1fr 1fr; column-gap: 20px; align-items: start; }
  #menu .brand { grid-row: span 2; }
  #menu .primary, #menu .row-btns { grid-column: 1 / -1; }
  #menu .rotate-hint { display: none; }
  .primary { padding: 12px; }
  .field input { padding: 10px 12px; }
  h1 { font-size: 28px; }
  .fin-time { font-size: 48px; }
  .board.compact { max-height: 28vh; }
}
@media (hover: hover) and (pointer: fine) {
  .zone-hint::after { content: ' · ou setas do teclado'; }
}

/* ---------- interior do carro ---------- */
#cockpit { position: fixed; inset: 0; pointer-events: none; will-change: transform; }
#cockpit[hidden] { display: none; }
.ck-roof { position: absolute; left: 0; top: 0; width: 100%; height: 11vh; }
.ck-mirror {
  position: absolute; top: 4.5vh; left: 50%; transform: translateX(-50%);
  width: 22vh; height: 6.5vh; border-radius: 1.4vh; background: #0d0e10; padding: .6vh;
  box-shadow: 0 .6vh 1.4vh rgba(0,0,0,.5);
}
.ck-mirror i { display: block; width: 100%; height: 100%; border-radius: 1vh; background: linear-gradient(180deg, #8fb3d6 0%, #c9d6df 48%, #6f6a55 52%, #3d3a30 100%); opacity: .85; }
.ck-pillar { position: absolute; top: 0; height: 100%; width: 13vw; }
.ck-pillar.left { left: 0; } .ck-pillar.right { right: 0; }
.ck-dash { position: absolute; left: 0; bottom: 0; width: 100%; height: 25vh; }
#dash {
  position: absolute; left: 50%; bottom: 3.5vh; transform: translateX(-50%);
  background: #050607; border: 1px solid #2f3237; border-radius: 1.6vh; padding: 1vh 1.8vh 1.2vh;
  font-family: 'Barlow Condensed', sans-serif; box-shadow: inset 0 0 2vh rgba(0,0,0,.8);
}
#shift { display: flex; gap: .7vh; justify-content: center; margin-bottom: .8vh; }
#shift i { width: 1.8vh; height: 1.8vh; border-radius: 50%; background: #1d2023; }
#shift i.g { background: #22c55e; box-shadow: 0 0 1vh #22c55e; }
#shift i.y { background: #facc15; box-shadow: 0 0 1vh #facc15; }
#shift i.r { background: #ef4444; box-shadow: 0 0 1vh #ef4444; }
#shift.flash i { background: #60a5fa; box-shadow: 0 0 1.2vh #60a5fa; }
.dash-row { display: flex; gap: 2.2vh; align-items: flex-end; }
.dash-cell { text-align: center; min-width: 7vh; }
.dash-cell b { display: block; font-size: 4.6vh; font-weight: 700; color: #e9f5ff; line-height: 1; font-variant-numeric: tabular-nums; }
.dash-cell.gear b { font-size: 7vh; color: var(--accent); }
.dash-cell small { font-size: 1.5vh; color: #7b8592; text-transform: uppercase; letter-spacing: .1vh; }
.speed-box-spacer { min-width: 104px; }
/* volante grande à frente do piloto */
#wheel { left: calc(24vw - 30vh); bottom: -16vh; width: 60vh; height: 60vh; filter: drop-shadow(0 1vh 2vh rgba(0,0,0,.6)); }
.zone-hint { display: none; }
@media (orientation: portrait) {
  #wheel { left: -6vw; bottom: -9vh; width: 34vh; height: 34vh; }
  .ck-dash { height: 16vh; } .ck-roof { height: 7vh; } .ck-pillar { width: 9vw; }
  #dash { transform: translateX(-50%) scale(.75); transform-origin: bottom center; }
}
