/* ════════════════════════════════════════════════════════
   Memory v2 — Stage 1: representation matching
   Cards show different visual representations of the same
   number (numeral, ten-frame, tally, dot pattern). Pairs
   are always two DIFFERENT representations of the same
   value, so kids must read the quantity from each card.
   ════════════════════════════════════════════════════════ */

*, *::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;
}

body {
  background: #FFD93D;
  display: flex;
  flex-direction: column;
  color: #5A3A0A;
}

/* ─── HUD ──────────────────────────────────────────────── */
.hud {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 16px 8px;
}

.hud-back {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 44px;
  height: 44px;
  background: #fff;
  color: #5A3A0A;
  border-radius: 999px;
  text-decoration: none;
  font-size: 22px;
  font-weight: 700;
  box-shadow: 0 4px 0 #C8941A;
  transition: transform 0.1s ease, box-shadow 0.1s ease;
}
.hud-back:active {
  transform: translateY(2px);
  box-shadow: 0 2px 0 #C8941A;
}

.hud-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: #fff;
  color: #5A3A0A;
  padding: 8px 16px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 18px;
  font-variant-numeric: tabular-nums;
  box-shadow: 0 4px 0 #C8941A;
  min-width: 84px;
  justify-content: center;
}
.pill-icon { font-size: 18px; }
.pill-num--bump {
  display: inline-block;
  animation: numBump 0.32s cubic-bezier(0.34, 1.56, 0.64, 1);
}
@keyframes numBump {
  0%, 100% { transform: scale(1); }
  50%      { transform: scale(1.35); color: #FF6B6B; }
}

/* ─── Progress ─────────────────────────────────────────── */
.progress-row {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 4px 16px 10px;
}
.progress-label {
  font-size: 14px;
  font-weight: 700;
  color: #5A3A0A;
  letter-spacing: 0.02em;
}
.progress-track {
  width: 240px;
  max-width: 80vw;
  height: 12px;
  background: #fff;
  border-radius: 999px;
  box-shadow: 0 3px 0 #C8941A;
  overflow: hidden;
}
.progress-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, #6D4FCF 0%, #0E8C7E 50%, #C2410C 100%);
  border-radius: 999px;
  transition: width 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* ─── 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: 14px;
  width: min(720px, calc(100vw - 32px), calc(100vh - 180px));
}

/* ─── Card ─────────────────────────────────────────────── */
.card {
  position: relative;
  aspect-ratio: 1 / 1;
  perspective: 900px;
  background: transparent;
  border: none;
  padding: 0;
  cursor: pointer;
  font-family: inherit;
  transition: transform 0.12s ease;
}
.card:active { transform: scale(0.96); }
.card--matched, .card--celebrate { cursor: default; }

.card-inner {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transition: transform 0.42s cubic-bezier(0.34, 1.45, 0.64, 1);
}
.card--flipped .card-inner,
.card--matched .card-inner { transform: rotateY(180deg); }

.card-face {
  position: absolute;
  inset: 0;
  border-radius: 18px;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  transition:
    background-color 0.32s cubic-bezier(0.4, 0, 0.2, 1),
    box-shadow 0.32s cubic-bezier(0.4, 0, 0.2, 1),
    color 0.32s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Card back — clean white tile with chunky amber pedestal */
.card-back {
  background: #fff;
  color: #C8941A;
  font-size: 56px;
  box-shadow:
    0 7px 0 #C8941A,
    inset 0 -3px 0 rgba(0, 0, 0, 0.04);
}
.card-back-mark {
  font-weight: 700;
  line-height: 1;
  transform: translateY(-2px);
}

/* Card front — face-up state. Cream background to clearly
   differentiate from the white back face. The representation
   is drawn in a per-type accent color (purely visual variety;
   doesn't help with matching since you have to count anyway). */
.card-front {
  transform: rotateY(180deg);
  background: #FFF5E1;
  color: #5A3A0A;
  padding: 14%;
  text-align: center;
  box-shadow:
    0 7px 0 #C8941A,
    inset 0 -3px 0 rgba(0, 0, 0, 0.06);
}

.card-display {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Per-representation accent colors (face-up, unmatched).
   Bloom on match overrides these. */
.card--rep-numeral .card-display { color: #6D4FCF; }
.card--rep-tenframe .card-display { color: #0E8C7E; }
.card--rep-tally .card-display { color: #15803D; }
.card--rep-dots .card-display { color: #C2410C; }
.card--rep-hanzi .card-display { color: #BE185D; }

/* Numeral representation */
.rep-numeral {
  font-size: clamp(48px, 12vw, 80px);
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.08);
}

/* Hanzi representation. Uses a CJK system stack so characters
   render with the iPad's native Chinese font rather than Fredoka's
   fallback (which renders hanzi poorly). Size auto-shrinks for
   compositional multi-character hanzi (十一 vs 一百零五) via
   container-fitted sizing. */
.rep-hanzi {
  font-weight: 600;
  line-height: 1;
  font-family: "PingFang SC", "Hiragino Sans GB", "Noto Sans CJK SC",
    "Microsoft YaHei", "WenQuanYi Micro Hei", sans-serif;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.08);
  /* Auto-fit: large for 1-2 char hanzi, smaller for 3-5 char strings */
  font-size: clamp(28px, 8vw, 56px);
  letter-spacing: -0.02em;
}

/* Equation representations — for arithmetic stages (G+). Show
   expressions like "5 + 3" or "10 − 2". Tabular-nums keeps digit
   alignment tidy; the proper minus sign (−, U+2212) reads cleaner
   than the hyphen at large sizes. */
.rep-equation {
  font-weight: 700;
  line-height: 1;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 2px 0 rgba(0, 0, 0, 0.08);
  font-size: clamp(28px, 8.5vw, 52px);
  letter-spacing: -0.01em;
  white-space: nowrap;
}

/* Ten-frame representation */
.rep-tenframe {
  width: 100%;
  height: auto;
  max-height: 100%;
}
.rep-tenframe circle.filled { fill: currentColor; }
.rep-tenframe circle.empty {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  opacity: 0.55;
}

/* Tally representation */
.rep-tally {
  width: 100%;
  height: auto;
  max-height: 100%;
}
.rep-tally line {
  stroke: currentColor;
  stroke-width: 5;
  stroke-linecap: round;
}

/* Dot pattern representation */
.rep-dots {
  width: 100%;
  height: auto;
  max-height: 100%;
}
.rep-dots circle { fill: currentColor; }

/* Match success — bloom into pair color, all reps in white */
.card--matched .card-front {
  background: var(--pair-bg, #A78BFA);
  box-shadow:
    0 7px 0 var(--pair-shadow, #6D4FCF),
    inset 0 -3px 0 rgba(0, 0, 0, 0.12);
}
.card--matched .card-display { color: #fff; }
.card--matched {
  animation: matchPop 0.5s cubic-bezier(0.34, 1.6, 0.64, 1);
}
@keyframes matchPop {
  0%   { transform: scale(1); }
  35%  { transform: scale(1.12); }
  100% { transform: scale(1); }
}

/* Mismatch — red flash + shake */
.card--wrong .card-front {
  background: #ef4444 !important;
  box-shadow:
    0 7px 0 #991b1b,
    inset 0 -3px 0 rgba(0, 0, 0, 0.15) !important;
}
.card--wrong .card-display { color: #fff !important; }
.card--wrong {
  animation: wrongShake 0.4s ease-in-out;
}
@keyframes wrongShake {
  0%, 100% { transform: translateX(0); }
  20%      { transform: translateX(-5px); }
  40%      { transform: translateX(5px); }
  60%      { transform: translateX(-4px); }
  80%      { transform: translateX(4px); }
}

/* Win celebration — wave pulse */
.card--celebrate {
  animation: celebratePulse 0.55s cubic-bezier(0.34, 1.6, 0.64, 1);
  animation-delay: var(--delay, 0s);
}
@keyframes celebratePulse {
  0%   { transform: scale(1) rotate(0deg); }
  45%  { transform: scale(1.15) rotate(-3deg); }
  75%  { transform: scale(0.97) rotate(2deg); }
  100% { transform: scale(1) rotate(0deg); }
}

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

.overlay-card {
  background: #fff;
  color: #5A3A0A;
  border-radius: 24px;
  padding: 32px 36px 28px;
  text-align: center;
  box-shadow: 0 10px 0 #C8941A;
  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(40px); opacity: 0; }
  to   { transform: scale(1) translateY(0); opacity: 1; }
}
.overlay-title {
  font-size: 28px;
  font-weight: 700;
  margin-bottom: 18px;
  color: #5A3A0A;
}
.overlay-stats {
  display: flex;
  gap: 14px;
  justify-content: center;
  margin-bottom: 16px;
}
.stat {
  background: #FFD93D;
  border-radius: 16px;
  padding: 12px 18px;
  min-width: 96px;
  box-shadow: 0 4px 0 #C8941A;
}
.stat-label {
  font-size: 12px;
  font-weight: 700;
  color: #5A3A0A;
  text-transform: uppercase;
  letter-spacing: 0.06em;
}
.stat-num {
  font-size: 36px;
  font-weight: 700;
  color: #5A3A0A;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
}
.overlay-msg {
  font-size: 16px;
  color: #5A3A0A;
  margin-bottom: 22px;
  min-height: 1.5em;
  font-weight: 600;
}
.overlay-msg--new-best {
  color: #FF6B6B;
  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: 20px;
  color: #fff;
  background: #FF6B6B;
  border: none;
  padding: 14px 36px;
  border-radius: 999px;
  cursor: pointer;
  box-shadow: 0 6px 0 #C94747;
  transition: transform 0.1s ease, box-shadow 0.1s ease;
}
.big-btn:active {
  transform: translateY(4px);
  box-shadow: 0 2px 0 #C94747;
}

/* ─── Responsive ───────────────────────────────────────── */
@media (max-width: 600px) {
  .hud { padding: 10px 8px 4px; gap: 6px; }
  .hud-back { width: 38px; height: 38px; font-size: 20px; }
  .hud-pill { padding: 6px 12px; font-size: 16px; min-width: 60px; }
  .pill-icon { font-size: 16px; }
  .progress-track { width: 200px; }
  .progress-label { font-size: 13px; }
  .board { gap: 10px; }
  .card-back { font-size: 38px; }
  .card-front { padding: 12%; }
  .rep-numeral { font-size: clamp(36px, 11vw, 64px); }
  .rep-hanzi { font-size: clamp(24px, 7vw, 48px); }
  .rep-equation { font-size: clamp(22px, 6vw, 42px); }
  .overlay-card { padding: 24px 26px 22px; min-width: 240px; }
  .overlay-title { font-size: 22px; }
  .stat-num { font-size: 28px; }
  .stat { padding: 10px 14px; min-width: 80px; }
  .big-btn { font-size: 18px; padding: 12px 28px; }
}
