/* ============================================================
   GARDEN SOLITAIRE — styles
   Palette + sizing tokens live in :root. Artwork comes in as
   --art-* variables set by js/art.js.
   ============================================================ */
:root {
  --moss: #7A9A5B;
  --moss-deep: #4E6B3A;
  --sage: #A3B18A;
  --cream: #FBF6EA;
  --paper: #FFFDF7;
  --ink: #3B3A36;
  --ink-soft: #6B675E;
  --maroon: #98424F;      /* sampled from Alli's red-suit symbols */
  --suit-dark: #220811;   /* sampled from her black-suit face cards: a very dark plum-brown */
  --blush: #F2B8B5;
  --butter: #F2C75C;
  --bark: #6B4F3A;

  --font: "EB Garamond", "Adobe Garamond Pro", Garamond, Georgia, serif;

  --gutter: clamp(10px, 2.5vw, 40px);
  --gap: clamp(5px, 1.5vw, 28px);          /* between the seven columns */
  --side-gap: clamp(10px, 3vw, 64px);      /* between the side piles and the columns */
  --stack-gap: clamp(6px, 1.4vh, 16px);    /* between piles in the side columns */
  /* How far a card sits below the one under it in a column, as a share of a
     card's height. Read by js/ui.js. */
  --fan-down: 0.11;        /* a face-down card: just enough to read as a stack */
  --fan-up: 0.27;          /* a face-up card: enough to show its corner index */
  --hud-h: 72px;
  --table-max: 1800px;
  /* Layout: foundations | seven columns | stock + waste = 9 card widths across.
     Cards size to whichever runs out first: that width, or the 4 foundations stacked down the left. */
  --card-w-by-width: calc((min(100vw, var(--table-max)) - 2 * var(--gutter) - 6 * var(--gap) - 2 * var(--side-gap)) / 9);
  --card-w-by-height: calc((100vh - var(--hud-h) - 3 * var(--stack-gap) - 46px) / 4 / 1.4);
  --card-w: min(180px, var(--card-w-by-width), max(64px, var(--card-w-by-height)));
  --card-h: calc(var(--card-w) * 1.4);
  --card-r: calc(var(--card-w) * 0.034);   /* matches the corner radius drawn into Alli's card back */
  --shadow: 0 1px 0 rgba(59, 58, 54, .12), 0 3px 8px rgba(59, 58, 54, .14);
  --shadow-lift: 0 10px 24px rgba(59, 58, 54, .28);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-weight: 700;
  color: var(--ink);
  background-color: var(--sage);
  /* Alli's board tiles seamlessly; --table-tile sets how big each repeat is (smaller = more tiles) */
  --table-tile: 600px;
  background-image: var(--art-tableBg);
  background-position: center top;
  background-size: var(--table-tile) var(--table-tile);
  background-repeat: repeat;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- HUD ---------- */
.hud {
  max-width: var(--table-max);
  margin: 0 auto;
  padding: 14px var(--gutter) 10px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 20px;
}

.brand {
  margin: 0;
  font-size: clamp(1.3rem, 3.4vw, 1.9rem);
  font-weight: 700;
  color: var(--cream);
  letter-spacing: .01em;
  text-shadow: 0 2px 0 var(--moss-deep);
  white-space: nowrap;
}
.brand span { color: var(--butter); }

.stats {
  margin: 0;
  display: flex;
  gap: 6px;
  background: var(--cream);
  padding: 5px 8px;
  border-radius: 999px;
  box-shadow: var(--shadow);
}
.stat { display: flex; align-items: baseline; gap: 6px; padding: 0 8px; }
.stat + .stat { border-left: 2px dotted var(--sage); }
.stat dt { font-size: .72rem; font-weight: 600; text-transform: uppercase; letter-spacing: .06em; color: var(--ink-soft); }
.stat dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; min-width: 2.2ch; }

.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-left: auto; }

.btn {
  font: 700 1rem/1 var(--font);
  color: var(--ink);
  background: var(--cream);
  border: 0;
  border-radius: 999px;
  padding: 9px 16px;
  box-shadow: 0 3px 0 var(--moss-deep);
  cursor: pointer;
  text-decoration: none;
  transition: transform .12s ease, box-shadow .12s ease, background .12s ease;
}
.btn:hover { background: #fff; }
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--moss-deep); }
.btn:focus-visible { outline: 3px solid var(--butter); outline-offset: 2px; }
.btn:disabled { opacity: .55; cursor: default; transform: none; }
.btn-primary { background: var(--maroon); color: var(--cream); box-shadow: 0 3px 0 #6A2E37; }
.btn-primary:hover { background: #AF4C5B; }
.btn-primary:active { box-shadow: 0 1px 0 #6A2E37; }
.btn-ghost { background: transparent; color: var(--cream); box-shadow: inset 0 0 0 2px var(--cream); }
.btn-ghost:hover { background: rgba(251, 246, 234, .18); }
.btn-ghost:active { box-shadow: inset 0 0 0 2px var(--cream); }

.btn-float {
  position: fixed;
  left: 50%;
  bottom: max(18px, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 2000;
  font-size: 1.05rem;
  padding: 12px 24px;
  animation: nudge 1.6s ease-in-out infinite;
}
.btn-float:active { transform: translateX(-50%) translateY(2px); }

/* ---------- table + piles ---------- */
/* Desktop: foundations down the left edge | the seven columns | stock, waste and undo down the right edge */
.table {
  position: relative;
  width: fit-content;
  max-width: 100%;
  margin: 0 auto;
  padding: 6px var(--gutter) 40px;
  display: grid;
  grid-template-columns: var(--card-w) auto var(--card-w);
  grid-template-areas: "left tab right";
  column-gap: var(--side-gap);
  align-items: start;
  user-select: none;
  touch-action: none;
}

.side {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--stack-gap);
}
.side-left { grid-area: left; }
.side-right { grid-area: right; }
.side-right .pile-waste { margin-bottom: calc(var(--card-h) * 0.4); }   /* room for the draw-3 fan */

.row-tableau {
  grid-area: tab;
  display: grid;
  grid-template-columns: repeat(7, var(--card-w));
  column-gap: var(--gap);
}

.btn-round {
  width: clamp(40px, calc(var(--card-w) * 0.42), 56px);
  height: clamp(40px, calc(var(--card-w) * 0.42), 56px);
  padding: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  font-size: clamp(1.2rem, calc(var(--card-w) * 0.2), 1.7rem);
}

/* Narrow screens: no room for side columns, so the piles go across the top
   (foundations left; undo, waste and stock right) with the seven columns underneath */
@media (max-width: 820px) {
  :root { --card-w: min(110px, calc((100vw - 2 * var(--gutter) - 6 * var(--gap)) / 7)); }
  .table {
    width: auto;
    grid-template-columns: auto auto;
    grid-template-areas: "left right" "tab tab";
    justify-content: space-between;
    row-gap: calc(var(--card-h) * 0.5);
  }
  .side { flex-direction: row; align-items: flex-start; gap: var(--gap); }
  .side-right { flex-direction: row-reverse; }
  .side-right .pile-waste { margin-bottom: 0; }
  .row-tableau { justify-content: space-between; }
}

.pile {
  position: relative;
  width: var(--card-w);
  height: var(--card-h);
  border-radius: var(--card-r);
}
.pile::before {                     /* empty-slot outline */
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  border: 2px dashed rgba(251, 246, 234, .75);
  background: rgba(78, 107, 58, .16);
}
.pile-stock { cursor: pointer; }
.pile-stock::after,
.pile-foundation::after {
  content: "";
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  font-weight: 700;
  color: rgba(251, 246, 234, .85);
  font-size: calc(var(--card-w) * 0.34);
}
.pile-stock::after { content: "↻"; }
.pile-foundation::after { content: "A"; }

.pile.drop-ok::before {
  border: 3px solid var(--butter);
  background: rgba(242, 199, 92, .28);
}

/* hint: the card to move, and where it should go */
.card.hint-from .card-inner,
.card.hint-to .card-inner {
  box-shadow: 0 0 0 3px var(--butter), var(--shadow);
  animation: hint-pulse 1.2s ease-in-out infinite;
}
.pile.hint-to::before {
  border: 3px solid var(--butter);
  background: rgba(242, 199, 92, .28);
  animation: hint-pulse 1.2s ease-in-out infinite;
}
.btn.shake { animation: wiggle .4s ease; }
@keyframes hint-pulse {
  50% { box-shadow: 0 0 0 7px rgba(242, 199, 92, .5), var(--shadow); }
}

/* ---------- cards ---------- */
.card {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--card-w);
  height: var(--card-h);
  perspective: 600px;
  cursor: grab;
  touch-action: none;
}
.card.is-down { cursor: default; }
.pile-stock .card { cursor: pointer; }
.card.dragging { cursor: grabbing; }

.card-inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform .32s cubic-bezier(.3, .7, .3, 1);
  border-radius: var(--card-r);
  box-shadow: var(--shadow);
}
.card.is-down .card-inner { transform: rotateY(180deg); }
/* stacked piles: only the top few cards cast a shadow, or 24 shadows pile into a dark halo */
.pile-stock .card:nth-last-child(n+4) .card-inner,
.pile-waste .card:nth-last-child(n+4) .card-inner,
.pile-foundation .card:nth-last-child(n+3) .card-inner { box-shadow: none; }
.card.dragging { will-change: transform; }   /* keep the dragged cards on their own layer */
.card.dragging .card-inner { box-shadow: var(--shadow-lift); }
.card.hint .card-inner { animation: wiggle .4s ease; }

.card-front,
.card-back {
  position: absolute;
  inset: 0;
  border-radius: var(--card-r);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
}

.card-back {
  transform: rotateY(180deg);
  background: var(--moss) var(--art-cardBack) center / cover no-repeat;
}

.card-front {
  background: var(--paper) var(--art-cardFace) center / cover no-repeat;
  border: 1px solid rgba(59, 58, 54, .12);
}
/* Red suits use Alli's maroon (the colour in her artwork); black suits stay dark,
   so the alternating red/black rule still reads at a glance. */
.card.red   { --suit-ink: var(--maroon); }
.card.black { --suit-ink: var(--suit-dark); }

.card.s-hearts   { --suit-art: var(--art-suitHearts); }
.card.s-diamonds { --suit-art: var(--art-suitDiamonds); }
.card.s-clubs    { --suit-art: var(--art-suitClubs); }
.card.s-spades   { --suit-art: var(--art-suitSpades); }

.corner {
  position: absolute;
  display: flex;
  /* The suit first, the rank to the right of it. That is the order Alli drew
     into the face cards -- there the symbol is part of the artwork and the
     game adds only the letter beside it -- so the number cards, which used
     to stack the rank above the suit, now read the same way. */
  flex-direction: row;
  align-items: center;
  gap: calc(var(--card-w) * 0.042);
  width: auto;
  line-height: 1;
  color: var(--suit-ink);
}
/* Measured off Alli's face-card artwork, where the suit is drawn into the
   picture: her symbol starts about 2.2% of a card width in and 2.6% down,
   stands about 21% wide by 25% tall, and the rank letter begins at 27.7% --
   which is exactly where .card.is-face puts its letter below. A number card
   draws its own symbol into that same space, so a seven of clubs and a king
   of clubs now read identically in the corner.
   The offsets below are a shade under those figures because .card-front
   carries a 1px border, and everything inside it starts after that. */
.corner.tl { left: calc(var(--card-w) * 0.018); top: calc(var(--card-w) * 0.018); }
.corner.br { right: calc(var(--card-w) * 0.018); bottom: calc(var(--card-w) * 0.018); transform: rotate(180deg); }
.corner .rank {
  font-variant-numeric: lining-nums;
  font-weight: 700;
  font-size: max(12px, calc(var(--card-w) * 0.2));
  letter-spacing: -.04em;
}
/* Suit art is used as a MASK: any one-colour silhouette works, and the game paints it
   red or black (--suit-ink), so one file per suit covers every use. */
.suit-icon,
.pip,
.ace-pip {
  display: block;
  background-color: var(--suit-ink);
  -webkit-mask: var(--suit-art) center / contain no-repeat;
  mask: var(--suit-art) center / contain no-repeat;
}
.corner .suit-icon {
  width: max(10px, calc(var(--card-w) * 0.21));
  height: max(12px, calc(var(--card-w) * 0.25));
}

/* The pip field. It starts below the corner mark rather than beside it:
   the corner runs about 27.5% of a card width down and, on a ten, nearly
   45% across, so a pip field beginning at 13% put its top-left pip straight
   through the index. 21% top and bottom clears it with room to spare. */
.pips {
  position: absolute;
  left: 24%;
  right: 24%;
  top: 21%;
  bottom: 21%;
}
.pip {
  position: absolute;
  width: var(--pip-size);
  height: var(--pip-size);
  transform: translate(-50%, -50%);
}
/* One pip size for every rank. The middle column sits 16.6% of a card width
   from the outer ones, so a pip narrower than that cannot reach its
   diagonal neighbour whatever the row spacing -- which is what used to
   collide on the nines and tens. Same size everywhere also means a seven
   and a ten are drawn from the same stamp. */
.card { --pip-size: calc(var(--card-w) * 0.155); }
.pip.flip { transform: translate(-50%, -50%) rotate(180deg); }

.ace-pip {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 46%;
  height: 46%;
  transform: translate(-50%, -50%);
}

/* Face cards are Alli's complete artwork, suit symbols included: the game only sets the
   image (--face-art, per card) and puts a small rank letter beside her corner symbol. */
.card.is-face .card-front { background-image: var(--face-art); }
.card.is-face .corner { width: auto; }
.card.is-face .corner.tl { left: calc(var(--card-w) * 0.27); top: calc(var(--card-w) * 0.045); }
.card.is-face .corner.br { right: calc(var(--card-w) * 0.27); bottom: calc(var(--card-w) * 0.045); }
.card.is-face .corner .rank {
  font-size: max(12px, calc(var(--card-w) * 0.2));
}

/* Very small cards (phones): drop the pip grid for one big suit symbol */
@media (max-width: 520px) {
  .pips .pip { display: none; }
  .pips::after {
    content: "";
    position: absolute;
    left: 50%;
    top: 50%;
    /* Off the card, not off the pip box: as a share of that box this came out
       17px on a 49px card, a third of the face, with blank card all round it.
       These are the ace's proportions -- 46% of a card's width by 46% of its
       height -- and on a phone this symbol is the whole of what you read. */
    width: calc(var(--card-w) * 0.46);
    height: calc(var(--card-w) * 0.644);
    transform: translate(-50%, -50%);
    background-color: var(--suit-ink);
    -webkit-mask: var(--suit-art) center / contain no-repeat;
    mask: var(--suit-art) center / contain no-repeat;
    opacity: .9;
  }
  .corner.br { display: none; }
}

/* Narrow screens: undo and hint leave the top row.
   From 820px down, the piles move up top -- four foundations on the left,
   then stock, waste, undo and hint on the right. Six piles and two round
   buttons is more than a phone is wide: on a 375px screen it came to about
   410px, and the stock ran clean off the right edge. The two buttons go to
   the bottom right instead, fixed, where a thumb reaches them and they are
   clear of the auto-finish button in the middle. Undo sits lowest because it
   is the one you reach for most. */
@media (max-width: 700px) {
  .side-right #btn-undo,
  .side-right #btn-hint {
    position: fixed;
    right: max(12px, env(safe-area-inset-right));
    z-index: 1900;
  }
  .side-right #btn-undo { bottom: max(16px, env(safe-area-inset-bottom)); }
  .side-right #btn-hint { bottom: calc(max(16px, env(safe-area-inset-bottom)) + 52px); }
}

/* Phones: every spare pixel into the cards.
   Seven columns have to fit across, so the only width the cards can gain is
   whatever the margins give up -- hence the small gutter and gaps. The rank
   and suit in the corner keep a floor in real pixels, because a percentage
   of a card this narrow comes out too small to read at arm's length. */
@media (max-width: 560px) {
  :root {
    --gutter: 6px;
    --gap: 3px;
    /* A phone is tall and narrow: seven columns have to fit across, which caps
       how big a card can be, so the board used to sit in the top third with
       half a screen of empty table under it. Fanning the columns further down
       spends that height instead. ui.js still squeezes a column that outgrows
       the screen. */
    --fan-down: 0.20;
    --fan-up: 0.36;
  }
  .corner .rank,
  .card.is-face .corner .rank { font-size: max(14px, calc(var(--card-w) * 0.26)); }
}

/* ---------- win screen ---------- */
.win {
  position: fixed;
  inset: 0;
  z-index: 3000;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(59, 58, 54, .38);
  animation: fade-in .4s ease both;
}
.win-card {
  position: relative;
  z-index: 1;
  width: min(360px, 100%);
  text-align: center;
  background: var(--cream);
  border-radius: 28px;
  padding: 18px 24px 26px;
  box-shadow: 0 18px 50px rgba(59, 58, 54, .35), inset 0 0 0 4px var(--blush);
  animation: pop-in .55s cubic-bezier(.2, 1.4, .4, 1) both .1s;
}
.win-critter {
  width: min(320px, 92%);
  aspect-ratio: 1806 / 1146;
  margin: -78px auto 4px;
  background: var(--art-winFlowers) center / contain no-repeat;
  animation: hop 1.1s ease-in-out infinite;
}
.win-card h2 { margin: 6px 0 8px; font-size: 1.6rem; font-weight: 700; }
.win-stats { margin: 0 0 18px; color: var(--ink-soft); line-height: 1.6; }
.win-stats strong { color: var(--ink); }

.petals { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
.petal {
  position: absolute;
  top: -30px;
  background: center / contain no-repeat;
  animation: fall linear forwards;
}
/* the five pieces Alli cut out of the bouquet; aspect-ratio keeps each one in shape */
.petal.f1 { background-image: var(--art-winFlowerBig);    aspect-ratio: 807 / 812; }
.petal.f2 { background-image: var(--art-winFlowerOrange); aspect-ratio: 414 / 414; }
.petal.f3 { background-image: var(--art-winFlowerPeach);  aspect-ratio: 451 / 451; }
.petal.f4 { background-image: var(--art-winLeafCurl);     aspect-ratio: 492 / 852; }
.petal.f5 { background-image: var(--art-winLeafLong);     aspect-ratio: 1068 / 358; }

/* ---------- motion ---------- */
@keyframes fade-in { from { opacity: 0; } }
@keyframes pop-in { from { opacity: 0; transform: scale(.7) translateY(20px); } }
@keyframes hop {
  0%, 100% { transform: translateY(0) rotate(-3deg); }
  50% { transform: translateY(-14px) rotate(3deg); }
}
@keyframes fall {
  to { transform: translate(var(--drift, 0), 110vh) rotate(var(--spin, 540deg)); }
}
@keyframes wiggle {
  25% { transform: rotate(-4deg); }
  75% { transform: rotate(4deg); }
}
@keyframes nudge {
  0%, 100% { translate: 0 0; }
  50% { translate: 0 -4px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition-duration: .01ms !important; }
}

@media (max-width: 700px) {
  .actions { margin-left: 0; width: 100%; }
  .actions .btn { padding: 8px 12px; font-size: .85rem; }
  .stats { order: 3; }
}

/* ---------- leaderboard (saved in this browser) ---------- */
.board-screen {
  position: fixed;
  inset: 0;
  z-index: 3100;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(59, 58, 54, .38);
  animation: fade-in .3s ease both;
}
.board-card {
  width: min(420px, 100%);
  max-height: 86vh;
  overflow: auto;
  text-align: center;
  background: var(--cream);
  border-radius: 24px;
  padding: 20px 22px 24px;
  box-shadow: 0 18px 50px rgba(59, 58, 54, .35), inset 0 0 0 4px var(--blush);
  animation: pop-in .45s cubic-bezier(.2, 1.4, .4, 1) both;
}
.board-card h2 { margin: 0 0 12px; font-size: 1.5rem; }

/* The "start over?" question. It borrows the leaderboard's card so the game
   asks in its own voice: a native confirm() can be switched off by the browser,
   and when it is the button just looks broken. */
.ask-screen { z-index: 3200; }
.ask-card { width: min(340px, 100%); }
.ask-text { margin: 0 0 18px; color: var(--ink-soft); line-height: 1.6; }
.ask-row { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; }
.board-tabs { display: flex; gap: 8px; justify-content: center; margin-bottom: 14px; }
.board-tabs .btn[aria-pressed="true"] { background: var(--maroon); color: var(--cream); box-shadow: 0 3px 0 #6A2E37; }

.board { list-style: none; margin: 0 0 16px; padding: 0; text-align: left; }
.board li {
  display: grid;
  grid-template-columns: 1.7rem 1fr auto;
  gap: 10px;
  align-items: baseline;
  padding: 7px 12px;
  margin-bottom: 6px;
  border-radius: 12px;
  background: var(--paper);
}
.board .rank-no { color: var(--ink-soft); font-variant-numeric: lining-nums; }
.board .who { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.board .pts { text-align: right; font-variant-numeric: lining-nums tabular-nums; }
.board small { display: block; font-size: .76rem; font-weight: 500; color: var(--ink-soft); }
.board li.me { background: #F6E7EA; box-shadow: inset 0 0 0 2px var(--maroon); }
.board li.empty { display: block; text-align: center; color: var(--ink-soft); background: none; }

.win-save { display: flex; gap: 8px; justify-content: center; margin: 0 0 14px; }
.win-save input {
  flex: 1 1 auto;
  min-width: 0;
  font: 700 1rem/1 var(--font);
  color: var(--ink);
  background: var(--paper);
  border: 2px solid var(--sage);
  border-radius: 999px;
  padding: 9px 14px;
}
.win-save input:focus-visible { outline: 3px solid var(--butter); outline-offset: 1px; }
#win-board { max-height: 32vh; overflow: auto; }
