:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface-2: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #8b5cf6;
  --accent-2: #ec4899;
  --success: #22c55e;
  --danger: #f43f5e;
  --radius: 14px;
  color-scheme: dark;
}

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

body {
  margin: 0;
  min-height: 100vh;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  color: var(--text);
  background: radial-gradient(circle at top, #312e81 0%, var(--bg) 55%);
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

.app {
  max-width: 640px;
  margin: 0 auto;
  padding: 1.5rem 1rem 3rem;
  display: grid;
  gap: 1.25rem;
}

.header {
  text-align: center;
}

.header h1 {
  margin: 0;
  font-size: clamp(1.8rem, 6vw, 2.6rem);
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.tagline {
  margin: 0.25rem 0 0;
  color: var(--muted);
}

/* Controls */
.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  justify-content: center;
  align-items: center;
}

.levels {
  display: inline-flex;
  margin: 0;
  padding: 4px;
  border: 0;
  border-radius: 999px;
  background: var(--surface);
}

.levels input {
  position: absolute;
  opacity: 0;
}

.levels span {
  display: block;
  padding: 0.5rem 1rem;
  border-radius: 999px;
  cursor: pointer;
  color: var(--muted);
  transition: background 0.2s, color 0.2s;
}

.levels input:checked + span {
  background: var(--surface-2);
  color: var(--text);
}

.levels input:focus-visible + span {
  outline: 2px solid var(--accent);
}

.btn {
  font: inherit;
  font-weight: 600;
  padding: 0.6rem 1.3rem;
  border: 0;
  border-radius: 999px;
  cursor: pointer;
  color: var(--text);
  background: var(--surface-2);
  transition: transform 0.1s, filter 0.2s;
}

.btn:hover {
  filter: brightness(1.15);
}

.btn:active {
  transform: scale(0.97);
}

.btn-primary {
  background: linear-gradient(90deg, var(--accent), var(--accent-2));
}

/* HUD */
.hud {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.75rem;
}

.hud-item {
  display: grid;
  justify-items: center;
  padding: 0.6rem;
  border-radius: var(--radius);
  background: var(--surface);
}

.hud-label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.hud-value {
  font-size: 1.4rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.hud-value.low {
  color: var(--danger);
  animation: pulse 1s infinite;
}

.hint {
  min-height: 1.2em;
  margin: 0;
  text-align: center;
  color: var(--muted);
}

/* Board */
.board {
  --size: 4;
  display: grid;
  grid-template-columns: repeat(var(--size), 1fr);
  gap: clamp(6px, 1.5vw, 12px);
  transition: opacity 0.3s;
}

.board.inactive .card[data-state='hidden'] {
  opacity: 0.6;
}

.card {
  aspect-ratio: 1;
  padding: 0;
  border: 0;
  background: none;
  perspective: 600px;
  cursor: pointer;
  font-size: clamp(1.4rem, calc(28vw / var(--size)), 2.8rem);
}

.card:disabled {
  cursor: default;
}

.card:focus-visible {
  outline: 3px solid var(--accent-2);
  outline-offset: 2px;
  border-radius: 10px;
}

.card-inner {
  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.4s ease;
}

.card[data-state='visible'] .card-inner,
.card[data-state='matched'] .card-inner {
  transform: rotateY(180deg);
}

.card-face {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: 10px;
  backface-visibility: hidden;
  box-shadow: 0 4px 12px rgb(0 0 0 / 0.35);
}

.card-back {
  background:
    repeating-linear-gradient(45deg, rgb(255 255 255 / 0.08) 0 6px, transparent 6px 12px),
    linear-gradient(135deg, var(--accent), var(--accent-2));
}

.card:not(:disabled):hover .card-back {
  filter: brightness(1.15);
}

.card-front {
  background: #f8fafc;
  transform: rotateY(180deg);
}

.card[data-state='matched'] .card-front {
  background: #dcfce7;
  box-shadow: 0 0 0 3px var(--success);
}

.card[data-state='matched'] {
  animation: pop 0.4s ease 0.3s;
}

.card[data-state='matched'] .card-inner {
  opacity: 0.75;
  transition: transform 0.4s ease, opacity 0.6s ease 0.6s;
}

/* Stats */
.stats h2 {
  margin: 0 0 0.5rem;
  font-size: 1.1rem;
}

.stats table {
  width: 100%;
  border-collapse: collapse;
  overflow: hidden;
  border-radius: var(--radius);
  background: var(--surface);
  font-variant-numeric: tabular-nums;
}

.stats th,
.stats td {
  padding: 0.55rem 0.6rem;
  text-align: center;
}

.stats th {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  background: var(--surface-2);
}

.stats tbody tr + tr td {
  border-top: 1px solid var(--surface-2);
}

/* Dialog */
.dialog {
  width: min(90vw, 380px);
  padding: 1.75rem;
  border: 0;
  border-radius: 20px;
  text-align: center;
  color: var(--text);
  background: var(--surface);
  box-shadow: 0 20px 60px rgb(0 0 0 / 0.5);
}

.dialog::backdrop {
  background: rgb(15 23 42 / 0.7);
  backdrop-filter: blur(3px);
}

.dialog[open] {
  animation: pop 0.3s ease;
}

.dialog-emoji {
  margin: 0;
  font-size: 3rem;
}

.dialog h2 {
  margin: 0.25rem 0;
}

.dialog-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: center;
  margin-top: 1.25rem;
}

@keyframes pop {
  50% {
    transform: scale(1.08);
  }
}

@keyframes pulse {
  50% {
    opacity: 0.55;
  }
}

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