:root {
  --ink: #f4f1ff;
  --dim: #a9a4c9;
  --glass: rgba(14, 14, 34, .72);
  --line: rgba(255, 255, 255, .16);
  --hot: #ff8a5c;
  --ok: #58e6a0;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; width: 100%; position: fixed; overflow: hidden;
  background: #05060f; color: var(--ink);
  font: 15px/1.35 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-user-select: none; user-select: none; overscroll-behavior: none;
}
[hidden] { display: none !important; }
h1 { margin: 0; font-size: 26px; letter-spacing: .06em; font-weight: 900; }
h1 i { color: var(--hot); }
h2 { margin: 0 0 6px; font-size: 22px; }
button { font: inherit; color: inherit; border: 0; cursor: pointer; touch-action: manipulation; }
.primary {
  background: linear-gradient(180deg, #ffa573, #f0623e); color: #1b0d08; font-weight: 800;
  padding: 11px 20px; border-radius: 999px; font-size: 16px;
}
.chip {
  background: rgba(255, 255, 255, .1); border: 1px solid var(--line);
  padding: 8px 12px; border-radius: 999px; font-size: 13px; white-space: nowrap;
}
.chip.on { background: rgba(255, 138, 92, .28); border-color: var(--hot); }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; background: #777; margin-right: 6px; }
.dot.ok { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.dot.wait { background: #ffd166; }
.dot.bad { background: #ff5d6c; }
.muted { color: var(--dim); }

/* ---------- stage: the whole unfolded screen ---------- */
#stage { position: absolute; left: 50%; top: 50%; overflow: hidden; transform-origin: center; }
#gl { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; }
#top, #bottom { position: absolute; left: 0; right: 0; pointer-events: none; }
#top { top: 0; display: flex; align-items: center; justify-content: center; }
#top > *, #bottom > * { pointer-events: auto; }

.panel {
  background: var(--glass); border: 1px solid var(--line); border-radius: 18px;
  -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px);
  padding: 14px 16px; max-width: calc(100% - 20px); max-height: calc(100% - 16px); overflow: auto;
  display: flex; gap: 16px; align-items: center;
}
.panel.small { flex-direction: column; gap: 8px; text-align: center; padding: 18px 26px; }
.panel p { margin: 0; }
.panel ol { margin: 8px 0 10px; padding-left: 20px; }
.panel li { margin: 2px 0; }
.col.main { min-width: 0; flex: 1 1 320px; max-width: 430px; }
.col.remote { display: flex; flex-direction: column; align-items: center; gap: 6px; flex: 0 0 150px; text-align: center; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; justify-content: flex-start; }
.panel.small .row { justify-content: center; }
.hinge-setting {
  display: flex; align-items: center; gap: 8px; margin-top: 8px;
  color: var(--dim); font-size: 13px; white-space: nowrap;
}
.hinge-setting input { min-width: 0; flex: 1; margin: 0; accent-color: var(--hot); touch-action: none; }
.hinge-setting output { min-width: 4ch; text-align: right; font-variant-numeric: tabular-nums; }
.hint { margin-top: 8px !important; color: var(--dim); font-size: 13px; }
#qr { width: 112px; height: 112px; background: #fff; border-radius: 10px; padding: 6px; }
#qr svg { width: 100%; height: 100%; display: block; }
#remoteCode { font: 800 24px/1.1 ui-monospace, Menlo, monospace; letter-spacing: .2em; margin-left: .2em; }
#remoteStatus { font-size: 13px; margin: 2px 0; }
.remote-info small { display: block; font-size: 11px; }

#hud {
  position: absolute; left: 0; right: 0; top: 0; display: flex; justify-content: space-between; align-items: flex-start;
  padding: calc(8px + env(safe-area-inset-top)) calc(10px + env(safe-area-inset-right)) 0 calc(10px + env(safe-area-inset-left));
  pointer-events: none !important; font-variant-numeric: tabular-nums; text-shadow: 0 1px 6px rgba(0, 0, 0, .7);
}
.hud-box { display: flex; flex-direction: column; min-width: 90px; }
.hud-box b { font-size: 30px; line-height: 1; font-weight: 900; }
.hud-box small { color: #ffe9dc; font-size: 12px; }
.hud-box.mid { align-items: center; }
.hud-box.right { flex-direction: row; gap: 10px; align-items: center; justify-content: flex-end; }
#hudLives { color: #ff5d6c; font-size: 22px; letter-spacing: 2px; }
.icon { pointer-events: auto; width: 40px; height: 40px; border-radius: 50%; background: rgba(10, 10, 30, .55); border: 1px solid var(--line); font-size: 12px; }
#count { font-size: 110px; font-weight: 900; font-style: italic; text-shadow: 0 4px 30px rgba(0, 0, 0, .6); pointer-events: none !important; }

/* ---------- calibration ---------- */
#carBox {
  position: absolute; border: 2px dashed rgba(255, 255, 255, .85); border-radius: 14% / 7%;
  display: flex; align-items: center; justify-content: center; touch-action: none; cursor: move;
}
#carBox span { font-size: 12px; letter-spacing: .12em; text-transform: uppercase; opacity: .8; writing-mode: vertical-rl; }
#carBox .h {
  position: absolute; width: 44px; height: 44px; margin: -22px; touch-action: none;
  background: radial-gradient(circle, #fff 0 8px, rgba(255, 138, 92, .9) 9px 12px, transparent 13px);
}
.h.tl { left: 0; top: 0; cursor: nwse-resize; } .h.tr { right: 0; top: 0; cursor: nesw-resize; }
.h.bl { left: 0; bottom: 0; cursor: nesw-resize; } .h.br { right: 0; bottom: 0; cursor: nwse-resize; }
#sizeBar {
  position: absolute; left: calc(10px + env(safe-area-inset-left)); bottom: calc(10px + env(safe-area-inset-bottom));
  display: flex; align-items: center; gap: 8px; background: var(--glass); border: 1px solid var(--line);
  border-radius: 999px; padding: 5px; font-size: 12px; color: var(--dim);
}
#sizeBar button { width: 40px; height: 40px; border-radius: 50%; background: rgba(255, 255, 255, .14); font-size: 22px; line-height: 1; }
#fold { position: absolute; left: 0; right: 0; height: 0; border-top: 2px dashed rgba(255, 255, 255, .8); touch-action: none; }
#fold span {
  position: absolute; right: calc(10px + env(safe-area-inset-right)); top: -19px; height: 36px; padding: 0 14px;
  display: flex; align-items: center; background: #fff; color: #111; border-radius: 999px; font-weight: 700; font-size: 13px; cursor: ns-resize;
}
#fold::before { content: ""; position: absolute; left: 0; right: 0; top: -20px; height: 40px; }
#flash { position: absolute; inset: 0; background: #ff3b3b; opacity: 0; pointer-events: none; transition: opacity .5s; }

/* ---------- remote ---------- */
#remoteUI { position: absolute; inset: 0; display: flex; flex-direction: column; background: radial-gradient(120% 90% at 50% 0, #2a1b5e, #070b24 70%); }
#rJoin { margin: auto; text-align: center; padding: 20px; max-width: 360px; }
#rJoin a { color: var(--dim); display: block; margin-top: 22px; font-size: 13px; }
#rForm { display: flex; gap: 8px; justify-content: center; margin-top: 12px; }
#rCode {
  width: 150px; font: 800 26px ui-monospace, Menlo, monospace; letter-spacing: .2em; text-align: center; text-transform: uppercase;
  background: rgba(255, 255, 255, .08); color: var(--ink); border: 1px solid var(--line); border-radius: 12px; padding: 8px;
  -webkit-user-select: text; user-select: text;
}
#rPad { flex: 1; display: flex; flex-direction: column; min-height: 0;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
#rBar { display: flex; align-items: center; gap: 10px; padding: 8px 12px; flex-wrap: wrap; }
#rStats { flex: 1; text-align: center; color: var(--dim); font-variant-numeric: tabular-nums; white-space: nowrap; }
#rStats b { color: var(--ink); } #rLives { color: #ff5d6c; }
#rBar .primary { padding: 8px 16px; font-size: 14px; }
#rControls { flex: 1; display: flex; gap: 10px; padding: 4px 10px 12px; min-height: 0; }
#rSteer, .pedal { border-radius: 22px; border: 1px solid var(--line); background: rgba(255, 255, 255, .06); touch-action: none; position: relative; }
#rSteer { flex: 1.3; display: flex; align-items: flex-end; justify-content: center; overflow: hidden; }
#rSteer span { color: var(--dim); font-size: 12px; padding-bottom: 12px; letter-spacing: .1em; }
#rKnob {
  position: absolute; left: 50%; top: 50%; width: 92px; height: 92px; margin: -46px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #ffc09d, #f0623e); box-shadow: 0 8px 24px rgba(0, 0, 0, .5); pointer-events: none;
}
#rPedals { flex: 1; display: flex; gap: 10px; }
.pedal { flex: 1; display: flex; align-items: center; justify-content: center; font-weight: 900; letter-spacing: .12em; color: var(--dim); }
#rGas { flex: 1.4; } #rGas.on { background: rgba(88, 230, 160, .35); color: #fff; }
#rBrake.on { background: rgba(255, 93, 108, .4); color: #fff; }
@media (orientation: portrait) {
  #rControls { flex-direction: column; }
  #rSteer { flex: 1; } #rPedals { flex: 1; }
}
