/* ════════════════════════════════════════════════════════
   Memory — equation/answer matching game
   ════════════════════════════════════════════════════════ */

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

html, body {
  height: 100vh;
  min-height: 100vh;
  width: 100vw;
  overflow: hidden;
  font-family: 'Fredoka', system-ui, -apple-system, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  color: #fff;
}

body {
  position: relative;
  background: transparent;
  display: flex;
  flex-direction: column;
}

/* ─── Night sky ────────────────────────────────────────── */
.sky {
  position: fixed;
  inset: 0;
  background: linear-gradient(
    180deg,
    #1a1248 0%,
    #2a1f5e 22%,
    #3a2a78 42%,
    #4d3a8c 60%,
    #5a4a9c 78%,
    #6c5fb0 92%,
    #8068c4 100%
  );
  overflow: hidden;
  z-index: -2;
}

.moon {
  position: absolute;
  top: 6%;
  right: 8%;
  font-size: 80px;
  filter: drop-shadow(0 0 30px rgba(255, 250, 200, 0.6));
  animation: moonGlow 4s ease-in-out infinite;
  transform: rotate(-15deg);
}
@keyframes moonGlow {
  0%, 100% { filter: drop-shadow(0 0 30px rgba(255, 250, 200, 0.6)); }
  50%      { filter: drop-shadow(0 0 50px rgba(255, 250, 200, 0.9)); }
}

.hills {
  position: absolute;
  bottom: 0;
  left: 0; right: 0;
  height: 22%;
  background:
    radial-gradient(ellipse 60% 100% at 20% 100%, #2a1f5e 0%, #2a1f5e 65%, transparent 70%),
    radial-gradient(ellipse 50% 80% at 75% 100%, #3a2a78 0%, #3a2a78 70%, transparent 75%),
    radial-gradient(ellipse 45% 70% at 50% 100%, #1a1248 0%, #1a1248 70%, transparent 75%);
  pointer-events: none;
}

.stars {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.star {
  position: absolute;
  width: var(--sz, 2px);
  height: var(--sz, 2px);
  background: #fff;
  border-radius: 50%;
  box-shadow: 0 0 var(--glow, 4px) rgba(255, 255, 255, 0.7);
  animation: twinkle var(--dur, 3s) ease-in-out infinite;
  opacity: 0.5;
}
@keyframes twinkle {
  0%, 100% { opacity: 0.2; transform: scale(0.8); }
  50%      { opacity: 1;   transform: scale(1.2); }
}

.fireflies {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.firefly {
  position: absolute;
  width: 6px;
  height: 6px;
  background: #ffe066;
  border-radius: 50%;
  box-shadow: 0 0 12px 4px rgba(255, 224, 102, 0.55);
  animation: fireflyDrift var(--dur, 12s) ease-in-out infinite;
  opacity: 0.4;
}
@keyframes fireflyDrift {
  0%, 100% { transform: translate(0, 0); opacity: 0.3; }
  20%      { transform: translate(30px, -20px); opacity: 0.9; }
  40%      { transform: translate(-15px, -40px); opacity: 0.6; }
  60%      { transform: translate(20px, -10px); opacity: 1; }
  80%      { transform: translate(-25px, 20px); opacity: 0.5; }
}

/* ─── HUD ──────────────────────────────────────────────── */
.hud {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 16px;
  z-index: 10;
}
.hud-side { flex: 0 0 auto; min-width: 96px; }
.hud-side--right { display: flex; justify-content: flex-end; }
.hud-center { flex: 1 1 auto; display: flex; justify-content: center; }

.stat-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(255, 255, 255, 0.92);
  padding: 8px 16px;
  border: 3px solid #fff;
  border-radius: 999px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
  font-size: 18px;
  font-weight: 700;
  color: #2a1f5e;
  font-variant-numeric: tabular-nums;
  min-width: 76px;
  justify-content: center;
}
.pill-icon { font-size: 18px; }
.pill-num--bump {
  animation: numBump 0.32s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes numBump {
  0%   { transform: scale(1); }
  50%  { transform: scale(1.4); color: #ff7eb3; }
  100% { transform: scale(1); }
}

.mode-tabs, .size-tabs {
  display: inline-flex;
  background: rgba(255, 255, 255, 0.85);
  border: 3px solid #fff;
  border-radius: 999px;
  padding: 4px;
  gap: 2px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.3);
}
.mode-btn, .size-btn {
  font-family: inherit;
  font-weight: 700;
  font-size: 15px;
  color: #5a4a9c;
  background: transparent;
  border: none;
  padding: 6px 12px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.18s ease, color 0.18s ease, transform 0.12s ease;
  white-space: nowrap;
}
.mode-btn:active, .size-btn:active { transform: scale(0.94); }
.mode-btn--active, .size-btn--active {
  background: linear-gradient(180deg, #c084fc, #9333ea);
  color: #fff;
  box-shadow: 0 2px 6px rgba(147, 51, 234, 0.6), inset 0 -2px 0 rgba(0,0,0,0.1);
  text-shadow: 0 1px 0 rgba(0,0,0,0.2);
}

.size-btn { padding: 6px 10px; font-size: 14px; min-width: 32px; text-align: center; }

/* ─── Progress row ─────────────────────────────────────── */
.progress-row {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  margin: 4px 16px 12px;
  z-index: 5;
}
.progress-label {
  font-size: 14px;
  font-weight: 600;
  color: rgba(255, 255, 255, 0.85);
  letter-spacing: 0.02em;
}
.progress-track {
  width: 240px;
  max-width: 80vw;
  height: 8px;
  background: rgba(0, 0, 0, 0.3);
  border-radius: 999px;
  overflow: hidden;
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.4);
}
.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #c084fc, #f472b6, #fbbf24);
  border-radius: 999px;
  transition: width 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
  box-shadow: 0 0 8px rgba(192, 132, 252, 0.6);
}

/* ─── Board ────────────────────────────────────────────── */
.board-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 12px;
  min-height: 0;
}

.board {
  display: grid;
  grid-template-columns: repeat(var(--cols, 4), 1fr);
  gap: 12px;
  width: 760px;
  max-width: calc(100% - 16px);
}

/* ─── Card ─────────────────────────────────────────────── */
.card {
  position: relative;
  /* aspect-ratio + a min-height fallback for environments that don't
     compute aspect-ratio from grid track widths reliably */
  aspect-ratio: 3 / 4;
  min-height: 110px;
  cursor: pointer;
  perspective: 1000px;
  background: transparent;
  border: none;
  padding: 0;
  font-family: inherit;
  transition: transform 0.2s ease;
}
.card:active { transform: scale(0.96); }

.card-inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform 0.55s cubic-bezier(0.4, 0, 0.2, 1);
  border-radius: 16px;
}
.card--flipped .card-inner,
.card--matched .card-inner { transform: rotateY(180deg); }

.card-face {
  position: absolute;
  inset: 0;
  border-radius: 16px;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.35);
  overflow: hidden;
}

/* Card back: deep teal-purple with star/moon ornament */
.card-back {
  background:
    radial-gradient(circle at 50% 50%, #4d3a8c 0%, #2a1f5e 70%, #1a1248 100%);
  border: 3px solid #6c5fb0;
  /* (do NOT set position here — would override .card-face's absolute) */
}
.card-back::before {
  content: '';
  position: absolute;
  inset: 6px;
  border-radius: 12px;
  border: 2px dashed rgba(255, 255, 255, 0.18);
  pointer-events: none;
}
.card-back::after {
  content: '✦';
  font-size: 48px;
  color: rgba(255, 255, 255, 0.18);
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  text-shadow: 0 0 20px rgba(255, 240, 180, 0.4);
  animation: backTwinkle 3s ease-in-out infinite;
}
@keyframes backTwinkle {
  0%, 100% { opacity: 0.3; transform: translate(-50%, -50%) scale(1) rotate(0deg); }
  50%      { opacity: 0.7; transform: translate(-50%, -50%) scale(1.15) rotate(20deg); }
}

/* Decorative tiny stars on card back */
.card-back-star {
  position: absolute;
  width: 4px; height: 4px;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 50%;
  box-shadow: 0 0 4px rgba(255, 255, 255, 0.7);
}
.card-back-star.s1 { top: 18%; left: 22%; }
.card-back-star.s2 { top: 28%; right: 18%; }
.card-back-star.s3 { bottom: 24%; left: 28%; }
.card-back-star.s4 { bottom: 18%; right: 22%; }

/* Card front: parchment-style cream with content */
.card-front {
  transform: rotateY(180deg);
  background: linear-gradient(180deg, #fff8e0 0%, #ffeeb8 100%);
  color: #2a1f5e;
  border: 3px solid #ffd96e;
  padding: 10px;
  text-align: center;
  position: relative;
}

/* Equation cards have warm orange accent */
.card--eq .card-front {
  border-color: #ff9b6a;
  background: linear-gradient(180deg, #fff5e6 0%, #ffd9b8 100%);
}
.card--eq .card-front::before {
  content: '';
  position: absolute;
  top: 6px; left: 6px; right: 6px;
  height: 4px;
  border-radius: 2px;
  background: linear-gradient(90deg, transparent, #ff9b6a, transparent);
}

/* Number cards have cool teal accent */
.card--num .card-front {
  border-color: #6dffe0;
  background: linear-gradient(180deg, #ecfff9 0%, #b8ffe8 100%);
}
.card--num .card-front::before {
  content: '';
  position: absolute;
  top: 6px; left: 6px; right: 6px;
  height: 4px;
  border-radius: 2px;
  background: linear-gradient(90deg, transparent, #2dd4bf, transparent);
}

.card-display {
  font-size: 30px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.04em;
  color: #2a1f5e;
  line-height: 1;
}
.card--num .card-display { font-size: 44px; color: #0f766e; }
.card--eq .card-display  { color: #c2410c; }

/* Match success state — green glow + bounce */
.card--matched .card-front {
  border-color: #4cd964;
  box-shadow:
    0 0 0 3px #4cd964,
    0 0 24px 4px rgba(76, 217, 100, 0.55),
    0 6px 16px rgba(0,0,0,0.35);
  background: linear-gradient(180deg, #d4fcd9 0%, #a8f5b0 100%);
}
.card--matched .card-display { color: #15803d; }
.card--matched {
  animation: matchBounce 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes matchBounce {
  0%   { transform: scale(1) rotate(0); }
  40%  { transform: scale(1.18) rotate(-3deg); }
  70%  { transform: scale(0.94) rotate(3deg); }
  100% { transform: scale(1) rotate(0); }
}

/* Mismatch flash — red border on the front */
.card--wrong .card-front {
  border-color: #ef4444;
  box-shadow:
    0 0 0 3px #ef4444,
    0 0 18px 2px rgba(239, 68, 68, 0.55),
    0 6px 16px rgba(0,0,0,0.35);
}
.card--wrong {
  animation: wrongShake 0.45s ease-in-out;
}
@keyframes wrongShake {
  0%, 100% { transform: translateX(0); }
  20%      { transform: translateX(-4px); }
  40%      { transform: translateX(4px); }
  60%      { transform: translateX(-3px); }
  80%      { transform: translateX(3px); }
}

/* Settled (post-match) state: subtle dim */
.card--settled .card-front {
  filter: brightness(0.92) saturate(0.85);
}

/* When the win sequence runs, all cards do a wave celebration */
.card--celebrate {
  animation: celebrate 0.8s ease-in-out;
  animation-delay: var(--delay, 0s);
}
@keyframes celebrate {
  0%   { transform: scale(1) rotate(0); }
  30%  { transform: scale(1.18) rotate(-6deg); filter: brightness(1.4); }
  60%  { transform: scale(0.94) rotate(6deg); }
  100% { transform: scale(1) rotate(0); filter: brightness(1); }
}

/* ─── Particles ────────────────────────────────────────── */
.particle-layer {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 100;
}

.spark {
  position: absolute;
  font-size: 22px;
  pointer-events: none;
  animation: sparkFly var(--dur, 1.0s) ease-out forwards;
}
@keyframes sparkFly {
  0%   { transform: translate(0, 0) scale(0.4) rotate(0); opacity: 0; }
  20%  { opacity: 1; }
  100% { transform: translate(var(--dx, 0), var(--dy, -120px)) scale(1.1) rotate(var(--rot, 360deg)); opacity: 0; }
}

.glow-ring {
  position: absolute;
  width: 80px;
  height: 80px;
  border: 4px solid rgba(192, 252, 161, 0.9);
  border-radius: 50%;
  pointer-events: none;
  animation: ringExpand 0.6s ease-out forwards;
}
@keyframes ringExpand {
  0%   { transform: scale(0.3); opacity: 1; }
  100% { transform: scale(2.5); opacity: 0; }
}

.confetti {
  position: absolute;
  width: 10px;
  height: 14px;
  pointer-events: none;
  animation: confettiFall var(--dur, 2.4s) cubic-bezier(0.5, 0.05, 0.6, 1) forwards;
}
@keyframes confettiFall {
  0%   { transform: translate(0, 0) rotate(0); opacity: 1; }
  100% { transform: translate(var(--dx, 0), var(--dy, 100vh)) rotate(var(--rot, 720deg)); opacity: 0.4; }
}

/* ─── Overlay ──────────────────────────────────────────── */
.overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 14, 50, 0.7);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
  animation: overlayFade 0.35s ease;
}
@keyframes overlayFade {
  from { opacity: 0; }
  to   { opacity: 1; }
}
.overlay[hidden] { display: none; }

.overlay-card {
  background: linear-gradient(180deg, #fff 0%, #fff8e0 100%);
  color: #2a1f5e;
  border: 4px solid #ffd96e;
  border-radius: 28px;
  padding: 28px 32px;
  text-align: center;
  box-shadow: 0 12px 40px rgba(0,0,0,0.5);
  max-width: 90vw;
  min-width: 280px;
  animation: cardPop 0.45s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes cardPop {
  from { transform: scale(0.5) translateY(30px); opacity: 0; }
  to   { transform: scale(1) translateY(0); opacity: 1; }
}
.overlay-emoji {
  font-size: 64px;
  line-height: 1;
  margin-bottom: 8px;
  filter: drop-shadow(0 4px 8px rgba(0,0,0,0.3));
  animation: emojiSpin 1.2s ease-in-out infinite;
}
@keyframes emojiSpin {
  0%, 100% { transform: rotate(-8deg) scale(1); }
  50%      { transform: rotate(8deg) scale(1.08); }
}
.overlay-title {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 18px;
  color: #2a1f5e;
}
.overlay-stats {
  display: flex;
  gap: 24px;
  justify-content: center;
  margin-bottom: 14px;
}
.stat {
  background: #f6f0ff;
  border: 3px solid #d4c2ff;
  border-radius: 16px;
  padding: 10px 18px;
  min-width: 96px;
}
.stat-label {
  font-size: 12px;
  font-weight: 600;
  color: #6b5b9c;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.stat-num {
  font-size: 36px;
  font-weight: 700;
  color: #2a1f5e;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.overlay-msg {
  font-size: 16px;
  color: #6b5b9c;
  margin-bottom: 18px;
  min-height: 1.5em;
}
.overlay-msg--new-best {
  color: #c2410c;
  font-weight: 700;
  font-size: 18px;
  animation: msgPulse 1s ease-in-out infinite;
}
@keyframes msgPulse {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.06); }
}

.big-btn {
  font-family: 'Fredoka', sans-serif;
  font-weight: 700;
  font-size: 22px;
  color: #fff;
  background: linear-gradient(180deg, #c084fc, #9333ea);
  border: none;
  padding: 14px 36px;
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 0 6px 0 rgba(0,0,0,0.18), 0 8px 16px rgba(147, 51, 234, 0.5);
  transition: transform 0.1s ease, box-shadow 0.1s ease;
  text-shadow: 0 1px 0 rgba(0,0,0,0.2);
}
.big-btn:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 rgba(0,0,0,0.18), 0 4px 8px rgba(147, 51, 234, 0.5);
}

/* ─── Responsive ───────────────────────────────────────── */
@media (max-width: 600px) {
  .hud { padding: 10px 8px; gap: 6px; }
  .hud-side { min-width: 0; }
  .stat-pill { padding: 6px 12px; font-size: 16px; min-width: 60px; }
  .pill-icon { font-size: 16px; }
  .mode-btn { font-size: 13px; padding: 5px 9px; }
  .size-btn { font-size: 13px; padding: 5px 8px; min-width: 28px; }
  .moon { font-size: 60px; top: 4%; right: 5%; }
  .progress-track { width: 200px; }
  .progress-label { font-size: 13px; }
  .board { gap: 8px; }
  .card-display { font-size: 22px; }
  .card--num .card-display { font-size: 32px; }
  .overlay-card { padding: 22px 24px; min-width: 240px; }
  .overlay-emoji { font-size: 52px; }
  .overlay-title { font-size: 22px; }
  .stat-num { font-size: 28px; }
  .stat { padding: 8px 14px; min-width: 80px; }
  .big-btn { font-size: 18px; padding: 12px 28px; }
}

@media (max-width: 380px) {
  .card-display { font-size: 18px; }
  .card--num .card-display { font-size: 26px; }
  .mode-btn { font-size: 12px; padding: 4px 7px; }
  .size-btn { font-size: 12px; padding: 4px 6px; min-width: 24px; }
}
