:root {
  --felt: #0f4a39;
  --felt-dark: #0a3329;
  --ink: #f3f6f4;
  --muted: #a9c3b8;
  --panel: rgba(0, 0, 0, 0.22);
  --accent: #ffcf4d;
  --accent-ink: #2b2100;
  --ok: #57d68d;
  --bad: #ff6b6b;
  --tile: #fbf6e9;
  --tile-edge: #d9cfb6;
  --c0: #d7263d;
  --c1: #1f5fd1;
  --c2: #e48a00;
  --c3: #1e1e1e;
  --rw: min(44px, calc((100vw - 24px - 9 * 4px) / 10));
  --rh: calc(var(--rw) * 1.38);
  --tw: min(38px, calc((100vw - 24px) / 13));
  --th: calc(var(--tw) * 1.38);
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(120% 80% at 50% 0%, #156048 0%, var(--felt) 45%, var(--felt-dark) 100%) fixed;
  background-color: var(--felt);
  color: var(--ink);
  font: 16px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Rounded", "Segoe UI", Roboto, sans-serif;
  overscroll-behavior: none;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-text-size-adjust: 100%;
}
input { -webkit-user-select: text; user-select: text; }
#app { height: 100dvh; display: flex; flex-direction: column; overflow: hidden; }
.hidden { display: none !important; }
.screen { flex: 1; min-height: 0; display: flex; flex-direction: column; }

/* ---------- buttons & fields ---------- */
.btn {
  appearance: none; border: 0; border-radius: 12px; padding: 12px 18px; font: inherit; font-weight: 600;
  color: var(--ink); background: rgba(255, 255, 255, 0.12); cursor: pointer; transition: transform .08s, opacity .15s;
}
.btn:active { transform: scale(0.96); }
.btn:disabled { opacity: 0.35; pointer-events: none; }
.btn.primary { background: var(--accent); color: var(--accent-ink); }
.btn.big { width: 100%; padding: 16px; font-size: 18px; }
.btn.sm { padding: 9px 12px; font-size: 15px; border-radius: 10px; }
.btn.link { background: none; color: var(--muted); font-weight: 500; text-decoration: underline; text-underline-offset: 3px; }
.iconbtn { appearance: none; border: 0; background: none; color: var(--ink); font-size: 22px; padding: 4px 8px; }
.field { display: block; text-align: left; width: 100%; }
.field span { display: block; font-size: 13px; color: var(--muted); margin: 0 0 6px 4px; }
input {
  width: 100%; font: inherit; font-size: 18px; color: var(--ink); background: rgba(0, 0, 0, 0.28); border: 1px solid rgba(255,255,255,.14);
  border-radius: 12px; padding: 13px 14px; outline: none;
}
input:focus { border-color: var(--accent); }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin: 0 1px; background: var(--c0); }
.dot.c1 { background: var(--c1); } .dot.c2 { background: var(--c2); }

/* ---------- home / lobby ---------- */
.home-inner {
  width: 100%; max-width: 420px; margin: 0 auto; padding: calc(var(--sat) + 28px) calc(16px + var(--sar)) calc(var(--sab) + 24px) calc(16px + var(--sal));
  display: flex; flex-direction: column; align-items: center; gap: 14px; text-align: center; overflow-y: auto; height: 100%;
}
h1 { margin: 4px 0 0; font-size: 34px; letter-spacing: -0.5px; }
h2 { margin: 10px 0 0; font-size: 18px; align-self: flex-start; }
.sub { margin: 0; color: var(--muted); font-size: 15px; }
.logo { display: flex; gap: 6px; margin-top: 18px; --rw: 48px; }
.logo .tile:nth-child(1) { transform: rotate(-10deg) translateY(4px); }
.logo .tile:nth-child(3) { transform: rotate(10deg) translateY(4px); }
#homeActions, #joinPrompt { width: 100%; display: flex; flex-direction: column; gap: 12px; }
.or { color: var(--muted); font-size: 13px; display: flex; align-items: center; gap: 10px; }
.or::before, .or::after { content: ""; flex: 1; height: 1px; background: rgba(255,255,255,.15); }
.joinrow { display: flex; gap: 8px; }
.joinrow input { text-transform: uppercase; letter-spacing: 6px; text-align: center; font-weight: 700; }
.bigcode { font-size: 56px; font-weight: 800; letter-spacing: 10px; padding-left: 10px; color: var(--accent); line-height: 1; }
.plist { list-style: none; margin: 0; padding: 0; width: 100%; display: flex; flex-direction: column; gap: 8px; }
.plist li { display: flex; align-items: center; gap: 10px; background: var(--panel); border-radius: 12px; padding: 12px 14px; text-align: left; }
.plist li .nm { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.plist li .tag { font-size: 12px; color: var(--muted); }
.plist li.empty { color: var(--muted); font-style: italic; background: rgba(0,0,0,.1); }
.plist .x { background: none; border: 0; color: var(--muted); font-size: 18px; padding: 0 4px; }
.seg { display: flex; background: var(--panel); border-radius: 12px; padding: 4px; gap: 4px; width: 100%; margin-top: 6px; }
.seg button { flex: 1; border: 0; background: none; color: var(--ink); font: inherit; font-size: 14px; padding: 9px 0; border-radius: 9px; }
.seg button.on { background: rgba(255,255,255,.18); font-weight: 700; }
#hostOpts { width: 100%; text-align: left; }
.online { width: 9px; height: 9px; border-radius: 50%; background: var(--ok); flex: none; }
.online.off { background: #6b7d76; }

/* ---------- tiles ---------- */
.tile {
  --w: var(--rw);
  width: var(--w); height: calc(var(--w) * 1.38); flex: none; position: relative;
  background: linear-gradient(180deg, #fffdf6 0%, var(--tile) 70%, #efe6cf 100%);
  border-radius: calc(var(--w) * 0.16);
  box-shadow: 0 1px 0 var(--tile-edge), 0 2px 0 #c3b796, 0 3px 5px rgba(0, 0, 0, 0.35);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  font-weight: 800; color: var(--c0); line-height: 1;
  transition: transform .12s ease, box-shadow .12s ease;
  touch-action: none;
}
.tile .n { font-size: calc(var(--w) * 0.6); letter-spacing: -1px; font-variant-numeric: tabular-nums; }
.tile .g { width: calc(var(--w) * 0.2); height: calc(var(--w) * 0.2); margin-top: calc(var(--w) * 0.08); background: currentColor; border-radius: 50%; opacity: .85; }
.tile.c1 { color: var(--c1); } .tile.c1 .g { border-radius: 2px; }
.tile.c2 { color: var(--c2); } .tile.c2 .g { border-radius: 0; clip-path: polygon(50% 0, 100% 100%, 0 100%); }
.tile.c3 { color: var(--c3); } .tile.c3 .g { border-radius: 0; clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%); }
.tile.joker .n { font-size: calc(var(--w) * 0.66); background: conic-gradient(var(--c0), var(--c2), var(--c1), var(--c3), var(--c0)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.tile.joker .g { display: none; }
.tile.sel { transform: translateY(-6px); box-shadow: 0 0 0 3px var(--accent), 0 8px 12px rgba(0, 0, 0, 0.4); }
.tile.fresh::after { content: ""; position: absolute; inset: -3px; border-radius: inherit; border: 2px solid rgba(255, 207, 77, .75); pointer-events: none; }
.tile.ghosted { opacity: .3; }

/* ---------- game ---------- */
#bar {
  display: flex; align-items: center; gap: 6px;
  padding: calc(var(--sat) + 6px) calc(8px + var(--sar)) 6px calc(4px + var(--sal));
  background: rgba(0, 0, 0, 0.25);
}
#players { flex: 1; display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; min-width: 0; }
#players::-webkit-scrollbar { display: none; }
.pchip {
  flex: 1 1 0; min-width: 0; max-width: 140px; background: rgba(255,255,255,.08); border-radius: 10px; padding: 5px 8px;
  display: flex; flex-direction: column; gap: 1px; border: 2px solid transparent; position: relative;
}
.pchip.turn { border-color: var(--accent); background: rgba(255, 207, 77, 0.14); }
.pchip .top { display: flex; align-items: center; gap: 5px; min-width: 0; }
.pchip .nm { font-size: 13px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.pchip .meta { font-size: 11px; color: var(--muted); white-space: nowrap; }
.pchip .online { width: 7px; height: 7px; }
#poolBox { display: flex; flex-direction: column; align-items: center; line-height: 1; padding: 0 4px; }
#poolBox span { font-weight: 800; font-size: 17px; }
#poolBox small { font-size: 10px; color: var(--muted); }

#table {
  flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 12px calc(10px + var(--sar)) 16px calc(10px + var(--sal));
  display: flex; flex-wrap: wrap; align-content: flex-start; gap: 12px 10px;
}
#table .tile { --w: var(--tw); touch-action: pan-y; }
.set { display: flex; gap: 2px; padding: 5px; border-radius: 9px; background: rgba(0, 0, 0, 0.16); border: 2px solid transparent; transition: background .12s, border-color .12s; }
.myturn .set.bad { border-color: var(--bad); background: rgba(255, 80, 80, 0.12); }
.set.target, .newset.target { border-color: var(--accent) !important; background: rgba(255, 207, 77, 0.18) !important; }
.newset {
  min-width: calc(var(--tw) * 3 + 14px); height: calc(var(--th) + 14px); border: 2px dashed rgba(255,255,255,.35); border-radius: 9px;
  display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 14px; font-weight: 600; padding: 0 10px;
}
.tablemsg { width: 100%; text-align: center; color: var(--muted); margin-top: 18vh; font-size: 15px; padding: 0 20px; }

#status { display: flex; align-items: center; gap: 8px; padding: 6px calc(12px + var(--sar)) 2px calc(12px + var(--sal)); min-height: 30px; font-size: 14px; }
#statusText { flex: 1; color: var(--muted); }
#statusText b { color: var(--ink); }
#statusText .good { color: var(--ok); }
#statusText .warn { color: var(--bad); }
#timer { font-weight: 800; font-variant-numeric: tabular-nums; font-size: 15px; }
#timer.low { color: var(--bad); }
#actions { display: flex; gap: 6px; padding: 4px calc(10px + var(--sar)) 6px calc(10px + var(--sal)); align-items: center; }
#actions .spacer { flex: 1; }
#rack {
  display: flex; flex-wrap: wrap; align-content: flex-start; gap: 6px 4px;
  padding: 10px calc(12px + var(--sar)) calc(var(--sab) + 12px) calc(12px + var(--sal));
  background: linear-gradient(180deg, #6b4a2b 0%, #553a20 100%);
  box-shadow: inset 0 3px 0 rgba(255,255,255,.12), 0 -4px 14px rgba(0,0,0,.35);
  min-height: calc(var(--rh) * 2 + 6px + 22px + var(--sab));
  max-height: calc(var(--rh) * 4 + 18px + 22px + var(--sab));
  overflow-y: auto;
}
#rack.target { box-shadow: inset 0 0 0 3px var(--accent); }
#rack .drop-mark { width: 3px; height: var(--rh); background: var(--accent); border-radius: 2px; flex: none; margin: 0 -3.5px; }

.ghost { position: fixed; z-index: 50; pointer-events: none; display: flex; gap: 2px; transform: translate(-50%, -100%) scale(1.08); filter: drop-shadow(0 10px 14px rgba(0,0,0,.45)); left: 0; top: 0; }
.ghost .tile { --w: var(--rw); }

/* ---------- overlay / toast ---------- */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.55); z-index: 40; display: flex; align-items: flex-end; justify-content: center; }
.sheet {
  width: 100%; max-width: 520px; max-height: 88dvh; overflow-y: auto; background: #123f32; border-radius: 20px 20px 0 0;
  padding: 18px calc(18px + var(--sar)) calc(18px + var(--sab)) calc(18px + var(--sal)); display: flex; flex-direction: column; gap: 12px;
  box-shadow: 0 -10px 30px rgba(0,0,0,.4);
}
@media (min-width: 700px) { .overlay { align-items: center; } .sheet { border-radius: 20px; } }
.sheet h2 { margin: 0; }
.rules { margin: 0; padding-left: 20px; color: #d7e5df; font-size: 15px; display: flex; flex-direction: column; gap: 6px; }
.results { display: flex; flex-direction: column; gap: 8px; }
.res { background: var(--panel); border-radius: 12px; padding: 10px 12px; display: flex; flex-direction: column; gap: 6px; }
.res.win { background: rgba(255, 207, 77, 0.18); }
.res .row { display: flex; justify-content: space-between; font-weight: 600; }
.res .mini { display: flex; flex-wrap: wrap; gap: 2px; }
.res .mini .tile { --w: 20px; box-shadow: 0 1px 2px rgba(0,0,0,.4); }
.res .mini .tile .g { display: none; }
.pos { color: var(--ok); } .neg { color: var(--bad); }
.toast {
  position: fixed; left: 50%; top: calc(var(--sat) + 64px); transform: translate(-50%, -20px); opacity: 0; z-index: 60;
  background: #222; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; font-weight: 600; max-width: calc(100vw - 32px);
  text-align: center; transition: opacity .2s, transform .2s; pointer-events: none; box-shadow: 0 6px 20px rgba(0,0,0,.4);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.bad { background: #8f1d1d; }
.conn { position: fixed; bottom: calc(var(--sab) + 8px); left: 50%; transform: translateX(-50%); background: #8f1d1d; color: #fff; padding: 6px 12px; border-radius: 999px; font-size: 13px; z-index: 70; }

@media (orientation: landscape) and (max-height: 500px) {
  :root { --rw: min(40px, calc((100vw - 24px) / 16)); --tw: min(32px, calc((100vw - 24px) / 20)); }
  #rack { min-height: calc(var(--rh) * 1 + 22px + var(--sab)); max-height: calc(var(--rh) * 2 + 28px + var(--sab)); }
  #bar { padding-top: 4px; }
}
