/* ── Reset & Root Variables ───────────────────────────── */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #1a1040;
  --surface: #231654;
  --surface2: #2e1d6e;
  --border: #4a3a8a;
  --accent: #7c3aed;
  --accent2: #a855f7;
  --gold: #f59e0b;
  --green: #10b981;
  --red: #ef4444;
  --text: #f0f0ff;
  --muted: #9d8ec4;
  --card-back: linear-gradient(135deg, #3b2f8a 0%, #5b3fbf 50%, #3b2f8a 100%);
}

/* ── Body & Atmospheric Background ───────────────────── */
body {
  font-family: 'Syne', sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
  position: relative;
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  background:
    radial-gradient(ellipse 80% 50% at 20% 10%, rgba(124,58,237,0.12) 0%, transparent 60%),
    radial-gradient(ellipse 60% 40% at 80% 90%, rgba(168,85,247,0.08) 0%, transparent 60%);
  pointer-events: none;
  z-index: 0;
}

/* ── Layout Container ─────────────────────────────────── */
.container {
  position: relative;
  z-index: 1;
  max-width: 900px;
  margin: 0 auto;
  padding: 24px 16px 40px;
}

/* ── Header ───────────────────────────────────────────── */
header {
  text-align: center;
  margin-bottom: 28px;
}

.game-title {
  font-size: clamp(2rem, 6vw, 3.5rem);
  font-weight: 800;
  letter-spacing: -2px;
  background: linear-gradient(135deg, #fff 30%, var(--accent2) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  line-height: 1;
}

.game-subtitle {
  font-family: 'DM Mono', monospace;
  font-size: 0.7rem;
  color: var(--muted);
  letter-spacing: 4px;
  text-transform: uppercase;
  margin-top: 6px;
}

/* ── Difficulty Selector ──────────────────────────────── */
.difficulty-bar {
  display: flex;
  gap: 8px;
  justify-content: center;
  margin-bottom: 20px;
}

.diff-btn {
  font-family: 'DM Mono', monospace;
  font-size: 0.82rem;
  font-weight: 500;
  letter-spacing: 1.5px;
  padding: 9px 22px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: all 0.2s;
  text-transform: uppercase;
}

.diff-btn:hover {
  border-color: var(--accent);
  color: var(--text);
}

.diff-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

/* ── Stats Bar ────────────────────────────────────────── */
.stats-bar {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
  margin-bottom: 20px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 16px 28px;
  text-align: center;
  min-width: 115px;
  transition: border-color 0.3s;
  position: relative;
  overflow: hidden;
}

.stat-card::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 2px;
  background: var(--accent);
  opacity: 0.6;
}

.stat-card.highlight::before { background: var(--gold); }
.stat-card.green::before     { background: var(--green); }

.stat-label {
  font-family: 'DM Mono', monospace;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 3px;
  text-transform: uppercase;
  color: var(--muted);
  margin-bottom: 6px;
}

.stat-value {
  font-size: 2rem;
  font-weight: 900;
  color: var(--text);
  font-family: 'Orbitron', sans-serif;
  letter-spacing: 1px;
  font-variant-numeric: tabular-nums;
  transition: color 0.3s;
}

.stat-card.highlight .stat-value { color: var(--gold); }
.stat-card.green .stat-value     { color: var(--green); }

/* Timer color states */
.timer-warning { color: var(--gold) !important; }
.timer-danger  { color: var(--red) !important; }

/* ── Progress Bar ─────────────────────────────────────── */
.progress-wrap {
  background: var(--surface);
  border-radius: 999px;
  height: 4px;
  margin-bottom: 20px;
  overflow: hidden;
  border: 1px solid var(--border);
}

.progress-bar {
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  border-radius: 999px;
  transition: width 0.5s ease;
  box-shadow: 0 0 8px var(--accent2);
  width: 0%;
}

/* ── Game Grid ────────────────────────────────────────── */
.game-grid {
  display: grid;
  gap: 10px;
  justify-content: center;
  margin-bottom: 24px;
}

.game-grid.grid-4x4 {
  grid-template-columns: repeat(4, 1fr);
  max-width: 520px;
  margin-inline: auto;
}

.game-grid.grid-4x5 {
  grid-template-columns: repeat(5, 1fr);
  max-width: 600px;
  margin-inline: auto;
}

.game-grid.grid-4x6 {
  grid-template-columns: repeat(6, 1fr);
  max-width: 680px;
  margin-inline: auto;
}

/* ── Card ─────────────────────────────────────────────── */
.card {
  aspect-ratio: 1;
  perspective: 800px;
  cursor: pointer;
}

.card-inner {
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.45s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  border-radius: 12px;
}

.card.flipped .card-inner,
.card.matched .card-inner {
  transform: rotateY(180deg);
}

.card-face {
  position: absolute;
  inset: 0;
  border-radius: 12px;
  backface-visibility: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(1.4rem, 4vw, 2rem);
  user-select: none;
}

/* Card Back */
.card-back {
  background: linear-gradient(135deg, #2d1b69 0%, #4c2fa8 50%, #2d1b69 100%);
  border: 2px solid rgba(168,85,247,0.5);
  box-shadow: 0 4px 16px rgba(76,47,168,0.4), inset 0 0 20px rgba(124,58,237,0.15);
  position: relative;
  overflow: hidden;
}

.card-back::before {
  content: '';
  position: absolute;
  inset: 4px;
  border: 1px solid rgba(168,85,247,0.15);
  border-radius: 9px;
}

.card-back::after {
  content: '✦';
  font-size: 1.2rem;
  color: rgba(168,85,247,0.4);
}

/* Card Front */
.card-front {
  transform: rotateY(180deg);
  background: linear-gradient(135deg, #4c1d95, #6d28d9);
  border: 2px solid rgba(168,85,247,0.6);
  box-shadow: 0 4px 20px rgba(109,40,217,0.5);
}

/* Matched Card */
.card.matched .card-front {
  background: linear-gradient(135deg, #064e3b, #065f46);
  border-color: var(--green);
  box-shadow: 0 0 20px rgba(16,185,129,0.3);
  animation: matchPulse 0.5s ease-out;
}

/* Wrong Card */
.card.wrong .card-inner {
  animation: wrongShake 0.4s ease-out;
}

/* Card Hover */
.card:not(.flipped):not(.matched):hover .card-inner {
  transform: rotateY(10deg) scale(1.04);
  box-shadow: 0 8px 30px rgba(124,58,237,0.3);
}

/* ── Animations ───────────────────────────────────────── */
@keyframes matchPulse {
  0%   { transform: rotateY(180deg) scale(1); }
  50%  { transform: rotateY(180deg) scale(1.08); }
  100% { transform: rotateY(180deg) scale(1); }
}

@keyframes wrongShake {
  0%, 100% { transform: rotateY(180deg) translateX(0); }
  20%       { transform: rotateY(180deg) translateX(-5px); }
  40%       { transform: rotateY(180deg) translateX(5px); }
  60%       { transform: rotateY(180deg) translateX(-4px); }
  80%       { transform: rotateY(180deg) translateX(4px); }
}

@keyframes confettiFall {
  0%   { transform: translateY(-20px) rotate(0deg); opacity: 1; }
  100% { transform: translateY(110vh) rotate(720deg); opacity: 0; }
}

/* ── Buttons ──────────────────────────────────────────── */
.controls {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

.btn {
  font-family: 'Syne', sans-serif;
  font-weight: 700;
  font-size: 0.85rem;
  padding: 11px 28px;
  border-radius: 10px;
  border: none;
  cursor: pointer;
  transition: all 0.2s;
  letter-spacing: 0.5px;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 4px 20px rgba(124,58,237,0.4);
}

.btn-primary:hover {
  background: var(--accent2);
  transform: translateY(-2px);
  box-shadow: 0 6px 24px rgba(124,58,237,0.5);
}

.btn-outline {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}

.btn-outline:hover {
  border-color: var(--accent);
  color: var(--text);
}

/* ── Disabled Button State ────────────────────────────── */
/* Applied to Hint button after use, and during the preview phase */
.btn:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  pointer-events: none;
  transform: none !important;
  box-shadow: none !important;
}

/* ── Win Modal ────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  backdrop-filter: blur(8px);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.4s;
}

.modal-overlay.visible {
  opacity: 1;
  pointer-events: all;
}

.modal {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 40px;
  text-align: center;
  max-width: 380px;
  width: 90%;
  transform: scale(0.85);
  transition: transform 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  position: relative;
  overflow: hidden;
}

.modal-overlay.visible .modal {
  transform: scale(1);
}

.modal::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--accent), var(--gold), var(--accent2));
}

.modal-emoji {
  font-size: 3.5rem;
  margin-bottom: 12px;
  display: block;
}

.modal-title {
  font-size: 1.8rem;
  font-weight: 800;
  margin-bottom: 8px;
}

.modal-sub {
  color: var(--muted);
  font-size: 0.85rem;
  margin-bottom: 24px;
  font-family: 'DM Mono', monospace;
}

.modal-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 24px;
}

.modal-stat {
  background: var(--bg);
  border-radius: 10px;
  padding: 12px;
  border: 1px solid var(--border);
}

.modal-stat-label {
  font-size: 0.6rem;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: var(--muted);
  font-family: 'DM Mono', monospace;
}

.modal-stat-val {
  font-size: 1.4rem;
  font-weight: 800;
  color: var(--gold);
}

.new-best {
  background: linear-gradient(135deg, rgba(245,158,11,0.15), rgba(245,158,11,0.05));
  border: 1px solid rgba(245,158,11,0.3);
  border-radius: 8px;
  padding: 8px 16px;
  font-size: 0.75rem;
  color: var(--gold);
  margin-bottom: 20px;
  font-family: 'DM Mono', monospace;
  letter-spacing: 1px;
}

/* ── Toast ────────────────────────────────────────────── */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 20px;
  font-family: 'DM Mono', monospace;
  font-size: 0.8rem;
  z-index: 200;
  transition: transform 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
  white-space: nowrap;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
}

/* ── Confetti ─────────────────────────────────────────── */
.confetti-container {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 90;
  overflow: hidden;
}

.confetti-particle {
  position: absolute;
  width: 8px;
  height: 8px;
  border-radius: 2px;
  animation: confettiFall linear forwards;
  opacity: 0;
}

/* ── Responsive ───────────────────────────────────────── */
@media (max-width: 560px) {
  .game-grid.grid-4x4 { gap: 7px; }
  .game-grid.grid-4x6 { grid-template-columns: repeat(4, 1fr); }
  .stat-card { min-width: 85px; padding: 12px 16px; }
  .stat-value { font-size: 1.6rem; }
  .stat-label { font-size: 0.68rem; }
}
.btn-outline:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  pointer-events: none;
}