:root {
  --bg: #0d1626;
  --bg2: #111d33;
  --panel: #16243d;
  --panel2: #1c2d4a;
  --line: #2a3d5e;
  --text: #e6edf7;
  --muted: #8da2c0;
  --accent: #f5c451;
  --accent-ink: #1b1405;
  --good: #3fcf8e;
  --bad: #ff6b6b;
  --card: #fbf8f1;
  --card-ink: #1d2433;
  --F: #e5484d;
  --G: #12a594;
  --R: #4f5d75;
  --R-on-dark: #9aaac4;
  --D: #8e4ec6;
  --S: #f5c451;
  --radius: 12px;
  --cw: 58px;
  --ch: 84px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: radial-gradient(1200px 600px at 70% -10%, #1d3358 0%, transparent 60%), var(--bg);
  background-attachment: fixed;
  color: var(--text);
  font: 15px/1.45 Inter, system-ui, -apple-system, sans-serif;
  min-height: 100vh;
}
h1, h2, h3 { font-family: Fraunces, Georgia, serif; font-weight: 600; margin: 0 0 .4em; line-height: 1.2; }
h2 { font-size: 1.25rem; }
h3 { font-size: 1.05rem; }
p { margin: 0 0 .6em; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.grow { flex: 1; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.row.between { justify-content: space-between; }
.row.center { justify-content: center; flex-wrap: wrap; }

button {
  font: inherit; color: var(--text); background: var(--panel2); border: 1px solid var(--line);
  border-radius: 10px; padding: 8px 14px; cursor: pointer; transition: background .15s, transform .1s, border-color .15s;
}
button:hover:not(:disabled) { background: #24395c; border-color: #3b5580; }
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: .45; cursor: not-allowed; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.primary:hover:not(:disabled) { background: #ffd56b; }
button.ghost { background: transparent; border-color: transparent; color: var(--muted); }
button.ghost:hover:not(:disabled) { color: var(--text); background: var(--panel2); }
button.big { padding: 12px 20px; font-size: 1rem; }
button.small { padding: 4px 10px; font-size: .85rem; }
button.tiny { padding: 1px 8px; font-size: .78rem; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

input {
  font: inherit; color: var(--text); background: var(--bg2); border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; min-width: 0;
}
input[type=range] { padding: 0; accent-color: var(--accent); flex: 1; }

/* ---- Top bar ---- */
.topbar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: rgba(13, 22, 38, .7); backdrop-filter: blur(8px); position: sticky; top: 0; z-index: 5; }
.brand { display: flex; align-items: center; gap: 8px; font-family: Fraunces, Georgia, serif; font-weight: 700; font-size: 1.1rem; }
.room-code { background: transparent; border-color: var(--line); padding: 4px 10px; }
.room-code b { letter-spacing: .12em; color: var(--accent); }
.offline { color: var(--bad); font-size: .85rem; }

/* ---- Landing ---- */
.landing { max-width: 920px; margin: 0 auto; padding: 48px 16px; display: grid; grid-template-columns: 1.2fr 1fr; gap: 32px; align-items: center; }
.hero h1 { font-size: clamp(2.4rem, 6vw, 3.6rem); }
.lede { color: var(--muted); font-size: 1.1rem; max-width: 34ch; }
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; }
.join-panel { display: flex; flex-direction: column; gap: 10px; }
.join-panel label { color: var(--muted); font-size: .9rem; }
.or { text-align: center; color: var(--muted); font-size: .85rem; margin: 6px 0; }
.code-input { text-transform: uppercase; letter-spacing: .2em; width: 120px; text-align: center; font-weight: 600; }
.center-screen { display: grid; place-items: center; min-height: 60vh; }

/* ---- Lobby ---- */
.lobby { max-width: 1100px; margin: 0 auto; padding: 20px 16px; display: grid; grid-template-columns: 320px 1fr; gap: 20px; align-items: start; }
.players { list-style: none; padding: 0; margin: 0 0 12px; counter-reset: p; }
.players li { display: flex; align-items: center; gap: 8px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.players li::before { counter-increment: p; content: counter(p); color: var(--muted); width: 1.2em; font-size: .85rem; }
.players li button { margin-left: auto; }
.rename { margin-top: 12px; }
.rename input { flex: 1; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #4a5a74; flex: none; display: inline-block; }
.dot.on { background: var(--good); box-shadow: 0 0 6px var(--good); }
.mission-grid { display: grid; grid-template-columns: repeat(10, 1fr); gap: 6px; margin: 8px 0 16px; }
.mcell { position: relative; padding: 8px 0; text-align: center; font-weight: 600; font-variant-numeric: tabular-nums; }
.mcell i { position: absolute; top: 1px; right: 4px; font-style: normal; font-size: .7rem; color: var(--good); }
.mcell i.muted { color: var(--muted); }
.mcell.done { border-color: #2f6b52; }
.mcell.sel { outline: 2px solid var(--accent); }
.mission-detail { border-top: 1px solid var(--line); padding-top: 14px; }
.mnum { display: inline-grid; place-items: center; min-width: 1.8em; height: 1.8em; border-radius: 8px; background: var(--accent); color: var(--accent-ink); font-family: Inter, sans-serif; font-size: .85em; font-weight: 700; margin-right: 4px; }
.rules-chips { display: flex; flex-wrap: wrap; gap: 6px; margin: 6px 0 10px; }
.rchip { background: var(--bg2); border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px; font-size: .8rem; color: var(--muted); }
.random-box { border-top: 1px solid var(--line); margin-top: 16px; padding-top: 14px; }
.random-box .row { margin-bottom: 10px; }

/* ---- Cards ---- */
.card {
  width: var(--cw); height: var(--ch); border-radius: 8px; background: var(--card); color: var(--card-ink); border: 1px solid #d9d2c3;
  position: relative; display: grid; place-items: center; padding: 0; flex: none; box-shadow: 0 2px 6px rgba(0,0,0,.35); font-family: Inter, sans-serif;
}
.card .cv { position: absolute; top: 4px; left: 6px; font-weight: 700; font-size: 1.05rem; line-height: 1; }
.card .cv2 { top: auto; left: auto; bottom: 4px; right: 6px; transform: rotate(180deg); }
.card .cg { font-size: 1.7rem; line-height: 1; display: grid; place-items: center; }
.suit-icon { width: 1em; height: 1em; display: inline-block; vertical-align: -0.12em; flex: none; }
.chip .suit-icon { width: .9em; height: .9em; margin-right: 1px; }
.card.s-F { color: var(--F); } .card.s-G { color: var(--G); } .card.s-R { color: var(--R); } .card.s-D { color: var(--D); }
.card.s-S { background: linear-gradient(160deg, #26324a, #141c2c); color: var(--S); border-color: #3a4a6a; }
.card.mini { --cw: 34px; --ch: 48px; width: var(--cw); height: var(--ch); border-radius: 6px; }
.card.mini .cv { font-size: .78rem; top: 2px; left: 4px; }
.card.mini .cv2 { display: none; }
.card.mini .cg { font-size: 1.05rem; margin-top: 8px; }
.card.played { opacity: .4; }
.card.is-task::after { content: ''; position: absolute; inset: -3px; border-radius: 10px; border: 2px dashed rgba(245,196,81,.75); pointer-events: none; }
.chip { display: inline-flex; align-items: center; gap: 1px; padding: 0 6px; border-radius: 6px; background: var(--card); font-weight: 700; font-size: .85em; line-height: 1.5; }
.chip.s-F { color: var(--F); } .chip.s-G { color: var(--G); } .chip.s-R { color: var(--R); } .chip.s-D { color: var(--D); }
.chip.s-S { background: #1e283b; color: var(--S); border: 1px solid #3a4a6a; }
.s { font-weight: 600; } .s-F.s { color: var(--F); } .s-G.s { color: var(--G); } .s-R.s { color: var(--R-on-dark); } .s-D.s { color: var(--D); } .s-S.s { color: var(--S); }
.tok { display: inline-grid; place-items: center; min-width: 20px; height: 20px; padding: 0 4px; border-radius: 999px; background: var(--accent); color: var(--accent-ink); font-size: .75rem; font-weight: 700; font-family: Inter, sans-serif; }

/* ---- Table ---- */
.table {
  display: grid; gap: 14px; padding: 14px 16px 24px; max-width: 1280px; margin: 0 auto;
  grid-template-columns: 1fr 280px;
  grid-template-areas: 'mission log' 'others log' 'board log' 'me log';
}
.mission-bar { grid-area: mission; display: flex; gap: 16px; justify-content: space-between; align-items: flex-start; }
.mission-bar h2 { margin: 0 0 2px; }
.mission-bar .rules-chips { justify-content: flex-end; margin: 0; max-width: 50%; }
.others { grid-area: others; display: grid; grid-template-columns: repeat(auto-fit, minmax(210px, 1fr)); gap: 10px; }
.board { grid-area: board; background: linear-gradient(180deg, #14304a, #10233a); border: 1px solid var(--line); border-radius: 18px; padding: 16px; min-height: 230px; display: flex; flex-direction: column; gap: 12px; }
.me { grid-area: me; display: grid; grid-template-columns: 260px 1fr; gap: 12px; align-items: start; }
.log { grid-area: log; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; display: flex; flex-direction: column; max-height: calc(100vh - 90px); position: sticky; top: 70px; }
.table.log-closed { grid-template-columns: 1fr auto; }
.log-closed .log { align-self: start; }
.log-toggle { all: unset; display: flex; align-items: center; justify-content: space-between; gap: 10px; cursor: pointer; width: 100%; border-radius: 6px; }
.log-toggle h3 { margin: 0; white-space: nowrap; }
.log.open .log-toggle { margin-bottom: 8px; }
.log-toggle:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; }
.log-toggle .chev { color: var(--muted); font-size: .9rem; }
.log-toggle:hover .chev { color: var(--text); }
.log-list { overflow-y: auto; font-size: .85rem; color: #c7d3e6; }
.log-list p { margin: 0 0 6px; }

.seat { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 12px; display: flex; flex-direction: column; gap: 6px; transition: border-color .2s, box-shadow .2s; }
.seat.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent), 0 0 18px rgba(245,196,81,.18); }
.seat-head { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.seat-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 12ch; }
.seat-stats { display: flex; gap: 10px; color: var(--muted); font-size: .82rem; flex-wrap: wrap; align-items: center; }
.seat-row { display: flex; gap: 10px; align-items: flex-start; }
.badge { font-size: .72rem; padding: 1px 8px; border-radius: 999px; background: var(--panel2); border: 1px solid var(--line); color: var(--muted); }
.badge.lead { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 700; }
.badge.chosen { background: #3a2346; border-color: #6c3b84; color: #e3c4f2; }
.badge.offline { background: #3a2a14; border-color: #7a5a24; color: #ffd28a; }
.off { color: #ffd28a; font-weight: 400; }
.badge.yes { background: #1d4a38; color: #9ff0c8; border-color: #2f6b52; }
.badge.no { background: #4a1d24; color: #ffb3b3; border-color: #7a2e38; }
.radio-slot { width: 64px; min-height: 52px; border: 1px dashed var(--line); border-radius: 8px; display: grid; place-items: center; text-align: center; padding: 2px; flex: none; }
.radio-slot.used { border-style: solid; border-color: #3b5580; gap: 2px; }
.radio-slot .pos { font-size: .68rem; color: var(--accent); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.radio-icon { font-size: .7rem; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; }
.seat-tasks { display: flex; flex-wrap: wrap; gap: 6px; }
.task-wrap { position: relative; }
.task-wrap .tok { position: absolute; top: -7px; right: -7px; min-width: 18px; height: 18px; font-size: .68rem; }
.task-wrap .tick { position: absolute; inset: 0; display: grid; place-items: center; color: var(--good); font-weight: 900; font-size: 1.4rem; text-shadow: 0 0 4px #0008; }
.card.task.done { opacity: .5; }

.status { font-size: 1rem; text-align: center; padding: 6px 10px; border-radius: 10px; background: rgba(0,0,0,.18); }
.status.mine { background: rgba(245,196,81,.14); color: #ffe4a1; }
.prompt { display: grid; place-items: center; gap: 10px; flex: 1; }
.trick { display: flex; justify-content: center; gap: 18px; flex-wrap: wrap; align-items: flex-end; flex: 1; min-height: 120px; position: relative; padding-bottom: 22px; }
.trick .play { display: flex; flex-direction: column; align-items: center; gap: 6px; font-size: .82rem; color: var(--muted); }
.trick .play.fresh { animation: drop .25s ease-out; }
.trick.last .play { opacity: .6; }
.trick.last .play.winner { opacity: 1; }
.trick.last .play.winner .card { box-shadow: 0 0 0 3px var(--accent), 0 4px 14px rgba(0,0,0,.4); }
.trick-caption { position: absolute; bottom: 0; left: 0; right: 0; text-align: center; color: var(--muted); font-size: .85rem; margin: 0; }
@keyframes drop { from { transform: translateY(-14px); opacity: 0; } to { transform: none; opacity: 1; } }
.open-tasks { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; justify-content: center; border-top: 1px solid rgba(255,255,255,.06); padding-top: 10px; }
.ot { display: inline-flex; gap: 4px; align-items: center; }

.task-pool { display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; align-items: flex-start; flex: 1; padding-top: 8px; }
.pool-item { display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 10px 10px 6px; border-radius: 12px; background: rgba(0,0,0,.18); border: 1px solid transparent; }
.pool-item .owner { font-size: .8rem; color: var(--muted); }
button.pool-item:not(:disabled) { border-color: var(--accent); }
button.pool-item:not(:disabled) .owner { color: var(--accent); font-weight: 600; }
.pool-item.sel { background: rgba(245,196,81,.15); }
.pool-item.taken { opacity: .8; }
button.pool-item:disabled { opacity: 1; cursor: default; }
.pool-item .card.mini { --cw: 46px; --ch: 66px; }
.pool-item .card.mini .cg { font-size: 1.4rem; }

.result { text-align: center; padding: 10px; flex: 1; display: flex; flex-direction: column; gap: 8px; align-items: center; justify-content: center; }
.result h2 { font-size: 1.8rem; }
.result.won h2 { color: var(--good); }
.result.lost h2 { color: var(--bad); }
.final-trick { width: 100%; }
.final-trick .trick { min-height: 0; flex: none; }
.final-trick > p { margin: 4px 0 8px; }
.history .hist-row { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; padding: 6px 0; border-bottom: 1px solid rgba(255,255,255,.06); }
.hist-n { width: 1.6em; color: var(--muted); font-variant-numeric: tabular-nums; text-align: right; }
.hist-play { display: inline-flex; align-items: center; gap: 4px; padding: 2px 6px 2px 2px; border-radius: 8px; }
.hist-play.win { background: rgba(245,196,81,.16); box-shadow: inset 0 0 0 1px var(--accent); }
.hist-name { font-size: .8rem; color: var(--muted); }
.hist-play.win .hist-name { color: var(--text); font-weight: 600; }
.hist-winner { margin-left: auto; font-size: .85rem; color: var(--muted); }
.chip.is-task { outline: 2px dashed rgba(245,196,81,.85); outline-offset: 1px; }
.reveal { margin-top: 10px; text-align: left; width: 100%; max-width: 640px; font-size: .9rem; }
.reveal summary { cursor: pointer; color: var(--muted); }
.reveal-row { display: flex; gap: 4px; flex-wrap: wrap; align-items: center; margin: 6px 0; }
.reveal-row b { min-width: 8ch; }

.hand-wrap { display: flex; flex-direction: column; gap: 10px; }
.hand { display: flex; flex-wrap: wrap; gap: 6px; }
.hand-card { position: relative; }
.hand .card { transition: transform .12s, opacity .15s, box-shadow .15s; }
.hand .card.legal { cursor: pointer; transform: translateY(-6px); box-shadow: 0 0 0 2px var(--accent), 0 6px 14px rgba(0,0,0,.4); }
.hand .card.legal:hover { transform: translateY(-12px); }
.hand .card.dim { opacity: .38; }
.hand .card.radioed { outline: 2px solid #5aa3ff; outline-offset: 2px; }
.hand .card.passing { opacity: .4; }
.radio-label { position: absolute; left: 0; right: 0; bottom: -18px; text-align: center; font-size: .66rem; color: var(--accent); font-weight: 700; text-transform: uppercase; }
.hand-actions { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; min-height: 34px; padding-top: 8px; }
.backup { display: inline-flex; gap: 6px; align-items: center; color: var(--muted); font-size: .85rem; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px); background: #26385a; color: var(--text); border: 1px solid #3b5580;
  padding: 10px 16px; border-radius: 10px; opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 20; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

dialog#rules { max-width: 680px; width: calc(100vw - 32px); max-height: calc(100vh - 48px); background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 16px; padding: 24px 28px; }
dialog#rules::backdrop { background: rgba(5, 10, 20, .7); }
dialog#rules h3 { margin-top: 1.2em; }
dialog#rules ul { padding-left: 1.2em; }
dialog#rules li { margin-bottom: .3em; }
.rules-close { position: sticky; top: 0; float: right; }
.rules-close button { background: transparent; border: 0; font-size: 1.6rem; line-height: 1; color: var(--muted); }

@media (max-width: 1000px) {
  .table { grid-template-columns: 1fr; grid-template-areas: 'mission' 'others' 'board' 'me' 'log'; }
  .log { position: static; max-height: 260px; }
  .table.log-closed { grid-template-columns: 1fr; }
  .mission-bar { flex-direction: column; }
  .mission-bar .rules-chips { justify-content: flex-start; max-width: none; }
}
@media (max-width: 760px) {
  .landing, .lobby { grid-template-columns: 1fr; padding-top: 24px; }
  .me { grid-template-columns: 1fr; }
  .mission-grid { grid-template-columns: repeat(8, 1fr); }
  :root { --cw: 44px; --ch: 64px; }
  .card .cg { font-size: 1.3rem; }
  .card .cv { font-size: .9rem; }
  .others { grid-template-columns: 1fr 1fr; }
  .seat-name { max-width: 9ch; }
  .room-code .muted { display: none; }
  .table { padding: 10px; gap: 10px; }
  .topbar { padding: 8px 10px; gap: 6px; }
  .topbar button { white-space: nowrap; padding: 6px 8px; font-size: .85rem; }
  .brand span, .brand { font-size: 0; gap: 0; }
  .brand img { width: 24px; height: 24px; }
  .board { min-height: 0; padding: 10px; }
  .trick { min-height: 90px; }
  .radio-slot { width: 52px; min-height: 44px; }
  .seat { padding: 8px 10px; }
  .mission-bar p { font-size: .82rem; }
}
