/*
 * Table screen: one responsive CSS grid (see docs/PAPER_INK.md §5.4).
 *
 * Everything is sized from `--u`, which tracks the stage container so the
 * table fits desktop, phone landscape, and phone portrait without JS
 * coordinates. Portrait reflows into the two-column layout with a two-row
 * hand.
 */

.table-screen {
  --u: clamp(4.5px, min(1cqw, 1.6cqh), 12px);
  --hand-card: calc(var(--u) * 7.4);
  --hand-step: calc(var(--hand-card) * 0.48);
  --kitty-card: calc(var(--u) * 5.6);
  --play-card: calc(var(--u) * 6.2);
  --play-step: calc(var(--play-card) * 0.42);

  position: absolute;
  inset: 0;
  overflow: hidden;
  isolation: isolate;
  font-family: var(--font-ui);
  font-size: calc(var(--u) * 1.5);
  line-height: 1.3;
  color: var(--text);
  user-select: none;
  -webkit-user-select: none;
}

.table-screen *,
.table-screen *::before,
.table-screen *::after {
  box-sizing: border-box;
}

.table-bg {
  position: absolute;
  inset: 0;
  z-index: 0;
  background: var(--pattern), var(--bg);
  pointer-events: none;
}

/* -------------------------------------------------------------------- grid */

.tbl {
  position: relative;
  z-index: 1;
  display: grid;
  height: 100%;
  max-width: calc(var(--u) * 150);
  margin: 0 auto;
  padding:
    max(calc(var(--u) * 2), var(--safe-top))
    max(calc(var(--u) * 2.4), var(--safe-right))
    max(calc(var(--u) * 1.6), var(--safe-bottom))
    max(calc(var(--u) * 2.4), var(--safe-left));
  gap: calc(var(--u) * 1.2) calc(var(--u) * 2);
  grid-template-columns:
    minmax(0, calc(var(--u) * 21))
    minmax(0, 1fr)
    minmax(0, calc(var(--u) * 21));
  grid-template-rows: auto minmax(0, 1fr) auto auto;
  grid-template-areas:
    "info   kitty   score"
    "left   center  right"
    "me     actions tools"
    "hand   hand    hand";
}

.table-info { grid-area: info; }
.table-kitty { grid-area: kitty; }
.table-score { grid-area: score; }
.seat--left { grid-area: left; }
.seat--right { grid-area: right; }
.table-centre { grid-area: center; }
.seat--bottom { grid-area: me; }
.table-actions { grid-area: actions; }
.table-tools { grid-area: tools; }
.table-hand { grid-area: hand; }

/* ------------------------------------------------------------------ panels */

.table-panel {
  align-self: start;
  border: 1px solid var(--line);
  border-radius: calc(var(--u) * 0.6);
  background: var(--panel);
  pointer-events: none;
}

.hud {
  padding: calc(var(--u) * 0.9) calc(var(--u) * 1.4);
  font-size: calc(var(--u) * 1.45);
  line-height: 1.75;
}

.hud-row {
  display: flex;
  justify-content: space-between;
  gap: var(--u);
  white-space: nowrap;
}

.hud-label {
  color: var(--muted);
}

.hud-value {
  overflow: hidden;
  font-weight: 700;
  text-overflow: ellipsis;
}

.hud-extra {
  margin-top: calc(var(--u) * 0.4);
  padding-top: calc(var(--u) * 0.4);
  border-top: 1px solid var(--line);
  color: var(--accent);
  font-size: calc(var(--u) * 1.15);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  white-space: nowrap;
}

.scoreboard {
  padding: calc(var(--u) * 0.8) calc(var(--u) * 1.3);
  font-size: calc(var(--u) * 1.45);
}

.scoreboard table {
  width: 100%;
  border-collapse: collapse;
  table-layout: fixed;
  font-variant-numeric: tabular-nums;
}

.scoreboard th {
  padding-bottom: calc(var(--u) * 0.35);
  color: var(--muted);
  font-size: calc(var(--u) * 1.05);
  font-weight: 600;
  letter-spacing: 0.14em;
  text-align: right;
  text-transform: uppercase;
}

.scoreboard td {
  padding: calc(var(--u) * 0.2) 0;
  border-top: 1px solid var(--line);
  overflow: hidden;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.scoreboard th.col-name,
.scoreboard td.col-name {
  width: 40%;
  text-align: left;
}

.scoreboard tr.scoreboard-human td {
  color: var(--accent);
  font-weight: 700;
}

/* ------------------------------------------------------------------- kitty */

.bottom-tray {
  --cw: var(--kitty-card);
  display: flex;
  justify-content: center;
  align-items: flex-start;
  gap: calc(var(--u) * 0.7);
  pointer-events: none;
  transition: opacity 0.25s ease;
}

.bottom-tray--dim {
  opacity: 0.85;
}

.bottom-slot {
  position: relative;
}

/* ------------------------------------------------------------------- seats */

.seat {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  align-self: center;
  gap: calc(var(--u) * 0.8);
  min-width: 0;
  pointer-events: none;
}

.seat-avatar {
  position: relative;
  display: grid;
  place-items: center;
  flex: none;
  width: calc(var(--u) * 7);
  height: calc(var(--u) * 7);
  border-radius: 50%;
  background: var(--av3);
  color: var(--av3-ink);
  font: 400 calc(var(--u) * 3.4) / 1 var(--font-display);
  transition: box-shadow 0.2s ease;
}

.seat--left .seat-avatar {
  background: var(--av1);
  color: #fffdf8;
}

.seat--right .seat-avatar {
  background: var(--av2);
  color: #fffdf8;
}

.seat--active .seat-avatar {
  box-shadow:
    0 0 0 calc(var(--u) * 0.3) var(--bg-solid),
    0 0 0 calc(var(--u) * 0.6) var(--accent);
}

.seat-tag {
  position: absolute;
  left: 50%;
  bottom: calc(var(--u) * -1);
  padding: calc(var(--u) * 0.15) calc(var(--u) * 0.55);
  border-radius: calc(var(--u) * 0.3);
  background: var(--accent-fill);
  color: #fffdf8;
  font: 900 calc(var(--u) * 1.1) / 1.2 var(--font-seal);
  white-space: nowrap;
  transform: translateX(-50%);
}

.seat-tag[hidden] {
  display: none;
}

.seat--landlord-pop .seat-tag {
  animation: landlordPop 0.5s ease-out;
}

@keyframes landlordPop {
  0% { transform: translateX(-50%) scale(0.3); opacity: 0; }
  60% { transform: translateX(-50%) scale(1.25); opacity: 1; }
  100% { transform: translateX(-50%) scale(1); }
}

.seat-plate {
  display: flex;
  align-items: center;
  gap: calc(var(--u) * 0.8);
  max-width: 100%;
  padding: calc(var(--u) * 0.35) calc(var(--u) * 0.4) calc(var(--u) * 0.35) calc(var(--u) * 1.2);
  border: 1px solid var(--line);
  border-radius: 99px;
  background: var(--panel);
  font-size: calc(var(--u) * 1.5);
  font-weight: 700;
  white-space: nowrap;
}

.seat-name {
  overflow: hidden;
  text-overflow: ellipsis;
}

.seat-count {
  min-width: calc(var(--u) * 2.7);
  padding: calc(var(--u) * 0.1) calc(var(--u) * 0.55);
  border-radius: 99px;
  background: var(--accent-fill);
  color: #fffdf8;
  font-size: calc(var(--u) * 1.25);
  font-weight: 800;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.seat-minis {
  display: flex;
  padding-left: calc(var(--u) * 1.6);
}

.seat-mini {
  --cw: calc(var(--u) * 2.8);
  --lat: calc(var(--cw) * 0.22);
  position: relative;
  width: var(--cw);
  height: calc(var(--cw) * 1.4);
  margin-left: calc(var(--u) * -1.6);
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-radius: calc(var(--cw) * 0.1);
  background:
    repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.22) 0 1px, transparent 1px var(--lat)),
    repeating-linear-gradient(-45deg, rgba(255, 255, 255, 0.22) 0 1px, transparent 1px var(--lat)),
    var(--accent-fill);
  box-shadow: calc(var(--u) * -0.15) 0 calc(var(--u) * 0.3) rgba(27, 26, 23, 0.15);
}

/* Stamps sit beside the avatar, toward the centre of the table. */
.seat-stamp {
  position: absolute;
  top: var(--u);
  z-index: 2;
  font-size: calc(var(--u) * 1.3);
}

.seat-stamp[hidden] {
  display: none;
}

.seat--left .seat-stamp {
  left: calc(50% + var(--u) * 4.2);
}

.seat--right .seat-stamp {
  right: calc(50% + var(--u) * 4.2);
}

.seat-stamp--pop {
  animation: stampPop 0.24s var(--ease-out);
}

.seat-stamp--pop.stamp--r {
  animation-name: stampPopR;
}

@keyframes stampPop {
  from { transform: scale(1.7) rotate(-14deg); opacity: 0; }
  to { transform: rotate(-6deg); opacity: 1; }
}

@keyframes stampPopR {
  from { transform: scale(1.7) rotate(12deg); opacity: 0; }
  to { transform: rotate(5deg); opacity: 1; }
}

/* The local seat: avatar + nameplate in a row, stamp above. */
.seat--bottom {
  flex-direction: row;
  align-self: end;
  justify-self: start;
  gap: var(--u);
}

.seat--bottom .seat-avatar {
  width: calc(var(--u) * 5.6);
  height: calc(var(--u) * 5.6);
  font-size: calc(var(--u) * 2.6);
}

.seat--bottom .seat-stamp {
  top: auto;
  bottom: calc(100% + var(--u) * 0.6);
  left: calc(var(--u) * 2);
}

/* ------------------------------------------------------------------ centre */

.table-centre {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--u);
  min-height: 0;
  text-align: center;
  pointer-events: none;
}

.centre-who {
  min-height: 1.3em;
  color: var(--muted);
  font-size: calc(var(--u) * 1.3);
  font-weight: 600;
  letter-spacing: 0.14em;
  text-transform: uppercase;
}

.centre-played {
  --cw: var(--play-card);
  display: flex;
  justify-content: center;
}

.centre-played[hidden],
.centre-bid[hidden] {
  display: none;
}

.centre-played .card + .card {
  margin-left: calc(var(--play-step) - var(--cw));
}

.centre-bid {
  display: flex;
  align-items: center;
  gap: calc(var(--u) * 1.4);
}

.centre-bid-number {
  display: grid;
  place-items: center;
  width: calc(var(--u) * 7);
  height: calc(var(--u) * 7);
  border: calc(var(--u) * 0.25) solid var(--text);
  border-radius: 50%;
  font: 400 calc(var(--u) * 4.4) / 1 var(--font-display);
}

.centre-bid-text {
  display: flex;
  flex-direction: column;
  font-size: calc(var(--u) * 1.5);
  line-height: 1.3;
  text-align: left;
}

.centre-bid-note {
  color: var(--muted);
}

.table-hint {
  min-height: 1.2em;
  font: 400 calc(var(--u) * 2) / 1.2 var(--font-display);
}

/* ----------------------------------------------------------------- actions */

.table-actions {
  display: flex;
  justify-content: center;
  align-items: center;
  align-self: end;
  min-width: 0;
}

.table-actions[hidden] {
  display: flex;
  visibility: hidden;
}

.table-tools {
  display: flex;
  justify-content: flex-end;
  align-self: end;
  gap: calc(var(--u) * 0.7);
}

.table-tools .icon-btn {
  width: max(40px, calc(var(--u) * 4.2));
  height: max(40px, calc(var(--u) * 4.2));
  border-radius: calc(var(--u) * 0.6);
}

.table-tools .icon-svg {
  width: 52%;
  height: 52%;
}

/* -------------------------------------------------------------------- hand */

.table-hand {
  --cw: var(--hand-card);
  display: grid;
  grid-auto-flow: column;
  grid-auto-columns: var(--hand-step);
  justify-content: center;
  align-items: start;
  min-height: calc(var(--cw) * 1.389 + var(--u) * 1.4);
  /* The last card overflows its column; pad so the fan stays centred. */
  padding: calc(var(--u) * 1.4) calc(var(--cw) - var(--hand-step)) 0 0;
}

/* ---------------------------------------------------------------- portrait */

@media (orientation: portrait) {
  .table-screen {
    --u: clamp(6px, 2.2cqw, 11px);
    --hand-card: calc(var(--u) * 7.2);
    --hand-step: calc(var(--u) * 3.5);
    --play-card: calc(var(--u) * 5.6);
  }

  .tbl {
    max-width: none;
    padding:
      max(calc(var(--u) * 2.4), var(--safe-top))
      max(calc(var(--u) * 2.4), var(--safe-right))
      max(calc(var(--u) * 2), var(--safe-bottom))
      max(calc(var(--u) * 2.4), var(--safe-left));
    gap: calc(var(--u) * 1.4);
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-rows: auto auto auto minmax(0, 1fr) auto auto auto;
    grid-template-areas:
      "info    score"
      "kitty   kitty"
      "left    right"
      "center  center"
      "actions actions"
      "hand    hand"
      "me      tools";
  }

  .seat--bottom,
  .table-tools {
    align-self: center;
  }

  .seat-stamp {
    font-size: calc(var(--u) * 1.15);
  }

  .seat--left .seat-stamp {
    left: auto;
    right: calc(50% + var(--u) * 4);
  }

  .seat--right .seat-stamp {
    right: auto;
    left: calc(50% + var(--u) * 4);
  }

  .table-actions .action-panel {
    width: 100%;
  }

  .table-actions .btn {
    flex: 1;
    min-width: 0;
    padding: 0 calc(var(--u) * 0.6);
  }

  /* Two centred rows of up to ten cards. */
  .table-hand {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    align-content: start;
    justify-self: center;
    max-width: calc(var(--hand-step) * 9 + var(--cw) + 1px);
    min-height: 0;
    padding: calc(var(--u) * 1.6) calc(var(--cw) - var(--hand-step)) calc(var(--u) * 4.7) 0;
  }

  .table-hand .hand-card {
    margin: 0 calc(var(--hand-step) - var(--cw)) calc(var(--u) * 5.4 - var(--cw) * 1.389) 0;
  }

  /* The end of each row is uncovered, so it keeps its pip. */
  .table-hand .hand-card:nth-child(10n) .card-pip {
    display: block;
  }
}

/* ---------------------------------------------------------- reduced motion */

@media (prefers-reduced-motion: reduce) {
  .table-screen .card,
  .table-screen .btn,
  .table-screen .bottom-tray,
  .table-screen .seat-avatar,
  .table-screen .panel {
    transition: none !important;
    animation: none !important;
  }

  .seat-stamp--pop,
  .seat--landlord-pop .seat-tag {
    animation: none !important;
  }
}
