:root {
  color-scheme: dark;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: #151a18;
  color: #f5f2e8;
  --surface: #202825;
  --felt: #26312c;
  --paper: #f9f5e8;
  --ink: #18241e;
  --gold: #e5c487;
  --muted: #b2beb4;
  --edge: #ffffff1c;
  --warning: #ffd5ae;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --max-app: 510px;
  -webkit-tap-highlight-color: transparent;
}

*, *::before, *::after { box-sizing: border-box; }
body { min-width: 280px; margin: 0; }
button, input { font: inherit; }
button { color: inherit; cursor: pointer; }
button:disabled { cursor: default; }
a { color: var(--gold); }
button:focus-visible, a:focus-visible, input:focus-visible {
  outline: 2px solid var(--gold);
  outline-offset: 3px;
}
p, h1, h2 { margin: 0; }
[hidden] { display: none !important; }

#app {
  width: min(100%, var(--max-app));
  margin: 0 auto;
  min-height: 100dvh;
  padding-top: var(--safe-top);
  background: var(--surface);
  background-image: radial-gradient(ellipse at 50% -16%, #303c34 0, transparent 69%);
}

@media (min-width: 570px) {
  #app { box-shadow: 0 0 0 1px var(--edge), 0 32px 80px #0005; }
}

.primary, .secondary {
  min-height: 46px;
  border: 0;
  border-radius: 24px;
  padding: 0.65rem 1.3rem;
  font-weight: 650;
  line-height: 1.2;
}
.primary { background: var(--paper); color: var(--ink); }
.secondary { background: #ffffff12; color: #f8f5eb; }
.primary:active, .secondary:active { filter: brightness(.9); }
.primary:disabled, .secondary:disabled { opacity: .48; }
.danger { color: #ffc9b0; }
.spaced { margin-top: 1rem; }
.stack { display: grid; gap: 0.8rem; }
.caption { color: var(--muted); font-size: 0.9rem; line-height: 1.45; }
.error { color: var(--warning); font-size: 0.9rem; line-height: 1.4; }
.number { font-variant-numeric: tabular-nums; }

.icon-button {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border: 1px solid var(--edge);
  border-radius: 50%;
  background: #ffffff13;
  color: #f7f3e8;
  font-size: 1.5rem;
  line-height: 1;
  user-select: none;
}
.icon-button:active { background: #ffffff25; }
.back-icon {
  width: 9px;
  height: 9px;
  border: solid currentColor;
  border-width: 0 0 2px 2px;
  transform: translateX(2px) rotate(45deg);
}
.dots { transform: translateY(-0.13em); letter-spacing: 0.045em; }

.center-page {
  min-height: calc(100dvh - var(--safe-top));
  padding: 44px 28px calc(40px + var(--safe-bottom));
  display: grid;
  place-items: center;
}
.entry { width: min(100%, 320px); }
.wordmark {
  font-family: Georgia, 'Times New Roman', serif;
  font-size: clamp(48px, 12vw, 64px);
  font-weight: 600;
  letter-spacing: -0.067em;
  line-height: 1;
}
.entry .variant { color: var(--gold); margin: 0.6rem 0 2.2rem; }
.entry .entryHeading { font-size: 1.4rem; margin-bottom: .9rem; }
.entry form { margin-top: 1rem; }
.entry-actions { display: flex; flex-wrap: wrap; column-gap: 1rem; }
.text-button {
  min-height: 44px;
  padding: .4rem 0;
  border: 0;
  background: transparent;
  color: var(--gold);
  font-size: .88rem;
  text-align: start;
}
.text-button:active { color: var(--paper); }
.field { display: grid; gap: 0.45rem; font-size: 0.88rem; color: var(--muted); }
.field input {
  width: 100%;
  height: 48px;
  min-width: 0;
  padding: 0 0.85rem;
  border: 1px solid #ffffff35;
  border-radius: 11px;
  background: #1017136b;
  color: #f7f3e8;
  font-size: 1rem;
}

.lobby { padding: 24px 20px calc(28px + var(--safe-bottom)); }
.lobby-header { display: flex; justify-content: space-between; align-items: center; gap: 1rem; }
.lobby-header h1 { flex-shrink: 0; font-size: clamp(1.8rem, 7.4vw, 2.3rem); letter-spacing: -.045em; }
.lobby-actions { display: flex; align-items: center; gap: .6rem; min-width: 0; }
.lobby-actions .icon-button { flex-shrink: 0; }
.player-name { color: var(--muted); font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lobby-create { width: 100%; margin-top: 1.2rem; }
.lobby-alerts { display: flex; justify-content: space-between; align-items: center; gap: .75rem; width: 100%; min-height: 46px; margin-top: .35rem; padding: .6rem .2rem; border: 0; border-bottom: 1px solid var(--edge); background: transparent; color: var(--gold); text-align: start; font-size: .92rem; }
.lobby-alerts .forward-icon { border-color: currentColor; margin-inline-end: .3rem; }
.lobby-note { margin-top: 1rem; }
.lobby .empty-lobby { margin: 18dvh auto 0; max-width: 270px; text-align: center; }
.empty-lobby h2 { font-size: 1.25rem; }
.empty-lobby .caption { margin-top: .55rem; }
.queue { margin-top: 1.45rem; }
.queue h2 { font-size: 0.9rem; font-weight: 650; color: var(--muted); margin: 0 0 .45rem .15rem; }
.queue > ul { margin: 0; padding: 0; list-style: none; }
.game-row {
  display: grid;
  grid-template-columns: 2.25rem minmax(0, 1fr) auto .49rem;
  align-items: center;
  gap: .8rem;
  width: 100%;
  min-height: 65px;
  margin-top: .3rem;
  padding: .65rem .85rem;
  border: 1px solid var(--edge);
  border-radius: 13px;
  background: #ffffff08;
  text-align: start;
}
.game-row:active { background: #ffffff17; }
.game-row .avatar {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  border-radius: 50%;
  background: #ffffff10;
  color: #e5ede4;
  font-family: Georgia, 'Times New Roman', serif;
  font-size: 1.05rem;
}
.turn-queue .avatar { background: #453d2d; color: var(--gold); }
.game-meta { min-width: 0; }
.game-title-line { display: flex; align-items: baseline; min-width: 0; }
.game-ordinal { flex-shrink: 0; white-space: pre; font-weight: 600; }
.game-name { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 650; }
.game-detail { display: block; margin-top: 0.2rem; color: var(--muted); font-size: .81rem; }
.game-row .number { font-size: .88rem; }
.forward-icon { width: .48rem; height: .48rem; border: solid var(--muted); border-width: 2px 2px 0 0; transform: rotate(45deg); }

html:has(#app.game), body:has(#app.game) {
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
}

#app.game {
  position: fixed;
  inset: 0;
  height: 100dvh;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: clip;
  touch-action: pinch-zoom;
  --card-gap: clamp(3px, 1.1vw, 5px);
  --card-aspect: .735;
  --opponent-scale: .84;
  --caption-font-size: clamp(8.5px, 2.6vw, 11px);
  --caption-height: max(12px, calc(var(--caption-font-size) * 1.25 + 1px));
  --divider-gap: 6px;
  --tray-gap: 9px;
  --tray-padding: 2px;
  --draw-heading-height: 17px;
  --draw-padding: 5px;
  --sorter-gap: 8px;
  --sorter-height: 34px;
}
#app.showdown { --opponent-scale: .9; }

.game-header {
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 44px;
  align-items: center;
  gap: 0.5rem;
  padding: 16px 14px 3px;
  text-align: center;
  flex-shrink: 0;
}
.game-title { min-width: 0; display: grid; gap: 2px; }
.game-title h1 { font-size: 1.05rem; font-weight: 680; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.table {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-height: 0;
  width: 100%;
  padding: 2px 0 4px;
}
.table-space {
  container-type: size;
  display: grid;
  align-content: start;
  flex: 1;
  min-height: 0;
}
.table-layout {
  /* Fit the cards into the space left by the header, result and action controls. */
  --card-rows: calc(4 + 3 * var(--opponent-scale));
  --board-inset: 48px;
  --board-gaps: calc(4 * var(--card-gap) + 2 * var(--divider-gap) + 1px);
  --tray-gaps: calc(var(--tray-gap) + 2 * var(--tray-padding));
  --layout-gaps: calc(var(--board-gaps) + var(--tray-gaps));
  --card-width: clamp(0px, min(calc((100cqw - var(--board-inset)) / 5), calc((100cqh - var(--layout-gaps) - 1px) * var(--card-aspect) / var(--card-rows))), 74px);
}
.has-draw .table-space { align-content: stretch; }
.has-draw .table-layout {
  display: flex;
  flex-direction: column;
  --layout-gaps: calc(var(--board-gaps) + var(--tray-gaps) + var(--draw-heading-height) + 3 * var(--draw-padding) + 1px);
}
.showdown .table-space { align-content: end; }
.showdown .table-layout {
  --card-rows: calc(3 + 3 * var(--opponent-scale));
  --board-inset: calc(80px + 4 * var(--card-gap));
  --layout-gaps: var(--board-gaps);
}
.is-fantasy .table-layout {
  --card-rows: 6;
  --layout-gaps: calc(4 * var(--card-gap) + var(--tray-gaps) + var(--sorter-gap) + var(--sorter-height));
}
.hand-heading {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: .3rem .6rem;
  color: var(--muted);
  font-size: .75rem;
  font-variant-numeric: tabular-nums;
}
.fantasyland-heading {
  flex-shrink: 0;
  margin: 1px 8px 6px;
  color: var(--gold);
  font-size: .9rem;
  font-weight: 650;
  text-align: center;
  animation: arrival .6s ease-out;
}
.board { display: grid; justify-content: center; gap: var(--card-gap); }
.open-seat {
  height: calc(3 * var(--card-width) * var(--opponent-scale) / var(--card-aspect) + 2 * var(--card-gap));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--muted);
  text-align: center;
}
.open-seat p { max-width: 90%; font-size: .9rem; overflow-wrap: anywhere; }
.seat-cards { position: relative; display: block; width: 46px; height: 37px; }
.seat-cards::before, .seat-cards::after {
  content: '';
  position: absolute;
  width: 24px;
  height: 33px;
  border: 1px solid #cbb68a77;
  border-radius: 4px;
  background: var(--felt);
}
.seat-cards::before { left: 5px; top: 1px; transform: rotate(-13deg); }
.seat-cards::after { right: 4px; bottom: 0; transform: rotate(9deg); }
.seat-invite {
  min-height: 44px;
  padding: .4rem 1rem;
  border: 1px solid #e5c4874a;
  border-radius: 22px;
  background: #e5c4870d;
  color: var(--gold);
  font-size: .9rem;
  font-weight: 600;
}
.seat-invite:active { background: #e5c48720; }
.opponent-board { --cell-width: calc(var(--card-width) * var(--opponent-scale)); }
.board-row { position: relative; display: grid; justify-items: center; width: calc(var(--cell-width, var(--card-width)) * 5 + var(--card-gap) * 4); }
.showdown .board-row { width: calc(var(--card-width) * 5 + var(--card-gap) * 4); }
.row-cards { display: flex; gap: var(--card-gap); position: relative; }
.row-caption {
  position: absolute;
  inset: auto 0 0;
  min-height: var(--caption-height);
  padding: 0 2px 1px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
  pointer-events: none;
  border-radius: 0 0 5px 5px;
  background: #131a17e3;
  color: #f7f4eb;
  font-size: var(--caption-font-size);
  line-height: 1.25;
}
.row-caption.invalid { color: var(--warning); }
.row-score-button {
  position: absolute;
  inset-inline-end: -32px;
  top: 50%;
  transform: translateY(-50%);
  width: 44px;
  height: min(100%, 44px);
  display: grid;
  align-items: center;
  justify-items: end;
  border: 0;
  padding: 0;
  background: transparent;
  font-size: .75rem;
  font-weight: 650;
}
.row-points { min-width: 28px; padding: .16em .2em; border-radius: 8px; background: #ffffff0d; }
.row-score-button:active .row-points { background: #ffffff24; }
.row-points.positive { color: var(--gold); }
.divider { height: 1px; width: calc(var(--card-width) * 5 + var(--card-gap) * 4); margin: var(--divider-gap) auto; background: var(--edge); }

.card {
  position: relative;
  flex: none;
  display: block;
  width: var(--cell-width, var(--card-width));
  aspect-ratio: var(--card-aspect);
  padding: 0;
  border: 1px solid #2028231f;
  border-radius: 6px;
  background: var(--paper);
  color: #1a2621;
  box-shadow: 0 1px 1px #0002;
  line-height: 1;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
}
.card.hearts { color: #a72c3c; }
.card.diamonds { color: #26578c; }
.card.clubs { color: #186449; }
.card.empty { background: #ffffff08; border-color: #ffffff2a; box-shadow: none; }
.card.back {
  border-color: #d4ac6944;
  background: repeating-linear-gradient(135deg, #e1c18d14 0 1px, transparent 1px 6px), #334137;
  box-shadow: inset 0 0 0 3px #334137;
}
.card .suit {
  position: absolute;
  inset: 8% auto auto 9%;
  font-size: calc(var(--cell-width, var(--card-width)) * .3);
  font-family: Georgia, 'Times New Roman', serif;
  pointer-events: none;
}
.card .rank {
  position: absolute;
  inset: 58% 0 auto;
  transform: translateY(-50%);
  text-align: center;
  font-family: Georgia, 'Times New Roman', serif;
  font-size: calc(var(--cell-width, var(--card-width)) * .61);
  font-weight: 600;
  letter-spacing: -0.06em;
  pointer-events: none;
}
.card.captioned .rank {
  top: min(52%, calc(100% - var(--caption-height) - var(--cell-width, var(--card-width)) * .305 - 1px));
}
.card[data-move-card] { touch-action: none; border-color: #bd9a57; }
.card.is-last-move { border-color: #b98d45; box-shadow: inset 0 0 0 2px #cca05c; }
.card.is-selected, .card.is-target { outline: 2px solid var(--gold); outline-offset: 1px; z-index: 1; }
.card.is-target.empty { background: #e5c48732; border-color: var(--gold); }
.card.is-pressed { transform: translateY(-2px) rotate(-2deg); }
.card.is-dragged { opacity: .24; transform: none; }
.card.is-discard { background: #e3dfd2; }
.card.is-discard .rank { top: 49%; }
.discard-label {
  position: absolute;
  inset: auto 0 2px;
  font: 650 clamp(8px, calc(var(--card-width) * .19), 12px)/1.15 -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  text-align: center;
  color: #444a40;
  pointer-events: none;
}
.card.just-placed { animation: settle .15s ease-out; }
.card.drag-ghost {
  position: fixed;
  z-index: 5;
  top: 0;
  left: 0;
  pointer-events: none;
  opacity: .86;
  box-shadow: 0 11px 24px #0008;
  border-color: #d5b574;
  will-change: transform;
}

.tray { margin-top: var(--tray-gap); display: grid; justify-items: center; }
.draw-tray {
  align-self: center;
  width: calc(5 * var(--card-width) + 4 * var(--card-gap));
  margin-top: auto;
  padding: var(--draw-padding) 0;
  gap: var(--draw-padding);
  border-top: 1px solid #c4b99740;
  background: linear-gradient(to bottom, #e5c48708, transparent);
}
.draw-heading {
  width: 100%;
  padding-inline: 6px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  height: var(--draw-heading-height);
  color: var(--muted);
  font-size: .77rem;
  line-height: 1.1;
}
.draw-heading strong { font-weight: 600; color: var(--paper); }
.packet { display: grid; grid-template-columns: repeat(5, var(--card-width)); gap: var(--card-gap); padding: var(--tray-padding); border-radius: 7px; }
.packet[data-columns='3'] { grid-template-columns: repeat(3, var(--card-width)); }
.packet.is-target { box-shadow: 0 0 0 1px #e5c487aa; }
.tray-hole { width: var(--card-width); aspect-ratio: var(--card-aspect); }
.tray-hole.returnable { border-radius: 6px; border: 1px dashed #ffffff26; background: transparent; }
.sorters { display: flex; justify-content: center; gap: .45rem; margin-top: var(--sorter-gap); }
.sorters button { min-height: var(--sorter-height); padding: .2rem 1rem; border: 1px solid var(--edge); border-radius: 1rem; background: #ffffff0d; font-size: .81rem; }
.sorters button[aria-pressed='true'] { color: var(--gold); border-color: #e5c48757; background: #e5c48712; }

.result-summary { flex-shrink: 0; padding: 10px 18px 2px; display: grid; justify-items: center; text-align: center; gap: .3rem; }
.result-totals { display: flex; justify-content: center; align-items: center; gap: 12px; }
.result-totals > span { display: grid; min-width: 68px; gap: .12rem; }
.result-totals > span + span { border-inline-start: 1px solid var(--edge); padding-inline-start: 12px; }
.result-totals small { color: var(--muted); font-size: .72rem; }
.result-totals strong { font-size: 1.25rem; line-height: 1.1; }
.result-earned { display: flex; justify-content: center; gap: .55rem; max-width: 100%; color: var(--muted); font-size: .78rem; line-height: 1.3; }
.result-earned strong { color: var(--paper); font-weight: 650; }
.result-earned-opponent { display: inline-flex; min-width: 0; max-width: 45%; gap: .2rem; }
.result-earned-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.result-note { font-size: .78rem; line-height: 1.3; color: #e4e8df; }
.result-note.foul { color: var(--warning); }
.result-note.award { color: var(--gold); }
.score-breakdown { display: grid; gap: .6rem; margin: .15rem 0; }
.score-breakdown div { display: flex; justify-content: space-between; gap: .7rem; align-items: baseline; }
.score-breakdown dt { min-width: 0; overflow-wrap: anywhere; }
.score-breakdown dd { flex-shrink: 0; margin: 0; }
.score-breakdown small { display: block; margin-top: .1rem; color: var(--muted); font-size: .8rem; }
.score-equation { flex-wrap: wrap; border-top: 1px solid var(--edge); padding-top: .7rem; }
.score-equation strong { color: var(--gold); }

.table-footer {
  display: grid;
  flex-shrink: 0;
  justify-items: center;
  align-content: center;
  min-height: calc(56px + var(--safe-bottom));
  padding: 3px 14px calc(8px + var(--safe-bottom));
  gap: .28rem;
  text-align: center;
  background: linear-gradient(180deg, #20282500, #202825 12px);
}
.table-footer .primary { min-width: 110px; }
.turn-status { color: var(--muted); font-size: .92rem; }
.foul-warning { color: var(--warning); font-size: .79rem; }

#sheet {
  position: fixed;
  inset: auto 0 0;
  width: min(100%, var(--max-app));
  max-width: 100%;
  max-height: calc(100dvh - var(--safe-top) - 10px);
  margin: 0 auto;
  padding: 15px 18px calc(18px + var(--safe-bottom));
  border: solid var(--edge);
  border-width: 1px 1px 0;
  border-radius: 18px 18px 0 0;
  background: #29332e;
  color: inherit;
  overflow-y: auto;
}
#sheet::backdrop { background: #0a100d99; }
.sheet-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: .8rem; }
.sheet-header h2 { font-size: 1.13rem; }
#sheet .queue { margin-top: 0; }
.close-button { min-width: 42px; min-height: 42px; border-radius: 21px; background: #ffffff12; border: 0; font-size: 1.35rem; }
.sheet-action { width: 100%; min-height: 45px; padding: .5rem .8rem; border: 0; background: #ffffff0b; border-radius: 9px; text-align: start; }
.preference { display: flex; align-items: center; justify-content: space-between; }
.preference > span { color: var(--muted); font-size: .85rem; }
.invite-link {
  display: block;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: .7rem .85rem;
  border: 1px solid var(--edge);
  border-radius: 10px;
  background: #141e18;
  font-size: .82rem;
}
.history-points { float: inline-end; }
.recovery-code { font-family: ui-monospace, monospace; }
.reference { padding: .55rem .7rem; border: 1px solid var(--edge); border-radius: 9px; font-size: .9rem; }
.reference summary { min-height: 30px; cursor: pointer; }
.reference dl { display: grid; grid-template-columns: 1fr auto; gap: .35rem 1rem; margin: .35rem 0 0; color: var(--muted); }
.reference dd { margin: 0; font-variant-numeric: tabular-nums; }

@keyframes settle { from { transform: scale(1.05); } to { transform: scale(1); } }
@keyframes arrival { from { color: #fff6d5; } to { color: var(--gold); } }
@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; } }

@media (max-height: 680px) {
  #app.game { --divider-gap: 4px; --tray-gap: 6px; }
  .game-header { padding-top: 13px; padding-bottom: 2px; }
  .table-footer { min-height: calc(50px + var(--safe-bottom)); }
  .table { padding-bottom: 3px; }
}

@media (max-height: 580px) {
  .has-draw .table-layout {
    --draw-padding: 2px;
    --layout-gaps: calc(var(--board-gaps) + var(--tray-gaps) + 2 * var(--draw-padding) + 1px);
  }
  .draw-tray { position: relative; width: calc(100% - 24px); }
  .draw-heading {
    position: absolute;
    inset-inline-start: 0;
    top: 50%;
    transform: translateY(-50%);
    width: calc((100% - 3 * var(--card-width) - 2 * var(--card-gap)) / 2);
    height: auto;
    flex-direction: column;
    align-items: start;
    gap: 3px;
    font-size: .72rem;
  }
}
