:root {
  --bg1: #fff6e5;
  --bg2: #e6f4ff;
  --ink: #3b3355;
  --muted: #7a7194;
  --accent: #ff7a59;
  --accent-dark: #e85d3c;
  --card-back1: #7c6cf2;
  --card-back2: #a78bfa;
  --card-face: #ffffff;
  --match: #5fd38d;
  --star: #ffc93c;
  --star-off: #e4e0ee;
  --shadow: 0 6px 0 rgba(59, 51, 85, 0.15), 0 10px 24px rgba(59, 51, 85, 0.12);
  --radius: 18px;
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font-family: "Baloo 2", "Comic Sans MS", "Trebuchet MS", system-ui, sans-serif;
  color: var(--ink);
  background: linear-gradient(160deg, var(--bg1) 0%, var(--bg2) 100%);
  background-attachment: fixed;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  overflow-x: hidden;
}

button { font-family: inherit; color: inherit; cursor: pointer; }

/* ---------- screens ---------- */
.screen { display: none; min-height: 100vh; min-height: 100dvh; }
.screen.active { display: flex; flex-direction: column; align-items: center; }

/* ---------- home ---------- */
#home { justify-content: center; padding: 24px 16px 40px; gap: 6px; }

.title {
  margin: 0;
  font-size: clamp(2.6rem, 9vw, 4.2rem);
  font-weight: 800;
  line-height: 1;
  text-align: center;
  color: var(--card-back1);
  text-shadow: 0 4px 0 rgba(124, 108, 242, 0.18);
}
.title span { color: var(--accent); }

.subtitle { margin: 4px 0 22px; font-size: 1.25rem; color: var(--muted); text-align: center; }

.levels {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 16px;
  width: 100%;
  max-width: 560px;
}

.level-btn {
  position: relative;
  border: 0;
  border-radius: 22px;
  padding: 16px 12px 12px;
  background: #fff;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  transition: transform 0.15s ease;
}
.level-btn:hover:not(:disabled) { transform: translateY(-3px); }
.level-btn:active:not(:disabled) { transform: translateY(2px); }
.level-btn .emoji { font-size: 2.4rem; line-height: 1.1; }
.level-btn .name { font-size: 1.35rem; font-weight: 800; }
.level-btn .count { font-size: 0.95rem; color: var(--muted); }
.level-btn .stars { font-size: 1.3rem; letter-spacing: 2px; }
.level-btn:disabled { opacity: 0.55; cursor: not-allowed; }
.level-btn:disabled .emoji { filter: grayscale(1); }
.level-btn .lock { position: absolute; top: 8px; right: 10px; font-size: 1.2rem; }

.star-on { color: var(--star); }
.star-off { color: var(--star-off); }

/* ---------- game ---------- */
#game { padding: 12px 16px 16px; }

.bar {
  width: 100%;
  max-width: 720px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
}
.bar-info { text-align: center; }
.level-name { font-size: 1.5rem; font-weight: 800; line-height: 1.1; }
.moves { font-size: 1.15rem; color: var(--muted); }
.moves strong { color: var(--accent); font-size: 1.35rem; }

.icon-btn {
  width: 56px; height: 56px;
  border: 0; border-radius: 50%;
  background: #fff;
  box-shadow: var(--shadow);
  font-size: 1.6rem;
  display: grid; place-items: center;
  transition: transform 0.15s ease;
}
.icon-btn:active { transform: translateY(2px) scale(0.96); }

.board {
  --cols: 3;
  --size: 100px;
  display: grid;
  grid-template-columns: repeat(var(--cols), var(--size));
  gap: var(--gap, 12px);
  justify-content: center;
  align-content: center;
  flex: 1;
  perspective: 1000px;
}

/* ---------- cards ---------- */
.card {
  width: var(--size);
  height: var(--size);
  padding: 0;
  border: 0;
  background: none;
  position: relative;
}
.card-inner {
  position: absolute; inset: 0;
  transform-style: preserve-3d;
  transition: transform 0.45s cubic-bezier(.4, 1.4, .6, 1);
}
.card.flipped .card-inner,
.card.matched .card-inner { transform: rotateY(180deg); }

.card-face, .card-back {
  position: absolute; inset: 0;
  border-radius: var(--radius);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  display: grid; place-items: center;
  box-shadow: var(--shadow);
}
.card-back {
  background:
    radial-gradient(circle at 30% 25%, rgba(255,255,255,.35) 0 12%, transparent 13%),
    linear-gradient(145deg, var(--card-back1), var(--card-back2));
  color: rgba(255, 255, 255, 0.9);
  font-size: calc(var(--size) * 0.42);
  font-weight: 800;
}
.card-face {
  background: var(--card-face);
  transform: rotateY(180deg);
  font-size: calc(var(--size) * 0.6);
  line-height: 1;
}
.card:not(.flipped):not(.matched):hover .card-inner { transform: translateY(-3px); }

.card.matched .card-face {
  background: #eafff1;
  box-shadow: 0 0 0 4px var(--match), var(--shadow);
  animation: pop 0.5s ease 0.35s;
}
.card.wrong .card-face { animation: shake 0.4s ease 0.4s; }

@keyframes pop {
  0%, 100% { transform: rotateY(180deg) scale(1); }
  50% { transform: rotateY(180deg) scale(1.12); }
}
@keyframes shake {
  0%, 100% { transform: rotateY(180deg) translateX(0); }
  25% { transform: rotateY(180deg) translateX(-6px); }
  75% { transform: rotateY(180deg) translateX(6px); }
}

/* ---------- finish overlay ---------- */
.overlay {
  position: fixed; inset: 0;
  background: rgba(59, 51, 85, 0.45);
  display: grid; place-items: center;
  padding: 16px;
  z-index: 10;
  animation: fade 0.3s ease;
}
.overlay[hidden] { display: none; }

.panel {
  background: #fff;
  border-radius: 28px;
  padding: 24px 22px 22px;
  width: 100%;
  max-width: 400px;
  text-align: center;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.25);
  animation: rise 0.4s cubic-bezier(.3, 1.5, .6, 1);
}
.panel h2 { margin: 6px 0 0; font-size: 2rem; line-height: 1.15; }
.finish-sub { margin: 4px 0 18px; color: var(--muted); font-size: 1.15rem; }

.big-stars { display: flex; justify-content: center; gap: 8px; font-size: 3.6rem; line-height: 1; min-height: 3.8rem; }
.big-stars span { opacity: 0; transform: scale(0.2) rotate(-30deg); }
.big-stars span.show { animation: starPop 0.5s cubic-bezier(.3, 1.8, .6, 1) forwards; }
.big-stars span.off { filter: grayscale(1); }
.big-stars span.off.show { animation-name: starPopOff; }

@keyframes starPop { to { opacity: 1; transform: scale(1) rotate(0); } }
@keyframes starPopOff { to { opacity: 0.25; transform: scale(0.85) rotate(0); } }

.panel-buttons { display: grid; gap: 10px; }
.btn {
  border: 0;
  border-radius: 16px;
  padding: 12px 16px;
  font-size: 1.3rem;
  font-weight: 700;
  background: #f1eefb;
  box-shadow: 0 4px 0 rgba(59, 51, 85, 0.12);
  transition: transform 0.12s ease;
}
.btn:active { transform: translateY(2px); }
.btn.primary { background: var(--accent); color: #fff; box-shadow: 0 4px 0 var(--accent-dark); }
.btn[hidden] { display: none; }

@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { transform: translateY(40px) scale(0.9); opacity: 0; } }

.panel { max-height: calc(100dvh - 32px); overflow-y: auto; }

/* ---------- ad slot (results screen only) ---------- */
.ad-slot {
  margin-top: 22px;
  padding-top: 12px;
  border-top: 2px dashed #ece8f6;
  animation: fade 0.4s ease;
}
.ad-slot[hidden] { display: none; }
.ad-label {
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: #a59fb8;
  margin-bottom: 6px;
}
.ad-content { min-height: 50px; max-height: 110px; overflow: hidden; display: grid; place-items: center; }
.ad-content ins { display: block; width: 100%; }

.house-ad {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  max-width: 340px;
  min-height: 64px;
  border: 0;
  border-radius: 14px;
  padding: 8px 12px;
  background: #f7f5fc;
  text-align: left;
  font-size: 1rem;
  line-height: 1.25;
}
.house-ad img { width: 64px; height: 64px; object-fit: cover; border-radius: 10px; flex: none; }
.house-ad .house-only-img { width: 100%; height: auto; max-height: 100px; object-fit: contain; }

/* ---------- parent gate ---------- */
.gate { z-index: 12; }
.gate-panel { display: grid; gap: 12px; }
.hold-btn { position: relative; overflow: hidden; touch-action: none; }
.hold-fill {
  position: absolute; inset: 0;
  width: 0;
  background: rgba(255, 255, 255, 0.35);
}
.hold-btn.holding .hold-fill { width: 100%; transition: width 3s linear; }
.hold-text { position: relative; }

/* ---------- home footer ---------- */
.home-footer { margin-top: 28px; font-size: 1rem; }
.home-footer a { color: var(--muted); }

/* ---------- confetti ---------- */
.confetti { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 11; }
.confetti i {
  position: absolute;
  top: -20px;
  width: 10px; height: 16px;
  border-radius: 3px;
  animation: fall linear forwards;
}
@keyframes fall {
  to { transform: translate(var(--dx), 110vh) rotate(var(--rot)); }
}

button:focus-visible { outline: 4px solid var(--accent); outline-offset: 3px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
  .confetti { display: none; }
}
