:root {
  --bg: #f4f5f8;
  --surface: #ffffff;
  --surface-2: #eceef3;
  --text: #1b1f2a;
  --muted: #6b7280;
  --line: #c9cdd6;
  --accent: #3563e9;
  --accent-text: #ffffff;
  --accent-soft: #e3eafc;
  --success: #1f9d55;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 4px 16px rgb(16 24 40 / 6%);
  --hint: #ffc53d;
  --radius: 16px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;

  /* Table and cards */
  --on-felt: #f2f7f3;
  --card-face: #ffffff;
  --card-edge: rgb(0 0 0 / 18%);
  --card-black: #1b1f2a;
  --card-red: #d0283a;
  --back: #3563e9;
  --back-2: #2448b8;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1115;
    --surface: #181b22;
    --surface-2: #232733;
    --text: #e8eaf0;
    --muted: #8b93a3;
    --line: #343a48;
    --accent: #6d94ff;
    --accent-text: #0b1020;
    --accent-soft: #1f2a44;
    --success: #4ccb84;
    --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 25%);
    --card-face: #eceef2;
    --card-edge: rgb(0 0 0 / 40%);
    --felt-shade: 0.62;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0f1115;
  --surface: #181b22;
  --surface-2: #232733;
  --text: #e8eaf0;
  --muted: #8b93a3;
  --line: #343a48;
  --accent: #6d94ff;
  --accent-text: #0b1020;
  --accent-soft: #1f2a44;
  --success: #4ccb84;
  --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 25%);
  --card-face: #eceef2;
  --card-edge: rgb(0 0 0 / 40%);
  --felt-shade: 0.62;
  color-scheme: dark;
}

/* Table colors (Settings → Table). Dark mode dims them via --felt-shade. */
:root[data-felt="green"], :root:not([data-felt]) { --felt-base: #2f7a53; }
:root[data-felt="blue"] { --felt-base: #2b5f8f; }
:root[data-felt="red"] { --felt-base: #8a3a3f; }
:root[data-felt="slate"] { --felt-base: #3e4654; }
:root {
  --felt: color-mix(in srgb, var(--felt-base) calc(var(--felt-shade, 1) * 100%), #05070a);
  --felt-dark: color-mix(in srgb, var(--felt) 78%, #000);
}

/* Card backs (Settings → Card back) */
:root[data-back="red"] { --back: #c8364a; --back-2: #96202f; }
:root[data-back="green"] { --back: #1f8a5b; --back-2: #146240; }
:root[data-back="purple"] { --back: #7a45c2; --back-2: #552c90; }

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
}

body { -webkit-user-select: none; user-select: none; }

button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}

svg {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

[hidden] { display: none !important; }

.muted { color: var(--muted); }
.center { text-align: center; }
.small { font-size: 0.875rem; }

/* ---------- Shared controls ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 0 20px;
  border-radius: 12px;
  background: var(--surface-2);
  font-weight: 600;
  font-size: 1rem;
  transition: transform 0.08s ease, filter 0.15s ease;
}
.btn:active { transform: scale(0.97); }
.btn.primary { background: var(--accent); color: var(--accent-text); }
.btn.ghost { background: transparent; color: var(--muted); }
.btn.small { min-height: 38px; padding: 0 14px; font-size: 0.9rem; }
.btn:disabled { opacity: 0.5; pointer-events: none; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 12px;
  color: inherit;
}
.icon-btn:active { background: var(--surface-2); }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

.eyebrow {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
}

.btn-row { display: flex; gap: 10px; }
.btn-row .btn { flex: 1; }
.btn-stack { display: flex; flex-direction: column; gap: 10px; }

:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

/* ---------- Home ---------- */

#home {
  max-width: 520px;
  margin: 0 auto;
  padding:
    calc(env(safe-area-inset-top) + 12px)
    calc(env(safe-area-inset-right) + 16px)
    calc(env(safe-area-inset-bottom) + 24px)
    calc(env(safe-area-inset-left) + 16px);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.home-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-right: -10px;
}
.home-header h1 { margin: 8px 0; font-size: 2rem; letter-spacing: -0.02em; }

.install-card { display: flex; flex-direction: column; gap: 12px; padding: 16px; }
.install-card p { margin: 4px 0 0; color: var(--muted); font-size: 0.9rem; }
.install-actions { display: flex; gap: 8px; }

.continue-card { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 16px 16px 20px; }
.continue-meta { font-weight: 600; margin-top: 4px; }

.section-title { margin: 8px 0 -4px; font-size: 1rem; color: var(--muted); font-weight: 600; }

.variant-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.variant {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
  min-height: 112px;
  padding: 16px 18px;
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--shadow);
  text-align: left;
  transition: transform 0.08s ease;
}
.variant:active { transform: scale(0.97); }
.variant span { font-weight: 700; font-size: 1.1rem; }
.variant em { font-style: normal; font-size: 0.9rem; }
.variant small { color: var(--muted); font-size: 0.82rem; margin-top: auto; }
.variant::before {
  content: "";
  display: block;
  width: 28px;
  height: 4px;
  border-radius: 2px;
  background: var(--accent);
  margin-bottom: 6px;
}
.variant[data-variant="klondike"]::before { background: #33b06a; }
.variant[data-variant="spider"]::before { background: #b5459b; }
.variant[data-variant="freecell"]::before { background: #3a8fe0; }
.variant[data-variant="yukon"]::before { background: #e3a325; }

/* ---------- Game ---------- */

#game {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding:
    env(safe-area-inset-top)
    env(safe-area-inset-right)
    calc(env(safe-area-inset-bottom) + 4px)
    env(safe-area-inset-left);
  overflow: hidden;
  background: radial-gradient(120% 90% at 50% 30%, var(--felt), var(--felt-dark));
  color: var(--on-felt);
}

.game-header {
  display: grid;
  grid-template-columns: 96px 1fr 96px;
  align-items: center;
  padding: 6px 8px;
  width: 100%;
  max-width: 1000px;
  margin: 0 auto;
}
.game-header .icon-btn:active { background: rgb(255 255 255 / 12%); }
.game-title { display: flex; flex-direction: column; align-items: center; line-height: 1.2; }
.game-title span { font-weight: 700; }
.game-title small { opacity: 0.75; font-size: 0.8rem; min-height: 1em; font-variant-numeric: tabular-nums; }
.timer-btn {
  justify-self: end;
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 40px;
  padding: 0 10px 0 12px;
  border-radius: 999px;
  background: rgb(255 255 255 / 14%);
  font-variant-numeric: tabular-nums;
  font-weight: 600;
}
.timer-btn svg { width: 18px; height: 18px; }

.table-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  width: 100%;
  max-width: 1000px;
  margin: 0 auto;
}

.table {
  position: absolute;
  inset: 0;
  touch-action: none;
  overflow: hidden;
}

/* Empty pile outlines */
.slot {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--cw);
  height: var(--ch);
  border-radius: var(--cr);
  border: 2px solid rgb(255 255 255 / 22%);
  background: rgb(0 0 0 / 8%);
  display: grid;
  place-items: center;
  color: rgb(255 255 255 / 35%);
}
.slot svg { width: 45%; height: 45%; }
.slot.foundation { font-size: calc(var(--cw) * 0.42); font-weight: 700; }
.slot.tableau { border-style: dashed; border-color: rgb(255 255 255 / 14%); background: transparent; }
.slot.cell { background: rgb(0 0 0 / 14%); }
.slot.hint { border-color: var(--hint); border-style: solid; box-shadow: 0 0 0 3px color-mix(in srgb, var(--hint) 45%, transparent); }

/* Cards: the outer element is positioned, the inner one is drawn and animated. */
.pc {
  position: absolute;
  left: 0;
  top: 0;
  width: var(--cw);
  height: var(--ch);
  transition: transform 0.24s cubic-bezier(0.2, 0.8, 0.3, 1);
  will-change: transform;
}
.pc.dragging { transition: none; }
.pc.instant { transition: none; }

.pc .in {
  position: absolute;
  inset: 0;
  border-radius: var(--cr);
  background: var(--card-face);
  box-shadow: 0 0 0 1px var(--card-edge), 0 1px 2px rgb(0 0 0 / 20%);
  color: var(--card-black);
  overflow: hidden;
  font-size: var(--cf);
  line-height: 1;
}
.pc.red .in { color: var(--card-red); }
.pc.dragging .in { box-shadow: 0 0 0 1px var(--card-edge), 0 10px 24px rgb(0 0 0 / 35%); }

.pc .rank {
  position: absolute;
  left: 0.14em;
  top: 0.12em;
  font-weight: 700;
  letter-spacing: -0.06em;
}
.pc .rank.ten { letter-spacing: -0.12em; left: 0.06em; }
.pc .sm {
  position: absolute;
  right: 0.12em;
  top: 0.14em;
  width: 0.8em;
  height: 0.8em;
  fill: currentColor;
  stroke: none;
}
.pc .lg {
  position: absolute;
  left: 50%;
  top: 60%;
  width: 52%;
  height: 52%;
  transform: translate(-50%, -50%);
  fill: currentColor;
  stroke: none;
}
.pc .face-letter {
  position: absolute;
  left: 50%;
  top: 53%;
  transform: translate(-50%, -50%);
  font-size: 1.8em;
  font-weight: 700;
  font-family: Georgia, "Times New Roman", serif;
}
.pc .face-letter + .lg { width: 22%; height: 22%; top: 85%; }

.pc.down .in {
  background:
    repeating-linear-gradient(45deg, rgb(255 255 255 / 10%) 0 2px, transparent 2px 7px),
    repeating-linear-gradient(-45deg, rgb(255 255 255 / 10%) 0 2px, transparent 2px 7px),
    linear-gradient(160deg, var(--back), var(--back-2));
  box-shadow: inset 0 0 0 calc(var(--cw) * 0.05) rgb(255 255 255 / 85%), 0 0 0 1px var(--card-edge), 0 1px 2px rgb(0 0 0 / 20%);
}
.pc.down .in > * { display: none; }

.pc.flip .in { animation: flip 0.22s ease-out; }
@keyframes flip { 0% { transform: scaleX(0.05); } 100% { transform: scaleX(1); } }

.pc.shake .in { animation: shake 0.3s ease; }
@keyframes shake { 25% { transform: translateX(-5px); } 50% { transform: translateX(5px); } 75% { transform: translateX(-3px); } }

.pc.hint .in { box-shadow: 0 0 0 2px var(--hint), 0 0 0 5px color-mix(in srgb, var(--hint) 45%, transparent); }

.table.celebrate .pc .in { animation: hop 0.7s cubic-bezier(0.3, 0.7, 0.4, 1) both; animation-delay: var(--d); }
@keyframes hop {
  0% { transform: none; }
  40% { transform: translateY(calc(var(--ch) * -0.35)) rotate(-6deg); }
  100% { transform: none; }
}

.finish-btn {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  z-index: 3000;
  box-shadow: 0 6px 20px rgb(0 0 0 / 30%);
  animation: rise 0.25s ease;
}
.finish-btn:active { transform: translateX(-50%) scale(0.97); }

.pause-overlay {
  position: absolute;
  inset: 0;
  z-index: 4000;
  display: flex;
  flex-direction: column;
  gap: 14px;
  align-items: center;
  justify-content: center;
  background: color-mix(in srgb, var(--felt-dark) 75%, transparent);
  -webkit-backdrop-filter: blur(18px);
  backdrop-filter: blur(18px);
  font-weight: 600;
  font-size: 1.1rem;
}

.tools {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding: 4px 8px 0;
}
.tool {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 8px 0 6px;
  border-radius: 12px;
  font-size: 0.78rem;
  font-weight: 600;
  color: inherit;
  opacity: 0.92;
}
.tool:active { background: rgb(255 255 255 / 12%); }
.tool svg { width: 24px; height: 24px; }
.tool:disabled { opacity: 0.35; }

/* Landscape phones: tools move to a column on the right. */
@media (orientation: landscape) and (max-height: 560px) {
  #game {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-rows: auto 1fr;
  }
  .game-header { grid-column: 1 / -1; padding: 0 8px; max-width: none; }
  .table-wrap { max-width: none; }
  .tools { grid-template-columns: 1fr; width: 76px; padding: 0 6px; align-content: center; }
}

/* ---------- Dialogs ---------- */

dialog {
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--text);
  max-width: 100vw;
  max-height: 100dvh;
  width: 100%;
  margin: auto auto 0;
}
dialog::backdrop { background: rgb(0 0 0 / 40%); }
dialog[open] .sheet { animation: rise 0.22s cubic-bezier(0.2, 0.9, 0.3, 1); }
@keyframes rise { from { translate: 0 24px; opacity: 0; } }

.sheet {
  width: 100%;
  max-width: 520px;
  max-height: 88dvh;
  overflow-y: auto;
  margin: 0 auto;
  background: var(--surface);
  border-radius: 22px 22px 0 0;
  padding: 22px 20px calc(env(safe-area-inset-bottom) + 20px);
  display: flex;
  flex-direction: column;
  gap: 14px;
  -webkit-user-select: text;
  user-select: text;
}
.sheet h2 { margin: 0; font-size: 1.4rem; }
.sheet-header { display: flex; align-items: center; justify-content: space-between; margin: -6px -10px -6px 0; }

@media (min-width: 640px) {
  dialog { margin: auto; }
  .sheet { border-radius: 22px; padding-bottom: 22px; }
}

.rules { margin: 0; line-height: 1.45; color: var(--muted); }
#setup-options:empty { display: none; }
#setup-options .segmented { width: 100%; }
#setup-options .segmented button { flex: 1; padding: 10px 12px; }
.setup-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }

#win-dialog h2 { text-align: center; font-size: 1.8rem; }
.win-stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.win-stats > div, .setup-stats > div {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 12px 4px;
  border-radius: 12px;
  background: var(--surface-2);
}
.stat-value { font-size: 1.35rem; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-label { font-size: 0.75rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; }
#win-note:empty { display: none; }
#win-note { margin: -4px 0 0; }

.toggle, .setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 48px;
  font-weight: 500;
  cursor: pointer;
}
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.toggle i {
  position: relative;
  flex: none;
  width: 50px;
  height: 30px;
  border-radius: 999px;
  background: var(--line);
  transition: background-color 0.2s ease;
}
.toggle i::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgb(0 0 0 / 25%);
  transition: transform 0.2s ease;
}
.toggle input:checked + i { background: var(--accent); }
.toggle input:checked + i::after { transform: translateX(20px); }
.toggle input:focus-visible + i { outline: 3px solid var(--accent); outline-offset: 2px; }

.segmented { display: flex; background: var(--surface-2); border-radius: 10px; padding: 3px; }
.segmented button { padding: 7px 12px; border-radius: 8px; font-size: 0.9rem; font-weight: 600; color: var(--muted); white-space: nowrap; }
.segmented button.active { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }

.swatches { display: flex; gap: 10px; }
.swatches button {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--c);
  box-shadow: inset 0 0 0 1px rgb(0 0 0 / 15%);
}
.swatches button.active { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--accent); }

#confirm-dialog p { margin: 4px 0 6px; font-size: 1.05rem; line-height: 1.4; }

/* ---------- Toast ---------- */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 84px);
  transform: translate(-50%, 20px);
  max-width: calc(100vw - 32px);
  padding: 12px 18px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-weight: 600;
  font-size: 0.95rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 10;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
