/* The Study: an ink-green card table, warm ivory cards, brass accents.
   Numerals are set in a bookish serif, like a mathematics text. */

:root {
  --pad: 8px;
  --gap: 4px;
  --card-w: min(56px, calc((100vw - 2 * var(--pad) - 6 * var(--gap)) / 7));
  --card-h: calc(var(--card-w) * 1.5);
  --fan-down: 14px;
  --fan-up: 26px;

  --serif: "Iowan Old Style", "Palatino Linotype", "Book Antiqua", Palatino, Georgia, serif;
  --sans: system-ui, "Segoe UI", "Helvetica Neue", sans-serif;

  --felt: #4e6b5c;
  --felt-deep: #42594d;
  --on-felt: #f2efe4;
  /* Small text on the felt needs this to clear WCAG AA. */
  --on-felt-dim: #e2e6dd;
  --card: #f7f2e4;
  --card-edge: #cfc2a6;
  --ink: #26241d;
  --ink-faint: #6e6852;
  --brass: #7a5f22;
  --brass-bright: #dcbd72;
  --brass-text: #f5f0e2;
  --red: #a83a32;
  --card-back: #2c4a6e;
  --complete: #a5c8e2;
}

/* The light palette is the default; the dark one is selected by the data
   theme attribute, which the UI sets from the stored choice or the system
   setting. */
:root[data-theme="dark"] {
  --felt: #18231e;
  --felt-deep: #101a14;
  --on-felt: #e9e2cf;
  --on-felt-dim: #98a08f;
  --card: #34312a;
  --card-edge: #4d4839;
  --ink: #e9e2cf;
  --ink-faint: #a89f85;
  --brass: #c9a557;
  --brass-bright: #c9a557;
  --brass-text: #26221a;
  --red: #d98a7a;
  --card-back: #27405f;
  --complete: #6b8ea8;
}

@media (min-width: 700px) {
  :root {
    --card-w: 72px;
  }
}

@media (min-width: 1000px) {
  :root {
    --card-w: 88px;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  font-family: var(--sans);
  color: var(--on-felt);
  background:
    radial-gradient(130% 110% at 50% 0%, var(--felt) 40%, var(--felt-deep) 100%);
}

:focus-visible {
  outline: 2px solid var(--brass-bright);
  outline-offset: 2px;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 4px 12px;
  padding: calc(var(--pad) * 1.5) var(--pad);
}

.header h1 {
  margin: 0;
  font-family: var(--serif);
  font-size: 1.3rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}

.version {
  font-size: 0.6em;
  font-weight: 400;
  color: var(--on-felt-dim);
}

.controls {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

#mistakes {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 999px;
  background: var(--card);
  color: var(--ink);
  font-size: 0.85rem;
}

#mistakes.has-mistakes {
  color: var(--red);
}

@keyframes bump {
  0% { transform: scale(1); }
  35% { transform: scale(1.15); }
  100% { transform: scale(1); }
}

#mistakes.bump {
  animation: bump 300ms ease-out;
}

.controls button {
  padding: 6px 14px;
  border: 1px solid color-mix(in srgb, var(--brass-bright) 40%, transparent);
  border-radius: 999px;
  background: color-mix(in srgb, var(--card) 10%, transparent);
  color: var(--on-felt);
  font: inherit;
  font-size: 0.9rem;
  touch-action: manipulation;
}

.controls button:active {
  opacity: 0.7;
}

#difficulty {
  padding: 6px 10px;
  border: 1px solid color-mix(in srgb, var(--brass-bright) 40%, transparent);
  border-radius: 999px;
  background: color-mix(in srgb, var(--card) 10%, transparent);
  color: var(--on-felt);
  font: inherit;
  font-size: 0.9rem;
  touch-action: manipulation;
}

#sound[aria-pressed="false"] {
  opacity: 0.45;
}

#board {
  flex: 1;
  padding: 0 var(--pad) var(--pad);
  user-select: none;
  -webkit-user-select: none;
  touch-action: pan-y;
}

.top,
.tableau {
  display: grid;
  grid-template-columns: repeat(7, var(--card-w));
  gap: var(--gap);
  justify-content: center;
}

.top {
  padding-top: var(--pad);
}

.top .cell:nth-child(1) { grid-area: 1 / 1; }
.top .cell:nth-child(2) { grid-area: 1 / 2; }
.top .cell:nth-child(3) { grid-area: 1 / 3; }
.top .cell:nth-child(4) { grid-area: 1 / 4; }
.top .cell:nth-child(5) { grid-area: 1 / 5; }
.top .cell:nth-child(6) { grid-area: 1 / 6; }
.top .cell:nth-child(7) { grid-area: 2 / 1; }
.top .cell:nth-child(8) { grid-area: 2 / 2; }
.top .cell:nth-child(9) { grid-area: 2 / 3; }
.top .cell:nth-child(10) { grid-area: 2 / 4; }

.tableau {
  margin-top: var(--gap);
}

.cell {
  position: relative;
  width: var(--card-w);
  height: var(--card-h);
}

/* Stock and waste piles occupy one cell; the badge sits in the corner. */
.pile {
  display: flex;
}

/* A dashed frame marks an empty spot on the table. */
.slot {
  width: 100%;
  height: 100%;
  border: 1px dashed color-mix(in srgb, var(--brass-bright) 35%, transparent);
  border-radius: calc(var(--card-w) * 0.12);
}

.badge {
  position: absolute;
  top: 3px;
  right: 3px;
  z-index: 2;
  padding: 0 5px;
  border-radius: 999px;
  background: var(--brass);
  color: var(--brass-text);
  font-size: 0.65rem;
  font-weight: 600;
  line-height: 1.5;
  box-shadow: 0 1px 2px rgb(0 0 0 / 0.3);
}

/* The icon overlays the cell (positioned over the dashed frame) instead of
   sitting next to it, so the two do not share the flex row. */
.recycle {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 58%;
  height: 58%;
  border: 1px dashed color-mix(in srgb, var(--brass-bright) 55%, transparent);
  border-radius: 50%;
  font-size: calc(var(--card-w) * 0.42);
  color: var(--brass-bright);
}

.card {
  width: var(--card-w);
  height: var(--card-h);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: 1px solid var(--card-edge);
  border-radius: calc(var(--card-w) * 0.12);
  background: var(--card);
  color: var(--ink);
  font-family: var(--serif);
  box-shadow:
    0 1px 2px rgb(0 0 0 / 0.35),
    0 3px 10px rgb(0 0 0 / 0.22);
  touch-action: none;
}

.card.face-down {
  /* A bright edge keeps each card in a fanned stack readable. */
  border-color: #f5f1e6;
  background:
    repeating-linear-gradient(
      45deg,
      transparent 0 4px,
      color-mix(in srgb, var(--brass-bright) 22%, transparent) 4px 5px
    ),
    repeating-linear-gradient(
      -45deg,
      transparent 0 4px,
      color-mix(in srgb, var(--brass-bright) 22%, transparent) 4px 5px
    ),
    var(--card-back);
  box-shadow:
    inset 0 0 0 3px var(--card-back),
    inset 0 0 0 4px color-mix(in srgb, var(--brass-bright) 45%, transparent),
    0 1px 2px rgb(0 0 0 / 0.35),
    0 3px 10px rgb(0 0 0 / 0.22);
}

.expr {
  font-size: calc(var(--card-w) * 0.25);
  line-height: 1.22;
  text-align: center;
  max-width: 100%;
  font-variant-numeric: lining-nums tabular-nums;
}

.initial-value {
  font-size: calc(var(--card-w) * 0.3);
  font-weight: 700;
}

.initial-count {
  font-family: var(--sans);
  font-size: calc(var(--card-w) * 0.16);
  color: var(--ink-faint);
}

.foundation .foundation-card {
  width: 100%;
  height: 100%;
  border: 1px solid color-mix(in srgb, var(--brass-bright) 55%, transparent);
  box-shadow: inset 0 1px 0 rgb(255 255 255 / 0.06);
}

.foundation.empty .foundation-card {
  border: 1px dashed color-mix(in srgb, var(--brass-bright) 32%, transparent);
  color: var(--on-felt-dim);
}

/* A complete foundation stands out on its light-blue field; the value and
   the count stay legible on it in both themes. */
.foundation-card.complete {
  background: var(--complete);
  color: var(--ink);
}

.foundation-card.complete .foundation-count {
  color: color-mix(in srgb, var(--ink) 70%, transparent);
}

.foundation-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border-radius: calc(var(--card-w) * 0.12);
  background: color-mix(in srgb, var(--felt-deep) 55%, transparent);
  color: var(--on-felt);
  font-family: var(--serif);
  touch-action: none;
}

.foundation .initial-value {
  font-weight: 600;
}

.foundation-count {
  font-family: var(--sans);
  font-size: calc(var(--card-w) * 0.16);
  color: color-mix(in srgb, var(--on-felt) 85%, transparent);
}

.column {
  display: flex;
  flex-direction: column;
}

.column .card + .card {
  margin-top: calc(var(--fan-down) - var(--card-h));
}

.column .card.face-up + .card.face-up {
  margin-top: calc(var(--fan-up) - var(--card-h));
}

.placeholder {
  width: var(--card-w);
  height: var(--card-h);
  border: 1px dashed color-mix(in srgb, var(--brass-bright) 32%, transparent);
  border-radius: calc(var(--card-w) * 0.12);
}

.card.selected {
  outline: 2px solid var(--brass-bright);
  outline-offset: -1px;
  box-shadow:
    0 0 0 3px color-mix(in srgb, var(--brass-bright) 30%, transparent),
    0 1px 2px rgb(0 0 0 / 0.35),
    0 3px 10px rgb(0 0 0 / 0.22);
}

/* The drag ghost: a fixed-position clone of the movable unit (the top card
   of the waste, or the whole face-up stack of a column), fanned like a
   column. The cloned cards keep their own shadows. */
.ghost {
  position: fixed;
  z-index: 10;
  opacity: 0.88;
  pointer-events: none;
  transition: none;
}

#message {
  min-height: 1.4em;
  margin: 0;
  padding: 4px var(--pad);
  text-align: center;
  font-family: var(--serif);
  font-style: italic;
  font-size: 0.9rem;
  color: var(--on-felt-dim);
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgb(0 0 0 / 0.5);
}

.overlay[hidden] {
  display: none;
}

.overlay-card {
  padding: 28px 36px;
  border-radius: 14px;
  background: var(--card);
  color: var(--ink);
  text-align: center;
  box-shadow: 0 16px 48px rgb(0 0 0 / 0.45);
  animation: rise 260ms ease-out;
}

.overlay-card h2 {
  margin: 0 0 4px;
  font-family: var(--serif);
  font-size: 1.7rem;
  font-weight: 600;
}

.overlay-card p {
  margin: 0;
  color: var(--ink-faint);
}

.overlay-card button {
  margin-top: 14px;
  padding: 8px 18px;
  border: none;
  border-radius: 999px;
  background: var(--brass);
  color: var(--brass-text);
  font: inherit;
  font-size: 0.95rem;
}

.overlay-card button:active {
  opacity: 0.8;
}

/* One-shot visual effects, driven by classes the UI adds at render time. */
@keyframes deal-in {
  from {
    opacity: 0;
    transform: translateY(10px);
  }
}

@keyframes flip-in {
  from {
    opacity: 0.3;
    transform: perspective(400px) rotateY(75deg);
  }
}

/* A foundation that just appeared - its initial card moved onto an empty
   slot - is revealed more slowly and deliberately than an ordinary card
   flip, so the moment is easier to see. */
@keyframes found-in {
  from {
    opacity: 0.3;
    transform: perspective(400px) rotateY(75deg) scale(1.05);
  }
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.97);
  }
}

.dealing .card,
.dealing .foundation-card,
.dealing .slot {
  animation: deal-in 220ms ease-out backwards;
  animation-delay: calc(var(--i, 0) * 16ms);
}

.flipping {
  animation: flip-in 420ms cubic-bezier(0.25, 0.7, 0.3, 1);
}

.founding {
  animation: found-in 1100ms cubic-bezier(0.25, 0.7, 0.3, 1);
}

@media (prefers-reduced-motion: reduce) {
  * {
    /* Delays must go too: deal-in uses a backwards fill, which holds cards
       invisible until their stagger delay has elapsed. */
    animation-duration: 0.01ms !important;
    animation-delay: 0s !important;
    transition-duration: 0.01ms !important;
  }
}
