: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 (deck + row, with room to spare) and in fixed chrome. */
  --height-units: 4.4;
  --chrome-height: 190px;
  --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 {
  /* Seven across on a phone (three at each end plus the tucked-away middle). */
  --card-width: clamp(
    36px,
    min(12.2vw, calc((100svh - var(--chrome-height)) / var(--height-units))),
    96px
  );
  --card-height: calc(var(--card-width) * 1.4);
  --corner-font-size: clamp(0.78rem, calc(var(--card-width) * 0.2), 1.9rem);
  --grid-gap: clamp(3px, 1vw, 10px);
}

* {
  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%, 1180px);
  margin: 0 auto;
  padding: clamp(14px, 3vw, 28px);
  grid-template-columns: minmax(0, 1fr);
  grid-template-areas:
    "deck"
    "row"
    "status"
    "newgame";
  align-items: start;
}

/* Status goes under the deck: its text wraps to a different number of lines as it
   changes, which would otherwise bounce the deck up and down mid-move. */
main[data-deck-position^="lower"] {
  grid-template-areas:
    "row"
    "deck"
    "status"
    "newgame";
}

.deck-group {
  grid-area: deck;
  display: flex;
  align-items: center;
  gap: clamp(12px, 3vw, 24px);
  justify-self: start;
  margin-bottom: clamp(18px, 5vw, 40px);
}

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

main[data-deck-position^="lower"] .deck-group {
  margin-top: clamp(18px, 5vw, 40px);
  margin-bottom: 0;
}

/* The most recent group cleared, as three small cards and their total. */
.last-group {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 5px;
  min-height: 1.6em;
  margin: 0;
  color: rgb(255 255 255 / 72%);
  font-size: 0.82rem;
}

main[data-deck-position$="right"] .last-group {
  justify-content: flex-end;
}

.last-group-label {
  width: 100%;
  font-size: 0.66rem;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
}

main[data-deck-position$="right"] .last-group-label {
  text-align: right;
}

.mini-card {
  min-width: 2.2em;
  padding: 3px 5px;
  color: var(--ink);
  font-family: Georgia, "Times New Roman", serif;
  font-weight: 700;
  text-align: center;
  background: var(--cream);
  border-radius: 4px;
  box-shadow: 0 1px 3px rgb(0 0 0 / 25%);
}

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

.last-group-total {
  margin-left: 2px;
  color: white;
  font-weight: 700;
}

/* The row: left to right, new cards on the right. Room above for a lifted pick. */
.card-row {
  grid-area: row;
  display: flex;
  align-items: center;
  gap: var(--grid-gap);
  min-width: 0;
  min-height: calc(var(--card-height) + 12px);
  padding-top: 6px;
}

.row-cell,
.row-placeholder {
  flex: none;
}

.row-placeholder {
  cursor: default;
}

/* Stands in for the middle of a long row: only the three cards at each end can play. */
.row-tucked {
  display: grid;
  flex: none;
  align-content: center;
  justify-items: center;
  gap: 2px;
  width: calc(var(--card-width) * 0.62);
  height: var(--card-height);
  color: rgb(255 255 255 / 78%);
  background:
    repeating-linear-gradient(
      90deg,
      rgb(255 255 255 / 16%) 0 2px,
      transparent 2px calc(var(--card-width) * 0.1)
    ),
    rgb(0 0 0 / 14%);
  border-radius: clamp(5px, 1.2vw, 9px);
}

button.row-tucked {
  padding: 0;
  font: inherit;
  border: 0;
  cursor: zoom-in;
  /* A held press opens the whole row, not the browser's text or link menu. */
  -webkit-touch-callout: none;
  user-select: none;
  touch-action: manipulation;
}

button.row-tucked:hover {
  color: white;
  background-color: rgb(0 0 0 / 22%);
}

/* Press-and-hold on the "more" marker: the whole row, overlapped left to right,
   wrapping onto more lines when it's long. */
.row-preview {
  position: fixed;
  inset: 0;
  z-index: 900;
  color: white;
  background: rgb(8 48 34 / 86%);
  cursor: zoom-out;
  contain: paint;
}

.row-preview[hidden] {
  display: none;
}

.row-preview-cards {
  /* Each card shows enough of itself to read its corner. */
  --peek-step: max(calc(var(--card-width) * 0.46), 38px);

  position: absolute;
  top: var(--preview-top);
  left: var(--preview-left);
  display: flex;
  flex-wrap: wrap;
  row-gap: 12px;
  width: var(--preview-width);
  max-height: calc(100svh - var(--preview-top) - 40px);
  /* Room for the last card on each line, which hangs past its narrow slot. */
  padding-right: calc(var(--card-width) - var(--peek-step));
  overflow-y: auto;
  overscroll-behavior: contain;
}

.row-preview-card {
  position: relative;
  flex: none;
  width: var(--peek-step);
  height: var(--card-height);
  animation: row-card-deal 160ms ease-out both;
  animation-delay: var(--deal-delay, 0ms);
}

.row-preview-card > .playing-card {
  position: absolute;
  inset: 0 auto auto 0;
  cursor: inherit;
  box-shadow: -2px 0 6px rgb(0 0 0 / 22%);
}

/* Covered cards only need their corner; the big suit would peek between them. */
.row-preview-card:not(:last-child) .suit-large {
  display: none;
}

/* Cards in the middle can't play until the ends clear; the six at the ends stay bright. */
.row-preview-card > .playing-card.is-middle {
  filter: brightness(0.86);
}

.row-preview-hint {
  position: absolute;
  right: 0;
  bottom: max(10px, env(safe-area-inset-bottom));
  left: 0;
  color: rgb(255 255 255 / 70%);
  font-size: 0.78rem;
  letter-spacing: 0.03em;
  text-align: center;
}

@keyframes row-card-deal {
  from {
    opacity: 0;
    transform: translateX(-10px);
  }
}

.row-tucked-count {
  font-size: clamp(0.85rem, calc(var(--card-width) * 0.26), 1.4rem);
  font-weight: 700;
  line-height: 1;
}

.row-tucked-label {
  font-size: 0.62rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.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 {
  flex: none;
  /* 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 {
  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;
}


/* Middle cards of a row that still fits: only the three at each end can play. */
.row-cell > .playing-card.is-blocked {
  cursor: default;
  filter: brightness(0.86);
}

.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-account-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) {
  main {
    padding-inline: 12px;
  }

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

@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;
  }

  /* A freshly turned card slides in from the deck's side. */
  .row-cell > .playing-card.is-new {
    animation: turn-in 200ms ease-out;
  }

  @keyframes turn-in {
    from {
      opacity: 0;
      transform: translateX(-14px) scale(0.96);
    }
  }

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

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

  .row-preview-card {
    animation: none;
  }
}
