/* ===== Reset & Base ===== */
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg-grad: linear-gradient(135deg, #ff9a9e 0%, #fecfef 50%, #a1c4fd 100%);
  --card-face: linear-gradient(145deg, #ff6b6b, #ee5a24);
  --card-face-hover: linear-gradient(145deg, #ff7b6b, #ff8a65);
  --text-dark: #5b3a8e;
  --shadow-soft: 0 8px 24px rgba(0, 0, 0, 0.15);
  --font: 'Comic Sans MS', 'Chalkboard SE', 'Baloo 2', 'Segoe UI', system-ui, sans-serif;
}

html, body {
  height: 100%;
  font-family: var(--font);
  color: var(--text-dark);
  overflow-x: hidden;
}

body {
  background: var(--bg-grad);
  background-attachment: fixed;
  min-height: 100dvh;
  touch-action: manipulation;
}

body.confetti-rain::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 60;
  background-image:
    radial-gradient(circle, #ffd93d 6px, transparent 6px),
    radial-gradient(circle, #6bcb77 5px, transparent 5px),
    radial-gradient(circle, #4d96ff 7px, transparent 7px),
    radial-gradient(circle, #ff6b6b 5px, transparent 5px),
    radial-gradient(circle, #f9f871 6px, transparent 6px);
  background-size: 180px 180px, 240px 240px, 210px 210px, 260px 260px, 150px 150px;
  background-position: 0 0, 40px 60px, 120px 30px, 80px 120px, 20px 90px;
  animation: confettiFall 3.5s linear infinite;
  opacity: 0;
  transition: opacity 0.3s;
}

body.confetti-rain-on::before {
  opacity: 1;
}

@keyframes confettiFall {
  0% { transform: translateY(-10vh); }
  100% { transform: translateY(110vh); }
}

/* ===== Layout ===== */
.app {
  max-width: 720px;
  margin: 0 auto;
  padding: 12px;
  padding-top: max(12px, env(safe-area-inset-top));
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

main {
  flex: 1;
  display: flex;
  flex-direction: column;
}

.screen {
  display: none;
  flex-direction: column;
  flex: 1;
  animation: fadeIn 0.4s ease;
}

.screen.active {
  display: flex;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ===== Topbar / HUD ===== */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-bottom: 12px;
}

.logo {
  font-size: clamp(1.05rem, 4.5vw, 1.5rem);
  font-weight: 900;
  text-shadow: 2px 2px 0 rgba(255, 255, 255, 0.6);
  letter-spacing: 0.5px;
  flex: 0 0 auto;
}

.hud {
  display: flex;
  gap: 8px;
  margin-left: auto;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.hud-item {
  background: rgba(255, 255, 255, 0.85);
  border-radius: 999px;
  padding: 6px 12px;
  font-weight: 800;
  font-size: 0.95rem;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.08);
  display: flex;
  align-items: center;
  gap: 5px;
  white-space: nowrap;
}

.hud-stars {
  font-size: 1rem;
  letter-spacing: 2px;
  color: #f7b500;
}

.icon-btn {
  background: rgba(255, 255, 255, 0.85);
  border: none;
  border-radius: 12px;
  width: 44px;
  height: 44px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  cursor: pointer;
  box-shadow: 0 3px 8px rgba(0, 0, 0, 0.08);
  transition: transform 0.15s;
}

.icon-btn:active {
  transform: scale(0.92);
}

.icon-btn span {
  width: 20px;
  height: 3px;
  background: var(--text-dark);
  border-radius: 3px;
}

/* ===== Home screen ===== */
.screen-home {
  justify-content: center;
  align-items: center;
  gap: 20px;
  text-align: center;
}

.hero {
  animation: bounceIn 0.6s ease;
}

@keyframes bounceIn {
  0% { transform: scale(0.6); opacity: 0; }
  60% { transform: scale(1.08); }
  100% { transform: scale(1); opacity: 1; }
}

.hero-emoji {
  font-size: 5rem;
  display: inline-block;
  animation: wiggle 2.4s ease-in-out infinite;
  filter: drop-shadow(0 6px 8px rgba(0, 0, 0, 0.18));
}

@keyframes wiggle {
  0%, 100% { transform: rotate(-6deg); }
  50% { transform: rotate(6deg); }
}

.hero h2 {
  font-size: clamp(1.6rem, 6vw, 2.2rem);
  color: #fff;
  text-shadow: 2px 3px 0 rgba(91, 58, 142, 0.35);
  margin-top: 6px;
}

.hero p {
  font-size: 1.05rem;
  color: rgba(255, 255, 255, 0.95);
  font-weight: 700;
  text-shadow: 1px 1px 0 rgba(91, 58, 142, 0.25);
}

.level-picker {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
}

.level-btn {
  background: rgba(255, 255, 255, 0.9);
  border: 3px solid transparent;
  border-radius: 20px;
  padding: 12px 16px;
  min-width: 110px;
  cursor: pointer;
  font-family: var(--font);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  box-shadow: var(--shadow-soft);
  transition: transform 0.15s, border-color 0.15s;
}

.level-btn:hover { transform: translateY(-3px); }

.level-btn.selected {
  border-color: #ffd93d;
  background: #fff;
  box-shadow: 0 0 0 4px rgba(255, 217, 61, 0.35), var(--shadow-soft);
}

.level-emoji { font-size: 2.2rem; }

.level-name { font-weight: 900; color: var(--text-dark); font-size: 1.05rem; }

.level-desc { font-size: 0.8rem; color: #8a6fb0; font-weight: 700; }

.btn-start {
  background: linear-gradient(145deg, #ffb347, #ff6b6b);
  border: none;
  color: #fff;
  font-family: var(--font);
  font-size: 1.3rem;
  font-weight: 900;
  padding: 14px 46px;
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 0 6px 0 #d64541, 0 12px 20px rgba(0, 0, 0, 0.2);
  transition: transform 0.1s, box-shadow 0.1s;
  text-shadow: 1px 2px 0 rgba(0, 0, 0, 0.15);
}

.btn-start:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 #d64541, 0 6px 12px rgba(0, 0, 0, 0.15);
}

.btn-ghost {
  background: rgba(255, 255, 255, 0.55);
  border: 2px solid rgba(255, 255, 255, 0.9);
  color: var(--text-dark);
  font-family: var(--font);
  font-size: 1rem;
  font-weight: 800;
  padding: 10px 26px;
  border-radius: 999px;
  cursor: pointer;
  transition: background 0.15s;
}

.btn-ghost:hover { background: rgba(255, 255, 255, 0.8); }

/* ===== Board ===== */
.screen-game {
  gap: 10px;
}

.board {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: clamp(6px, 1.5vw, 12px);
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
}

.board[data-pairs="6"] { grid-template-columns: repeat(3, 1fr); max-width: 420px; }
.board[data-pairs="8"] { grid-template-columns: repeat(4, 1fr); max-width: 480px; }

.card {
  aspect-ratio: 3/4;
  perspective: 700px;
  cursor: pointer;
}

.card-inner {
  position: relative;
  width: 100%;
  height: 100%;
  transform-style: preserve-3d;
  transition: transform 0.45s cubic-bezier(0.4, 0.2, 0.2, 1);
}

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

.card.matched {
  pointer-events: none;
}

.card.matched .card-inner {
  transform: rotateY(180deg) scale(0.94);
}

.card-face {
  position: absolute;
  inset: 0;
  border-radius: 16px;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.card-back {
  background: var(--card-face);
  box-shadow: inset 0 -6px 0 rgba(0, 0, 0, 0.12), var(--shadow-soft);
}

.card-back::after {
  content: '?';
  font-size: clamp(1.4rem, 5vw, 2.2rem);
  font-weight: 900;
  color: rgba(255, 255, 255, 0.9);
  text-shadow: 1px 2px 0 rgba(0, 0, 0, 0.2);
}

.card-back::before {
  content: '';
  position: absolute;
  inset: 8px;
  border: 3px dashed rgba(255, 255, 255, 0.45);
  border-radius: 12px;
}

.card:not(.flipped):not(.matched):hover .card-back {
  background: var(--card-face-hover);
}

.card-front {
  background: #fff;
  transform: rotateY(180deg);
  box-shadow: var(--shadow-soft);
}

.card-front .emoji {
  font-size: clamp(2rem, 8vw, 3.4rem);
  animation: pop 0.4s ease;
  filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.15));
}

@keyframes pop {
  0% { transform: scale(0.3); }
  70% { transform: scale(1.2); }
  100% { transform: scale(1); }
}

.card.matched .card-front {
  background: linear-gradient(145deg, #c8f7c5, #a5e8a0);
}

.card.matched .card-front::after {
  content: '✓';
  position: absolute;
  top: 4px;
  right: 8px;
  font-size: 1.1rem;
  color: #2e9e44;
  font-weight: 900;
}

.card.matched .card-front .emoji {
  animation: matchBounce 0.6s ease;
}

@keyframes matchBounce {
  0% { transform: scale(1); }
  40% { transform: scale(1.35) rotate(-8deg); }
  70% { transform: scale(0.95) rotate(5deg); }
  100% { transform: scale(1); }
}

.card.matched::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 16px;
  animation: glowPulse 1.2s ease infinite;
  z-index: 5;
}

@keyframes glowPulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 215, 61, 0.8); }
  50% { box-shadow: 0 0 0 10px rgba(255, 215, 61, 0); }
}

.card.wrong {
  animation: shake 0.45s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-7px); }
  40% { transform: translateX(7px); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(5px); }
}

.card.dim .card-inner {
  filter: brightness(0.6) saturate(0.7);
  transition: filter 0.4s;
}

/* ===== Win screen ===== */
.screen-win {
  justify-content: center;
  align-items: center;
  text-align: center;
}

.win-card {
  background: rgba(255, 255, 255, 0.92);
  border-radius: 28px;
  padding: 28px 24px;
  box-shadow: var(--shadow-soft);
  animation: bounceIn 0.6s ease;
  width: 100%;
  max-width: 420px;
}

.win-emoji { font-size: 4.5rem; animation: wiggle 1.6s ease-in-out infinite; }

.win-card h2 {
  font-size: 1.6rem;
  margin: 8px 0 16px;
}

.win-stats {
  display: flex;
  justify-content: center;
  gap: 14px;
  margin-bottom: 20px;
}

.stat {
  background: linear-gradient(145deg, #fdf6ff, #fff);
  border-radius: 16px;
  padding: 12px 10px;
  min-width: 92px;
  box-shadow: inset 0 0 0 2px #f0e3ff;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat-icon { font-size: 1.4rem; }

.stat-value { font-weight: 900; font-size: 1.05rem; color: var(--text-dark); }

.stat-label { font-size: 0.72rem; color: #8a6fb0; font-weight: 700; }

#winStars { color: #f7b500; letter-spacing: 3px; font-size: 1.1rem; }

.win-btns { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }

/* ===== Drawer ===== */
.drawer {
  position: fixed;
  inset: 0;
  z-index: 100;
  visibility: hidden;
  pointer-events: none;
}

.drawer.open { visibility: visible; pointer-events: auto; }

.drawer-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  opacity: 0;
  transition: opacity 0.25s;
}

.drawer.open .drawer-backdrop { opacity: 1; }

.drawer-sheet {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: min(320px, 86vw);
  background: #fff;
  padding: 22px 18px;
  transform: translateX(100%);
  transition: transform 0.28s ease;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.18);
}

.drawer.open .drawer-sheet { transform: translateX(0); }

.drawer-close {
  align-self: flex-end;
  background: #f0e3ff;
  border: none;
  border-radius: 50%;
  width: 38px;
  height: 38px;
  font-size: 1rem;
  cursor: pointer;
  color: var(--text-dark);
}

.drawer h3 { font-size: 1.25rem; }

.drawer-row { display: flex; flex-direction: column; gap: 10px; }

.switch-label {
  display: flex;
  align-items: center;
  gap: 10px;
  font-weight: 800;
  cursor: pointer;
  font-size: 1rem;
}

.switch-label input { display: none; }

.switch {
  width: 52px;
  height: 28px;
  background: #d5c6e8;
  border-radius: 999px;
  position: relative;
  transition: background 0.2s;
  flex: 0 0 auto;
}

.switch::after {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 22px;
  height: 22px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2);
}

.switch-label input:checked + .switch { background: #6bcb77; }

.switch-label input:checked + .switch::after { transform: translateX(24px); }

.drawer-btn { width: 100%; }

/* ===== Toast ===== */
.toast {
  position: fixed;
  left: 50%;
  bottom: 30px;
  transform: translateX(-50%) translateY(80px);
  background: rgba(91, 58, 142, 0.95);
  color: #fff;
  padding: 12px 22px;
  border-radius: 999px;
  font-weight: 800;
  box-shadow: var(--shadow-soft);
  opacity: 0;
  transition: transform 0.3s, opacity 0.3s;
  z-index: 120;
  max-width: 90vw;
  text-align: center;
}

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

/* ===== Responsive ===== */
@media (max-width: 480px) {
  .app { padding: 8px; }
  .hud-item { font-size: 0.8rem; padding: 5px 9px; }
  .logo { font-size: 1rem; }
  .level-btn { min-width: 92px; padding: 10px 8px; }
  .level-emoji { font-size: 1.8rem; }
  .card-face { border-radius: 12px; }
  .card-back::before { inset: 5px; }
}

@media (min-width: 600px) {
  .board[data-pairs="10"] .card-front .emoji { font-size: 3.4rem; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
