:root {
  --bg: #0f0f1a;
  --card: #1a1a2e;
  --line: #2c2c48;
  --text: #f4f4fb;
  --muted: #9a9ab8;
  --p1: #ff5a6e;
  --p2: #2ec4f1;
  --accent: #ffd23f;
}

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

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

button, input { font: inherit; color: inherit; }

.screen {
  display: none;
  min-height: 100dvh;
  padding: 24px 16px calc(24px + env(safe-area-inset-bottom));
  flex-direction: column;
  align-items: center;
  gap: 16px;
  max-width: 520px;
  margin: 0 auto;
  position: relative;
}
.screen.active { display: flex; }
.hidden { display: none !important; }
.center { display: flex; flex-direction: column; align-items: center; gap: 14px; width: 100%; }
.muted { color: var(--muted); margin: 0; }

/* ---------- home ---------- */
.logo {
  font-size: clamp(44px, 14vw, 72px);
  font-weight: 900;
  letter-spacing: -2px;
  margin: 24px 0 0;
  line-height: 1;
  text-align: center;
}
.logo span { color: var(--p1); margin-left: 6px; }
.logo em { color: var(--p2); font-style: normal; margin-left: 6px; }
.logo b { color: var(--accent); animation: dots 1.4s steps(4) infinite; display: inline-block; overflow: hidden; vertical-align: bottom; width: 1.1em; }
@keyframes dots { 0% { width: 0; } 100% { width: 1.1em; } }
.tagline { color: var(--muted); margin: 0 0 8px; text-align: center; }

.card {
  width: 100%;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.label { font-size: 13px; text-transform: uppercase; letter-spacing: 1px; color: var(--muted); }

.formats { display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px; width: 100%; }
.formats button {
  border: 2px solid var(--line);
  background: transparent;
  border-radius: 12px;
  padding: 10px 0;
  font-weight: 800;
  cursor: pointer;
}
.formats button.wide { grid-column: 1 / -1; }
.formats button.selected { border-color: var(--accent); color: var(--accent); background: #ffd23f14; }
.formats button:disabled { opacity: .5; cursor: default; }
.formats.small button { padding: 6px 0; font-size: 14px; }

.tabs { display: flex; width: 100%; background: var(--card); border-radius: 14px; padding: 4px; border: 1px solid var(--line); }
.tab { flex: 1; border: 0; background: transparent; padding: 10px; border-radius: 10px; font-weight: 700; color: var(--muted); cursor: pointer; }
.tab.selected { background: var(--line); color: var(--text); }

input {
  width: 100%;
  min-width: 0;
  background: var(--bg);
  border: 2px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  outline: none;
}
input:focus { border-color: var(--accent); }
input.p1:focus { border-color: var(--p1); }
input.p2:focus { border-color: var(--p2); }
#code { text-transform: uppercase; text-align: center; letter-spacing: 4px; font-weight: 800; width: 96px; flex: none; }

.row { display: flex; gap: 8px; width: 100%; }
.row > .btn { flex: 1; }

.btn {
  border: 2px solid var(--line);
  background: var(--card);
  border-radius: 14px;
  padding: 12px 14px;
  font-weight: 800;
  cursor: pointer;
  transition: transform .08s;
}
.btn:active { transform: scale(.97); }
.btn.primary { background: var(--accent); color: #1a1400; border-color: var(--accent); }
.btn.big { font-size: 22px; padding: 16px 40px; }
.btn:disabled { opacity: .45; cursor: default; }
.btn.is-ready { background: #3ddc84; border-color: #3ddc84; color: #002b12; }

.link { background: none; border: 0; color: var(--muted); text-decoration: underline; cursor: pointer; padding: 8px; }
.hint { font-size: 13px; color: var(--muted); margin: 0; text-align: center; line-height: 1.6; }
kbd { background: var(--line); border-radius: 6px; padding: 1px 7px; font-weight: 800; color: var(--text); }
.error { color: var(--p1); min-height: 1.2em; margin: 0; text-align: center; }

.back {
  position: absolute; top: 16px; left: 16px;
  width: 40px; height: 40px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--card); cursor: pointer; font-size: 18px;
}

/* ---------- lobby ---------- */
#lobby .center { margin-top: 64px; }
.room-code { font-size: 64px; font-weight: 900; letter-spacing: 10px; color: var(--accent); }
.lobby-players { display: flex; gap: 12px; width: 100%; }
.lobby-player {
  flex: 1; text-align: center; padding: 16px 8px; border-radius: 16px;
  border: 2px dashed var(--line); background: var(--card); font-weight: 800;
}
.lobby-player.p1 { border-color: var(--p1); border-style: solid; }
.lobby-player.p2 { border-color: var(--p2); border-style: solid; }
.lobby-player small { display: block; font-weight: 600; color: var(--muted); margin-top: 6px; }
.lobby-player small.ok { color: #3ddc84; }
.spinner {
  width: 56px; height: 56px; border-radius: 50%;
  border: 6px solid var(--line); border-top-color: var(--accent);
  animation: spin 0.9s linear infinite; margin-top: 64px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- game ---------- */
#game { max-width: none; padding: 0; gap: 0; height: 100dvh; overflow: hidden; touch-action: none; user-select: none; -webkit-user-select: none; }
.pad {
  flex: 1;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  touch-action: none;
  cursor: pointer;
}
.pad.top { background: radial-gradient(circle at 50% 60%, #ff5a6e33, #ff5a6e0d 70%); }
.pad.bottom { background: radial-gradient(circle at 50% 40%, #2ec4f133, #2ec4f10d 70%); }
.pad.top .pad-count { color: var(--p1); }
.pad.bottom .pad-count { color: var(--p2); }
.pad.flipped .pad-inner { transform: rotate(180deg); }
.pad-inner { text-align: center; pointer-events: none; }
.pad-name { font-weight: 800; font-size: 18px; color: var(--muted); }
.pad-count { font-size: clamp(80px, 22vh, 180px); font-weight: 900; line-height: 1; font-variant-numeric: tabular-nums; }
.pad-hint { font-size: 14px; color: var(--muted); min-height: 1.2em; }
.pad.bump .pad-count { animation: bump .12s ease-out; }
@keyframes bump { 0% { transform: scale(1.12); } 100% { transform: scale(1); } }
.pad.won { box-shadow: inset 0 0 0 6px var(--accent); }

.ripple {
  position: absolute; width: 90px; height: 90px; margin: -45px 0 0 -45px;
  border-radius: 50%; background: #ffffff40; pointer-events: none;
  animation: ripple .45s ease-out forwards;
}
@keyframes ripple { from { transform: scale(.2); opacity: 1; } to { transform: scale(2.4); opacity: 0; } }

.hud {
  width: 100%;
  padding: 10px 16px;
  background: var(--card);
  border-top: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 4px 12px;
  z-index: 2;
}
.hud-round { font-size: 13px; color: var(--muted); font-weight: 700; }
.hud-big { font-size: 32px; font-weight: 900; text-align: center; min-width: 120px; font-variant-numeric: tabular-nums; }
.hud-big.go { color: var(--accent); }
.hud-score { font-size: 13px; color: var(--muted); font-weight: 700; text-align: right; }
.timebar { grid-column: 1 / -1; height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; }
#timebar-fill { height: 100%; width: 100%; background: var(--accent); transform-origin: left; }
.back.over { top: auto; bottom: calc(16px + env(safe-area-inset-bottom)); left: 16px; z-index: 3; opacity: .6; }

/* ---------- results ---------- */
#results { justify-content: center; }
.winner { font-size: 40px; font-weight: 900; text-align: center; margin: 0; }
.winner.p1 { color: var(--p1); }
.winner.p2 { color: var(--p2); }
.rounds { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.rounds th, .rounds td { padding: 10px 8px; text-align: center; border-bottom: 1px solid var(--line); }
.rounds th { color: var(--muted); font-size: 13px; font-weight: 700; }
.rounds th.p1 { color: var(--p1); }
.rounds th.p2 { color: var(--p2); }
.rounds td.win { color: var(--accent); font-weight: 900; }
.rounds tfoot td { font-weight: 900; border-bottom: 0; }

/* ---------- leaderboard ---------- */
#leaders h2 { margin: 24px 0 0; }
.lb { width: 100%; list-style: none; padding: 0; margin: 0; counter-reset: lb; }
.lb li {
  counter-increment: lb;
  display: flex; align-items: center; gap: 12px;
  padding: 12px 14px; border-bottom: 1px solid var(--line);
}
.lb li::before { content: counter(lb); width: 24px; color: var(--muted); font-weight: 800; }
.lb li:nth-child(1)::before { content: "🥇"; }
.lb li:nth-child(2)::before { content: "🥈"; }
.lb li:nth-child(3)::before { content: "🥉"; }
.lb .n { flex: 1; font-weight: 700; overflow: hidden; text-overflow: ellipsis; }
.lb .t { font-weight: 900; color: var(--accent); }
.lb .r { color: var(--muted); font-size: 13px; width: 70px; text-align: right; }
.lb .empty::before { content: ""; width: 0; }
.small-print { font-size: 12px; text-align: center; }

/* ---------- last 5 games ---------- */
#history-record { text-transform: none; letter-spacing: 0; color: var(--text); font-weight: 700; }
.history { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.history li { display: flex; align-items: center; gap: 10px; font-variant-numeric: tabular-nums; }
.history .badge {
  width: 28px; height: 28px; border-radius: 8px; flex: none;
  display: grid; place-items: center; font-weight: 900; font-size: 14px;
  background: var(--line); color: var(--muted);
}
.history .badge.W { background: #3ddc84; color: #002b12; }
.history .badge.L { background: var(--p1); color: #2b0007; }
.history .who { flex: 1; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history .score { font-weight: 800; }
.history .fmt { width: 52px; text-align: right; color: var(--muted); font-size: 13px; }

/* ---------- account bar ---------- */
.account {
  width: 100%; display: flex; align-items: center; gap: 10px;
  background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 8px 12px;
}
.me-stars { font-weight: 900; color: var(--accent); }
.me-name {
  font-weight: 800; flex: 1; min-width: 0; text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  background: none; border: 0; padding: 4px 0; cursor: pointer;
}
.me-name::after { content: " ✎"; color: var(--muted); font-weight: 600; font-size: 13px; }
.me-rank { font-weight: 700; }
.account .link { padding: 4px; }

/* ---------- toasts, banner ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(24px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  max-width: min(92vw, 480px); padding: 12px 16px; border-radius: 12px; z-index: 50;
  background: #3a1520; border: 1px solid var(--p1); color: var(--text); font-weight: 600; text-align: center;
  box-shadow: 0 8px 24px #0008; animation: toast-in .2s ease-out;
}
.toast.ok { background: #10301f; border-color: #3ddc84; }
.toast.info { background: var(--card); border-color: var(--line); }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }
.net-banner {
  position: fixed; top: 0; left: 0; right: 0; z-index: 40;
  padding: calc(6px + env(safe-area-inset-top)) 16px 6px; text-align: center; font-size: 13px; font-weight: 700;
  background: #5c4400; color: #fff3c4;
}

/* ---------- modal ---------- */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 30; background: #000a;
  display: grid; place-items: center; padding: 16px;
}
.modal {
  width: min(100%, 380px); background: var(--card); border: 1px solid var(--line); border-radius: 18px;
  padding: 20px; display: flex; flex-direction: column; gap: 12px; text-align: center;
}
.modal h3 { margin: 0; font-size: 20px; }
.modal p { margin: 0; color: var(--muted); }
.modal-timer { height: 4px; background: var(--line); border-radius: 2px; overflow: hidden; }
.modal-timer div { height: 100%; background: var(--accent); transform-origin: left; }
.btn.danger { background: var(--p1); border-color: var(--p1); color: #2b0007; }
.btn.small { padding: 6px 12px; font-size: 14px; border-radius: 10px; }

/* ---------- friends ---------- */
#friends h2 { margin: 24px 0 0; }
#btn-friends { display: flex; align-items: center; justify-content: center; gap: 8px; }
.pill { font-size: 12px; font-weight: 800; background: #3ddc8426; color: #3ddc84; border-radius: 99px; padding: 2px 8px; }
.dot-badge { font-size: 12px; font-weight: 900; background: var(--p1); color: #fff; border-radius: 99px; min-width: 20px; padding: 1px 6px; }
.people { list-style: none; margin: 0; padding: 0; width: 100%; }
.people li { display: flex; align-items: center; gap: 10px; padding: 10px 4px; border-bottom: 1px solid var(--line); }
.people .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--line); flex: none; }
.people li.online .dot { background: #3ddc84; box-shadow: 0 0 8px #3ddc84; }
.people .n { font-weight: 800; }
.people .s { flex: 1; color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.people .acts { display: flex; gap: 6px; }
.people li.pending { opacity: .75; }
.people .empty { justify-content: center; border: 0; }
#requests-card .people li:last-child { border-bottom: 0; }

/* ---------- lobby / results / rankings additions ---------- */
.lobby-player .stars { display: block; color: var(--accent); font-size: 14px; margin-top: 4px; }
.star-change { font-size: 22px; font-weight: 900; margin: -6px 0 0; color: var(--muted); }
.star-change.up { color: #3ddc84; }
.star-change.down { color: var(--p1); }
.formats.six { grid-template-columns: repeat(6, 1fr); }
.lb li.me { background: #ffd23f14; border-radius: 10px; }
.lb .n.is-online::after { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: #3ddc84; margin-left: 6px; vertical-align: middle; }

/* ---------- server switch ---------- */
.server-switch { width: 100%; display: flex; flex-direction: column; gap: 6px; }
.seg { display: flex; background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 4px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 8px; border-radius: 10px; font-weight: 700; font-size: 14px; color: var(--muted); cursor: pointer; }
.seg button.selected { background: var(--line); color: var(--text); }
.server-switch .hint .link { padding: 0 4px; }
.server-status { font-weight: 700; }
.server-status.ok { color: #3ddc84; }
.server-status.warn { color: var(--accent); }
.server-status.bad { color: var(--p1); }

/* ---------- guests ---------- */
.or { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: 13px; }
.or::before, .or::after { content: ""; flex: 1; height: 1px; background: var(--line); }
.pill.guest { background: var(--line); color: var(--muted); }
#claim-card { border-color: var(--accent); }

/* ---------- land grab ---------- */
.land { display: none; position: absolute; inset: 0; z-index: 0; background: #18455a; }
#game.land-mode .land { display: block; }
.land-top { height: 50%; background: #572533; transition: height .08s ease-out; }
#game.land-mode .pad { background: transparent; z-index: 1; }
#game.land-mode .hud { position: relative; z-index: 2; background: #1a1a2ee6; }
#game.land-mode .pad-count, #game.land-mode .timebar { display: none; }
/* Land Grab icon: a square split between the two players' colours. */
.land-icon {
  display: inline-block; width: 14px; height: 14px; margin-right: 8px; vertical-align: -2px;
  border-radius: 3px; background: linear-gradient(var(--p1) 0 50%, var(--p2) 50% 100%);
}

/* ---------- chat ---------- */
.chat { gap: 8px; padding: 12px; }
.chat-log { max-height: 220px; min-height: 60px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; padding-right: 4px; }
.chat-log:empty::before { content: "Say hi 👋 — messages are kept for next time."; color: var(--muted); font-size: 13px; text-align: center; margin: auto; }
.msg { display: flex; flex-direction: column; max-width: 80%; }
.msg.me { align-self: flex-end; align-items: flex-end; }
.msg.them { align-self: flex-start; align-items: flex-start; }
.msg .bubble { padding: 8px 12px; border-radius: 14px; line-height: 1.35; word-break: break-word; white-space: pre-wrap; }
.msg.me .bubble { background: #2ec4f1; color: #00202b; border-bottom-right-radius: 4px; }
.msg.them .bubble { background: var(--line); color: var(--text); border-bottom-left-radius: 4px; }
.msg.big .bubble { background: none; font-size: 34px; padding: 0 4px; line-height: 1.1; }
.msg .meta { font-size: 11px; color: var(--muted); margin: 2px 4px 0; }
.emoji-row { display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none; }
.emoji-row::-webkit-scrollbar { display: none; }
.emoji-row button { flex: none; border: 0; background: transparent; font-size: 22px; padding: 4px 6px; border-radius: 10px; cursor: pointer; }
.emoji-row button:active { background: var(--line); transform: scale(1.15); }
#chat-input { padding: 10px 12px; }
#room-view .chat, #results .chat { width: 100%; }

/* ---------- compact layout ---------- */
/* Round picker: a tall Land Grab on the left, lengths in two rows beside it. */
.formats.picker { grid-template-columns: 1.5fr repeat(3, 1fr); grid-auto-rows: 40px; gap: 6px; }
.formats.picker button { padding: 0; font-size: 15px; display: flex; align-items: center; justify-content: center; }
.formats.picker .grab { grid-row: span 2; flex-direction: column; gap: 6px; font-size: 13px; line-height: 1.1; }
.formats.picker .grab .land-icon { margin: 0; width: 18px; height: 18px; }
.formats.picker .any { font-size: 14px; }

/* Less vertical space on the home screen. */
#home { gap: 10px; padding-top: calc(10px + env(safe-area-inset-top)); }
#home .logo { font-size: clamp(34px, 11vw, 52px); margin: 2px 0 0; }
#home .tagline { margin: -4px 0 0; font-size: 13px; }
#home .card { padding: 12px; gap: 10px; }
#home .label { font-size: 11px; }
#home .tab { padding: 8px; }
#home .btn { padding: 10px 14px; }
#home input { padding: 10px; }
.account { padding: 4px 12px; }
.history { gap: 4px; }
.history .badge { width: 24px; height: 24px; font-size: 13px; }
#lobby .center { margin-top: 48px; gap: 12px; }
#panel-online .row .btn { white-space: nowrap; padding: 10px 8px; font-size: 14px; }
#panel-online #code { width: 78px; letter-spacing: 3px; }
