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

body {
  font-family: system-ui, sans-serif;
  background: #1a1a2e;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  min-height: 100vh;
  padding: 16px;
  user-select: none;
  -webkit-user-select: none;
}

.hidden { display: none !important; }

/* ─── Mode Select ────────────────────────────────────────────────────────── */
#mode-select {
  width: 100%;
  max-width: 440px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding-top: 48px;
  gap: 32px;
}

#mode-header { text-align: center; }

#mode-select h1 {
  font-size: 2.4rem;
  font-weight: 700;
  color: #e0e0e0;
  margin-bottom: 8px;
}

#mode-subtitle {
  font-size: 1rem;
  color: #7c83a8;
}

#mode-buttons {
  display: flex;
  gap: 16px;
  width: 100%;
}

.mode-btn {
  flex: 1;
  background: #16213e;
  border: 2px solid #2a2d5e;
  border-radius: 16px;
  padding: 24px 16px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  transition: border-color 0.15s, transform 0.1s;
  color: #e0e0e0;
}
.mode-btn:hover { border-color: #6c63ff; transform: translateY(-2px); }
.mode-btn:active { transform: translateY(0); }

.mode-icon { font-size: 2.4rem; line-height: 1; }
.mode-name { font-size: 1.1rem; font-weight: 700; }
.mode-desc { font-size: 0.75rem; color: #7c83a8; }
.mode-score {
  font-size: 0.85rem;
  font-weight: 700;
  color: #ffd93d;
  min-height: 1.2em; /* reserve space when empty to prevent layout shift */
}

.how-to-play-btn {
  background: transparent;
  color: #7c83a8;
  border: 1px solid #2a2d5e;
  border-radius: 10px;
  padding: 10px 20px;
  font-family: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
  margin-top: 8px;
}
.how-to-play-btn:hover { color: #e0e0e0; border-color: #6c63ff; }

/* ─── Game Area ──────────────────────────────────────────────────────────── */
#game-area {
  width: 100%;
  max-width: 440px;
  position: relative;
}

#header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

#level-indicator {
  font-size: 1.1rem;
  font-weight: 700;
  color: #e0e0e0;
}

.stat-box {
  background: #16213e;
  border-radius: 8px;
  padding: 6px 14px;
  text-align: center;
  min-width: 68px;
}

.stat-label {
  display: block;
  font-size: 0.6rem;
  font-weight: 700;
  color: #7c83a8;
  letter-spacing: 0.08em;
}

#moves {
  display: block;
  font-size: 1.2rem;
  font-weight: 700;
  color: #e0e0e0;
}

#combo-box { opacity: 0.4; transition: opacity 0.3s; }
#combo-box.active { opacity: 1; }

#combo-display {
  font-size: 1.1rem;
  font-weight: 800;
  color: #ffd93d;
}

@keyframes combo-pop {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.5); }
  100% { transform: scale(1); }
}

#combo-display.pop {
  animation: combo-pop 0.35s ease;
}

/* ─── Countdown Row ──────────────────────────────────────────────────────── */
#countdown-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 10px;
}

#countdown-bar {
  flex: 1;
  height: 8px;
  background: #16213e;
  border-radius: 4px;
  overflow: hidden;
}

#countdown-fill {
  height: 100%;
  width: 100%;
  border-radius: 4px;
  background: #6c63ff;
  transition: width 0.1s linear, background 0.4s;
}

#countdown-fill.warning { background: #f59563; }
#countdown-fill.danger  { background: #ff6b6b; }

#time-remaining {
  font-size: 0.8rem;
  font-weight: 700;
  color: #7c83a8;
  min-width: 34px;
  text-align: right;
  font-variant-numeric: tabular-nums;
  transition: color 0.4s;
}
#time-remaining.warning { color: #f59563; }
#time-remaining.danger  { color: #ff6b6b; }

/* ─── Controls ───────────────────────────────────────────────────────────── */
#controls {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-bottom: 10px;
}

#menu-btn {
  background: transparent;
  color: #7c83a8;
  border: 1px solid #2a2d5e;
  border-radius: 8px;
  padding: 6px 16px;
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}
#menu-btn:hover { color: #e0e0e0; border-color: #6c63ff; }

#mute-btn,
#pause-btn {
  background: transparent;
  color: #7c83a8;
  border: 1px solid #2a2d5e;
  border-radius: 8px;
  padding: 6px 12px;
  font-size: 1rem;
  min-height: 36px;
  min-width: 44px;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
}
#mute-btn:hover,
#pause-btn:hover { color: #e0e0e0; border-color: #6c63ff; }

/* ─── Grid ───────────────────────────────────────────────────────────────── */
#grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
}

/* ─── Card ───────────────────────────────────────────────────────────────── */
.card {
  aspect-ratio: 1;
  perspective: 600px;
  cursor: pointer;
  animation: card-appear 0.25s cubic-bezier(0.34, 1.56, 0.64, 1) backwards;
}

.card-inner {
  width: 100%;
  height: 100%;
  position: relative;
  transform-style: preserve-3d;
  transition: transform 0.4s ease;
  border-radius: 12px;
}

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

.card-front,
.card-back {
  position: absolute;
  inset: 0;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.card-front {
  background: #6c63ff;
  font-size: 1.6rem;
  color: rgba(255, 255, 255, 0.5);
}

.card-back {
  background: var(--card-bg, #f0effe);
  transform: rotateY(180deg);
  padding: 8px;
}
.card-back svg {
  width: 100%;
  height: 100%;
}

@keyframes card-appear {
  from { transform: scale(0.7); opacity: 0; }
  to   { transform: scale(1);   opacity: 1; }
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  15%       { transform: translateX(-7px); }
  30%       { transform: translateX(7px); }
  45%       { transform: translateX(-5px); }
  60%       { transform: translateX(5px); }
  75%       { transform: translateX(-3px); }
  90%       { transform: translateX(3px); }
}

.card.shake {
  animation: shake 0.5s ease !important;
}

/* ─── Overlay ────────────────────────────────────────────────────────────── */
#overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 10, 30, 0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 10;
}

#overlay-content {
  background: #16213e;
  border-radius: 20px;
  padding: 32px 36px;
  text-align: center;
  color: #e0e0e0;
  max-width: 320px;
  width: 90%;
}

.overlay-icon { font-size: 3rem; margin-bottom: 8px; }

#overlay-content h2 {
  font-size: 1.6rem;
  font-weight: 700;
  margin-bottom: 6px;
}

.overlay-sub {
  font-size: 0.95rem;
  color: #7c83a8;
  margin-bottom: 8px;
}

.new-best {
  font-size: 1rem;
  font-weight: 700;
  color: #ffd93d;
  margin-bottom: 8px;
}

.stars {
  font-size: 1.8rem;
  letter-spacing: 4px;
  color: #ffd93d;
  margin: 12px 0;
}

.win-stars {
  margin: 12px 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.win-level {
  display: flex;
  justify-content: space-between;
  font-size: 0.9rem;
  color: #a0a8c8;
}
.win-level span:last-child {
  color: #ffd93d;
  letter-spacing: 2px;
}

/* ─── Star breakdown ─────────────────────────────────────────────────────── */
.star-breakdown {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 12px 0 20px;
  text-align: left;
}

.breakdown-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.82rem;
  color: #c0c8e8;
  padding: 5px 10px;
  background: rgba(108, 99, 255, 0.08);
  border-radius: 6px;
  gap: 12px;
}

.breakdown-row span:first-child {
  flex-shrink: 0;
  color: #7c83a8;
}

.breakdown-row span:last-child {
  text-align: right;
}

.overlay-btn {
  display: block;
  width: 100%;
  margin-top: 10px;
  background: #6c63ff;
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 12px 24px;
  font-family: inherit;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s;
}
.overlay-btn:hover { background: #574fd6; }
.overlay-btn.secondary {
  background: transparent;
  color: #7c83a8;
  border: 1px solid #2a2d5e;
}
.overlay-btn.secondary:hover { color: #e0e0e0; border-color: #6c63ff; background: transparent; }

/* ─── Tutorial Overlay ───────────────────────────────────────────────────── */
#tutorial-overlay {
  position: fixed;
  inset: 0;
  background: rgba(10, 10, 30, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
}

#tutorial-content {
  background: #16213e;
  border-radius: 20px;
  padding: 36px 40px;
  text-align: center;
  color: #e0e0e0;
  max-width: 340px;
  width: 90%;
}

.tutorial-icon { font-size: 3rem; margin-bottom: 12px; }

#tutorial-content h2 {
  font-size: 1.6rem;
  font-weight: 700;
  margin-bottom: 20px;
}

.tutorial-steps {
  display: flex;
  flex-direction: column;
  gap: 14px;
  margin-bottom: 28px;
  text-align: left;
}

.tutorial-step {
  display: flex;
  align-items: flex-start;
  gap: 14px;
  font-size: 0.95rem;
  line-height: 1.4;
  color: #c0c8e8;
}

.step-num {
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: #6c63ff;
  color: white;
  font-weight: 700;
  font-size: 0.85rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

#tutorial-btn {
  display: block;
  width: 100%;
  background: #6c63ff;
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 14px 24px;
  font-family: inherit;
  font-size: 1.05rem;
  font-weight: 700;
  cursor: pointer;
  transition: background 0.15s;
}
#tutorial-btn:hover { background: #574fd6; }

/* ─── Responsive ─────────────────────────────────────────────────────────── */
@media (max-width: 380px) {
  #mode-select h1 { font-size: 1.8rem; }
  .mode-btn { padding: 16px 10px; }
}

/* ─── Board-clear celebration ──────────────────────────────────────────────── */
@keyframes celebrate {
  0%   { transform: translateY(0)    scale(1);    }
  25%  { transform: translateY(-9px) scale(1.1);  }
  55%  { transform: translateY(0)    scale(1);    }
  75%  { transform: translateY(-4px) scale(1.05); }
  100% { transform: translateY(0)    scale(1);    }
}

.card.celebrating {
  position: relative;
  animation: celebrate 0.55s ease both;
  z-index: 10;
}

/* ─── Achievement badges ────────────────────────────────────────────────────── */
.achievement-badges {
  display: flex;
  gap: 10px;
  justify-content: center;
  margin: 12px 0 4px;
}

.badge {
  font-size: 1.6rem;
  cursor: default;
  transition: transform 0.15s, opacity 0.15s;
}

.badge.locked {
  opacity: 0.25;
  filter: grayscale(1);
}

.badge.unlocked {
  opacity: 1;
  filter: none;
}

.badge.unlocked:hover {
  transform: scale(1.2);
}

/* ─── Achievement toast ─────────────────────────────────────────────────────── */
.achievement-toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(80px);
  background: #1a1a2e;
  color: #fff;
  border: 2px solid #f4b942;
  border-radius: 12px;
  padding: 12px 18px;
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.9rem;
  z-index: 9999;
  opacity: 0;
  transition: transform 0.35s ease, opacity 0.35s ease;
  pointer-events: none;
  min-width: 220px;
  max-width: 320px;
}

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

.toast-icon {
  font-size: 1.6rem;
  line-height: 1;
}

/* ─── Daily challenge ───────────────────────────────────────────────────────── */
#daily-streak-display {
  font-size: 0.75rem;
}
