:root {
  --felt: #16553a;
  --felt-dark: #0f3d2a;
  --felt-light: #1f6e4b;
  --ink: #f3efe2;
  --muted: #b9c9bd;
  --gold: #e8c26a;
  --card: #fdfbf5;
  --card-edge: #d8d2c2;
  --red: #c0272d;
  --black: #1b1b1f;
  --panel: rgba(8, 32, 22, 0.94);
  --us: #7fd1a6;
  --them: #f0a27a;
  --cw: clamp(52px, 10.5vw, 96px); /* hand card width */
  --tw: clamp(46px, 8.5vw, 80px); /* trick card width */
  --bw: clamp(22px, 3.2vw, 34px); /* card back width */
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  color: var(--ink);
  background: radial-gradient(ellipse at 50% 40%, var(--felt-light) 0%, var(--felt) 45%, var(--felt-dark) 100%) fixed;
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding: env(safe-area-inset-top, 0) env(safe-area-inset-right, 0) env(safe-area-inset-bottom, 0) env(safe-area-inset-left, 0);
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.22);
  border-radius: 8px;
  padding: 6px 12px;
  cursor: pointer;
}
button:hover:not(:disabled) { background: rgba(255, 255, 255, 0.16); }
button:disabled { opacity: 0.4; cursor: default; }
button.primary { background: var(--gold); color: #2a2208; border-color: var(--gold); font-weight: 600; }
button.primary:hover:not(:disabled) { background: #f2d488; }
button.link { background: none; border: none; color: var(--gold); text-decoration: underline; padding: 4px; }
button:focus-visible, .card:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

/* ---------- Top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  flex-wrap: wrap;
  padding: 8px 16px;
  background: rgba(0, 0, 0, 0.28);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}
.brand { margin: 0; font: 600 22px/1 Georgia, "Times New Roman", serif; letter-spacing: 0.02em; }
.brand span { color: var(--gold); }

.scoreboard { display: flex; align-items: stretch; gap: 10px; }
.team {
  min-width: 128px;
  padding: 4px 12px;
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.25);
  border: 1px solid rgba(255, 255, 255, 0.08);
  text-align: center;
}
.team-name { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.06em; }
.team-0 .team-name { color: var(--us); }
.team-1 .team-name { color: var(--them); }
.team-score { font: 700 24px/1.1 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; font-variant-numeric: tabular-nums; }
.team-detail { font-size: 12px; color: var(--muted); display: flex; gap: 8px; justify-content: center; white-space: nowrap; }
.bags.warn { color: var(--gold); font-weight: 600; }
.vs { display: flex; flex-direction: column; justify-content: center; align-items: center; color: var(--muted); font-size: 12px; }
.target { white-space: nowrap; }

.menu { display: flex; gap: 6px; flex-wrap: wrap; }

/* ---------- Table ---------- */

.table {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(64px, 1fr) minmax(0, 2.4fr) minmax(64px, 1fr);
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    ".    north ."
    "west center east"
    ".    south .";
  gap: 6px;
  width: 100%;
  max-width: 1000px;
  margin: 0 auto;
  padding: 10px 16px 0;
  min-height: 0;
}
.seat-north { grid-area: north; }
.seat-west { grid-area: west; }
.seat-east { grid-area: east; }
.seat-south { grid-area: south; }
.center { grid-area: center; position: relative; min-height: calc(var(--tw) * 3.3); }

.seat { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; }
.seat-label { display: flex; align-items: center; gap: 6px; }
.seat-name {
  font-weight: 600;
  padding: 2px 10px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.25);
  transition: background 0.2s, color 0.2s, box-shadow 0.2s;
}
.seat-name small { font-weight: 400; color: var(--muted); }
.seat.active .seat-name { background: var(--gold); color: #2a2208; box-shadow: 0 0 14px rgba(232, 194, 106, 0.7); }
.seat.active .seat-name small { color: #5a4a1a; }
.dealer-chip {
  width: 20px; height: 20px; border-radius: 50%;
  display: inline-grid; place-items: center;
  font-size: 11px; font-weight: 700;
  background: var(--ink); color: var(--felt-dark);
}
.dealer-chip[hidden] { display: none; }
.seat-info { font-size: 13px; color: var(--muted); min-height: 1.4em; text-align: center; }
.seat-info b { color: var(--ink); }
.seat-info .nil { color: var(--gold); font-weight: 700; }
.seat-info .nil.failed { color: #ff8a80; text-decoration: line-through; }
.seat-info .sep { margin: 0 4px; }

.backs { display: flex; justify-content: center; }
.back {
  width: var(--bw);
  height: calc(var(--bw) * 1.4);
  border-radius: 4px;
  border: 2px solid #f4f0e4;
  background:
    repeating-linear-gradient(45deg, rgba(255,255,255,0.14) 0 3px, transparent 3px 7px),
    linear-gradient(135deg, #8e2430, #5d1520);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  flex: none;
}
.seat-north .back + .back { margin-left: calc(var(--bw) * -0.62); }
.seat-west .backs, .seat-east .backs { flex-direction: column; }
.seat-west .back + .back, .seat-east .back + .back { margin-top: calc(var(--bw) * -1.12); }

/* ---------- Cards ---------- */

.card {
  position: relative;
  font-variant-numeric: lining-nums;
  width: var(--cw);
  aspect-ratio: 5 / 7;
  padding: 0;
  border-radius: calc(var(--cw) * 0.08);
  background: var(--card);
  border: 1px solid var(--card-edge);
  color: var(--black);
  box-shadow: 0 2px 5px rgba(0, 0, 0, 0.35);
  font-family: Georgia, "Times New Roman", serif;
  user-select: none;
  flex: none;
}
.card:hover:not(:disabled) { background: var(--card); }
.card.red { color: var(--red); }
.card .corner {
  position: absolute;
  top: 4%;
  left: 6%;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 0.95;
  font-size: calc(var(--cw) * 0.24);
  font-weight: 700;
}
.card .corner .s { font-size: 0.85em; }
.card .corner.br { top: auto; left: auto; bottom: 4%; right: 6%; transform: rotate(180deg); }
.card .pip {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-size: calc(var(--cw) * 0.5);
  line-height: 1;
}
.card .pip.face { font-size: calc(var(--cw) * 0.42); font-weight: 700; }
.card .pip.face::after {
  content: attr(data-suit);
  position: absolute;
  font-size: 0.5em;
  transform: translate(0, 85%);
}

/* ---------- Trick area ---------- */

.trick { position: absolute; inset: 0; }
.trick .card { --cw: var(--tw); cursor: default; }
.trick .slot { position: absolute; }
.slot-0 { left: 50%; bottom: 2%; transform: translateX(-50%); }
.slot-2 { left: 50%; top: 2%; transform: translateX(-50%); }
.slot-1 { left: 12%; top: 50%; transform: translateY(-50%); }
.slot-3 { right: 12%; top: 50%; transform: translateY(-50%); }
.trick .card.win { box-shadow: 0 0 0 3px var(--gold), 0 0 18px rgba(232, 194, 106, 0.8); }

.slot-0 .enter { animation: from-south 0.28s ease-out; }
.slot-1 .enter { animation: from-west 0.28s ease-out; }
.slot-2 .enter { animation: from-north 0.28s ease-out; }
.slot-3 .enter { animation: from-east 0.28s ease-out; }
@keyframes from-south { from { transform: translateY(120%); opacity: 0; } }
@keyframes from-north { from { transform: translateY(-120%); opacity: 0; } }
@keyframes from-west { from { transform: translateX(-120%); opacity: 0; } }
@keyframes from-east { from { transform: translateX(120%); opacity: 0; } }

.trick .card { transition: transform 0.4s ease-in, opacity 0.4s ease-in; }
.trick.collect-0 .card { transform: translateY(200%) scale(0.5); opacity: 0; }
.trick.collect-2 .card { transform: translateY(-200%) scale(0.5); opacity: 0; }
.trick.collect-1 .card { transform: translateX(-200%) scale(0.5); opacity: 0; }
.trick.collect-3 .card { transform: translateX(200%) scale(0.5); opacity: 0; }

/* ---------- Center panels ---------- */

.panel {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(360px, 100%);
  padding: 14px;
  border-radius: 14px;
  background: var(--panel);
  border: 1px solid rgba(232, 194, 106, 0.4);
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
  text-align: center;
  z-index: 2;
}
.panel[hidden] { display: none; }
.panel-title { font: 600 18px Georgia, serif; color: var(--gold); }
.panel-sub { font-size: 13px; color: var(--muted); margin: 4px 0 10px; }
.panel-actions { display: flex; gap: 8px; justify-content: center; flex-wrap: wrap; }
.bid-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 5px; }
.bid-grid button { padding: 7px 0; font-weight: 600; font-variant-numeric: tabular-nums; }
.bid-grid button.nil { grid-column: span 2; }
.bid-grid button.suggested { border-color: var(--gold); box-shadow: 0 0 0 2px var(--gold) inset; }

/* ---------- Status bar ---------- */

.statusbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  max-width: 1000px;
  margin: 0 auto;
  padding: 6px 16px;
}
.status { font-weight: 500; min-height: 1.4em; }
.status-actions { display: flex; gap: 6px; flex: none; }

/* ---------- Hand ---------- */

.hand-area { padding: 4px 16px calc(14px + env(safe-area-inset-bottom, 0)); }
.hand {
  display: flex;
  justify-content: center;
  width: 100%;
  max-width: calc(var(--cw) * 9);
  margin: 0 auto;
  min-height: calc(var(--cw) * 1.4 + 16px);
  padding-top: 16px;
}
.hand .hslot { flex: 1 1 0; min-width: 0; max-width: calc(var(--cw) * 0.72); }
.hand .hslot:last-child { flex: none; width: var(--cw); max-width: none; }
.hand .card { transition: transform 0.12s ease, filter 0.12s; }
.hand.my-turn .card.legal { cursor: pointer; }
.hand.my-turn .card.legal:hover, .hand.my-turn .card.legal:focus-visible { transform: translateY(-14px); }
.hand.my-turn .card.illegal { filter: brightness(0.6) saturate(0.6); cursor: not-allowed; }
.hand .card.hint { transform: translateY(-14px); box-shadow: 0 0 0 3px var(--gold), 0 0 16px rgba(232, 194, 106, 0.8); }
.hand.face-down .card { visibility: hidden; }
.hand .back-card { width: var(--cw); aspect-ratio: 5/7; height: auto; border-radius: calc(var(--cw) * 0.08); }

/* ---------- Dialogs ---------- */

.dialog {
  width: min(560px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  overflow: auto;
  padding: 18px 20px;
  border-radius: 14px;
  border: 1px solid rgba(232, 194, 106, 0.4);
  background: #10301f;
  color: var(--ink);
  box-shadow: 0 20px 50px rgba(0, 0, 0, 0.6);
}
.dialog-small { width: min(380px, calc(100vw - 32px)); }
.dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
.dialog h2 { margin: 0 0 12px; font: 600 22px Georgia, serif; color: var(--gold); }
.dialog h3 { margin: 16px 0 6px; font-size: 15px; color: var(--gold); }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; flex-wrap: wrap; margin-top: 16px; }
.rules ul { padding-left: 20px; margin: 4px 0; }
.rules li { margin: 4px 0; }
.scroll-x { overflow-x: auto; }

.results { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums lining-nums; }
.results th, .results td { padding: 6px 8px; text-align: right; border-bottom: 1px solid rgba(255, 255, 255, 0.1); white-space: nowrap; }
.results th:first-child, .results td:first-child { text-align: left; }
.results thead th { color: var(--muted); font-weight: 500; font-size: 13px; }
.results .us { color: var(--us); }
.results .them { color: var(--them); }
.results .total td { font-weight: 700; border-top: 1px solid rgba(255, 255, 255, 0.3); }
.results .neg { color: #ff8a80; }
.note { color: var(--muted); font-size: 13px; margin: 10px 0 0; }
.big-result { font: 700 22px system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; margin: 0 0 8px; }

.last-trick { display: grid; grid-template-columns: repeat(4, auto); justify-content: center; gap: 10px; }
.last-trick figure { margin: 0; text-align: center; font-size: 12px; color: var(--muted); }
.last-trick figure.won figcaption { color: var(--gold); font-weight: 600; }
.last-trick .card { --cw: 60px; margin: 0 auto 4px; }

fieldset { border: 1px solid rgba(255, 255, 255, 0.15); border-radius: 10px; margin: 0 0 12px; padding: 10px 14px; }
legend { color: var(--gold); padding: 0 6px; }
legend small { color: var(--muted); }
fieldset label { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 6px 0; }
fieldset label.check { justify-content: flex-start; }
select { font: inherit; color: var(--ink); background: #0b2417; border: 1px solid rgba(255, 255, 255, 0.25); border-radius: 6px; padding: 4px 8px; }

/* ---------- Small screens ---------- */

@media (max-width: 640px) {
  .topbar { padding: 6px 16px; }
  .brand { font-size: 18px; }
  .scoreboard { order: 3; width: 100%; justify-content: center; }
  .team { min-width: 0; flex: 1; padding: 3px 8px; }
  .team-score { font-size: 20px; }
  .menu button { padding: 4px 8px; font-size: 13px; }
  .table { grid-template-columns: 56px minmax(0, 1fr) 56px; padding-top: 6px; }
  .seat-name { font-size: 13px; padding: 1px 8px; }
  .seat-name small { display: none; }
  .seat-info { font-size: 12px; line-height: 1.3; }
  .seat-west .seat-info > span, .seat-east .seat-info > span { display: block; }
  .seat-west .seat-info .sep, .seat-east .seat-info .sep { display: none; }
  .seat-west .back + .back, .seat-east .back + .back { margin-top: calc(var(--bw) * -1.25); }
  .slot-1 { left: 2%; }
  .slot-3 { right: 2%; }
  .statusbar { font-size: 14px; }
}

@media (max-height: 700px) and (min-width: 641px) {
  :root { --cw: clamp(52px, 9vw, 80px); --tw: clamp(44px, 7vw, 66px); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* Keep a hinted or keyboard-focused card above its overlapping neighbours. */
.hand .card.hint, .hand .card:focus-visible { z-index: 2; }
