/* Math Mode — Duolingo-style Pokémon math academy.
   The whole UI lives inside a fixed-width "phone-shaped" container so the
   layout looks identical on iPhone, iPad, and desktop. Container width is
   480px; below 500px viewport it stretches to fit. */

/* ----- Overlay container override --------------------------------------- */
/* The base .howto-card sets max-width: 720px. We force the math overlay to
   a fixed 480px so the experience is consistent — like opening a phone app
   on any device. */
.math-overlay .howto-card {
  max-width: 480px !important;
  width: 480px;
  margin: 0 auto;
}
@media (max-width: 500px) {
  .math-overlay .howto-card {
    max-width: 100vw !important;
    width: 100vw;
    border-radius: 0;
    min-height: 100vh;
  }
}

#math-root {
  font-family: "Inter", system-ui, sans-serif;
  color: #1f2937;
  max-height: 80vh;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 0 14px 28px;
  /* Smooth scrolling on mobile + iPad. */
  -webkit-overflow-scrolling: touch;
}
@media (max-width: 500px) {
  #math-root { max-height: calc(100vh - 60px); padding: 0 10px 24px; }
}

.math-loading, .math-error {
  text-align: center;
  padding: 50px 20px;
  font-size: 16px;
  color: #6b7280;
}
.math-error { color: #b91c1c; font-weight: 600; }

/* ---------- Hub screen ---------- */

.math-hub { padding: 8px 4px; }
.math-mascot {
  font-size: 56px;
  text-align: center;
  margin-bottom: 4px;
  animation: mathBob 2s ease-in-out infinite;
}
@keyframes mathBob {
  0%, 100% { transform: translateY(0); }
  50%      { transform: translateY(-6px); }
}
.math-title {
  text-align: center;
  margin: 0 0 4px;
  font-size: 24px;
  font-weight: 800;
  color: #1e3a8a;
  letter-spacing: -0.5px;
}
.math-subtitle {
  text-align: center;
  margin: 0 0 18px;
  color: #6b7280;
  font-size: 13px;
}

.math-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-bottom: 16px;
}
.math-stat {
  background: linear-gradient(135deg, #fef3c7, #fde68a);
  border-radius: 12px;
  padding: 12px 6px;
  text-align: center;
  box-shadow: 0 2px 0 #f59e0b inset;
}
.math-stat-num {
  font-size: 22px;
  font-weight: 800;
  color: #92400e;
}
.math-stat-label {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: #78350f;
  font-weight: 600;
}

.math-card-bar { margin: 14px 0 22px; }
.math-card-bar-label {
  font-size: 13px;
  color: #374151;
  margin-bottom: 6px;
}
.math-card-bar-track {
  background: #e5e7eb;
  border-radius: 999px;
  height: 14px;
  overflow: hidden;
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.1);
}
.math-card-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, #fbbf24, #f59e0b, #d97706);
  border-radius: 999px;
  transition: width 0.6s ease;
  box-shadow: 0 0 12px rgba(251, 191, 36, 0.6);
}

/* ---- Quiz format toggle (Quick / Worksheet) ---- */
.math-format-toggle {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 8px 0 4px;
}
.math-format-btn {
  border: 2px solid #d1d5db;
  background: #fff;
  border-radius: 12px;
  padding: 10px 8px;
  font-family: inherit;
  font-weight: 800;
  font-size: 13px;
  color: #6b7280;
  cursor: pointer;
  box-shadow: 0 2px 0 #d1d5db;
  transition: transform 0.1s ease;
}
.math-format-btn.active {
  background: linear-gradient(135deg, #3b82f6, #1e40af);
  color: #fff;
  border-color: #1e3a8a;
  box-shadow: 0 3px 0 #1e3a8a;
}
.math-format-btn:hover { transform: translateY(-1px); }
.math-format-hint {
  text-align: center;
  font-size: 12px;
  color: #6b7280;
  font-style: italic;
  margin: 4px 0 12px;
}

/* ---- Worksheet mode ---- */
.math-worksheet { padding: 6px 0 20px; }
.math-ws-top {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}
.math-ws-title {
  flex: 1;
  margin: 0;
  font-size: 18px;
  font-weight: 800;
  color: #1e3a8a;
  text-align: center;
}
.math-ws-sub {
  text-align: center;
  font-size: 12px;
  color: #6b7280;
  margin: 0 0 16px;
  font-style: italic;
}
.math-ws-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.math-ws-row {
  display: flex;
  gap: 10px;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 12px;
  box-shadow: 0 1px 3px rgba(0,0,0,0.04);
}
.math-ws-num {
  font-weight: 800;
  color: #6b7280;
  font-size: 16px;
  width: 26px;
  flex-shrink: 0;
}
.math-ws-body { flex: 1; min-width: 0; }
.math-ws-prompt {
  font-weight: 700;
  font-size: 15px;
  color: #1f2937;
  margin-bottom: 10px;
  white-space: pre-wrap;
}
.math-ws-input {
  width: 100%;
  max-width: 200px;
  font-size: 18px;
  padding: 8px 12px;
}
.math-ws-choices {
  grid-template-columns: 1fr 1fr;
}
.math-ws-choices .math-choice {
  padding: 8px 8px;
  font-size: 14px;
}
.math-ws-choices .math-choice.emoji .math-choice-text { font-size: 32px; }
.math-ws-tf {
  margin-top: 0;
}
.math-ws-tf .math-tf-btn {
  padding: 12px 4px;
}
.math-ws-tf .math-tf-btn span { font-size: 26px; }
.math-ws-tf .math-tf-btn small { font-size: 10px; letter-spacing: 1px; }
.math-ws-submit-wrap {
  position: sticky;
  bottom: 0;
  background: linear-gradient(to top, white 60%, rgba(255,255,255,0.9));
  padding: 14px 0 8px;
  text-align: center;
  margin-top: 8px;
}
.math-ws-submit { padding: 14px 28px; font-size: 16px; }

.math-grade-heading {
  margin: 16px 0 10px;
  font-size: 15px;
  color: #1f2937;
}

.math-grade-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.math-grade-card {
  position: relative;
  border: none;
  background: var(--vibe, #fff);
  border-radius: 14px;
  padding: 12px 6px;
  cursor: pointer;
  font-family: inherit;
  text-align: center;
  transition: transform 0.12s ease, box-shadow 0.12s ease;
  box-shadow: 0 3px 0 rgba(0, 0, 0, 0.12);
}
.math-grade-card:hover:not(:disabled),
.math-grade-card:focus-visible:not(:disabled) {
  transform: translateY(-2px);
  box-shadow: 0 5px 0 rgba(0, 0, 0, 0.12);
}
.math-grade-card.locked {
  opacity: 0.55;
  cursor: not-allowed;
  filter: grayscale(0.4);
}
.math-grade-card.current {
  outline: 3px solid #1e3a8a;
  outline-offset: 2px;
}
.math-grade-emoji { font-size: 30px; line-height: 1; }
.math-grade-label {
  margin-top: 4px;
  font-weight: 800;
  color: #1f2937;
  font-size: 12px;
}
.math-grade-correct {
  margin-top: 2px;
  font-size: 10px;
  color: rgba(31, 41, 55, 0.7);
}

.math-hint {
  margin-top: 16px;
  font-size: 11px;
  text-align: center;
  color: #6b7280;
  font-style: italic;
}

/* ---------- Quiz screen ---------- */

.math-quiz { padding: 6px 0; }
.math-quiz-top {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}
.math-quit {
  background: #e5e7eb;
  border: none;
  border-radius: 999px;
  padding: 6px 10px;
  cursor: pointer;
  font-family: inherit;
  font-weight: 700;
  color: #374151;
  font-size: 16px;
  line-height: 1;
}
.math-quit:hover { background: #d1d5db; }
.math-progress-track {
  flex: 1;
  background: #e5e7eb;
  border-radius: 999px;
  height: 10px;
  overflow: hidden;
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.1);
}
.math-progress-fill {
  height: 100%;
  background: linear-gradient(90deg, #60a5fa, #3b82f6);
  border-radius: 999px;
  transition: width 0.4s ease;
}
.math-hearts {
  font-size: 14px;
  letter-spacing: -2px;
  min-width: 50px;
  text-align: center;
}
.math-streak {
  font-weight: 800;
  color: #6b7280;
  font-size: 15px;
  min-width: 42px;
  text-align: right;
  transition: color 0.2s, transform 0.2s;
}
.math-streak.ablaze {
  color: #ea580c;
  transform: scale(1.15);
  text-shadow: 0 0 8px rgba(234, 88, 12, 0.4);
}

.math-streak-banner {
  text-align: center;
  font-weight: 800;
  font-size: 14px;
  color: #d97706;
  padding: 6px 12px;
  margin-bottom: 10px;
  background: linear-gradient(135deg, #fef3c7, #fde68a);
  border-radius: 999px;
  letter-spacing: 1px;
  animation: mathBannerPop 0.4s ease-out;
}
@keyframes mathBannerPop {
  0%   { transform: scale(0.6); opacity: 0; }
  60%  { transform: scale(1.08); }
  100% { transform: scale(1); opacity: 1; }
}

.math-q-card {
  background: linear-gradient(145deg, #ffffff, #f3f4f6);
  border-radius: 18px;
  padding: 20px 14px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.06);
}
.math-q-num {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: #6b7280;
  font-weight: 700;
  margin-bottom: 8px;
}
.math-q-prompt-row {
  display: flex;
  align-items: flex-start;
  justify-content: center;
  gap: 8px;
  margin-bottom: 18px;
}
.math-q-prompt {
  font-size: 20px;
  font-weight: 700;
  color: #111827;
  line-height: 1.4;
  text-align: center;
  white-space: pre-wrap;
  flex: 1;
}
.math-tts-btn {
  flex-shrink: 0;
  background: linear-gradient(135deg, #fef3c7, #fde68a);
  border: 2px solid #f59e0b;
  border-radius: 50%;
  width: 38px;
  height: 38px;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  box-shadow: 0 2px 0 #d97706;
  transition: transform 0.12s ease;
}
.math-tts-btn:hover { transform: scale(1.08); }
.math-tts-btn:active { transform: scale(0.95); box-shadow: 0 1px 0 #d97706; }
.math-q-choices {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
@media (max-width: 380px) {
  .math-q-choices { grid-template-columns: 1fr; }
}
.math-choice {
  background: #fff;
  border: 2px solid #d1d5db;
  border-radius: 14px;
  padding: 12px 10px;
  font-family: inherit;
  font-size: 16px;
  font-weight: 700;
  color: #1f2937;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 8px;
  transition: transform 0.1s ease, border-color 0.12s ease, background 0.12s ease;
  box-shadow: 0 2px 0 #d1d5db;
  text-align: left;
}
.math-choice:hover:not(.answered) {
  border-color: #3b82f6;
  transform: translateY(-1px);
}
.math-choice:active:not(.answered) {
  transform: translateY(1px);
  box-shadow: 0 1px 0 #d1d5db;
}
.math-choice.answered { cursor: default; opacity: 0.55; }
.math-choice.chosen {
  border-color: #3b82f6;
  background: #dbeafe;
  opacity: 1;
}
.math-choice-letter {
  background: #f3f4f6;
  width: 26px;
  height: 26px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #4b5563;
  flex-shrink: 0;
}
.math-choice-text { flex: 1; font-weight: 700; }

/* Emoji / single-glyph choices — render big so Pre-K can recognize them
   without having to read anything. */
.math-choice.emoji { justify-content: center; }
.math-choice.emoji .math-choice-letter { display: none; }
.math-choice.emoji .math-choice-text {
  font-size: 44px;
  line-height: 1;
  text-align: center;
}

/* ---- Input-mode (type the answer) ---- */
.math-q-input-wrap {
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: center;
}
.math-q-input {
  flex: 1;
  max-width: 240px;
  font-family: inherit;
  font-size: 24px;
  font-weight: 800;
  text-align: center;
  padding: 12px 14px;
  border: 3px solid #3b82f6;
  border-radius: 12px;
  background: #fff;
  color: #1f2937;
  outline: none;
  box-shadow: 0 2px 0 #1e40af;
}
.math-q-input:focus { border-color: #1e40af; box-shadow: 0 2px 0 #1e3a8a, 0 0 0 4px rgba(59, 130, 246, 0.18); }
.math-q-input-submit {
  flex-shrink: 0;
  font-size: 14px;
  padding: 12px 18px;
}

/* ---- True/False ---- */
.math-q-tf {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 4px;
}
.math-tf-btn {
  font-family: inherit;
  border: 3px solid #d1d5db;
  border-radius: 16px;
  padding: 22px 8px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: transform 0.1s ease, border-color 0.12s ease;
  box-shadow: 0 3px 0 #d1d5db;
  background: white;
}
.math-tf-btn span { font-size: 38px; line-height: 1; font-weight: 800; }
.math-tf-btn small { font-size: 12px; font-weight: 800; letter-spacing: 2px; color: #6b7280; }
.math-tf-btn.math-tf-true  { border-color: #34d399; background: #f0fdf4; }
.math-tf-btn.math-tf-true span { color: #059669; }
.math-tf-btn.math-tf-false { border-color: #f87171; background: #fef2f2; }
.math-tf-btn.math-tf-false span { color: #dc2626; }
.math-tf-btn:hover:not(.answered) { transform: translateY(-2px); }
.math-tf-btn:active:not(.answered) { transform: translateY(1px); box-shadow: 0 1px 0 #d1d5db; }
.math-tf-btn.answered { opacity: 0.55; cursor: default; }
.math-tf-btn.chosen { opacity: 1; transform: scale(1.04); }

.math-feedback { min-height: 0; margin-top: 10px; text-align: center; }
.math-hint-body {
  background: #fffbeb;
  border: 1px dashed #f59e0b;
  border-radius: 10px;
  padding: 8px 12px;
  color: #92400e;
  font-size: 13px;
  font-weight: 600;
}

.math-q-actions {
  display: flex;
  justify-content: center;
  margin-top: 14px;
}
.math-hint-btn {
  background: #fffbeb;
  border: 2px dashed #f59e0b;
  color: #92400e;
  border-radius: 10px;
  padding: 6px 14px;
  font-family: inherit;
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
}
.math-hint-btn:hover:not(:disabled) { background: #fef3c7; }
.math-hint-btn:disabled { opacity: 0.4; cursor: default; border-style: solid; }

/* ---------- Result screen ---------- */

.math-result { padding: 6px 4px; position: relative; }
.math-result-verdict {
  text-align: center;
  margin: 6px 0 4px;
  font-size: 24px;
  font-weight: 800;
  color: #1e3a8a;
}
.math-result-mascot {
  text-align: center;
  color: #6b7280;
  font-style: italic;
  font-size: 13px;
  margin: 0 0 12px;
}
.math-result-score {
  text-align: center;
  font-size: 48px;
  font-weight: 800;
  color: #f59e0b;
  text-shadow: 0 2px 0 rgba(0,0,0,0.05);
  margin: 4px 0 16px;
  animation: mathScorePop 0.5s ease-out;
}
@keyframes mathScorePop {
  0%   { transform: scale(0.3); opacity: 0; }
  60%  { transform: scale(1.1); }
  100% { transform: scale(1); opacity: 1; }
}

.math-result-streak {
  text-align: center;
  margin: 12px 0;
  font-size: 13px;
  color: #374151;
}

.math-unlock-banner {
  background: linear-gradient(135deg, #c084fc, #a855f7);
  color: white;
  padding: 12px;
  border-radius: 12px;
  text-align: center;
  font-weight: 800;
  margin: 12px 0;
  font-size: 14px;
  box-shadow: 0 3px 0 #7e22ce;
  animation: mathUnlock 0.6s ease-out;
}
@keyframes mathUnlock {
  0%   { transform: scale(0.9); opacity: 0; }
  60%  { transform: scale(1.04); }
  100% { transform: scale(1); opacity: 1; }
}

/* Reward block */
.math-reward {
  background: linear-gradient(145deg, #fef3c7, #fde68a);
  border-radius: 18px;
  padding: 16px 12px;
  margin: 16px 0;
  text-align: center;
  border: 3px dashed #f59e0b;
}
.math-reward-banner {
  font-size: 17px;
  font-weight: 800;
  color: #92400e;
  margin-bottom: 10px;
}
.math-reward-picks {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
}
.math-reward-card {
  background: white;
  border-radius: 14px;
  padding: 10px 8px;
  width: 130px;
  box-shadow: 0 3px 8px rgba(0,0,0,0.1);
  border: 3px solid #e5e7eb;
  animation: mathCardPop 0.5s ease-out;
}
@keyframes mathCardPop {
  0%   { transform: scale(0.5) rotate(-10deg); opacity: 0; }
  60%  { transform: scale(1.08) rotate(2deg); }
  100% { transform: scale(1) rotate(0); opacity: 1; }
}
.math-reward-card.rarity-common    { border-color: #94a3b8; }
.math-reward-card.rarity-uncommon  { border-color: #34d399; }
.math-reward-card.rarity-rare      { border-color: #60a5fa; box-shadow: 0 0 14px rgba(96,165,250,0.4); }
.math-reward-card.rarity-epic      { border-color: #a78bfa; box-shadow: 0 0 16px rgba(167,139,250,0.5); }
.math-reward-card.rarity-legendary {
  border-color: #f59e0b;
  box-shadow: 0 0 24px rgba(245,158,11,0.7);
  background: linear-gradient(145deg, #fffbeb, #fef3c7);
}
.math-reward-sprite img {
  width: 96px;
  height: 96px;
  image-rendering: pixelated;
}
.math-reward-name {
  font-weight: 800;
  font-size: 14px;
  color: #1f2937;
  margin-top: 4px;
}
.math-reward-rarity {
  font-size: 10px;
  letter-spacing: 1.5px;
  color: #6b7280;
  margin-bottom: 6px;
}

/* Review block */
.math-review-heading {
  margin: 20px 0 10px;
  font-size: 15px;
  color: #1f2937;
}
.math-review {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.math-review-row {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 10px 10px;
  border-radius: 10px;
  background: #f9fafb;
}
.math-review-row.ok  { background: #ecfdf5; }
.math-review-row.bad { background: #fef2f2; }
.math-review-icon {
  font-size: 20px;
  font-weight: 800;
  flex-shrink: 0;
  width: 22px;
  text-align: center;
}
.math-review-row.ok  .math-review-icon { color: #059669; }
.math-review-row.bad .math-review-icon { color: #dc2626; }
.math-review-body { flex: 1; min-width: 0; }
.math-review-prompt {
  font-weight: 600;
  font-size: 13px;
  color: #1f2937;
  margin-bottom: 2px;
  white-space: pre-wrap;
}
.math-review-meta {
  font-size: 12px;
  color: #6b7280;
}
/* The strategy / "why" explanation — most useful on wrong answers,
   shown on correct ones too for reinforcement. */
.math-review-explanation {
  margin-top: 6px;
  font-size: 12px;
  color: #374151;
  background: rgba(255, 255, 255, 0.6);
  border-left: 3px solid #f59e0b;
  padding: 6px 10px;
  border-radius: 6px;
  font-style: italic;
  line-height: 1.4;
}

.math-result-actions {
  display: flex;
  gap: 8px;
  margin-top: 20px;
  justify-content: center;
  flex-wrap: wrap;
}
.math-btn {
  background: #fff;
  border: 2px solid #d1d5db;
  border-radius: 12px;
  padding: 10px 18px;
  font-family: inherit;
  font-weight: 700;
  font-size: 14px;
  color: #1f2937;
  cursor: pointer;
  box-shadow: 0 2px 0 #d1d5db;
  transition: transform 0.1s ease;
}
.math-btn:hover { transform: translateY(-1px); }
.math-btn:active { transform: translateY(1px); box-shadow: 0 1px 0 #d1d5db; }
.math-btn-primary {
  background: linear-gradient(135deg, #3b82f6, #1e40af);
  color: white;
  border-color: #1e3a8a;
  box-shadow: 0 3px 0 #1e3a8a;
}
.math-btn-primary:hover { background: linear-gradient(135deg, #2563eb, #1e3a8a); }
.math-btn-primary:disabled { opacity: 0.6; cursor: default; }

/* ---------- Confetti (celebrate ≥80% scores) ---------- */
.math-confetti {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 1;
}
.math-confetti-piece {
  position: absolute;
  top: -20px;
  width: 8px;
  height: 14px;
  border-radius: 2px;
  animation: mathConfettiFall linear forwards;
}
@keyframes mathConfettiFall {
  0%   { transform: translateY(-20px) rotate(0deg); opacity: 1; }
  100% { transform: translateY(110vh) rotate(540deg); opacity: 0.6; }
}

/* iPhone portrait — tighten everything but keep the 480 layout shape */
@media (orientation: portrait) and (max-width: 500px) {
  .math-title { font-size: 22px; }
  .math-q-prompt { font-size: 18px; }
  .math-grade-grid { grid-template-columns: repeat(3, 1fr); }
  .math-grade-emoji { font-size: 26px; }
  .math-result-score { font-size: 40px; }
}
