:root {
  --bg: #0b0d16;
  --bg-2: #141827;
  --ink: #f3f1ea;
  --ink-dim: #8f93a8;
  --red: #e8362c;
  --yellow: #f9c82b;
  --blue: #2f7fe0;
  --green: #2fb35a;
  --card: rgba(255, 255, 255, 0.045);
  --card-2: rgba(255, 255, 255, 0.08);
  --border: rgba(255, 255, 255, 0.11);
  --radius: 18px;
}

/* touch-action: manipulation everywhere — no double-tap zoom (iOS ignores user-scalable=no);
   the stage canvas sets touch-action: none itself for its own orbit/pinch. */
* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; touch-action: manipulation; }

html, body {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  width: 100%;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: 'Fredoka', ui-rounded, 'Nunito', system-ui, sans-serif;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
  overflow-x: hidden;
  position: relative;
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse at 15% -10%, rgba(249, 200, 43, 0.13), transparent 55%),
    radial-gradient(ellipse at 100% 110%, rgba(232, 54, 44, 0.12), transparent 55%),
    radial-gradient(circle, rgba(255, 255, 255, 0.035) 1.5px, transparent 2px) 0 0 / 22px 22px;
  pointer-events: none;
  z-index: 0;
}

button { font-family: inherit; }

/* ---- wordmark: each letter is a brick ---- */
.wordmark {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 5px 16px;
}
.wordmark .word { display: flex; gap: 4px; }
.wordmark .l {
  position: relative;
  width: 40px;
  height: 48px;
  border-radius: 6px;
  display: grid;
  place-items: center;
  font-weight: 700;
  font-size: 28px;
  color: rgba(0, 0, 0, 0.72);
  box-shadow: inset 0 -5px 0 rgba(0, 0, 0, 0.22), inset 0 2px 0 rgba(255, 255, 255, 0.35);
}
.wordmark .l::before {
  content: '';
  position: absolute;
  top: -6px;
  left: 50%;
  width: 18px;
  height: 7px;
  margin-left: -9px;
  border-radius: 4px 4px 1px 1px;
  background: inherit;
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.35);
}
.wordmark .l:nth-child(4n+1) { background: var(--red); }
.wordmark .l:nth-child(4n+2) { background: var(--yellow); }
.wordmark .l:nth-child(4n+3) { background: var(--blue); color: rgba(255, 255, 255, 0.92); }
.wordmark .l:nth-child(4n+4) { background: var(--green); }
.wordmark.small .l { width: 22px; height: 26px; font-size: 15px; border-radius: 4px; box-shadow: inset 0 -3px 0 rgba(0,0,0,0.22); }
.wordmark.small .l::before { top: -4px; width: 10px; height: 4px; margin-left: -5px; }
.wordmark.small { gap: 3px 9px; }
.wordmark.small .word { gap: 2px; }

/* ---- shared bits ---- */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
}
.card h2 {
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--yellow);
  margin-bottom: 14px;
}
.hint { font-size: 14px; color: var(--ink-dim); line-height: 1.5; }
.error {
  display: none;
  margin-top: 10px;
  padding: 9px 12px;
  border-radius: 10px;
  background: rgba(232, 54, 44, 0.12);
  border: 1px solid rgba(232, 54, 44, 0.35);
  color: #ff9b94;
  font-size: 14px;
}

.input {
  width: 100%;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 13px 14px;
  color: var(--ink);
  font-family: inherit;
  font-size: 16px;
  outline: none;
}
.input:focus { border-color: var(--yellow); }

.btn {
  width: 100%;
  border: none;
  border-radius: 14px;
  padding: 14px 18px;
  font-size: 17px;
  font-weight: 600;
  cursor: pointer;
  color: #2a1d00;
  background: var(--yellow);
  box-shadow: inset 0 -4px 0 rgba(0, 0, 0, 0.2);
  transition: transform 0.08s, filter 0.15s;
}
.btn:hover { filter: brightness(1.06); }
.btn:active { transform: translateY(2px); box-shadow: inset 0 -2px 0 rgba(0, 0, 0, 0.2); }
.btn:disabled { opacity: 0.45; cursor: default; transform: none; }
.btn.red { background: var(--red); color: #fff; }
.btn.ghost { background: var(--card-2); color: var(--ink); box-shadow: none; border: 1px solid var(--border); }

.back { color: var(--ink-dim); text-decoration: none; font-size: 14px; }
.back:hover { color: var(--ink); }

/* ---- model picker ---- */
.models {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
  gap: 10px;
}
.model-card {
  background: rgba(255, 255, 255, 0.03);
  border: 2px solid transparent;
  border-radius: 14px;
  padding: 8px 8px 10px;
  cursor: pointer;
  color: var(--ink);
  text-align: left;
  transition: border-color 0.15s, background 0.15s;
}
@media (hover: hover) {
  .model-card:not(.static):hover { background: rgba(255, 255, 255, 0.06); }
}
.model-card.static { cursor: default; }
.model-card.selected { border-color: var(--yellow); background: rgba(249, 200, 43, 0.08); }
.model-card img { width: 100%; aspect-ratio: 1; display: block; }
.model-card .name { font-weight: 600; font-size: 15px; margin-top: 4px; }
.model-card .meta { font-size: 12px; color: var(--ink-dim); margin-top: 2px; display: flex; align-items: center; gap: 6px; }
.dots { display: inline-flex; gap: 2px; }
.dots i { width: 6px; height: 6px; border-radius: 2px; background: rgba(255, 255, 255, 0.15); }
.dots i.on { background: var(--yellow); }

/* =========================== landing =========================== */
.landing {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 620px;
  margin: 0 auto;
  padding: max(34px, env(safe-area-inset-top)) 16px max(40px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.landing .hero { text-align: center; margin-bottom: 6px; }
.landing .tagline { margin-top: 18px; color: var(--ink-dim); font-size: 15px; line-height: 1.5; }
.create-row { display: flex; flex-direction: column; gap: 10px; }
.join-row { display: flex; gap: 8px; }
.join-row .input { flex: 1; letter-spacing: 0.08em; }
.join-row .btn { width: auto; }
.rules p { font-size: 15px; line-height: 1.55; color: #cfd0d8; }
.rules p + p { margin-top: 8px; }
.footer { text-align: center; font-size: 12px; color: var(--ink-dim); }

/* =========================== room =========================== */
.room {
  position: relative;
  z-index: 1;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: max(10px, env(safe-area-inset-top)) 16px 10px;
}
.topbar .spacer { flex: 1; }
.code-badge {
  font-weight: 600;
  letter-spacing: 0.12em;
  font-size: 15px;
  padding: 6px 10px;
  border-radius: 10px;
  background: var(--card-2);
}
.icon-btn {
  border: 1px solid var(--border);
  background: var(--card-2);
  color: var(--ink);
  border-radius: 10px;
  min-width: 38px;
  height: 36px;
  padding: 0 10px;
  font-size: 14px;
  cursor: pointer;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.13); }

.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 360px;
  gap: 16px;
  padding: 0 16px 16px;
  min-height: 0;
}
.stage {
  position: relative;
  border-radius: var(--radius);
  background:
    radial-gradient(ellipse at 50% 65%, rgba(255, 255, 255, 0.07), transparent 65%),
    var(--bg-2);
  border: 1px solid var(--border);
  overflow: hidden;
  min-height: 420px;
}
.stage canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; cursor: grab; }
.stage canvas:active { cursor: grabbing; }
.stage-top {
  position: absolute;
  top: 12px;
  left: 12px;
  right: 12px;
  display: flex;
  align-items: flex-start;
  gap: 10px;
  pointer-events: none;
}
.pill {
  background: rgba(10, 12, 20, 0.72);
  border: 1px solid var(--border);
  backdrop-filter: blur(6px);
  border-radius: 12px;
  padding: 8px 12px;
  font-size: 14px;
}
.pill b { font-weight: 600; }
.pill .sub { display: block; color: var(--ink-dim); font-size: 12px; margin-top: 1px; }
.progress {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 6px;
  background: rgba(255, 255, 255, 0.06);
}
.progress i {
  display: block;
  height: 100%;
  width: 0;
  background: linear-gradient(90deg, var(--yellow), var(--red));
  transition: width 0.4s;
}
.stage-controls {
  position: absolute;
  right: 12px;
  bottom: 18px;
  display: flex;
  gap: 6px;
}
.stage-controls .icon-btn { background: rgba(10, 12, 20, 0.72); backdrop-filter: blur(6px); width: 42px; height: 42px; font-size: 18px; }
.toast {
  position: absolute;
  left: 50%;
  top: 64px;
  transform: translate(-50%, -8px);
  opacity: 0;
  pointer-events: none;
  background: rgba(10, 12, 20, 0.85);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 16px;
  font-size: 15px;
  white-space: nowrap;
  max-width: calc(100% - 24px);
  overflow: hidden;
  text-overflow: ellipsis;
  transition: opacity 0.2s, transform 0.2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.big { font-size: 20px; font-weight: 600; color: var(--yellow); }
.confetti { position: absolute; inset: 0; pointer-events: none; }

.side { display: flex; flex-direction: column; gap: 12px; min-width: 0; }

.tray { display: grid; grid-template-columns: repeat(auto-fill, minmax(76px, 1fr)); gap: 8px; }
.piece {
  position: relative;
  border: 2px solid transparent;
  background: rgba(255, 255, 255, 0.04);
  border-radius: 12px;
  padding: 4px 4px 6px;
  cursor: pointer;
  color: var(--ink);
}
.piece:hover { background: rgba(255, 255, 255, 0.08); }
.piece.selected { border-color: var(--yellow); background: rgba(249, 200, 43, 0.1); }
.piece img { width: 100%; aspect-ratio: 4 / 3; display: block; object-fit: contain; }
.piece .size { font-size: 12px; color: var(--ink-dim); }
.piece .count {
  position: absolute;
  top: 4px;
  right: 6px;
  font-weight: 600;
  font-size: 14px;
}

.parts { display: flex; flex-direction: column; gap: 6px; }
.part { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.part img { width: 46px; height: 34px; object-fit: contain; flex-shrink: 0; }
.part .label { flex: 1; color: #d9dae2; }
.part.done { opacity: 0.38; }
.part.done .label { text-decoration: line-through; }
.part .holders { display: flex; gap: 3px; }
.part .holders i { width: 10px; height: 10px; border-radius: 3px; }

.players { display: flex; flex-direction: column; gap: 6px; }
.player {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid transparent;
  font-size: 15px;
  transition: border-color 0.3s, background 0.3s;
}
.player.me { border-color: rgba(255, 255, 255, 0.18); }
.player.ping { border-color: var(--yellow); background: rgba(249, 200, 43, 0.12); }
.player .swatch {
  width: 18px;
  height: 18px;
  border-radius: 5px;
  box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.25);
  flex-shrink: 0;
}
.player .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player .tag { font-size: 12px; color: var(--ink-dim); }
.player.away .name { opacity: 0.5; }
.bar { height: 6px; border-radius: 3px; background: rgba(255, 255, 255, 0.07); overflow: hidden; margin-top: 4px; }
.bar i { display: block; height: 100%; }
.result-row { font-size: 14px; }
.result-row + .result-row { margin-top: 10px; }
.result-row .top { display: flex; justify-content: space-between; gap: 8px; }
.big-number { font-size: 34px; font-weight: 700; color: var(--yellow); }

.log { display: flex; flex-direction: column; gap: 4px; max-height: 160px; overflow-y: auto; }
.log div { font-size: 13px; color: var(--ink-dim); }

.hidden { display: none !important; }

/* Desktop: the room fits the viewport — the stage takes the full height, the side column scrolls. */
@media (min-width: 901px) {
  .room { height: 100vh; height: 100dvh; min-height: 0; overflow: hidden; }
  .layout { grid-template-rows: minmax(0, 1fr); }
  .stage { min-height: 0; }
  .side { overflow-y: auto; min-height: 0; overscroll-behavior: contain; padding-right: 2px; }
  /* Only the lobby's set list scrolls: the lobby card shrinks to fit (its header, hint and start
     button stay put), every other card — players included — keeps its full height. */
  .side > .card { flex-shrink: 0; }
  .side > #lobby-card { flex-shrink: 1; min-height: 0; display: flex; flex-direction: column; }
  #lobby-card > * { flex-shrink: 0; }
  #lobby-card > #lobby-models {
    flex: 0 1 auto;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    margin-bottom: 12px;
    padding-right: 4px;
  }
}

@media (max-width: 900px) {
  .layout { grid-template-columns: minmax(0, 1fr); }
  .stage { min-height: 0; height: 58vh; height: 58dvh; }
}
@media (max-width: 560px) {
  .topbar .wordmark { display: none; }
  .topbar { gap: 8px; }
}
