:root {
  --bg: #0f0b16;
  --panel: #18121f;
  --panel2: #211829;
  --line: #2e2338;
  --text: #ece6f3;
  --muted: #9c8fae;
  --accent: #a46bf0;
  --accent2: #6d3bb3;
  --good: #3ecf8e;
  --bad: #ff5d73;
  --warn: #ffb547;
  --felt: #164a35;
  --radius: 12px;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--text); }
body { min-height: 100vh; display: grid; grid-template-rows: auto 1fr auto; grid-template-columns: 1fr 300px; grid-template-areas: "top top" "main chat" "foot chat"; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.wrap { padding: 20px 24px; max-width: 1200px; width: 100%; margin: 0 auto; }
main { grid-area: main; min-width: 0; }
.foot { grid-area: foot; color: var(--muted); font-size: 13px; display: flex; gap: 10px; flex-wrap: wrap; border-top: 1px solid var(--line); }

/* top bar */
.topbar { grid-area: top; display: flex; align-items: center; gap: 20px; padding: 10px 20px; background: var(--panel); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 5; }
.brand { color: var(--text); font-size: 19px; letter-spacing: .3px; }
.brand b { color: var(--accent); }
.logo { color: var(--accent); }
#nav { display: flex; gap: 4px; flex: 1; flex-wrap: wrap; }
#nav a { color: var(--muted); padding: 6px 10px; border-radius: 8px; }
#nav a.on, #nav a:hover { color: var(--text); background: var(--panel2); text-decoration: none; }
#userbox { display: flex; align-items: center; gap: 10px; font-size: 14px; }
.bal { background: var(--panel2); border: 1px solid var(--line); padding: 6px 10px; border-radius: 8px; font-variant-numeric: tabular-nums; }
.bal b { color: var(--accent); }

/* generic */
h1 { font-size: 24px; margin: 4px 0 16px; }
h2 { font-size: 18px; margin: 22px 0 10px; }
h3 { font-size: 15px; margin: 14px 0 8px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .6px; }
.muted { color: var(--muted); }
.small { font-size: 12.5px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; word-break: break-all; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.grid { display: grid; gap: 14px; }
.g2 { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
.g3 { grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.spacer { flex: 1; }
.pill { display: inline-block; padding: 2px 8px; border-radius: 99px; font-size: 12px; background: var(--panel2); border: 1px solid var(--line); color: var(--muted); }
.pill.good { color: var(--good); border-color: #1f5a41; }
.pill.bad { color: var(--bad); border-color: #6a2531; }
.pill.warn { color: var(--warn); border-color: #6a4a1a; }
.good { color: var(--good); } .bad { color: var(--bad); } .warn { color: var(--warn); }

button, .btn { font: inherit; cursor: pointer; border: 1px solid var(--line); background: var(--panel2); color: var(--text); padding: 8px 14px; border-radius: 9px; }
button:hover:not(:disabled) { border-color: var(--accent2); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary { background: linear-gradient(180deg, var(--accent), var(--accent2)); border-color: transparent; color: #fff; font-weight: 600; }
button.danger { border-color: #6a2531; color: var(--bad); }
button.ghost { background: transparent; }
button.big { padding: 12px 22px; font-size: 16px; }
input, select { font: inherit; background: #0c0912; color: var(--text); border: 1px solid var(--line); border-radius: 9px; padding: 8px 10px; min-width: 0; }
input:focus, select:focus { outline: 2px solid var(--accent2); outline-offset: 0; }
label { display: grid; gap: 5px; font-size: 13px; color: var(--muted); }
form.stack, .stack { display: grid; gap: 12px; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; color: var(--muted); font-weight: 500; font-size: 12px; text-transform: uppercase; letter-spacing: .5px; padding: 6px 8px; border-bottom: 1px solid var(--line); }
td { padding: 8px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tr:hover td { background: #1b1423; }
.empty { color: var(--muted); padding: 20px; text-align: center; }
.tabs { display: flex; gap: 6px; margin-bottom: 12px; overflow-x: auto; max-width: 100%; }
.tabs button { white-space: nowrap; flex: none; }
.tabs button.on { border-color: var(--accent); color: var(--accent); }

/* lobby tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }
.tile { background: linear-gradient(160deg, #241933, #17111e); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px; display: grid; gap: 8px; cursor: pointer; transition: transform .12s, border-color .12s; }
.tile:hover { transform: translateY(-2px); border-color: var(--accent2); }
.tile .ico { font-size: 30px; }
.tile .name { font-weight: 700; }
.tile .desc { font-size: 12.5px; color: var(--muted); }

/* modal */
.modal-bg { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: grid; place-items: center; z-index: 20; padding: 16px; }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 22px; width: min(460px, 100%); max-height: 90vh; overflow: auto; }

/* toasts */
#toasts { position: fixed; right: 320px; bottom: 20px; display: grid; gap: 8px; z-index: 30; }
.toast { background: var(--panel2); border: 1px solid var(--line); border-left: 4px solid var(--accent); padding: 10px 14px; border-radius: 10px; max-width: 360px; font-size: 14px; animation: pop .2s; }
.toast.err { border-left-color: var(--bad); }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } }

/* chat */
.chat { grid-area: chat; background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; height: calc(100vh - 53px); position: sticky; top: 53px; }
.chat .msgs { flex: 1; overflow-y: auto; padding: 12px; display: grid; gap: 6px; align-content: start; font-size: 13.5px; }
.chat .msgs b { color: var(--accent); margin-right: 4px; }
.chat form { display: flex; gap: 6px; padding: 10px; border-top: 1px solid var(--line); }
.chat form input { flex: 1; }
.chat h3 { margin: 12px 12px 0; }

/* match */
.match-head { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.vs { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.player { background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 8px 12px; min-width: 120px; }
.player.me { border-color: var(--accent2); }
.player.win { border-color: var(--good); box-shadow: 0 0 0 1px var(--good) inset; }
.stage { min-height: 300px; display: grid; place-items: center; padding: 24px; }
.result-banner { font-size: 22px; font-weight: 700; text-align: center; margin: 10px 0; }
.timer { font-variant-numeric: tabular-nums; }
.fair { font-size: 12.5px; }
.fair dt { color: var(--muted); margin-top: 6px; }
.fair dd { margin: 2px 0 0; }

/* coin */
.coin { width: 140px; height: 140px; border-radius: 50%; display: grid; place-items: center; font-size: 22px; font-weight: 800; background: radial-gradient(circle at 35% 30%, #f3d27a, #b8860b); color: #3b2a00; box-shadow: 0 10px 30px rgba(0,0,0,.4); animation: flip 1.2s ease-out; }
@keyframes flip { 0% { transform: rotateY(0) scale(.6); } 70% { transform: rotateY(1440deg) scale(1.1); } 100% { transform: rotateY(1440deg) scale(1); } }
.dice-rounds { display: grid; gap: 8px; text-align: center; }
.die { display: inline-grid; place-items: center; width: 70px; height: 70px; border-radius: 14px; background: #f5f0ff; color: #2a1640; font-size: 26px; font-weight: 800; margin: 0 8px; animation: pop .4s; }

/* cards */
.card { display: inline-grid; place-items: center; width: 52px; height: 74px; border-radius: 7px; background: #fbfbfd; color: #111; font-weight: 700; font-size: 20px; margin: 2px; box-shadow: 0 2px 6px rgba(0,0,0,.35); line-height: 1; }
.card.red { color: #d0213b; }
.card.back { background: repeating-linear-gradient(45deg, #5b2a86, #5b2a86 6px, #46206a 6px, #46206a 12px); }
.card.small { width: 38px; height: 54px; font-size: 15px; }
.card.big { width: 90px; height: 128px; font-size: 34px; }

/* rps */
.rps-choices { display: flex; gap: 14px; justify-content: center; }
.rps-choices button { font-size: 40px; width: 96px; height: 96px; border-radius: 18px; }
.rps-choices button.sel { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent) inset; }

/* chess */
.chess-wrap { display: grid; grid-template-columns: auto 220px; gap: 18px; align-items: start; }
.board { display: grid; grid-template-columns: repeat(8, minmax(36px, 58px)); grid-template-rows: repeat(8, minmax(36px, 58px)); border: 3px solid #3a2a4a; border-radius: 6px; overflow: hidden; user-select: none; }
.sq { display: grid; place-items: center; font-size: clamp(26px, 4.4vw, 42px); cursor: pointer; position: relative; }
.sq.l { background: #e9dcc6; } .sq.d { background: #a27c5b; }
.sq.sel { box-shadow: inset 0 0 0 4px var(--accent); }
.sq.tgt::after { content: ''; width: 26%; height: 26%; border-radius: 50%; background: rgba(60, 20, 90, .45); position: absolute; }
.sq.last { background-image: linear-gradient(rgba(164,107,240,.35), rgba(164,107,240,.35)); }
.sq .pw { color: #fff; text-shadow: 0 0 2px #000, 0 0 1px #000; }
.sq .pb { color: #111; }
.sq .coord { position: absolute; left: 3px; bottom: 1px; font-size: 10px; color: rgba(0,0,0,.45); }
.clock { font-size: 26px; font-variant-numeric: tabular-nums; background: var(--panel2); border: 1px solid var(--line); padding: 8px 12px; border-radius: 10px; text-align: center; }
.clock.run { border-color: var(--accent); color: var(--accent); }
.clock.low { color: var(--bad); }
.movelist { max-height: 220px; overflow: auto; font-family: ui-monospace, monospace; font-size: 13px; }

/* poker */
.table { background: radial-gradient(ellipse at center, #1f6a4a, var(--felt) 70%); border: 10px solid #3a2414; border-radius: 160px; padding: 26px; min-height: 340px; display: grid; grid-template-rows: auto 1fr auto; gap: 12px; }
.seats { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.seat { background: rgba(0,0,0,.35); border: 1px solid rgba(255,255,255,.12); border-radius: 12px; padding: 8px; text-align: center; font-size: 13px; }
.seat.act { border-color: var(--warn); box-shadow: 0 0 12px rgba(255,181,71,.45); }
.seat.folded { opacity: .45; }
.seat.out { opacity: .25; }
.seat.me { border-color: var(--accent); }
.seat .chips { font-weight: 700; font-variant-numeric: tabular-nums; }
.seat .betchip { color: var(--warn); }
.board-cards { text-align: center; min-height: 80px; }
.pot { text-align: center; font-weight: 700; color: #ffe08a; }
.actionbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; justify-content: center; margin-top: 14px; }
.actionbar input[type=range] { width: 180px; }
.log { font-size: 12.5px; color: var(--muted); max-height: 150px; overflow: auto; }
.dealer { display: inline-block; width: 18px; height: 18px; border-radius: 50%; background: #fff; color: #000; font-size: 11px; font-weight: 800; line-height: 18px; }

.qr { background: #fff; padding: 8px; border-radius: 10px; width: 200px; height: 200px; }
.notice { background: #2a1d10; border: 1px solid #6a4a1a; color: #ffd9a1; padding: 10px 14px; border-radius: 10px; font-size: 13.5px; }

@media (max-width: 980px) {
  body { grid-template-columns: 1fr; grid-template-areas: "top" "main" "chat" "foot"; }
  .chat { position: static; height: 360px; border-left: 0; border-top: 1px solid var(--line); }
  #toasts { right: 16px; }
  .chess-wrap { grid-template-columns: 1fr; }
  .wrap { padding: 16px; }
  .topbar { flex-wrap: wrap; gap: 8px 12px; padding: 10px 16px; }
  #nav { order: 3; flex: 1 1 100%; flex-wrap: nowrap; overflow-x: auto; }
  #nav a { white-space: nowrap; }
  #userbox { margin-left: auto; }
  .table { border-radius: 40px; padding: 16px; }
}
@media (max-width: 600px) {
  table { font-size: 13px; }
  td, th { padding: 6px 4px; }
  .panel { padding: 12px; overflow-x: auto; }
}
