:root {
  --bg: #0a0f1e;
  --bg2: #0e1528;
  --panel: #131b31;
  --panel2: #1a2440;
  --line: #27334f;
  --text: #eef2fa;
  --muted: #8f9bb6;
  --faint: #5d6886;
  --accent: #8aa4ff;
  --seat: #8aa4ff;
  --danger: #ff6b6b;
  --ok: #3ddc97;
  --radius: 18px;
  color-scheme: dark;
}

[hidden] {
  display: none !important;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  height: 100%;
  background: radial-gradient(120% 80% at 50% -10%, #17224a 0%, var(--bg) 55%) fixed, var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  cursor: pointer;
}

button:focus-visible,
input:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.screen {
  display: none;
  min-height: 100dvh;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom))
    max(16px, env(safe-area-inset-left));
}

body[data-screen="join"] #join,
body[data-screen="lobby"] #lobby,
body[data-screen="end"] #end {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

body[data-screen="pad"] #pad {
  display: flex;
}

.card {
  width: 100%;
  max-width: 440px;
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 22px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.35);
}

h1 {
  margin: 0 0 4px;
  font-size: 26px;
  letter-spacing: -0.01em;
}

h2 {
  margin: 0 0 10px;
  font-size: 15px;
  color: var(--muted);
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}

.lead {
  margin: 0 0 18px;
  color: var(--muted);
}

/* ---- Join ---- */

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 6vh;
  color: var(--muted);
  font-size: 18px;
  letter-spacing: 0.02em;
}

.brand b {
  color: var(--text);
}

.mark {
  width: 28px;
  height: 28px;
  color: var(--accent);
}

.code {
  position: relative;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  margin-bottom: 14px;
}

.code input {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  font-size: 16px; /* no zoom on focus */
  caret-color: transparent;
  border: 0;
  z-index: 2;
  letter-spacing: 3em;
}

.code .box {
  height: 76px;
  border-radius: 14px;
  background: var(--bg2);
  border: 2px solid var(--line);
  display: grid;
  place-items: center;
  font-size: 40px;
  font-weight: 800;
  letter-spacing: 0.02em;
  font-variant-numeric: tabular-nums;
  transition: border-color 0.15s, transform 0.15s, background 0.15s;
}

.code .box.filled {
  background: var(--panel2);
}

.code.focused .box.caret {
  border-color: var(--accent);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 22%, transparent);
}

.code .box.caret::after {
  content: "";
  width: 3px;
  height: 36px;
  border-radius: 2px;
  background: var(--accent);
  animation: blink 1s steps(1) infinite;
}

.code:not(.focused) .box.caret::after {
  display: none;
}

.code.bad .box {
  border-color: var(--danger);
  animation: shake 0.3s;
}

@keyframes blink {
  50% {
    opacity: 0;
  }
}

@keyframes shake {
  25% {
    transform: translateX(-4px);
  }
  75% {
    transform: translateX(4px);
  }
}

.preview {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 0 0 16px;
  padding: 12px 14px;
  border-radius: 14px;
  background: var(--bg2);
  border: 1px solid var(--line);
  min-height: 58px;
}

.preview .dot {
  flex: none;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--ok);
  box-shadow: 0 0 10px var(--ok);
}

.preview.warn .dot {
  background: #ffc145;
  box-shadow: 0 0 10px #ffc145;
}

.preview.bad .dot {
  background: var(--danger);
  box-shadow: none;
}

.preview.loading .dot {
  background: var(--faint);
  box-shadow: none;
  animation: blink 0.8s steps(1) infinite;
}

.preview strong {
  display: block;
  font-size: 17px;
}

.preview small {
  color: var(--muted);
  font-size: 14px;
}

.field {
  display: grid;
  gap: 6px;
  margin-bottom: 16px;
  color: var(--muted);
  font-size: 14px;
}

.field input {
  height: 54px;
  padding: 0 16px;
  border-radius: 14px;
  border: 2px solid var(--line);
  background: var(--bg2);
  color: var(--text);
  font-size: 18px;
}

.field input:focus {
  border-color: var(--accent);
  outline: none;
}

.primary,
.secondary {
  width: 100%;
  height: 58px;
  border-radius: 16px;
  font-size: 19px;
  font-weight: 700;
  transition: transform 0.08s, filter 0.15s, background 0.15s, opacity 0.15s;
}

.primary {
  background: var(--accent);
  color: #0a0f1e;
}

.primary:active,
.secondary:active {
  transform: scale(0.98);
}

.primary:disabled {
  opacity: 0.35;
  cursor: default;
}

.secondary {
  background: transparent;
  border: 2px solid var(--line);
  color: var(--text);
  font-weight: 600;
}

.error {
  margin: 14px 0 0;
  color: var(--danger);
  text-align: center;
}

.foot {
  margin: 0;
  color: var(--faint);
  font-size: 14px;
  text-align: center;
}

/* ---- Lobby ---- */

.bar {
  width: 100%;
  max-width: 440px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.bar .title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 700;
  font-size: 18px;
}

.chip {
  padding: 6px 10px;
  border-radius: 10px;
  background: var(--panel2);
  border: 1px solid var(--line);
  font-weight: 800;
  letter-spacing: 0.12em;
  font-size: 15px;
}

.text-button {
  background: none;
  color: var(--muted);
  padding: 10px 6px;
  font-size: 15px;
}

.seat-card {
  width: 100%;
  max-width: 440px;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 20px;
  border-radius: var(--radius);
  background: linear-gradient(135deg, color-mix(in srgb, var(--seat) 34%, var(--panel)) 0%, var(--panel) 75%);
  border: 1px solid color-mix(in srgb, var(--seat) 55%, var(--line));
  box-shadow: 0 0 40px color-mix(in srgb, var(--seat) 18%, transparent);
}

.seat-badge {
  flex: none;
  display: grid;
  place-items: center;
  width: 72px;
  height: 72px;
  border-radius: 22px;
  background: var(--seat);
  color: #0a0f1e;
  font-size: 34px;
  font-weight: 900;
}

.seat-label {
  margin: 0;
  color: var(--muted);
  font-size: 15px;
}

.seat-name {
  margin: 2px 0 0;
  font-size: 26px;
  font-weight: 800;
  overflow-wrap: anywhere;
}

.say {
  width: 100%;
  max-width: 440px;
  margin: 0;
  padding: 12px 16px;
  border-radius: 14px;
  background: var(--panel2);
  border-left: 4px solid var(--seat);
  font-size: 17px;
}

.players {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
}

.players li {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 12px;
  border-radius: 12px;
  background: var(--bg2);
}

.players li.me {
  outline: 2px solid color-mix(in srgb, var(--seat) 70%, transparent);
}

.players li.away {
  opacity: 0.45;
}

.players .swatch {
  flex: none;
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  border-radius: 9px;
  color: #0a0f1e;
  font-weight: 900;
  font-size: 15px;
}

.players .swatch.screen {
  background: var(--panel2);
  color: var(--muted);
}

.players .who {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 600;
}

.players .tag {
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.players .tag.ready {
  color: var(--ok);
  font-weight: 700;
}

.players svg {
  width: 16px;
  height: 16px;
  vertical-align: -2px;
  margin-left: 6px;
}

.count {
  color: var(--faint);
  margin-left: 6px;
}

.status {
  margin: 0;
  color: var(--muted);
  text-align: center;
  min-height: 1.4em;
}

.lobby-actions {
  width: 100%;
  max-width: 440px;
  display: grid;
  gap: 10px;
  margin-top: auto;
}

.ready[aria-pressed="true"] {
  background: var(--ok);
}

/* ---- The controller ---- */

body[data-screen="pad"] {
  position: fixed;
  inset: 0;
  overflow: hidden;
}

#pad {
  position: fixed;
  inset: 0;
  flex-direction: column;
  padding: 0;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: none;
  background: radial-gradient(90% 70% at 50% 120%, color-mix(in srgb, var(--seat) 16%, transparent), transparent 70%), var(--bg);
}

.pad-top {
  flex: none;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
}

.pad-seat {
  flex: none;
  padding: 6px 12px;
  border-radius: 999px;
  background: var(--seat);
  color: #0a0f1e;
  font-weight: 800;
  font-size: 14px;
  max-width: 40vw;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.pad-say {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
  font-size: 15px;
  text-align: center;
}

.pad-start {
  flex: none;
  padding: 8px 18px;
  border-radius: 999px;
  background: var(--panel2);
  border: 1px solid var(--line);
  font-weight: 700;
  font-size: 14px;
  touch-action: none;
}

.pad-start.down {
  background: var(--seat);
  color: #0a0f1e;
}

.pad-menu {
  flex: none;
  width: 40px;
  height: 36px;
  border-radius: 12px;
  background: transparent;
  display: grid;
  place-items: center;
}

.pad-menu svg {
  width: 22px;
  height: 22px;
  fill: var(--muted);
}

.pad-body {
  position: relative;
  flex: 1;
  display: flex;
  padding: 0 max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
}

.zone {
  position: relative;
  flex: 1;
  touch-action: none;
}

.zone + .zone {
  border-left: 1px dashed color-mix(in srgb, var(--line) 60%, transparent);
}

.zone-label {
  position: absolute;
  left: 50%;
  bottom: 10px;
  transform: translateX(-50%);
  color: var(--faint);
  font-size: 12px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  pointer-events: none;
}

/* Floating sticks */
.stick-base,
.stick-knob {
  position: absolute;
  left: 0;
  top: 0;
  border-radius: 50%;
  pointer-events: none;
  will-change: transform;
}

.stick-base {
  border: 3px solid color-mix(in srgb, var(--seat) 55%, var(--line));
  background: color-mix(in srgb, var(--seat) 8%, transparent);
}

.stick-knob {
  background: radial-gradient(circle at 35% 30%, color-mix(in srgb, white 35%, var(--seat)), var(--seat));
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
}

.zone.idle .stick-base,
.zone.idle .stick-knob {
  opacity: 0.35;
}

/* D-pad */
.dpad {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.dpad .arm {
  position: absolute;
  background: var(--panel2);
  border: 2px solid var(--line);
  border-radius: 12px;
  transition: background 0.05s;
}

.dpad .arm.on {
  background: var(--seat);
  border-color: var(--seat);
}

.dpad .hub {
  position: absolute;
  background: var(--panel2);
}

/* Buttons */
.btn {
  position: absolute;
  display: grid;
  place-items: center;
  border-radius: 50%;
  font-weight: 900;
  color: color-mix(in srgb, var(--seat) 80%, white);
  background: radial-gradient(circle at 40% 35%, var(--panel2), var(--panel));
  border: 3px solid color-mix(in srgb, var(--seat) 65%, var(--line));
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.4), inset 0 -4px 0 rgba(0, 0, 0, 0.25);
  pointer-events: none;
  transition: transform 0.05s, background 0.05s;
}

.btn.b {
  border-color: var(--line);
  color: var(--muted);
}

.btn.on {
  transform: scale(0.94);
  background: var(--seat);
  color: #0a0f1e;
  border-color: var(--seat);
  box-shadow: 0 0 30px color-mix(in srgb, var(--seat) 55%, transparent);
}

.btn.buzzer {
  font-size: 28px;
  letter-spacing: 0.08em;
  background: radial-gradient(circle at 40% 30%, color-mix(in srgb, white 25%, var(--seat)), color-mix(in srgb, var(--seat) 70%, black));
  color: #0a0f1e;
  border: 6px solid color-mix(in srgb, var(--seat) 50%, white);
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.5), inset 0 -10px 0 rgba(0, 0, 0, 0.25);
}

.btn.buzzer.on {
  transform: scale(0.92) translateY(4px);
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.5), 0 0 60px var(--seat), inset 0 -4px 0 rgba(0, 0, 0, 0.25);
}

/* Tilt */
.tilt {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  border: 3px solid color-mix(in srgb, var(--seat) 55%, var(--line));
  background: color-mix(in srgb, var(--seat) 6%, transparent);
  pointer-events: none;
}

.tilt .ball {
  position: absolute;
  left: 50%;
  top: 50%;
  border-radius: 50%;
  background: var(--seat);
  box-shadow: 0 0 20px var(--seat);
}

.tilt-cta {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  padding: 14px 22px;
  border-radius: 14px;
  background: var(--seat);
  color: #0a0f1e;
  font-weight: 800;
  font-size: 17px;
  white-space: nowrap;
}

.rotate-hint {
  position: absolute;
  left: 50%;
  top: 64px;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 8px 8px 14px;
  border-radius: 12px;
  background: var(--panel2);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 14px;
  white-space: nowrap;
  z-index: 5;
}

.rotate-hint button {
  background: none;
  color: var(--muted);
  padding: 4px 8px;
}

@media (orientation: landscape) {
  .rotate-hint {
    display: none;
  }
}

.pad-menu-sheet {
  position: absolute;
  right: max(12px, env(safe-area-inset-right));
  top: calc(max(8px, env(safe-area-inset-top)) + 48px);
  display: grid;
  min-width: 220px;
  padding: 6px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.5);
  z-index: 10;
}

.pad-menu-sheet button {
  text-align: left;
  padding: 14px 14px;
  border-radius: 10px;
  background: none;
  font-size: 16px;
}

.pad-menu-sheet button:active {
  background: var(--panel2);
}

.pad-menu-sheet .danger {
  color: var(--danger);
}

.pad-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(10, 15, 30, 0.72);
  backdrop-filter: blur(3px);
  z-index: 8;
}

.pad-overlay p {
  margin: 0;
  padding: 16px 22px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid var(--line);
  font-size: 18px;
  font-weight: 600;
}

.flash {
  position: absolute;
  inset: 0;
  pointer-events: none;
  box-shadow: inset 0 0 0 0 var(--seat);
  z-index: 9;
}

.flash.on {
  animation: flash 0.35s ease-out;
}

@keyframes flash {
  0% {
    box-shadow: inset 0 0 0 14px var(--seat);
  }
  100% {
    box-shadow: inset 0 0 0 0 var(--seat);
  }
}

/* ---- End ---- */

#end {
  justify-content: center;
}

.end-card {
  text-align: center;
  display: grid;
  gap: 12px;
}

.end-card p {
  margin: 0 0 6px;
  color: var(--muted);
}

.end-icon {
  justify-self: center;
  width: 64px;
  height: 64px;
  border-radius: 20px;
  display: grid;
  place-items: center;
  background: var(--panel2);
  font-size: 32px;
}

.connection {
  position: fixed;
  left: 50%;
  bottom: max(16px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  padding: 10px 16px;
  border-radius: 999px;
  background: #ffc145;
  color: #0a0f1e;
  font-weight: 700;
  z-index: 20;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation: none !important;
    transition: none !important;
  }
}
