:root {
  --felt: #17603b;
  --felt-dark: #0f4a2d;
  --felt-edge: #0a3620;
  --gold: #e7c66b;
  --ink: #10241a;
  --paper: #f6f4ee;
  --card-w: 112px;
  --card-h: 157px;
  --accent: #2f6fed;
  --danger: #d8543f;
  --shadow: 0 8px 24px rgba(0, 0, 0, .35);
  --radius: 14px;
  font-family: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body {
  margin: 0; height: 100%;
  background: radial-gradient(circle at 50% 20%, #22343f, #141c22 70%);
  color: #f2f2f2;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

.screen { display: none; height: 100vh; height: 100dvh; flex-direction: column; }
.screen.active { display: flex; }

/* ---------- buttons & chips ---------- */
.btn {
  font: inherit; border: none; border-radius: 10px; padding: .55em 1em;
  cursor: pointer; color: #fff; background: #33424d; transition: transform .08s, background .15s, opacity .15s;
  font-weight: 600;
}
.btn:active { transform: translateY(1px); }
.btn.primary { background: linear-gradient(180deg, #3a7bf0, #2f6fed); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost { background: rgba(255,255,255,.08); }
.btn.ghost:hover { background: rgba(255,255,255,.16); }
.btn.big { font-size: 1.25rem; padding: .7em 1.8em; }
.btn:disabled { opacity: .4; cursor: not-allowed; }
.chip {
  background: rgba(255,255,255,.12); border-radius: 999px; padding: .3em .9em; font-weight: 600;
}
.muted { color: #9fb0ab; font-weight: 400; }
.error { color: var(--danger); min-height: 1.2em; margin: .4em 0 0; font-size: .9rem; }

/* ---------- auth ---------- */
#auth { align-items: center; justify-content: center; }
.auth-card {
  background: rgba(20,30,26,.85); padding: 2.4rem 2.2rem; border-radius: 20px;
  box-shadow: var(--shadow); width: min(90vw, 360px); text-align: center;
  border: 1px solid rgba(231,198,107,.25);
}
.brand { font-size: 2.4rem; margin: 0; letter-spacing: .5px; color: var(--gold); }
.brand-sm { font-weight: 800; color: var(--gold); letter-spacing: .5px; }
.tagline { margin: .2rem 0 1.6rem; color: #b9c4c0; }
.auth-card form { display: flex; flex-direction: column; gap: .7rem; }
input {
  font: inherit; padding: .7em .9em; border-radius: 10px; border: 1px solid rgba(255,255,255,.15);
  background: rgba(255,255,255,.06); color: #fff;
}
input::placeholder { color: #8b9a95; }

/* ---------- topbar ---------- */
.topbar {
  display: flex; align-items: center; gap: .6rem; padding: .6rem 1rem;
  background: rgba(0,0,0,.28); backdrop-filter: blur(6px);
}
.topbar-right { margin-left: auto; display: flex; gap: .4rem; flex-wrap: wrap; }
.table-title { font-weight: 700; }

/* ---------- lobby ---------- */
.lobby-grid {
  flex: 1; display: grid; grid-template-columns: 1.6fr 1fr; gap: 1rem; padding: 1rem;
  overflow: auto;
}
@media (max-width: 720px) { .lobby-grid { grid-template-columns: 1fr; } }
.panel {
  background: rgba(255,255,255,.05); border-radius: var(--radius); padding: 1rem 1.2rem;
  border: 1px solid rgba(255,255,255,.07);
}
.panel h2 { margin: .2rem 0 1rem; font-size: 1.1rem; }
.create-row { display: flex; gap: .5rem; margin-bottom: 1rem; }
.create-row input { flex: 1; }
.table-list, .online-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: .6rem; }
.table-row {
  display: flex; align-items: center; gap: .8rem; padding: .8rem 1rem;
  background: rgba(255,255,255,.05); border-radius: 12px; border: 1px solid rgba(255,255,255,.06);
}
.table-row .meta { flex: 1; }
.table-row .name { font-weight: 700; }
.table-row .sub { font-size: .82rem; color: #9fb0ab; }
.seat-dots { display: flex; gap: 4px; }
.seat-dot { width: 12px; height: 12px; border-radius: 50%; background: rgba(255,255,255,.18); }
.seat-dot.filled { background: var(--gold); }
.seat-dot.filled.off { background: var(--danger); }
.online-row { display: flex; align-items: center; gap: .5rem; padding: .4rem .2rem; }
.dot-online { width: 9px; height: 9px; border-radius: 50%; background: #57d38a; }
.badge { font-size: .7rem; background: rgba(231,198,107,.2); color: var(--gold); padding: .1em .5em; border-radius: 6px; }

/* ---------- felt / table ---------- */
.felt {
  position: relative; flex: 1;
  background:
    radial-gradient(ellipse at 50% 42%, var(--felt) 0%, var(--felt-dark) 62%, var(--felt-edge) 100%);
  overflow: hidden;
}
.seat { position: absolute; display: flex; flex-direction: column; align-items: center; gap: 4px; }
.seat-top    { top: 2%; left: 50%; transform: translateX(-50%); }
.seat-left   { left: 2%; top: 46%; transform: translateY(-50%); }
.seat-right  { right: 2%; top: 46%; transform: translateY(-50%); }
.seat-bottom { bottom: 3%; left: 50%; transform: translateX(-50%); }

.nameplate {
  display: flex; align-items: center; gap: .5rem; padding: .32em .9em; border-radius: 14px;
  background: rgba(0,0,0,.4); white-space: nowrap;
  border: 2px solid transparent; transition: border-color .2s, box-shadow .2s;
}
.nameplate.turn { border-color: var(--gold); box-shadow: 0 0 16px rgba(231,198,107,.6); }
.nameplate.disconnected { opacity: .5; }
.nameplate .who { display: flex; flex-direction: column; line-height: 1.12; }
.nameplate .pname { font-size: 1.12rem; font-weight: 800; letter-spacing: .2px; }
.nameplate .puser { font-size: .74rem; font-weight: 500; color: #a9bab4; }
.nameplate .empty { font-size: 1rem; font-weight: 600; color: #9fb0ab; }
.nameplate .team-tag { font-size: .74rem; font-weight: 700; background: rgba(231,198,107,.18); color: var(--gold); padding: .1em .5em; border-radius: 6px; }
.nameplate.dealer::after { content: "D"; align-self: center; font-size: .7rem; background: var(--gold); color: #222; border-radius: 5px; padding: .05em .4em; font-weight: 800; }
.timer-ring { font-variant-numeric: tabular-nums; font-size: .9rem; font-weight: 700; color: var(--gold); }

/* Trick-count icon: a little stack of cards with a number over the top. */
.trick-badge { position: relative; width: 22px; height: 30px; margin: 2px 0 0 6px; }
.trick-badge .card-stack {
  width: 18px; height: 26px; border-radius: 3px;
  background: repeating-linear-gradient(45deg, #b3202a 0 4px, #9a1a23 4px 8px);
  border: 1.5px solid #f6f4ee;
  box-shadow: 1.5px 1.5px 0 -0.5px #9a1a23, 2.5px 2.5px 0 -0.5px #f6f4ee,
              4px 4px 0 -0.5px #9a1a23, 5px 5px 0 -0.5px #f6f4ee;
}
.trick-badge .n {
  position: absolute; top: -9px; right: -10px; min-width: 19px; height: 19px;
  background: var(--gold); color: #2a1e00; font-weight: 800; font-size: .74rem;
  border-radius: 10px; display: flex; align-items: center; justify-content: center;
  padding: 0 3px; box-shadow: 0 1px 3px rgba(0,0,0,.45);
}
.seat-info { display: flex; align-items: center; gap: 6px; }

/* Seat block groups avatar + nameplate + cards; it gets the talking highlight. */
.seat-block {
  display: flex; flex-direction: column; align-items: center; gap: 5px;
  padding: 5px; border-radius: 16px; transition: background .18s, box-shadow .18s;
}
.seat-block.speaking {
  background: rgba(231,198,107,.16);
  box-shadow: 0 0 0 2px rgba(231,198,107,.5), 0 0 20px rgba(231,198,107,.32);
}

/* Animated card-player avatar. */
.avatar {
  position: relative; width: 2.5rem; height: 2.5rem; flex: none;
  display: flex; align-items: center; justify-content: center;
  border-radius: 50%; background: rgba(0,0,0,.34);
  animation: avatar-idle 3.4s ease-in-out infinite;
}
.avatar .face { font-size: 1.7rem; line-height: 1; }
.avatar .mic-dot { position: absolute; top: -7px; right: -7px; font-size: .82rem; display: none; }
@keyframes avatar-idle { 0%,100% { transform: translateY(0) rotate(-2deg); } 50% { transform: translateY(-3px) rotate(2deg); } }
.seat-block.speaking .avatar { box-shadow: 0 0 0 3px rgba(105,225,150,.85); animation: avatar-talk .5s ease-in-out infinite; }
.seat-block.speaking .avatar .mic-dot { display: block; }
@keyframes avatar-talk { 0%,100% { transform: scale(1); } 50% { transform: scale(1.13); } }

.ready-tag { font-size: .72rem; font-weight: 700; color: #a9bab4; background: rgba(255,255,255,.1); padding: .1em .5em; border-radius: 6px; }
.ready-tag.yes { color: #123; background: #6fe09a; }
.ready-box { display: flex; flex-direction: column; align-items: center; gap: .7rem; }

.avatar-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
.avatar-cell {
  font-size: 1.5rem; padding: .3em 0; background: rgba(255,255,255,.06);
  border: 2px solid transparent; border-radius: 10px; cursor: pointer; line-height: 1;
}
.avatar-cell.sel { border-color: var(--gold); background: rgba(231,198,107,.18); }
.btn.mic-on { background: linear-gradient(180deg, #3ac07a, #2ea866); color: #04240f; }
@media (max-width: 560px) { .avatar-grid { grid-template-columns: repeat(5, 1fr); } }

.backcard {
  display: inline-block; border-radius: 5px;
  background:
    repeating-linear-gradient(45deg, #b3202a 0 6px, #9a1a23 6px 12px);
  border: 2px solid #f6f4ee; box-sizing: border-box;
}
.oppo-cards { display: flex; }
.oppo-cards .backcard { width: 48px; height: 67px; margin-left: -28px; filter: drop-shadow(0 2px 4px rgba(0,0,0,.45)); }
.oppo-cards .backcard:first-child { margin-left: 0; }
.seat-left .oppo-cards, .seat-right .oppo-cards { flex-direction: column; }
.seat-left .oppo-cards .backcard, .seat-right .oppo-cards .backcard { margin-left: 0; margin-top: -48px; }
.seat-left .oppo-cards .backcard:first-child, .seat-right .oppo-cards .backcard:first-child { margin-top: 0; }
@media (max-width: 560px) {
  .oppo-cards .backcard { width: 38px; height: 53px; margin-left: -24px; }
  .seat-left .oppo-cards .backcard, .seat-right .oppo-cards .backcard { margin-top: -40px; }
}

/* Cards. The .pc wrapper owns the size; the inner custom element always fills
   it, so contexts like the trump reference can shrink cards just by sizing .pc. */
.pc { width: var(--card-w); height: var(--card-h); display: inline-block; }
.pc playing-card { display: block; width: 100%; height: 100%; }
.pc img { width: 100%; height: 100%; display: block; }

/* trick area */
.trick-area {
  position: absolute; left: 50%; top: 47%; transform: translate(-50%, -50%);
  width: min(52vw, 360px); height: min(40vh, 320px);
}
.trick-card {
  position: absolute; left: 50%; top: 50%; width: var(--card-w); height: var(--card-h);
  transition: transform .35s cubic-bezier(.2,.8,.3,1), opacity .3s;
  filter: drop-shadow(0 6px 10px rgba(0,0,0,.4));
}
.trick-card .who {
  position: absolute; bottom: -18px; left: 50%; transform: translateX(-50%);
  font-size: .7rem; background: rgba(0,0,0,.55); padding: .1em .5em; border-radius: 6px; white-space: nowrap;
}
.trick-card.win { animation: winpulse .5s ease; }
@keyframes winpulse { 50% { filter: drop-shadow(0 0 18px var(--gold)); } }

.center-msg {
  position: absolute; left: 50%; top: 47%; transform: translate(-50%,-50%);
  text-align: center; pointer-events: none; text-shadow: 0 2px 6px rgba(0,0,0,.6);
}
/* Interactive content inside the center overlay must still receive clicks. */
.center-msg .ready-box, .center-msg button { pointer-events: auto; }
.deal-panel {
  position: absolute; left: 50%; top: 55%; transform: translate(-50%,-50%);
  text-align: center; display: flex; flex-direction: column; gap: .6rem; align-items: center;
}

/* Session scorecard — tucked into the top-left corner so it never covers the
   top player's nameplate. */
.scorestrip {
  position: absolute; top: 10px; left: 10px;
  display: flex; flex-direction: column; gap: .22rem;
  background: rgba(0,0,0,.44); padding: .6em .85em .7em; border-radius: 12px;
  font-size: 1rem; border: 1px solid rgba(255,255,255,.08);
  box-shadow: 0 4px 14px rgba(0,0,0,.35); max-width: 46vw; z-index: 4;
}
.scorestrip .title {
  font-size: .74rem; letter-spacing: .09em; text-transform: uppercase;
  color: #9fb0ab; margin-bottom: .22rem;
}
.scorestrip .s { display: flex; justify-content: space-between; gap: 1rem; white-space: nowrap; }
.scorestrip .s b { color: var(--gold); font-variant-numeric: tabular-nums; }
@media (max-width: 560px) {
  .scorestrip { font-size: .85rem; padding: .45em .6em; }
}

/* ---------- debug state button ---------- */
.debug-btn {
  position: fixed; left: 10px; bottom: 10px; z-index: 40;
  font: inherit; font-size: .78rem; font-weight: 700; cursor: pointer; color: #cdd6d2;
  background: rgba(15,25,20,.85); border: 1px solid rgba(255,255,255,.14);
  border-radius: 9px; padding: .4em .7em; box-shadow: var(--shadow); opacity: .75;
}
.debug-btn:hover { opacity: 1; }
.debug-area {
  width: 100%; height: 46vh; min-height: 220px; resize: vertical;
  font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: .78rem;
  background: #0e1712; color: #d7e2dc; border: 1px solid rgba(255,255,255,.14);
  border-radius: 10px; padding: .7em; white-space: pre; overflow: auto;
}
@media (max-width: 560px) { .debug-btn { font-size: .72rem; padding: .35em .55em; } }

/* ---------- table chat ---------- */
.chat { position: fixed; right: 12px; bottom: 12px; width: 300px; z-index: 40;
  display: flex; flex-direction: column; align-items: flex-end; }
.chat-toggle {
  font: inherit; font-weight: 700; cursor: pointer; color: #fff;
  background: rgba(15,25,20,.92); border: 1px solid rgba(255,255,255,.14);
  border-radius: 10px; padding: .5em .9em; box-shadow: var(--shadow);
}
.chat-unread {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 18px; height: 18px; margin-left: .3em; padding: 0 4px;
  background: var(--danger); color: #fff; border-radius: 9px; font-size: .72rem;
}
.chat-body {
  width: 100%; height: 300px; display: flex; flex-direction: column; margin-top: 6px;
  background: rgba(15,25,20,.96); border: 1px solid rgba(255,255,255,.12);
  border-radius: 12px; box-shadow: var(--shadow); overflow: hidden;
}
.chat.collapsed .chat-body { display: none; }
.chat-log { flex: 1; overflow-y: auto; padding: .6em .7em; display: flex; flex-direction: column; gap: .35em; font-size: .88rem; }
.chat-msg { line-height: 1.3; word-break: break-word; }
.chat-msg .time { color: #6f7f7a; font-size: .72rem; margin-right: .4em; font-variant-numeric: tabular-nums; }
.chat-msg .from { font-weight: 700; color: var(--gold); margin-right: .35em; }
.chat-msg.me .from { color: #7fb2ff; }
/* System lines get their own colours, distinct from any player. */
.chat-msg.system .sys-text { font-weight: 700; font-style: italic; }
.chat-msg.firsttrick .sys-text { color: #58d6c0; }   /* teal — First Trick */
.chat-msg.cheat .sys-text { color: #ff7a63; }         /* red — game-state peek */
.chat-empty { color: #7f8f8a; font-style: italic; }
.chat-form { display: flex; gap: .4em; padding: .5em; border-top: 1px solid rgba(255,255,255,.08); }
.chat-form input { flex: 1; min-width: 0; }
@media (max-width: 560px) {
  .chat { width: min(78vw, 300px); right: 8px; bottom: 8px; }
  .chat-body { height: 240px; }
}

/* my hand */
.hand {
  display: flex; justify-content: center; align-items: flex-end; gap: 0;
  padding: 8px 8px 12px; min-height: calc(var(--card-h) + 20px);
  background: linear-gradient(0deg, rgba(0,0,0,.35), transparent);
}
.hand .pc {
  margin-left: -34px; transition: transform .18s, margin .18s, filter .18s;
  cursor: default; position: relative;
}
.hand .pc:first-child { margin-left: 0; }
.hand .pc.playable { cursor: pointer; }
.hand .pc.playable:hover { transform: translateY(-18px); z-index: 5; }
.hand .pc.illegal { filter: grayscale(.7) brightness(.62); }
.hand .pc.selected { transform: translateY(-24px); z-index: 6; }
@media (hover: none) {
  .hand .pc.playable { transform: translateY(-10px); }
}

/* modal */
.modal-backdrop {
  position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex;
  align-items: center; justify-content: center; z-index: 50; padding: 1rem;
}
.modal {
  background: #1c2a24; border-radius: 16px; padding: 1.4rem 1.6rem; width: min(94vw, 460px);
  box-shadow: var(--shadow); border: 1px solid rgba(231,198,107,.25); max-height: 88vh; overflow: auto;
}
.modal h3 { margin: 0 0 1rem; color: var(--gold); }
.modal .row { display: flex; justify-content: space-between; align-items: center; gap: 1rem; padding: .5rem 0; border-bottom: 1px solid rgba(255,255,255,.07); }
.modal .row:last-child { border-bottom: none; }
.modal-actions { display: flex; gap: .6rem; justify-content: flex-end; margin-top: 1.2rem; }
.field { display: flex; flex-direction: column; gap: .3rem; margin-bottom: .8rem; }
.field label { font-size: .85rem; color: #b9c4c0; }
select { font: inherit; padding: .55em; border-radius: 8px; background: rgba(255,255,255,.06); color: #fff; border: 1px solid rgba(255,255,255,.15); }

/* trump reference */
.trump-list { display: flex; flex-wrap: wrap; gap: 6px; }
.trump-list .pc { width: 48px; height: 67px; margin: 0; }
.trump-group { margin-bottom: .8rem; }
.trump-group h4 { margin: .3rem 0; font-size: .85rem; color: var(--gold); }

/* gather step: completed trick sits out until the winner collects it.
   Caption sits just above the pile of cards, the button just below it — both
   anchored to the trick's centre so they never reach the players' nameplates. */
.trick-caption {
  position: absolute; left: 50%; top: 47%;
  transform: translate(-50%, calc(-50% - 70px - (var(--card-h) / 2) - 26px));
  font-weight: 800; font-size: 1.05rem; color: var(--gold);
  background: rgba(0,0,0,.5); padding: .3em .9em; border-radius: 999px;
  text-shadow: 0 2px 6px rgba(0,0,0,.6); z-index: 7; white-space: nowrap;
  animation: fadein .25s ease;
}
.gather-panel {
  position: absolute; left: 50%; top: 47%;
  transform: translate(-50%, calc(-50% + 70px + (var(--card-h) / 2) + 26px));
  display: flex; flex-direction: column; align-items: center; gap: .3rem; z-index: 7;
  text-align: center; max-width: 90vw;
}
.gather-panel .muted { background: rgba(0,0,0,.5); padding: .3em .8em; border-radius: 10px; }

/* Celebratory "you won" gather button. */
.btn.gather {
  background: linear-gradient(180deg, #63dc8d, #33b06a); color: #05270f;
  box-shadow: 0 6px 20px rgba(51,176,106,.5); font-weight: 800;
  animation: gather-bounce 1.15s ease-in-out infinite;
}
.btn.gather:hover { filter: brightness(1.06); }
@keyframes gather-bounce { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-4px); } }
@media (max-width: 560px) { .trick-caption { font-size: .95rem; } }

/* hand summary */
.summary-teams { display: flex; gap: 1rem; margin: 1rem 0; }
.summary-team { flex: 1; background: rgba(255,255,255,.05); border-radius: 12px; padding: .8rem; text-align: center; }
.summary-team.win { border: 2px solid var(--gold); }
.summary-team h4 { margin: 0 0 .4rem; }
.summary-team .pts { font-size: 1.8rem; font-weight: 800; color: var(--gold); }
.flags { display: flex; gap: .5rem; justify-content: center; margin: .6rem 0; }
.flag { background: var(--gold); color: #222; font-weight: 800; padding: .1em .6em; border-radius: 7px; font-size: .8rem; }
.delta-row { display: flex; justify-content: space-between; padding: .3em 0; }
.delta.pos { color: #6fe09a; } .delta.neg { color: var(--danger); }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--danger); color: #fff; padding: .7em 1.2em; border-radius: 10px;
  box-shadow: var(--shadow); z-index: 100; animation: fadein .2s;
}
.announce-banner {
  position: fixed; top: 12%; left: 50%; transform: translateX(-50%);
  background: linear-gradient(180deg, #f3d27a, var(--gold)); color: #2a1e00;
  font-weight: 800; font-size: 1.15rem; padding: .8em 1.5em; border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0,0,0,.5); z-index: 120; text-align: center;
  animation: banner-in .35s cubic-bezier(.2,.9,.3,1.4); max-width: 92vw;
}
.announce-banner.cheat { background: linear-gradient(180deg, #e8654f, var(--danger)); color: #fff; }
@keyframes banner-in { from { opacity: 0; transform: translate(-50%, -14px) scale(.9); } }
@keyframes fadein { from { opacity: 0; transform: translate(-50%, 10px); } }

/* small screens: shrink cards */
/* Tablets / iPad: keep the roomy desktop layout but nudge cards down a touch. */
@media (max-width: 900px) {
  :root { --card-w: 96px; --card-h: 135px; }
}

/* Phones. */
@media (max-width: 560px) {
  :root { --card-w: 72px; --card-h: 101px; }
  .hand { padding: 6px 4px 10px; min-height: calc(var(--card-h) + 14px); }
  .hand .pc { margin-left: -36px; }
  .topbar { padding: .5rem .6rem; gap: .4rem; }
  .topbar-right { gap: .3rem; }
  .topbar-right .btn { padding: .45em .6em; font-size: .8rem; }
  .nameplate { padding: .26em .6em; gap: .35rem; }
  .nameplate .pname { font-size: .95rem; }
  .nameplate .puser { font-size: .68rem; }
  .avatar { width: 2rem; height: 2rem; }
  .avatar .face { font-size: 1.35rem; }
  .seat-top { top: 1%; } .seat-left { left: 1%; } .seat-right { right: 1%; }
  .seat-bottom { bottom: 1%; }
}

/* Small phones — squeeze the fan so all eight cards stay on screen. */
@media (max-width: 380px) {
  :root { --card-w: 60px; --card-h: 84px; }
  .hand .pc { margin-left: -34px; }
}

/* Landscape phones: very little vertical room — shrink cards, tighten the hand. */
@media (max-height: 480px) and (orientation: landscape) {
  :root { --card-w: 66px; --card-h: 92px; }
  .hand { min-height: calc(var(--card-h) + 8px); padding: 4px; }
  .seat-top { top: 0; } .seat-bottom { bottom: 0; }
}
