:root {
  --bg: #0f1420;
  --card: #1a2232;
  --card-2: #222c42;
  --accent: #ff7a59;
  --accent-2: #ffd166;
  --good: #2ecc71;
  --bad: #e74c3c;
  --text: #eef2f7;
  --muted: #9aa7bd;
  --radius: 16px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
}

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

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family:
    "Segoe UI",
    system-ui,
    -apple-system,
    Roboto,
    Helvetica,
    Arial,
    sans-serif;
  overflow: hidden;
  touch-action: manipulation;
}

/* ---------- screen switching ---------- */
.screen {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom)
    env(safe-area-inset-left);
}
.screen.active {
  display: flex;
}

/* ---------- cards ---------- */
.home-card,
.lobby-card,
.results-card {
  width: min(92vw, 440px);
  max-height: 92vh;
  overflow-y: auto;
  background: linear-gradient(160deg, var(--card), var(--card-2));
  border: 1px solid rgba(255, 255, 255, 0.06);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 28px 24px;
  text-align: center;
}

.logo {
  font-size: 2.4rem;
  margin: 0 0 4px;
  letter-spacing: -0.5px;
}
.tagline {
  color: var(--muted);
  margin: 0 0 24px;
  font-size: 0.95rem;
}

/* ---------- form fields ---------- */
.field {
  display: block;
  text-align: left;
  margin-bottom: 18px;
}
.field > span {
  display: block;
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 6px;
}
input {
  width: 100%;
  padding: 14px 16px;
  font-size: 1.05rem;
  background: #0d1420;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 12px;
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}
input:focus {
  border-color: var(--accent);
}

.join-row {
  display: flex;
  gap: 10px;
}
.code-field {
  text-transform: uppercase;
  letter-spacing: 6px;
  text-align: center;
  font-weight: 700;
  font-size: 1.3rem;
}

/* ---------- buttons ---------- */
.btn {
  appearance: none;
  border: none;
  border-radius: 12px;
  padding: 14px 18px;
  font-size: 1rem;
  font-weight: 600;
  color: var(--text);
  background: #33415c;
  cursor: pointer;
  transition:
    transform 0.08s ease,
    filter 0.15s ease,
    opacity 0.15s;
}
.btn:active {
  transform: translateY(1px) scale(0.99);
}
.btn.primary {
  background: linear-gradient(135deg, var(--accent), #ff5a7a);
  color: #1a1020;
}
.btn.big {
  width: 100%;
  padding: 16px;
  font-size: 1.1rem;
  margin-top: 6px;
}
.btn.ghost {
  background: transparent;
  color: var(--muted);
  margin-top: 14px;
}
.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.divider {
  position: relative;
  margin: 22px 0;
  color: var(--muted);
  font-size: 0.8rem;
}
.divider::before,
.divider::after {
  content: "";
  position: absolute;
  top: 50%;
  width: 38%;
  height: 1px;
  background: rgba(255, 255, 255, 0.1);
}
.divider::before {
  left: 0;
}
.divider::after {
  right: 0;
}

.error {
  color: var(--bad);
  min-height: 1.2em;
  font-size: 0.9rem;
  margin: 10px 0 0;
}

.how {
  margin-top: 22px;
  text-align: left;
  color: var(--muted);
  font-size: 0.9rem;
}
.how summary {
  cursor: pointer;
  color: var(--accent-2);
  font-weight: 600;
}
.how ul {
  margin: 10px 0 0;
  padding-left: 18px;
  line-height: 1.5;
}

/* ---------- lobby ---------- */
.code-display {
  font-size: 3rem;
  font-weight: 800;
  letter-spacing: 10px;
  color: var(--accent-2);
  background: #0d1420;
  border-radius: 12px;
  padding: 14px;
  margin: 8px 0 6px;
}
.hint {
  color: var(--muted);
  font-size: 0.85rem;
  margin: 8px 0;
}
.badge {
  font-size: 0.8rem;
  background: #33415c;
  border-radius: 999px;
  padding: 2px 10px;
  vertical-align: middle;
}
.player-list {
  list-style: none;
  padding: 0;
  margin: 10px 0 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.player-list li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #0d1420;
  border-radius: 10px;
  padding: 12px 14px;
  font-weight: 600;
}
.player-list .tag {
  font-size: 0.72rem;
  color: var(--accent-2);
  background: rgba(255, 209, 102, 0.12);
  padding: 2px 8px;
  border-radius: 999px;
}
.player-list .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--good);
  margin-right: 8px;
  display: inline-block;
}
.player-list .dot.off {
  background: var(--muted);
}

/* ---------- avatar studio ---------- */
.avatar-studio {
  display: flex;
  gap: 14px;
  align-items: flex-start;
  background: #0d1420;
  border-radius: 12px;
  padding: 12px;
  margin-bottom: 18px;
  text-align: left;
}
.avatar-preview {
  width: 120px;
  height: 160px;
  flex-shrink: 0;
  border-radius: 10px;
  background: radial-gradient(circle at 50% 35%, #263349, #0d1420 70%);
}
.avatar-controls {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 0;
}
.swatch-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.swatch-label {
  font-size: 0.68rem;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--muted);
}
.swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}
.swatch {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 2px solid transparent;
  cursor: pointer;
  padding: 0;
  transition:
    transform 0.08s,
    border-color 0.12s;
}
.swatch:active {
  transform: scale(0.9);
}
.swatch.selected {
  border-color: var(--accent-2);
  box-shadow: 0 0 0 2px rgba(255, 209, 102, 0.3);
}
.swatches.chips {
  gap: 6px;
}
.chip {
  border-radius: 999px;
  width: auto;
  height: auto;
  padding: 5px 10px;
  font-size: 0.75rem;
  font-weight: 600;
  background: #22304a;
  color: var(--text);
  border: 2px solid transparent;
  cursor: pointer;
}
.chip.selected {
  border-color: var(--accent-2);
  color: var(--accent-2);
}

/* ---------- game ---------- */
#game {
  padding: 0;
}
#scene {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}
#hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
  display: flex;
  flex-direction: column;
}
.hud-top {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: calc(env(safe-area-inset-top) + 10px) 12px 0;
}
.hud-pill {
  background: rgba(13, 20, 32, 0.7);
  backdrop-filter: blur(6px);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 999px;
  padding: 8px 14px;
  font-weight: 700;
  font-size: 0.95rem;
}
.hud-pill.timer {
  color: var(--accent-2);
}

.order-banner {
  align-self: center;
  margin-top: 10px;
  background: rgba(13, 20, 32, 0.78);
  backdrop-filter: blur(6px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 14px;
  padding: 8px 16px;
  text-align: center;
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 220px;
}
.order-label {
  font-size: 0.7rem;
  letter-spacing: 2px;
  color: var(--muted);
}
.order-food {
  font-size: 1.15rem;
  font-weight: 800;
}
.order-target {
  font-size: 0.95rem;
  color: var(--accent-2);
  font-weight: 700;
}

.combo {
  align-self: center;
  margin-top: 8px;
  font-weight: 800;
  color: var(--accent-2);
  text-shadow: 0 2px 8px rgba(0, 0, 0, 0.6);
  min-height: 1.2em;
}

.toast {
  position: fixed;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%) scale(0.8);
  font-size: 1.8rem;
  font-weight: 900;
  opacity: 0;
  transition:
    opacity 0.2s,
    transform 0.2s;
  pointer-events: none;
  text-shadow: 0 3px 12px rgba(0, 0, 0, 0.7);
}
.toast.show {
  opacity: 1;
  transform: translate(-50%, -50%) scale(1);
}
.toast.good {
  color: var(--good);
}
.toast.bad {
  color: var(--bad);
}

.leaderboard {
  position: fixed;
  top: 92px;
  right: 10px;
  width: 170px;
  background: rgba(13, 20, 32, 0.68);
  backdrop-filter: blur(6px);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 12px;
  padding: 8px;
  font-size: 0.82rem;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.leaderboard .row {
  display: flex;
  justify-content: space-between;
  gap: 6px;
  padding: 3px 6px;
  border-radius: 6px;
}
.leaderboard .row.me {
  background: rgba(255, 122, 89, 0.22);
}
.leaderboard .row .nm {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.leaderboard .row .pts {
  font-weight: 800;
  color: var(--accent-2);
}

/* ---------- touch controls ---------- */
.controls {
  margin-top: auto;
  padding: 12px 14px calc(env(safe-area-inset-bottom) + 14px);
  display: flex;
  gap: 10px;
  pointer-events: auto;
}
.ctrl {
  appearance: none;
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 16px;
  background: rgba(30, 40, 60, 0.82);
  color: var(--text);
  font-size: 1.6rem;
  font-weight: 800;
  padding: 18px 0;
  cursor: pointer;
  user-select: none;
}
.ctrl.steer {
  flex: 1;
}
.ctrl.deliver {
  flex: 1.6;
  font-size: 1.2rem;
  background: linear-gradient(135deg, var(--accent), #ff5a7a);
  color: #1a1020;
}
.ctrl:active {
  filter: brightness(1.15);
  transform: scale(0.98);
}

/* ---------- overlays ---------- */
.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(8, 12, 20, 0.6);
  backdrop-filter: blur(3px);
  font-size: 7rem;
  font-weight: 900;
  color: var(--accent-2);
  z-index: 20;
}
.overlay.hidden {
  display: none;
}

/* ---------- results ---------- */
.results-list {
  list-style: none;
  counter-reset: rank;
  padding: 0;
  margin: 16px 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.results-list li {
  counter-increment: rank;
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #0d1420;
  border-radius: 10px;
  padding: 12px 14px;
  font-weight: 600;
}
.results-list li::before {
  content: counter(rank);
  width: 26px;
  height: 26px;
  margin-right: 10px;
  border-radius: 50%;
  background: #33415c;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 0.85rem;
  flex-shrink: 0;
}
.results-list li:first-child::before {
  background: var(--accent-2);
  color: #1a1020;
}
.results-list li .name-wrap {
  display: flex;
  align-items: center;
  overflow: hidden;
}
.results-list li .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.results-list li .pts {
  color: var(--accent-2);
  font-weight: 800;
}

@media (min-width: 720px) {
  .leaderboard {
    width: 200px;
    font-size: 0.9rem;
  }
  .controls {
    max-width: 560px;
    margin-left: auto;
    margin-right: auto;
    width: 100%;
  }
}
