:root {
  --felt: #0e5a3f;
  --felt-dark: #0a3f2c;
  --ink: #f3f1e8;
  --ink-dim: #b9cbbf;
  --card: #fdfcf7;
  --card-edge: #d6d2c4;
  --red: #c62828;
  --black: #1c1c1c;
  --accent: #f5c542;
  --danger: #ff8a80;
  --card-w: clamp(56px, 14vw, 88px);
  --card-h: calc(var(--card-w) * 1.452); /* matches the card art's proportions */
  --mini-w: clamp(26px, 6.5vw, 38px);
  --role-boss: #f5c542;
  --role-foreman: #64b5f6;
  --role-worker: #a5d6a7;
  --role-welfare: #ef9a9a;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: radial-gradient(ellipse at center, var(--felt) 0%, var(--felt-dark) 100%) fixed;
  color: var(--ink);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100%;
  max-width: 900px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) 12px env(safe-area-inset-bottom);
}

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 4px;
}
.topbar h1 { margin: 0; font-size: 1.3rem; letter-spacing: .04em; }
.round-label { color: var(--ink-dim); font-size: .9rem; flex: 1; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 50%;
  border: 1px solid rgba(255,255,255,.35); background: rgba(0,0,0,.2);
  color: var(--ink); font-size: 1.1rem; cursor: pointer;
}

/* ---------- Table ---------- */
.table {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas: ". top last" "left center right";
  gap: 8px;
  min-height: 260px;
}
.seat-top { grid-area: top; justify-self: center; }
.seat-left { grid-area: left; align-self: center; }
.seat-right { grid-area: right; align-self: center; }
.center { grid-area: center; }

.last-lap {
  grid-area: last; justify-self: end; align-self: start;
  display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 6px 10px; border-radius: 10px;
  background: rgba(0,0,0,.25); border: 1px solid rgba(255,255,255,.15);
  font-size: .85rem;
}
.last-lap[hidden] { display: none; }
.last-title { font-size: .68rem; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-dim); }
.last-cards .cards { display: inline-flex; gap: 2px; }

.seat {
  display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 8px; border-radius: 12px;
  border: 2px solid transparent;
  transition: border-color .2s, background .2s;
  min-width: 0;
}
.seat.turn, .me.turn { border-color: var(--accent); background: rgba(245,197,66,.08); }
.seat.out { opacity: .6; }
.seat-head { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 4px 6px; }
.name { font-weight: 600; }

.role {
  font-size: .72rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em;
  padding: 2px 7px; border-radius: 999px; color: #1b1b1b;
}
.role-Boss { background: var(--role-boss); }
.role-Foreman { background: var(--role-foreman); }
.role-Worker { background: var(--role-worker); }
.role-Welfare { background: var(--role-welfare); }

.backs { display: flex; align-items: center; gap: 6px; }
.back {
  width: var(--mini-w); height: calc(var(--mini-w) * 1.452); border-radius: 3px;
  background: url("../cards/back.svg") center / 100% 100% no-repeat;
  box-shadow: 0 1px 3px rgba(0,0,0,.4);
}
/* Opponent's hand: a small fan of card backs */
.stack {
  --back-w: clamp(30px, 7.5vw, 44px);
  position: relative;
  width: calc(var(--back-w) * 1.7); height: calc(var(--back-w) * 1.6);
}
.stack .back {
  position: absolute; left: 50%; bottom: 0;
  width: var(--back-w); height: calc(var(--back-w) * 1.452);
  transform-origin: 50% 100%;
}
.stack .back:nth-child(1) { transform: translateX(-50%) rotate(-12deg); }
.stack .back:nth-child(2) { transform: translateX(-50%); }
.stack .back:nth-child(3) { transform: translateX(-50%) rotate(12deg); }
.stack .back:only-child { transform: translateX(-50%); }
.count { font-variant-numeric: tabular-nums; font-weight: 700; }

.bubble {
  min-height: calc(var(--mini-w) * 1.4);
  display: flex; align-items: center; gap: 2px;
  font-size: .85rem; color: var(--ink-dim);
}
.bubble.pass { font-style: italic; }
.place { color: var(--accent); font-weight: 700; }

/* ---------- Center pile: felt play mat ---------- */
.center {
  position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px;
  padding: 20px 12px;
  border-radius: 28px;
  background:
    /* fine fibre texture */
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.9' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .55 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E"),
    radial-gradient(ellipse at 50% 45%, #a3232f 0%, #861a25 55%, #5e111a 100%);
  background-blend-mode: soft-light, normal;
  box-shadow:
    inset 0 0 0 3px #430b12,          /* mat edge */
    inset 0 0 40px rgba(0,0,0,.45),   /* sunken edges */
    0 8px 24px rgba(0,0,0,.35);
}
/* stitched border */
.center::before {
  content: ""; position: absolute; inset: 10px;
  border: 2px dashed rgba(240,230,200,.28);
  border-radius: 20px;
  pointer-events: none;
}
.center > * { position: relative; }
.trick-label { text-align: center; font-size: .95rem; min-height: 1.3em; }
.pile { display: flex; gap: 4px; min-height: var(--card-h); align-items: center; }
.pile .card { animation: drop .25s ease-out; }
@keyframes drop { from { transform: translateY(-16px) scale(1.05); opacity: 0; } }
.lap { display: flex; flex-wrap: wrap; justify-content: center; gap: 4px; margin: 0; padding: 0; list-style: none; font-size: .75rem; }
.lap li { padding: 2px 8px; border-radius: 999px; background: rgba(255,255,255,.08); color: var(--ink-dim); }
.lap li.done { text-decoration: line-through; opacity: .55; }
.lap li.now { background: var(--accent); color: #1b1b1b; font-weight: 700; }

/* ---------- Cards ----------
 * Faces are PNGs from the public-domain deck in /cards (see cards/LICENSE.txt).
 * Full cards show the whole image; mini cards show just the top-left corner index.
 */
.card {
  position: relative; flex: none;
  width: var(--card-w); height: var(--card-h);
  background-color: transparent;
  background-repeat: no-repeat;
  background-size: 100% 100%;
  border-radius: calc(var(--card-w) * .055);
  box-shadow: 0 2px 4px rgba(0,0,0,.35);
}
.card.mini {
  width: var(--mini-w); height: calc(var(--mini-w) * 1.4); border-radius: 4px;
  background-color: var(--card);
  background-size: 300% auto;
  background-position: 0 0;
}

/* ---------- My area ---------- */
.me {
  margin-top: 8px; padding: 8px; border-radius: 14px;
  border: 2px solid transparent; background: rgba(0,0,0,.12);
}
.me-info { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.toggle {
  margin-left: auto; display: inline-flex; align-items: center; gap: 8px;
  font-size: .85rem; color: var(--ink-dim); cursor: pointer;
}
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.switch {
  position: relative; width: 38px; height: 22px; border-radius: 999px; flex: none;
  background: rgba(255,255,255,.2); transition: background .15s;
}
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 16px; height: 16px;
  border-radius: 50%; background: #fff; transition: transform .15s;
}
.toggle input:checked + .switch { background: var(--accent); }
.toggle input:checked + .switch::after { transform: translateX(16px); }
.toggle input:focus-visible + .switch { outline: 2px solid #fff; outline-offset: 2px; }

.status { min-height: 1.4em; margin: 6px 2px; font-size: .95rem; }
.status.error { color: var(--danger); }

.hand {
  display: flex; justify-content: center;
  padding: 18px 0 6px;
  min-height: calc(var(--card-h) + 24px);
}
.hand .card {
  cursor: pointer;
  transition: transform .15s ease, box-shadow .15s ease;
}
.hand .card.selected { transform: translateY(-16px); box-shadow: 0 0 0 3px var(--accent), 0 6px 10px rgba(0,0,0,.4); }
.hand.disabled .card { cursor: default; }
.hand .card.unplayable { filter: grayscale(1) brightness(.6); cursor: not-allowed; }
.hand .card.received { outline: 3px dashed var(--accent); outline-offset: -3px; }

.actions { display: flex; gap: 10px; justify-content: center; padding: 6px 0 10px; }
.btn {
  min-width: 120px; min-height: 48px; padding: 0 20px;
  border-radius: 12px; border: 1px solid rgba(255,255,255,.35);
  background: rgba(0,0,0,.25); color: var(--ink);
  font-size: 1.05rem; font-weight: 600; cursor: pointer;
}
.btn.primary { background: var(--accent); color: #1b1b1b; border-color: var(--accent); }
.btn:disabled { opacity: .35; cursor: default; }

/* ---------- Dialog ---------- */
dialog {
  width: min(92vw, 460px); max-height: 85vh; overflow: auto;
  border: none; border-radius: 16px; padding: 20px;
  background: #fbfaf5; color: #1b1b1b;
  box-shadow: 0 10px 40px rgba(0,0,0,.5);
}
dialog::backdrop { background: rgba(0,0,0,.55); }
dialog h2 { margin: 0 0 12px; }
dialog h3 { margin: 16px 0 6px; font-size: 1rem; }
dialog p, dialog li { line-height: 1.45; }
dialog ul, dialog ol { padding-left: 20px; }
dialog .row { display: flex; align-items: center; gap: 8px; margin: 6px 0; flex-wrap: wrap; }
dialog .cards { display: inline-flex; gap: 3px; }
dialog .btn { width: 100%; margin-top: 16px; }
dialog .you { font-weight: 700; }
.results { list-style: none; padding: 0; }
.results li { display: flex; align-items: center; gap: 10px; padding: 6px 0; border-bottom: 1px solid #e6e2d4; }
.results .stat { margin-left: auto; color: #666; font-size: .85rem; }

/* ---------- Large screens: use the whole window ---------- */
@media (min-width: 900px) {
  :root {
    --card-w: clamp(72px, 6vw, 100px);
    --mini-w: clamp(32px, 2.6vw, 44px);
  }
  #app { max-width: min(1700px, 100%); padding-left: 24px; padding-right: 24px; }
  .table {
    grid-template-columns: minmax(180px, 1fr) minmax(480px, 2.4fr) minmax(180px, 1fr);
    grid-template-rows: auto 1fr;
    gap: 16px 32px;
    min-height: 420px;
  }
  .seat-left { justify-self: start; }
  .seat-right { justify-self: end; }
  .seat-top { align-self: start; }
  .stack { --back-w: clamp(44px, 3.6vw, 60px); }
  .center {
    align-self: center;
    height: clamp(320px, 52vh, 560px);
    margin: 8px 0 16px;
    border-radius: 50%;  /* oval mat */
    padding: 40px 48px;
  }
  .center::before { inset: 14px; border-radius: inherit; }
  .me { max-width: 1100px; width: 100%; margin-left: auto; margin-right: auto; }
}

/* ---------- Small screens ---------- */
@media (max-width: 520px) {
  #app { padding-left: 8px; padding-right: 8px; }
  .table { grid-template-columns: 1fr 1.6fr 1fr; gap: 4px; }
  .seat { padding: 6px 2px; }
  .name { font-size: .85rem; }
  .role { font-size: .62rem; padding: 2px 5px; }
  .btn { min-width: 0; flex: 1; }
}

/* ---------- Screens: home, room lobby, game ---------- */
.screen { display: none; }
body[data-screen="home"] #home,
body[data-screen="room"] #room { display: flex; }
body[data-screen="game"] #app { display: flex; }

.panel-screen {
  min-height: 100%;
  align-items: center; justify-content: center;
  padding: calc(env(safe-area-inset-top) + 16px) 16px calc(env(safe-area-inset-bottom) + 16px);
}
.panel {
  width: min(100%, 420px);
  display: flex; flex-direction: column; gap: 12px;
  padding: 24px; border-radius: 18px;
  background: rgba(0,0,0,.25); border: 1px solid rgba(255,255,255,.15);
  box-shadow: 0 10px 40px rgba(0,0,0,.3);
}
.logo { margin: 0; text-align: center; font-size: 2.2rem; letter-spacing: .06em; }
.tagline { margin: -6px 0 6px; text-align: center; color: var(--ink-dim); }
.panel h2 { margin: 10px 0 0; font-size: 1rem; text-transform: uppercase; letter-spacing: .08em; color: var(--ink-dim); text-align: center; }
.btn.wide { width: 100%; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: .9rem; color: var(--ink-dim); }
.panel input {
  min-height: 48px; padding: 0 14px; border-radius: 12px;
  border: 1px solid rgba(255,255,255,.35); background: rgba(0,0,0,.25);
  color: var(--ink); font-size: 1.05rem; user-select: text;
}
.panel input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.online-box { display: flex; flex-direction: column; gap: 12px; }
.online-box[hidden], .share-box[hidden], .panel .btn[hidden] { display: none; }
.join-form { display: flex; gap: 8px; }
.join-form input { flex: 1; min-width: 0; text-transform: uppercase; letter-spacing: .3em; text-align: center; font-weight: 700; }
.join-form .btn { min-width: 96px; }
.note { margin: 0; color: var(--ink-dim); font-size: .9rem; line-height: 1.45; }
.msg { margin: 0; min-height: 1.2em; color: var(--danger); text-align: center; }

.share-box p { margin: 0 0 6px; color: var(--ink-dim); font-size: .9rem; }
.share-row { display: flex; gap: 8px; }
.share-row input { flex: 1; min-width: 0; font-size: .9rem; }
.share-row .btn { min-width: 90px; }
.room-seats { margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 6px; counter-reset: seat; }
.room-seats li {
  padding: 10px 14px; border-radius: 10px; background: rgba(255,255,255,.08);
  font-weight: 600; overflow-wrap: anywhere;
}
.room-seats li.you { outline: 2px solid var(--accent); }
.room-seats li.empty { font-weight: 400; font-style: italic; color: var(--ink-dim); }
.seat-tags { font-weight: 400; color: var(--ink-dim); }

.conn-banner {
  position: fixed; top: 0; left: 0; right: 0; z-index: 10;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  background: #8a1c1c; color: #fff; text-align: center; font-weight: 600;
}
.conn-banner[hidden] { display: none; }

.tag {
  font-size: .62rem; font-weight: 700; letter-spacing: .05em;
  padding: 1px 5px; border-radius: 4px;
  border: 1px solid rgba(255,255,255,.4); color: var(--ink-dim);
}
.tag.away { border-color: var(--danger); color: var(--danger); }
.name { overflow-wrap: anywhere; }

dialog .note { color: #666; font-size: .85rem; }
.confirm-row { display: flex; gap: 10px; }
.confirm-row .btn { flex: 1; }
#confirm-dialog .btn { margin-top: 8px; }
#confirm-dialog .btn:not(.primary) { background: #eee; color: #1b1b1b; border-color: #ccc; }
.actions .btn[hidden] { display: none; }
body:not([data-screen="game"]) #app { display: none; }
