:root {
  --bg: #0a0e1f;
  --panel: rgba(255, 255, 255, .05);
  --panel-2: rgba(255, 255, 255, .08);
  --line: rgba(255, 255, 255, .09);
  --text: #eef1f8;
  --muted: #8d97b0;
  --accent: #7c6cff;
  --accent-2: #4f8bff;
  --ok: #4ade80;
  --radius: 20px;
  --field: rgba(0, 0, 0, .25);
  --field-2: rgba(0, 0, 0, .35);
  --sunken: rgba(0, 0, 0, .2);
  --hover: rgba(255, 255, 255, .13);
  --track: rgba(255, 255, 255, .15);
  --surface: #141a33;
  --topbar: rgba(10, 14, 31, .6);
  --tile-a: rgba(255, 255, 255, .07);
  --tile-b: rgba(255, 255, 255, .03);
  --glow-a: rgba(124, 108, 255, .18);
  --glow-b: rgba(79, 139, 255, .14);
  --scrim: rgba(5, 8, 20, .65);
  color-scheme: dark;
}
/* Light theme: chosen in settings, or following the device when set to Auto. */
:root[data-theme="light"] {
  --bg: #eef1f8;
  --panel: rgba(255, 255, 255, .72);
  --panel-2: rgba(15, 23, 42, .06);
  --line: rgba(15, 23, 42, .12);
  --text: #111827;
  --muted: #586174;
  --field: rgba(255, 255, 255, .9);
  --field-2: #fff;
  --sunken: rgba(15, 23, 42, .05);
  --hover: rgba(15, 23, 42, .1);
  --track: rgba(15, 23, 42, .2);
  --surface: #fff;
  --topbar: rgba(255, 255, 255, .75);
  --tile-a: rgba(255, 255, 255, .92);
  --tile-b: rgba(255, 255, 255, .7);
  --glow-a: rgba(124, 108, 255, .16);
  --glow-b: rgba(79, 139, 255, .12);
  --scrim: rgba(15, 23, 42, .45);
  color-scheme: light;
}
@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #eef1f8;
    --panel: rgba(255, 255, 255, .72);
    --panel-2: rgba(15, 23, 42, .06);
    --line: rgba(15, 23, 42, .12);
    --text: #111827;
    --muted: #586174;
    --field: rgba(255, 255, 255, .9);
    --field-2: #fff;
    --sunken: rgba(15, 23, 42, .05);
    --hover: rgba(15, 23, 42, .1);
    --track: rgba(15, 23, 42, .2);
    --surface: #fff;
    --topbar: rgba(255, 255, 255, .75);
    --tile-a: rgba(255, 255, 255, .92);
    --tile-b: rgba(255, 255, 255, .7);
    --glow-a: rgba(124, 108, 255, .16);
    --glow-b: rgba(79, 139, 255, .12);
    --scrim: rgba(15, 23, 42, .45);
    color-scheme: light;
}
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0;
  color: var(--text);
  background:
    radial-gradient(60vw 50vh at 0% 0%, var(--glow-a), transparent 70%),
    radial-gradient(50vw 50vh at 100% 100%, var(--glow-b), transparent 70%),
    var(--bg);
  font: 15px/1.45 "Plus Jakarta Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, p { margin: 0; }
svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

button, input { font: inherit; color: inherit; }
input {
  width: 100%; min-width: 0; padding: 11px 14px;
  background: var(--field); border: 1px solid var(--line); border-radius: 12px;
  transition: border-color .15s, background .15s;
}
input:focus { outline: none; border-color: var(--accent); background: var(--field-2); }
input::placeholder { color: var(--muted); }
button { cursor: pointer; border: 0; background: none; }
button:disabled { cursor: not-allowed; opacity: .45; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 18px; border-radius: 12px; font-weight: 600;
  background: var(--panel-2); border: 1px solid var(--line);
  transition: background .15s, transform .1s, box-shadow .2s;
}
.btn:hover:not(:disabled) { background: var(--hover); }
.btn:active:not(:disabled) { transform: scale(.97); }
.btn.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); border-color: transparent;
  box-shadow: 0 8px 24px -8px rgba(124, 108, 255, .7);
}
.btn.primary:hover:not(:disabled) { filter: brightness(1.1); background: linear-gradient(135deg, var(--accent), var(--accent-2)); }
.btn.ghost { background: transparent; }
.btn.sm { padding: 7px 12px; font-size: .85rem; border-radius: 10px; }
.btn.on { background: rgba(74, 222, 128, .16); border-color: rgba(74, 222, 128, .4); color: var(--ok); }
.icon-btn {
  width: 40px; height: 40px; display: inline-grid; place-items: center; border-radius: 12px;
  color: var(--muted); transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.icon-btn.sm { width: 30px; height: 30px; border-radius: 9px; }
.icon-btn.off { color: #f87171; }

/* ---------- animated background ---------- */
.bg-fx { position: fixed; inset: 0; z-index: -1; overflow: hidden; pointer-events: none; transition: opacity .6s; }
.bg-fx::before, .bg-fx::after {
  content: ''; position: absolute; width: 55vmax; height: 55vmax; border-radius: 50%; filter: blur(90px); opacity: .2;
  animation: aurora 28s ease-in-out infinite alternate;
}
.bg-fx::before { background: #7c6cff; left: -15vmax; top: -20vmax; }
.bg-fx::after { background: #22c55e; right: -20vmax; bottom: -25vmax; opacity: .12; animation-duration: 34s; animation-direction: alternate-reverse; }
@keyframes aurora { to { transform: translate(18vw, 12vh) scale(1.2); } }
.fx {
  position: absolute; left: var(--x); top: var(--y); width: var(--s); height: var(--s); opacity: .14;
  animation: drift var(--d) ease-in-out var(--delay) infinite alternate; will-change: transform;
}
@keyframes drift { to { transform: translate(var(--dx), var(--dy)) rotate(var(--r)); } }
.fx-die {
  --pip: calc(var(--s) * .1); border-radius: 24%;
  background:
    radial-gradient(circle var(--pip) at 28% 28%, #1d2333 96%, transparent), radial-gradient(circle var(--pip) at 72% 28%, #1d2333 96%, transparent),
    radial-gradient(circle var(--pip) at 50% 50%, #1d2333 96%, transparent), radial-gradient(circle var(--pip) at 28% 72%, #1d2333 96%, transparent),
    radial-gradient(circle var(--pip) at 72% 72%, #1d2333 96%, transparent), #eef1f8;
}
.fx-piece {
  border-radius: 50%; opacity: .2;
  background: radial-gradient(circle at 35% 30%, rgba(255, 255, 255, .75), transparent 45%), var(--c);
  box-shadow: inset 0 0 0 calc(var(--s) * .14) var(--c), inset 0 0 0 calc(var(--s) * .2) rgba(255, 255, 255, .8);
}
.fx-star { background: var(--c); opacity: .18; clip-path: polygon(50% 0, 61% 35%, 98% 35%, 68% 57%, 79% 91%, 50% 70%, 21% 91%, 32% 57%, 2% 35%, 39% 35%); }
body[data-screen="game"] .bg-fx { opacity: .45; }
.tile, .panel, .lobby-card, .pcard, .name-field { backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); }

/* ---------- shell ---------- */
.app { height: 100dvh; display: flex; flex-direction: column; }
.topbar {
  flex: none; height: 64px; padding: 0 20px; display: flex; align-items: center; gap: 16px;
  border-bottom: 1px solid var(--line); background: var(--topbar); backdrop-filter: blur(12px);
}
.brand { display: grid; grid-template-columns: 10px 10px auto; grid-template-rows: 10px 10px; gap: 2px; align-items: center; color: inherit; text-decoration: none; }
.brand i { width: 10px; height: 10px; border-radius: 3px; }
.brand i:nth-child(1) { background: #ef4444; } .brand i:nth-child(2) { background: #22c55e; grid-row: 1; grid-column: 2; }
.brand i:nth-child(3) { background: #3b82f6; grid-row: 2; grid-column: 1; } .brand i:nth-child(4) { background: #facc15; grid-row: 2; grid-column: 2; }
.brand span { grid-row: 1 / 3; grid-column: 3; margin-left: 10px; font-weight: 800; font-size: 1.2rem; letter-spacing: -.01em; }
.status { flex: 1; text-align: center; font-weight: 600; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.status b { color: var(--text); }
.actions { display: flex; align-items: center; gap: 6px; }
.chip {
  display: inline-flex; align-items: center; gap: 8px; padding: 8px 12px; border-radius: 999px;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--muted); font-size: .85rem; font-weight: 600;
}
.chip code { color: var(--text); letter-spacing: .14em; font: 700 .95rem/1 ui-monospace, Menlo, monospace; }
.chip:hover { color: var(--text); }
.chip svg { width: 15px; height: 15px; }

.layout { flex: 1; min-height: 0; display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 18px; padding: 18px; }
.play { min-width: 0; min-height: 0; display: flex; overflow: auto; }
.screen { flex: 1; min-width: 0; }

/* ---------- home ---------- */
.home { max-width: 1120px; margin: auto; padding: 12px 0 24px; display: flex; flex-direction: column; gap: 32px; }
.hero { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 14px; }
.hero h1 { font-size: clamp(2.2rem, 5vw, 3.6rem); font-weight: 800; letter-spacing: -.035em; line-height: 1.05; }
.grad { background: linear-gradient(90deg, #ef4444, #facc15 35%, #22c55e 65%, #3b82f6); -webkit-background-clip: text; background-clip: text; color: transparent; }
.hero p { color: var(--muted); font-size: 1.05rem; }
.name-field {
  margin-top: 6px; display: flex; align-items: center; gap: 4px; padding: 5px 5px 5px 16px; width: min(380px, 100%);
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
}
.name-field span { color: var(--muted); font-size: .85rem; font-weight: 600; white-space: nowrap; }
.name-field input { border: 0; background: transparent; font-weight: 700; }
.seg span.piece-opt { display: flex; align-items: center; gap: 6px; padding: 4px 10px 4px 6px; }
.settings { display: flex; flex-direction: column; gap: 8px; text-align: left; }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 10px; color: var(--muted); font-size: .85rem; font-weight: 600; }
.house { border: 1px solid var(--line); border-radius: 12px; padding: 0 12px; background: var(--sunken); }
.house summary { cursor: pointer; padding: 9px 0; color: var(--muted); font-size: .85rem; font-weight: 700; }
.house[open] { padding-bottom: 8px; }
.toggle { display: flex; align-items: center; gap: 10px; padding: 6px 0; cursor: pointer; font-size: .9rem; font-weight: 600; position: relative; }
.toggle input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.toggle small { display: block; color: var(--muted); font-weight: 500; font-size: .75rem; }
.switch { flex: none; width: 34px; height: 20px; border-radius: 99px; background: var(--track); position: relative; transition: background .2s; }
.switch::after { content: ''; position: absolute; top: 3px; left: 3px; width: 14px; height: 14px; border-radius: 50%; background: #fff; transition: transform .2s; }
.toggle input:checked + .switch { background: var(--accent); }
.toggle input:checked + .switch::after { transform: translateX(14px); }
.toggle input:focus-visible + .switch { outline: 2px solid var(--accent); outline-offset: 2px; }
.toggle input:disabled ~ * { opacity: .6; cursor: not-allowed; }
.stats { color: var(--muted); font-size: .85rem; font-weight: 600; min-height: 1.2em; }
.watchers { margin: 0; color: var(--muted); font-size: .8rem; }
.members li { flex-wrap: wrap; }
.members li .icon-btn.sm { width: 28px; height: 28px; }
.members .mod { display: flex; gap: 6px; flex-basis: 100%; padding-left: 42px; }
.chip-btn { padding: 4px 10px; border-radius: 999px; font-size: .78rem; font-weight: 700; background: var(--panel-2); border: 1px solid var(--line); }
.chip-btn:hover { background: var(--hover); }
.chip-btn.danger { color: #fca5a5; border-color: rgba(248, 113, 113, .35); }
#chat-input:disabled { opacity: .6; }
.quick .react { font-size: 1.05rem; padding: 3px 8px; }
.fly {
  position: fixed; z-index: 30; pointer-events: none; font-size: 2.4rem; transform: translate(-50%, -50%);
  animation: fly 2.2s cubic-bezier(.2, .7, .3, 1) forwards; text-shadow: 0 6px 18px rgba(0, 0, 0, .4);
}
@keyframes fly {
  15% { transform: translate(-50%, -80%) scale(1.5); opacity: 1; }
  100% { transform: translate(calc(-50% + var(--dx)), -260px) scale(1.1); opacity: 0; }
}
.mode-help { margin: -4px 0 0; color: var(--muted); font-size: .8rem; text-align: left; }
.seg input:disabled { cursor: not-allowed; }
.seg input:disabled + span { opacity: .6; }
.piece-row { display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: .85rem; font-weight: 600; }
.tiles { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 18px; }
.tile {
  position: relative; display: flex; flex-direction: column; gap: 12px; padding: 22px;
  background: linear-gradient(180deg, var(--tile-a), var(--tile-b));
  border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden;
  transition: transform .2s, border-color .2s;
}
.tile::before { content: ''; position: absolute; inset: 0 0 auto; height: 3px; background: var(--c); }
.tile:hover { transform: translateY(-2px); border-color: color-mix(in srgb, var(--text) 18%, transparent); }
.tile-ico { width: 46px; height: 46px; display: grid; place-items: center; font-size: 1.4rem; border-radius: 14px; background: color-mix(in srgb, var(--c) 18%, transparent); }
.tile h2 { font-size: 1.15rem; font-weight: 700; display: flex; align-items: center; justify-content: space-between; }
.tile p { color: var(--muted); font-size: .92rem; }
.tile > .btn:last-child { margin-top: auto; }
.join { display: flex; gap: 8px; }
.join input { text-transform: uppercase; letter-spacing: .12em; font-weight: 700; }
.join input::placeholder { letter-spacing: normal; text-transform: none; font-weight: 400; }
.rooms { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; min-height: 120px; max-height: 210px; overflow: auto; }
.rooms li { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border-radius: 12px; background: var(--sunken); }
.rooms li.pick { cursor: pointer; transition: background .15s; }
.rooms li.pick:hover { background: var(--hover); }
.rooms li > span:nth-child(2) { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rooms .empty { color: var(--muted); justify-content: center; flex: 1; background: transparent; border: 1px dashed var(--line); }
.pill { font-size: .75rem; font-weight: 700; padding: 3px 9px; border-radius: 999px; background: var(--panel-2); color: var(--muted); }
.offline-seats { display: flex; flex-direction: column; gap: 8px; }
.seat-row { display: flex; align-items: center; gap: 10px; }
.seat-row > span:nth-child(2) { flex: 1; font-weight: 600; }
.dot { width: 12px; height: 12px; border-radius: 50%; flex: none; background: var(--c); box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 25%, transparent); }
.seg { display: inline-flex; padding: 3px; border-radius: 10px; background: var(--sunken); }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; cursor: pointer; }
.seg span { display: block; padding: 4px 10px; border-radius: 8px; font-size: .8rem; font-weight: 600; color: var(--muted); transition: .15s; }
.seg input:checked + span { background: var(--panel-2); color: var(--text); box-shadow: 0 1px 0 rgba(255, 255, 255, .08) inset; }
.seg input:focus-visible + span { outline: 2px solid var(--accent); }

/* ---------- lobby ---------- */
.lobby { display: grid; place-items: center; }
.lobby-card {
  width: min(520px, 100%); padding: 28px; display: flex; flex-direction: column; gap: 18px; text-align: center;
  background: var(--panel); border: 1px solid var(--line); border-radius: 24px;
}
.eyebrow { color: var(--muted); font-size: .85rem; font-weight: 600; text-transform: uppercase; letter-spacing: .08em; }
.code {
  align-self: center; display: inline-flex; align-items: center; gap: 12px; padding: 10px 18px; border-radius: 16px;
  font: 800 2.4rem/1 ui-monospace, Menlo, monospace; letter-spacing: .2em; color: var(--text);
  background: var(--field); border: 1px dashed var(--line); transition: border-color .15s;
}
.code:hover { border-color: var(--accent); }
.code svg { width: 22px; height: 22px; color: var(--muted); }
.seat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.seat {
  display: flex; align-items: center; gap: 10px; padding: 12px; border-radius: 16px; text-align: left; min-width: 0;
  background: color-mix(in srgb, var(--c) 10%, var(--sunken)); border: 1px solid color-mix(in srgb, var(--c) 35%, transparent);
}
.seat.open { background: var(--sunken); border-style: dashed; border-color: var(--line); }
.seat .who { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.seat .who b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.seat .who small { color: var(--muted); font-size: .75rem; font-weight: 600; }
.ava {
  width: 38px; height: 38px; border-radius: 50%; flex: none; display: grid; place-items: center;
  background: var(--c); color: #fff; font-weight: 800; text-shadow: 0 1px 2px rgba(0, 0, 0, .3);
}
.seat.open .ava { background: transparent; border: 2px dashed color-mix(in srgb, var(--c) 60%, transparent); }
.note { color: var(--muted); min-height: 1.4em; }
.lobby-actions { display: flex; gap: 10px; }
.lobby-actions .btn { flex: 1; }

/* ---------- game ---------- */
.arena { display: grid; grid-template-rows: auto minmax(0, 1fr) auto; justify-items: center; gap: 12px; height: 100%; }
.board-wrap { position: relative; width: 100%; min-height: 0; display: grid; place-items: center; }
.frame {
  position: relative; padding: 11px; border-radius: 30px;
  background: linear-gradient(145deg, #fbe7a1, #b8862b 28%, #f6d77f 50%, #9a6b1c 76%, #ecc86d);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .55), 0 0 0 1px #4a320a, 0 30px 80px -20px rgba(0, 0, 0, .85), 0 0 70px -12px rgba(224, 168, 46, .4);
}
.frame::after { /* corner rivets */
  content: ''; position: absolute; inset: 0; border-radius: inherit; pointer-events: none;
  --rv: #fff8dc 0 2px, #7a520f 3px, transparent 3.6px;
  background:
    radial-gradient(circle at 12px 12px, var(--rv)), radial-gradient(circle at calc(100% - 12px) 12px, var(--rv)),
    radial-gradient(circle at 12px calc(100% - 12px), var(--rv)), radial-gradient(circle at calc(100% - 12px) calc(100% - 12px), var(--rv));
}
#board { display: block; border-radius: 20px; box-shadow: 0 0 0 2px #5a3d0c; touch-action: manipulation; }
.cards { width: var(--bs, 100%); display: flex; justify-content: space-between; gap: 10px; }
.pcard {
  --c: #888; position: relative; display: flex; align-items: center; gap: 10px; min-width: 0;
  padding: 6px; padding-right: 8px; border-radius: 999px;
  background: var(--panel); border: 1px solid var(--line);
  transition: border-color .25s, box-shadow .25s, background .25s;
}
.pcard.right { flex-direction: row-reverse; padding: 6px; padding-left: 8px; text-align: right; }
.pcard.empty { visibility: hidden; }
.pcard.active {
  border-color: color-mix(in srgb, var(--c) 70%, transparent);
  background: color-mix(in srgb, var(--c) 14%, var(--surface));
  box-shadow: 0 0 30px -6px color-mix(in srgb, var(--c) 70%, transparent);
}
.pcard.done { opacity: .6; }
.pcard.mine .meta small { color: var(--c); text-transform: uppercase; letter-spacing: .06em; font-weight: 800; }
.pcard .meta { min-width: 0; display: flex; flex-direction: column; }
.pcard .meta b { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 140px; }
.pcard .meta small { color: var(--muted); font-size: .75rem; font-weight: 600; }
@property --t { syntax: '<number>'; inherits: false; initial-value: 1; }
.pcard .ava { width: 46px; height: 46px; position: relative; font-size: 1.1rem; }
.ava[data-team]::after {
  content: attr(data-team); position: absolute; right: -5px; bottom: -5px; width: 18px; height: 18px; display: grid; place-items: center;
  border-radius: 50%; border: 2px solid var(--bg); background: #fff; color: #111; font: 800 .65rem/1 "Plus Jakarta Sans", sans-serif;
}
.ava.emoji { font-size: 1.55em; text-shadow: none; background: radial-gradient(circle at 35% 30%, color-mix(in srgb, var(--c) 45%, #fff), var(--c)); }
.avatars { display: flex; flex-wrap: wrap; justify-content: center; gap: 6px; max-width: 460px; }
.avatars label { position: relative; }
.avatars input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.avatars span {
  width: 42px; height: 42px; display: grid; place-items: center; font-size: 1.5rem; border-radius: 50%;
  background: var(--panel); border: 2px solid transparent; transition: transform .15s, border-color .15s, background .15s;
}
.avatars label:hover span { transform: scale(1.1); }
.avatars input:checked + span { border-color: var(--accent); background: rgba(124, 108, 255, .2); transform: scale(1.12); }
.avatars input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.pcard .ava::before {
  content: ''; position: absolute; inset: -5px; border-radius: 50%; opacity: 0; transition: opacity .2s;
  background: conic-gradient(var(--ok) calc(var(--t) * 360deg), rgba(255, 255, 255, .12) 0);
  -webkit-mask: radial-gradient(farthest-side, transparent calc(100% - 3.5px), #000 calc(100% - 3px));
  mask: radial-gradient(farthest-side, transparent calc(100% - 3.5px), #000 calc(100% - 3px));
}
.pcard.active .ava::before { opacity: 1; }
.pcard.timer .ava::before { animation: countdown 30s linear forwards; animation-delay: calc(var(--left, 30s) - 30s); }
@keyframes countdown { to { --t: 0; } }
.slot { width: 58px; height: 58px; flex: none; display: grid; place-items: center; border-radius: 50%; background: var(--sunken); border: 1px dashed var(--line); }
.pcard.active .slot { border-color: transparent; background: transparent; }
.bubble {
  position: absolute; bottom: calc(100% + 8px); left: 12px; max-width: 220px; z-index: 2;
  padding: 8px 12px; border-radius: 14px 14px 14px 4px; background: #fff; color: #111; font-weight: 600; font-size: .9rem;
  box-shadow: 0 10px 30px -10px rgba(0, 0, 0, .6); border: 1px solid rgba(0, 0, 0, .08); overflow-wrap: anywhere;
  opacity: 0; transform: translateY(6px) scale(.95); pointer-events: none; transition: .2s;
}
.pcard.right .bubble { left: auto; right: 12px; border-radius: 14px 14px 4px 14px; }
.cards.top .bubble { bottom: auto; top: calc(100% + 8px); }
.bubble.show { opacity: 1; transform: none; }

/* 3D die: a cube of six faces; main.js rotates it to the rolled value */
.die {
  --c: #ef4444; --s: 56px; position: relative; width: var(--s); height: var(--s); padding: 0; border-radius: 15px;
  perspective: 320px; filter: drop-shadow(0 8px 10px rgba(0, 0, 0, .4));
}
.cube { position: absolute; inset: 0; transform-style: preserve-3d; transition: transform .65s cubic-bezier(.2, .8, .25, 1); }
.face {
  position: absolute; inset: 0; display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); padding: calc(var(--s) * .15);
  border-radius: 22%; background: linear-gradient(160deg, #fff, #e3e7f0); backface-visibility: hidden;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .08), inset 0 -4px 0 color-mix(in srgb, var(--c) 55%, #000);
}
.face i { width: calc(var(--s) * .16); height: calc(var(--s) * .16); border-radius: 50%; background: #1d2333; place-self: center; opacity: 0; }
.face i.on { opacity: 1; }
.face.front { transform: translateZ(calc(var(--s) / 2)); }
.face.back { transform: rotateY(180deg) translateZ(calc(var(--s) / 2)); }
.face.right { transform: rotateY(90deg) translateZ(calc(var(--s) / 2)); }
.face.left { transform: rotateY(-90deg) translateZ(calc(var(--s) / 2)); }
.face.top { transform: rotateX(90deg) translateZ(calc(var(--s) / 2)); }
.face.bottom { transform: rotateX(-90deg) translateZ(calc(var(--s) / 2)); }
.die:not([data-t=""])::after {
  content: attr(data-t); position: absolute; top: -9px; right: -9px; z-index: 2; min-width: 24px; height: 24px; padding: 0 5px;
  display: grid; place-items: center; border-radius: 999px; border: 2px solid var(--bg);
  background: var(--c); color: #fff; font: 800 .75rem/1 "Plus Jakarta Sans", sans-serif; text-shadow: 0 1px 1px rgba(0, 0, 0, .35);
}
.die.urgent::after { background: #ef4444; animation: tick 1s infinite; }
@keyframes tick { 50% { transform: scale(1.18); } }
.die:disabled { opacity: 1; cursor: default; }
.die:not(:disabled) { animation: bob 1s ease-in-out infinite; box-shadow: 0 0 0 4px color-mix(in srgb, var(--c) 50%, transparent); }
@keyframes bob { 50% { transform: translateY(-4px); } }

/* ---------- side: voice + chat ---------- */
.side { width: 340px; min-height: 0; display: flex; flex-direction: column; gap: 14px; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; gap: 10px; min-height: 0; }
.panel header { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.panel h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 700; }
.voice-ctl { display: flex; align-items: center; gap: 6px; }
.members { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.members li { display: flex; align-items: center; gap: 10px; padding: 6px 8px; border-radius: 12px; }
.members li > span:nth-child(2) { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.members li.out { opacity: .5; }
.members .ava { width: 32px; height: 32px; font-size: .85rem; transition: box-shadow .15s; }
.members li.speaking .ava { box-shadow: 0 0 0 3px var(--bg), 0 0 0 5px var(--ok); }
.members svg { width: 16px; height: 16px; color: var(--muted); }
.members svg.off { color: #f87171; }
.chat { flex: 1; }
.chat-log { flex: 1; min-height: 80px; overflow-y: auto; list-style: none; margin: 0; padding: 2px; display: flex; flex-direction: column; gap: 6px; }
.msg {
  align-self: flex-start; max-width: 85%; padding: 7px 11px; border-radius: 14px 14px 14px 4px;
  background: var(--panel-2); overflow-wrap: anywhere; animation: pop .2s ease-out;
}
.msg b { display: block; font-size: .75rem; color: var(--c); }
.msg.mine { align-self: flex-end; border-radius: 14px 14px 4px 14px; background: linear-gradient(135deg, var(--accent), var(--accent-2)); }
.sys { align-self: center; font-size: .78rem; color: var(--muted); }
@keyframes pop { from { opacity: 0; transform: translateY(4px); } }
.quick { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.quick button { flex: none; padding: 5px 10px; border-radius: 999px; background: var(--panel-2); border: 1px solid var(--line); font-size: .82rem; font-weight: 600; }
.quick button:hover { background: var(--hover); }
.chat-form { display: flex; gap: 6px; }
.chat-form .send { background: linear-gradient(135deg, var(--accent), var(--accent-2)); color: #fff; }

/* ---------- overlay / toast ---------- */
.overlay { position: fixed; inset: 0; z-index: 10; display: grid; place-items: center; padding: 16px; background: var(--scrim); backdrop-filter: blur(6px); animation: fade .3s; }
.over-card { width: min(380px, 100%); padding: 28px; border-radius: 24px; background: var(--surface); border: 1px solid var(--line); display: flex; flex-direction: column; gap: 16px; text-align: center; animation: rise .4s cubic-bezier(.2, .9, .3, 1.2); }
.trophy { font-size: 3rem; }
.ranks { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; counter-reset: r; }
.ranks li { counter-increment: r; display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-radius: 14px; background: var(--panel); font-weight: 700; }
.ranks li::before { content: '#' counter(r); color: var(--muted); width: 2ch; }
.ranks li:first-child { background: linear-gradient(90deg, rgba(250, 204, 21, .2), transparent); }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(20px) scale(.96); } }
#toast {
  position: fixed; left: 50%; bottom: 28px; z-index: 20; transform: translate(-50%, 16px); opacity: 0; pointer-events: none;
  padding: 11px 20px; border-radius: 999px; background: #fff; color: #111; font-weight: 700; box-shadow: 0 10px 30px rgba(0, 0, 0, .4); transition: .25s;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- responsive ---------- */
@media (max-width: 1100px) {
  .tiles { grid-template-columns: 1fr 1fr; }
  .tiles .tile:last-child { grid-column: 1 / -1; }
  .side { width: 300px; }
}
@media (max-width: 860px) {
  .app { height: auto; min-height: 100dvh; }
  .topbar { height: 56px; padding: 0 12px; gap: 10px; position: sticky; top: 0; z-index: 5; }
  .brand span { display: none; }
  .status { font-size: .88rem; }
  .chip { padding: 6px 10px; } .chip > :not(code) { display: none; }
  .layout { grid-template-columns: minmax(0, 1fr); padding: 12px; gap: 12px; }
  .play { overflow: visible; }
  .tiles { grid-template-columns: 1fr; }
  .arena { height: auto; gap: 8px; grid-template-rows: auto auto auto; align-content: center; }
  .board-wrap { height: min(100vw - 24px, 64dvh); }
  .pcard .meta small { display: none; }
  .pcard .meta b { max-width: 90px; font-size: .85rem; }
  .pcard .ava { width: 38px; height: 38px; }
  .slot { width: 48px; height: 48px; }
  .die { --s: 48px; }
  .side { width: auto; }
  .chat { height: 380px; flex: none; }
  .seat-grid { grid-template-columns: 1fr; }
}
@media (max-width: 520px) {
  .status { display: none; }
  .actions { margin-left: auto; }
  .pcard .meta { display: none; }
  .pcard, .pcard.right { padding: 4px; }
}
/* ---------- in-game feedback ---------- */
.turn-banner {
  position: absolute; left: 50%; top: 50%; z-index: 3; pointer-events: none; white-space: nowrap;
  padding: 14px 30px; border-radius: 999px; font-size: 1.6rem; font-weight: 800; color: #fff;
  background: color-mix(in srgb, var(--c, #7c6cff) 85%, #000); box-shadow: 0 20px 50px -10px rgba(0, 0, 0, .6), 0 0 0 4px rgba(255, 255, 255, .25);
  transform: translate(-50%, -50%) scale(.6); opacity: 0;
}
.turn-banner.show { animation: banner 1.5s cubic-bezier(.2, .9, .3, 1.2) forwards; }
@keyframes banner {
  15%, 75% { opacity: 1; transform: translate(-50%, -50%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -62%) scale(.95); }
}
.frame.my-turn { animation: glow 1.6s ease-in-out infinite; }
@keyframes glow {
  50% { box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .55), 0 0 0 1px #4a320a, 0 30px 80px -20px rgba(0, 0, 0, .85), 0 0 60px 8px color-mix(in srgb, var(--c) 70%, transparent); }
}
.frame.shake { animation: shake .4s; }
@keyframes shake {
  20% { transform: translate(-6px, 2px) rotate(-.6deg); } 40% { transform: translate(5px, -2px) rotate(.5deg); }
  60% { transform: translate(-3px, 1px); } 80% { transform: translate(2px, 0); }
}
.callout {
  position: absolute; left: 50%; top: 22%; z-index: 4; pointer-events: none; white-space: nowrap;
  padding: 8px 18px; border-radius: 999px; font-weight: 800; font-size: 1.1rem; color: #fff;
  background: color-mix(in srgb, var(--c) 80%, #000); box-shadow: 0 12px 30px -8px rgba(0, 0, 0, .6);
  animation: callout 1.6s ease-out forwards;
}
@keyframes callout {
  0% { opacity: 0; transform: translate(-50%, 10px) scale(.8); } 15% { opacity: 1; transform: translate(-50%, 0) scale(1.05); }
  70% { opacity: 1; transform: translate(-50%, -6px); } 100% { opacity: 0; transform: translate(-50%, -24px); }
}
.spark { position: fixed; z-index: 30; width: 8px; height: 8px; border-radius: 50%; pointer-events: none; transform: translate(-50%, -50%); animation: spark .7s ease-out forwards; }
@keyframes spark { to { transform: translate(calc(-50% + var(--dx)), calc(-50% + var(--dy))) scale(.2); opacity: 0; } }
.cards:not(:has(.pcard:not(.empty))) { display: none; } /* both corners empty: give the board the room */
.ranks li small { margin-left: auto; color: var(--muted); font-weight: 600; font-size: .78rem; }
#ready-note { font-size: .85rem; }

/* ---------- dialogs, invite, connection ---------- */
.dlg {
  width: min(440px, calc(100vw - 32px)); max-height: 86dvh; padding: 24px; border-radius: 22px;
  border: 1px solid var(--line); background: var(--surface); color: var(--text); box-shadow: 0 30px 80px -20px rgba(0, 0, 0, .7);
}
.dlg::backdrop { background: var(--scrim); backdrop-filter: blur(4px); }
.dlg h2 { margin-bottom: 14px; }
.dlg h3 { margin: 16px 0 8px; font-size: .8rem; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); }
.dlg form { margin-top: 18px; }
.dlg form .btn { width: 100%; }
.dlg .setting { margin: 14px 0; }
.dlg .note { margin-top: 12px; }
.howto { margin: 0; padding-left: 20px; display: grid; gap: 8px; }
.help-rules { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; font-size: .88rem; color: var(--muted); }
.help-rules li.on { color: var(--text); }
.help-rules .pill { margin-left: 6px; background: var(--accent); color: #fff; }
input[type="range"] { width: 170px; padding: 0; border: 0; background: none; accent-color: var(--accent); }
.qr { text-align: center; }
.qr img { display: block; margin: 8px auto; border-radius: 12px; background: #fff; image-rendering: pixelated; }
.invite-row { display: flex; gap: 8px; justify-content: center; }
.net {
  position: fixed; top: 74px; left: 50%; z-index: 25; transform: translateX(-50%);
  padding: 8px 16px; border-radius: 999px; background: #f59e0b; color: #111; font-weight: 800; font-size: .85rem;
  box-shadow: 0 10px 30px rgba(0, 0, 0, .4); animation: blink 1.2s ease-in-out infinite;
}
@keyframes blink { 50% { opacity: .65; } }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* ---------- phones: chat and voice in a bottom sheet while playing ---------- */
.chat-fab {
  position: fixed; right: 16px; bottom: 16px; z-index: 12; width: 58px; height: 58px; border-radius: 50%; font-size: 1.5rem;
  background: linear-gradient(135deg, var(--accent), var(--accent-2)); box-shadow: 0 12px 30px -8px rgba(0, 0, 0, .6); display: none;
}
.chat-fab b {
  position: absolute; top: -4px; right: -4px; min-width: 22px; height: 22px; padding: 0 6px; border-radius: 999px;
  display: grid; place-items: center; background: #ef4444; color: #fff; font-size: .72rem; border: 2px solid var(--bg);
}
.sheet-close { display: none; }
@media (max-width: 860px) and (orientation: portrait) {
  body[data-screen="game"] .chat-fab:not([hidden]) { display: grid; place-items: center; }
  body[data-screen="game"].sheet-open .chat-fab { display: none; }
  body[data-screen="game"] .side {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 11; max-height: 80dvh; padding: 10px 12px calc(12px + env(safe-area-inset-bottom));
    background: var(--surface); border-radius: 22px 22px 0 0; box-shadow: 0 -20px 60px rgba(0, 0, 0, .5); overflow: auto;
    transform: translateY(105%); transition: transform .3s cubic-bezier(.2, .8, .2, 1);
  }
  body[data-screen="game"].sheet-open .side { transform: none; }
  body[data-screen="game"] .sheet-close {
    display: block; align-self: center; padding: 5px 20px; border-radius: 999px; background: var(--panel-2); font-weight: 700; font-size: .8rem;
  }
  body[data-screen="game"] .chat { height: 46dvh; }
}

/* ---------- phones held sideways: board in the middle, players on its sides, chat on the right ---------- */
@media (orientation: landscape) and (max-height: 560px) {
  .app { height: 100dvh; min-height: 0; }
  .topbar { height: 46px; position: static; }
  .layout { grid-template-columns: minmax(0, 1fr) 280px; padding: 8px; gap: 8px; }
  .play { overflow: auto; }
  .arena { height: 100%; grid-template: "tl board tr" 1fr "bl board br" 1fr / auto minmax(0, 1fr) auto; gap: 6px 10px; }
  .cards { display: contents; }
  .pcard[data-seat="0"] { grid-area: tl; align-self: start; }
  .pcard[data-seat="1"] { grid-area: tr; align-self: start; }
  .pcard[data-seat="3"] { grid-area: bl; align-self: end; }
  .pcard[data-seat="2"] { grid-area: br; align-self: end; }
  .pcard, .pcard.right { flex-direction: column; border-radius: 18px; padding: 6px; }
  .pcard .meta { display: none; }
  .pcard .bubble, .cards.top .bubble { top: 0; bottom: auto; left: calc(100% + 6px); right: auto; }
  .pcard.right .bubble { left: auto; right: calc(100% + 6px); }
  .board-wrap { grid-area: board; height: 100%; }
  .layout { overflow: hidden; }
  .side { width: auto; overflow: auto; }
  .chat { height: auto; flex: 1; min-height: 200px; }
}
@media (prefers-reduced-motion: reduce) { .fx, .bg-fx::before, .bg-fx::after { animation: none !important; } * { animation-duration: .01ms !important; transition: none !important; } }
