:root {
  --felt: #1e5a3a;
  --felt-deep: #123d27;
  --rail: #4a2f1c;
  --rail-light: #6b4629;
  --cream: #f3ead8;
  --cream-dim: #d8cdb8;
  --ink: #1c1a16;
  --gold: #d9a441;
  --red: #a8312f;
  --blue: #2f4f8f;
  --panel: #2a2420;
  --panel-2: #3a322b;
  --line: rgba(243, 234, 216, 0.14);
  --serif: "Iowan Old Style", "Palatino Linotype", "Book Antiqua", Palatino, Georgia, serif;
  --sans: "Avenir Next", "Segoe UI", system-ui, -apple-system, Helvetica, Arial, sans-serif;
  --card-w: 76px;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--sans);
  color: var(--cream);
  background: var(--panel);
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; cursor: pointer; }
button:focus-visible, input:focus-visible, select:focus-visible { outline: 2px solid var(--gold); outline-offset: 2px; }
h1, h2, h3 { font-family: var(--serif); font-weight: 600; margin: 0; letter-spacing: 0.01em; }
.hidden { display: none !important; }

/* ---------- buttons ---------- */
.btn {
  border: 1px solid var(--line); background: var(--panel-2); color: var(--cream);
  padding: 9px 16px; border-radius: 6px; line-height: 1.1;
}
.btn:hover { background: #4a4037; }
.btn.primary { background: var(--gold); border-color: var(--gold); color: var(--ink); font-weight: 600; }
.btn.primary:hover { background: #e6b65c; }
.btn.ghost { background: transparent; }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.danger { border-color: rgba(168,49,47,.6); color: #ffb3b1; }
.btn[disabled] { opacity: .45; cursor: default; }
.btn.on { background: var(--gold); color: var(--ink); border-color: var(--gold); }

/* ---------- landing ---------- */
#landing {
  height: 100%; display: grid; place-items: center;
  background: radial-gradient(ellipse at 50% 40%, var(--felt) 0%, var(--felt-deep) 70%);
}
.landing-card {
  width: min(420px, 92vw); background: var(--panel); border: 1px solid var(--line);
  border-radius: 10px; padding: 32px 28px;
  box-shadow: 0 30px 60px rgba(0,0,0,.45);
}
.landing-card h1 { font-size: 44px; line-height: 1; margin-bottom: 4px; }
.landing-card .tag { color: var(--cream-dim); margin: 0 0 24px; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 13px; color: var(--cream-dim); }
.field input, .field select, .rules input, .rules select {
  font: inherit; color: var(--cream); background: #1c1815; border: 1px solid var(--line);
  border-radius: 6px; padding: 9px 10px;
}
.field input.code { text-transform: uppercase; letter-spacing: .3em; font-family: var(--serif); font-size: 20px; }
.landing-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 8px; }
.landing-actions .btn { padding: 12px; }
.or { text-align: center; color: var(--cream-dim); font-size: 13px; margin: 14px 0; }

/* ---------- app frame ---------- */
#app { height: 100%; display: grid; grid-template-columns: 1fr 320px; grid-template-rows: 52px 1fr; }
#topbar {
  grid-column: 1 / -1; display: flex; align-items: center; gap: 14px; padding: 0 14px;
  background: linear-gradient(var(--rail-light), var(--rail)); border-bottom: 1px solid rgba(0,0,0,.4);
}
#topbar .code { font-family: var(--serif); font-size: 20px; letter-spacing: .2em; }
#topbar .code small { font-family: var(--sans); font-size: 12px; letter-spacing: 0; color: var(--cream-dim); margin-right: 8px; }
#topbar .spacer { flex: 1; }
#topbar .score { font-family: var(--serif); font-size: 18px; display: flex; gap: 14px; }
#topbar .score b { color: var(--gold); }
#main { position: relative; overflow: hidden; }
#sidebar {
  background: var(--panel); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0;
}
#sidebar-toggle { display: none; }

/* ---------- lobby ---------- */
#lobby { height: 100%; overflow: auto; padding: 24px; background: radial-gradient(ellipse at 50% 30%, var(--felt) 0%, var(--felt-deep) 75%); }
.lobby-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 24px; max-width: 980px; margin: 0 auto; }
.seats-panel h2, .rules h2 { font-size: 22px; margin-bottom: 4px; }
.seats-panel .hint, .rules .hint { color: var(--cream-dim); font-size: 13px; margin: 0 0 16px; }
.seat-list { display: grid; gap: 10px; }
.seat-row {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px; border-radius: 8px;
  background: rgba(0,0,0,.28); border: 1px solid var(--line);
}
.seat-row .team { width: 34px; height: 34px; border-radius: 50%; display: grid; place-items: center; font-family: var(--serif); font-weight: 700; }
.seat-row .team.t0 { background: var(--gold); color: var(--ink); }
.seat-row .team.t1 { background: var(--cream); color: var(--ink); }
.seat-row .who { flex: 1; }
.seat-row .who .name { font-weight: 600; }
.seat-row .who .sub { font-size: 12px; color: var(--cream-dim); }
.seat-row .offline { color: #ffb3b1; }
.watching { margin-top: 18px; font-size: 13px; color: var(--cream-dim); }
.lobby-actions { margin-top: 20px; display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.share { margin-top: 18px; padding: 12px 14px; background: rgba(0,0,0,.28); border-radius: 8px; font-size: 13px; }
.share code { display: block; margin-top: 6px; font-size: 13px; color: var(--gold); word-break: break-all; }

.rules { background: rgba(0,0,0,.28); border: 1px solid var(--line); border-radius: 8px; padding: 18px; }
.rules fieldset { border: 0; padding: 0; margin: 0 0 16px; }
.rules legend { font-family: var(--serif); font-size: 17px; margin-bottom: 8px; padding: 0; }
.rules .row { display: flex; align-items: center; gap: 10px; padding: 6px 0; font-size: 14px; }
.rules .row label { flex: 1; }
.rules .row input[type="number"] { width: 84px; padding: 6px 8px; }
.rules .row input[type="text"] { width: 200px; padding: 6px 8px; }
.rules .row input[type="checkbox"] { width: 18px; height: 18px; accent-color: var(--gold); }
.rules .row select { padding: 6px 8px; }
.rules .row.stack { flex-direction: column; align-items: stretch; gap: 6px; }
.rules .row.stack select { width: 100%; }
.rules .row.sub { padding-left: 18px; }
.rules[data-locked="true"] input, .rules[data-locked="true"] select { pointer-events: none; opacity: .7; }
.rules .note { font-size: 12px; color: var(--cream-dim); margin: 2px 0 6px; }
.rules .preset { display: flex; gap: 8px; align-items: center; margin-bottom: 14px; }
.rules .preset select { flex: 1; }

/* ---------- table ---------- */
#table {
  position: absolute; inset: 0;
  background: radial-gradient(ellipse at 50% 45%, #256b46 0%, var(--felt) 45%, var(--felt-deep) 100%);
  overflow: hidden;
}
#table::before {
  content: ""; position: absolute; inset: 10px; border-radius: 40% / 30%;
  box-shadow: inset 0 0 60px rgba(0,0,0,.35); pointer-events: none;
}
.seat { position: absolute; display: flex; flex-direction: column; align-items: center; gap: 6px; z-index: 2; }
.seat.pos-bottom { left: 50%; bottom: 8px; transform: translateX(-50%); }
.seat.pos-top { left: 50%; top: 14px; transform: translateX(-50%); }
.seat.pos-left { left: 16px; top: 50%; transform: translateY(-50%); }
.seat.pos-right { right: 16px; top: 50%; transform: translateY(-50%); }
.plate {
  display: flex; align-items: center; gap: 10px; padding: 7px 12px 7px 8px; border-radius: 999px;
  background: rgba(0,0,0,.45); border: 1px solid var(--line); min-width: 150px;
}
.plate .avatar { width: 30px; height: 30px; border-radius: 50%; display: grid; place-items: center; font-family: var(--serif); font-weight: 700; font-size: 14px; }
.plate .avatar.t0 { background: var(--gold); color: var(--ink); }
.plate .avatar.t1 { background: var(--cream); color: var(--ink); }

.plate .info { display: flex; flex-direction: column; line-height: 1.15; }
.plate .info .name { font-weight: 600; font-size: 14px; white-space: nowrap; max-width: 130px; overflow: hidden; text-overflow: ellipsis; }
.plate .info .stat { font-size: 12px; color: var(--cream-dim); }
.plate .info .stat b { color: var(--cream); }
.plate.turn { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(217,164,65,.35), 0 0 22px rgba(217,164,65,.35); }
.plate.offline { opacity: .6; }
.plate .dealer { font-size: 10px; background: var(--cream); color: var(--ink); padding: 2px 6px; border-radius: 999px; font-weight: 700; }
.plate .timer { font-variant-numeric: tabular-nums; font-size: 12px; color: var(--gold); }
.plate .mic { font-size: 12px; }
.backs { display: flex; height: 44px; align-items: flex-start; }
.backs img { width: 30px; height: auto; margin-left: -18px; filter: drop-shadow(0 2px 3px rgba(0,0,0,.4)); }
.backs img:first-child { margin-left: 0; }
.seat.pos-left .backs, .seat.pos-right .backs { height: 34px; }

.trick {
  position: absolute; left: 50%; top: 50%; width: 260px; height: 220px;
  transform: translate(-50%, -52%); z-index: 1;
}
.trick .played { position: absolute; width: var(--card-w); left: 50%; top: 50%; margin-left: calc(var(--card-w) / -2); margin-top: -54px; transition: transform .35s ease, opacity .3s; }
.trick .played .cardface { box-shadow: 0 6px 14px rgba(0,0,0,.4); }
.trick .played.from-bottom { transform: translateY(46px); animation: in-bottom .35s ease; }
.trick .played.from-top { transform: translateY(-46px); animation: in-top .35s ease; }
.trick .played.from-left { transform: translateX(-70px) rotate(-6deg); animation: in-left .35s ease; }
.trick .played.from-right { transform: translateX(70px) rotate(6deg); animation: in-right .35s ease; }
.trick .played.winner { z-index: 3; filter: drop-shadow(0 0 10px var(--gold)); }
.trick .played.loser { opacity: .55; }
@keyframes in-bottom { from { transform: translateY(160px); opacity: 0; } }
@keyframes in-top { from { transform: translateY(-160px); opacity: 0; } }
@keyframes in-left { from { transform: translateX(-220px); opacity: 0; } }
@keyframes in-right { from { transform: translateX(220px); opacity: 0; } }
.trick .empty { position: absolute; inset: 0; display: grid; place-items: center; color: rgba(243,234,216,.55); font-family: var(--serif); font-size: 17px; text-align: center; padding: 0 20px; }
.trick .empty small { display: block; font-family: var(--sans); font-size: 12px; margin-top: 6px; }
.trick .kitty { position: absolute; right: -10px; top: -10px; width: 34px; }
.trick .kitty img { width: 100%; }
@media (prefers-reduced-motion: reduce) { .trick .played { animation: none !important; transition: none; } }

.hand {
  position: absolute; left: 50%; bottom: 74px; transform: translateX(-50%);
  display: flex; align-items: flex-end; z-index: 4; padding: 0 20px; max-width: 100%;
}
.hand .row { display: flex; align-items: flex-end; }
.hand .card {
  width: var(--card-w); margin-left: calc(var(--card-w) * -0.55); background: none; border: 0; padding: 0; flex: 0 0 auto;
  transition: transform .15s ease, filter .15s; position: relative; border-radius: 6px;
}
.hand .card:first-child { margin-left: 0; }
.hand .card .cardface { box-shadow: -2px 2px 8px rgba(0,0,0,.45); }
.hand .card.legal:hover, .hand .card.legal:focus-visible { transform: translateY(-16px); }
.hand .card.illegal { filter: brightness(.55) saturate(.6); cursor: default; pointer-events: none; }
.hand .card:hover, .hand .card:focus-visible { z-index: 6; }
.hand.free .card.illegal { pointer-events: auto; }
.hand .card.selected { transform: translateY(-20px); filter: drop-shadow(0 0 8px var(--gold)); }
.hand .card.legal { cursor: pointer; }
.hand-note small { display: block; color: var(--cream-dim); }
.hand-note {
  position: absolute; left: 50%; bottom: 190px; transform: translateX(-50%); z-index: 3; pointer-events: none; white-space: nowrap; height: auto; max-height: 44px;
  background: rgba(0,0,0,.55); padding: 8px 14px; border-radius: 999px; font-size: 13px; border: 1px solid var(--line);
}

/* live camera next to a seat */
.seat-cam { width: 108px; height: 81px; border-radius: 10px; overflow: hidden; background: #111; border: 2px solid rgba(243,234,216,.7); box-shadow: 0 6px 16px rgba(0,0,0,.45); }
.seat-cam video { width: 100%; height: 100%; object-fit: cover; display: block; }
.seat-cam video.mirror { transform: scaleX(-1); }
.seat-cam.mine { position: absolute; left: 10px; top: 10px; width: 84px; height: 63px; z-index: 3; }
.seat.pos-left .seat-cam, .seat.pos-right .seat-cam { margin-bottom: 2px; }
@media (max-width: 900px) {
  .seat-cam { width: 84px; height: 63px; }
  .seat-cam.mine { width: 64px; height: 48px; left: 6px; top: 6px; }
}
@media (max-width: 520px) {
  .seat.pos-left .seat-cam, .seat.pos-right .seat-cam { width: 64px; height: 48px; }
  .seat.pos-top .seat-cam { width: 72px; height: 54px; }
}

/* card faces with text fallback */
.cardface { position: relative; display: block; width: 100%; aspect-ratio: 167 / 243; border-radius: 6px; background: #fdfaf3; overflow: hidden; }
.cardface img { position: absolute; inset: 0; width: 100%; height: 100%; display: block; opacity: 0; transition: opacity .15s; }
.cardface.loaded img { opacity: 1; }
.cardface .cardtext { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; font-family: var(--serif); font-weight: 700; font-size: 1.35em; line-height: 1.05; padding: 4px; color: var(--ink); }
.cardface.red .cardtext { color: var(--red); }
.cardface.loaded .cardtext { display: none; }
.hand .card img, .trick .played img { box-shadow: none; }
.seat-actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }

/* bid panel */
.bid-panel {
  position: absolute; left: 50%; bottom: 200px; transform: translateX(-50%); z-index: 6;
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px;
  box-shadow: 0 20px 40px rgba(0,0,0,.5); text-align: center;
}
.bid-panel h3 { font-size: 18px; margin-bottom: 10px; }
.bid-panel .nums { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; max-width: 330px; }
.bid-panel .nums .btn { min-width: 42px; padding: 8px 0; font-family: var(--serif); font-size: 18px; }
.bid-panel .special { margin-top: 10px; display: flex; gap: 8px; justify-content: center; }
.bid-panel .sub { font-size: 12px; color: var(--cream-dim); margin-top: 8px; }

.bid-badge {
  position: absolute; font-size: 12px; background: var(--cream); color: var(--ink); padding: 2px 8px; border-radius: 999px; font-weight: 700;
}

/* overlays */
.overlay {
  position: absolute; inset: 0; z-index: 20; display: grid; place-items: center; background: rgba(0,0,0,.55);
}
.overlay .box {
  width: min(560px, 92%); background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 24px 26px;
  box-shadow: 0 30px 60px rgba(0,0,0,.5); max-height: 90%; overflow: auto;
}
.overlay h2 { font-size: 28px; margin-bottom: 4px; }
.overlay .sub { color: var(--cream-dim); margin-bottom: 16px; }
.score-table { width: 100%; border-collapse: collapse; margin-bottom: 14px; }
.score-table th, .score-table td { padding: 8px 6px; text-align: left; border-bottom: 1px solid var(--line); vertical-align: top; font-size: 14px; }
.score-table th { font-family: var(--serif); font-weight: 600; font-size: 16px; }
.score-table .delta { font-family: var(--serif); font-size: 20px; }
.score-table .delta.neg { color: #ffb3b1; }
.score-table .delta.pos { color: #b7e3c0; }
.score-table .notes { font-size: 12px; color: var(--cream-dim); }
.overlay .actions { display: flex; gap: 10px; justify-content: flex-end; flex-wrap: wrap; }

/* ---------- sidebar ---------- */
.side-tabs { display: flex; border-bottom: 1px solid var(--line); }
.side-tabs button { flex: 1; background: none; border: 0; color: var(--cream-dim); padding: 12px 6px; border-bottom: 2px solid transparent; }
.side-tabs button.active { color: var(--cream); border-bottom-color: var(--gold); }
.side-pane { flex: 1; min-height: 0; display: flex; flex-direction: column; }
#chat-log { flex: 1; overflow: auto; padding: 12px; display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
#chat-log .msg .from { color: var(--gold); font-weight: 600; margin-right: 6px; }
#chat-log .msg.system { color: var(--cream-dim); font-style: italic; font-size: 13px; }
#chat-form { display: flex; gap: 6px; padding: 10px; border-top: 1px solid var(--line); }
#chat-form input { flex: 1; font: inherit; color: var(--cream); background: #1c1815; border: 1px solid var(--line); border-radius: 6px; padding: 8px 10px; }

#video-pane { padding: 10px; display: flex; flex-direction: column; gap: 10px; overflow: auto; }
.av-controls { display: flex; gap: 8px; flex-wrap: wrap; }
.av-status { font-size: 12px; color: var(--cream-dim); }
.tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.tile { position: relative; aspect-ratio: 4 / 3; background: #1c1815; border-radius: 8px; overflow: hidden; border: 1px solid var(--line); }
.tile video { width: 100%; height: 100%; object-fit: cover; display: block; }
.tile.nocam video { display: none; }
.tile .label { position: absolute; left: 6px; bottom: 6px; font-size: 12px; background: rgba(0,0,0,.6); padding: 2px 6px; border-radius: 4px; }
.tile .avatar-big { position: absolute; inset: 0; display: grid; place-items: center; font-family: var(--serif); font-size: 34px; color: var(--cream-dim); }
.tile.nocam .avatar-big { display: grid; }
.tile:not(.nocam) .avatar-big { display: none; }
.tile.muted .label::after { content: " · muted"; color: #ffb3b1; }
.tile.speaking { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(217,164,65,.35); }
.tile .level { position: absolute; left: 0; bottom: 0; height: 3px; width: 0; background: var(--gold); transition: width .12s; }
.tile audio { display: none; }

#history-pane { padding: 12px; overflow: auto; font-size: 13px; }
#history-pane table { width: 100%; border-collapse: collapse; }
#history-pane td, #history-pane th { padding: 5px 4px; border-bottom: 1px solid var(--line); text-align: right; }
#history-pane th:first-child, #history-pane td:first-child { text-align: left; }
#history-pane .who { color: var(--cream-dim); margin-bottom: 8px; }

/* unread chat badge */
.badge { display: inline-grid; place-items: center; min-width: 18px; height: 18px; padding: 0 5px; border-radius: 999px; background: var(--red); color: #fff; font-size: 11px; font-weight: 700; margin-left: 6px; vertical-align: middle; }
.side-tabs button .badge { margin-left: 4px; }

/* toasts */
#toasts { position: fixed; left: 50%; top: 64px; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; pointer-events: none; }
.toast { background: var(--panel); border: 1px solid var(--line); border-left: 4px solid var(--gold); padding: 10px 14px; border-radius: 6px; font-size: 14px; box-shadow: 0 10px 30px rgba(0,0,0,.5); }
.toast.error { border-left-color: var(--red); }

/* ---------- responsive ---------- */
@media (max-width: 900px) {
  :root { --card-w: 62px; }
  #app { grid-template-columns: 1fr; }
  #sidebar { position: absolute; right: 0; top: 52px; bottom: 0; width: min(320px, 88vw); transform: translateX(100%); transition: transform .25s; z-index: 30; box-shadow: -10px 0 30px rgba(0,0,0,.4); }
  #sidebar.open { transform: none; }
  #sidebar-toggle { display: inline-flex; }
  .lobby-grid { grid-template-columns: 1fr; }
  .plate { min-width: 0; }
  .plate .info .name { max-width: 90px; }
  .seat.pos-left { left: 6px; } .seat.pos-right { right: 6px; }
  .seat.pos-left .backs, .seat.pos-right .backs { display: none; }
  .trick { width: 220px; height: 200px; transform: translate(-50%, -58%); }
  .hand { bottom: 68px; padding: 0 10px; }
  .hand .card { margin-left: calc(var(--card-w) * -0.62); }
  .hand.rows { flex-direction: column; align-items: center; gap: 0; }
  .hand.rows .row { display: flex; align-items: flex-end; }
  .hand.rows .row + .row { margin-top: calc(var(--card-w) * -0.55); }
  .hand.rows .row .card { margin-left: calc(var(--card-w) * -0.42); }
  .hand.rows .row .card:first-child { margin-left: 0; }
  .hand.rows .row + .row .card { position: relative; }
  .hand.rows .card.legal:hover, .hand.rows .card.legal:focus-visible { transform: translateY(-10px); }
  .bid-panel { top: auto; bottom: 0; left: 0; right: 0; width: 100%; max-width: none; transform: none; border-radius: 12px 12px 0 0; padding: 8px 10px 10px; }
  .bid-panel h3 { font-size: 15px; margin-bottom: 6px; }
  .bid-panel .nums { gap: 5px; max-width: none; }
  .bid-panel .nums .btn { min-width: 36px; padding: 6px 0; font-size: 16px; }
  .bid-panel .sub { margin-top: 6px; }
  #table.bidding-me .hand { bottom: var(--bid-h, 150px); }
  #table.bidding-me .seat.pos-bottom, #table.bidding-me .seat.pos-left .backs, #table.bidding-me .seat.pos-right .backs { display: none; }
  #topbar .score { font-size: 15px; }
}
@media (max-width: 520px) {
  :root { --card-w: 54px; }
  #topbar { gap: 8px; padding: 0 8px; }
  #topbar .code { font-size: 16px; letter-spacing: .12em; }
  #topbar .score { font-size: 13px; gap: 8px; }
  #btn-copy-link { display: none; }
  .plate .info .stat { display: none; }
  .seat.pos-bottom { left: auto; right: 8px; bottom: 8px; transform: none; }
  .seat.pos-bottom .plate .info .stat { display: block; }
  .seat.pos-bottom .plate { padding: 5px 10px 5px 6px; }
  .hand { bottom: 52px; }
  .hand-note { bottom: 186px; top: auto; height: auto; max-height: 44px; font-size: 12px; }
  .seat.pos-left, .seat.pos-right { top: 40%; }
}
