:root {
  --bg: #0b0d14;
  --panel: #141825;
  --line: #2a3148;
  --text: #e8ecf8;
  --muted: #8b93a7;
  --red: #ff4d4d;
  --blue: #4da6ff;
  --green: #3ddc97;
  --yellow: #ffd24d;
  --accent: #ff6b35;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: "Segoe UI", system-ui, sans-serif;
  overflow: hidden;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

#app {
  height: 100%;
  display: flex;
  flex-direction: column;
}

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.5rem 1rem;
  border-bottom: 1px solid var(--line);
  background: rgba(20, 24, 37, 0.95);
  z-index: 5;
  min-height: 48px;
}

.brand {
  font-weight: 800;
  letter-spacing: 0.12em;
  color: var(--accent);
  font-size: 0.95rem;
}

#hud {
  display: flex;
  gap: 1.25rem;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  font-size: 0.95rem;
}
#hud.hidden { display: none; }

#screens {
  flex: 1;
  position: relative;
  min-height: 0;
}

.screen {
  position: absolute;
  inset: 0;
  overflow: auto;
  padding: 1.25rem;
}
.screen.hidden { display: none; }

#menu, #online-lobby, #results {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  background:
    radial-gradient(ellipse 80% 50% at 50% 0%, rgba(255, 107, 53, 0.12), transparent),
    var(--bg);
}
#menu.hidden, #online-lobby.hidden, #results.hidden { display: none; }

h1 {
  font-size: clamp(2rem, 6vw, 3.2rem);
  letter-spacing: 0.06em;
  margin-top: 1rem;
}
h2 { font-size: 1.15rem; color: var(--yellow); margin-bottom: 0.5rem; }
.tagline { color: var(--muted); margin-bottom: 0.5rem; }

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  width: min(320px, 100%);
}
.field span { font-size: 0.85rem; color: var(--muted); }
input[type="text"] {
  background: var(--panel);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 10px;
  padding: 0.75rem 1rem;
  font-size: 1rem;
  outline: none;
}
input[type="text"]:focus { border-color: var(--accent); }
input.code {
  width: 7rem;
  text-align: center;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  font-weight: 700;
}

.menu-actions, .row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  justify-content: center;
  width: min(520px, 100%);
}
.join-row { display: flex; gap: 0.5rem; }

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  border-radius: 12px;
  padding: 0.85rem 1.25rem;
  font-size: 1rem;
  font-weight: 650;
  cursor: pointer;
  transition: transform 0.08s, border-color 0.15s, background 0.15s;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: scale(0.97); }
.btn.primary {
  background: linear-gradient(135deg, #ff6b35, #ff3d5a);
  border-color: transparent;
  color: #fff;
}
.btn.ghost {
  background: transparent;
  color: var(--muted);
}
.btn.ready-on {
  background: rgba(61, 220, 151, 0.2);
  border-color: var(--green);
  color: var(--green);
}

.boards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
  width: min(640px, 100%);
  margin-top: 0.5rem;
}
.board {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 1rem 1.1rem;
}
.board ol {
  list-style: none;
  counter-reset: place;
}
.board li {
  counter-increment: place;
  display: flex;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.35rem 0;
  border-bottom: 1px solid rgba(42, 49, 72, 0.6);
  font-size: 0.92rem;
}
.board li::before {
  content: counter(place) ".";
  color: var(--muted);
  width: 1.4rem;
}
.board li span.time {
  font-variant-numeric: tabular-nums;
  color: var(--yellow);
  font-weight: 600;
}
.board li.empty { color: var(--muted); justify-content: center; }
.board li.empty::before { content: ""; }

.help {
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
  max-width: 420px;
  line-height: 1.5;
}
kbd {
  display: inline-block;
  border: 1px solid var(--line);
  background: #0f1320;
  border-radius: 5px;
  padding: 0.05rem 0.35rem;
  font-size: 0.8rem;
  margin: 0 0.1rem;
}

#error, .error, #online-error {
  color: var(--red);
  min-height: 1.2em;
  font-size: 0.9rem;
}

#lobby-players {
  list-style: none;
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 0.5rem;
}
#lobby-players li {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.55rem 0.7rem;
}
.dot {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  flex-shrink: 0;
}
.badge {
  margin-left: auto;
  font-size: 0.75rem;
  color: var(--muted);
}
.badge.on { color: var(--green); }

#game {
  padding: 0;
  background: #0a0c12;
  display: flex;
  align-items: center;
  justify-content: center;
}
#game.hidden { display: none; }

#canvas {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #10141f;
  display: block;
  max-height: 100%;
  max-width: 100%;
}

#overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
}
#countdown, #message {
  font-size: clamp(2.5rem, 10vw, 5rem);
  font-weight: 900;
  text-shadow: 0 4px 30px rgba(0,0,0,0.7);
  letter-spacing: 0.08em;
}
#countdown.hidden, #message.hidden { display: none; }
#countdown.go { color: var(--green); }
#message {
  font-size: clamp(1.4rem, 5vw, 2.4rem);
  text-align: center;
  background: rgba(10, 12, 18, 0.75);
  padding: 1.2rem 1.6rem;
  border-radius: 16px;
  border: 1px solid var(--line);
  max-width: 90%;
}

.quit {
  position: absolute;
  top: 0.6rem;
  right: 0.6rem;
  z-index: 6;
  padding: 0.45rem 0.8rem;
  font-size: 0.85rem;
}

#touch {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 4;
}
#touch.hidden { display: none; }
#touch > * { pointer-events: auto; }

#stick-zone {
  position: absolute;
  left: 0;
  bottom: 0;
  width: 46%;
  height: 42%;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 1.2rem;
}
#stick-base {
  width: 120px;
  height: 120px;
  border-radius: 50%;
  background: rgba(255,255,255,0.08);
  border: 2px solid rgba(255,255,255,0.18);
  position: relative;
  touch-action: none;
}
#stick-knob {
  width: 54px;
  height: 54px;
  border-radius: 50%;
  background: rgba(255,255,255,0.35);
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border: 2px solid rgba(255,255,255,0.5);
}

#pedals {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 48%;
  height: 42%;
  display: flex;
  align-items: flex-end;
  justify-content: flex-end;
  gap: 0.75rem;
  padding: 1.2rem;
}
.pedal {
  width: 96px;
  height: 96px;
  border-radius: 18px;
  border: 2px solid rgba(255,255,255,0.2);
  background: rgba(255, 77, 77, 0.25);
  color: #fff;
  font-weight: 800;
  font-size: 0.85rem;
  touch-action: none;
}
.pedal.gas {
  background: rgba(61, 220, 151, 0.28);
  width: 110px;
  height: 110px;
}
.pedal:active, .pedal.active {
  transform: scale(0.96);
  filter: brightness(1.2);
}

#results-body {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 1rem;
}
#results-body .row-res {
  display: flex;
  justify-content: space-between;
  padding: 0.45rem 0;
  border-bottom: 1px solid rgba(42,49,72,0.5);
}
#results-body .big-time {
  text-align: center;
  font-size: 2rem;
  font-weight: 800;
  color: var(--yellow);
  padding: 0.5rem 0 1rem;
}

@media (max-width: 700px) {
  #hud { gap: 0.6rem; font-size: 0.78rem; }
  .boards { grid-template-columns: 1fr; }
  #stick-base { width: 100px; height: 100px; }
  .pedal { width: 80px; height: 80px; }
  .pedal.gas { width: 94px; height: 94px; }
}
