* { margin: 0; padding: 0; box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; touch-action: none; }
body {
  font-family: ui-rounded, "SF Pro Rounded", system-ui, sans-serif;
  background: #8ed1fc;
  user-select: none;
  -webkit-user-select: none;
}
#game { position: fixed; inset: 0; width: 100%; height: 100%; display: block; }
.hidden { display: none !important; }

/* --- overlaye / karty --- */
.overlay {
  position: fixed; inset: 0; z-index: 30;
  display: flex; align-items: center; justify-content: center;
  background: rgba(30, 20, 60, 0.45);
  backdrop-filter: blur(4px);
}
.card {
  background: linear-gradient(160deg, #fff, #ffe8f0);
  border-radius: 28px;
  padding: 32px 40px;
  text-align: center;
  box-shadow: 0 18px 50px rgba(30, 20, 60, 0.4);
  max-width: min(92vw, 460px);
  max-height: 88vh;
  overflow-y: auto;
}
.card h1 {
  font-size: 52px; letter-spacing: 2px; color: #118ab2;
  text-shadow: 3px 3px 0 #ffd166;
}
.card h1 span { color: #ff5470; }
.card h2 { font-size: 32px; color: #118ab2; margin-bottom: 12px; }
.tag { color: #666; margin: 6px 0 20px; }
.err { color: #ff5470; font-weight: 700; min-height: 1.4em; margin-top: 8px; }
.hint { color: #888; font-size: 13px; margin-top: 14px; }

input {
  width: 100%; padding: 14px 18px; font-size: 20px; text-align: center;
  border: 3px solid #118ab2; border-radius: 16px; outline: none;
  font-family: inherit; margin-bottom: 14px; background: #fff;
}
button.big {
  width: 100%; padding: 16px; font-size: 24px; font-weight: 800;
  color: #fff; background: linear-gradient(180deg, #ff7a94, #ff5470);
  border: 0; border-radius: 18px; cursor: pointer;
  box-shadow: 0 6px 0 #c93a56; font-family: inherit;
  transition: transform 0.06s;
}
button.big:active { transform: translateY(4px); box-shadow: 0 2px 0 #c93a56; }
button.big:disabled { opacity: 0.6; }
button.big.armed { background: linear-gradient(180deg, #45e08a, #06d6a0); box-shadow: 0 6px 0 #059677; }

/* --- lobby --- */
.panel {
  position: fixed; top: 16px; left: 16px; z-index: 20;
  background: rgba(20, 20, 45, 0.78); color: #fff;
  border-radius: 20px; padding: 18px 20px; width: 260px;
  backdrop-filter: blur(6px);
}
.panel h2 { color: #ffd166; font-size: 22px; margin-bottom: 8px; }
.panel ul { list-style: none; max-height: 34vh; overflow-y: auto; margin-bottom: 8px; }
.panel li { padding: 4px 6px; font-size: 16px; border-radius: 8px; }
.panel li.me { background: rgba(255, 213, 102, 0.18); font-weight: 700; }
.panel p { font-size: 13px; color: #cbd; margin-bottom: 10px; }
.panel .hint { color: #99a; }

/* głosowanie trybu wyścigu */
.mode-vote {
  display: grid;
  /* minmax(0,1fr): bez tego guziki nie mogą się skurczyć poniżej szerokości
     tekstu i wyłażą poza panel, nachodząc na siebie (nieklikalne) */
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 6px; margin-bottom: 10px;
}
.mv {
  min-width: 0; padding: 9px 2px; font-size: 11.5px; font-weight: 800;
  font-family: inherit; color: #fff; cursor: pointer;
  background: rgba(255, 255, 255, 0.12);
  border: 2px solid transparent; border-radius: 12px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.mv.sel { border-color: #ffd166; background: rgba(255, 209, 102, 0.25); }

/* lampa trybu ŚWIATŁA */
#light-dot {
  position: fixed; top: 44px; left: 50%; transform: translateX(-50%);
  z-index: 16; width: 56px; height: 56px; border-radius: 50%;
  border: 4px solid rgba(20, 20, 45, 0.7); pointer-events: none;
  transition: background 0.12s, box-shadow 0.12s;
}
#light-dot[data-c="g"] { background: #3ef26b; box-shadow: 0 0 26px #3ef26b; }
#light-dot[data-c="y"] { background: #ffd166; box-shadow: 0 0 26px #ffd166; }
#light-dot[data-c="r"] { background: #ff3c50; box-shadow: 0 0 34px #ff3c50; }

#race-timer {
  position: fixed; top: 12px; left: 50%; transform: translateX(-50%);
  z-index: 15; background: rgba(20, 20, 45, 0.65); color: #ffd166;
  padding: 6px 18px; border-radius: 999px; font-size: 17px; font-weight: 800;
  pointer-events: none;
}

/* --- HUD --- */
#race-hud { position: fixed; right: 12px; bottom: 12px; z-index: 15; pointer-events: none; }
#tach { width: 190px; height: 190px; filter: drop-shadow(0 4px 10px rgba(0,0,0,0.4)); }

#progress {
  position: fixed; top: 10px; left: 50%; transform: translateX(-50%);
  width: min(70vw, 700px); height: 14px; z-index: 15;
  background: rgba(20,20,45,0.55); border-radius: 8px;
}
#progress .dot {
  position: absolute; top: 2px; width: 10px; height: 10px; border-radius: 50%;
  background: #ffd166; transform: translateX(-5px);
}
#progress .dot.me { background: #3ef26b; box-shadow: 0 0 6px #3ef26b; }

#mode-info {
  position: fixed; top: 15vh; left: 50%; transform: translateX(-50%);
  z-index: 24; max-width: min(90vw, 560px); text-align: center;
  background: rgba(20,20,45,0.72); color: #fff; padding: 12px 22px;
  border-radius: 16px; font-size: 16px; font-weight: 700; line-height: 1.4;
  pointer-events: none;
}

#countdown {
  position: fixed; top: 22vh; left: 0; right: 0; z-index: 25;
  text-align: center; font-size: min(15vw, 96px); font-weight: 900; color: #fff;
  text-shadow: 0 6px 0 rgba(30,20,60,0.5), 0 0 40px rgba(255,213,102,0.7);
  pointer-events: none;
}
#banner {
  position: fixed; top: 38vh; left: 0; right: 0; z-index: 25;
  text-align: center; font-size: min(8vw, 44px); font-weight: 900; color: #ffd166;
  text-shadow: 0 4px 0 rgba(30,20,60,0.6);
  pointer-events: none;
}

#spectate-bar {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%);
  z-index: 15; background: rgba(20,20,45,0.75); color: #fff;
  padding: 10px 22px; border-radius: 999px; font-size: 16px; font-weight: 700;
}

#results table { width: 100%; border-collapse: collapse; font-size: 20px; }
#results td { padding: 8px 10px; text-align: left; color: #333; }
#results tr.me td { background: rgba(255, 213, 102, 0.35); font-weight: 800; border-radius: 8px; }

#top-buttons {
  position: fixed; z-index: 40; display: flex; gap: 10px;
  top: max(14px, env(safe-area-inset-top));
  right: max(14px, env(safe-area-inset-right));
}
#top-buttons button {
  width: 46px; height: 46px; font-size: 21px; border: 0; border-radius: 50%;
  background: rgba(20,20,45,0.6); cursor: pointer; color: #fff;
}

/* --- czat --- */
#chat-log {
  position: fixed; z-index: 14; pointer-events: none;
  left: max(12px, env(safe-area-inset-left));
  bottom: max(14px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; gap: 4px;
  max-width: min(46vw, 340px);
}
#chat-log .msg {
  background: rgba(20,20,45,0.65); color: #fff; font-size: 13px;
  padding: 4px 10px; border-radius: 10px; word-break: break-word;
  opacity: 1; transition: opacity 0.9s;
}
#chat-log .msg b { color: #ffd166; }
#chat-log .msg.fade { opacity: 0; }
body.touch #chat-log { bottom: 210px; }

#chat-box {
  position: fixed; z-index: 26; left: 50%; transform: translateX(-50%);
  bottom: max(14px, env(safe-area-inset-bottom));
  display: flex; gap: 8px; align-items: center;
  width: min(70vw, 420px);
}
#chat-box input {
  flex: 1; margin: 0; padding: 10px 14px; font-size: 15px;
  border: 2px solid #118ab2; border-radius: 14px; background: #fff;
}
#chat-box button {
  width: 42px; height: 42px; border: 0; border-radius: 50%;
  font-size: 16px; cursor: pointer; color: #fff; flex-shrink: 0;
}
#chat-send { background: #06d6a0; }
#chat-close { background: #ff5470; }
body.touch #chat-box { bottom: 210px; }

/* --- dotyk --- */
#touch-controls { position: fixed; inset: 0; z-index: 18; pointer-events: none; }
#touch-controls button {
  pointer-events: auto; touch-action: none;
  border: 0; border-radius: 20px; font-family: inherit; font-weight: 800;
  color: #fff; background: rgba(20,20,45,0.7);
  backdrop-filter: blur(3px);
  box-shadow: inset 0 0 0 2px rgba(255,255,255,0.4), 0 2px 8px rgba(0,0,0,0.3);
}
#touch-controls button.active { background: rgba(255, 84, 112, 0.75); }
.tc-left {
  position: absolute; left: max(12px, env(safe-area-inset-left));
  bottom: max(14px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; align-items: center; gap: 10px;
}
.tc-steer { display: flex; gap: 12px; }
.tc-steer button { width: 84px; height: 84px; font-size: 34px; }
.tc-left #btn-up, .tc-left #btn-dn { width: 180px; height: 48px; font-size: 15px; }
.tc-right {
  position: absolute; right: max(12px, env(safe-area-inset-right));
  bottom: max(14px, env(safe-area-inset-bottom));
  display: flex; gap: 12px; align-items: flex-end;
}
.tc-right #btn-gas { width: 108px; height: 108px; font-size: 20px; border-radius: 50%; background: rgba(6, 214, 160, 0.55); }
.tc-right #btn-gas.active { background: rgba(6, 214, 160, 0.9); }
.tc-right #btn-brake { width: 88px; height: 88px; font-size: 14px; border-radius: 50%; background: rgba(255, 84, 112, 0.5); }

body.touch #race-hud { right: 8px; bottom: 130px; }
body.touch #tach { width: 130px; height: 130px; }

/* --- obróć telefon --- */
#rotate-overlay { display: none; }
@media (orientation: portrait) and (pointer: coarse) {
  #rotate-overlay {
    display: flex; position: fixed; inset: 0; z-index: 100;
    align-items: center; justify-content: center; text-align: center;
    background: #1e143c; color: #ffd166;
    font-size: 30px; font-weight: 800; line-height: 1.6;
  }
}
