/* Klondike Solitaire: dark page, green felt table, cards drawn with CSS and SVG. */

:root {
  color-scheme: dark;
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-hover: #273449;
  --line: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #86efac;
  --accent-ink: #052e16;
  --cursor: #fde047;
  --radius: 12px;
  --shadow: 0 10px 28px rgba(2, 6, 23, 0.5);
  --fast: 150ms;
  --mid: 220ms;
  --ink-red: #c81e3a;
  --ink-black: #0f172a;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.4;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: inherit;
}

button:focus-visible,
a:focus-visible,
input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

[hidden] {
  display: none !important;
}

svg {
  flex: none;
}

/* ---------- header ---------- */

.topbar {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas:
    "brand tools"
    "actions actions"
    "options options"
    "stats stats";
  align-items: center;
  gap: 6px 10px;
  padding: 8px 12px 6px;
}

.brand {
  grid-area: brand;
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.brand h1 {
  margin: 0;
  min-width: 0;
  overflow: hidden;
  font-size: 1.05rem;
  letter-spacing: 0.01em;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.deal-chip {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 40px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 0.8rem;
  white-space: nowrap;
  cursor: pointer;
  transition: background var(--fast), border-color var(--fast);
}

.deal-chip:hover {
  background: var(--surface-hover);
}

.deal-value {
  color: var(--text);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.verified {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  color: var(--accent);
  font-weight: 600;
}

.verified svg {
  width: 15px;
  height: 15px;
  fill: none;
  stroke: currentColor;
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.actions {
  grid-area: actions;
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 6px;
}

.btn {
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-height: 46px;
  padding: 4px 6px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
  cursor: pointer;
  transition: background var(--fast), border-color var(--fast), transform var(--fast), opacity var(--fast);
}

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

.btn:hover:not(:disabled) {
  background: var(--surface-hover);
  border-color: #475569;
}

.btn:active:not(:disabled) {
  transform: translateY(1px);
}

.btn:disabled {
  opacity: 0.4;
  cursor: default;
}

.btn.primary {
  border-color: transparent;
  background: var(--accent);
  color: var(--accent-ink);
}

.btn.primary:hover:not(:disabled) {
  background: #a7f3c0;
}

.tools {
  grid-area: tools;
  display: flex;
  gap: 6px;
  justify-self: end;
}

.icon-btn {
  width: 40px;
  height: 40px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--surface);
  font-weight: 700;
  cursor: pointer;
  transition: background var(--fast);
}

.icon-btn:hover {
  background: var(--surface-hover);
}

.icon-btn.lang {
  font-size: 0.8rem;
  letter-spacing: 0.04em;
}

.options {
  grid-area: options;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 6px 12px;
}

.segmented {
  display: inline-flex;
  padding: 3px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
}

.segmented button {
  min-width: 66px;
  min-height: 40px;
  padding: 0 12px;
  border: 0;
  border-radius: 9px;
  background: transparent;
  color: var(--muted);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--fast), color var(--fast);
}

.segmented button[aria-pressed="true"] {
  background: var(--accent);
  color: var(--accent-ink);
}

.switch {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 40px;
  font-size: 0.8rem;
  cursor: pointer;
}

.switch input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
}

.switch .track {
  position: relative;
  flex: none;
  width: 40px;
  height: 24px;
  border-radius: 12px;
  background: var(--line);
  transition: background var(--fast);
}

.switch .track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: var(--text);
  transition: transform var(--fast), background var(--fast);
}

.switch input:checked + .track {
  background: var(--accent);
}

.switch input:checked + .track::after {
  transform: translateX(16px);
  background: var(--accent-ink);
}

.switch input:focus-visible + .track {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.stats {
  grid-area: stats;
  display: flex;
  justify-content: space-around;
  gap: 12px;
  margin: 0;
}

.stat {
  display: flex;
  align-items: baseline;
  gap: 6px;
}

.stat dt {
  color: var(--muted);
  font-size: 0.7rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.stat dd {
  margin: 0;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

@media (max-width: 519px) {
  .deal-word,
  .verified-word {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }
}

@media (min-width: 640px) {
  .actions {
    display: flex;
    gap: 8px;
  }

  .btn {
    flex-direction: row;
    gap: 8px;
    min-height: 42px;
    padding: 0 14px;
    font-size: 0.875rem;
  }

  .btn svg {
    width: 18px;
    height: 18px;
  }
}

@media (min-width: 960px) {
  .topbar {
    grid-template-columns: 1fr auto 1fr;
    grid-template-areas:
      "brand actions tools"
      "stats . options";
    gap: 8px 16px;
    padding: 10px 20px 8px;
  }

  .brand h1 {
    font-size: 1.2rem;
  }

  .stats {
    justify-content: flex-start;
    gap: 22px;
  }

  .options {
    justify-content: flex-end;
  }
}

/* ---------- table ---------- */

.play {
  position: relative;
  display: flex;
  flex: 1;
  flex-direction: column;
  min-height: 0;
  padding: 0 6px;
}

.table-wrap {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  border-radius: var(--radius);
  background:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 .09 0'/%3E%3C/filter%3E%3Crect width='180' height='180' filter='url(%23n)'/%3E%3C/svg%3E"),
    radial-gradient(130% 95% at 50% 0%, #1f8a52 0%, #156c40 48%, #0d4a2c 100%);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.05), inset 0 14px 40px rgba(0, 0, 0, 0.28);
}

.table {
  position: relative;
  width: 100%;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  outline: none;
}

.table:focus-visible {
  box-shadow: inset 0 0 0 2px var(--accent);
}

.slot,
.card {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--card-w);
  height: var(--card-h);
}

.slot {
  display: grid;
  place-items: center;
  border: 2px solid rgba(255, 255, 255, 0.14);
  border-radius: calc(var(--card-w) * 0.08);
  background: rgba(0, 0, 0, 0.1);
  transition: background var(--fast), border-color var(--fast);
}

.slot::after {
  color: rgba(255, 255, 255, 0.24);
  font-size: calc(var(--card-w) * 0.38);
  font-weight: 700;
  line-height: 1;
}

.slot-foundation::after,
.slot-tableau::after {
  content: attr(data-hint);
}

.slot-waste {
  border-style: dashed;
  border-color: rgba(255, 255, 255, 0.08);
  background: transparent;
}

.slot-stock {
  cursor: pointer;
}

.slot-stock.recycle::after {
  content: "\21BB";
  font-size: calc(var(--card-w) * 0.5);
}

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

.card {
  --r: calc(var(--card-w) * 0.08);
  perspective: 900px;
  cursor: pointer;
  transition: transform 240ms cubic-bezier(0.2, 0.75, 0.25, 1) var(--delay, 0ms);
}

.card-inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform 240ms ease var(--delay, 0ms);
}

.card.down .card-inner {
  transform: rotateY(180deg);
}

.card-face,
.card-back {
  position: absolute;
  inset: 0;
  border-radius: var(--r);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 0 0 1px rgba(15, 23, 42, 0.22);
  transition: box-shadow var(--fast);
}

.card-face {
  overflow: hidden;
  background: linear-gradient(160deg, #ffffff 0%, #f1f5f9 100%);
  color: var(--ink-black);
}

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

.card-back {
  transform: rotateY(180deg);
  border: max(2px, calc(var(--card-w) * 0.055)) solid #f1f5f9;
  background-color: #1e3a5f;
  background-image:
    radial-gradient(circle, transparent 0 15%, rgba(134, 239, 172, 0.55) 16% 21%, transparent 22%),
    repeating-linear-gradient(45deg, rgba(134, 239, 172, 0.2) 0 2px, transparent 2px 7px),
    repeating-linear-gradient(-45deg, rgba(134, 239, 172, 0.2) 0 2px, transparent 2px 7px);
}

.rank {
  position: absolute;
  top: 4%;
  left: 7%;
  font-size: var(--rank-size);
  font-weight: 800;
  line-height: 1;
  letter-spacing: -0.05em;
}

.corner-suit {
  position: absolute;
  top: 6%;
  right: 7%;
  width: calc(var(--rank-size) * 0.8);
  height: calc(var(--rank-size) * 0.8);
  fill: currentColor;
}

.pip {
  position: absolute;
  top: calc(var(--card-h) * 0.6 - var(--card-w) * 0.28);
  left: 22%;
  width: 56%;
  height: calc(var(--card-w) * 0.56);
  fill: currentColor;
}

.pip.ace {
  top: calc(var(--card-h) * 0.58 - var(--card-w) * 0.33);
  left: 17%;
  width: 66%;
  height: calc(var(--card-w) * 0.66);
}

.court {
  position: absolute;
  top: 30%;
  right: 13%;
  bottom: 7%;
  left: 13%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: max(1px, calc(var(--card-w) * 0.02)) solid currentColor;
  border-radius: calc(var(--r) * 0.8);
  background: linear-gradient(180deg, rgba(15, 23, 42, 0.03), rgba(15, 23, 42, 0.09));
}

.card.red .court {
  background: linear-gradient(180deg, rgba(200, 30, 58, 0.04), rgba(200, 30, 58, 0.12));
}

.court-rank {
  font-family: Georgia, "Times New Roman", serif;
  font-size: calc(var(--card-w) * 0.4);
  font-weight: 700;
  line-height: 1;
}

.court-suit {
  width: calc(var(--card-w) * 0.2);
  height: calc(var(--card-w) * 0.2);
  fill: currentColor;
}

/* ---------- states ---------- */

.card.dragging {
  cursor: grabbing;
  transition: none;
}

.card.dragging .card-face {
  box-shadow: 0 14px 28px rgba(0, 0, 0, 0.45), 0 0 0 1px rgba(15, 23, 42, 0.22);
}

.card.picked .card-face {
  box-shadow: 0 10px 20px rgba(0, 0, 0, 0.4), 0 0 0 2px var(--cursor);
}

.card.mark-hint .card-face,
.card.mark-hint .card-back {
  box-shadow: 0 0 0 3px var(--accent), 0 0 16px rgba(134, 239, 172, 0.85);
  animation: glow 900ms ease-in-out infinite alternate;
}

.card.mark-drop .card-face {
  box-shadow: 0 0 0 3px var(--accent), 0 0 18px rgba(134, 239, 172, 0.9);
}

.slot.mark-hint,
.slot.mark-drop {
  border-color: var(--accent);
  background: rgba(134, 239, 172, 0.16);
}

.card.nope {
  animation: nope 260ms ease;
}

.table.instant .card,
.table.instant .card-inner,
.table.instant .kbd-cursor {
  transition: none !important;
}

.kbd-cursor {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 500;
  box-sizing: content-box;
  margin: -4px;
  padding: 1px;
  border: 3px solid var(--cursor);
  border-radius: calc(var(--card-w) * 0.1);
  opacity: 0;
  pointer-events: none;
  transition: transform var(--fast), width var(--fast), height var(--fast), opacity var(--fast);
}

.kbd-cursor.on {
  opacity: 1;
}

.fx {
  position: absolute;
  inset: 0;
  z-index: 600;
  display: none;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.fx.on {
  display: block;
}

/* ---------- overlays ---------- */

.auto-btn {
  position: absolute;
  right: 18px;
  bottom: 14px;
  z-index: 650;
  min-height: 44px;
  padding: 0 20px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: var(--accent-ink);
  font-weight: 700;
  box-shadow: var(--shadow);
  cursor: pointer;
  animation: pop var(--mid) ease;
}

.win {
  position: absolute;
  top: 50%;
  left: 50%;
  z-index: 700;
  width: min(320px, calc(100% - 32px));
  padding: 22px 24px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: rgba(30, 41, 59, 0.95);
  box-shadow: var(--shadow);
  text-align: center;
  transform: translate(-50%, -50%);
  animation: rise 250ms ease;
}

.win h2 {
  margin: 0 0 6px;
  color: var(--accent);
  font-size: 1.6rem;
}

.win p {
  margin: 0 0 16px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

.win .btn {
  width: 100%;
  font-size: 0.95rem;
}

.toast {
  position: absolute;
  bottom: 16px;
  left: 50%;
  z-index: 800;
  width: max-content;
  max-width: calc(100% - 32px);
  padding: 10px 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.94);
  box-shadow: var(--shadow);
  font-size: 0.85rem;
  text-align: center;
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 12px);
  transition: opacity var(--mid), transform var(--mid);
}

.toast.on {
  opacity: 1;
  transform: translate(-50%, 0);
}

.footer {
  text-align: center;
  font-size: 0.75rem;
}

.footer a {
  display: inline-block;
  padding: 10px 12px;
  line-height: 20px;
  color: var(--muted);
  text-decoration: none;
  transition: color var(--fast);
}

.footer a:hover {
  color: var(--accent);
}

/* ---------- dialogs ---------- */

dialog {
  width: min(460px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  padding: 20px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

dialog::backdrop {
  background: rgba(2, 6, 23, 0.62);
}

dialog h2 {
  margin: 0 0 12px;
  font-size: 1.15rem;
}

dialog h3 {
  margin: 16px 0 8px;
  font-size: 0.95rem;
}

dialog p {
  margin: 0 0 10px;
  color: #cbd5e1;
  font-size: 0.9rem;
}

.field {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 0.85rem;
}

.field input {
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 1rem;
  font-variant-numeric: tabular-nums;
}

.field input[aria-invalid="true"] {
  border-color: #f87171;
  box-shadow: 0 0 0 2px rgba(248, 113, 113, 0.35);
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 18px;
}

.dialog-actions .btn {
  flex-direction: row;
  min-width: 96px;
  min-height: 42px;
  padding: 0 16px;
  font-size: 0.875rem;
}

.keys {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px 12px;
  margin: 0;
  font-size: 0.85rem;
}

.keys dt {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
}

.keys dd {
  margin: 0;
  color: #cbd5e1;
}

kbd {
  display: inline-block;
  min-width: 26px;
  padding: 2px 6px;
  border: 1px solid #475569;
  border-bottom-width: 2px;
  border-radius: 6px;
  background: var(--bg);
  font-family: inherit;
  font-size: 0.8rem;
  text-align: center;
}

/* ---------- motion ---------- */

@keyframes nope {
  0%,
  100% {
    translate: 0 0;
  }

  20%,
  60% {
    translate: -5px 0;
  }

  40%,
  80% {
    translate: 5px 0;
  }
}

@keyframes glow {
  to {
    box-shadow: 0 0 0 2px var(--accent), 0 0 6px rgba(134, 239, 172, 0.5);
  }
}

@keyframes pop {
  from {
    opacity: 0;
    transform: scale(0.9);
  }
}

@keyframes rise {
  from {
    opacity: 0;
    transform: translate(-50%, -42%);
  }
}

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