:root {
  --bg: #0b0f1e;
  --panel: #161c33;
  --text: #f1f3fb;
  --muted: #9aa3c4;
  --accent: #4facfe;
  --left: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  --right: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  --launch: linear-gradient(135deg, #4facfe 0%, #00c6d7 100%);
  color-scheme: dark;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
}

body {
  -webkit-tap-highlight-color: transparent;
}

[hidden] {
  display: none !important;
}

.screen {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: max(16px, env(safe-area-inset-top)) 16px max(16px, env(safe-area-inset-bottom));
}

.center {
  margin: auto;
  text-align: center;
}

h1 {
  font-size: 1.6rem;
}

.muted {
  color: var(--muted);
}

.status {
  background: var(--panel);
  border-radius: 12px;
  padding: 12px 14px;
}

.status:empty {
  display: none;
}

button {
  font: inherit;
  color: var(--text);
  background: var(--panel);
  border: 1px solid #2a3358;
  border-radius: 12px;
  padding: 12px 18px;
  cursor: pointer;
}

button:disabled {
  opacity: 0.45;
}

button.primary {
  background: var(--accent);
  border-color: transparent;
  color: #04122a;
  font-weight: 700;
}

#start {
  font-size: 1.4rem;
  padding: 22px;
}

.row {
  display: flex;
  gap: 10px;
}

.row > * {
  flex: 1;
}

input {
  font: inherit;
  min-width: 0;
  color: var(--text);
  background: var(--panel);
  border: 1px solid #2a3358;
  border-radius: 12px;
  padding: 12px;
}

form {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

form .row input {
  flex: 2;
}

.switch {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
}

.switch input {
  width: 20px;
  height: 20px;
}

.final {
  font-size: 2.2rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

/* Highscore tables */
.highscores {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
}

.highscores h2 {
  font-size: 1rem;
  color: var(--muted);
  margin-bottom: 6px;
}

.highscores ol {
  list-style: none;
  background: var(--panel);
  border-radius: 12px;
  overflow: hidden;
}

.highscores li {
  display: grid;
  grid-template-columns: 2.4em 1fr auto;
  gap: 8px;
  padding: 8px 12px;
  font-variant-numeric: tabular-nums;
}

.highscores li + li {
  border-top: 1px solid #222a4a;
}

.highscores li.me {
  background: #24407a;
  font-weight: 700;
}

.highscores li.gap {
  display: block;
  text-align: center;
  color: var(--muted);
  padding: 2px;
}

.highscores .name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.highscores .empty {
  color: var(--muted);
  padding: 8px 12px;
  display: block;
}

/* Play screen */
#screen-play {
  height: 100%;
  min-height: 0;
  padding: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom));
  gap: 8px;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 8px;
  color: var(--muted);
}

.bar .score {
  color: var(--text);
  font-size: 1.6rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

.flippers {
  flex: 1;
  display: flex;
  gap: 8px;
}

.pad {
  flex: 1;
  border: none;
  border-radius: 18px;
  color: white;
  font-size: 1.8rem;
  font-weight: 800;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  transition: filter 0.05s, transform 0.05s;
}

.pad[data-button="left"] {
  background: var(--left);
}

.pad[data-button="right"] {
  background: var(--right);
}

.pad.launch {
  flex: 0 0 22%;
  background: var(--launch);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.pad small {
  font-size: 0.8rem;
  font-weight: 500;
  opacity: 0.85;
}

.pad.down {
  filter: brightness(0.7);
  transform: scale(0.98);
}

#idle-warning {
  position: fixed;
  left: 16px;
  right: 16px;
  top: 30%;
  padding: 18px;
  border-radius: 14px;
  background: rgba(200, 40, 60, 0.95);
  text-align: center;
  font-weight: 600;
  pointer-events: none;
}

#toast {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 10;
  padding: 12px 14px;
  border-radius: 12px;
  background: #3a2440;
  border: 1px solid #a0457a;
}

/* Game and character pickers */
.games {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(96px, 1fr));
  gap: 10px;
}

.game-card {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 26px 8px 16px;
  font-weight: 700;
}

/* Multiplayer games: how many play together. */
.mp-badge {
  position: absolute;
  top: 5px;
  right: 5px;
  padding: 1px 6px;
  border-radius: 999px;
  font-size: 0.7rem;
  font-weight: 800;
  line-height: 1.5;
  color: var(--accent);
  background: #0b1020;
  box-shadow: 0 0 0 1.5px var(--accent) inset;
}

.game-card .icon {
  font-size: 2.2rem;
}

.game-card.selected,
.character.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent) inset;
  background: #1d2a52;
}

#character-picker h2 {
  font-size: 1rem;
  color: var(--muted);
  margin-bottom: 6px;
}

.characters {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  scroll-snap-type: x mandatory;
}

.character {
  flex: 0 0 auto;
  width: 84px;
  padding: 6px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  font-size: 0.75rem;
  scroll-snap-align: start;
}

.character img {
  width: 64px;
  height: 64px;
  object-fit: contain;
}

/* Crossy Road swipe pad */
#screen-crossy {
  height: 100%;
  min-height: 0;
  padding: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom));
  gap: 8px;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

#swipe-pad {
  flex: 1;
  display: grid;
  place-items: center;
  text-align: center;
  color: rgba(255, 255, 255, 0.75);
  font-weight: 600;
  border-radius: 18px;
  background: linear-gradient(160deg, #3fa34d 0%, #2d6a3e 100%);
  touch-action: none;
  transition: filter 0.05s;
}

#swipe-pad.down {
  filter: brightness(0.85);
}

/* Play bar: latency and restart */
.bar .tools {
  display: flex;
  align-items: center;
  gap: 10px;
}

.restart,
.exit {
  padding: 6px 12px;
  font-size: 1.1rem;
  white-space: nowrap;
}

.restart.confirm,
.exit.confirm {
  background: #c8283c;
  border-color: transparent;
  font-size: 0.9rem;
  font-weight: 700;
}

.latency.good {
  color: #5fd38d;
}

.latency.ok {
  color: #f2c14e;
}

.latency.bad {
  color: #ff6b81;
}

/* Tetris pad */
#screen-tetris {
  --pad-bottom: max(8px, env(safe-area-inset-bottom));
  height: 100%;
  min-height: 0;
  padding: max(8px, env(safe-area-inset-top)) 8px var(--pad-bottom);
  gap: 8px;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.tetris-pad {
  /* Two big buttons per row, easy to hit without looking: move, rotate, then drop across the bottom. The pad sits at
     the bottom with free space above; the rows are sized so that the centre of ◀ ▶ is 2/5 of the screen height from
     the bottom: pad-bottom + 0.8 r + gap + r + gap + r / 2 = 40vh. */
  --gap: 8px;
  --r: calc((40vh - var(--pad-bottom) - 2 * var(--gap)) / 2.3);
  --r: calc((40dvh - var(--pad-bottom) - 2 * var(--gap)) / 2.3);
  flex: none;
  margin-top: auto;
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: calc(0.45 * var(--r)) var(--r) var(--r) calc(0.8 * var(--r));
  gap: var(--gap);
}

.tpad {
  display: grid;
  place-items: center;
  padding: 0;
  line-height: 1;
  overflow: hidden;
  border: none;
  border-radius: 18px;
  color: white;
  font-size: 2.2rem;
  font-weight: 800;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  background: linear-gradient(160deg, #23305e 0%, #151c3a 100%);
  box-shadow: inset 0 0 0 2px #2f6bff55;
  transition: filter 0.05s, transform 0.05s;
}

.tpad.small {
  font-size: 1.1rem;
}

.tpad.move {
  background: linear-gradient(160deg, #1d9bf0 0%, #6d28d9 100%);
}

.tpad.rotate {
  font-size: min(5rem, calc(0.7 * var(--r)));
  font-weight: 400;
  background: linear-gradient(160deg, #ec4899 0%, #8b5cf6 100%);
}

.tpad.drop {
  grid-column: 1 / -1;
  font-size: 2rem;
  letter-spacing: 0.1em;
  background: linear-gradient(160deg, #f59e0b 0%, #db2777 100%);
}

.tpad.soft {
  font-size: 1.6rem;
  background: linear-gradient(160deg, #10b981 0%, #0e7490 100%);
}

.tpad.down {
  filter: brightness(0.7);
  transform: scale(0.97);
}

/* Multiplayer: lobby, steering, result */
#mp-name {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.mp-players,
.mp-scores {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

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

.mp-players li.you {
  box-shadow: 0 0 0 2px var(--accent) inset;
}

.mp-players .dot {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  flex: 0 0 auto;
  box-shadow: 0 0 8px currentColor;
  background: currentColor;
}

.mp-players .badge {
  margin-left: auto;
  font-size: 0.8rem;
  color: var(--muted);
}

#mp-countdown {
  font-size: 4rem;
  font-weight: 900;
  text-align: center;
  color: var(--accent);
}

#screen-curvy,
#screen-bomber,
#screen-tanks {
  height: 100%;
  min-height: 0;
  padding: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom));
  gap: 8px;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.you-tag {
  font-weight: 800;
  font-size: 1.2rem;
}

.mp-scores {
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px 12px;
  font-variant-numeric: tabular-nums;
}

.steer-pad {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.steer {
  border: none;
  border-radius: 18px;
  color: white;
  font-size: 4rem;
  font-weight: 800;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  background: var(--player-color, var(--accent));
  box-shadow: inset 0 0 0 9999px rgba(0, 0, 0, 0.25);
  transition: filter 0.05s, transform 0.05s;
}

.steer.down {
  filter: brightness(1.3);
  transform: scale(0.97);
}

/* Bomberman: d-pad (direction from the centre, slide to turn) left, bomb right, both in thumb reach. */
.bomber-pad {
  margin-top: auto;
  padding-bottom: 12vh;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.dpad {
  position: relative;
  width: min(58vw, 46vh);
  aspect-ratio: 1;
  border-radius: 50%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  background: radial-gradient(circle, #1d2547 0 22%, #151c3a 23%);
  box-shadow: inset 0 0 0 3px var(--player-color, var(--accent));
}

.dpad span {
  position: absolute;
  display: grid;
  place-items: center;
  width: 34%;
  height: 34%;
  font-size: 2rem;
  color: rgba(255, 255, 255, 0.75);
  border-radius: 50%;
  pointer-events: none;
  transition: background 0.05s;
}

.dpad [data-dir="up"] { left: 33%; top: 2%; }
.dpad [data-dir="down"] { left: 33%; bottom: 2%; }
.dpad [data-dir="left"] { left: 2%; top: 33%; }
.dpad [data-dir="right"] { right: 2%; top: 33%; }

.dpad span.down {
  color: white;
  background: var(--player-color, var(--accent));
}

.bomb-button {
  width: min(34vw, 30vh);
  aspect-ratio: 1;
  border: none;
  border-radius: 50%;
  font-size: 3.4rem;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  background: radial-gradient(circle at 35% 30%, #ff8a5c, #e0352b 70%);
  box-shadow: 0 6px 0 #8a1d16, inset 0 0 0 3px rgba(255, 255, 255, 0.25);
  transition: transform 0.05s, box-shadow 0.05s;
}

.bomb-button.down {
  transform: translateY(4px) scale(0.97);
  box-shadow: 0 2px 0 #8a1d16, inset 0 0 0 3px rgba(255, 255, 255, 0.4);
}

/* Tank Trouble: fire left, as tall as the stick (easy to hit without looking). */
.fire-button {
  aspect-ratio: auto;
  height: min(58vw, 46vh);
  border-radius: 999px;
}

/* Tank Trouble stick: inner ring = aim (turn only), outside it = drive. The knob follows the finger. */
.stick {
  position: relative;
  width: min(58vw, 46vh);
  aspect-ratio: 1;
  border-radius: 50%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  background: #151c3a;
  box-shadow: inset 0 0 0 3px var(--player-color, var(--accent));
}

.stick-zone {
  position: absolute;
  inset: 28%;
  border-radius: 50%;
  border: 2px dashed rgba(255, 255, 255, 0.2);
  pointer-events: none;
}

.stick-knob {
  position: absolute;
  left: 34%;
  top: 34%;
  width: 32%;
  height: 32%;
  border-radius: 50%;
  background: var(--player-color, var(--accent));
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.5), inset 0 0 0 3px rgba(255, 255, 255, 0.35);
  pointer-events: none;
}

/* Flappy Bird: one big button, every touch flaps. */
#screen-flappy {
  height: 100%;
  min-height: 0;
  padding: max(8px, env(safe-area-inset-top)) 8px max(8px, env(safe-area-inset-bottom));
  gap: 8px;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.flap-pad {
  flex: 1;
  border: none;
  border-radius: 24px;
  font-size: 3rem;
  font-weight: 900;
  letter-spacing: 0.15em;
  color: rgba(255, 255, 255, 0.9);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  background: linear-gradient(170deg, #ffd23f 0%, #ff8a3c 100%);
  box-shadow: inset 0 -8px 0 rgba(0, 0, 0, 0.15);
}

.flap-pad.down {
  animation: flap-press 0.15s ease-out;
}

@keyframes flap-press {
  0% {
    filter: brightness(1.25);
    transform: scale(0.985);
  }
  100% {
    filter: none;
    transform: none;
  }
}

/* Lobby option: language (Draw & Guess). */
.segmented {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.segmented button.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent) inset;
  background: #1d2a52;
}

.segmented button:disabled {
  opacity: 1;
}

.segmented button:disabled:not(.selected) {
  opacity: 0.45;
}

/* Draw & Guess */
#screen-draw {
  gap: 8px;
  padding: max(8px, env(safe-area-inset-top)) 10px max(10px, env(safe-area-inset-bottom));
}

.draw-status {
  text-align: center;
  color: var(--muted);
  font-weight: 600;
}

.draw-hint {
  text-align: center;
  font-size: 1.8rem;
  font-weight: 900;
  letter-spacing: 0.08em;
  color: var(--player-color, var(--accent));
  min-height: 1.2em;
  overflow-wrap: anywhere;
}

.draw-choices {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.draw-choices button {
  padding: 18px;
  font-size: 1.4rem;
}

.draw-canvas {
  display: block;
  width: 100%;
  aspect-ratio: 1;
  border-radius: 12px;
  background: #fff;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}

.draw-tools {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  margin-top: 8px;
}

.draw-tools .swatch {
  flex: 1;
  height: 34px;
  padding: 0;
  border-radius: 8px;
  border: 2px solid rgba(255, 255, 255, 0.25);
}

.draw-tools .swatch.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent);
}

.draw-widths {
  display: flex;
  gap: 6px;
}

.draw-widths .width {
  width: 46px;
  height: 46px;
  padding: 0;
  display: grid;
  place-items: center;
}

.draw-widths .width span {
  display: block;
  border-radius: 50%;
  background: #fff;
}

.draw-widths .width.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px var(--accent) inset;
}

#draw-undo,
#draw-clear {
  min-width: 56px;
  height: 46px;
  padding: 0 10px;
  font-size: 1.3rem;
}

.draw-guess {
  display: flex;
  gap: 8px;
}

.draw-guess input {
  flex: 1;
  min-width: 0;
  font-size: 1.3rem;
}

.draw-guess button {
  width: 64px;
  font-size: 1.4rem;
}

.draw-guesses {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-weight: 600;
}

.draw-guesses .wrong {
  color: var(--muted);
}

.draw-guesses .close {
  color: #ffd23f;
}

.draw-guesses .correct {
  color: #7cff6b;
  font-size: 1.3rem;
  text-align: center;
}
