:root {
  --felt: #0f5a3a; --felt2: #0a4a2f; --ink: #2a1a12; --paper: #fffdf6;
  --gold: #f2c218; --red: #b5241b; --muted: #cfe3d6;
  --cw: clamp(58px, 10.5vw, 104px);   /* card width */
  --mini: clamp(22px, 4vw, 34px);
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; color: #fff;
  background: radial-gradient(ellipse at 50% 40%, #167349 0%, var(--felt) 45%, var(--felt2) 100%) fixed;
  -webkit-tap-highlight-color: transparent;
}
.hidden { display: none !important; }
h1, h2 { font-family: Georgia, "Times New Roman", serif; margin: 0 0 .4em; }
h1 { font-size: clamp(28px, 6vw, 42px); }
h2 { font-size: 19px; }
input {
  font: inherit; padding: 10px 12px; border-radius: 8px; border: 1px solid #b9c9bf;
  background: #fff; color: var(--ink); min-width: 0; flex: 1;
}
.btn {
  font: inherit; font-weight: 600; border: 0; border-radius: 8px; padding: 10px 16px; cursor: pointer;
  background: #e9efe9; color: var(--ink); transition: transform .1s, filter .15s;
}
.btn:hover:not(:disabled) { filter: brightness(1.06); }
.btn:active:not(:disabled) { transform: translateY(1px); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--gold); color: #3a2600; width: 100%; padding: 12px; font-size: 17px; }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn.link { background: none; color: inherit; text-decoration: underline; opacity: .8; }
.row { display: flex; gap: 8px; align-items: center; }
.row.wrap { flex-wrap: wrap; }

.screen { min-height: 100%; }
.panel {
  background: var(--paper); color: var(--ink); border-radius: 16px; padding: 24px;
  box-shadow: 0 10px 40px #0007; width: min(480px, calc(100% - 32px)); margin: 32px auto;
}
.home h1 { color: var(--red); text-align: center; margin-bottom: 0; }
.sub { margin: 0 0 16px; color: #6b5d50; text-align: center; }
.field { display: flex; flex-direction: column; gap: 6px; font-weight: 600; margin-bottom: 16px; }
.box { border: 1px solid #e4dccb; border-radius: 12px; padding: 14px; margin-bottom: 12px; }
.seg { display: flex; gap: 6px; margin-bottom: 12px; }
.seg button { flex: 1; font: inherit; padding: 9px 4px; border-radius: 8px; border: 1px solid #d5cbb8; background: #fff; cursor: pointer; color: var(--ink); }
.seg button.on { background: var(--ink); color: #fff; border-color: var(--ink); }
#code { text-transform: uppercase; letter-spacing: .3em; font-weight: 700; }
.err { color: var(--red); min-height: 1.2em; margin: 4px 0; font-weight: 600; }
.rules summary { cursor: pointer; font-weight: 600; }
.rules ul { padding-left: 18px; line-height: 1.45; font-size: 14px; }
.foot { text-align: center; font-size: 14px; margin: 8px 0 0; }
.foot a { color: var(--red); }
.hint { color: #6b5d50; font-size: 14px; }

/* lobby */
.code { background: var(--ink); color: var(--gold); padding: 0 10px; border-radius: 8px; letter-spacing: .12em; }
.seats { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 18px 0 6px; }
.seat { border: 2px solid #e4dccb; border-radius: 12px; padding: 10px; display: flex; flex-direction: column; gap: 2px; min-height: 76px; position: relative; }
.seat span { font-weight: 700; }
.seat small { color: #7d7064; }
.seat.mine { border-color: var(--gold); background: #fff8dc; }
.seat.empty { border-style: dashed; cursor: pointer; color: #8b7e70; }
.seat.empty:hover { background: #f6f1e6; }
.seat .btn.link { position: absolute; right: 4px; bottom: 4px; font-size: 12px; padding: 2px 4px; }
.team { font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .06em; }
.team.t0 { color: #1f5fbf; } .team.t1 { color: var(--red); }
#hostCtl { margin: 12px 0; justify-content: space-between; }
.check { display: flex; align-items: center; gap: 6px; font-size: 14px; }
.check input { flex: none; }
#leave { display: block; margin: 10px auto 0; }

/* chat */
.chat {
  position: fixed; right: 12px; bottom: 12px; width: min(320px, calc(100% - 24px)); z-index: 40;
  background: var(--paper); color: var(--ink); border-radius: 12px; box-shadow: 0 8px 30px #0008;
  display: flex; flex-direction: column; padding: 10px; gap: 8px;
}
.chat.docked { position: static; width: auto; box-shadow: none; border: 1px solid #e4dccb; margin-top: 16px; padding: 10px; }
.chat.docked .chathead button { display: none; }
.chathead { display: flex; justify-content: space-between; align-items: center; font-weight: 700; }
.chatlog { height: 160px; overflow-y: auto; font-size: 14px; line-height: 1.4; }
.chatlog .sys { color: #8b7e70; font-style: italic; font-size: 13px; }
.badge { background: var(--red); color: #fff; border-radius: 9px; padding: 0 6px; font-size: 11px; }

/* game */
#game { display: flex; flex-direction: column; height: 100vh; height: 100dvh; overflow: hidden; }
.bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 10px; background: #0006; flex-wrap: wrap; position: relative; z-index: 5; }
.tag { background: #ffffff1a; border-radius: 999px; padding: 4px 10px; font-size: 13px; white-space: nowrap; }
.tag small { opacity: .7; }
.trumptag { background: #f2c21833; }
.scorebar { display: flex; gap: 6px; flex-wrap: wrap; justify-content: center; }
.baractions { display: flex; gap: 6px; }

/* the table: sizes follow the screen (width and height) */
.table {
  position: relative; flex: 1; min-height: 0; overflow: hidden; overflow: clip; /* never scrolls */
  --hcw: min(30vw, 19dvh, 165px);   /* your hand */
  --tcw: min(27vw, 16dvh, 140px);   /* cards in the middle */
  --ocw: min(17vw, 11dvh, 95px);    /* opponents' cards */
  --scw: min(19vw, 12dvh, 105px);   /* deck + vida */
  --tw: calc(var(--tcw) * 2.3);     /* width of the middle area */
  --T: calc(var(--ocw) * 1.55 * .58 + 34px);   /* space kept for the player across */
  --B: calc(var(--hcw) * 1.55 * .96 + 48px);   /* space kept for your hand + status line */
}
.table.n2 { --tw: calc(var(--tcw) * 1.5); }
.table.n3 { --tcw: min(25vw, 15dvh, 135px); }
.table.n4 { --tcw: min(23vw, 15dvh, 130px); }

.card { width: var(--cw); aspect-ratio: 200 / 310; flex: none; border-radius: 6%; filter: drop-shadow(0 2px 3px #0006); user-select: none; -webkit-user-select: none; }
.card svg, .card img { width: 100%; height: 100%; display: block; border-radius: 6%; }

.plate { background: #000a; border-radius: 999px; padding: 4px 12px; font-size: 14px; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; transition: box-shadow .3s, background .3s, color .3s; }
.plate em { font-style: normal; opacity: .7; font-size: 12px; }
.plate.turn { background: var(--gold); color: #3a2600; box-shadow: 0 0 0 3px #f2c21855, 0 0 18px #f2c218aa; }
.plate.partner { outline: 2px solid #8fd3ff; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #999; }
.dot.on { background: #4ade80; }
.dealer { background: #fff; color: var(--ink); border-radius: 50%; width: 18px; height: 18px; display: inline-grid; place-items: center; font-size: 11px; font-weight: 800; }

/* opponents: their cards peek in from the edges */
.opp { position: absolute; z-index: 2; }
.opp .backs { display: flex; }
.opp .backs .card { width: var(--ocw); margin-left: calc(var(--ocw) * -.45); }
.opp .backs .card:first-child { margin-left: 0; }
.opp.top { top: 0; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column-reverse; justify-content: flex-end; align-items: center; gap: 6px; }
.opp.top .backs { transform: rotate(180deg); margin-top: calc(var(--ocw) * -1.55 * .42); }
.opp.left, .opp.right { top: 40%; width: 0; height: 0; }
.opp.left { left: 0; }
.opp.right { right: 0; }
.opp.left .backs, .opp.right .backs { position: absolute; top: 0; }
.opp.left .backs { left: 0; transform: translate(-50%, -50%) rotate(90deg); }
.opp.right .backs { right: 0; transform: translate(50%, -50%) rotate(-90deg); }
.opp.left .plate, .opp.right .plate { position: absolute; top: calc(var(--ocw) * 1.2); }
.opp.left .plate { left: 6px; }
.opp.right .plate { right: 6px; }

/* middle: deck + vida, and the cards being played */
.center {
  position: absolute; left: 0; right: 0; z-index: 1;
  top: var(--T);
  bottom: var(--B);
  display: flex; align-items: center; justify-content: center; gap: 4vw;
}
.stock {
  position: absolute; z-index: 1; width: calc(var(--scw) * 1.65); height: calc(var(--scw) * 1.55);
  top: calc((var(--T) + 100% - var(--B)) / 2); left: calc(50% - var(--tw) / 2 - var(--scw) * 1.65 - 4vw); transform: translateY(-50%);
}
.stock .card { width: var(--scw); }
.stock .trumpcard { position: absolute; left: calc(var(--scw) * .62); top: calc(var(--scw) * .27); transform: rotate(90deg); }
.stock .deck { position: absolute; left: 0; top: 0; }
.stock .deck .card { box-shadow: 2px 2px 0 #ddd, 4px 4px 0 #bbb; border-radius: 6%; }
.stock .count { position: absolute; left: 50%; bottom: -10px; transform: translateX(-50%); background: #000a; padding: 1px 8px; border-radius: 999px; font-size: 12px; font-weight: 700; }
.notrump { text-align: center; font-size: 13px; background: #0004; border-radius: 12px; padding: 10px; display: flex; flex-direction: column; align-items: center; gap: 4px; }

.trick { position: relative; width: var(--tw); height: calc(var(--tcw) * 2.45); flex: none; }
.trick .card { width: var(--tcw); }
.slot { position: absolute; left: 50%; top: 50%; }
.slot.bottom { transform: translate(-50%, -22%); }
.slot.top { transform: translate(-50%, -78%); }
.slot.left { transform: translate(-108%, -50%) rotate(-8deg); }
.slot.right { transform: translate(8%, -50%) rotate(8deg); }
.slot.win .card { filter: drop-shadow(0 0 10px var(--gold)) drop-shadow(0 0 4px var(--gold)); }

/* you: big cards, the bottom tucked under the edge of the screen */
.me { position: absolute; left: 0; right: 0; bottom: 0; z-index: 3; display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none; }
.me > * { pointer-events: auto; }
.merow { position: relative; z-index: 2; display: flex; align-items: center; justify-content: center; gap: 10px; flex-wrap: wrap; padding: 0 8px;
  margin-bottom: calc(var(--hcw) * 1.55 * .17); /* room for your cards to rise without covering the text */ }
.status { text-align: center; font-size: 15px; min-height: 1.4em; text-shadow: 0 1px 3px #000a; }
.status .yours { color: var(--gold); font-weight: 700; }
.hand { display: flex; gap: 2vw; justify-content: center; margin-bottom: calc(var(--hcw) * 1.55 * -.2); }
.hand .card { width: var(--hcw); transition: transform .25s ease-out; }
.hand.active .card.playable { cursor: pointer; transform: translateY(-7%); }
@media (hover: hover) { .hand.active .card.playable:hover { transform: translateY(-15%); } }
.hand .card.going, .hand.active .card.playable.going { transform: translateY(-18%); }

.mano { background: #111; color: var(--gold); border: 2px solid var(--gold); padding: 6px 14px; font-size: 14px; white-space: nowrap; animation: glow 1.6s infinite; }
.manoshow { position: fixed; z-index: 24; display: flex; gap: 10px; pointer-events: none; }
.manoshow .card { filter: drop-shadow(0 0 12px var(--gold)) drop-shadow(0 4px 8px #0008); }
.mncards { display: flex; gap: 8px; justify-content: center; margin: 6px 0 2px; }
.mncards .card { width: 64px; }
.swap { background: var(--gold); color: #3a2600; animation: glow 1.6s infinite; padding: 7px 14px; font-size: 14px; white-space: nowrap; }
@keyframes glow { 50% { box-shadow: 0 0 16px var(--gold); } }

/* tall screens (phones): deck goes to the top-left corner so the middle cards can be bigger */
@media (max-aspect-ratio: 4/5) {
  .table { --scw: min(15vw, 10dvh, 90px); }
  .stock { left: 8px; top: 6px; transform: none; }
  /* the player across moves right a little so their name never touches the deck */
  .opp.top { left: calc(50% + var(--scw) * .6); }
}
/* short screens (phone held sideways): smaller cards so everything fits */
@media (max-height: 520px) {
  .table { --hcw: min(30vw, 17dvh, 165px); --tcw: min(23vw, 11dvh, 140px); --ocw: min(17vw, 10dvh, 95px); }
  .table.n4, .table.n3 { --tcw: min(23vw, 10.5dvh, 130px); }
  .bar { padding: 3px 8px; }
}

/* home screen: the four aces in a fan */
.acefan { --fan: clamp(64px, 21vw, 100px); position: relative; height: calc(var(--fan) * 1.84 + 22px); margin: 0 auto 4px; }
.acefan .card { position: absolute; left: 50%; top: calc(var(--fan) * .15 + 2px); width: var(--fan); transform-origin: 50% 50%;
  filter: drop-shadow(0 4px 6px #0004); animation: fanin .7s cubic-bezier(.25,.75,.3,1) both; }
@keyframes fanin { from { transform: translateX(-50%) rotate(0deg); opacity: 0; } }

.overlay { position: fixed; inset: 0; background: #0008; display: grid; place-items: center; z-index: 30; padding: 16px; }
.overlay .panel { margin: 0; text-align: center; }
.overlay .btn { margin-top: 8px; }
.results { width: 100%; border-collapse: collapse; margin: 12px 0; font-size: 17px; }
.results td { padding: 8px; border-bottom: 1px solid #e4dccb; text-align: left; }
.results td:last-child { text-align: right; font-weight: 700; }
.results tr.won td { color: #1b7a3a; }
.lastbody { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin: 12px 0; }
.lt { display: flex; flex-direction: column; align-items: center; gap: 4px; font-size: 13px; }
.lt.win .card { filter: drop-shadow(0 0 8px var(--gold)); }

.conn { position: fixed; top: 0; left: 0; right: 0; z-index: 50; background: var(--red); text-align: center; padding: 4px; font-size: 14px; }
.toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #000c; padding: 8px 16px; border-radius: 999px; z-index: 60; font-size: 14px; }

@media (max-width: 560px) {
  .bar { padding: 4px 6px; gap: 4px; justify-content: center; }
  .bar > div:first-child { display: none; }
  .bar .tag { font-size: 12px; padding: 3px 8px; }
  .bar .btn.small { padding: 5px 9px; font-size: 12px; }
  .plate { font-size: 12px; padding: 3px 9px; }
  .status { font-size: 14px; }
}

/* ---------- animations ---------- */
.ghost { visibility: hidden !important; }
.flyer { position: fixed; left: 0; top: 0; z-index: 25; pointer-events: none; perspective: 800px; filter: drop-shadow(0 6px 10px #0007); will-change: transform; }
.flyer > svg, .flyer > img { width: 100%; height: 100%; display: block; border-radius: 6%; }
.flip { position: relative; width: 100%; height: 100%; transform-style: preserve-3d; }
.flip .face { position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden; }
.flip .face svg, .flip .face img { width: 100%; height: 100%; display: block; border-radius: 6%; }
.flip .backf { transform: rotateY(180deg); }
.notice {
  position: fixed; left: 50%; top: 42%; transform: translate(-50%, -50%); z-index: 26; pointer-events: none;
  background: #000d; color: #fff; padding: 12px 20px; border-radius: 14px; font-size: clamp(15px, 3.6vw, 20px);
  text-align: center; max-width: calc(100% - 32px); box-shadow: 0 8px 30px #0008; border: 1px solid #f2c21866; opacity: 0;
}
.notice b { color: var(--gold); }
.suiticon { vertical-align: middle; }
.hand .card.going { transform: translateY(-14px); }
.status .yours { display: inline-block; }
.plate.turn { animation: turnglow 1.4s ease-in-out infinite; }
@keyframes turnglow { 50% { box-shadow: 0 0 0 4px #f2c21855, 0 0 22px #f2c218aa; } }
.plate.turn { animation-duration: 2.2s; }
.btn.exit { background: #f6d5d2; color: #7d1510; }
.btn.danger { background: var(--red); color: #fff; }
.confirmrow { justify-content: center; margin-top: 12px; }
.confirmrow .btn { margin: 0; }

/* 4 players: spread the middle cards a bit more so each one is readable */
.table.n4 .trick { height: calc(var(--tcw) * 2.6); }
.table.n4 .slot.bottom { transform: translate(-50%, -14%); }
.table.n4 .slot.top { transform: translate(-50%, -86%); }
.table.n4 .slot.left { transform: translate(-120%, -50%) rotate(-8deg); }
.table.n4 .slot.right { transform: translate(20%, -50%) rotate(8deg); }

/* 3 players: nobody sits across, so the side names go above their cards, away from the middle */
.table.n3 .opp.left .plate, .table.n3 .opp.right .plate { top: auto; bottom: calc(var(--ocw) * 1.2); }
