/* Mahjong table. Seat plates and speech bubbles come from assets/css/seats.css.

   The table is the window under the header (base.css): tiles size themselves from its box
   (container units) times --fit, which TC.fitTable steps down if anything would overflow. Your own
   tiles are sized by mahjong.js (--tw on the hand): one row when they're big enough to read, two
   on a narrow phone. Nothing scrolls. */
.table.mj {
  --fit: 1;
  --mt: calc(clamp(15px, min(2.7cqw, 4cqh), 34px) * var(--fit)); /* melds, rivers, flowers */
  container: mj-table / size;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  gap: 4px;
  max-width: max(1100px, 150dvh);
  margin: 0 auto;
  padding: clamp(4px, 1.4dvh, 12px) 12px calc(clamp(6px, 1.6dvh, 14px) + env(safe-area-inset-bottom));
}
.table.mj.locked { justify-content: center; }
.mj .fan, .mj .books { display: none; }

/* ---------- Tiles ---------- */
.tile {
  --w: 40px;
  position: relative;
  display: inline-block;
  flex: none;
  width: var(--w);
  height: calc(var(--w) * 1.34);
  border-radius: calc(var(--w) * 0.13);
  background: linear-gradient(170deg, #fffdf6, #f4ecd6);
  box-shadow:
    inset 0 0 0 1px rgba(120, 100, 60, 0.25),
    0 calc(var(--w) * 0.07) 0 #d7c79b,
    0 calc(var(--w) * 0.1) 0 #2f7d5a,
    0 calc(var(--w) * 0.1 + 2px) 5px rgba(0, 0, 0, 0.4);
  margin-bottom: calc(var(--w) * 0.1);
}
.tile svg { display: block; width: 100%; height: 100%; padding: 6% 6% 4%; }
.tile.back {
  background: linear-gradient(160deg, #3aa477, #1d6b4a 70%);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.18), 0 calc(var(--w) * 0.07) 0 #e9dfc2, 0 calc(var(--w) * 0.07 + 2px) 4px rgba(0, 0, 0, 0.4);
}
.tile.small { --w: var(--mt); }
.tile.new, .meld.new { animation: tile-in 0.35s var(--ease); }
@keyframes tile-in { from { opacity: 0; scale: 0.6; } }

/* ---------- The board: opponents round the discards, you below ---------- */
.board {
  flex: 1 1 auto;
  width: 100%;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  grid-template-areas: '. top .' 'left center right' '. me .';
  grid-template-rows: auto 1fr auto;
  align-items: center;
  justify-items: center;
  gap: clamp(4px, 1.2cqh, 12px) 14px;
  /* The table, seen from above, in the room (base.css: body.room): a raised wooden edge round a felt
     top, a player at each side, the discards in the middle. */
  --edge: clamp(9px, 1.5cqmin, 16px);
  position: relative;
  padding: clamp(6px, 1.2cqh, 14px) clamp(8px, 1.4cqw, 20px);
  border: var(--edge) solid transparent;
  border-radius: clamp(16px, 3cqmin, 30px);
  background:
    radial-gradient(ellipse at 50% 45%, var(--table-felt-1), var(--table-felt-2) 65%, var(--table-felt-3)) padding-box,
    var(--wood) border-box;
  box-shadow:
    inset 0 0 0 1.5px rgba(0, 0, 0, 0.55), inset 0 4px 12px rgba(0, 0, 0, 0.45),
    0 0 0 1px var(--wood-3), 0 14px 36px rgba(0, 0, 0, 0.65);
}
/* The edge's polish: a light along its top and a dark lip on the felt side. */
.board::before {
  content: '';
  position: absolute;
  inset: calc(var(--edge) * -1);
  border-radius: inherit;
  pointer-events: none;
  box-shadow: inset 0 1px 0 rgba(255, 226, 180, 0.35), inset 0 -1px 0 rgba(0, 0, 0, 0.4);
}
.opp[data-pos='left'] { grid-area: left; }
.opp[data-pos='top'] { grid-area: top; }
.opp[data-pos='right'] { grid-area: right; }
.center { grid-area: center; }
.me { grid-area: me; }
.opp, .me { min-width: 0; max-width: 100%; }

.mj .seat { gap: clamp(3px, 0.8cqh, 6px); }
.backs { display: flex; justify-content: center; }
.backs .tile { --w: calc(var(--mt) * 0.72); }
.backs .tile + .tile { margin-left: calc(var(--w) * -0.3); }
.sets { display: flex; flex-wrap: wrap; justify-content: center; align-items: flex-end; gap: 4px 8px; max-width: 100%; }
.melds, .flowers { display: flex; flex-wrap: wrap; justify-content: center; gap: 4px 6px; }
.melds:empty, .flowers:empty { display: none; }
.meld { display: inline-flex; gap: 1px; }
.flowers .tile { --w: calc(var(--mt) * 0.8); }
.flowers { gap: 1px; }
.seat.dealer .avatar { box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.35), 0 0 0 2px var(--gold); }
.seat-count { min-width: 34px; }

/* Your own seat: plate, then your sets and flowers beside it. */
.me .seat { flex-direction: row; flex-wrap: wrap; justify-content: center; gap: 6px 12px; }
.me .backs { display: none; }

/* Between a phone and a wide screen the side seats sit in narrow columns beside the discards, and
   a plate in a row (avatar, name, BOT, points) left the name 0 to 30 px ("Gian…"). There their
   plates stack, as on a phone, so the name gets the plate's whole width; a long one puts BOT on
   its own line. */
@media (min-width: 641px) and (max-width: 1100px) {
  .opp:is([data-pos='left'], [data-pos='right']) .seat-plate { flex-direction: column; gap: 2px; min-height: 0; max-width: 100%; padding: 5px 10px 6px; border-radius: 14px; text-align: center; }
  .opp:is([data-pos='left'], [data-pos='right']) .avatar { width: 26px; height: 26px; font-size: 0.8rem; }
  .opp:is([data-pos='left'], [data-pos='right']) .seat-who { align-items: center; max-width: 100%; }
  .opp:is([data-pos='left'], [data-pos='right']) .seat-line { flex-wrap: wrap; justify-content: center; row-gap: 2px; max-width: 100%; }
  .opp:is([data-pos='left'], [data-pos='right']) .seat-count { height: 22px; font-size: 0.75rem; }
}

/* ---------- Discards ---------- */
.center {
  display: grid;
  grid-template-areas: '. rt .' 'rl info rr' '. rb .';
  grid-template-columns: auto auto auto;
  align-items: center;
  justify-items: center;
  gap: 6px;
  padding: clamp(6px, 1.2cqh, 12px);
  border-radius: 18px;
  background: rgba(0, 0, 0, 0.12);
  box-shadow: inset 0 0 0 1px rgba(255, 236, 200, 0.08);
}
.river {
  --cols: 8;
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 2px;
  width: calc(var(--cols) * (var(--mt) + 2px));
  min-height: calc(var(--mt) * 1.44 * 2 + 2px);
}
.river .tile { margin-bottom: calc(var(--mt) * 0.1); }
.river[data-pos='top'] { grid-area: rt; flex-wrap: wrap-reverse; align-content: flex-start; }
.river[data-pos='bottom'] { grid-area: rb; }
.river[data-pos='left'] { grid-area: rl; --cols: 4; min-height: calc(var(--mt) * 1.44 * 3); }
.river[data-pos='right'] { grid-area: rr; --cols: 4; min-height: calc(var(--mt) * 1.44 * 3); }
.river .tile.last { outline: 2px solid var(--gold-2); outline-offset: 1px; box-shadow: 0 0 14px rgba(244, 220, 149, 0.8), 0 calc(var(--w) * 0.07) 0 #d7c79b; }
.info {
  grid-area: info;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 8px 12px;
  border-radius: 12px;
  background: rgba(2, 16, 9, 0.6);
  border: 1px solid var(--panel-border);
  text-align: center;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.info-wall { color: var(--gold-2); }

/* ---------- Your hand ---------- */
.prompt { min-height: 1.4em; margin: 0; text-align: center; font-weight: 600; color: var(--ink-2); }
.prompt strong { color: var(--gold-2); }
.hand {
  --tw: 48px;
  --row-w: 100%;
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: 2px;
  width: 100%;
  min-height: calc(var(--tw) * 1.45 + 12px);
  padding-top: 12px; /* (room for the picked tile to rise) */
}
.hand.two-rows { flex-wrap: wrap; row-gap: 6px; padding-inline: calc((100% - var(--row-w)) / 2); }
.ht {
  appearance: none;
  flex: none;
  padding: 0;
  margin: 0;
  border: 0;
  background: none;
  color: inherit;
  border-radius: calc(var(--tw) * 0.13);
  cursor: default;
  transition: translate 0.18s var(--ease);
  -webkit-tap-highlight-color: transparent;
}
.ht .tile { --w: var(--tw); }
.ht.drawn { margin-left: calc(var(--tw) * 0.4); }
.hand.live .ht { cursor: pointer; }
.hand.live .ht:hover { translate: 0 -5px; }
.ht.picked { translate: 0 -12px; }
.hand.live .ht.picked:hover { translate: 0 -13px; }
.ht.picked .tile { box-shadow: 0 0 0 3px var(--gold-2), 0 8px 20px rgba(244, 220, 149, 0.35), 0 calc(var(--w) * 0.1) 0 #2f7d5a; }
.ht.hint .tile::after {
  content: '';
  position: absolute;
  inset: -3px;
  border-radius: inherit;
  border: 2px dashed rgba(244, 220, 149, 0.9);
  pointer-events: none;
}
.ht:focus-visible { outline: 2px solid var(--gold-2); outline-offset: 3px; }

/* ---------- Controls ---------- */
.controls { position: relative; display: flex; flex-wrap: wrap; justify-content: center; align-items: center; gap: 8px; width: 100%; min-height: 52px; }
.act-group { display: contents; }
.act .mini-set { display: inline-flex; gap: 1px; margin-right: 2px; }
.act .mini-set .tile { --w: 15px; margin-bottom: 1px; }
.act .mini-set .tile svg { padding: 3%; }
#discard-btn { min-width: min(200px, 100%); }

/* ---------- Panels ---------- */
.panel-card {
  position: absolute;
  left: 50%;
  top: 46%;
  z-index: 20;
  translate: -50% -50%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  width: min(440px, calc(100% - 24px));
  max-height: calc(100% - 16px);
  overflow-y: auto;
  padding: 24px 20px;
  text-align: center;
  border-radius: 20px;
  background: rgba(3, 22, 13, 0.96);
  border: 1px solid rgba(217, 180, 90, 0.45);
  box-shadow: var(--shadow);
  animation: card-in 0.3s var(--ease);
}
@keyframes card-in { from { opacity: 0; scale: 0.96; } }
.panel-card h2 { margin: 0; font-family: var(--font-display); font-size: 1.6rem; color: var(--gold-2); }
.table.mj.paused > :is(.board, .prompt, .hand, .controls) { opacity: 0.25; pointer-events: none; }
.setup-note { margin: 0; color: var(--ink-2); font-size: 0.95rem; line-height: 1.45; }
.check { display: flex; align-items: center; gap: 10px; min-height: 44px; font-weight: 600; color: var(--ink-2); cursor: pointer; }
.check input { width: 20px; height: 20px; accent-color: var(--gold); }

.scored { width: min(500px, calc(100% - 24px)); gap: 10px; }
.scored-how { margin: -4px 0 0; color: var(--ink-2); font-weight: 600; }
.scored-hand { --mt: 22px; display: flex; flex-wrap: wrap; justify-content: center; gap: 4px 10px; }
.faan-lines, .faan-table { width: 100%; margin: 0; padding: 0; list-style: none; }
.faan-lines li, .faan-table li { display: flex; justify-content: space-between; gap: 12px; padding: 4px 2px; border-top: 1px solid var(--hairline); text-align: left; }
.faan-lines b, .faan-table b { color: var(--gold-2); white-space: nowrap; }
.faan-table { margin: 6px 0; font-size: 0.92rem; }
.rules-note { margin: 0 0 6px; }
.scored-sum { margin: 0; font-weight: 700; color: var(--ink); }

.standings { width: 100%; margin: 0; padding: 0; list-style: none; display: flex; flex-direction: column; gap: 6px; }
.standings li { display: flex; align-items: center; gap: 12px; padding: 6px 14px 6px 6px; border-radius: 14px; background: rgba(255, 255, 255, 0.05); text-align: left; }
.standings li.winner { background: rgba(244, 220, 149, 0.12); box-shadow: inset 0 0 0 1px rgba(244, 220, 149, 0.45); }
.standings .avatar { width: 30px; height: 30px; font-size: 0.9rem; }
.standings .who { flex: 1; min-width: 0; font-weight: 700; }
.standings .delta { font-weight: 700; font-variant-numeric: tabular-nums; }
.standings .delta.up { color: var(--win); }
.standings .delta.down { color: var(--lose); }
.standings .n { min-width: 64px; text-align: right; font-weight: 800; font-size: 1.05rem; font-variant-numeric: tabular-nums; }
.standings .n small { font-weight: 600; font-size: 0.75rem; color: var(--ink-3); }

.speed-btn { min-height: 40px; height: 40px; padding: 0 14px; font-size: 0.85rem; }
.speed-btn svg { width: 18px; height: 18px; }
.speed-btn[aria-pressed='true'] { color: #2b1c04; border-color: transparent; background: linear-gradient(180deg, var(--gold-2), var(--gold)); }

.toast {
  position: absolute;
  left: 50%;
  top: 12px;
  z-index: 30;
  translate: -50% 0;
  max-width: calc(100% - 32px);
  padding: 12px 22px;
  border-radius: 999px;
  font-weight: 700;
  text-align: center;
  color: var(--ink);
  background: rgba(2, 14, 8, 0.9);
  border: 1px solid var(--panel-border);
  box-shadow: var(--shadow);
  opacity: 0;
  scale: 0.9;
  pointer-events: none;
  transition: opacity 0.25s, scale 0.3s var(--ease);
}
.toast.show { opacity: 1; scale: 1; }
.toast.lose { color: var(--lose); border-color: rgba(255, 114, 114, 0.4); }

/* ---------- Phones: the three opponents in a row along the top ---------- */
@media (max-width: 640px) {
  .topbar .brand { display: none; }
  .topbar-title { border-left: none; padding-left: 4px; font-size: 1.05rem; color: var(--ink); }
  .speed-text { display: none; }
  .speed-btn { width: 36px; padding: 0; }
  .table.mj { --mt: calc(clamp(13px, min(4.4cqw, 3.2cqh), 24px) * var(--fit)); padding-inline: 6px; }
  .board { grid-template-columns: repeat(3, minmax(0, 1fr)); grid-template-areas: 'left top right' 'center center center' 'me me me'; gap: 6px 4px; }
  .opp { width: 100%; align-self: start; }
  .opp .seat-plate { flex-direction: column; gap: 2px; min-height: 0; width: 100%; padding: 4px 4px 5px; border-radius: 12px; text-align: center; }
  .opp .avatar { width: 22px; height: 22px; font-size: 0.72rem; }
  .opp .seat-who { align-items: center; max-width: 100%; }
  .opp .seat-name { font-size: 0.82rem; }
  .opp .seat-tag { font-size: 0.66rem; }
  .opp .seat-count { height: 20px; font-size: 0.72rem; }
  .opp .backs { display: none; }
  .opp .seat[data-pos='left'] .bubble { left: 0; translate: 0 0; }
  .opp .seat[data-pos='left'] .bubble::before { left: 24px; }
  .opp .seat[data-pos='right'] .bubble { left: auto; right: 0; translate: 0 0; }
  .opp .seat[data-pos='right'] .bubble::before { left: auto; right: 16px; }
  .river { --cols: 6; }
  .river[data-pos='left'], .river[data-pos='right'] { --cols: 3; }
  .center { gap: 4px; padding: 6px; }
  .info { padding: 5px 6px; font-size: 0.66rem; white-space: normal; }
  .me .seat-plate { min-height: 36px; padding: 3px 10px 3px 3px; }
  .me .avatar { width: 28px; height: 28px; font-size: 0.85rem; }
  .controls { gap: 6px; }
  .controls .btn-lg { min-height: 46px; padding: 0 16px; font-size: 0.98rem; }
  .act .mini-set { display: none; }
}
.scored-hand .tile.win-tile { outline: 2px solid var(--gold-2); outline-offset: 1px; }
@media (max-width: 480px) {
  .topbar { padding-inline: 12px; gap: 6px; }
  .topbar-heading { flex-direction: column; align-items: flex-start; gap: 3px; }
  .topbar-title { font-size: 1rem; line-height: 1.1; padding-left: 0; }
  .mode-badge { padding: 2px 7px; font-size: 0.75rem; letter-spacing: 0.04em; }
  .topbar .btn-icon, .speed-btn { width: 36px; height: 36px; min-height: 36px; }
}

/* ---------- Online ---------- */
.scored-next, .over-note { margin: 0; color: var(--ink-2); font-weight: 600; font-variant-numeric: tabular-nums; }
.claim-clock { display: inline-block; min-width: 2.4em; margin-left: 4px; padding: 1px 8px; border-radius: 999px; background: rgba(0, 0, 0, 0.35); color: var(--gold-2); font-variant-numeric: tabular-nums; }
.board.watching .me .backs { display: flex; }
.leave-btn { min-height: 44px; color: var(--ink-3); }
.over-members { width: 100%; }
.seg { margin: 0; padding: 0; border: 0; display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.seg legend { width: 100%; margin-bottom: 8px; font-size: 0.78rem; font-weight: 700; letter-spacing: 0.14em; text-transform: uppercase; color: var(--ink-3); }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.seg span { display: inline-grid; place-items: center; min-width: 64px; min-height: 44px; padding: 0 16px; border-radius: 999px; border: 1px solid rgba(255, 255, 255, 0.16); background: rgba(255, 255, 255, 0.06); font-weight: 700; transition: background 0.2s, border-color 0.2s, color 0.2s; }
.seg input:checked + span { color: #2b1c04; border-color: transparent; background: linear-gradient(180deg, var(--gold-2), var(--gold)); }
.seg input:focus-visible + span { outline: 2px solid var(--gold-2); outline-offset: 3px; }
.rank-pill { padding: 0 14px; color: var(--gold-2); text-decoration: none; }
.rank-pill:hover { border-color: rgba(217, 180, 90, 0.55); }
.leave-btn { min-height: 44px; margin-left: 10px; color: var(--ink-3); }
.leave-btn:hover { color: var(--ink); }
.ranked-start p { margin: 0; }
.ranked-terms { font-size: 1.05rem; line-height: 1.5; color: var(--ink-2); }
.ranked-terms strong { color: var(--gold-2); font-size: 1.15rem; }
.ranked-note { font-size: 0.88rem; line-height: 1.4; color: var(--ink-3); }
.ranked-balance { font-weight: 600; color: var(--ink-2); }
.ranked-balance strong, .over-balance strong { color: var(--ink); font-variant-numeric: tabular-nums; }
.ranked-next { color: var(--ink-2); }
.claim-btn .coin { width: 20px; height: 20px; }
.ranked-start.broke h2 { color: var(--lose); }
.over-prize { margin: -6px 0 0; font-family: var(--font-display); font-weight: 700; font-size: 1.25rem; color: var(--gold-2); }
.over-prize.none { font-family: inherit; font-weight: 600; font-size: 1rem; color: var(--ink-2); }
.over-balance { margin: 0; color: var(--ink-2); }
