:root {
  --bg: #10131b;
  --surface: #1a1f2b;
  --surface-2: #232a39;
  --line: #313a4d;
  --text: #f2f4f8;
  --muted: #9aa5ba;
  --accent: #f2b134;
  --accent-2: #6ee7b7;
  --danger: #f87171;
  --radius: 18px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  min-height: 100vh;
  color: var(--text);
  background:
    radial-gradient(circle at 15% 10%, rgba(242, 177, 52, 0.12), transparent 40%),
    radial-gradient(circle at 85% 90%, rgba(110, 231, 183, 0.10), transparent 45%),
    var(--bg);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
}

button, input { font: inherit; }

button {
  cursor: pointer;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 11px 16px;
  color: var(--text);
  background: var(--surface-2);
  transition: background 140ms ease, border-color 140ms ease, opacity 140ms ease;
}

button:hover:not(:disabled) { background: #2c3446; border-color: #4a5670; }
button:disabled { opacity: 0.45; cursor: not-allowed; }

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #2a1c00;
  font-weight: 700;
}

button.primary:hover:not(:disabled) { background: #ffc455; border-color: #ffc455; }
button.ghost { background: transparent; }
button.small { padding: 8px 12px; font-size: 13px; }

input {
  width: 100%;
  padding: 11px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #121722;
  color: var(--text);
  outline: none;
}

input:focus { border-color: var(--accent); }

.app {
  width: min(1100px, 100%);
  margin: 0 auto;
  padding: 20px 20px 48px;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 28px;
}

.brand { display: flex; align-items: baseline; gap: 10px; }
.brand-mark { font-size: 26px; font-weight: 800; letter-spacing: 1px; color: var(--accent); }
.brand-sub { color: var(--muted); font-size: 13px; letter-spacing: 2px; text-transform: uppercase; }

.connection, .live-badge {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
}

.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--danger); }
.connection.online .dot, .live-badge .dot { background: var(--accent-2); }

.hidden { display: none !important; }

.card {
  background: linear-gradient(160deg, rgba(35, 42, 57, 0.96), rgba(26, 31, 43, 0.96));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 28px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.28);
}

.home-card { max-width: 520px; margin: 40px auto; }
.lobby-card { max-width: 620px; margin: 20px auto; }

h1 { margin: 0 0 8px; font-size: 34px; }
h1 strong { color: var(--accent); }
h2 { margin: 0; font-size: 20px; }
.subtitle { color: var(--muted); margin: 0 0 22px; line-height: 1.5; }
.hint { color: var(--muted); font-size: 13px; line-height: 1.5; margin: 16px 0 0; }
.hint.center { text-align: center; }

.eyebrow {
  display: block;
  color: var(--muted);
  font-size: 11px;
  letter-spacing: 1.6px;
  text-transform: uppercase;
  margin-bottom: 6px;
}

.field { display: block; margin-bottom: 20px; }
.field span { display: block; color: var(--muted); font-size: 13px; margin-bottom: 8px; }

.home-actions { display: grid; gap: 12px; }
.home-actions .primary { width: 100%; }
.join-row { display: flex; gap: 10px; }
.join-row input { flex: 1; letter-spacing: 2px; text-transform: uppercase; }

.room-heading { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; }
.room-code {
  font-size: 38px;
  font-weight: 800;
  letter-spacing: 8px;
  color: var(--accent);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}

.players-box {
  background: #121722;
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 16px;
  margin: 18px 0;
}

.player-chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chip {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 11px;
  border-radius: 999px;
  background: var(--surface-2);
  font-size: 13px;
}
.chip .tag { color: var(--accent-2); font-size: 11px; }
.chip.offline { opacity: 0.5; }

.lobby-actions { display: flex; gap: 10px; }
.lobby-actions .primary { flex: 1; }

.game-heading { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 18px; }
.pill {
  padding: 8px 13px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 13px;
}
.pill strong { color: var(--text); }
#phase-pill { color: var(--accent-2); }
#phase-pill.finished { color: var(--accent); }
#game-leave-btn { margin-left: auto; }

.game-layout {
  display: grid;
  grid-template-columns: minmax(0, 560px) minmax(240px, 1fr);
  gap: 22px;
  align-items: start;
}

.board-wrap { min-width: 0; }
.board {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  padding: 0;
  border-radius: var(--radius);
  background: #222938;
  border: 1px solid var(--line);
}

.grid, .tiles { position: absolute; inset: 0; }
.cell {
  position: absolute;
  border-radius: 10px;
  background: #171c27;
}

.tile {
  position: absolute;
  top: 0;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  font-weight: 800;
  background: #eee4da;
  color: #1d2230;
  transition: transform 120ms ease-out;
  will-change: transform;
}

.tile.spawn { animation: pop 150ms ease-out; }
.tile.compact { font-size: 0.72em !important; }

@keyframes pop {
  from { opacity: 0; }
  to { opacity: 1; }
}

.tile-2 { background: #eee4da; }
.tile-4 { background: #ede0c8; }
.tile-8 { background: #f2b179; color: #fff; }
.tile-16 { background: #f59563; color: #fff; }
.tile-32 { background: #f67c5f; color: #fff; }
.tile-64 { background: #f65e3b; color: #fff; }
.tile-128 { background: #edcf72; color: #fff; }
.tile-256 { background: #edcc61; color: #fff; }
.tile-512 { background: #edc850; color: #fff; }
.tile-1024 { background: #edc53f; color: #fff; }
.tile-2048 { background: #ecc22c; color: #fff; box-shadow: 0 0 24px rgba(236, 194, 44, 0.5); }

.panel {
  background: linear-gradient(160deg, rgba(35, 42, 57, 0.96), rgba(26, 31, 43, 0.96));
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 20px;
}

.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; }

.leaderboard { list-style: none; margin: 0; padding: 0; display: grid; gap: 8px; }
.leaderboard li {
  display: grid;
  grid-template-columns: 26px 1fr auto;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 12px;
  background: #141925;
  border: 1px solid transparent;
  font-size: 14px;
}
.leaderboard li.you { border-color: var(--accent); }
.leaderboard li.winner { border-color: var(--accent-2); }
.leaderboard .rank { color: var(--muted); font-size: 12px; }
.leaderboard .who { display: flex; align-items: center; gap: 6px; min-width: 0; }
.leaderboard .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.leaderboard .score-val { font-weight: 800; color: var(--accent); }
.leaderboard .badge { color: var(--muted); font-size: 11px; }

.mini-board {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 3px;
  margin-top: 8px;
}

.mini-cell {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  font-size: 10px;
  font-weight: 700;
}

.mini-cell:not([class*="tile-"]) {
  background: #171c27;
  color: var(--text);
}

.overlay {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(6, 8, 13, 0.78);
  z-index: 20;
}
.results-card { width: min(480px, 100%); text-align: center; }
.results-card h2 { font-size: 30px; margin-bottom: 8px; color: var(--accent); }
.results-card .primary { width: 100%; margin-top: 18px; }
.results-card .eyebrow { text-align: center; }
.results-list { text-align: left; max-height: 320px; overflow-y: auto; }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  max-width: min(420px, calc(100% - 40px));
  padding: 12px 18px;
  border-radius: 12px;
  background: var(--surface-2);
  border: 1px solid var(--line);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.4);
  font-size: 14px;
  z-index: 30;
}

@media (max-width: 820px) {
  .game-layout { grid-template-columns: 1fr; }
  .panel { order: 2; }
}
