:root {
  color-scheme: light;
  --felt: #18563d;
  --felt-dark: #103d2c;
  --felt-light: #216d4e;
  --cream: #f7f1e5;
  --ink: #19201d;
  --red: #b92f3d;
  --gold: #e8c46b;
  /* Board height in card widths (card rows + column depth) and in fixed chrome. */
  --height-units: 6.8;
  --chrome-height: 200px;
  /* Visible band of each card in a suit column, and of each fanned waste card. */
  --row-step-ratio: 0.3;
  --fan-step-ratio: 0.34;
  --gutter-width: 1.6rem;
  --topbar-height: 64px;
  --topbar-inline: clamp(12px, 3vw, 28px);
  --menu-button-size: 42px;
  --menu-drawer-inline: 18px;
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

/* Redeclared on main: custom properties resolve where declared, so main's
   lower-deck budget would otherwise never reach the derived sizes. */
:root,
main {
  /* Columns run 13 rows deep, so cards also shrink to fit short viewports. */
  --card-width: clamp(
    40px,
    min(12.5vw, calc((100svh - var(--chrome-height)) / var(--height-units))),
    120px
  );
  --card-height: calc(var(--card-width) * 1.4);
  --corner-font-size: clamp(0.78rem, calc(var(--card-width) * 0.2), 1.9rem);
  --row-step: max(calc(var(--card-width) * var(--row-step-ratio)), 21px);
  --fan-step: calc(var(--card-width) * var(--fan-step-ratio));
  --column-height: calc(var(--row-step) * 12 + var(--card-height));
}

/* A lower deck adds a third row of cards under the columns. */
main[data-deck-position^="lower"] {
  --height-units: 8.2;
  --chrome-height: 230px;
}

* {
  box-sizing: border-box;
}

html,
body {
  overscroll-behavior-y: none;
}

body {
  min-width: 320px;
  min-height: 100svh;
  margin: 0;
  color: white;
  background:
    radial-gradient(circle at 50% -10%, var(--felt-light), transparent 52%),
    var(--felt);
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
}

button:focus-visible {
  outline: 3px solid var(--gold);
  outline-offset: 3px;
}

.topbar {
  position: relative;
  display: grid;
  grid-template-columns: 44px 1fr auto;
  align-items: center;
  min-height: var(--topbar-height);
  padding: 8px var(--topbar-inline);
  background: rgb(8 37 26 / 58%);
  border-bottom: 1px solid rgb(255 255 255 / 12%);
  box-shadow: 0 2px 15px rgb(0 0 0 / 15%);
}

h1 {
  position: absolute;
  left: 50%;
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(1.25rem, 4vw, 1.8rem);
  font-weight: 600;
  letter-spacing: 0.015em;
  text-align: center;
  transform: translateX(-50%);
}

.icon-button {
  display: grid;
  width: var(--menu-button-size);
  height: var(--menu-button-size);
  padding: 0;
  color: white;
  font-size: 1.65rem;
  line-height: 1;
  place-items: center;
  background: transparent;
  border: 0;
  border-radius: 50%;
  cursor: pointer;
}

.icon-button:hover {
  background: rgb(255 255 255 / 10%);
}

.menu-glyph {
  position: relative;
  display: block;
  width: 21px;
  height: 14px;
}

.menu-glyph i {
  position: absolute;
  left: 0;
  display: block;
  width: 100%;
  height: 2px;
  background: currentColor;
  border-radius: 2px;
}

.menu-glyph i:nth-child(1) {
  top: 0;
}

.menu-glyph i:nth-child(2) {
  top: 6px;
}

.menu-glyph i:nth-child(3) {
  top: 12px;
}

.icon-button.dark {
  color: var(--ink);
}

.icon-button.dark:hover {
  background: rgb(0 0 0 / 7%);
}

.score {
  display: flex;
  align-items: baseline;
  justify-self: end;
  gap: 5px;
  min-height: 36px;
  padding: 6px 12px;
  color: rgb(255 255 255 / 70%);
  font-size: 0.72rem;
  line-height: 1;
  white-space: nowrap;
  background: rgb(255 255 255 / 9%);
  border: 1px solid rgb(255 255 255 / 10%);
  border-radius: 999px;
}

.score > span:first-child {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
}

.score strong {
  color: white;
  font-size: 1.08rem;
  line-height: 1;
}

main {
  display: grid;
  width: min(100%, 900px);
  margin: 0 auto;
  padding: clamp(14px, 3vw, 28px);
  gap: 0 clamp(8px, 2vw, 24px);
  grid-template-columns: auto 1fr;
  grid-template-areas:
    "deck piles"
    "columns columns"
    "status status"
    "newgame newgame";
  align-items: start;
}

main[data-deck-position="upper-right"] {
  grid-template-areas:
    "piles deck"
    "columns columns"
    "status status"
    "newgame newgame";
}

main[data-deck-position="lower-left"] {
  grid-template-areas:
    ". piles"
    "columns columns"
    "status status"
    "deck ."
    "newgame newgame";
}

main[data-deck-position="lower-right"] {
  grid-template-areas:
    ". piles"
    "columns columns"
    "status status"
    ". deck"
    "newgame newgame";
}

.deck-group {
  grid-area: deck;
  justify-self: start;
}

/* On the right, keep the waste on the inner side of the deck. */
main[data-deck-position="upper-right"] .deck-group,
main[data-deck-position="lower-right"] .deck-group {
  justify-self: end;
  flex-direction: row-reverse;
}

main[data-deck-position="lower-left"] .deck-group,
main[data-deck-position="lower-right"] .deck-group {
  margin-top: 18px;
}

.piles {
  grid-area: piles;
  justify-self: end;
}

main[data-deck-position="upper-right"] .piles {
  justify-self: start;
}

.deck-group,
.piles {
  display: flex;
  gap: clamp(7px, 1.5vw, 14px);
}

.card-slot,
.playing-card {
  position: relative;
  display: block;
  width: var(--card-width);
  height: var(--card-height);
  padding: 0;
  border-radius: clamp(5px, 1.2vw, 9px);
}

.card-slot {
  /* No border on the slot itself — a bordered box shrinks the content area and
     top-left-pins the card, so a shorter ghost shares the top edge and leaves a
     gap at the bottom. Helper chrome lives on an explicitly centered ::before. */
  border: 0;
  background: transparent;
  cursor: pointer;
  touch-action: none;
}

.card-slot::before {
  box-sizing: border-box;
  position: absolute;
  top: 50%;
  left: 50%;
  width: calc(var(--card-width) - 6px);
  height: calc(var(--card-height) - 6px);
  content: "";
  background: rgb(0 0 0 / 9%);
  border: 2px solid rgb(255 255 255 / 20%);
  border-radius: clamp(4px, 1.1vw, 8px);
  transform: translate(-50%, -50%);
  pointer-events: none;
}

.card-slot:has(> .playing-card)::before,
.card-slot.card-back::before {
  content: none;
}

.card-slot > .playing-card {
  /* Full-size card, same box as the slot — shares the ghost's centerline. */
  position: relative;
  z-index: 1;
}

.playing-card {
  overflow: hidden;
  color: var(--ink);
  background: linear-gradient(145deg, #fff 0%, var(--cream) 100%);
  border: 1px solid rgb(0 0 0 / 18%);
  box-shadow: 0 3px 7px rgb(0 0 0 / 24%);
  cursor: pointer;
  touch-action: none;
  user-select: none;
}

.playing-card.red {
  color: var(--red);
}

.playing-card .corner {
  position: absolute;
  top: 7%;
  left: 10%;
  display: flex;
  flex-direction: row;
  align-items: baseline;
  gap: 0.12em;
  font-family: Georgia, "Times New Roman", serif;
  font-size: var(--corner-font-size);
  font-weight: 700;
  line-height: 1;
}

.playing-card .suit-large {
  position: absolute;
  right: 9%;
  bottom: 3%;
  font-family: Georgia, "Times New Roman", serif;
  font-size: calc(var(--card-width) * 0.58);
  line-height: 1;
}

.card-back {
  background-color: #173e78;
  background-image:
    linear-gradient(30deg, #315b98 12%, transparent 12.5%, transparent 87%, #315b98 87.5%),
    linear-gradient(150deg, #315b98 12%, transparent 12.5%, transparent 87%, #315b98 87.5%),
    linear-gradient(30deg, #315b98 12%, transparent 12.5%, transparent 87%, #315b98 87.5%),
    linear-gradient(150deg, #315b98 12%, transparent 12.5%, transparent 87%, #315b98 87.5%);
  background-position:
    0 0,
    0 0,
    12px 21px,
    12px 21px;
  background-size: 24px 42px;
  border: 4px solid #f3e9d5;
  box-shadow:
    inset 0 0 0 2px #173e78,
    0 3px 7px rgb(0 0 0 / 24%);
}

.selected {
  outline: 4px solid var(--gold);
  outline-offset: 2px;
  transform: translateY(-4px);
}

.stock-count,
.pile-count {
  position: absolute;
  left: 4px;
  bottom: 4px;
  z-index: 2;
  min-width: 22px;
  padding: 2px 5px;
  color: white;
  font-size: clamp(0.69rem, calc(var(--card-width) * 0.09), 0.9rem);
  font-weight: 700;
  background: rgb(0 0 0 / 64%);
  border-radius: 10px;
}


.card-slot.recycle::after {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  content: "↻";
  color: rgb(255 255 255 / 45%);
  font-size: calc(var(--card-width) * 0.55);
  line-height: 1;
}

#waste {
  width: calc(var(--card-width) + var(--fan-step) * 2);
}

#waste::before {
  left: 3px;
  transform: translateY(-50%);
}

#waste > .playing-card {
  position: absolute;
  top: 0;
  left: calc(var(--fan, 0) * var(--fan-step));
}

/* Covered waste cards only need their corner. */
#waste > .playing-card:not(.is-top) .suit-large {
  display: none;
}

.columns-area {
  position: relative;
  grid-area: columns;
  display: flex;
  justify-content: center;
  gap: clamp(8px, 3vw, 28px);
  margin-top: clamp(24px, 5vw, 48px);
}

/* Mirror the rank gutter so the four columns sit centered on the board. */
.columns-area::after {
  flex: 0 0 var(--gutter-width);
  content: "";
}

.rank-gutter {
  position: relative;
  flex: 0 0 var(--gutter-width);
  height: var(--column-height);
}

.rank-gutter span {
  position: absolute;
  top: calc(var(--row) * var(--row-step) + var(--card-height) * 0.07);
  right: 0;
  color: rgb(255 255 255 / 38%);
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(0.7rem, calc(var(--card-width) * 0.16), 1.1rem);
  font-weight: 700;
  line-height: 1;
}

.suit-column {
  position: relative;
  flex: 0 0 var(--card-width);
  height: var(--column-height);
}

.suit-column.is-empty::before {
  box-sizing: border-box;
  position: absolute;
  top: 2px;
  left: 2px;
  width: calc(var(--card-width) - 4px);
  height: calc(var(--card-height) - 4px);
  content: "";
  background: rgb(0 0 0 / 9%);
  border: 2px dashed rgb(255 255 255 / 18%);
  border-radius: clamp(4px, 1.1vw, 8px);
}

.column-start-rank {
  position: absolute;
  top: calc(var(--card-height) * 0.07 + 2px);
  left: calc(var(--card-width) * 0.1 + 3px);
  color: rgb(255 255 255 / 22%);
  font-family: Georgia, "Times New Roman", serif;
  font-size: var(--corner-font-size);
  font-weight: 700;
  line-height: 1;
}

.suit-column > .playing-card {
  position: absolute;
  top: calc(var(--row) * var(--row-step));
  left: 0;
  cursor: default;
}

/* Only the corner of a covered column card shows. */
.suit-column > .playing-card.is-covered .suit-large {
  display: none;
}

.drag-origin {
  opacity: 0.48;
}

.drag-ghost,
.drag-ghost * {
  /* Drops are resolved with elementFromPoint, which must see through the ghost. */
  pointer-events: none !important;
}

.drag-ghost {
  position: fixed !important;
  z-index: 10000 !important;
  margin: 0 !important;
  opacity: 1 !important;
  transform: none !important;
  filter: drop-shadow(0 12px 12px rgb(0 0 0 / 30%));
}

body.dragging,
body.dragging * {
  cursor: grabbing !important;
}

.status {
  grid-area: status;
  min-height: 22px;
  margin: 16px 0 0;
  color: rgb(255 255 255 / 76%);
  font-size: 0.83rem;
  text-align: center;
}

.board-new-game.primary-button {
  display: block;
  grid-area: newgame;
  min-width: 150px;
  margin: 18px auto 0;
  color: #fff !important;
  background: #315b98 !important;
  border: 1px solid #173e78 !important;
  box-shadow: 0 6px 16px rgb(0 0 0 / 32%);
}

.board-new-game.primary-button:hover {
  background: #173e78 !important;
}

.board-new-game[hidden] {
  display: none;
}

.build-info {
  position: fixed;
  right: 10px;
  bottom: max(8px, env(safe-area-inset-bottom));
  z-index: 50;
  color: rgb(255 255 255 / 38%);
  font-size: 0.67rem;
  letter-spacing: 0.04em;
  cursor: pointer;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

.build-info:focus-visible {
  color: rgb(255 255 255 / 70%);
  outline: 2px solid var(--gold);
  outline-offset: 3px;
}

.menu-backdrop {
  position: fixed;
  inset: 0;
  z-index: 800;
  background: rgb(4 24 17 / 62%);
  border: 0;
  cursor: pointer;
}

.menu-backdrop[hidden] {
  display: none;
}

.menu-drawer {
  position: fixed;
  top: 0;
  bottom: 0;
  left: 0;
  z-index: 850;
  display: flex;
  flex-direction: column;
  width: min(320px, calc(100% - 48px));
  padding: 0 var(--menu-drawer-inline) max(22px, env(safe-area-inset-bottom));
  color: var(--ink);
  background: #fffdf8;
  box-shadow: 4px 0 18px rgb(0 0 0 / 22%);
  transform: translateX(-105%);
  visibility: hidden;
  transition:
    transform 220ms ease,
    visibility 220ms ease;
  will-change: transform;
}

.menu-drawer.is-open {
  transform: translateX(0);
  visibility: visible;
}

.menu-actions {
  display: grid;
  gap: 9px;
}

.menu-actions button,
.menu-actions .menu-link,
.result-actions button,
.primary-button {
  min-height: 46px;
  padding: 10px 16px;
  color: var(--ink);
  font: inherit;
  font-weight: 650;
  text-align: center;
  text-decoration: none;
  background: #f1ecdf;
  border: 1px solid #ddd5c4;
  border-radius: 9px;
  cursor: pointer;
}

.menu-actions button:hover,
.menu-actions .menu-link:hover,
.result-actions button:hover {
  background: #e9e1d2;
}

.menu-actions .menu-link {
  display: grid;
  place-items: center;
}

.menu-divider {
  width: 40%;
  height: 1px;
  margin: 10px auto;
  background: rgb(0 0 0 / 14%);
}

.gg-menu-auth {
  display: grid;
  gap: 9px;
  margin-top: 4px;
}

.gg-menu-auth .gg-login-btn {
  color: #fff;
  background: var(--felt);
  border-color: var(--felt-dark);
}

.gg-menu-auth .gg-logout-btn {
  color: #fff;
  background: var(--red);
  border-color: #8f2430;
}

.gg-menu-auth .gg-login-btn[hidden],
.gg-menu-auth .gg-logout-btn[hidden] {
  display: none;
}

.gg-screen-name {
  margin: 0 0 8px;
  color: rgb(0 0 0 / 55%);
  font-family: ui-sans-serif, system-ui, sans-serif;
  font-size: 0.82rem;
  text-align: center;
}

.gg-screen-name[hidden] {
  display: none;
}

dialog {
  width: min(calc(100% - 32px), 430px);
  padding: 22px;
  color: var(--ink);
  background: #fffdf8;
  border: 0;
  border-radius: 16px;
  box-shadow: 0 20px 60px rgb(0 0 0 / 38%);
}

dialog::backdrop {
  background: rgb(4 24 17 / 72%);
}

.dialog-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}

.menu-drawer .dialog-heading {
  min-height: var(--topbar-height);
  margin-bottom: 12px;
  /* Sit beside the close button (aligned to the hamburger). */
  padding-left: calc(var(--topbar-inline) + var(--menu-button-size) - var(--menu-drawer-inline));
  justify-content: flex-start;
}

.menu-drawer #close-menu {
  position: absolute;
  top: calc((var(--topbar-height) - var(--menu-button-size)) / 2);
  left: var(--topbar-inline);
}

.dialog-heading h2,
.result-dialog h2 {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1.65rem;
}

.primary-button {
  color: white !important;
  background: var(--felt) !important;
  border-color: var(--felt-dark) !important;
}

.rules-copy {
  max-height: min(58vh, 520px);
  overflow-y: auto;
  line-height: 1.52;
}

.rules-copy p:first-child {
  margin-top: 0;
}

.option-group {
  margin: 0 0 20px;
  padding: 0;
  border: 0;
}

.option-group legend {
  margin-bottom: 12px;
  padding: 0;
  font-weight: 700;
  font-size: 0.95rem;
}

.option-choices {
  display: grid;
  gap: 8px;
}

.option-choice {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 46px;
  padding: 10px 14px;
  background: #f1ecdf;
  border: 1px solid #ddd5c4;
  border-radius: 9px;
  cursor: pointer;
}

.option-choice:hover {
  background: #e9e1d2;
}

.option-choice:has(:checked) {
  background: #e4dbd0;
  border-color: #c9bea6;
}

.option-choice:has(:checked):hover {
  background: #ddd4c6;
}

.option-choice input {
  width: 1.1em;
  height: 1.1em;
  accent-color: var(--felt);
  flex-shrink: 0;
}

.option-choice span {
  font-weight: 650;
}

/* Deck location: one portrait phone screen with a card spot in each corner. */
.deck-phone {
  position: relative;
  width: 124px;
  height: 220px;
  margin: 0 auto;
  border: 3px solid #a99f8c;
  border-radius: 18px;
}

/* Speaker notch, so the outline reads as a phone. */
.deck-phone::before {
  content: "";
  position: absolute;
  top: 6px;
  left: 50%;
  width: 28px;
  height: 4px;
  background: #a99f8c;
  border-radius: 2px;
  transform: translateX(-50%);
}

.deck-corner {
  position: absolute;
  width: 34px;
  height: 48px;
  background: #ddd5c4;
  border-radius: 5px;
  cursor: pointer;
}

.deck-corner:hover {
  background: #cfc6b3;
}

.deck-corner:has(:checked) {
  background: var(--felt);
}

.deck-corner:has(:focus-visible) {
  outline: 2px solid var(--felt);
  outline-offset: 2px;
}

.deck-corner input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.deck-corner[data-corner^="upper"] {
  top: 18px;
}

.deck-corner[data-corner^="lower"] {
  bottom: 10px;
}

.deck-corner[data-corner$="left"] {
  left: 10px;
}

.deck-corner[data-corner$="right"] {
  right: 10px;
}

#options-done {
  width: 100%;
}

.rules-dialog > .primary-button {
  width: 100%;
  margin-top: 8px;
}

.result-dialog {
  text-align: center;
}

.result-symbol {
  margin-bottom: 6px;
  font-size: 3rem;
}

.result-dialog p {
  color: #4f5753;
  line-height: 1.45;
}

.result-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-top: 20px;
}

@media (max-width: 620px) {
  :root {
    --height-units: 9.4;
    --row-step-ratio: 0.55;
    --fan-step-ratio: 0.4;
    --gutter-width: 1.1rem;
  }

  main[data-deck-position^="lower"] {
    --height-units: 10.8;
  }

  main {
    padding-inline: 9px;
  }

  .deck-group,
  .piles {
    gap: 4px;
  }

  .playing-card .corner {
    top: 6%;
    left: 8%;
    gap: 0.06em;
    font-size: min(calc(var(--card-width) * 0.42), 2rem);
    white-space: nowrap;
  }

  .column-start-rank {
    font-size: min(calc(var(--card-width) * 0.42), 2rem);
  }
}

@media (max-width: 410px) {
  :root {
    --topbar-inline: 8px;
  }

  .topbar {
    padding-inline: var(--topbar-inline);
  }

  .score {
    min-height: 34px;
    padding-inline: 10px;
  }

  h1 {
    font-size: 1.15rem;
  }
}

@media (prefers-reduced-motion: no-preference) {
  .playing-card,
  .card-slot,
  dialog {
    transition:
      transform 140ms ease,
      box-shadow 140ms ease,
      opacity 140ms ease;
  }

  dialog[open] {
    animation: appear 160ms ease-out;
  }

  @keyframes appear {
    from {
      opacity: 0;
      transform: translateY(8px) scale(0.98);
    }
  }
}

@media (prefers-reduced-motion: reduce) {
  .menu-drawer {
    transition: none;
  }
}
