:root {
  --bg: #0b1020;
  --bg-2: #121a33;
  --panel: #151e3b;
  --line: rgba(148, 163, 184, 0.16);
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #f7734b;
  --accent-2: #7c8cf0;
  --danger: #f87171;
  --good: #4ade80;
  --radius: 14px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: ui-rounded, 'SF Pro Rounded', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body {
  background: radial-gradient(1200px 600px at 50% -10%, #1b2750 0%, var(--bg) 60%) fixed;
}

#app { height: 100dvh; position: relative; overflow: hidden; }

.hidden { display: none !important; }

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

.screen {
  display: none;
  position: absolute;
  inset: 0;
  padding: calc(var(--safe-t) + 12px) calc(var(--safe-r) + 16px) calc(var(--safe-b) + 12px) calc(var(--safe-l) + 16px);
}
.screen.active { display: flex; }

/* ---------- Menu & lobby ---------- */
#menu, #lobby { overflow-y: auto; justify-content: center; }
.menu-inner {
  width: 100%;
  max-width: 420px;
  margin: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 12px 0 24px;
}

h1 {
  margin: 0;
  text-align: center;
  font-size: clamp(40px, 12vw, 56px);
  letter-spacing: 0.02em;
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}
h2 { margin: 0 0 4px; font-size: 18px; }
.tagline { margin: -6px 0 6px; text-align: center; color: var(--muted); }

.logo { display: grid; grid-template-columns: repeat(2, 26px); gap: 4px; justify-content: center; margin-top: 8px; }
.logo span {
  width: 26px; height: 26px; border-radius: 8px; background: var(--c);
  animation: bob 2.4s ease-in-out infinite;
}
.logo span:nth-child(2) { animation-delay: .2s; }
.logo span:nth-child(3) { animation-delay: .4s; }
.logo span:nth-child(4) { animation-delay: .6s; }
@keyframes bob { 50% { transform: translateY(-4px); } }

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.field { display: flex; flex-direction: column; gap: 6px; color: var(--muted); font-size: 14px; }
.field.inline { flex-direction: row; align-items: center; justify-content: space-between; }
input[type=text], select {
  background: var(--bg-2);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 17px;
  color: var(--text);
  outline: none;
  width: 100%;
}
select { width: auto; padding: 8px 12px; }
input[type=text]:focus, select:focus { border-color: var(--accent); }

.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--bg-2);
  border-radius: 12px;
  padding: 12px 16px;
  font-weight: 700;
  font-size: 16px;
  cursor: pointer;
  touch-action: manipulation;
  transition: transform .08s ease, background .15s ease, border-color .15s;
}
.btn:hover { border-color: rgba(148,163,184,.35); }
.btn:active { transform: scale(.97); }
.btn:focus-visible, .ctl:focus-visible, .icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.btn.primary { background: linear-gradient(135deg, #e2552f, #4f5fd8); border-color: transparent; color: white; }
.btn.big { padding: 16px; font-size: 18px; }
.btn.ghost { background: transparent; }
.btn:disabled { opacity: .5; pointer-events: none; }

.join-row { display: flex; gap: 8px; }
.join-row input { text-transform: uppercase; letter-spacing: .25em; text-align: center; font-weight: 700; }
.join-row .btn { flex: 0 0 auto; min-width: 88px; }

.best { text-align: center; margin: 0; color: var(--muted); }
.best strong { color: var(--text); }

.settings summary { cursor: pointer; font-weight: 700; list-style: none; }
.settings summary::-webkit-details-marker { display: none; }
.settings summary::after { content: '▾'; float: right; color: var(--muted); }
.settings[open] summary::after { content: '▴'; }
.toggle { display: flex; align-items: center; gap: 10px; font-size: 15px; }
.toggle input { width: 20px; height: 20px; accent-color: var(--accent); }
.help p { margin: 8px 0; font-size: 14px; line-height: 1.45; color: #cbd5e1; }
.sync p { margin: 6px 0; font-size: 14px; line-height: 1.45; color: #cbd5e1; }
.sync .join-row input { letter-spacing: .08em; font-size: 15px; padding: 12px 10px; min-width: 0; }
.sync-code { text-align: center; font-size: 22px !important; font-weight: 800; letter-spacing: .12em; color: var(--text) !important; font-variant-numeric: tabular-nums; user-select: all; }
.sync-row { display: flex; gap: 8px; }
.sync-row .btn { flex: 1; }
.fine { font-size: 12px !important; color: var(--muted) !important; text-align: center; }

.code-wrap { display: flex; flex-direction: column; align-items: center; gap: 8px; }
.code-label { color: var(--muted); font-size: 14px; }
.code { font-size: 56px; font-weight: 800; letter-spacing: .18em; padding-left: .18em; font-variant-numeric: tabular-nums; }
.status { display: flex; align-items: center; justify-content: center; gap: 10px; color: var(--muted); min-height: 24px; text-align: center; }
.status.error { color: var(--danger); }
.status.ok .spinner { display: none; }
.status.error .spinner { display: none; }
.spinner { width: 16px; height: 16px; border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; flex: 0 0 auto; }
@keyframes spin { to { transform: rotate(360deg); } }
.players { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 8px; font-weight: 700; }
.player { display: flex; align-items: center; gap: 8px; background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 8px 14px; max-width: 46%; }
.player.me { border-color: var(--accent); }
.player span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--good); flex: 0 0 auto; }
.vs { color: var(--muted); }

/* ---------- Game ---------- */
#game { flex-direction: column; gap: 8px; padding-top: calc(var(--safe-t) + 8px); padding-bottom: calc(var(--safe-b) + 8px); padding-left: calc(var(--safe-l) + 8px); padding-right: calc(var(--safe-r) + 8px); }
.hud { display: flex; align-items: center; gap: 8px; }
.stat { flex: 1; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 4px 10px; display: flex; flex-direction: column; min-width: 0; }
.stat span { font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.stat strong { font-size: 18px; font-variant-numeric: tabular-nums; overflow: hidden; text-overflow: ellipsis; }
/* Score gets more room than lines and level: a five or six digit score must not truncate on a phone. */
.hud .stat:first-child { flex: 1.8; }
.icon-btn { width: 44px; height: 44px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); font-size: 14px; cursor: pointer; touch-action: manipulation; }
#btn-restart { font-size: 22px; line-height: 1; }
#btn-end { font-size: 16px; }
body.versus #btn-restart, body.versus #btn-end { display: none; }
@media (max-width: 420px) {
  .hud { gap: 6px; }
  .hud .icon-btn { width: 40px; }
  .stat { padding: 4px 8px; }
  .stat strong { font-size: 17px; }
}

.play { flex: 1; min-height: 0; display: flex; justify-content: center; align-items: center; gap: 8px; }
.side { flex: 0 0 clamp(52px, 14vw, 150px); width: clamp(52px, 14vw, 150px); min-width: 0; align-self: stretch; display: flex; flex-direction: column; gap: 8px; min-height: 0; overflow: hidden; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 6px; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.panel-label { font-size: 10px; text-transform: uppercase; letter-spacing: .1em; color: var(--muted); max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.panel canvas { display: block; max-width: 100%; }
.panel { min-width: 0; overflow: hidden; }
.opps { display: flex; flex-direction: column; gap: 8px; min-height: 0; min-width: 0; }
.opps:empty { display: none; }
.opp { flex: 0 1 auto; }
.opp.dead { opacity: .6; }
.opp-stats { font-size: 11px; color: var(--muted); }

/* Portrait phones: Hold (and the opponent) and Next move into a strip above the board, so the board
   gets the full width instead of losing ~30% of it to the side columns. Toggled by layout() in app.js. */
.play.stacked {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-areas: "left right" "board board";
  align-items: stretch;
}
.play.stacked .side { width: auto; flex: none; flex-direction: row; align-self: stretch; overflow: visible; }
.play.stacked .side.left { grid-area: left; }
.play.stacked .opps { flex-direction: row; }
.play.stacked .opp { padding: 4px; gap: 2px; }
.play.stacked .opp .panel-label { max-width: 52px; }
.play.stacked .opps[data-count="3"] .opp .panel-label { max-width: 38px; }
.play.stacked .side.right { grid-area: right; }
.play.stacked .side.right .panel { flex: 1; }
.play.stacked .panel canvas { margin: auto 0; }
.play.stacked .board-col { grid-area: board; justify-self: center; align-self: center; }

.board-col { position: relative; display: flex; align-items: flex-start; gap: 4px; flex: 0 0 auto; min-height: 0; }
.board-wrap { position: relative; border-radius: 8px; overflow: hidden; box-shadow: 0 0 0 2px #1e2a52, 0 20px 50px rgba(0,0,0,.45); touch-action: none; user-select: none; -webkit-user-select: none; }
.board-wrap canvas { display: block; }
.garbage-meter { width: 6px; align-self: stretch; background: rgba(148,163,184,.12); border-radius: 3px; position: relative; overflow: hidden; }
#garbage-fill { position: absolute; left: 0; right: 0; bottom: 0; height: 0; background: var(--danger); transition: height .15s; box-shadow: 0 0 10px var(--danger); }
body:not(.versus) .garbage-meter { visibility: hidden; }
.spectate { position: absolute; left: 8px; right: 8px; top: 38%; padding: 10px 12px; border-radius: 10px; background: rgba(11,16,32,.85); border: 1px solid var(--line); text-align: center; font-weight: 700; pointer-events: none; }
.standings { list-style: none; margin: 8px 0; padding: 0; display: grid; gap: 4px; }
.standings li { display: grid; grid-template-columns: 3em 1fr auto; gap: 8px; align-items: center; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 6px 10px; text-align: left; }
.standings li.me { border-color: var(--accent); }
.standings li strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.popup {
  position: absolute; left: 0; right: 0; top: 30%;
  text-align: center; font-weight: 900; font-size: clamp(16px, 5vw, 26px);
  color: white; text-shadow: 0 2px 12px rgba(0,0,0,.8), 0 0 18px rgba(192,132,252,.7);
  pointer-events: none; opacity: 0; line-height: 1.2;
}
.popup.show { animation: pop 1.1s ease-out forwards; }
@keyframes pop { 0% { opacity: 0; transform: scale(.7); } 15% { opacity: 1; transform: scale(1.08); } 70% { opacity: 1; transform: translateY(-10px); } 100% { opacity: 0; transform: translateY(-24px); } }
.board-wrap.shake { animation: shake .25s; }
@keyframes shake { 25% { transform: translateY(4px); } 50% { transform: translateY(-2px); } 75% { transform: translateY(1px); } }

.controls {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  grid-template-areas: "hold hard hard ccw cw" "left soft right ccw cw";
  gap: 8px;
  flex: 0 0 auto;
}
.controls.hidden-controls { display: none; }
.ctl {
  appearance: none; border: 1px solid var(--line); background: var(--panel);
  border-radius: 14px; min-height: 54px; font-size: 22px; font-weight: 800; color: var(--text);
  touch-action: none; user-select: none; -webkit-user-select: none; cursor: pointer;
}
.ctl.pressed { background: #26325e; transform: scale(.96); }
.ctl[data-action=hold] { grid-area: hold; font-size: 15px; }
.ctl[data-action=hard] { grid-area: hard; }
.ctl[data-action=ccw] { grid-area: ccw; font-size: 28px; }
.ctl[data-action=cw] { grid-area: cw; font-size: 28px; background: #1d2750; }
.ctl[data-action=left] { grid-area: left; }
.ctl[data-action=soft] { grid-area: soft; }
.ctl[data-action=right] { grid-area: right; }

.overlay { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; background: rgba(5, 8, 20, .72); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); padding: 16px; z-index: 10; }
.overlay-card { width: 100%; max-width: 360px; background: var(--panel); border: 1px solid var(--line); border-radius: 18px; padding: 20px; display: flex; flex-direction: column; gap: 14px; text-align: center; }
.overlay-card h2 { font-size: 28px; margin: 0; }
.overlay-card h2.win { color: var(--good); }
.overlay-card h2.lose { color: var(--danger); }
.overlay-actions { display: flex; flex-direction: column; gap: 10px; }
.result-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.result-grid div { background: var(--bg-2); border-radius: 10px; padding: 8px; display: flex; flex-direction: column; }
.result-grid span { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; }
.result-grid strong { font-size: 20px; font-variant-numeric: tabular-nums; }
.new-best { color: #facc15; font-weight: 800; }
.muted { color: var(--muted); }

.countdown { position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; font-size: 120px; font-weight: 900; color: white; text-shadow: 0 6px 30px rgba(247,115,75,.6); pointer-events: none; z-index: 11; }
.countdown.tick { animation: tick .9s ease-out; }
@keyframes tick { 0% { transform: scale(1.6); opacity: 0; } 20% { transform: scale(1); opacity: 1; } 100% { opacity: .2; } }

.toast { position: absolute; left: 50%; bottom: calc(var(--safe-b) + 24px); transform: translate(-50%, 20px); background: #1e293b; border: 1px solid var(--line); padding: 10px 16px; border-radius: 12px; opacity: 0; transition: opacity .2s, transform .2s; pointer-events: none; z-index: 20; max-width: calc(100% - 32px); text-align: center; font-size: 14px; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (hover: hover) and (pointer: fine) {
  .controls:not(.force) { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition: none !important; }
}

/* iOS app: Game Center buttons on the menu */
.gc-row { display: flex; gap: 8px; width: 100%; }
.gc-row .btn { flex: 1; }
