:root {
  --felt: #13513c;
  --felt-dark: #0b2e22;
  --felt-light: #1d6b50;
  --ink: #f4efe2;
  --ink-soft: #b9cfc4;
  --gold: #e8b64c;
  --red: #b3261e;
  --black: #1d1d1f;
  --paper: #fffaf0;
  --card-face: #f5eedc;
  --deco-ink: #1d2338;
  --deco-red: #a3242b;
  --deco-gold: #b8913f;
  --deco-gold-light: #c9a24a;
  --back-ink: #1d2338;
  --cw: clamp(48px, 12.5vw, 96px);   /* card width */
  --ch: calc(var(--cw) * 1.42);       /* card height */
  --radius: calc(var(--cw) * 0.09);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: dark;
}
@media (max-height: 640px) { :root { --cw: clamp(40px, 9.5vh, 80px); } }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: radial-gradient(ellipse at 50% 40%, var(--felt-light), var(--felt) 45%, var(--felt-dark) 100%) fixed;
  color: var(--ink);
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}
#app { height: 100%; }
.screen { height: 100%; }

/* ---------- buttons & forms */
.btn {
  font: inherit; font-weight: 600; font-size: 0.95rem;
  border: 1px solid rgba(255,255,255,0.25); background: rgba(0,0,0,0.2); color: var(--ink);
  padding: 0.55em 1.1em; border-radius: 999px; cursor: pointer;
  transition: transform .1s, background .15s;
}
.btn:hover { background: rgba(0,0,0,0.32); }
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--gold); color: #2a1c00; border-color: transparent; }
.btn.primary:hover { background: #f2c766; }
.btn.warn { background: #c9573c; border-color: transparent; color: #fff; }
.btn.warn:hover { background: #d86a4f; }
.btn.ghost { background: transparent; border-color: transparent; padding: 0.45em 0.7em; }
.btn.ghost:hover { background: rgba(255,255,255,0.08); }
.btn:focus-visible, select:focus-visible, .card:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

/* ---------- home */
.home { overflow-y: auto; padding: max(24px, env(safe-area-inset-top)) 16px 32px; }
.brand { text-align: center; margin: 4vh 0 28px; }
.brand h1 { font-family: Georgia, "Times New Roman", serif; font-size: clamp(2.6rem, 8vw, 4rem); margin: 0; letter-spacing: 0.02em; }
.brand p { margin: 6px 0 0; color: var(--ink-soft); }
.tiles { display: grid; gap: 16px; max-width: 640px; margin: 0 auto; }
.game-tile {
  background: rgba(0,0,0,0.22); border: 1px solid rgba(255,255,255,0.1);
  border-radius: 20px; padding: 20px; box-shadow: 0 10px 30px rgba(0,0,0,0.25);
}
.tile-head { display: flex; gap: 18px; align-items: center; }
.tile-head h2 { margin: 0 0 4px; font-family: Georgia, serif; font-size: 1.6rem; }
.tile-head p { margin: 0; color: var(--ink-soft); line-height: 1.4; }
.tile-cards { position: relative; width: 96px; height: 80px; flex: none; }
.tile-cards .card { --cw: 46px; position: absolute; }
.tile-cards .card:nth-child(1) { left: 0; top: 10px; transform: rotate(-12deg); }
.tile-cards .card:nth-child(2) { left: 24px; top: 4px; }
.tile-cards .card:nth-child(3) { left: 48px; top: 10px; transform: rotate(12deg); }
.fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; margin: 18px 0; }
.field { display: grid; gap: 4px; font-size: 0.85rem; color: var(--ink-soft); }
.field select {
  font: inherit; font-size: 1rem; color: var(--ink); background: rgba(0,0,0,0.3);
  border: 1px solid rgba(255,255,255,0.2); border-radius: 10px; padding: 0.5em 0.6em;
}
.tile-foot { display: flex; align-items: center; gap: 8px; }
.tile-foot .stats { margin-right: auto; color: var(--ink-soft); font-size: 0.9rem; }

dialog.rules {
  max-width: min(560px, calc(100vw - 32px)); border: none; border-radius: 18px; padding: 22px 24px;
  background: #fbf6ea; color: #222; line-height: 1.5; box-shadow: 0 20px 60px rgba(0,0,0,0.5);
}
dialog.rules::backdrop { background: rgba(0,0,0,0.55); }
dialog.rules h2 { margin-top: 0; font-family: Georgia, serif; }
dialog.rules form { text-align: right; margin-top: 12px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 16px; }
dialog.rules .btn:not(.primary):not(.warn) { color: #222; border-color: #bbb; background: transparent; }

/* online play panel */
.online-form { display: grid; gap: 12px; }
dialog.online .field { color: #555; }
dialog.online select, dialog.online input {
  font: inherit; font-size: 1rem; color: #222; background: #fff; border: 1px solid #c9c2b1; border-radius: 10px; padding: 0.5em 0.6em;
}
dialog.online select:disabled, dialog.online input:disabled { opacity: 0.6; }
.online-settings { margin: 0; color: #666; font-size: 0.85rem; }
.online-status { min-height: 1.5em; margin: 12px 0 0; font-weight: 600; color: #1d6b50; }
.online-status:empty { display: none; }
.online-banner {
  background: #c9573c; color: #fff; text-align: center; font-weight: 600; font-size: 0.9rem;
  padding: 6px max(16px, env(safe-area-inset-left, 0px));
}
.chip.role.offline { background: rgba(201,87,60,0.9); color: #fff; }
/* online turn clock */
.chip.timer { font-variant-numeric: tabular-nums; background: rgba(232,182,76,0.9); color: #2a1c00; font-weight: 700; min-width: 3.2em; text-align: center; }
.chip.timer.urgent { background: #d84a3a; color: #fff; animation: timer-pulse 1s ease-in-out infinite; }
@keyframes timer-pulse { 50% { transform: scale(1.08); } }

/* ---------- settings: theme picker */
.settings h3 { margin: 0 0 12px; font-size: 1rem; color: var(--ink-soft); font-weight: 600; }
.theme-options { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 10px; }
.theme-option {
  font: inherit; color: var(--ink); background: rgba(255,255,255,0.04); border: 2px solid transparent;
  border-radius: 14px; padding: 8px; cursor: pointer; display: grid; gap: 6px; text-align: center;
}
.theme-option:hover { background: rgba(255,255,255,0.08); }
.theme-option[aria-pressed="true"] { border-color: var(--gold); }
.theme-option .swatch { display: block; height: 54px; border-radius: 9px; }
.theme-name { font-size: 0.82rem; }

/* ---------- game shell */
.game-shell { height: 100%; display: flex; flex-direction: column; }
.topbar {
  display: flex; align-items: center; gap: 8px; padding: max(6px, env(safe-area-inset-top)) 10px 6px;
  background: rgba(0,0,0,0.18);
}
.topbar .title { flex: 1; text-align: center; font-weight: 600; color: var(--ink-soft); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-right { display: flex; }
.game-body { flex: 1; min-height: 0; position: relative; }

.durak {
  display: grid; grid-template-rows: auto 1fr auto auto; gap: 4px;
  padding: 6px 12px max(10px, env(safe-area-inset-bottom)); max-width: 1100px; margin: 0 auto;
}
.opp-zone, .me-zone { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.opp-seat { display: flex; flex-direction: column; align-items: center; gap: 6px; width: 100%; min-width: 0; }
/* two opponents: side by side, smaller fans */
.opp-zone.seats-2 { flex-direction: row; align-items: flex-start; gap: 16px; }
.opp-zone.seats-2 .opp-seat { flex: 1; }
.opp-zone.seats-2 .opp-hand { --cw: calc(clamp(48px, 12.5vw, 96px) * 0.6); max-width: 360px; }
@media (max-width: 520px) {
  .opp-zone.seats-2 { gap: 8px; }
  .opp-zone.seats-2 .seat-bar { padding: 3px 8px 3px 3px; gap: 5px; font-size: 0.8rem; max-width: 100%; }
  .opp-zone.seats-2 .seat-bar .chip:not(.role) { display: none; } /* incl. the small timer: the big turn clock shows it */
  .opp-zone.seats-2 .avatar { width: 22px; height: 22px; font-size: 0.8rem; }
  .opp-zone.seats-2 .chip { font-size: 0.68rem; padding: 2px 6px; }
  .opp-zone.seats-2 .chip.role.co { display: none; }
}
.seat-bar { display: flex; align-items: center; gap: 8px; padding: 4px 12px 4px 4px; border-radius: 999px; background: rgba(0,0,0,0.18); transition: box-shadow .2s; }
.seat-bar.active { box-shadow: 0 0 0 2px var(--gold); }
.avatar { width: 28px; height: 28px; display: grid; place-items: center; background: rgba(255,255,255,0.12); border-radius: 50%; }
.seat-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chip { white-space: nowrap; }
.chip { font-size: 0.75rem; padding: 2px 8px; border-radius: 999px; background: rgba(255,255,255,0.12); color: var(--ink-soft); }
.chip.role.atk { background: rgba(201,87,60,0.85); color: #fff; }
.chip.role.def { background: rgba(70,130,180,0.85); color: #fff; }
.chip.role.co { background: rgba(255,255,255,0.14); color: var(--ink); }
.chip.role.out { background: rgba(232,182,76,0.85); color: #2a1c00; }

/* hands: overlap automatically to fit the width */
.hand { display: flex; justify-content: center; width: 100%; padding: 0 4px; }
.hand > .card + .card { margin-left: min(6px, calc((100% - var(--n) * var(--cw)) / max(1, var(--n) - 1))); }
.opp-hand { --cw: calc(clamp(48px, 12.5vw, 96px) * 0.72); }
.opp-hand > .card + .card { margin-left: min(-10px, calc((100% - var(--n) * var(--cw)) / max(1, var(--n) - 1))); }
.my-hand { padding-top: 14px; }
/* overlapping cards: only the last card shows its bottom-right index (see markCrowded) */
.hand.crowded > .card:not(:last-child) .corner.br { visibility: hidden; }

/* ---------- card (Art deco design; artwork in src/ui/cardArt.ts) */
.card {
  position: relative; flex: none; width: var(--cw); height: calc(var(--cw) * 1.42);
  border-radius: calc(var(--cw) * 0.065); background: var(--card-face);
  box-shadow: 0 1px 2px rgba(0,0,0,0.2), 0 4px 12px rgba(0,0,0,0.28);
  font-family: Didot, "Bodoni MT", "Bodoni 72", "Playfair Display", Georgia, serif;
  padding: 0; border: 0; color: var(--deco-ink); overflow: hidden;
  user-select: none; transition: transform .16s ease, box-shadow .16s ease, filter .16s;
}
button.card { cursor: pointer; }
.card.red { color: var(--deco-red); }
.card svg { display: block; fill: currentColor; }
/* gold double border */
.card::before {
  content: ""; position: absolute; inset: 4.5%; pointer-events: none;
  border: 1px solid var(--deco-gold); border-radius: calc(var(--cw) * 0.035);
  box-shadow: inset 0 0 0 calc(var(--cw) * 0.022) var(--card-face), inset 0 0 0 calc(var(--cw) * 0.022 + 1px) var(--deco-gold);
}
.card .corner { position: absolute; z-index: 1; display: flex; flex-direction: column; align-items: center; width: calc(var(--cw) * 0.2); }
.card .corner b { font-size: calc(var(--cw) * 0.2); font-weight: 400; line-height: 1; letter-spacing: -0.04em; }
.card .corner .idx { width: calc(var(--cw) * 0.115); height: calc(var(--cw) * 0.115); }
.card .tl { top: 8.5%; left: 8%; }
.card .br { bottom: 8.5%; right: 8%; transform: rotate(180deg); }
.card .medallion { position: absolute; left: 50%; top: 50%; width: 62%; height: auto; aspect-ratio: 1; transform: translate(-50%, -50%); color: var(--deco-gold); }
.card .centre-suit { position: absolute; left: 50%; top: 50%; width: 30%; height: auto; aspect-ratio: 1; transform: translate(-50%, -50%); }
.card .centre-suit.small { width: 16%; top: 70%; }
.card .monogram {
  position: absolute; left: 50%; top: 50%; transform: translate(-50%, -54%);
  font-size: calc(var(--cw) * 0.46); font-style: italic; line-height: 1;
}

/* back: dark panel + gold sunburst; panel colour follows the theme (--back-ink) */
.card.back { background: var(--back-ink); color: var(--deco-gold-light); }
.card.back::before { border-color: var(--deco-gold-light); box-shadow: inset 0 0 0 calc(var(--cw) * 0.022) var(--back-ink), inset 0 0 0 calc(var(--cw) * 0.022 + 1px) var(--deco-gold-light); }
.card.back .medallion { width: 80%; color: var(--deco-gold-light); }
.card.back .centre-suit { width: 22%; }
.card.is-trump::after {
  content: ""; position: absolute; inset: 0; border-radius: inherit; pointer-events: none;
  box-shadow: inset 0 0 0 2px rgba(232,182,76,0.75);
}
.card.playable { transform: translateY(-10px); box-shadow: 0 0 0 2px var(--gold), 0 10px 18px rgba(0,0,0,0.35); }
.card.playable:hover { transform: translateY(-18px); }
.card.dim { filter: brightness(0.72) saturate(0.7); }
.card.shake { animation: shake .3s; }
@keyframes shake { 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }

/* ---------- middle: deck, table, discard */
.middle { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 10px; min-height: calc(var(--ch) * 1.35); }
.deck-area, .discard-area {
  /* piles are drawn larger than table cards (--pile-scale) */
  --cw: calc(clamp(48px, 12.5vw, 96px) * var(--pile-scale));
  --ch: calc(var(--cw) * 1.42);
  display: flex; flex-direction: column; align-items: center; gap: 6px; width: calc(var(--cw) * 1.3);
}
:root { --pile-scale: 1.35; }
@media (max-height: 640px) { .deck-area, .discard-area { --cw: calc(clamp(40px, 9.5vh, 80px) * var(--pile-scale)); } }
/* the face-up trump sticks out sideways: give it room and keep it above the table */
.deck-area { position: relative; z-index: 1; width: calc(var(--cw) * 1.75); align-items: flex-start; }
.deck-area .deck-label { width: var(--cw); }
.deck-stack { position: relative; width: var(--cw); height: var(--ch); }
.deck-stack .card { position: absolute; inset: 0; }
.deck-stack .trump-card { transform: rotate(90deg) translateY(-28%); }
.deck-stack .deck-top { box-shadow: 2px 2px 0 rgba(0,0,0,0.25), 4px 4px 0 rgba(0,0,0,0.15), 0 6px 14px rgba(0,0,0,0.3); }
.trump-only { position: absolute; inset: 0; display: grid; place-items: center; font-size: calc(var(--cw) * 0.7); color: var(--ink); opacity: 0.8;
  border: 2px dashed rgba(255,255,255,0.25); border-radius: var(--radius); }
.trump-only.red { color: #ff8a80; }
.deck-label { white-space: nowrap; text-align: center; font-size: 0.9rem; color: var(--ink-soft); line-height: 1.4; }
.deck-label b { color: var(--ink); font-size: 1.1rem; }
.deck-label .suit { font-size: 1.2rem; }
.suit { font-size: 1.05rem; color: var(--ink); }
.suit.red { color: #ff8a80; }
.discard-pile { position: relative; width: var(--cw); height: var(--ch); }
.discard-pile .card { position: absolute; inset: 0; transform: rotate(-8deg); }
.discard-pile .card.skew { transform: rotate(11deg); }
.slot { width: var(--cw); height: var(--ch); border: 2px dashed rgba(255,255,255,0.15); border-radius: var(--radius); }

.battlefield {
  position: relative; align-self: stretch; display: grid; place-items: center;
  border-radius: 24px; background: rgba(0,0,0,0.10); box-shadow: inset 0 0 40px rgba(0,0,0,0.18); padding: 10px;
}
.pairs { display: flex; flex-wrap: wrap; justify-content: center; gap: calc(var(--cw) * 0.15) calc(var(--cw) * 0.45); }
.pair { position: relative; width: calc(var(--cw) * 1.22); height: calc(var(--ch) * 1.16); }
.pair .card { position: absolute; left: 0; top: 0; }
.pair .card.defense { left: calc(var(--cw) * 0.22); top: calc(var(--ch) * 0.16); transform: rotate(8deg); }
.empty-field { color: var(--ink-soft); text-align: center; padding: 0 12px; font-size: 0.95rem; max-width: 34ch; }
.limit { position: absolute; top: 8px; right: 12px; font-size: 0.75rem; color: var(--ink-soft); }

/* ---------- status */
.status { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 6px 14px; min-height: 48px; text-align: center; }

/* online turn clock: a ring that empties as time runs out */
.turn-clock { position: relative; width: 64px; height: 64px; flex: none; display: grid; place-items: center; --clock: var(--gold); }
.turn-clock svg { position: absolute; inset: 0; width: 100%; height: 100%; transform: rotate(-90deg); }
.turn-clock .track { fill: rgba(0,0,0,0.3); stroke: rgba(255,255,255,0.14); stroke-width: 3; }
.turn-clock .left { fill: none; stroke: var(--clock); stroke-width: 3.4; stroke-linecap: round; stroke-dasharray: 100 100; transition: stroke-dashoffset 0.25s linear, stroke 0.3s; }
.turn-clock .secs { position: relative; font-size: 1.45rem; font-weight: 800; font-variant-numeric: tabular-nums; color: var(--clock); line-height: 1; }
.turn-clock .who { position: absolute; top: calc(100% + 2px); left: 50%; transform: translateX(-50%); font-size: 0.68rem; color: var(--ink-soft); white-space: nowrap; }
.turn-clock.urgent { --clock: #ff6b57; animation: clock-pulse 1s ease-in-out infinite; }
@keyframes clock-pulse { 50% { transform: scale(1.1); } }
@media (max-width: 520px) { .turn-clock { width: 52px; height: 52px; } .turn-clock .secs { font-size: 1.2rem; } }
.prompt { font-weight: 600; }
.note { color: var(--ink-soft); font-size: 0.9rem; }
.actions { display: flex; gap: 8px; }

/* ---------- result overlay */
.overlay { position: absolute; inset: 0; display: grid; place-items: center; background: rgba(0,0,0,0.45); animation: fade .25s; padding: 16px; }
@keyframes fade { from { opacity: 0; } }
.result { background: #fbf6ea; color: #222; border-radius: 22px; padding: 26px 30px; text-align: center; min-width: min(320px, 100%); box-shadow: 0 20px 60px rgba(0,0,0,0.5); }
.result h2 { margin: 6px 0; font-family: Georgia, serif; font-size: 2rem; }
.result.win h2 { color: #1d6b50; }
.result.lose h2 { color: var(--red); }
.result p { margin: 0 0 18px; color: #555; }
.result .actions { justify-content: center; }
.result .btn:not(.primary) { color: #222; border-color: #bbb; background: transparent; }
.result-icon { font-size: 2.6rem; }

@media (max-width: 520px) {
  .topbar .title { font-size: 0.9rem; }
  :root { --pile-scale: 1.2; }
  .discard-area { width: auto; min-width: var(--cw); }
  .deck-area { width: calc(var(--cw) * 1.62); }
  .deck-label { font-size: 0.78rem; }
  .deck-label b { font-size: 0.95rem; }
  .middle { gap: 6px; }
  .pairs { gap: 4px calc(var(--cw) * 0.3); }
}
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }

/* =================================================================== themes
   Default (no data-table-theme / "felt") is the classic green table above. */

/* B · Midnight glow */
html[data-table-theme="midnight"] body {
  background:
    radial-gradient(60% 50% at 50% 45%, rgba(99,102,241,.28), transparent 70%),
    radial-gradient(40% 40% at 85% 10%, rgba(56,189,248,.18), transparent 70%),
    linear-gradient(180deg, #0b1020, #121a33 60%, #0a0f1e) fixed;
}
html[data-table-theme="midnight"] .battlefield {
  background: rgba(255,255,255,.04); border: 1px solid rgba(255,255,255,.08);
  box-shadow: 0 20px 60px rgba(0,0,0,.35); backdrop-filter: blur(6px);
}
html[data-table-theme="midnight"] .topbar { background: rgba(255,255,255,.03); border-bottom: 1px solid rgba(255,255,255,.06); }
html[data-table-theme="midnight"] { --back-ink: #1b1d4a; }

/* C · Emerald mesh */
html[data-table-theme="emerald"] body {
  background:
    radial-gradient(45% 55% at 15% 20%, rgba(16,185,129,.45), transparent 70%),
    radial-gradient(50% 50% at 90% 80%, rgba(20,184,166,.35), transparent 70%),
    radial-gradient(60% 60% at 60% 30%, rgba(6,95,70,.6), transparent 75%),
    #04241c fixed;
}
html[data-table-theme="emerald"] .battlefield {
  background: rgba(255,255,255,.06); border: 1px solid rgba(255,255,255,.12); border-radius: 28px;
  box-shadow: inset 0 1px 0 rgba(255,255,255,.08), 0 24px 60px rgba(0,0,0,.3); backdrop-filter: blur(10px);
}
html[data-table-theme="emerald"] .topbar { background: transparent; }
html[data-table-theme="emerald"] { --back-ink: #0b3a31; }

/* D · Graphite & gold */
html[data-table-theme="graphite"] body { background: radial-gradient(70% 60% at 50% 40%, #2a2a2e, #161618 70%, #0e0e10) fixed; }
html[data-table-theme="graphite"] body::before {
  content: ""; position: fixed; inset: 0; pointer-events: none; opacity: .07;
  background-image: radial-gradient(rgba(255,255,255,.9) 1px, transparent 1px); background-size: 22px 22px;
}
html[data-table-theme="graphite"] .battlefield {
  background: linear-gradient(180deg, rgba(255,255,255,.035), rgba(255,255,255,.01));
  border: 1px solid rgba(232,182,76,.25); box-shadow: 0 0 0 6px rgba(232,182,76,.04), 0 24px 60px rgba(0,0,0,.45);
}
html[data-table-theme="graphite"] .topbar { background: rgba(0,0,0,.25); }
html[data-table-theme="graphite"] { --back-ink: #18181b; }
