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

:root {
  --bg: #1a1f3a;
  --panel: #2a3358;
  --accent: #ffcb05;
  --accent-dark: #e6b800;
  --correct: #4ade80;
  --wrong: #f87171;
  --text: #f8fafc;
  --muted: #94a3b8;
  --radius: 16px;
  --tap-min: 2.75rem;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: linear-gradient(160deg, #0f172a 0%, var(--bg) 40%, #312e81 100%);
  color: var(--text);
  overflow-x: hidden;
}

.app {
  position: relative;
  z-index: 1;
  max-width: 720px;
  margin: 0 auto;
  padding: calc(0.75rem + var(--safe-top)) calc(0.75rem + var(--safe-right))
    calc(0.75rem + var(--safe-bottom)) calc(0.75rem + var(--safe-left));
}

header {
  text-align: center;
  margin-bottom: 1rem;
}

.header-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 0.65rem 1rem;
}

.header-row h1 {
  margin: 0;
}

.collection-open-btn {
  position: relative;
  background: #e3350d;
  color: #fef08a;
  border: 3px solid #1a1a1a;
  font-size: 0.95rem;
  padding: 0.5rem 0.85rem;
  min-height: var(--tap-min);
  box-shadow: 0 3px 0 #1a1a1a;
  touch-action: manipulation;
}

@media (hover: hover) {
  .collection-open-btn:hover:not(:disabled) {
    transform: translateY(-1px);
  }
}

.collection-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 1.35rem;
  height: 1.35rem;
  margin-left: 0.35rem;
  padding: 0 0.3rem;
  border-radius: 999px;
  background: var(--accent);
  color: #1e293b;
  font-size: 0.8rem;
  font-weight: 800;
}

h1 {
  margin: 0;
  font-size: clamp(1.5rem, 5vw, 2rem);
  color: var(--accent);
  text-shadow: 2px 2px 0 #3b82f6;
}

.subtitle {
  margin: 0.35rem 0 0;
  color: var(--muted);
  font-size: clamp(0.85rem, 3.5vw, 1rem);
  line-height: 1.35;
  padding: 0 0.25rem;
}

.hidden {
  display: none !important;
}

.loading-panel {
  text-align: center;
  padding: 2rem 1rem;
  background: var(--panel);
  border-radius: var(--radius);
}

.progress-bar {
  height: 12px;
  background: #1e293b;
  border-radius: 999px;
  overflow: hidden;
  margin-top: 1rem;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.2s ease;
}

.ops {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
  margin-bottom: 1.25rem;
}

.op-btn {
  flex: 1;
  max-width: none;
  min-height: var(--tap-min);
  min-width: var(--tap-min);
  font-size: clamp(1.5rem, 8vw, 2rem);
  font-weight: 700;
  border: 3px solid transparent;
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.1s, background 0.15s, border-color 0.15s;
}

@media (hover: hover) {
  .op-btn:hover:not(:disabled) {
    transform: scale(1.05);
  }
}

.op-btn.active {
  background: var(--accent);
  color: #1e293b;
  border-color: #3b82f6;
}

.op-btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.problem-area {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
}

.pokemon-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: start;
  justify-items: center;
  gap: 0.25rem 0.35rem;
}

.pokemon-card {
  text-align: center;
  width: 100%;
  min-width: 0;
  max-width: 200px;
}

.pokemon-card img {
  width: min(100%, 7.5rem);
  height: min(100%, 7.5rem);
  object-fit: contain;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.pokemon-card img.placeholder {
  opacity: 0.4;
}

.pokemon-name {
  margin: 0.25rem 0 0;
  font-size: 0.85rem;
  color: var(--muted);
  text-transform: capitalize;
}

.dex-number {
  margin: 0.15rem 0 0;
  font-size: clamp(1.65rem, 9vw, 2.5rem);
  font-weight: 800;
  color: var(--accent);
  line-height: 1.1;
}

.op-symbol {
  font-size: clamp(1.75rem, 8vw, 2.5rem);
  font-weight: 800;
  color: var(--text);
  min-width: 1.75rem;
  text-align: center;
  align-self: center;
  padding-top: 1.5rem;
}

.math-problem {
  display: flex;
  justify-content: center;
  margin: 1rem 0 0;
  padding: 0.75rem 1rem;
  border-radius: 12px;
  background: rgba(15, 23, 42, 0.35);
}

.math-stack {
  display: inline-grid;
  grid-template-columns: minmax(1.25ch, auto) minmax(2.5ch, 1fr);
  column-gap: 0.35rem;
  row-gap: 0.1rem;
  align-items: baseline;
  font-variant-numeric: tabular-nums;
  font-size: clamp(1.75rem, 7vw, 2.5rem);
  font-weight: 800;
  line-height: 1.15;
  color: var(--text);
}

.math-top {
  grid-column: 2;
  text-align: right;
}

.math-op {
  grid-column: 1;
  text-align: right;
  color: var(--muted);
}

.math-bottom {
  grid-column: 2;
  text-align: right;
}

.math-rule {
  grid-column: 1 / -1;
  height: 3px;
  margin: 0.2rem 0 0.15rem;
  background: var(--accent);
  border-radius: 2px;
}

.math-result {
  grid-column: 2;
  text-align: right;
  color: var(--accent);
  letter-spacing: 0.04em;
}

.answer-area {
  background: var(--panel);
  border-radius: var(--radius);
  padding: 1rem;
  margin-bottom: 1rem;
}

.answer-area label {
  display: block;
  margin-bottom: 0.5rem;
  font-weight: 600;
}

.answer-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

#answer-input {
  flex: 1 1 8rem;
  width: 100%;
  font-size: 1.75rem;
  min-height: var(--tap-min);
  padding: 0.65rem 1rem;
  border: 3px solid #475569;
  border-radius: var(--radius);
  background: #1e293b;
  color: var(--text);
  min-width: 0;
}

#check-btn {
  flex: 1 1 auto;
  min-height: var(--tap-min);
  min-width: var(--tap-min);
  touch-action: manipulation;
}

#answer-input:focus {
  border-color: var(--accent);
}

#answer-input:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.btn {
  font-size: 1.1rem;
  font-weight: 700;
  padding: 0.75rem 1.25rem;
  min-height: var(--tap-min);
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  touch-action: manipulation;
  transition: transform 0.1s, opacity 0.15s;
}

@media (hover: hover) {
  .btn:hover:not(:disabled) {
    transform: scale(1.03);
  }
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn.primary {
  background: #3b82f6;
  color: white;
}

.btn:focus-visible,
.op-btn:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.feedback {
  min-height: 1.5rem;
  margin: 0.75rem 0 0;
  font-size: 1.2rem;
  font-weight: 700;
  text-align: center;
}

.feedback.wrong {
  color: var(--wrong);
}

.success-panel {
  display: block;
  margin-bottom: 1rem;
  animation: successPop 0.45s ease;
}

.success-panel.hidden {
  display: none !important;
}

@keyframes successPop {
  0% {
    opacity: 0;
    transform: scale(0.92);
  }
  100% {
    opacity: 1;
    transform: scale(1);
  }
}

/* --- Pokédex success reveal --- */
.pokedex {
  --pokedex-red: #e3350d;
  --pokedex-red-dark: #b82e0a;
  --pokedex-bezel: #1a1a1a;
  --pokedex-screen: #9ad4f0;
  --pokedex-screen-mid: #6eb8dc;
  --pokedex-screen-text: #0f2d3d;
  width: 100%;
  max-width: 420px;
  margin: 0 auto;
  padding: 0.85rem 0.75rem 1rem;
  border-radius: 20px 20px 28px 28px;
  background: linear-gradient(165deg, #f04a2a 0%, var(--pokedex-red) 35%, var(--pokedex-red-dark) 100%);
  border: 4px solid var(--pokedex-bezel);
  box-shadow:
    0 8px 0 var(--pokedex-bezel),
    0 14px 32px rgba(0, 0, 0, 0.45),
    inset 0 2px 0 rgba(255, 255, 255, 0.25);
}

.pokedex-header {
  display: flex;
  align-items: flex-start;
  gap: 0.75rem;
  margin-bottom: 0.85rem;
}

.pokedex-lights {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.35rem;
  flex-shrink: 0;
}

.pokedex-light--main {
  width: 3.25rem;
  height: 3.25rem;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #8fd4ff 0%, #3b9fd9 45%, #1e6fa8 100%);
  border: 4px solid #f8fafc;
  box-shadow:
    0 0 12px rgba(96, 200, 255, 0.7),
    inset 0 -4px 8px rgba(0, 60, 120, 0.35);
  animation: pokedexPulse 2s ease-in-out infinite;
}

.pokedex-light-row {
  display: flex;
  gap: 0.35rem;
  margin-left: 0.15rem;
}

.pokedex-light--sm {
  width: 0.65rem;
  height: 0.65rem;
  border-radius: 50%;
  border: 2px solid rgba(0, 0, 0, 0.35);
  box-shadow: inset 0 1px 2px rgba(255, 255, 255, 0.35);
}

.pokedex-light--red {
  background: #ef4444;
}

.pokedex-light--yellow {
  background: #facc15;
}

.pokedex-light--green {
  background: #4ade80;
}

.pokedex-brand {
  margin: 0.35rem 0 0;
  flex: 1;
  font-size: clamp(1.5rem, 6vw, 2rem);
  font-weight: 900;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: #fef08a;
  text-shadow:
    2px 2px 0 #1a1a1a,
    0 0 12px rgba(254, 240, 138, 0.45);
  text-align: right;
}

.pokedex-screen {
  text-align: center;
  padding: 1rem 0.85rem 1.1rem;
  border-radius: 12px;
  border: 4px solid var(--pokedex-bezel);
  background:
    repeating-linear-gradient(
      0deg,
      transparent,
      transparent 2px,
      rgba(15, 45, 61, 0.04) 2px,
      rgba(15, 45, 61, 0.04) 4px
    ),
    linear-gradient(180deg, var(--pokedex-screen) 0%, var(--pokedex-screen-mid) 100%);
  box-shadow:
    inset 0 0 24px rgba(255, 255, 255, 0.35),
    inset 0 -6px 16px rgba(15, 45, 61, 0.12);
}

.success-catch-count {
  margin: 0.35rem 0 0;
  font-size: clamp(0.9rem, 3.5vw, 1.05rem);
  font-weight: 700;
  color: #b45309;
}

.success-banner {
  margin: 0;
  font-size: clamp(0.95rem, 4vw, 1.15rem);
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #166534;
}

.success-heading {
  margin: 0.35rem 0 0.85rem;
  font-size: clamp(1.15rem, 5vw, 1.5rem);
  font-weight: 800;
  color: var(--pokedex-screen-text);
  text-shadow: none;
}

.success-card {
  margin: 0 auto 0.75rem;
  max-width: 280px;
  padding: 0.5rem 0.65rem 0.65rem;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.55);
  border: 2px solid rgba(15, 45, 61, 0.2);
}

.success-card img {
  width: min(11.25rem, 42vw);
  height: min(11.25rem, 42vw);
  filter: drop-shadow(0 4px 0 rgba(15, 45, 61, 0.15));
}

.success-pokemon-name {
  margin: 0.5rem 0 0;
  font-size: clamp(1.25rem, 5vw, 1.75rem);
  font-weight: 800;
  color: var(--pokedex-screen-text);
  text-transform: capitalize;
}

.success-dex {
  font-size: clamp(2.5rem, 10vw, 3.5rem);
  margin-top: 0.25rem;
  color: #b45309;
  text-shadow: 1px 1px 0 rgba(255, 255, 255, 0.6);
}

.success-equation {
  margin: 0;
  font-size: clamp(1.1rem, 4.5vw, 1.45rem);
  font-weight: 700;
  color: var(--pokedex-screen-text);
  opacity: 0.85;
}

.pokedex-footer {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-top: 0.85rem;
}

.pokedex-dpad {
  flex-shrink: 0;
  width: 2.75rem;
  height: 2.75rem;
  border-radius: 6px;
  background:
    linear-gradient(#374151, #374151) center/38% 100% no-repeat,
    linear-gradient(#374151, #374151) center/100% 38% no-repeat,
    #1f2937;
  border: 3px solid var(--pokedex-bezel);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.12);
}

.pokedex-btn {
  flex: 1;
  margin: 0;
  background: linear-gradient(180deg, #fef08a 0%, var(--accent) 55%, var(--accent-dark) 100%);
  color: #1e293b;
  font-size: clamp(1rem, 4vw, 1.15rem);
  padding: 0.9rem 1rem;
  min-height: var(--tap-min);
  border: 3px solid var(--pokedex-bezel);
  border-radius: 999px;
  box-shadow: 0 4px 0 var(--pokedex-bezel);
  touch-action: manipulation;
}

@media (hover: hover) {
  .pokedex-btn:hover:not(:disabled) {
    transform: translateY(-1px);
    box-shadow: 0 5px 0 var(--pokedex-bezel);
  }
}

.pokedex-btn:active:not(:disabled) {
  transform: translateY(3px);
  box-shadow: 0 1px 0 var(--pokedex-bezel);
}

@keyframes pokedexPulse {
  0%, 100% {
    box-shadow:
      0 0 12px rgba(96, 200, 255, 0.7),
      inset 0 -4px 8px rgba(0, 60, 120, 0.35);
  }
  50% {
    box-shadow:
      0 0 20px rgba(96, 200, 255, 0.95),
      inset 0 -4px 8px rgba(0, 60, 120, 0.35);
  }
}

.problem-area.shake {
  animation: shake 0.45s ease;
}

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

/* --- Grass meadow (decorative + answer easter egg) --- */
.grass-meadow {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  display: none;
  height: 110px;
  pointer-events: none;
  z-index: 0;
  overflow: hidden;
}

.grass-hill {
  position: absolute;
  bottom: 0;
  left: -5%;
  right: -5%;
  height: 72px;
  background: linear-gradient(to top, #1f5c34 0%, #2f7d4a 45%, #4caf6a 100%);
  border-radius: 50% 50% 0 0 / 100% 100% 0 0;
  box-shadow: inset 0 8px 20px rgba(255, 255, 255, 0.12);
}

.grass-blades {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 28px;
  background: repeating-linear-gradient(
    90deg,
    transparent 0,
    transparent 8px,
    #3d9b5c 8px,
    #3d9b5c 10px,
    transparent 10px,
    transparent 18px,
    #2d7a46 18px,
    #2d7a46 20px
  );
  opacity: 0.55;
}

.grass-pokemon {
  position: absolute;
  bottom: 18px;
  left: 0;
  right: 0;
  height: 80px;
}

.grass-mon {
  position: absolute;
  bottom: 0;
  width: 72px;
  height: 72px;
  animation: grassPeek 2.8s ease-in-out infinite;
}

.grass-mon img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  filter: drop-shadow(0 4px 6px rgba(0, 0, 0, 0.35));
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

@keyframes grassPeek {
  0%,
  100% {
    transform: translateY(6px) scale(1);
  }
  50% {
    transform: translateY(-2px) scale(1.04);
  }
}

@media (prefers-reduced-motion: reduce) {
  .grass-mon,
  .pokedex-light--main,
  .success-panel,
  .problem-area.shake {
    animation: none;
  }
}

/* --- Mobile-first tweaks (phones, car trip) --- */
@media (max-width: 399px) {
  .header-row {
    flex-direction: column;
    width: 100%;
  }

  .header-row h1 {
    font-size: clamp(1.25rem, 6vw, 1.5rem);
  }

  .collection-open-btn {
    width: 100%;
  }

  .answer-row {
    flex-direction: column;
  }

  #check-btn {
    width: 100%;
  }

  .pokedex-light--main {
    width: 2.5rem;
    height: 2.5rem;
  }

  .pokedex-brand {
    font-size: 1.25rem;
  }
}

@media (max-width: 500px) {
  .collection-modal {
    padding: 0.5rem;
    padding-top: calc(0.5rem + var(--safe-top));
    padding-bottom: calc(0.5rem + var(--safe-bottom));
    align-items: stretch;
  }

  .collection-dialog {
    max-width: none;
  }

  .collection-screen {
    max-height: min(55dvh, calc(100dvh - 16rem));
    -webkit-overflow-scrolling: touch;
  }

  .collection-stats {
    font-size: 0.78rem;
    line-height: 1.35;
  }

  .collection-grid {
    grid-template-columns: repeat(auto-fill, minmax(4.5rem, 1fr));
    gap: 0.4rem;
  }

  .collection-entry img {
    width: 3rem;
    height: 3rem;
  }

  .collection-close-btn {
    min-height: var(--tap-min);
    font-size: 1.15rem;
  }
}

@media (min-width: 500px) {
  .app {
    padding: calc(1.25rem + var(--safe-top)) calc(1.25rem + var(--safe-right))
      calc(7.5rem + var(--safe-bottom)) calc(1.25rem + var(--safe-left));
  }

  .pokemon-card img {
    width: 8.75rem;
    height: 8.75rem;
  }

  .op-btn {
    max-width: 5rem;
  }

  .grass-meadow {
    display: block;
    height: 7.5rem;
  }

  .grass-mon {
    width: 5rem;
    height: 5rem;
  }
}

/* --- My Pokédex collection --- */
body.collection-open {
  overflow: hidden;
}

.collection-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 0.75rem;
  padding-top: calc(0.75rem + var(--safe-top));
  padding-bottom: calc(0.75rem + var(--safe-bottom));
  background: rgba(15, 23, 42, 0.82);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.collection-modal.hidden {
  display: none !important;
}

.collection-dialog {
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
}

.collection-screen {
  max-height: min(58dvh, 420px);
  overflow-y: auto;
  text-align: left;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.collection-ops {
  margin-bottom: 0.75rem;
}

.collection-stats,
.collection-range {
  margin: 0 0 0.5rem;
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--pokedex-screen-text, #0f2d3d);
  text-align: center;
}

.collection-range {
  font-weight: 600;
  opacity: 0.85;
  margin-bottom: 0.75rem;
}

.collection-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(88px, 1fr));
  gap: 0.5rem;
}

.collection-entry {
  position: relative;
  text-align: center;
  padding: 0.4rem 0.25rem 0.5rem;
  content-visibility: auto;
  contain-intrinsic-size: 88px 100px;
  border-radius: 8px;
  border: 2px solid rgba(15, 45, 61, 0.15);
  background: rgba(255, 255, 255, 0.45);
}

.collection-entry.empty {
  opacity: 0.55;
  background: rgba(15, 45, 61, 0.08);
}

.collection-entry.empty img {
  opacity: 0.35;
}

.collection-entry-num {
  margin: 0;
  font-size: 0.7rem;
  font-weight: 800;
  color: #b45309;
}

.collection-entry img {
  width: 56px;
  height: 56px;
  object-fit: contain;
  display: block;
  margin: 0.15rem auto;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.collection-entry-name {
  margin: 0;
  font-size: 0.68rem;
  font-weight: 700;
  color: var(--pokedex-screen-text, #0f2d3d);
  text-transform: capitalize;
  line-height: 1.2;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.collection-entry-count {
  position: absolute;
  top: 0.2rem;
  right: 0.2rem;
  min-width: 1.4rem;
  padding: 0.1rem 0.3rem;
  border-radius: 999px;
  background: #166534;
  color: #ecfdf5;
  font-size: 0.65rem;
  font-weight: 800;
  border: 2px solid rgba(255, 255, 255, 0.6);
}

.collection-footer {
  flex-direction: column;
  align-items: stretch;
}

.collection-backup-panel {
  margin-bottom: 0.65rem;
  padding: 0.75rem;
  border-radius: 12px;
  border: 2px solid #1a1a1a;
  background: #0f2d3d;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.1);
}

.collection-backup-hint {
  margin: 0 0 0.65rem;
  font-size: 0.8rem;
  line-height: 1.4;
  font-weight: 600;
  color: #e2e8f0;
  text-align: center;
}

.collection-backup-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  margin-bottom: 0.5rem;
}

.collection-backup-btn,
.collection-replace-btn {
  flex: 1 1 calc(50% - 0.25rem);
  min-height: var(--tap-min);
  font-size: 0.9rem;
  font-weight: 700;
  padding: 0.55rem 0.65rem;
  border-radius: var(--radius);
  border: 2px solid #1a1a1a;
  background: #f8fafc;
  color: #0f2d3d;
  touch-action: manipulation;
}

.collection-replace-btn {
  flex: 1 1 100%;
  font-size: 0.85rem;
  background: #e2e8f0;
}

.collection-backup-btn:focus-visible,
.collection-replace-btn:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.collection-backup-status {
  min-height: 1.25rem;
  margin: 0;
  font-size: 0.85rem;
  font-weight: 700;
  line-height: 1.35;
  text-align: center;
  color: #bbf7d0;
}

.collection-backup-status.error {
  color: #fecaca;
}

.collection-footer .pokedex-dpad {
  display: none;
}

.collection-close-btn {
  flex: none;
  width: 100%;
  min-height: var(--tap-min);
  border-radius: var(--radius);
  box-shadow: none;
  touch-action: manipulation;
}
