/* ---------- Reset & tokens ---------- */
*, *::before, *::after { box-sizing: border-box; }

/* `hidden` is only a UA rule; any author `display` beats it. Pin it once, here. */
[hidden] { display: none !important; }

:root {
  --bg: #f4f5f9;
  --bg-glow-a: #dfe4ff;
  --bg-glow-b: #ffe6f0;
  --surface: #ffffff;
  --surface-2: #f0f1f6;
  --border: #e2e4ee;
  --text: #191b23;
  --text-dim: #6a6f80;
  --accent: #5b5bd6;
  --accent-soft: #ececff;
  --accent-text: #ffffff;
  --good: #17936a;
  --good-soft: #e2f6ee;
  --card-back-a: #6366f1;
  --card-back-b: #a855f7;
  --shadow-sm: 0 1px 2px rgba(20, 22, 40, .06), 0 2px 8px rgba(20, 22, 40, .05);
  --shadow-md: 0 6px 24px rgba(20, 22, 40, .10);
  --shadow-lg: 0 24px 60px rgba(20, 22, 40, .22);
  --radius: 14px;
  --gap: clamp(6px, 1.4vw, 12px);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d0f16;
    --bg-glow-a: #1b1f3d;
    --bg-glow-b: #2a1630;
    --surface: #171a24;
    --surface-2: #1f232f;
    --border: #2a2f3d;
    --text: #eef0f6;
    --text-dim: #9aa1b4;
    --accent: #8b8bf0;
    --accent-soft: #23244a;
    --accent-text: #0d0f16;
    --good: #48d3a0;
    --good-soft: #16302a;
    --card-back-a: #4f46e5;
    --card-back-b: #9333ea;
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
    --shadow-md: 0 6px 24px rgba(0, 0, 0, .45);
    --shadow-lg: 0 24px 60px rgba(0, 0, 0, .6);
  }
}

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  background-image:
    radial-gradient(60vw 60vw at 12% -10%, var(--bg-glow-a), transparent 60%),
    radial-gradient(50vw 50vw at 100% 0%, var(--bg-glow-b), transparent 55%);
  background-attachment: fixed;
  color: var(--text);
  font: 16px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

.sr-only {
  position: absolute; width: 1px; height: 1px;
  padding: 0; margin: -1px; overflow: hidden;
  clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- Layout ---------- */
.app {
  min-height: 100%;
  max-width: 900px;
  margin: 0 auto;
  padding: clamp(14px, 3vw, 28px) clamp(12px, 3vw, 24px) 28px;
  display: flex;
  flex-direction: column;
  gap: clamp(12px, 2vw, 18px);
}

.topbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.brand {
  margin: 0;
  font-size: clamp(1.15rem, 3.4vw, 1.5rem);
  font-weight: 700;
  letter-spacing: -0.02em;
  display: flex;
  align-items: center;
  gap: 8px;
}
.brand-mark { font-size: 1.15em; line-height: 1; }

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

/* ---------- Buttons & inputs ---------- */
.btn {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 600;
  font-size: .875rem;
  padding: 9px 14px;
  border-radius: 10px;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  transition: transform .12s ease, background-color .15s ease, border-color .15s ease;
}
.btn:hover { border-color: var(--accent); }
.btn:active { transform: translateY(1px); }

.btn-primary {
  background: var(--accent);
  border-color: transparent;
  color: var(--accent-text);
}
.btn-primary:hover { filter: brightness(1.08); }

.segmented {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 11px;
}
.seg {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  font: inherit;
  font-weight: 600;
  font-size: .8125rem;
  padding: 6px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background-color .15s ease, color .15s ease;
}
.seg:hover { color: var(--text); }
.seg[aria-pressed="true"] {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.select-wrap { display: inline-flex; }
#theme {
  appearance: none;
  font: inherit;
  font-weight: 600;
  font-size: .8125rem;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 30px 8px 12px;
  cursor: pointer;
  box-shadow: var(--shadow-sm);
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%),
                    linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: calc(100% - 15px) 55%, calc(100% - 10px) 55%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
}

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

/* ---------- Stats ---------- */
.stats {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}
.stat {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 9px 12px;
  box-shadow: var(--shadow-sm);
  min-width: 0;
}
.stat-label {
  display: block;
  font-size: .6875rem;
  font-weight: 600;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.stat-value {
  display: block;
  font-size: clamp(1rem, 3.6vw, 1.25rem);
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.stat-sub { color: var(--text-dim); font-size: .8em; font-weight: 600; }
.stat-value.pulse { animation: pop .28s ease; }
@keyframes pop { 50% { transform: scale(1.18); color: var(--good); } }

/* ---------- Board ---------- */
.board-wrap {
  display: flex;
  flex: 1 1 auto;
  min-height: 0;
  justify-content: center;
  align-items: flex-start;
}

.board {
  display: grid;
  gap: var(--gap);
  width: 100%;
  max-width: var(--board-max, 100%);
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
}

.card {
  appearance: none;
  border: 0;
  padding: 0;
  margin: 0;
  background: transparent;
  aspect-ratio: 3 / 4;
  perspective: 900px;
  cursor: pointer;
  border-radius: var(--radius);
}
.card[disabled] { cursor: default; }

.card-inner {
  display: block;
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: var(--radius);
  transform-style: preserve-3d;
  transition: transform .42s cubic-bezier(.3, 1.4, .5, 1);
  box-shadow: var(--shadow-sm);
}
.card:not(.flipped):hover:not([disabled]) .card-inner { transform: translateY(-3px); }
.card.flipped .card-inner { transform: rotateY(180deg); }

.face {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: var(--radius);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  overflow: hidden;
}

.face-back {
  background: linear-gradient(140deg, var(--card-back-a), var(--card-back-b));
  color: rgba(255, 255, 255, .55);
  font-size: clamp(1rem, 4vw, 1.6rem);
}
.face-back::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image:
    radial-gradient(circle at 25% 25%, rgba(255, 255, 255, .22) 1.5px, transparent 1.6px),
    radial-gradient(circle at 75% 75%, rgba(255, 255, 255, .22) 1.5px, transparent 1.6px);
  background-size: 14px 14px;
  opacity: .9;
}

.face-front {
  background: var(--surface);
  border: 1px solid var(--border);
  transform: rotateY(180deg);
  font-size: clamp(1.4rem, 6.5vw, 2.4rem);
  line-height: 1;
}

.card.matched .card-inner { box-shadow: 0 0 0 2px var(--good), var(--shadow-sm); }
.card.matched .face-front { background: var(--good-soft); border-color: transparent; }
.card.matched { animation: matched-pop .4s ease; }
@keyframes matched-pop {
  40% { transform: scale(1.07); }
  100% { transform: scale(1); }
}

.card.wrong .card-inner { animation: shake .38s ease; }
@keyframes shake {
  20% { transform: rotateY(180deg) translateX(-6%); }
  50% { transform: rotateY(180deg) translateX(6%); }
  80% { transform: rotateY(180deg) translateX(-3%); }
  100% { transform: rotateY(180deg); }
}

.board.dealing .card { animation: deal .4s backwards cubic-bezier(.2, .9, .3, 1); }
@keyframes deal {
  from { opacity: 0; transform: translateY(14px) scale(.9); }
}

/* ---------- Hint & footer ---------- */
.hint, .footer {
  text-align: center;
  color: var(--text-dim);
  font-size: .8125rem;
  margin: 0;
}
.footer { margin-top: auto; padding-top: 8px; font-size: .75rem; }

/* ---------- Win modal ---------- */
#confetti {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 20;
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 30;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(10, 12, 20, .5);
  backdrop-filter: blur(4px);
  animation: fade .2s ease;
}
@keyframes fade { from { opacity: 0; } }

.modal {
  width: min(400px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: var(--shadow-lg);
  padding: 26px 24px 22px;
  text-align: center;
  animation: rise .28s cubic-bezier(.2, .9, .3, 1);
}
@keyframes rise { from { opacity: 0; transform: translateY(16px) scale(.96); } }

.modal-mark { font-size: 2.75rem; line-height: 1; }
.modal h2 { margin: 10px 0 4px; font-size: 1.3rem; letter-spacing: -0.02em; }
.modal-sub { margin: 0; color: var(--text-dim); font-size: .875rem; min-height: 1.3em; }

.modal-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 18px 0;
}
.modal-stats > div {
  background: var(--surface-2);
  border-radius: 12px;
  padding: 10px 6px;
}
.modal-stats dt {
  font-size: .6875rem;
  font-weight: 600;
  letter-spacing: .06em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.modal-stats dd {
  margin: 2px 0 0;
  font-size: 1.05rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.modal-actions { display: flex; gap: 8px; justify-content: center; }
.modal-actions .btn { flex: 1; }

@media (max-width: 560px) {
  .stats { grid-template-columns: repeat(2, 1fr); }
  .topbar { gap: 10px; }
  .controls { width: 100%; }
  .segmented { flex: 1 1 auto; }
  .seg { flex: 1 1 0; }
  .select-wrap { flex: 0 0 auto; }
  #restart { flex: 1 1 100%; justify-content: center; }
}

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