:root {
  color-scheme: dark;
  --bg: #0a0f10;
  --bg-glow: #142426;
  --panel: #141d1f;
  --panel-raised: #1d2a2c;
  --felt: #15706a;
  --felt-deep: #093f3c;
  --felt-light: #1f8a82;
  --felt-rim: rgba(255,255,255,.06);
  --line: rgba(235,250,246,.10);
  --line-strong: rgba(235,250,246,.20);
  --text: #f2f7f5;
  --muted: #8fa3a0;
  --soft: rgba(255,255,255,.62);
  --accent: #e8b96d;
  --accent-light: #f7d596;
  --accent-dark: #9a6e30;
  --accent-glow: rgba(232,185,109,.22);
  --red: #ed747b;
  --green: #7fd1c0;
  --shadow: 0 28px 80px rgba(0,0,0,.42), 0 8px 24px rgba(0,0,0,.18);
  --card-shadow: 0 6px 14px rgba(0,0,0,.32), 0 2px 4px rgba(0,0,0,.20);
  --card-shadow-hover: 0 14px 28px rgba(0,0,0,.38), 0 4px 8px rgba(0,0,0,.22);
  --radius: 10px;
  --radius-lg: 26px;
}

@media (prefers-color-scheme: light) {
  :root {
    color-scheme: light;
    --bg: #e8efed;
    --bg-glow: #d4e6e1;
    --panel: #ffffff;
    --panel-raised: #f2f7f5;
    --felt: #2c8278;
    --felt-deep: #195955;
    --felt-light: #3a958a;
    --felt-rim: rgba(255,255,255,.45);
    --line: rgba(13,42,39,.10);
    --line-strong: rgba(13,42,39,.18);
    --text: #14201e;
    --muted: #5a6e6b;
    --soft: rgba(255,255,255,.72);
    --accent: #c9954a;
    --accent-light: #e8b96d;
    --accent-dark: #8a6420;
    --accent-glow: rgba(201,149,74,.18);
    --shadow: 0 24px 64px rgba(26,72,66,.14), 0 6px 18px rgba(26,72,66,.08);
    --card-shadow: 0 6px 14px rgba(17,57,51,.16), 0 2px 4px rgba(17,57,51,.10);
    --card-shadow-hover: 0 14px 28px rgba(17,57,51,.20), 0 4px 8px rgba(17,57,51,.12);
  }
}

html[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0a0f10;
  --bg-glow: #142426;
  --panel: #141d1f;
  --panel-raised: #1d2a2c;
  --felt: #15706a;
  --felt-deep: #093f3c;
  --felt-light: #1f8a82;
  --felt-rim: rgba(255,255,255,.06);
  --line: rgba(235,250,246,.10);
  --line-strong: rgba(235,250,246,.20);
  --text: #f2f7f5;
  --muted: #8fa3a0;
  --soft: rgba(255,255,255,.62);
  --accent: #e8b96d;
  --accent-light: #f7d596;
  --accent-dark: #9a6e30;
  --accent-glow: rgba(232,185,109,.22);
  --red: #ed747b;
  --green: #7fd1c0;
  --shadow: 0 28px 80px rgba(0,0,0,.42), 0 8px 24px rgba(0,0,0,.18);
  --card-shadow: 0 6px 14px rgba(0,0,0,.32), 0 2px 4px rgba(0,0,0,.20);
  --card-shadow-hover: 0 14px 28px rgba(0,0,0,.38), 0 4px 8px rgba(0,0,0,.22);
}

html[data-theme="light"] {
  color-scheme: light;
  --bg: #e8efed;
  --bg-glow: #d4e6e1;
  --panel: #ffffff;
  --panel-raised: #f2f7f5;
  --felt: #2c8278;
  --felt-deep: #195955;
  --felt-light: #3a958a;
  --felt-rim: rgba(255,255,255,.45);
  --line: rgba(13,42,39,.10);
  --line-strong: rgba(13,42,39,.18);
  --text: #14201e;
  --muted: #5a6e6b;
  --soft: rgba(255,255,255,.72);
  --accent: #c9954a;
  --accent-light: #e8b96d;
  --accent-dark: #8a6420;
  --accent-glow: rgba(201,149,74,.18);
  --shadow: 0 24px 64px rgba(26,72,66,.14), 0 6px 18px rgba(26,72,66,.08);
  --card-shadow: 0 6px 14px rgba(17,57,51,.16), 0 2px 4px rgba(17,57,51,.10);
  --card-shadow-hover: 0 14px 28px rgba(17,57,51,.20), 0 4px 8px rgba(17,57,51,.12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  min-height: 100%;
  overflow: hidden;
}

body {
  background:
    radial-gradient(ellipse 60rem 40rem at 15% -8%, var(--bg-glow), transparent 60%),
    radial-gradient(ellipse 50rem 35rem at 85% 110%, var(--bg-glow), transparent 55%),
    var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-font-smoothing: antialiased;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

button:focus-visible,
.card:focus-visible {
  outline: 2.5px solid var(--accent);
  outline-offset: 3px;
}

.app-shell {
  min-height: 100dvh;
  height: 100dvh;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  gap: clamp(10px, 1.8vw, 22px);
  padding: clamp(12px, 2.2vw, 28px) clamp(14px, 3.5vw, 56px) calc(56px + env(safe-area-inset-bottom));
}

/* ─── Topbar ─── */

.topbar {
  display: flex;
  align-items: center;
  gap: clamp(14px, 2.5vw, 40px);
  min-width: 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 14px;
  min-width: 140px;
}

.brand-card {
  width: 36px;
  height: 50px;
  flex: none;
  border-radius: 6px;
  object-fit: cover;
  transform: rotate(-9deg);
  box-shadow: 0 6px 18px rgba(0,0,0,.32), 0 0 0 1.5px rgba(255,255,255,.12);
  transition: transform .4s cubic-bezier(.2,.9,.3,1.3);
}

.brand:hover .brand-card {
  transform: rotate(-4deg) scale(1.06);
}

.eyebrow {
  margin: 0 0 3px;
  color: var(--accent);
  font-size: 10px;
  font-weight: 800;
  letter-spacing: .22em;
}

h1, h2, p { margin: 0; }

h1 {
  font-size: clamp(22px, 2.8vw, 34px);
  line-height: .95;
  letter-spacing: -.055em;
  font-weight: 800;
}

.stats {
  display: flex;
  gap: clamp(16px, 2.5vw, 38px);
  margin-left: auto;
}

.stat {
  display: grid;
  gap: 3px;
  min-width: 40px;
}

.stat-label {
  color: var(--muted);
  font-size: 9px;
  font-weight: 800;
  letter-spacing: .16em;
}

.stat strong {
  font-size: clamp(16px, 1.8vw, 20px);
  line-height: 1;
  font-variant-numeric: tabular-nums;
  letter-spacing: -.03em;
  font-weight: 700;
}

.best-stat strong { color: var(--accent); }

/* ─── Buttons ─── */

.actions {
  display: flex;
  gap: 8px;
}

.primary-button,
.quiet-button,
.text-button {
  min-height: 44px;
  border-radius: 12px;
  padding: 0 18px;
  font-weight: 750;
  font-size: 13px;
  letter-spacing: .01em;
  transition: background .18s ease, transform .18s ease, box-shadow .18s ease, border-color .18s ease, opacity .18s ease;
}

.primary-button {
  border: 0;
  color: #1a1a14;
  background: linear-gradient(135deg, var(--accent-light), var(--accent));
  box-shadow: 0 4px 0 var(--accent-dark), 0 8px 20px var(--accent-glow);
}

.primary-button:hover {
  background: var(--accent-light);
  transform: translateY(-1px);
  box-shadow: 0 5px 0 var(--accent-dark), 0 12px 26px var(--accent-glow);
}

.primary-button:active {
  transform: translateY(3px);
  box-shadow: 0 1px 0 var(--accent-dark);
}

.primary-button:disabled {
  opacity: .4;
  cursor: not-allowed;
  transform: none;
  box-shadow: 0 4px 0 var(--accent-dark);
}

.quiet-button {
  border: 1px solid var(--line);
  background: var(--panel-raised);
  box-shadow: 0 4px 14px rgba(0,0,0,.06);
}

.quiet-button:hover {
  border-color: var(--line-strong);
  background: var(--panel);
  transform: translateY(-1px);
}

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

/* ─── Felt / Game Region ─── */

.game-region {
  display: flex;
  min-height: 0;
}

.felt {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  padding: clamp(14px, 2.2vw, 28px);
  border: 1px solid var(--felt-rim);
  border-radius: var(--radius-lg);
  background:
    radial-gradient(ellipse 90% 70% at 50% 8%, var(--felt-light) 0%, var(--felt) 45%, var(--felt-deep) 100%);
  box-shadow: var(--shadow), inset 0 1px 0 rgba(255,255,255,.12);
  transition: box-shadow .3s ease;
}

.felt.has-selection {
  box-shadow: var(--shadow), inset 0 0 0 1.5px var(--accent-glow), inset 0 1px 0 rgba(255,255,255,.12);
}

.felt::before {
  content: "";
  position: absolute;
  inset: 0;
  opacity: .08;
  pointer-events: none;
  background-image: radial-gradient(rgba(255,255,255,.6) .6px, transparent .6px);
  background-size: 7px 7px;
}

.felt::after {
  content: "";
  position: absolute;
  inset: 10px;
  pointer-events: none;
  border: 1px solid rgba(255,255,255,.06);
  border-radius: 18px;
}

.table-top,
.tableau,
.felt-footer {
  position: relative;
  z-index: 1;
}

.table-top {
  display: flex;
  justify-content: space-between;
  gap: 18px;
}

.left-piles,
.foundations {
  display: flex;
  gap: clamp(7px, 1.3vw, 16px);
}

/* ─── Piles ─── */

.pile {
  position: relative;
  width: clamp(52px, 7.5vw, 90px);
  min-width: 44px;
  aspect-ratio: 5 / 7;
  padding: 0;
  border: 0;
  border-radius: var(--radius);
  background: rgba(0,28,28,.14);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.10), 0 4px 8px rgba(0,0,0,.06);
  touch-action: manipulation;
  -webkit-touch-callout: none;
  transition: filter .18s ease, transform .18s ease, box-shadow .18s ease;
  display: grid;
  place-items: center;
}

.pile img,
.card img {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 8px;
  object-fit: cover;
  image-rendering: -webkit-optimize-contrast;
  user-select: none;
  pointer-events: none;
  -webkit-user-drag: none;
}

.stock {
  box-shadow: inset 0 0 0 2px rgba(232,185,109,.6), 0 6px 0 rgba(0,0,0,.12);
}

.stock.empty {
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.14);
}

.stock:hover:not(.empty),
.waste:hover,
.foundation:hover,
.column:hover {
  filter: brightness(1.08);
}

.stock:active:not(.empty),
.waste:active,
.foundation:active {
  transform: translateY(2px);
}

.waste { box-shadow: inset 0 0 0 1px rgba(255,255,255,.18); }

.foundation {
  border: 1px dashed rgba(255,255,255,.28);
}

.foundation:hover { border-color: rgba(255,255,255,.40); }

.suit-label {
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(26px, 4.5vw, 50px);
  opacity: .72;
  text-shadow: 0 2px 6px rgba(0,0,0,.10);
  transition: opacity .2s ease, transform .2s ease;
}

.foundation:hover .suit-label {
  opacity: .9;
  transform: scale(1.05);
}

.suit-label.hearts,
.suit-label.diamonds { color: #ff9c9f; }

.suit-label.clubs,
.suit-label.spades { color: #d4ede8; }

.empty-label,
.pile-mark {
  color: rgba(255,255,255,.42);
  font-size: 9px;
  font-weight: 800;
  letter-spacing: .15em;
}

.art-fallback {
  display: grid;
  place-items: center;
  background: #fff;
  color: #182023;
  font-weight: 800;
  border-radius: 8px;
}

/* ─── Tableau ─── */

.tableau {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 1fr));
  gap: clamp(4px, 1.2vw, 16px);
  height: calc(100% - 116px);
  min-height: 0;
  margin-top: clamp(14px, 2.5vw, 32px);
}

.column {
  position: relative;
  min-width: 0;
  min-height: 90px;
  border-radius: var(--radius);
  background: rgba(0,25,25,.08);
  box-shadow: inset 0 0 0 1px rgba(255,255,255,.05);
  touch-action: manipulation;
  transition: box-shadow .2s ease, background .2s ease;
}

.column:hover {
  background: rgba(255,255,255,.035);
}

.column.selected-pile {
  box-shadow: inset 0 0 0 2px rgba(232,185,109,.55), 0 0 18px rgba(232,185,109,.06);
  background: rgba(232,185,109,.04);
}

/* ─── Cards ─── */

.card {
  position: absolute;
  left: 0;
  width: 100%;
  aspect-ratio: 5 / 7;
  border-radius: var(--radius);
  transition: filter .15s ease, transform .15s ease, box-shadow .15s ease;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  will-change: transform;
}

.card.face-up {
  cursor: grab;
  box-shadow: var(--card-shadow);
}

.card.face-up::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 8px;
  pointer-events: none;
  background: linear-gradient(160deg, rgba(255,255,255,.10) 0%, transparent 30%, transparent 100%);
}

.card.face-up:hover {
  transform: translateY(-4px);
  filter: brightness(1.05) saturate(1.04);
  box-shadow: var(--card-shadow-hover);
}

.card.selected {
  transform: translateY(-10px);
  filter: drop-shadow(0 0 8px var(--accent)) brightness(1.06);
  box-shadow: var(--card-shadow-hover);
}

.card.drag-source { opacity: .22; }

/* ─── Felt Footer ─── */

.felt-footer {
  position: absolute;
  right: clamp(14px, 2.2vw, 28px);
  bottom: clamp(10px, 1.8vw, 22px);
  left: clamp(14px, 2.2vw, 28px);
  display: flex;
  align-items: center;
  gap: 9px;
  color: rgba(255,255,255,.82);
  font-size: 12px;
  font-weight: 500;
}

.status-dot {
  width: 7px;
  height: 7px;
  flex: none;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 4px rgba(232,185,109,.14);
  animation: pulse 2.6s ease-in-out infinite;
}

.rules {
  margin-left: auto;
  color: rgba(255,255,255,.52);
  font-size: 11px;
}

/* ─── Bottom Bar ─── */

.bottom-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 44px;
  padding-top: 4px;
  border-top: 1px solid var(--line);
}

.text-button {
  border: 0;
  padding: 0 12px;
  color: var(--muted);
  background: transparent;
  font-weight: 650;
  font-size: 13px;
  transition: color .18s ease, opacity .18s ease;
}

.text-button:hover {
  color: var(--text);
}

.text-button:disabled {
  opacity: .3;
  cursor: not-allowed;
}

.legend {
  display: flex;
  align-items: center;
  gap: 5px;
  color: var(--muted);
  font-size: 11px;
  font-weight: 500;
}

.legend-card {
  display: inline-block;
  width: 11px;
  height: 15px;
  margin-left: 8px;
  border-radius: 2.5px;
  background: #f6f6f2;
  box-shadow: 0 1.5px 4px rgba(0,0,0,.14);
}

.red-card { border-left: 3px solid #df686d; }
.black-card { border-left: 3px solid #283638; }

/* ─── Modals ─── */

.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(3,10,11,.72);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.modal[hidden] { display: none; }

.modal-card {
  position: relative;
  width: min(420px, 100%);
  padding: clamp(24px, 4.5vw, 44px);
  border: 1px solid var(--line);
  border-radius: 24px;
  background: var(--panel);
  box-shadow: var(--shadow), inset 0 1px 0 rgba(255,255,255,.06);
  animation: modal-in .4s cubic-bezier(.2,.9,.25,1) both;
}

.modal-card h2 {
  margin: 8px 0 14px;
  font-size: clamp(28px, 6vw, 44px);
  line-height: .96;
  letter-spacing: -.055em;
  font-weight: 800;
}

.modal-card p:not(.eyebrow) {
  color: var(--muted);
  line-height: 1.6;
  font-size: 14px;
}

.modal-close {
  position: absolute;
  top: 14px;
  right: 14px;
  min-height: 40px;
  min-width: 40px;
  border: 0;
  border-radius: 10px;
  color: var(--muted);
  background: transparent;
  font-size: 13px;
  font-weight: 650;
  transition: color .18s ease, background .18s ease;
}

.modal-close:hover {
  color: var(--text);
  background: var(--panel-raised);
}

.help-steps {
  display: grid;
  gap: 14px;
  margin: 24px 0;
}

.help-steps div {
  display: flex;
  align-items: center;
  gap: 13px;
  color: var(--text);
  font-size: 14px;
}

.help-steps b {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  flex: none;
  border-radius: 50%;
  color: #1a1a14;
  background: linear-gradient(135deg, var(--accent-light), var(--accent));
  font-size: 13px;
  font-weight: 800;
  box-shadow: 0 3px 8px var(--accent-glow);
}

.wide { width: 100%; }

/* ─── Win Modal ─── */

.win-modal:not([hidden]) { animation: modal-in .35s ease both; }

.win-card {
  z-index: 1;
  text-align: center;
  animation: win-pop .6s cubic-bezier(.2,.9,.25,1.2) both;
}

.win-badge {
  display: inline-block;
  padding: 5px 12px;
  border-radius: 20px;
  color: var(--accent);
  background: var(--accent-glow);
  font-size: 10px;
  font-weight: 850;
  letter-spacing: .2em;
}

.win-card-decoration {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  border-radius: 24px;
}

.win-card-decoration i {
  position: absolute;
  width: 7px;
  height: 18px;
  border-radius: 2px;
  animation: confetti 1.8s ease-out infinite;
}

.win-card-decoration i:nth-child(1) { left: 18%; top: 30%; transform: rotate(25deg); background: var(--accent); }
.win-card-decoration i:nth-child(2) { left: 28%; top: 12%; background: var(--red); animation-delay: .10s; }
.win-card-decoration i:nth-child(3) { left: 42%; top: 22%; transform: rotate(-20deg); background: var(--green); animation-delay: .20s; }
.win-card-decoration i:nth-child(4) { left: 72%; top: 16%; transform: rotate(-28deg); background: var(--accent); animation-delay: .30s; }
.win-card-decoration i:nth-child(5) { left: 82%; top: 32%; background: var(--red); animation-delay: .40s; }
.win-card-decoration i:nth-child(6) { left: 12%; top: 58%; background: var(--green); animation-delay: .50s; }
.win-card-decoration i:nth-child(7) { left: 35%; top: 68%; transform: rotate(15deg); background: var(--accent); animation-delay: .60s; }
.win-card-decoration i:nth-child(8) { left: 62%; top: 72%; transform: rotate(-22deg); background: var(--red); animation-delay: .70s; }
.win-card-decoration i:nth-child(9) { left: 78%; top: 60%; background: var(--green); animation-delay: .80s; }
.win-card-decoration i:nth-child(10) { left: 50%; top: 40%; transform: rotate(40deg); background: var(--accent); animation-delay: .90s; }

/* ─── Drag Ghost ─── */

.drag-ghost {
  position: fixed;
  z-index: 50;
  width: clamp(52px, 7.5vw, 90px);
  aspect-ratio: 5 / 7;
  pointer-events: none;
  filter: drop-shadow(0 16px 18px rgba(0,0,0,.38));
  transition: filter .1s ease;
}

.drag-ghost img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  border-radius: 8px;
  object-fit: cover;
}

/* ─── Animations ─── */

.shuffle .card,
.shuffle .pile img {
  animation: shuffle .65s cubic-bezier(.2,.7,.3,1) both;
  animation-delay: var(--deal-delay, 0ms);
}

@keyframes shuffle {
  0% { opacity: 0; transform: translateY(-12px) rotate(-3deg) scale(.96); }
  50% { opacity: 1; transform: translateY(2px) rotate(1.5deg) scale(1.01); }
  100% { opacity: 1; transform: none; }
}

@keyframes modal-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes win-pop {
  from { opacity: 0; transform: translateY(20px) scale(.92); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

@keyframes confetti {
  0% { opacity: 0; transform: translateY(0) rotate(0); }
  15% { opacity: 1; }
  100% { opacity: 0; transform: translateY(120px) rotate(180deg); }
}

@keyframes pulse {
  0%, 100% { opacity: .65; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.18); }
}

/* ─── Responsive ─── */

@media (max-width: 650px) {
  .app-shell {
    gap: 8px;
    padding-right: 10px;
    padding-left: 10px;
  }

  .topbar {
    flex-wrap: wrap;
    gap: 8px 14px;
  }

  .brand { flex: 1 1 130px; gap: 10px; }
  .brand-card { width: 30px; height: 42px; }

  .stats {
    order: 3;
    width: 100%;
    justify-content: space-between;
    padding: 8px 2px 0;
    border-top: 1px solid var(--line);
    gap: 12px;
  }

  .actions { margin-left: auto; gap: 6px; }

  .quiet-button,
  .primary-button {
    padding: 0 12px;
    font-size: 12px;
    min-height: 40px;
  }

  .felt {
    padding: 10px;
    border-radius: 18px;
  }

  .felt::after { inset: 7px; border-radius: 12px; }

  .table-top { gap: 6px; }
  .left-piles,
  .foundations { gap: 4px; }

  .tableau {
    gap: 4px;
    height: calc(100% - 104px);
    margin-top: 14px;
  }

  .felt-footer {
    right: 10px;
    bottom: 8px;
    left: 10px;
    font-size: 10px;
  }

  .rules { display: none; }
  .bottom-bar { min-height: 44px; }
  .legend { font-size: 10px; }
}

@media (max-height: 600px) and (orientation: landscape) {
  .app-shell {
    gap: 6px;
    padding-top: 8px;
    padding-bottom: calc(44px + env(safe-area-inset-bottom));
  }

  .topbar { gap: 12px; }

  .stats {
    display: flex;
    width: auto;
    gap: 16px;
    padding: 0;
    border: 0;
  }

  .tableau {
    height: calc(100% - 90px);
    margin-top: 10px;
  }

  .felt-footer { display: none; }
}

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