:root {
  --bg: #0b1a2b;
  --glass: rgba(14, 24, 38, 0.62);
  --glass-strong: rgba(14, 24, 38, 0.82);
  --line: rgba(255, 255, 255, 0.12);
  --text: #f2f5f9;
  --dim: #a9b6c6;
  --accent: #e9c440;
  --danger: #ff7a66;
  --radius: 16px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --res-L: #3f9a4b; --res-B: #c8673f; --res-W: #a9d977; --res-G: #ecc94b; --res-O: #9aa5b8;
}

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

html, body {
  margin: 0; height: 100%; overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg); color: var(--text);
  font: 15px/1.35 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
}

[hidden] { display: none !important; }

#scene {
  position: fixed; inset: 0; width: 100%; height: 100%; display: block;
  touch-action: none; outline: none;
}

.panel {
  background: var(--glass);
  -webkit-backdrop-filter: blur(18px) saturate(140%);
  backdrop-filter: blur(18px) saturate(140%);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
}

/* ── buttons & inputs ─────────────────────────────────────────── */

.btn {
  min-height: 48px; padding: 0 18px; border-radius: 14px;
  border: 1px solid var(--line); background: rgba(255, 255, 255, 0.08);
  color: var(--text); font: inherit; font-weight: 600; letter-spacing: 0.01em;
  touch-action: manipulation;
}
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); color: #1b1503; border-color: transparent; }
.btn.danger { background: rgba(255, 122, 102, 0.18); color: #ffd2ca; }
.btn.ghost { background: transparent; color: var(--dim); }
.btn:disabled { opacity: 0.38; }
.btn.small { min-height: 44px; padding: 0 12px; font-size: 14px; }

input[type="text"] {
  width: 100%; min-height: 48px; padding: 0 14px; border-radius: 14px;
  border: 1px solid var(--line); background: rgba(0, 0, 0, 0.28); color: var(--text);
  font: inherit; font-size: 17px; /* 16px+ stops iOS zooming on focus */
  -webkit-user-select: text; user-select: text; outline: none;
}
input[type="text"]:focus { border-color: var(--accent); }

/* ── lobby ────────────────────────────────────────────────────── */

.screen {
  position: fixed; inset: 0; display: grid; place-items: center;
  padding: calc(16px + var(--safe-t)) calc(16px + var(--safe-r)) calc(16px + var(--safe-b)) calc(16px + var(--safe-l));
  overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: radial-gradient(120% 90% at 50% 0%, rgba(11, 26, 43, 0.25), rgba(11, 26, 43, 0.78));
}
.lobby-card { width: min(440px, 100%); padding: 22px; background: var(--glass-strong); }
.lobby-card h1 { margin: 0 0 18px; font-size: 28px; letter-spacing: -0.01em; display: flex; align-items: center; gap: 10px; }
.hex-mark {
  width: 26px; height: 30px; background: var(--accent); display: inline-block;
  clip-path: polygon(50% 0, 100% 25%, 100% 75%, 50% 100%, 0 75%, 0 25%);
}
.lobby-card h2 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--dim); }
.field { display: block; margin-bottom: 14px; }
.field > span { display: block; margin-bottom: 6px; font-size: 13px; color: var(--dim); }
.swatches { display: flex; flex-wrap: wrap; gap: 10px; }
.swatch {
  width: 44px; height: 44px; border-radius: 50%; border: 3px solid transparent; padding: 0;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.35);
}
.swatch.on { border-color: #fff; }
.split { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; margin-top: 6px; }
.choice { display: flex; flex-direction: column; gap: 10px; padding: 14px; border-radius: 14px; background: rgba(255, 255, 255, 0.04); border: 1px solid var(--line); }
.choice .btn { margin-top: auto; }
#code { text-align: center; text-transform: uppercase; letter-spacing: 0.3em; font-weight: 700; }
.segmented { display: grid; grid-template-columns: 1fr 1fr; border-radius: 12px; overflow: hidden; border: 1px solid var(--line); }
.segmented button { min-height: 44px; border: 0; background: transparent; color: var(--dim); font: inherit; font-weight: 600; }
.segmented button.on { background: rgba(255, 255, 255, 0.16); color: var(--text); }
.error { min-height: 1.3em; margin: 12px 0 0; color: var(--danger); font-size: 14px; }
.hint { margin: 8px 0 0; color: var(--dim); font-size: 13px; }
.room-code { font-size: 54px; font-weight: 800; letter-spacing: 0.22em; text-align: center; margin: 4px 0 14px; color: var(--accent); -webkit-user-select: text; user-select: text; }
.seat-list { list-style: none; margin: 0 0 8px; padding: 0; display: grid; gap: 8px; }
.seat-list li { display: flex; align-items: center; gap: 10px; min-height: 44px; padding: 0 12px; border-radius: 12px; background: rgba(255, 255, 255, 0.05); }
.seat-list .dot { width: 16px; height: 16px; border-radius: 50%; flex: none; }
.seat-list .empty { color: var(--dim); font-style: italic; }
#waiting .btn { width: 100%; margin-top: 12px; }

/* ── HUD ──────────────────────────────────────────────────────── */

#hud { position: fixed; inset: 0; pointer-events: none; display: flex; flex-direction: column; }
#hud > * { pointer-events: auto; }

.players {
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px;
  padding: calc(8px + var(--safe-t)) calc(8px + var(--safe-r)) 0 calc(8px + var(--safe-l));
  max-width: 760px; width: 100%; margin: 0 auto;
}
.pl {
  position: relative; padding: 7px 8px 6px; border-radius: 13px; min-width: 0;
  background: var(--glass); -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  border: 1px solid var(--line); border-top: 3px solid var(--c, #888);
  transition: box-shadow 0.25s, transform 0.25s;
}
.pl.turn { box-shadow: 0 0 0 2px var(--c), 0 6px 22px rgba(0, 0, 0, 0.4); transform: translateY(2px); }
.pl.off { opacity: 0.55; }
.pl .nm { display: flex; align-items: center; gap: 3px; font-size: 12px; font-weight: 600; white-space: nowrap; min-width: 0; }
.pl .nm .name { overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.pl .nm .me { color: var(--accent); }
.pl .row2 { display: flex; align-items: center; justify-content: space-between; gap: 4px; }
.pl .vp { font-size: 24px; font-weight: 800; line-height: 1.05; white-space: nowrap; }
.pl .st { display: grid; grid-template-columns: auto auto; gap: 1px 5px; font-size: 10.5px; line-height: 1.25; color: var(--dim); white-space: nowrap; }
.pl .badges { margin-left: auto; display: flex; gap: 1px; font-size: 12px; flex: none; }
.thinking { display: inline-flex; gap: 2px; margin-left: 2px; }
.thinking i { width: 4px; height: 4px; border-radius: 50%; background: currentColor; animation: blink 1s infinite; }
.thinking i:nth-child(2) { animation-delay: 0.15s; }
.thinking i:nth-child(3) { animation-delay: 0.3s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.2; } 40% { opacity: 1; } }

.effects { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; padding: 6px 10px 0; pointer-events: none !important; }
.effect { padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; background: rgba(233, 196, 64, 0.2); border: 1px solid rgba(233, 196, 64, 0.5); color: #ffe9a3; -webkit-backdrop-filter: blur(10px); backdrop-filter: blur(10px); }

.banner {
  margin: 10px auto 0; width: min(520px, calc(100% - 24px)); padding: 14px 16px;
  background: linear-gradient(135deg, rgba(74, 52, 8, 0.85), rgba(14, 24, 38, 0.85));
  border-color: rgba(233, 196, 64, 0.5);
  animation: drop 0.45s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}
.banner h3 { margin: 0 0 4px; font-size: 17px; color: var(--accent); }
.banner p { margin: 0; font-size: 14px; color: #f4ecd2; }
@keyframes drop { from { transform: translateY(-20px); opacity: 0; } }

.prompt {
  align-self: center; margin-top: 10px; padding: 8px 14px; border-radius: 999px;
  background: var(--glass-strong); border: 1px solid var(--line); font-weight: 600; font-size: 14px;
  -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px); pointer-events: none !important;
}

.log { margin-top: auto; padding: 0 12px 8px; display: flex; flex-direction: column; align-items: flex-start; gap: 4px; pointer-events: none !important; }
.log .ev {
  max-width: min(88%, 420px); padding: 5px 10px; border-radius: 10px; font-size: 13px;
  background: rgba(8, 14, 24, 0.66); -webkit-backdrop-filter: blur(8px); backdrop-filter: blur(8px);
  border-left: 3px solid var(--c, transparent);
  animation: evin 0.25s ease-out; transition: opacity 0.6s;
}
.log .ev.out { opacity: 0; }
@keyframes evin { from { transform: translateX(-12px); opacity: 0; } }

.bottom {
  padding: 0 calc(8px + var(--safe-r)) calc(8px + var(--safe-b)) calc(8px + var(--safe-l));
  max-width: 760px; width: 100%; margin: 0 auto;
}
.hand { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; margin-bottom: 8px; }
.card {
  position: relative; min-height: 50px; border-radius: 12px; padding: 4px 2px 3px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  background: var(--glass); border: 1px solid var(--line); border-bottom: 3px solid var(--rc, #666);
  -webkit-backdrop-filter: blur(14px); backdrop-filter: blur(14px);
  font: inherit; color: var(--text);
}
.card .e { font-size: 19px; line-height: 1; }
.card .n { font-size: 15px; font-weight: 800; line-height: 1.2; }
.card.zero { opacity: 0.45; }
.card.bump { animation: bump 0.5s; }
.card.dev { border-bottom-color: #9b5de5; touch-action: manipulation; }
@keyframes bump { 30% { transform: translateY(-7px) scale(1.12); } }

.thumbbar { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.thumb {
  min-height: 58px; border-radius: 16px; border: 1px solid var(--line);
  background: var(--glass-strong); -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  color: var(--text); font: inherit; font-size: 13px; font-weight: 700;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1px;
  touch-action: manipulation; transition: transform 0.12s, opacity 0.2s, background 0.2s;
}
.thumb .ico { font-size: 21px; line-height: 1.1; }
.thumb:disabled { opacity: 0.35; }
.thumb:not(:disabled):active { transform: scale(0.95); }
.thumb.go { background: var(--accent); color: #1b1503; border-color: transparent; animation: pulse 1.6s infinite; }
.thumb.active { background: rgba(255, 255, 255, 0.22); }
@keyframes pulse { 50% { box-shadow: 0 0 0 7px rgba(233, 196, 64, 0.18); } }

/* ── radial menu ──────────────────────────────────────────────── */

.radial { position: fixed; z-index: 30; width: 0; height: 0; }
.radial button {
  position: absolute; transform: translate(-50%, -50%) scale(0.6); opacity: 0;
  min-width: 64px; min-height: 64px; padding: 6px 10px; border-radius: 999px;
  border: 1px solid var(--line); background: var(--glass-strong); color: var(--text);
  -webkit-backdrop-filter: blur(16px); backdrop-filter: blur(16px);
  font: inherit; font-size: 12px; font-weight: 700; white-space: nowrap;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  transition: transform 0.18s cubic-bezier(0.2, 0.9, 0.3, 1.3), opacity 0.18s;
}
.radial.open button { transform: translate(-50%, -50%) scale(1); opacity: 1; }
.radial button.yes { background: var(--accent); color: #1b1503; border-color: transparent; }
.radial button .cost { font-size: 13px; font-weight: 500; letter-spacing: 1px; }

/* ── sheets ───────────────────────────────────────────────────── */

.backdrop { position: fixed; inset: 0; z-index: 40; background: rgba(4, 8, 14, 0.35); }
.sheet {
  position: fixed; z-index: 41; left: 50%; bottom: 0; transform: translateX(-50%);
  width: min(560px, 100%); max-height: 78vh; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 16px calc(16px + var(--safe-r)) calc(16px + var(--safe-b)) calc(16px + var(--safe-l));
  border-radius: 22px 22px 0 0; border-bottom: 0; background: var(--glass-strong);
  animation: up 0.28s cubic-bezier(0.2, 0.9, 0.3, 1);
}
@keyframes up { from { transform: translate(-50%, 40%); opacity: 0; } }
.sheet h2 { margin: 0 0 4px; font-size: 19px; }
.sheet .sub { margin: 0 0 14px; color: var(--dim); font-size: 14px; }
.sheet .row { display: flex; gap: 8px; margin-top: 14px; }
.sheet .row .btn { flex: 1; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; margin: 10px 0 14px; border-radius: 12px; overflow: hidden; border: 1px solid var(--line); }
.tabs button { min-height: 44px; border: 0; background: transparent; color: var(--dim); font: inherit; font-weight: 700; }
.tabs button.on { background: rgba(255, 255, 255, 0.16); color: var(--text); }

.opts { display: grid; gap: 8px; }
.opt {
  display: flex; align-items: center; gap: 12px; min-height: 56px; padding: 8px 14px; width: 100%;
  border-radius: 14px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.06);
  color: var(--text); font: inherit; text-align: left; touch-action: manipulation;
}
.opt:disabled { opacity: 0.38; }
.opt .ico { font-size: 24px; width: 30px; text-align: center; }
.opt .t { flex: 1; font-weight: 700; }
.opt .t small { display: block; font-weight: 500; color: var(--dim); font-size: 12px; }
.opt .cost { font-size: 15px; letter-spacing: 1px; white-space: nowrap; }

.pick { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.pick .lbl { grid-column: 1 / -1; font-size: 12px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--dim); margin-top: 6px; }
.res {
  position: relative; min-height: 64px; border-radius: 12px; border: 1px solid var(--line); border-bottom: 3px solid var(--rc);
  background: rgba(255, 255, 255, 0.06); color: var(--text); font: inherit;
  display: flex; flex-direction: column; align-items: center; justify-content: center; padding: 4px 0;
  touch-action: manipulation;
}
.res .e { font-size: 22px; line-height: 1.1; }
.res .n { font-size: 13px; font-weight: 700; }
.res .have { font-size: 11px; color: var(--dim); }
.res.on { background: rgba(255, 255, 255, 0.22); box-shadow: inset 0 0 0 2px #fff; }
.res:disabled { opacity: 0.3; }
.stepper { display: flex; flex-direction: column; align-items: stretch; gap: 4px; }
.stepper .pm { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; }
.stepper .pm button { min-height: 44px; border-radius: 10px; border: 1px solid var(--line); background: rgba(255, 255, 255, 0.08); color: var(--text); font-size: 20px; font-weight: 700; touch-action: manipulation; }
.stepper .pm button:disabled { opacity: 0.3; }

.replies { display: grid; gap: 8px; margin-top: 12px; }
.reply { display: flex; align-items: center; gap: 10px; min-height: 48px; padding: 4px 6px 4px 12px; border-radius: 12px; background: rgba(255, 255, 255, 0.05); border-left: 4px solid var(--c); }
.reply .who { flex: 1; font-weight: 600; }
.reply .what { color: var(--dim); font-size: 13px; }
.offer { display: flex; align-items: center; justify-content: center; gap: 14px; font-size: 24px; padding: 12px; border-radius: 14px; background: rgba(0, 0, 0, 0.25); }
.offer .arrow { color: var(--dim); font-size: 18px; }
.offer small { display: block; font-size: 11px; color: var(--dim); text-align: center; letter-spacing: 0.06em; text-transform: uppercase; }

.scores { display: grid; gap: 8px; margin: 12px 0; }
.score { display: flex; align-items: center; gap: 10px; min-height: 46px; padding: 0 12px; border-radius: 12px; background: rgba(255, 255, 255, 0.05); border-left: 4px solid var(--c); }
.score b { margin-left: auto; font-size: 20px; }
.score.win { background: rgba(233, 196, 64, 0.18); }

.net {
  position: fixed; z-index: 60; top: calc(10px + var(--safe-t)); left: 50%; transform: translateX(-50%);
  padding: 8px 16px; border-radius: 999px; background: rgba(160, 40, 20, 0.92); font-weight: 700; font-size: 13px;
}
.debug {
  position: fixed; z-index: 70; right: calc(6px + var(--safe-r)); bottom: calc(150px + var(--safe-b)); margin: 0; padding: 6px 8px;
  font: 11px/1.3 ui-monospace, Menlo, Consolas, monospace; color: #9f9; background: rgba(0, 0, 0, 0.6); border-radius: 8px; pointer-events: none;
}
.toast-err {
  position: fixed; z-index: 65; left: 50%; bottom: calc(150px + var(--safe-b)); transform: translateX(-50%);
  padding: 10px 16px; border-radius: 12px; background: rgba(160, 40, 20, 0.92); font-size: 14px; font-weight: 600;
  animation: evin 0.2s;
}

@media (min-width: 700px) {
  .pl .nm { font-size: 14px; }
  .pl .st { font-size: 12px; }
}
@media (max-height: 480px) and (orientation: landscape) {
  .players { grid-template-columns: repeat(4, minmax(0, 150px)); justify-content: center; }
  .card { min-height: 40px; }
  .thumb { min-height: 46px; flex-direction: row; gap: 6px; }
  .sheet { max-height: 92vh; }
}
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

/* settings */
.fab {
  position: fixed; z-index: 5; right: calc(8px + var(--safe-r)); top: calc(84px + var(--safe-t));
  width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line); font-size: 19px;
  background: var(--glass); -webkit-backdrop-filter: blur(12px); backdrop-filter: blur(12px); color: var(--text);
  touch-action: manipulation;
}
.setting { display: flex; align-items: center; gap: 12px; min-height: 52px; }
.setting span { flex: 0 0 110px; font-weight: 600; }
.setting input[type="range"] { flex: 1; height: 44px; accent-color: var(--accent); }
