:root {
  --page-pad: 12px;
  --gap: clamp(5px, 1.2vw, 14px);
  --card-w: min(calc((100vw - 2 * var(--page-pad) - 6 * var(--gap)) / 7), 110px);
  --card-h: calc(var(--card-w) * 1.45);
  --radius: calc(var(--card-w) * 0.09);
  --felt-light: #2e8b57;
  --felt-dark: #0d4a28;
  --ink: #f2f6f0;
  --red: #c7222c;
  --black: #1d1d22;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
}

body {
  min-height: 100vh;
  background: radial-gradient(ellipse at 50% -10%, var(--felt-light), var(--felt-dark) 75%) fixed;
  color: var(--ink);
  font-family: "Segoe UI", system-ui, -apple-system, Helvetica, Arial, sans-serif;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 18px;
  padding: 8px var(--page-pad);
  background: rgba(0, 0, 0, 0.35);
  box-shadow: 0 1px 6px rgba(0, 0, 0, 0.35);
  user-select: none;
}

.topbar h1 {
  margin: 0;
  font-size: 1.15rem;
  letter-spacing: 0.04em;
}

.arcade-link {
  color: #cfd6e4;
  opacity: 0.65;
  text-decoration: none;
  font-size: 1.1rem;
  line-height: 1;
  padding: 2px 4px;
}

.arcade-link:hover {
  opacity: 1;
}

.suit-red { color: #ff6b72; }
.suit-black { color: #cfd6e4; }

.hud {
  display: flex;
  gap: 14px;
  font-size: 0.85rem;
  opacity: 0.95;
}

.hud-item b {
  font-variant-numeric: tabular-nums;
}

.controls {
  margin-left: auto;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
}

.controls button,
.draw-select select {
  font: inherit;
  font-size: 0.82rem;
  padding: 5px 10px;
  border: 1px solid rgba(255, 255, 255, 0.35);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.12);
  color: var(--ink);
  cursor: pointer;
}

.controls button:hover,
.draw-select select:hover {
  background: rgba(255, 255, 255, 0.25);
}

.controls button:active {
  transform: translateY(1px);
}

.controls button:disabled {
  opacity: 0.4;
  cursor: default;
}

.draw-select {
  font-size: 0.82rem;
  display: flex;
  align-items: center;
  gap: 5px;
}

.draw-select select option {
  color: #000;
}

.attention {
  animation: attention 1.2s ease-in-out infinite;
  background: rgba(255, 214, 90, 0.35) !important;
  border-color: rgba(255, 214, 90, 0.8) !important;
}

@keyframes attention {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 214, 90, 0.7); }
  50% { box-shadow: 0 0 0 6px rgba(255, 214, 90, 0); }
}

/* ---------- board ---------- */

#board {
  width: calc(7 * var(--card-w) + 6 * var(--gap));
  margin: 0 auto;
  padding: calc(var(--gap) * 1.5) 0 40vh;
  user-select: none;
}

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

.top-row {
  margin-bottom: calc(var(--gap) * 1.6);
}

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

.pile.tableau {
  min-height: var(--card-h);
  height: auto;
}

.pile::before {
  content: "";
  position: absolute;
  inset: 0;
  height: var(--card-h);
  border: 2px dashed rgba(255, 255, 255, 0.28);
  border-radius: var(--radius);
}

.pile .placeholder-glyph,
.pile::after {
  pointer-events: none;
}

.foundation::after {
  content: "A";
  position: absolute;
  inset: 0;
  height: var(--card-h);
  display: grid;
  place-items: center;
  font-size: calc(var(--card-w) * 0.42);
  font-weight: 700;
  color: rgba(255, 255, 255, 0.22);
}

.stock {
  cursor: pointer;
}

.stock.can-recycle::after {
  content: "\21BB";
  position: absolute;
  inset: 0;
  height: var(--card-h);
  display: grid;
  place-items: center;
  font-size: calc(var(--card-w) * 0.5);
  color: rgba(255, 255, 255, 0.45);
}

.pile.drop-ok::before {
  border-color: rgba(255, 230, 120, 0.95);
  border-style: solid;
  box-shadow: 0 0 12px rgba(255, 230, 120, 0.45);
}

.pile.hint::before {
  border-color: rgba(120, 200, 255, 0.95);
  border-style: solid;
  animation: hint-pulse 0.8s ease-in-out infinite;
}

/* ---------- cards ---------- */

.card {
  position: absolute;
  width: var(--card-w);
  height: var(--card-h);
  border-radius: var(--radius);
  background: #fdfdf6;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
  font-size: calc(var(--card-w) / 7);
  touch-action: none;
  will-change: transform;
}

.card.moving {
  z-index: 100;
}

.card.dragging {
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.5);
  cursor: grabbing;
}

.card.hint {
  animation: hint-pulse 0.8s ease-in-out infinite;
}

@keyframes hint-pulse {
  0%, 100% { box-shadow: 0 0 0 2px rgba(120, 200, 255, 0.95), 0 1px 2px rgba(0, 0, 0, 0.45); }
  50% { box-shadow: 0 0 14px 5px rgba(120, 200, 255, 0.75), 0 1px 2px rgba(0, 0, 0, 0.45); }
}

.card-face,
.card-back {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  overflow: hidden;
}

.card.face-down .card-face { display: none; }
.card:not(.face-down) .card-back { display: none; }

.card:not(.face-down) {
  cursor: grab;
}

.card-face {
  background: #fdfdf6;
  border: 1px solid #c9c9bd;
}

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

.card-back {
  background:
    repeating-linear-gradient(45deg, rgba(255, 255, 255, 0.14) 0 0.45em, transparent 0.45em 0.9em),
    repeating-linear-gradient(-45deg, rgba(255, 255, 255, 0.14) 0 0.45em, transparent 0.45em 0.9em),
    linear-gradient(160deg, #3563b8, #1d3c7a);
  border: 0.32em solid #f4f4ec;
  box-shadow: inset 0 0 0 1px #b8b8ac;
}

.corner {
  position: absolute;
  display: flex;
  flex-direction: column;
  align-items: center;
  line-height: 1;
  font-weight: 700;
  width: 1.5em;
}

.corner .rank { font-size: 1.05em; }
.corner .suit { font-size: 0.95em; margin-top: 0.08em; }

.corner.tl { top: 0.3em; left: 0.12em; }
.corner.br { bottom: 0.3em; right: 0.12em; transform: rotate(180deg); }

.pips {
  position: absolute;
  inset: 9% 24%;
}

.pip {
  position: absolute;
  transform: translate(-50%, -50%);
  font-size: 1.6em;
  line-height: 1;
}

.pip.flip {
  transform: translate(-50%, -50%) rotate(180deg);
}

.pip.big {
  font-size: 3.4em;
}

.face-frame {
  position: absolute;
  inset: 14% 22%;
  border: 0.14em solid currentColor;
  border-radius: 0.4em;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.15em;
  background:
    linear-gradient(135deg, rgba(212, 175, 55, 0.18), transparent 55%),
    linear-gradient(-45deg, rgba(212, 175, 55, 0.18), transparent 55%);
}

.face-frame .face-letter {
  font-size: 2.3em;
  font-weight: 700;
  font-family: Georgia, "Times New Roman", serif;
  line-height: 1;
}

.face-frame .face-suit {
  font-size: 1.3em;
  line-height: 1;
}

/* ---------- layers ---------- */

#drag-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 300;
}

#cascade-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 500;
  overflow: hidden;
}

#drag-layer .card,
#cascade-layer .card {
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.5);
}

/* ---------- message toast ---------- */

.message {
  position: fixed;
  left: 50%;
  bottom: 26px;
  transform: translateX(-50%);
  background: rgba(0, 0, 0, 0.75);
  color: #fff;
  padding: 9px 18px;
  border-radius: 999px;
  font-size: 0.9rem;
  z-index: 400;
  pointer-events: none;
  white-space: nowrap;
}

/* ---------- win overlay ---------- */

.overlay[hidden],
.message[hidden] {
  display: none;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: grid;
  place-items: center;
  z-index: 450;
}

.dialog {
  background: #fffef8;
  color: #222;
  border-radius: 14px;
  padding: 28px 40px;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  max-width: min(90vw, 420px);
}

.dialog h2 {
  margin: 0 0 10px;
  font-size: 1.7rem;
}

.dialog p {
  margin: 6px 0;
}

.dialog .muted {
  color: #777;
  font-size: 0.88rem;
}

.dialog button {
  margin-top: 16px;
  font: inherit;
  font-size: 1rem;
  padding: 10px 26px;
  border: none;
  border-radius: 8px;
  background: #1d6f42;
  color: #fff;
  cursor: pointer;
}

.dialog button:hover {
  background: #27874f;
}

/* ---------- small screens ---------- */

@media (max-width: 560px) {
  .topbar h1 { display: none; }
  .hud { gap: 9px; font-size: 0.78rem; }
  .controls { margin-left: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .attention, .pile.hint::before, .card.hint {
    animation: none;
  }
}
