:root {
  --felt: #13513a;
  --felt-dark: #0b3324;
  --ink: #f4efe3;
  --muted: #b7c9bd;
  --gold: #f2c14e;
  --red: #c8102e;
  --black: #1b1b1b;
  --paper: #fffdf7;
  --panel: rgba(8, 30, 21, .82);
  --cw: clamp(40px, 6.4vw, 70px);
  --ch: calc(var(--cw) * 1.4);
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", "PingFang TC", "Microsoft JhengHei", sans-serif;
  color: var(--ink);
  background: radial-gradient(ellipse at 50% 40%, var(--felt) 0%, var(--felt-dark) 75%) fixed;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}
.hidden { display: none !important; }
button {
  font: inherit; font-weight: 600; cursor: pointer;
  color: var(--ink); background: rgba(255, 255, 255, .1);
  border: 1px solid rgba(255, 255, 255, .25); border-radius: 10px;
  padding: .6em 1.1em; transition: background .15s, transform .1s;
}
button:hover:not(:disabled) { background: rgba(255, 255, 255, .18); }
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: .4; cursor: default; }
button.primary { background: var(--gold); color: #2a1c00; border-color: var(--gold); }
button.primary:hover:not(:disabled) { background: #ffd36b; }
button.small { padding: .35em .75em; font-size: .85rem; }
button.big { font-size: 1.15rem; padding: .8em; }
button.link { background: none; border: none; color: var(--muted); text-decoration: underline; }
input {
  font: inherit; color: var(--black); background: var(--paper);
  border: 2px solid transparent; border-radius: 10px; padding: .6em .8em; width: 100%;
}
input:focus { outline: none; border-color: var(--gold); }

/* ---------- home / lobby ---------- */
.screen { height: 100dvh; }
#home, #lobby { display: grid; place-items: center; padding: 16px; overflow-y: auto; }
.panel {
  width: min(420px, 100%); background: var(--panel); border-radius: 18px; padding: 28px 24px;
  display: flex; flex-direction: column; gap: 12px; box-shadow: 0 20px 60px rgba(0, 0, 0, .4);
}
h1 { margin: 0; font-size: 2.6rem; text-align: center; color: var(--gold); letter-spacing: .05em; }
h1 small { display: block; font-size: 1rem; color: var(--ink); letter-spacing: .3em; text-transform: uppercase; }
h2 { margin: 0; }
.sub { margin: 0; color: var(--muted); text-align: center; font-size: .92rem; }
.field { display: flex; flex-direction: column; gap: 6px; color: var(--muted); font-size: .9rem; }
.or { text-align: center; color: var(--muted); font-size: .85rem; margin-top: 6px; }
.join { display: flex; gap: 8px; }
.join input { text-transform: uppercase; letter-spacing: .3em; text-align: center; font-weight: 700; }
.join button { white-space: nowrap; }
.row { display: flex; gap: 10px; justify-content: flex-end; }
.row button { flex: 1; }
.code { color: var(--gold); letter-spacing: .2em; }
#lobby-seats { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
#lobby-seats li {
  display: flex; align-items: center; gap: 8px; padding: 10px 12px;
  background: rgba(255, 255, 255, .06); border-radius: 10px; min-height: 48px;
}
#lobby-seats .name { flex: 1; }
.tag { font-size: .75rem; color: var(--gold); text-transform: uppercase; letter-spacing: .05em; }
.muted { color: var(--muted); font-style: italic; }

/* ---------- table ---------- */
#table { display: flex; flex-direction: column; }
.bar { display: flex; align-items: center; gap: 6px; padding: 8px 12px; background: rgba(0, 0, 0, .25); font-size: .9rem; }
.bar .spacer { flex: 1; }
.felt {
  flex: 1; min-height: 0; display: grid; gap: 8px; padding: 8px 12px;
  grid-template-columns: minmax(0, 1fr) minmax(0, 2.4fr) minmax(0, 1fr);
  grid-template-rows: auto 1fr;
  grid-template-areas: "top top top" "left center right";
}
.opp, .seat-tag {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 4px;
  padding: 8px 10px; border-radius: 14px; background: rgba(0, 0, 0, .2);
  border: 2px solid transparent; transition: border-color .2s, box-shadow .2s;
  align-self: center; justify-self: center; min-width: 110px; max-width: 100%;
}
.opp.turn, .seat-tag.turn { border-color: var(--gold); box-shadow: 0 0 18px rgba(242, 193, 78, .45); }
.opp.away, .seat-tag.away { opacity: .55; }
.who { display: flex; gap: 8px; align-items: baseline; max-width: 100%; }
.who .name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who .score { font-size: .8rem; color: var(--gold); font-variant-numeric: tabular-nums; }
.backs { display: flex; height: 34px; padding-right: 18px; }
.back {
  width: 24px; height: 34px; margin-right: -18px; border-radius: 4px;
  background: repeating-linear-gradient(45deg, #9b1c2c 0 4px, #b52a3b 4px 8px);
  border: 2px solid #fff; box-shadow: 0 1px 2px rgba(0, 0, 0, .4);
}
.count { font-size: .8rem; color: var(--muted); }
.count.low { color: #ff8a80; font-weight: 700; }
.bubble {
  position: absolute; top: -10px; right: -8px; padding: 2px 10px; border-radius: 999px;
  background: var(--paper); color: var(--black); font-size: .8rem; font-weight: 700;
  box-shadow: 0 2px 6px rgba(0, 0, 0, .35);
}
.bubble.thinking { background: var(--gold); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .5; } }

.center { grid-area: center; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; min-width: 0; }
#pile { display: flex; justify-content: center; min-height: var(--ch); }
#pile .card { margin: 0 -6px; animation: land .25s ease-out; }
#pile[data-from="1"] .card { --dx: 60px; --dy: 0; }
#pile[data-from="2"] .card { --dx: 0; --dy: -60px; }
#pile[data-from="3"] .card { --dx: -60px; --dy: 0; }
#pile[data-from="0"] .card { --dx: 0; --dy: 60px; }
@keyframes land { from { transform: translate(var(--dx, 0), var(--dy, 0)) scale(.9); opacity: 0; } }
#pile-label { font-size: .9rem; color: var(--muted); text-align: center; min-height: 1.2em; }

/* ---------- cards ---------- */
.card {
  position: relative; flex: none; width: var(--cw); height: var(--ch);
  background: var(--paper); border-radius: calc(var(--cw) * .1);
  box-shadow: 0 2px 5px rgba(0, 0, 0, .35); border: 1px solid #d9d2c0;
  user-select: none; transition: transform .12s ease-out, box-shadow .12s;
}
.card.red { color: var(--red); }
.card.black { color: var(--black); }
.card .corner {
  position: absolute; top: 3px; left: 4px; display: flex; flex-direction: column; align-items: center;
  line-height: 1; font-size: calc(var(--cw) * .27);
}
.card .corner b { font-weight: 800; letter-spacing: -.06em; }
.card .pip { position: absolute; right: 8%; bottom: 6%; font-size: calc(var(--cw) * .5); line-height: 1; }
.card.tiny { --cw: 26px; display: inline-block; margin-right: -10px; box-shadow: none; }
.card.tiny .pip { display: none; }

/* ---------- my area ---------- */
.me { padding: 8px 12px calc(10px + env(safe-area-inset-bottom)); background: rgba(0, 0, 0, .22); }
.me-head { display: flex; align-items: center; justify-content: center; gap: 12px; flex-wrap: wrap; }
.seat-tag { flex-direction: row; min-width: 0; padding: 4px 12px; }
#status { color: var(--muted); font-size: .95rem; }
#status.mine { color: var(--gold); font-weight: 700; }
#hand {
  display: flex; justify-content: center; padding: 22px 0 6px; overflow-x: auto; overflow-y: visible;
  padding-left: calc(var(--cw) * .5);
}
#hand .card { margin-left: calc(var(--cw) * -.48); cursor: pointer; }
#hand .card:hover { transform: translateY(-6px); }
#hand .card.sel { transform: translateY(-18px); box-shadow: 0 0 0 3px var(--gold), 0 6px 12px rgba(0, 0, 0, .4); }
#hand:not(.active) .card { filter: saturate(.85) brightness(.95); }
.controls { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; align-items: center; margin-top: 6px; }
#sel-info { flex-basis: 100%; text-align: center; font-size: .9rem; min-height: 1.2em; }
#sel-info.ok { color: #9be8a8; }
#sel-info.bad { color: #ff9e8f; }
#play { min-width: 96px; }

#log {
  position: fixed; right: 12px; top: 52px; bottom: 12px; width: min(340px, calc(100vw - 24px));
  background: rgba(6, 22, 15, .95); border-radius: 14px; padding: 12px 16px; overflow-y: auto; z-index: 5;
  box-shadow: 0 10px 40px rgba(0, 0, 0, .5); font-size: .88rem;
}
#log h3 { margin: 0 0 8px; }
#log ol { margin: 0; padding-left: 0; list-style: none; display: flex; flex-direction: column; gap: 4px; }
#log li.round { color: var(--gold); margin-top: 6px; }

/* ---------- modal / toast ---------- */
#modal { position: fixed; inset: 0; background: rgba(0, 0, 0, .55); display: grid; place-items: center; padding: 16px; z-index: 10; }
#modal .box {
  background: #10301f; border-radius: 18px; padding: 22px; width: min(560px, 100%); max-height: 90dvh; overflow-y: auto;
  display: flex; flex-direction: column; gap: 12px; box-shadow: 0 20px 60px rgba(0, 0, 0, .6);
}
#modal h3 { margin: 10px 0 0; color: var(--gold); font-size: 1rem; }
#modal p, #modal li { line-height: 1.5; color: #dfe8e1; margin: 0; }
#modal ul { margin: 0; padding-left: 20px; }
#modal .order { font-size: 1.1rem; font-weight: 700; }
#modal .red { color: #ff6b6b; } #modal .black { color: #fff; }
.scroll { overflow-x: auto; }
table.results { width: 100%; border-collapse: collapse; font-size: .92rem; }
.results th, .results td { padding: 8px 6px; text-align: left; border-bottom: 1px solid rgba(255, 255, 255, .1); white-space: nowrap; }
.results th { color: var(--muted); font-weight: 600; font-size: .8rem; }
.results tr.winner td:first-child::before { content: "👑 "; }
.results tr.me { background: rgba(242, 193, 78, .08); }
.results td.left { padding-right: 16px; }
.results .pos { color: #9be8a8; font-weight: 700; }
.results .neg { color: #ff9e8f; font-weight: 700; }

#toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 40px); opacity: 0; pointer-events: none;
  background: var(--paper); color: var(--black); padding: 10px 18px; border-radius: 999px; font-weight: 600;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .4); transition: .25s; z-index: 20; max-width: calc(100vw - 32px); text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (min-width: 900px) {
  #hand { padding-left: calc(var(--cw) * .3); }
  #hand .card { margin-left: calc(var(--cw) * -.3); }
}

/* ---------- phones ---------- */
@media (max-width: 640px) {
  .me-head { margin-bottom: 6px; }
  .felt { grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); grid-template-rows: auto auto 1fr; grid-template-areas: "top top" "left right" "center center"; }
  .opp { min-width: 0; width: 100%; padding: 6px; }
  .backs { height: 26px; }
  .back { width: 18px; height: 26px; margin-right: -13px; }
  #hand { padding-top: 18px; }
  #hand .card { margin-left: calc(var(--cw) * -.5); }
  .controls button { padding: .55em .8em; }
}
@media (max-height: 560px) and (orientation: landscape) {
  :root { --cw: clamp(36px, 5vw, 56px); }
  .felt { padding: 4px 12px; }
}
