:root {
  --felt: #0b3d2a;
  --felt-2: #0f4a33;
  --felt-3: #145c40;
  --ink: #f4efe2;
  --muted: #a9c2b4;
  --line: rgba(244, 239, 226, 0.14);
  --panel: rgba(4, 28, 19, 0.55);
  --gold: #f2b705;
  --gold-ink: #2b2000;
  --red: #e5484d;
  --orange: #f08c3a;
  --blue: #6cb4ff;
  --tile: #f7f1e1;
  --tile-edge: #d9cfb5;
  --pip: #1d1d1b;
  --radius: 14px;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
  --font: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  --display: 'Bricolage Grotesque', var(--font);
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 50% -10%, var(--felt-3), transparent 70%),
    repeating-linear-gradient(45deg, rgba(255,255,255,0.012) 0 2px, transparent 2px 6px),
    var(--felt);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--gold); }
button, input, select { font: inherit; }
[hidden] { display: none !important; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 16px;
  padding: 10px max(16px, env(safe-area-inset-left));
  background: rgba(5, 30, 20, 0.85);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none; }
.brand-text { font-family: var(--display); font-weight: 600; font-size: 18px; letter-spacing: -0.01em; white-space: nowrap; }
.brand-text b { color: var(--gold); font-weight: 800; }
.brand-tile {
  width: 18px; height: 32px; border-radius: 4px; background: var(--tile);
  display: grid; grid-template-rows: 1fr 1fr; position: relative; box-shadow: 0 2px 0 var(--tile-edge);
}
.brand-tile::after { content: ''; position: absolute; left: 3px; right: 3px; top: 15px; height: 1.5px; background: var(--pip); }
.brand-tile i { display: block; width: 5px; height: 5px; border-radius: 50%; background: var(--pip); margin: auto; }
.brand-tile i:last-child { background: #c8102e; }
.nav { display: flex; gap: 4px; margin-left: 8px; }
.nav a {
  color: var(--muted); text-decoration: none; padding: 8px 12px; border-radius: 999px; font-weight: 600; font-size: 14px;
}
.nav a:hover { color: var(--ink); background: rgba(255,255,255,0.06); }
.nav a.active { color: var(--gold-ink); background: var(--gold); }
.me { margin-left: auto; display: flex; align-items: center; gap: 10px; font-size: 14px; color: var(--muted); }
.me #me-name { color: var(--ink); font-weight: 600; }

@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; gap: 8px; padding: 8px 16px; }
  .nav { order: 3; width: 100%; margin: 0; justify-content: space-between; }
  .nav a { flex: 1; text-align: center; padding: 8px 4px; }
  .brand-text { font-size: 16px; }
}

/* ---------- layout ---------- */
.app { max-width: 1100px; margin: 0 auto; padding: 20px 16px 80px; }
h1, h2, h3 { font-family: var(--display); letter-spacing: -0.02em; margin: 0 0 12px; }
h1 { font-size: clamp(26px, 5vw, 36px); font-weight: 800; }
h2 { font-size: 20px; font-weight: 600; }
.sub { color: var(--muted); margin: -4px 0 20px; }
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow);
}
.grid-2 { display: grid; grid-template-columns: 340px 1fr; gap: 20px; align-items: start; }
@media (max-width: 860px) { .grid-2 { grid-template-columns: 1fr; } }
.grid-2 > *, .game-cols > * { min-width: 0; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- forms ---------- */
label { display: block; font-size: 13px; font-weight: 600; color: var(--muted); margin: 12px 0 6px; }
input[type=text], input[type=password], input[type=number], input[type=date], select {
  width: 100%; padding: 11px 12px; border-radius: 10px; border: 1px solid var(--line);
  background: rgba(0,0,0,0.25); color: var(--ink); outline: none;
}
input:focus, select:focus { border-color: var(--gold); box-shadow: 0 0 0 3px rgba(242,183,5,0.2); }
select option { background: #0b3d2a; }
.seg { display: inline-flex; background: rgba(0,0,0,0.25); border: 1px solid var(--line); border-radius: 999px; padding: 3px; }
.seg button {
  border: 0; background: transparent; color: var(--muted); padding: 7px 14px; border-radius: 999px; cursor: pointer; font-weight: 600; font-size: 14px;
}
.seg button.on { background: var(--gold); color: var(--gold-ink); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 18px; border-radius: 10px; border: 1px solid transparent; cursor: pointer;
  font-weight: 700; background: var(--gold); color: var(--gold-ink); text-decoration: none;
  transition: transform .06s ease, filter .15s ease;
}
.btn:hover { filter: brightness(1.06); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.ghost { background: transparent; color: var(--ink); border-color: var(--line); }
.btn.ghost:hover { background: rgba(255,255,255,0.06); }
.btn.danger { background: transparent; color: #ff9b9e; border-color: rgba(229,72,77,0.4); }
.btn.sm { padding: 7px 12px; font-size: 13px; border-radius: 8px; }
.btn.block { width: 100%; }
.link { background: none; border: 0; color: var(--gold); cursor: pointer; padding: 0; font-weight: 600; }

.error { color: #ffb4b6; font-size: 14px; min-height: 20px; margin-top: 10px; }

/* ---------- login ---------- */
.login-wrap { min-height: calc(100vh - 40px); display: grid; place-items: center; padding: 20px 0; }
.login { width: min(420px, 100%); text-align: center; }
.login h1 { font-size: clamp(36px, 9vw, 52px); line-height: 0.95; margin: 18px 0 6px; }
.login h1 b { color: var(--gold); display: block; }
.login .panel { text-align: left; margin-top: 22px; }
.hero-tiles { display: flex; justify-content: center; gap: 8px; transform: rotate(-4deg); }
.hero-tiles .tile:nth-child(2) { transform: translateY(-8px) rotate(6deg); }

/* ---------- tiles ---------- */
.tile {
  --u: 26px;
  display: inline-grid; grid-template-columns: var(--u) var(--u); grid-template-rows: var(--u);
  background: linear-gradient(180deg, #fffaf0, var(--tile));
  border-radius: calc(var(--u) * 0.22);
  box-shadow: 0 2px 0 var(--tile-edge), 0 4px 10px rgba(0,0,0,0.35);
  position: relative; flex: none;
}
.tile.v { grid-template-columns: var(--u); grid-template-rows: var(--u) var(--u); }
.tile::after { content: ''; position: absolute; background: rgba(29,29,27,0.75); }
.tile:not(.v)::after { left: 50%; top: 14%; bottom: 14%; width: 1.5px; transform: translateX(-50%); }
.tile.v::after { top: 50%; left: 14%; right: 14%; height: 1.5px; transform: translateY(-50%); }
.half { display: grid; grid-template: repeat(3, 1fr) / repeat(3, 1fr); padding: 16%; }
.half i { width: 72%; height: 72%; margin: auto; border-radius: 50%; background: var(--pip); box-shadow: inset 0 -1px 0 rgba(255,255,255,0.25); }
.half i.e { visibility: hidden; }
.tile.lg { --u: 38px; }
.tile.md { --u: 30px; }
.tile.sm { --u: 18px; }
.tile.xs { --u: 13px; }
.tile.back { background: linear-gradient(135deg, #1e6b4b, #0d4a33); box-shadow: 0 2px 0 #072a1d, 0 3px 8px rgba(0,0,0,.35); }
.tile.back::after { display: none; }

/* ---------- lobby ---------- */
.table-list { display: grid; gap: 10px; }
.table-card {
  display: flex; align-items: center; gap: 14px; padding: 14px 16px; border-radius: 12px;
  background: rgba(255,255,255,0.04); border: 1px solid var(--line);
}
.table-card .t-name { font-weight: 700; }
.badge {
  display: inline-block; font-size: 11px; font-weight: 700; letter-spacing: .04em; text-transform: uppercase;
  padding: 3px 8px; border-radius: 999px; background: rgba(255,255,255,0.08); color: var(--muted);
}
.badge.wait { background: rgba(108,180,255,0.15); color: var(--blue); }
.badge.live { background: rgba(242,183,5,0.15); color: var(--gold); }
.badge.online { background: rgba(108,180,255,0.15); color: var(--blue); }
.badge.physical { background: rgba(240,140,58,0.15); color: var(--orange); }
.empty { text-align: center; padding: 30px 10px; color: var(--muted); }

/* ---------- table / game ---------- */
.game { display: grid; gap: 14px; }
.players { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.pcard {
  padding: 10px 12px; border-radius: 12px; background: rgba(255,255,255,0.04); border: 1px solid var(--line);
  position: relative; transition: border-color .2s, box-shadow .2s;
}
.pcard.turn { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(242,183,5,0.25), 0 0 24px rgba(242,183,5,0.15); }
.pcard.you .pname::after { content: ' (you)'; color: var(--muted); font-weight: 500; }
.pname { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pmeta { font-size: 12px; color: var(--muted); margin-top: 2px; display: flex; gap: 8px; align-items: center; }
.dots { display: flex; gap: 4px; margin-top: 8px; }
.dots span { width: 12px; height: 12px; border-radius: 50%; border: 1.5px solid rgba(244,239,226,0.35); }
.dots span.on { background: var(--gold); border-color: var(--gold); }
.turn-tag { position: absolute; top: 8px; right: 10px; font-size: 11px; font-weight: 800; color: var(--gold); letter-spacing: .05em; }
.order { font-size: 13px; color: var(--muted); }

.board {
  min-height: 190px; padding: 18px; border-radius: 18px;
  background: radial-gradient(ellipse at center, #136246, #0b4430 80%);
  border: 1px solid rgba(255,255,255,0.08); box-shadow: inset 0 2px 30px rgba(0,0,0,0.35);
  display: flex; flex-direction: column; gap: 12px;
}
.chain { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 4px 3px; flex: 1; align-content: center; }
.chain .tile.last { outline: 2px solid var(--gold); outline-offset: 2px; }
.chain .end-btn {
  border: 2px dashed var(--gold); background: rgba(242,183,5,0.12); color: var(--gold);
  border-radius: 10px; padding: 8px 10px; font-weight: 800; cursor: pointer; align-self: stretch;
}
.ends { display: flex; justify-content: space-between; font-size: 13px; color: var(--muted); }
.ends b { color: var(--ink); font-size: 16px; }
.status-line { text-align: center; font-weight: 700; min-height: 24px; }
.status-line.you { color: var(--gold); }

.hand-wrap { padding: 14px; border-radius: 16px; background: rgba(0,0,0,0.22); border: 1px solid var(--line); }
.hand { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.hand .tile { cursor: default; opacity: .55; transition: transform .12s, opacity .12s; }
.hand .tile.ok { opacity: 1; cursor: pointer; }
.hand .tile.ok:hover { transform: translateY(-4px); }
.hand .tile.sel { transform: translateY(-8px); outline: 3px solid var(--gold); outline-offset: 3px; }
.hand.myturn .tile:not(.ok) { opacity: .35; }
.hand:not(.myturn) .tile { opacity: .9; }
.side-choice { display: flex; gap: 10px; justify-content: center; margin-top: 12px; }

.log { font-size: 13px; color: var(--muted); max-height: 180px; overflow: auto; display: grid; gap: 4px; }
.log div:last-child { color: var(--ink); }

.game-cols { display: grid; grid-template-columns: 1fr 260px; gap: 14px; align-items: start; }
@media (max-width: 860px) { .game-cols { grid-template-columns: 1fr; } }

/* result overlay */
.overlay {
  position: fixed; inset: 0; z-index: 40; background: rgba(0,0,0,0.55); display: grid; place-items: center; padding: 16px;
  animation: fade .2s ease;
}
.modal { width: min(560px, 100%); max-height: 90vh; overflow: auto; background: #0d3f2c; border: 1px solid var(--line); border-radius: 18px; padding: 22px; box-shadow: var(--shadow); }
.modal h2 { font-size: 26px; }
.reveal { display: grid; gap: 10px; margin: 14px 0; }
.reveal-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.reveal-row .who { width: 110px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reveal-row .tiles { display: flex; gap: 4px; flex-wrap: wrap; }
.big-result { font-family: var(--display); font-size: 34px; font-weight: 800; line-height: 1.05; margin: 6px 0 12px; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

/* outcome chips */
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; font-size: 13px; font-weight: 700; background: rgba(255,255,255,0.07); }
.chip.win { background: rgba(242,183,5,0.18); color: var(--gold); }
.chip.jailed { background: rgba(240,140,58,0.16); color: var(--orange); }
.chip.antiman { background: rgba(229,72,77,0.2); color: #ff8f93; }
.chip.none { color: var(--muted); }

/* ---------- scores ---------- */
.podium { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin-bottom: 16px; }
@media (max-width: 600px) { .podium { grid-template-columns: 1fr; } }
.pod { padding: 16px; border-radius: 14px; background: rgba(255,255,255,0.04); border: 1px solid var(--line); }
.pod .rank { font-family: var(--display); font-size: 13px; color: var(--muted); font-weight: 600; }
.pod .nm { font-family: var(--display); font-size: 22px; font-weight: 800; margin: 2px 0 6px; }
.pod.first { border-color: rgba(242,183,5,0.5); background: linear-gradient(160deg, rgba(242,183,5,0.12), rgba(255,255,255,0.03)); }
.pod .stat-line { display: flex; gap: 14px; font-size: 13px; color: var(--muted); }
.pod .stat-line b { color: var(--ink); font-size: 18px; display: block; }

.tbl-wrap { overflow-x: auto; }
table.lb { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.lb th, .lb td { padding: 10px 10px; text-align: right; border-bottom: 1px solid var(--line); white-space: nowrap; }
.lb th { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 700; }
.lb th:nth-child(-n+2), .lb td:nth-child(-n+2) { text-align: left; }
.lb td.w { color: var(--gold); font-weight: 800; }
.lb td.j { color: var(--orange); font-weight: 700; }
.lb td.a { color: #ff8f93; font-weight: 700; }
.lb tr:hover td { background: rgba(255,255,255,0.03); }
.filters { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }

/* ---------- record ---------- */
.seat-list { display: grid; gap: 8px; }
.seat-row { display: grid; grid-template-columns: 28px 1fr auto; gap: 8px; align-items: center; }
.seat-no { width: 28px; height: 28px; border-radius: 50%; background: rgba(255,255,255,0.08); display: grid; place-items: center; font-weight: 700; font-size: 13px; }
.games-tbl { width: 100%; border-collapse: separate; border-spacing: 0 8px; }
.games-tbl th { font-size: 12px; color: var(--muted); text-align: center; font-weight: 700; padding: 0 4px; }
.games-tbl th:first-child { text-align: left; }
.games-tbl td { padding: 0 4px; vertical-align: middle; }
.games-tbl input[type=number] { text-align: center; padding: 9px 2px; min-width: 44px; }
.games-tbl .gno { color: var(--muted); font-weight: 700; width: 34px; }
.games-tbl .res { font-size: 13px; padding: 0 4px 6px; }
.games-tbl .res .chip { margin: 0 4px 4px 0; }
.games-tbl tr.res-row td { border-bottom: 1px solid var(--line); }
.games-tbl .res.bad { color: #ffb4b6; }
.games-tbl .x { background: none; border: 0; color: var(--muted); cursor: pointer; font-size: 18px; }
.hint { font-size: 13px; color: var(--muted); background: rgba(255,255,255,0.04); border-radius: 10px; padding: 10px 12px; margin-top: 10px; line-height: 1.45; }
.hint b { color: var(--ink); }

/* ---------- history ---------- */
.hist { display: grid; gap: 10px; }
.hist-item { padding: 12px 14px; border-radius: 12px; background: rgba(255,255,255,0.04); border: 1px solid var(--line); }
.hist-item .top { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; font-size: 13px; color: var(--muted); margin-bottom: 8px; }
.hist-item .chips { display: flex; gap: 6px; flex-wrap: wrap; }

/* toast */
.toast {
  position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%) translateY(20px); opacity: 0; pointer-events: none;
  background: #111; color: #fff; padding: 10px 16px; border-radius: 10px; font-weight: 600; transition: all .2s; z-index: 60; max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.err { background: #7a1d21; }
