:root {
  --bg: #0d1016; --bg2: #141922; --card: #1a202b; --card2: #222a37; --line: #2b3444; --text: #eef2f8; --muted: #8e9ab0;
  --pri: #3b82f6; --pri-ink: #fff; --good: #22c55e; --warn: #ef4444; --gold: #facc15; --focus: #93b4ff;
  --r: 14px; --safe-b: env(safe-area-inset-bottom, 0px); --safe-t: env(safe-area-inset-top, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
body { overscroll-behavior: none; }
button, input, select, textarea { font: inherit; color: inherit; }
svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.small { font-size: 13px; } .muted { color: var(--muted); }
#app { height: 100%; }
.screen { height: 100%; display: flex; flex-direction: column; }

/* buttons */
.btn { background: var(--card2); border: 1px solid var(--line); border-radius: 12px; padding: 10px 16px; font-weight: 600; cursor: pointer; min-height: 44px; }
.btn:active { transform: scale(.98); }
.btn.pri { background: var(--pri); border-color: var(--pri); color: var(--pri-ink); }
.btn.warn { background: #3a1a1e; border-color: #6b2a31; color: #fecaca; }
.btn.ghost { background: transparent; }
.btn.big { width: 100%; padding: 16px; font-size: 17px; }
.btn.sm { min-height: 34px; padding: 6px 12px; font-size: 14px; border-radius: 10px; }
.btn:disabled { opacity: .45; cursor: default; }
.icon-btn { background: none; border: 0; padding: 8px; border-radius: 10px; cursor: pointer; display: grid; place-items: center; color: var(--text); min-width: 40px; min-height: 40px; }
.icon-btn:active { background: var(--card2); }
.link { background: none; border: 0; color: var(--focus); padding: 6px 0; cursor: pointer; font-weight: 600; }

/* home */
#home { overflow: auto; }
.home-in { width: 100%; max-width: 420px; margin: 0 auto; padding: calc(48px + var(--safe-t)) 20px 32px; display: flex; flex-direction: column; gap: 16px; }
.logo { width: 84px; height: 84px; border-radius: 50%; background: #06080b; border: 5px solid var(--good); box-shadow: 0 0 40px #22c55e55; display: grid; place-items: center; }
.logo span { width: 26px; height: 26px; border-radius: 50%; background: var(--good); }
h1 { font-size: 34px; letter-spacing: .08em; margin: 4px 0 0; }
.lede { color: var(--muted); margin: 0 0 8px; font-size: 16px; }
.join label { font-weight: 600; display: block; margin-bottom: 6px; }
.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; min-width: 0; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px; font-size: 22px; letter-spacing: .3em; text-transform: uppercase; font-weight: 700; }
.recent-list { display: flex; gap: 8px; flex-wrap: wrap; }
.home-note { margin-top: 8px; }

/* top bar */
.top { display: flex; align-items: center; gap: 6px; padding: calc(6px + var(--safe-t)) 8px 6px; background: var(--bg2); border-bottom: 1px solid var(--line); }
.top-mid { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: flex-start; }
.top-game { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 100%; }
.code-chip { background: none; border: 0; padding: 0; color: var(--muted); font-size: 13px; cursor: pointer; }
.code-chip b { color: var(--text); letter-spacing: .12em; }
.conn { width: 10px; height: 10px; border-radius: 50%; background: var(--muted); margin: 0 6px; }
.conn.ok { background: var(--good); } .conn.bad { background: var(--warn); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .3; } }
#muteBtn[aria-pressed="true"] .wave { display: none; }
#muteBtn[aria-pressed="true"] { color: var(--muted); }

main#views { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; }
.tab { max-width: 640px; margin: 0 auto; padding: 14px 14px 24px; display: flex; flex-direction: column; gap: 14px; }
.tabs { display: flex; background: var(--bg2); border-top: 1px solid var(--line); padding-bottom: var(--safe-b); }
.tabs button { flex: 1; background: none; border: 0; color: var(--muted); padding: 8px 0 6px; display: flex; flex-direction: column; align-items: center; gap: 2px; font-size: 12px; font-weight: 600; cursor: pointer; }
.tabs button[aria-selected="true"] { color: var(--text); }
.tabs button[aria-selected="true"] svg { stroke: var(--pri); }

/* cards */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--r); padding: 14px; }
.card h3 { margin: 0 0 10px; font-size: 13px; text-transform: uppercase; letter-spacing: .08em; color: var(--muted); font-weight: 700; display: flex; align-items: center; gap: 8px; }
.card h3 .grow { flex: 1; }
.title-card h2 { margin: 2px 0 0; font-size: 22px; line-height: 1.2; }
.title-card .sub { color: var(--muted); font-size: 13px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }
.btnrow { display: flex; gap: 8px; flex-wrap: wrap; }
.btnrow .btn { flex: 1 1 auto; }

/* PUCK screens */
.strip { display: flex; gap: 10px; overflow-x: auto; padding: 4px 2px 6px; scroll-snap-type: x mandatory; }
.strip .puck { flex: 0 0 auto; width: 92px; scroll-snap-align: start; }
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px 14px; }
@media (min-width: 560px) { .grid.many { grid-template-columns: repeat(3, 1fr); } }
.puck { position: relative; display: flex; flex-direction: column; align-items: center; gap: 6px; }
.puck .disc { position: relative; width: 100%; aspect-ratio: 1; border-radius: 50%; background: #23272f; padding: 5%; box-shadow: 0 8px 22px #0008, inset 0 1px 0 #ffffff14; }
.puck canvas { width: 100%; height: 100%; border-radius: 50%; display: block; touch-action: none; cursor: pointer; user-select: none; -webkit-user-select: none; }
.puck .lbl { font-size: 13px; font-weight: 600; display: flex; gap: 4px; align-items: center; max-width: 100%; }
.puck .lbl span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.puck .lbl .crown { color: var(--gold); }
.puck .chip { position: absolute; top: 46%; left: 50%; transform: translate(-50%, -50%) scale(.9); background: #000c; color: #fff; padding: 3px 9px; border-radius: 99px; font-size: 12px; pointer-events: none; opacity: 0; transition: opacity .15s, transform .15s; white-space: nowrap; }
.puck .chip.show { opacity: 1; transform: translate(-50%, -50%) scale(1); }
.puck .more { position: absolute; right: -4px; bottom: 26px; background: var(--card2); border: 1px solid var(--line); border-radius: 50%; width: 34px; height: 34px; display: grid; place-items: center; cursor: pointer; padding: 0; }
.puck .more svg { width: 18px; height: 18px; }
.strip .puck .lbl { font-size: 12px; }
.strip .puck .more { display: none; }
.strip .puck .chip { font-size: 10px; padding: 2px 6px; }
.tab { width: 100%; }
.hint { color: var(--muted); font-size: 13px; margin: 0; }

/* players */
.player { display: flex; align-items: center; gap: 10px; padding: 10px 4px; border-top: 1px solid var(--line); }
.player:first-of-type { border-top: 0; }
.player .dot { width: 14px; height: 14px; border-radius: 50%; flex: none; border: 2px solid transparent; }
.player.lit .dot { box-shadow: 0 0 0 3px #ffffff22, 0 0 12px currentColor; }
.player .who { flex: 1; min-width: 0; background: none; border: 0; text-align: left; padding: 0; cursor: pointer; }
.player .who b { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.player .who small { color: var(--muted); }
.player.out .who b { text-decoration: line-through; color: var(--muted); }
.score { display: flex; align-items: center; gap: 4px; }
.score output { min-width: 34px; text-align: center; font-weight: 800; font-size: 20px; font-variant-numeric: tabular-nums; }
.sq { width: 40px; height: 40px; padding: 0; min-height: 40px; border-radius: 10px; font-size: 20px; line-height: 1; }
.pdetail { padding: 4px 0 10px 24px; display: flex; flex-direction: column; gap: 8px; }
.kv { display: flex; align-items: center; gap: 8px; }
.kv .k { flex: 1; color: var(--muted); }
.kv output { min-width: 40px; text-align: center; font-weight: 700; font-variant-numeric: tabular-nums; }
.feed { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; font-size: 13px; }
.feed li { color: var(--muted); } .feed li b { color: var(--text); font-weight: 600; }
.timers { display: flex; gap: 8px; flex-wrap: wrap; }
.timer { background: var(--card2); border-radius: 99px; padding: 3px 10px; font-size: 13px; font-variant-numeric: tabular-nums; }
details.how summary { cursor: pointer; font-weight: 600; }
details.how p { white-space: pre-wrap; color: var(--muted); margin: 8px 0 0; }

/* games */
.games { display: flex; flex-direction: column; gap: 8px; }
.game { text-align: left; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; cursor: pointer; width: 100%; }
.game b { display: block; }
.game span { color: var(--muted); font-size: 13px; }
.game.cur { border-color: var(--pri); }
.cat { margin: 10px 0 2px; font-size: 13px; color: var(--muted); text-transform: uppercase; letter-spacing: .08em; font-weight: 700; }
.search { width: 100%; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 14px; }

/* rule editor */
.ed-bar { position: sticky; top: -14px; z-index: 3; margin: -14px -14px 0; padding: 10px 14px; background: color-mix(in srgb, var(--bg) 92%, transparent); backdrop-filter: blur(8px); border-bottom: 1px solid var(--line); display: flex; gap: 8px; align-items: center; }
.ed-bar .state { flex: 1; font-size: 13px; color: var(--muted); }
.ed-bar .state.dirty { color: var(--gold); }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { font-size: 12px; color: var(--muted); font-weight: 600; }
input.in, select.in, textarea.in { background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; min-height: 40px; min-width: 0; max-width: 100%; }
textarea.in { width: 100%; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; min-height: 260px; }
input.in.code, .in.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 14px; }
input.in[aria-invalid="true"] { border-color: var(--warn); background: #2a1316; }
.err { color: #fca5a5; font-size: 12px; }
.decls { display: flex; flex-direction: column; gap: 6px; }
.decl { display: grid; grid-template-columns: 1fr 1fr auto; gap: 6px; align-items: center; }
.rule { background: var(--card); border: 1px solid var(--line); border-radius: var(--r); overflow: hidden; }
.rule.off { opacity: .55; }
.rule-head { display: flex; align-items: center; gap: 4px; padding: 6px 6px 6px 12px; }
.rule-head .sum { flex: 1; min-width: 0; background: none; border: 0; text-align: left; padding: 6px 0; cursor: pointer; }
.rule-head .sum b { display: block; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rule-head .sum small { color: var(--muted); display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rule-body { border-top: 1px solid var(--line); padding: 12px; display: flex; flex-direction: column; gap: 12px; }
.clause { display: flex; flex-direction: column; gap: 6px; }
.kw { font-size: 11px; font-weight: 800; letter-spacing: .12em; padding: 2px 8px; border-radius: 6px; align-self: flex-start; }
.kw.when { background: #1e3a8a; color: #bfdbfe; } .kw.if { background: #4c1d95; color: #ddd6fe; } .kw.then { background: #14532d; color: #bbf7d0; } .kw.else { background: #7c2d12; color: #fed7aa; }
.act { background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; padding: 8px; display: flex; flex-direction: column; gap: 6px; }
.act .row > .in { flex: 1 1 120px; }
.act .word { color: var(--muted); font-size: 13px; }
.swatches { display: flex; gap: 6px; flex-wrap: wrap; }
.swatch { width: 28px; height: 28px; border-radius: 50%; border: 2px solid transparent; cursor: pointer; padding: 0; }
.swatch[aria-pressed="true"] { border-color: #fff; box-shadow: 0 0 0 2px var(--bg2); }
.mini { display: flex; gap: 2px; }
.mini .icon-btn { min-width: 32px; min-height: 32px; padding: 4px; }
.mini svg { width: 18px; height: 18px; }
.switch { position: relative; width: 44px; height: 26px; flex: none; }
.switch input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; }
.switch span { position: absolute; inset: 0; border-radius: 99px; background: var(--line); transition: background .15s; pointer-events: none; }
.switch span::after { content: ''; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch input:checked + span { background: var(--good); }
.switch input:checked + span::after { transform: translateX(18px); }
.add { align-self: flex-start; }

/* sheet + toast */
.sheet { position: fixed; inset: 0; background: #000a; display: flex; align-items: flex-end; justify-content: center; z-index: 20; }
.sheet-card { background: var(--bg2); border: 1px solid var(--line); border-radius: 18px 18px 0 0; width: 100%; max-width: 520px; padding: 16px 16px calc(16px + var(--safe-b)); display: flex; flex-direction: column; gap: 10px; max-height: 85vh; overflow: auto; }
.sheet-card h2 { margin: 0; font-size: 18px; }
.sheet-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.toast { position: fixed; left: 50%; bottom: calc(80px + var(--safe-b)); transform: translateX(-50%) translateY(20px); background: #f1f5f9; color: #0b0d11; padding: 10px 16px; border-radius: 12px; font-weight: 600; opacity: 0; pointer-events: none; transition: .2s; max-width: calc(100% - 32px); z-index: 30; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.bad { background: #fecaca; }
