:root {
  --bg: #f4f1ea;
  --panel: #ffffff;
  --text: #1f2328;
  --muted: #6b7280;
  --border: #ddd6c8;
  --accent: #c0392b;
  --accent-text: #ffffff;
  --card: #ffffff;
  --card-red: #c0392b;
  --card-black: #1f2328;
  --felt: #1f6b45;
  --felt-edge: #17573a;
  --felt-text: #f2f7f3;
  --side0: #1d6fb8;
  --side1: #c2410c;
  --side2: #7b3fa0;
  --held: #2e7d4f;
  --option: #b45309;
  --current: #fdf3e1;
  --sum-bg: #e3edf7;
  --sum-text: #1d4f7a;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171a;
    --panel: #1f2226;
    --text: #e8e6e1;
    --muted: #9aa0a6;
    --border: #33373d;
    --accent: #e0574a;
    --card: #f2efe8;
    --felt: #18412d;
    --felt-edge: #10301f;
    --side0: #5aa2e0;
    --side1: #f08a4b;
    --side2: #b07fd0;
    --held: #4caf7a;
    --option: #f0a94b;
    --current: #2b2a22;
    --sum-bg: #1c2a38;
    --sum-text: #9cc7ec;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
main { max-width: 900px; margin: 0 auto; padding: 16px; }
h1 { font-size: 2rem; margin: 8px 0 16px; letter-spacing: .02em; }
h2 { font-size: 1.1rem; margin: 0 0 12px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 16px;
}
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.spread { justify-content: space-between; }

input {
  font: inherit;
  padding: 10px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  min-width: 0;
  flex: 1;
}
input.code { text-transform: uppercase; letter-spacing: .2em; max-width: 9ch; flex: none; }

button {
  font: inherit;
  padding: 10px 16px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
}
button.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
button:disabled { opacity: .45; cursor: not-allowed; }
.segmented { display: inline-flex; align-items: center; gap: 0; }
.segmented .muted { margin-right: 8px; }
.segmented button { border-radius: 0; min-width: 44px; }
.segmented button:first-of-type { border-radius: 8px 0 0 8px; }
.segmented button:last-of-type { border-radius: 0 8px 8px 0; border-left: 0; }
.segmented button.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); font-weight: 600; }
.settings { display: flex; flex-wrap: wrap; gap: 12px 24px; align-items: center; }
.setting { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; }
.setting input[type=checkbox] { width: 18px; height: 18px; flex: none; accent-color: var(--accent); }
select {
  font: inherit; padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--bg); color: var(--text);
}
.open-games { display: flex; flex-direction: column; gap: 8px; }
.open-game {
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
  padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg);
}
.small-text { font-size: .85rem; }

/* Start page: "join with code" or "start new" side by side */
.home-pair { display: grid; grid-template-columns: 1fr auto 1fr; gap: 12px; align-items: stretch; margin-bottom: 16px; }
.home-pair > .panel { margin-bottom: 0; }
.home-pair .or { align-self: center; color: var(--muted); font-weight: 600; text-transform: uppercase; font-size: .8rem; }
@media (max-width: 720px) {
  .home-pair { grid-template-columns: 1fr; }
  .home-pair .or { justify-self: center; }
}

/* Finished games list */
.finished-games { display: flex; flex-direction: column; gap: 8px; }
.finished-game {
  display: flex; flex-wrap: wrap; justify-content: space-between; align-items: center; gap: 4px 16px;
  width: 100%; text-align: left; padding: 10px 12px; background: var(--bg);
}
.finished-game:hover { border-color: var(--accent); }
.fg-players { font-size: .9rem; }
.winner-name { color: var(--held); font-weight: 600; }

/* Game log modal */
.modal {
  width: min(900px, calc(100% - 32px)); max-height: 85vh; padding: 0;
  border: 1px solid var(--border); border-radius: 12px;
  background: var(--panel); color: var(--text);
}
.modal::backdrop { background: rgba(0, 0, 0, .55); }
.modal-body { padding: 20px; }
.modal h3 { font-size: 1rem; margin: 20px 0 8px; }
.event-wrap { max-height: 50vh; overflow-y: auto; border: 1px solid var(--border); border-radius: 8px; }
table.events th { position: sticky; top: 0; background: var(--panel); text-align: left; }
table.events td { text-align: left; white-space: normal; }
table.events td:nth-child(-n+2) { white-space: nowrap; font-variant-numeric: tabular-nums; }
tr.ev-score td { background: var(--current); }
tr.ev-start td, tr.ev-finish td { background: var(--sum-bg); color: var(--sum-text); }
tr.winner-row td { font-weight: 600; }
.dice-text { font-size: 1.25rem; letter-spacing: 2px; line-height: 1; }
button.small { padding: 4px 10px; font-size: .85rem; }

.room-code { font-size: 2.2rem; font-weight: 700; letter-spacing: .25em; }
.players { list-style: none; padding: 0; margin: 0; }
.players li { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--border); }
.players li:last-child { border-bottom: 0; }
.badge { font-size: .75rem; padding: 2px 8px; border-radius: 99px; background: var(--bg); border: 1px solid var(--border); margin-left: 6px; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--held); margin-right: 8px; }
.dot.off { background: var(--muted); opacity: .5; }

/* Three-column game layout: players + chat | board | log */
main.wide { max-width: 1600px; }
.game-layout {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr) 260px;
  gap: 16px;
  align-items: start;
}
.side { position: sticky; top: 16px; display: flex; flex-direction: column; }
.center { min-width: 0; }
@media (max-width: 1100px) {
  .game-layout { grid-template-columns: 1fr; }
  .side { position: static; }
  .center { order: -1; }
}

.log { font-size: .9rem; max-height: 70vh; overflow-y: auto; margin: 0; padding-left: 22px; }
.log li { padding: 2px 0; }

.chat-list {
  height: 260px; overflow-y: auto; font-size: .9rem;
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px; margin-bottom: 8px; overflow-wrap: anywhere;
}
.chat-msg { padding: 3px 0; }
.chat-msg.mine strong { color: var(--accent); }
.chat-form { flex-wrap: nowrap; }
.winner { font-size: 1.4rem; font-weight: 700; text-align: center; margin: 8px 0 16px; }

.status {
  position: sticky; top: 0; z-index: 10;
  background: var(--option); color: #fff; text-align: center; padding: 6px; font-weight: 600;
}
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: var(--text); color: var(--bg);
  padding: 10px 18px; border-radius: 8px; max-width: calc(100% - 32px);
  box-shadow: 0 4px 16px rgba(0,0,0,.25);
}

.turn-timer { font-variant-numeric: tabular-nums; font-weight: 600; }
.turn-timer.low { color: var(--accent); }
.turn-info { margin-bottom: 10px; gap: 8px 16px; }

/* Tables (score sheet, deal result, game log) */
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-variant-numeric: tabular-nums; }
th, td { padding: 6px 10px; border-bottom: 1px solid var(--border); text-align: center; white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
thead th { font-size: .9rem; }
tr.sub td, tr.total td { background: var(--sum-bg); color: var(--sum-text); }
tr.sub td { font-weight: 600; }
tr.total td { font-weight: 700; font-size: 1.05rem; border-top: 2px solid var(--sum-text); }
th.side-0 { color: var(--side0); }
th.side-1 { color: var(--side1); }
th.side-2 { color: var(--side2); }
.score-sheet th { max-width: 9ch; overflow: hidden; text-overflow: ellipsis; }
.score-sheet th, .score-sheet td { padding: 4px 6px; font-size: .9rem; }
.score-sheet td.caller { text-decoration: underline; text-underline-offset: 3px; }
.score-sheet td.failed, .score-sheet td.hanging { text-decoration: line-through; color: var(--muted); }
.score-sheet tfoot td { font-weight: 700; background: var(--sum-bg); color: var(--sum-text); border-top: 2px solid var(--sum-text); }
.final-table { max-width: 420px; margin: 0 auto; font-size: 1.05rem; }
.rules-table th, .rules-table td { padding: 4px 8px; }
.rules ul { padding-left: 20px; margin: 4px 0; }
.rules li { margin: 4px 0; }
.ctext { font-weight: 600; white-space: nowrap; }
.ctext.red { color: var(--card-red); }
.intro { margin: -8px 0 16px; }
button.linkish {
  border: 0; background: none; padding: 0; color: var(--accent); text-decoration: underline; cursor: pointer; font: inherit;
}
.team-chip { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }
.team-chip.side-0 { background: var(--side0); }
.team-chip.side-1 { background: var(--side1); }

/* Cards: faces stay light in dark mode, like real cards */
.card {
  position: relative; flex: none;
  width: 64px; height: 92px; padding: 0;
  background: var(--card); color: var(--card-black);
  border: 1px solid #c9c2b4; border-radius: 8px;
  box-shadow: 0 2px 3px rgba(0, 0, 0, .18);
  font: 700 1rem/1 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  user-select: none;
}
.card.red { color: var(--card-red); }
.card .corner { position: absolute; top: 5px; left: 6px; display: flex; flex-direction: column; align-items: center; gap: 1px; }
.card .face { position: absolute; inset: 0; display: grid; place-items: center; font-size: 2.1rem; padding-top: 10px; }
.card.small { width: 52px; height: 74px; font-size: .9rem; }
.card.small .face { font-size: 1.7rem; }
.card.is-trump { box-shadow: 0 2px 3px rgba(0, 0, 0, .18), inset 0 0 0 2px rgba(180, 83, 9, .55); }
.card.clickable { cursor: pointer; transition: transform .12s, box-shadow .12s; }
.card.clickable:hover, .card.clickable:focus-visible { transform: translateY(-10px); box-shadow: 0 6px 12px rgba(0, 0, 0, .3); }
.card.dim { opacity: .45; filter: saturate(.4); }
.card.selected { transform: translateY(-14px); outline: 3px solid var(--option); outline-offset: 1px; }
.card.extra { outline: 2px dashed var(--option); outline-offset: 2px; }
.card.back {
  background:
    repeating-linear-gradient(45deg, rgba(255, 255, 255, .16) 0 4px, transparent 4px 8px),
    var(--accent);
  border-color: rgba(0, 0, 0, .25);
}
.card.mini { width: 22px; height: 32px; border-radius: 4px; box-shadow: none; }
.card.fresh { animation: play-in .25s ease-out; }
.card.taken { opacity: .55; }
.card.took { box-shadow: 0 0 0 3px #f5c542; }
@keyframes play-in {
  from { transform: translateY(14px) scale(.9); opacity: 0; }
  to { transform: none; opacity: 1; }
}

/* The table: opponents around a felt, your seat at the bottom */
.table-panel { padding: 12px; }
.trump-badge { font-weight: 600; }
.trump-suit { font-size: 1.4rem; line-height: 1; vertical-align: -2px; }
.trump-suit.red, .suit-btn.red { color: var(--card-red); }
.declared {
  font-size: .9rem; margin: 0 0 8px; padding: 6px 10px;
  border: 1px dashed var(--option); border-radius: 8px;
}
.table {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(190px, 1.6fr) minmax(0, 1fr);
  grid-template-areas: "left top right" "left center right" "bottom bottom bottom";
  gap: 8px; padding: 12px; min-height: 300px;
  background: radial-gradient(ellipse at center, var(--felt) 0%, var(--felt-edge) 100%);
  color: var(--felt-text); border-radius: 16px;
}
.table.n2 { grid-template-areas: "top top top" "center center center" "bottom bottom bottom"; }
.seat { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 6px; min-width: 0; text-align: center; }
.seat-top { grid-area: top; }
.seat-left { grid-area: left; }
.seat-right { grid-area: right; }
.seat-bottom { grid-area: bottom; }
.table-center { grid-area: center; display: grid; place-items: center; min-height: 200px; }
.seat-label {
  display: inline-flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 4px;
  padding: 4px 10px; border-radius: 99px; background: rgba(0, 0, 0, .22); max-width: 100%;
}
.seat-label.active { background: #f5c542; color: #1f2328; box-shadow: 0 0 0 3px rgba(245, 197, 66, .35); }
.seat-label .badge { background: rgba(255, 255, 255, .85); color: #1f2328; border: 0; margin: 0; }
.seat-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 14ch; }
.bid { font-weight: 700; padding: 0 4px; }
.backs { display: flex; justify-content: center; }
.backs .card + .card { margin-left: -12px; }
.center-msg { font-weight: 600; text-align: center; opacity: .9; }
.trick-area { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.trick {
  display: grid; grid-template-columns: repeat(3, 56px); grid-template-rows: repeat(3, 54px);
  grid-template-areas: ". top ." "left mid right" ". bottom ."; place-items: center;
}
.table.n2 .trick { grid-template-areas: ". top ." ". mid ." ". bottom ."; }
.slot-top { grid-area: top; }
.slot-left { grid-area: left; }
.slot-right { grid-area: right; }
.slot-bottom { grid-area: bottom; }
.trick-caption { font-size: .85rem; font-weight: 600; margin-top: 18px; }

/* Your hand and what you can do */
.hand-wrap { overflow-x: auto; padding: 18px 4px 4px; }
.hand { display: flex; gap: 6px; justify-content: center; flex-wrap: wrap; min-height: 96px; }
.actions { text-align: center; margin-top: 12px; }
.actions p { margin: 4px 0; }
.your-turn { font-weight: 700; color: var(--held); }
.bid-box { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.suit-btn {
  font-size: 1.6rem; line-height: 1; min-width: 64px; display: inline-flex; flex-direction: column; align-items: center; gap: 2px;
  color: var(--card-black); background: var(--card);
}
.suit-btn-name { font-size: .7rem; color: #4b5563; }
.deal-result { text-align: left; max-width: 560px; margin: 0 auto; }
.deal-result .outcome { font-weight: 700; text-align: center; margin: 4px 0 8px; }
.deal-result .outcome.made { color: var(--held); }
.deal-result .outcome.failed { color: var(--accent); }
.deal-result .outcome.hanging { color: var(--option); }

@media (max-width: 640px) {
  main { padding: 10px; }
  .card { width: 46px; height: 68px; font-size: .85rem; border-radius: 6px; }
  .card .face { font-size: 1.5rem; }
  .card.small { width: 40px; height: 58px; }
  .card.small .face { font-size: 1.3rem; }
  .card .corner { top: 3px; left: 4px; }
  .hand { flex-wrap: nowrap; justify-content: safe center; gap: 0; min-height: 72px; }
  .hand .card { width: 42px; height: 64px; }
  .hand .card + .card { margin-left: -6px; }
  .table { padding: 8px; grid-template-columns: minmax(0, 1fr) minmax(140px, 1.6fr) minmax(0, 1fr); min-height: 240px; }
  .trick { grid-template-columns: repeat(3, 44px); grid-template-rows: repeat(3, 42px); }
  .table-center { min-height: 160px; }
  .card.mini { width: 14px; height: 22px; }
  .backs .card + .card { margin-left: -8px; }
  .seat-name { max-width: 9ch; }
  .suit-btn { min-width: 52px; }
}
.declare-box {
  display: inline-flex; flex-direction: column; align-items: flex-start; gap: 4px; text-align: left;
  margin: 0 auto 8px; padding: 8px 12px; border: 1px dashed var(--option); border-radius: 8px;
}
.declare-option { font-size: .95rem; }
.offered { display: flex; flex-direction: column; align-items: center; gap: 4px; margin-bottom: 6px; }

/* Won cards */
.card.tiny { width: 30px; height: 44px; font-size: .7rem; border-radius: 4px; box-shadow: none; }
.card.tiny .corner { top: 2px; left: 3px; }
.card.tiny .face { font-size: 1rem; padding-top: 8px; }
.pile { display: flex; flex-wrap: wrap; gap: 6px 16px; }
.pile-trick { display: flex; align-items: center; gap: 3px; flex-wrap: wrap; }
.pile-points { font-size: .85rem; font-weight: 600; margin-left: 6px; font-variant-numeric: tabular-nums; }
.my-pile { text-align: left; margin-top: 10px; padding: 6px 10px; border: 1px solid var(--border); border-radius: 8px; }
.my-pile summary { cursor: pointer; font-weight: 600; }
.my-pile .pile { margin-top: 8px; }
.piles { margin-top: 12px; }
.piles h3 { font-size: 1rem; margin: 0 0 8px; }
.pile-side { margin-bottom: 10px; }
.pile-name { font-weight: 700; margin-bottom: 4px; }
.pile-name.side-0 { color: var(--side0); }
.pile-name.side-1 { color: var(--side1); }
.pile-name.side-2 { color: var(--side2); }
.calls { margin-bottom: 4px; }
.call-best { font-weight: 700; }
.call-beaten { text-decoration: line-through; opacity: .7; }
