:root {
  --felt: #0f4a37; --felt-dark: #0a3326; --ink: #f4f1e8; --muted: #b9cfc4;
  --gold: #e9c46a; --danger: #e76f51; --panel: rgba(0,0,0,.35);
  /* One card size everywhere; every image is normalised to 5:7 */
  --cw: clamp(34px, min(8.2vw, calc((100vh - 150px) / 4.8)), 96px);
  --cw: clamp(34px, min(8.2vw, calc((100dvh - 150px) / 4.8)), 96px); /* dvh = the part of the screen you can actually see on a phone */
  --ch: calc(var(--cw) * 1.4);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink); background: radial-gradient(ellipse at center, var(--felt) 0%, var(--felt-dark) 100%) fixed;
  overflow: hidden; -webkit-tap-highlight-color: transparent;
}
.screen { position: fixed; inset: 0; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
.center { display: flex; align-items: center; justify-content: center; overflow: auto; }
.panel { background: var(--panel); border: 1px solid rgba(255,255,255,.12); border-radius: 16px; padding: 24px; width: min(92vw, 420px); }
.logo { font-size: 2.6rem; margin: 0; text-align: center; color: var(--gold); letter-spacing: .04em; }
.tagline { text-align: center; color: var(--muted); margin: 4px 0 20px; }
h2 { margin: 0 0 8px; }
label { display: block; margin: 12px 0 6px; font-size: .9rem; color: var(--muted); }
input[type=text] { width: 100%; padding: 12px; border-radius: 10px; border: 1px solid rgba(255,255,255,.25); background: rgba(0,0,0,.35); color: var(--ink); font-size: 1rem; }
#session-input { text-transform: uppercase; letter-spacing: .2em; text-align: center; }
.row { display: flex; gap: 10px; margin-top: 14px; }
.row > * { flex: 1; }
.btn { padding: 11px 16px; border-radius: 10px; border: 1px solid rgba(255,255,255,.3); background: rgba(255,255,255,.08); color: var(--ink); font-size: 1rem; cursor: pointer; }
.btn:hover:not(:disabled) { background: rgba(255,255,255,.16); }
.btn:disabled { opacity: .4; cursor: default; }
.btn.primary { background: var(--gold); color: #1d2b24; border-color: var(--gold); font-weight: 600; }
.btn.primary:hover:not(:disabled) { background: #f1d283; }
.btn.small { padding: 6px 12px; font-size: .85rem; flex: 0; }
.link-btn { background: none; border: none; color: var(--gold); cursor: pointer; font-size: .85rem; padding: 0 0 0 8px; }
.error { color: var(--danger); min-height: 1.2em; margin: 12px 0 0; }
.muted { color: var(--muted); }
.session-id-row { display: flex; align-items: center; gap: 12px; justify-content: center; margin: 8px 0 16px; }
.session-id { font-size: 2rem; letter-spacing: .25em; font-weight: 700; color: var(--gold); font-family: ui-monospace, Menlo, monospace; }
.player-list { list-style: none; padding: 0; margin: 0 0 8px; }
.player-list li { padding: 8px 12px; margin-bottom: 6px; border-radius: 8px; background: rgba(255,255,255,.08); display: flex; justify-content: space-between; }
.tag { font-size: .75rem; color: var(--gold); }

/* Info button, top-right corner (unused on the table) */
.info-btn { position: fixed; top: calc(8px + env(safe-area-inset-top)); right: calc(8px + env(safe-area-inset-right)); width: 34px; height: 34px; border-radius: 50%;
  border: 2px solid var(--gold); background: rgba(0,0,0,.4); color: var(--gold); font: italic 700 18px Georgia, serif; cursor: pointer; z-index: 55; }
.chip { position: fixed; top: calc(8px + env(safe-area-inset-top)); left: calc(8px + env(safe-area-inset-left)); background: var(--panel); padding: 6px 12px; border-radius: 999px; font-size: .85rem; z-index: 20; }
.chip strong { letter-spacing: .15em; color: var(--gold); }

/* Table layout: opponents on top, pile in the middle, me at the bottom */
.table { position: absolute; inset: 0; display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto 1fr auto; padding: 40px 12px 6px; gap: 4px; }
.opponents { display: flex; justify-content: center; gap: clamp(8px, 3vw, 40px); align-items: flex-start; }
.seat { text-align: center; padding: 4px 8px; border-radius: 12px; border: 2px solid transparent; }
.seat.current { border-color: var(--gold); background: rgba(233,196,106,.08); }
.seat .nm { font-size: .85rem; margin-bottom: 2px; white-space: nowrap; max-width: 30vw; overflow: hidden; text-overflow: ellipsis; }
.seat.offline { opacity: .75; }
.seat .away { font-size: .72rem; color: var(--danger); margin-bottom: 2px; }
.rejoin { margin-top: 16px; } .rejoin .btn { width: 100%; }
.seat .nm .count { color: var(--muted); }
.mid { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px; min-height: 0; }
.center-zone { display: flex; align-items: center; justify-content: center; gap: clamp(12px, 3vw, 40px); }
.side { display: flex; align-items: center; min-width: 0; }
.mid > .side:first-child { justify-content: flex-start; } .mid > .side:last-child { justify-content: flex-end; }
.seat.sideseat .seatrow { flex-direction: column; align-items: center; gap: 4px; }
.seat.sideseat .fan { justify-content: center; }
.pile-box { text-align: center; }
.pile-box .lbl { font-size: .75rem; color: var(--muted); margin-top: 4px; }
.status { text-align: center; font-weight: 600; min-height: 1.4em; color: var(--gold); }
.me { text-align: center; min-width: 0; position: relative; }
.me .nm { font-size: .85rem; margin-bottom: 2px; }
.me.current .nm { color: var(--gold); font-weight: 700; }
.actions { display: flex; gap: 10px; justify-content: center; margin: 6px 0; min-height: 40px; }
.actions .btn { flex: 0; white-space: nowrap; }
@media (orientation: portrait) and (max-width: 600px) { .actions { gap: 6px; } .actions .btn { padding: 9px 11px; font-size: .92rem; } }

/* Cards: every card is exactly the same size */
.card { width: var(--cw); height: var(--ch); border-radius: calc(var(--cw) * .07); flex: none; display: block; user-select: none; -webkit-user-drag: none;
  box-shadow: 0 2px 5px rgba(0,0,0,.45); background: #fff; object-fit: fill; }
.card.empty { background: rgba(0,0,0,.15); box-shadow: inset 0 0 0 2px rgba(255,255,255,.15); }
.seatrow, .merow { display: flex; align-items: flex-start; justify-content: center; gap: calc(var(--cw) * .3); }
.merow { padding-top: 8px; }
.hand { display: flex; justify-content: flex-start; padding: 16px 4px 2px; --n: 3; min-width: 0; overflow-x: auto; overflow-y: hidden; scrollbar-width: none; -webkit-overflow-scrolling: touch; overscroll-behavior-x: contain; }
.hand::-webkit-scrollbar { display: none; }
.handwrap { position: relative; min-width: 0; display: flex; }
.hand-arrow { display: none; position: absolute; top: 55%; transform: translateY(-50%); width: 30px; height: 30px; padding: 0 0 3px; border-radius: 50%; border: 2px solid var(--gold); background: rgba(0,0,0,.65); color: var(--gold); font-size: 22px; line-height: 1; cursor: pointer; z-index: 3; }
.hand-arrow.l { left: 0; } .hand-arrow.r { right: 0; }
.handwrap.more-left .hand-arrow.l, .handwrap.more-right .hand-arrow.r { display: block; }
.handwrap.more-right .hand-arrow.r { animation: nudge 1.4s ease-in-out 3; }
@keyframes nudge { 50% { transform: translate(4px, -50%); } }
.hand.more-right { -webkit-mask-image: linear-gradient(to right, #000 calc(100% - 26px), transparent); mask-image: linear-gradient(to right, #000 calc(100% - 26px), transparent); }
.hand.more-left { -webkit-mask-image: linear-gradient(to left, #000 calc(100% - 26px), transparent); mask-image: linear-gradient(to left, #000 calc(100% - 26px), transparent); }
.hand.more-left.more-right { -webkit-mask-image: linear-gradient(to right, transparent, #000 26px, #000 calc(100% - 26px), transparent); mask-image: linear-gradient(to right, transparent, #000 26px, #000 calc(100% - 26px), transparent); }
.hand .card, .slot .card { cursor: default; }
/* cards overlap to fit; never tighter than 45% visible, past that the hand scrolls sideways */
.hand .card { margin-left: calc(max(min(var(--cw) * .75, (100vw - var(--cw) * 3.6 - 60px) / var(--n)), var(--cw) * .45) - var(--cw)); transition: transform .12s; }
.hand .card:first-child { margin-left: 0; }
.card.legal { cursor: pointer; box-shadow: 0 0 0 2px var(--gold), 0 2px 6px rgba(0,0,0,.5); }
.card.indicator { cursor: pointer; box-shadow: 0 0 0 2px var(--danger), 0 2px 6px rgba(0,0,0,.5); }
.card.selected { transform: translateY(-14px); }
.card.reveal { box-shadow: 0 0 0 3px var(--gold), 0 0 16px 5px rgba(233,196,106,.75); animation: reveal-pulse 1s ease-in-out infinite; z-index: 2; }
/* the winning move: bigger, faster and brighter than an ordinary reveal */
.card.reveal.win { box-shadow: 0 0 0 4px #fff3c4, 0 0 26px 10px rgba(255,214,102,.95); animation: win-flash .45s ease-in-out infinite alternate; }
@keyframes win-flash { to { box-shadow: 0 0 0 5px #ffffff, 0 0 44px 18px rgba(255,236,160,1), 0 0 90px 30px rgba(233,196,106,.7); filter: brightness(1.3) saturate(1.2); } }
@keyframes reveal-pulse { 50% { box-shadow: 0 0 0 3px var(--gold), 0 0 24px 9px rgba(233,196,106,.95); } }
.sortbtn { position: absolute; left: 2px; top: -22px; z-index: 4; padding: 3px 9px; border-radius: 12px; border: 1px solid rgba(233,196,106,.7); background: rgba(0,0,0,.45); color: var(--gold); font-size: .72rem; line-height: 1.15; cursor: pointer; text-align: center; }
.sortbtn:hover { background: rgba(0,0,0,.65); }
.merow { position: relative; }
.slots { display: flex; gap: calc(var(--cw) * .12); justify-content: center; }
.slot { position: relative; width: var(--cw); height: var(--ch); }
.slot .card { position: absolute; left: 0; }
.slot .down { top: 0; }
.slot .up { top: calc(var(--ch) * .22); }
.slot.flippable .down { cursor: pointer; box-shadow: 0 0 0 2px var(--gold); }
.slot.has-up { height: calc(var(--ch) * 1.22); }
.slot.only-down { height: var(--ch); }
/* Opponents' hands: backs fanned; overlap grows with the number of cards so the row never gets wide */
.fan { display: flex; justify-content: flex-start; margin-top: 0; --step: calc(var(--cw) * .14); }
.fan .card { margin-left: calc(var(--cw) * -0.86); }
.fan .card:first-child { margin-left: 0; }
.pile { position: relative; width: calc(var(--cw) * 1.9); height: var(--ch); }
.pile .card { position: absolute; top: 0; }
.chain-box { text-align: center; }

.log { position: fixed; right: 8px; bottom: calc(8px + env(safe-area-inset-bottom)); max-width: min(34vw, 300px); font-size: .72rem; color: var(--muted); text-align: right; pointer-events: none; opacity: .8; }
.log div { margin-top: 2px; }
/* desktop / tablet / resized windows: a fixed spot in the free top-right corner, so a large hand never covers it */
@media (min-width: 700px) and (min-height: 480px) {
  .log { right: calc(12px + env(safe-area-inset-right)); bottom: auto; top: calc(58px + env(safe-area-inset-top)); width: clamp(190px, 19vw, 250px); max-width: none; min-height: calc(4 * 1.5em + 14px); box-sizing: border-box;
    padding: 6px 10px; font-size: .8rem; line-height: 1.5; opacity: 1; background: rgba(0,0,0,.22); border-radius: 10px; }
  .log div { margin-top: 0; }
  .log div:last-child { color: var(--ink); }
}

/* Modals / overlays */
.modal, .overlay { position: fixed; inset: 0; background: rgba(0,0,0,.65); display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal-box { background: #14372b; border: 1px solid rgba(255,255,255,.2); border-radius: 16px; width: min(94vw, 680px); max-height: 88vh; display: flex; flex-direction: column; }
.modal-head { display: flex; align-items: center; gap: 12px; padding: 14px 18px; border-bottom: 1px solid rgba(255,255,255,.12); }
.modal-head h2 { flex: 1; margin: 0; font-size: 1.2rem; }
.modal-body { padding: 4px 20px 20px; overflow: auto; line-height: 1.5; }
.modal-body h3 { color: var(--gold); margin: 16px 0 4px; }
.modal-body ul { padding-left: 20px; margin: 4px 0; }
.modal-body section { display: none; }
.modal-body[data-lang=en] section[lang=en], .modal-body[data-lang=da] section[lang=da] { display: block; }
.lang-toggle button { background: none; border: 1px solid rgba(255,255,255,.3); color: var(--ink); padding: 4px 10px; cursor: pointer; }
.lang-toggle button:first-child { border-radius: 8px 0 0 8px; } .lang-toggle button:last-child { border-radius: 0 8px 8px 0; }
.lang-toggle button.active { background: var(--gold); color: #1d2b24; }
.close-btn { background: none; border: none; color: var(--ink); font-size: 1.8rem; cursor: pointer; line-height: 1; }
.gameover-box { text-align: center; padding: 32px 40px; border-radius: 20px; background: #14372b; border: 2px solid var(--gold); max-width: 92vw; }
.gameover-box h2 { font-size: clamp(1.6rem, 6vw, 2.6rem); color: var(--gold); }
.gameover-box.lost h2 { color: var(--danger); }
.rematch { margin: 14px 0 4px; max-width: 360px; }
.rematch p { margin: 6px 0; }
.rematch-timer, #rematch-timer { font-size: .9rem; }
.countdown { color: var(--gold); font-weight: 700; font-variant-numeric: tabular-nums; }
.gameover-box .row { justify-content: center; }
.gameover-box .row > * { flex: 0 1 auto; min-width: 120px; }
.conn-banner { position: fixed; top: 0; left: 0; right: 0; padding: 6px; text-align: center; background: var(--danger); color: #fff; z-index: 60; font-size: .85rem; }
.toast { position: fixed; bottom: 120px; left: 50%; transform: translateX(-50%); background: #000c; padding: 10px 16px; border-radius: 10px; z-index: 60; max-width: 90vw; }
/* Short screens (phones held sideways): status and buttons share one line, side players put their hand next to their cards */
@media (max-height: 460px) {
  .table { padding: 34px 10px 6px; gap: 2px; }
  .logo { font-size: 1.8rem; }
  .me { display: flex; flex-wrap: wrap; justify-content: center; align-items: center; column-gap: 14px; }
  .me .status { min-height: 0; font-size: .9rem; }
  .me .actions { margin: 2px 0; min-height: 0; }
  .me .actions .btn { padding: 5px 12px; font-size: .9rem; }
  .me .merow { flex: 0 0 100%; padding-top: 4px; }
  .hand { padding-top: 8px; }
  .card.selected { transform: translateY(-8px); }
  .seat { padding: 2px 6px; }
  .seat .nm { font-size: .78rem; margin-bottom: 0; }
  .seat .away { font-size: .66rem; margin-bottom: 0; }
  .seat.sideseat .seatrow { flex-direction: row; align-items: flex-start; gap: 6px; }
  .pile-box .lbl { font-size: .68rem; margin-top: 2px; }
}

/* 3-4 players: opponents also sit left and right, so cards are a little smaller */
.table.many { --cw: clamp(32px, min(7vw, calc((100vh - 150px) / 6.7)), 90px); --ch: calc(var(--cw) * 1.4); }
.table.many { --cw: clamp(32px, min(7vw, calc((100dvh - 150px) / 6.7)), 90px); --ch: calc(var(--cw) * 1.4); }

.mute-btn { right: calc(50px + env(safe-area-inset-right)); font: 16px system-ui, sans-serif; font-style: normal; }

.audio-pop { position: fixed; top: calc(50px + env(safe-area-inset-top)); right: calc(8px + env(safe-area-inset-right)); z-index: 56; width: 220px;
  background: #14372b; border: 1px solid rgba(255,255,255,.2); border-radius: 12px; padding: 12px 14px; }
.audio-row { display: flex; align-items: center; gap: 8px; margin: 8px 0 4px; color: var(--ink); font-size: .9rem; }
.audio-row:first-child { margin-top: 0; }
.audio-pop input[type=range] { width: 100%; accent-color: var(--gold); }

.version { position: fixed; left: calc(8px + env(safe-area-inset-left)); bottom: calc(4px + env(safe-area-inset-bottom)); font-size: .7rem; color: var(--muted); opacity: .6; pointer-events: none; }

/* Card size on short screens - last in the file so it also beats the 3-4 player rule above */
@media (max-height: 460px) {
  .table { --cw: clamp(28px, min(8vw, calc((100vh - 135px) / 5.2)), 80px); --ch: calc(var(--cw) * 1.4); }
  .table.many { --cw: clamp(24px, min(5.2vw, calc((100vh - 160px) / 5.1)), 70px); --ch: calc(var(--cw) * 1.4); }
}
@supports (height: 100dvh) {
  @media (max-height: 460px) {
    .table { --cw: clamp(28px, min(8vw, calc((100dvh - 135px) / 5.2)), 80px); --ch: calc(var(--cw) * 1.4); }
    .table.many { --cw: clamp(24px, min(5.2vw, calc((100dvh - 160px) / 5.1)), 70px); --ch: calc(var(--cw) * 1.4); }
  }
}

/* Only shown on phones held upright (rule at the end of this file) */
.log-inline { display: none; }

/* Phones held upright: bigger cards, side players above the pile instead of beside it, recent plays as text */
@media (orientation: portrait) and (max-width: 600px) {
  /* --cwb = base card size. Your own cards are the largest, the two piles next, opponents smallest. */
  .table { --cwb: clamp(22px, min(11vw, calc((100dvh - 320px) / 10.4)), 58px); --cw: var(--cwb); --ch: calc(var(--cw) * 1.4); padding-top: 58px; }
  .table.many { --cwb: clamp(20px, min(10.5vw, calc((100dvh - 345px) / 11.6)), 52px); --cw: var(--cwb); --ch: calc(var(--cw) * 1.4); }
  .table .me { --cw: calc(var(--cwb) * 1.4); --ch: calc(var(--cw) * 1.4); }
  .table .center-zone { --cw: calc(var(--cwb) * 1.55); --ch: calc(var(--cw) * 1.4); }
  .table:not(.many) .opponents { --cw: calc(var(--cwb) * 1.25); --ch: calc(var(--cw) * 1.4); }
  .opponents .seat.top .seatrow { flex-direction: row; }
  .table:not(.many) .opponents .seat.top .seatrow { flex-direction: column; align-items: center; gap: 6px; }
  .mid { grid-template-columns: 1fr 1fr; grid-template-areas: "l r" "c c"; row-gap: 14px; align-content: center; }
  .mid > .side:first-child { grid-area: l; } .mid > .side:last-child { grid-area: r; } .mid > .center-zone { grid-area: c; }
  .log { display: none; }
  /* my slots on one row, my hand on its own full-width row below (it scrolls sideways when it is long) */
  .merow { flex-direction: column; align-items: center; gap: 0; padding-top: 4px; }
  .handwrap { width: 100%; }
  .sortbtn { top: calc(var(--ch) * .5 + 4px); transform: translateY(-50%); max-width: 72px; }
  .hand { width: 100%; --n: 3; }
  .hand .card { margin-left: calc(max(min(var(--cw) * .75, (100vw - var(--cw) - 48px) / var(--n)), var(--cw) * .5) - var(--cw)); }
  .hand .card:first-child { margin-left: auto; }
  .hand .card:last-child { margin-right: auto; }
  /* recent plays as text between the opponents and the piles */
  .table { grid-template-rows: auto auto 1fr auto; }
  .log-inline { display: block; min-height: calc(4 * 1.3em + 6px); padding: 6px 8px 0; font-size: .8rem; line-height: 1.3; color: var(--muted); text-align: center; }
  .log-inline div:last-child { color: var(--ink); }
}

/* Short upright phones: the single opponent keeps the slots-beside-hand layout to save height */
@media (orientation: portrait) and (max-width: 600px) and (max-height: 700px) {
  .table:not(.many) { --cwb: clamp(22px, min(10vw, calc((100dvh - 300px) / 8.8)), 52px); --cw: var(--cwb); --ch: calc(var(--cw) * 1.4); }
  .table:not(.many) .opponents { --cw: calc(var(--cwb) * 1.1); --ch: calc(var(--cw) * 1.4); }
  .table:not(.many) .opponents .seat.top .seatrow { flex-direction: row; }
}
