:root {
  color-scheme: light;
  --paper: #f4f6ff;
  --ink: #142451;
  --muted: #506187;
  --blue: #3157d6;
  --pale-blue: #e7edff;
  --line: #c8d3ef;
  --yellow: #ffda65;
  --panel: #fcfdff;
  font-family: ui-rounded, "Avenir Next", system-ui, sans-serif;
  background: var(--paper);
  color: var(--ink);
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
.symbol-library { position: absolute; width: 0; height: 0; overflow: hidden; }
.sr-only, #help-dialog.keyboard-open .lock h3, #help-dialog.keyboard-open .lock label {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip-path: inset(50%); white-space: nowrap; border: 0;
}
body { margin: 0; min-height: 100svh; display: flex; flex-direction: column; }
body:has(dialog[open]) { overflow: hidden; }
button, input { font: inherit; -webkit-tap-highlight-color: transparent; }
button { touch-action: manipulation; color: inherit; cursor: pointer; }
button:disabled { opacity: .4; cursor: default; }
:focus-visible { outline: 3px solid var(--blue); outline-offset: 3px; }
p, h1, h2, h3 { margin: 0; }

.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 14px;
  width: 100%; max-width: 1050px; margin-inline: auto;
  padding: max(17px, env(safe-area-inset-top)) max(18px, env(safe-area-inset-right)) 0 max(18px, env(safe-area-inset-left));
}
.brand { font-size: .94rem; font-weight: 850; letter-spacing: -.035em; }
.scores { display: flex; align-items: center; gap: 15px; }
.score { color: var(--muted); display: grid; justify-items: end; gap: 1px; white-space: nowrap; font-size: .875rem; font-weight: 700; }
.score strong { color: var(--ink); font-size: 1.16rem; line-height: 1; font-weight: 900; font-variant-numeric: tabular-nums; }
.score + .score { border-left: 1px solid var(--line); padding-left: 15px; }

.practice {
  flex: 1; display: grid; place-items: center;
  padding: 18px max(14px, env(safe-area-inset-right)) max(65px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
}
.practice-inner { width: min(100%, 680px); text-align: center; }
.problem { min-height: clamp(104px, 26vw, 174px); display: grid; place-items: center; }
.equation {
  width: 100%; min-width: 0; padding: 4px 0 8px; border: 0; border-radius: 18px; background: transparent;
  display: grid; place-items: center; font-variant-numeric: tabular-nums;
}
.multiplication {
  display: flex; align-items: baseline; justify-content: center; gap: clamp(9px, 3vw, 31px);
  font-size: clamp(3.25rem, 23vw, 9rem); font-weight: 900; letter-spacing: -.095em; line-height: 1;
}
.operator { color: var(--blue); font-size: .62em; font-weight: 800; letter-spacing: 0; }
.addition {
  max-width: min(100%, 510px); padding: 4px 6px; margin-inline: auto; color: var(--blue); font-size: clamp(1.6rem, 7vw, 3.2rem);
  font-weight: 870; letter-spacing: -.045em; line-height: 1.13; overflow-wrap: anywhere; text-wrap: balance;
}
.icon-button {
  border: 0; background: transparent; border-radius: 50%; padding: 0;
  display: inline-grid; place-items: center; width: 48px; height: 48px; flex: 0 0 auto;
}
.collection-toggle {
  width: 44px; height: 44px; border: 1px solid var(--line); border-radius: 12px;
  background: var(--panel); color: var(--blue);
}
.swap { color: var(--blue); background: var(--pale-blue); margin-top: 2px; }
.swap-note {
  min-height: 22px; display: grid; place-items: center; margin-top: 5px; color: var(--muted);
  font-size: .875rem; line-height: 1.35; font-weight: 750;
}

#answer-form { width: min(100%, 285px); margin: 12px auto 0; }
#answer-form label, #lock-form label {
  display: block; margin-bottom: 7px; text-align: left; font-size: .94rem; font-weight: 850;
}
.answer-row { display: flex; align-items: stretch; gap: 9px; }
#answer, #lock-answer {
  min-width: 0; width: 100%; height: 61px; border: 1.5px solid #b8c7ef; border-radius: 15px;
  background: var(--panel); color: var(--ink); padding: 6px 10px;
  font-size: 1.85rem; font-weight: 900; font-variant-numeric: tabular-nums; text-align: center;
}
#answer::placeholder, #lock-answer::placeholder { color: #5e709a; opacity: 1; }
#answer[aria-invalid="true"], #lock-answer[aria-invalid="true"] { border-color: #b34f38; }
#answer[data-correct="true"] { border-color: #64a98e; color: #195d49; background: #ecf8f0; }
.submit {
  min-width: 72px; padding: 0 8px; border: 1.5px solid var(--ink); border-radius: 15px;
  background: var(--yellow); color: var(--ink); display: grid; place-items: center;
  font-weight: 900; box-shadow: 0 3px 0 var(--ink);
}
.submit:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--ink); }
.submit-grade { display: none; }
.feedback { min-height: 24px; margin-top: 10px; color: var(--muted); font-weight: 750; line-height: 1.35; }
.feedback[data-kind="right"] { color: #185f49; }
.feedback[data-kind="wrong"] { color: #903e2b; }

.practice.keyboard-open {
  position: fixed; z-index: 2; inset-inline: 0; top: var(--keyboard-top); height: var(--keyboard-height);
  display: flex; overflow: auto; overscroll-behavior: contain; background: var(--paper);
  padding: max(8px, calc(env(safe-area-inset-top) - var(--keyboard-top)))
    max(12px, env(safe-area-inset-right)) 12px max(12px, env(safe-area-inset-left));
}
.keyboard-open .practice-inner {
  flex: 0 0 auto; width: min(100%, 345px); height: min(100%, 235px); margin: auto;
  display: grid; grid-template-columns: minmax(0, 1fr) 44px;
  grid-template-rows: minmax(0, max-content) auto auto;
  gap: 0 8px; align-content: center; align-items: center;
}
.keyboard-open .problem { grid-area: 1 / 1; min-height: 0; max-height: 100%; overflow: auto; }
.keyboard-open .equation { padding: 0; }
.keyboard-open .multiplication { font-size: clamp(2.3rem, calc(var(--keyboard-height) * .20), 4.6rem); }
.keyboard-open .addition { font-size: clamp(1.12rem, calc(var(--keyboard-height) * .075), 1.7rem); }
.keyboard-open .swap { grid-area: 1 / 2; width: 44px; height: 44px; margin: 0; }
.keyboard-open .swap-note, .keyboard-open #answer-form { grid-column: 1 / -1; }
.keyboard-open .swap-note { grid-row: 2; }
.keyboard-open .swap-note:empty, .keyboard-open .feedback:empty { display: none; }
.keyboard-open #answer-form { grid-row: 3; margin-top: 9px; }
.keyboard-open #answer-form label { margin-bottom: 4px; }
.keyboard-open #answer { height: 53px; font-size: 1.62rem; }
.keyboard-open .submit { min-width: 88px; }
.keyboard-open .submit-grade { display: block; }
.keyboard-open #submit-icon { display: none; }
.keyboard-open .feedback { min-height: 0; margin-top: 7px; }

.celebration {
  display: flex; align-items: center; gap: 11px; margin-top: 8px; padding: 10px 12px;
  color: #543401; background: #fff1bd; border: 1px solid #eec561; border-radius: 14px;
  box-shadow: 0 3px 0 #eec561;
  text-align: left;
}
.celebration-icon {
  flex: 0 0 auto; display: grid; place-items: center; width: 39px; height: 39px;
  border-radius: 10px; background: #fff9df;
}
.celebration-icon svg, .reward-icon svg, .capture-fallback svg {
  width: 25px; height: 25px; fill: none; stroke: currentColor;
  stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round;
}
.celebration-title { font-size: 1.06rem; line-height: 1.2; font-weight: 900; }
.celebration-detail { margin-top: 3px; font-size: .875rem; line-height: 1.35; font-weight: 700; }
.confetti { position: fixed; z-index: 2; inset: 0; overflow: hidden; pointer-events: none; }
.confetti span {
  position: absolute; left: var(--burst-x); top: var(--burst-y); width: 7px; height: 10px;
  border-radius: 2px; animation: confetti-burst var(--duration) ease-out both;
}
.confetti span:nth-child(3n) { width: 8px; height: 8px; border-radius: 50%; }
@keyframes confetti-burst {
  0% { transform: translate(0, 0) rotate(0); opacity: 0; }
  12% { opacity: 1; }
  42% { transform: translate(calc(var(--x) * .72), var(--y)) rotate(calc(var(--spin) * .55)); }
  72% { opacity: 1; }
  100% { transform: translate(var(--x), calc(var(--y) + 170px)) rotate(var(--spin)); opacity: 0; }
}
@media (prefers-reduced-motion: no-preference) {
  .celebration { animation: celebration-in 230ms ease-out both; }
  @keyframes celebration-in {
    from { transform: translateY(4px) scale(.96); }
    to { transform: translateY(0) scale(1); }
  }
}

.help {
  position: fixed; z-index: 1; right: max(16px, env(safe-area-inset-right)); bottom: max(16px, env(safe-area-inset-bottom));
  width: 54px; height: 54px; background: var(--ink); color: #fff; box-shadow: 0 3px 0 #c6d2f1;
}
.help:focus-visible { outline-offset: 4px; }

dialog {
  width: min(100% - 28px, 420px); max-width: none; max-height: calc(100dvh - 22px); padding: 0;
  background: var(--panel); color: var(--ink); border: 1px solid var(--line); border-radius: 23px; overflow: auto;
  box-shadow: 0 12px 45px #10265c45;
}
dialog::backdrop { background: #0d1f4c8f; }
.capture-dialog {
  width: min(100% - 28px, 370px); padding: 18px 16px 20px;
  background: #fffdf6; border-color: #e9ce7e; text-align: center;
  overscroll-behavior: contain;
}
.capture-portrait {
  display: grid; place-items: center; width: min(100%, 264px); height: clamp(118px, 50vw, 206px);
  margin: 0 auto 12px; border-radius: 20px;
  background: repeating-conic-gradient(from -9deg at 50% 58%, #ffffff50 0deg 10deg, transparent 10deg 25deg),
    radial-gradient(ellipse at center, #fffdf3, var(--capture-accent, #ffe17d));
}
.capture-portrait img {
  width: 100%; height: 100%; min-height: 0; object-fit: contain; padding: 8px;
  filter: drop-shadow(0 5px 4px #45311835);
}
.capture-fallback svg { width: 60px; height: 60px; }
.capture-dialog h2 {
  font-size: clamp(1.35rem, 6vw, 1.85rem); line-height: 1.12; font-weight: 900;
  letter-spacing: -.025em; text-wrap: balance;
}
.capture-streak { margin-top: 6px; color: var(--muted); line-height: 1.35; font-weight: 700; }
.capture-record {
  width: fit-content; margin: 9px auto 0; padding: 3px 10px;
  border-radius: 999px; background: #fff0ae; color: #543401; font-size: .875rem; font-weight: 850;
}
.capture-next { width: min(100%, 225px); min-height: 48px; margin: 16px auto 2px; }
.capture-dialog .confetti { position: absolute; border-radius: inherit; }
@media (prefers-reduced-motion: no-preference) {
  .capture-dialog[open] .capture-portrait { animation: celebration-in 260ms ease-out both; }
}
.capture-dialog::backdrop { background: #0a1127a8; }
.dialog-header {
  display: flex; align-items: center; gap: 9px; min-height: 59px; padding: 5px 9px 5px 18px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.dialog-header h2 { margin-right: auto; font-size: 1rem; font-weight: 900; }
#map-target { color: var(--muted); white-space: nowrap; font-weight: 800; font-size: .875rem; }
.close { color: var(--ink); background: var(--pale-blue); width: 44px; height: 44px; }

.collection {
  --night: #13192d;
  --night-ink: #f2f4ff;
  --night-muted: #bec9e9;
  --night-line: #414d70;
  --caught: #a0e8c5;
  --next: #c0ccff;
  color: var(--night-ink); background: var(--night); border-color: var(--night-line);
  overscroll-behavior: contain;
}
.collection::backdrop { background: #0a1127b8; }
.collection-header {
  position: sticky; z-index: 2; top: 0; display: flex; justify-content: space-between;
  align-items: center; gap: 10px; padding: 16px 14px 14px 17px;
  background: var(--night); border-bottom: 1px solid var(--night-line);
}
.collection-header h2 { font-size: clamp(1.1rem, 5vw, 1.42rem); line-height: 1.15; font-weight: 900; }
#collection-count { margin-top: 4px; color: var(--night-muted); font-size: .875rem; }
.collection-close {
  width: 44px; height: 44px; background: #273353; color: var(--night-ink);
}
.collection :focus-visible { outline-color: var(--next); outline-offset: 2px; }
.reward-track {
  display: grid; grid-auto-rows: minmax(69px, auto); gap: 10px; list-style: none;
  margin: 0; padding: 14px 14px 18px;
}
.reward-track li {
  position: relative; display: grid; grid-template-columns: 26px minmax(0, 1fr);
  align-items: center; gap: 9px;
}
.reward-track li::before, .reward-track li::after {
  content: ""; position: absolute; left: 12px; width: 2px; background: var(--night-line);
}
.reward-track li::before { top: -10px; bottom: 50%; }
.reward-track li::after { top: 50%; bottom: 0; }
.reward-track li:first-child::before, .reward-track li:last-child::after { display: none; }
.reward-track li[data-status="caught"]::before,
.reward-track li[data-status="caught"]::after,
.reward-track li[data-status="next"]::before { background: var(--caught); }
.reward-marker {
  position: relative; z-index: 1; display: grid; place-items: center; width: 26px; height: 26px;
  background: var(--night); border: 2px solid var(--night-line); border-radius: 50%;
  font-size: .875rem; font-weight: 900;
}
[data-status="caught"] .reward-marker { background: var(--caught); border-color: var(--caught); color: #153728; }
[data-status="next"] .reward-marker { border-color: var(--next); }
.reward-card {
  --reward-icon-size: 52px;
  display: grid; grid-template-columns: var(--reward-icon-size) minmax(0, 1fr); align-items: center; gap: 8px;
  align-self: stretch; min-width: 0; padding: 7px 8px;
  border: 1px solid #303b59; border-radius: 12px; background: #1c243b;
}
[data-status="next"] .reward-card { border-color: #879be5; background: #252f51; }
[data-status="caught"] .reward-card { border-color: #467a69; background: #203c38; }
.reward-icon {
  display: grid; place-items: center; width: var(--reward-icon-size); height: var(--reward-icon-size);
  border-radius: 10px;
  color: #adc0e5; background: radial-gradient(ellipse at 48% 61%, #354469, #141b30 76%);
  font-size: 1.35rem; font-weight: 900;
}
.reward-icon img { display: block; width: 100%; height: 100%; padding: 2px; object-fit: contain; }
[data-status="caught"] .reward-icon { color: #1e3422; background: var(--reward-accent); }
.reward-details { display: grid; gap: 3px; }
.reward-details strong { font-size: 1rem; line-height: 1.15; font-weight: 850; }
.reward-details p { color: var(--night-muted); font-size: .875rem; line-height: 1.2; }
[data-status="caught"] .reward-details p { color: var(--caught); }
.reward-progress {
  appearance: none; width: 100%; max-width: 170px; height: 5px; margin-top: 2px;
  border: 0; border-radius: 5px; background: var(--night-line); overflow: hidden;
}
.reward-progress::-webkit-progress-bar { background: var(--night-line); }
.reward-progress::-webkit-progress-value { background: var(--next); }
.reward-progress::-moz-progress-bar { background: var(--next); }

.lock { padding: 24px 18px 16px; text-align: center; }
.lock h3 { font-size: 1.19rem; font-weight: 900; letter-spacing: -.022em; }
.lock-problem {
  width: fit-content; margin: 13px auto 16px; border-radius: 8px; color: var(--blue);
  font-size: clamp(2.4rem, 11vw, 3.4rem); line-height: 1.1; font-weight: 900;
  font-variant-numeric: tabular-nums;
}
#lock-form { width: min(100%, 245px); margin: 0 auto; }
#lock-answer { height: 54px; font-size: 1.55rem; }
.lock-feedback { min-height: 1.35em; margin-top: 10px; color: #903e2b; font-weight: 700; }

#help-dialog.keyboard-open:not(.unlocked) {
  position: fixed; inset: var(--keyboard-top) 0 auto; margin: 6px auto 0;
  max-height: calc(var(--keyboard-height) - 12px);
}
#help-dialog.keyboard-open:not(.unlocked) .dialog-header { min-height: 49px; padding-block: 2px; }
#help-dialog.keyboard-open .lock {
  display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 0 9px;
  align-items: center; padding: 9px 12px 10px;
}
#help-dialog.keyboard-open .lock-problem {
  margin: 0; font-size: clamp(1.28rem, 7vw, 1.75rem); white-space: nowrap;
}
#help-dialog.keyboard-open #lock-form { width: 100%; }
#help-dialog.keyboard-open #lock-answer { height: 48px; }
#help-dialog.keyboard-open .lock-feedback { grid-column: 1 / -1; margin-top: 6px; }
#help-dialog.keyboard-open .lock-feedback:empty { min-height: 0; margin-top: 0; }

dialog.unlocked {
  width: 100%; height: 100dvh; max-height: none; margin: 0; border: 0; border-radius: 0; overflow: hidden;
  display: flex; flex-direction: column; background: #172749;
}
dialog.unlocked .dialog-header {
  padding-top: max(5px, env(safe-area-inset-top));
  padding-right: max(9px, env(safe-area-inset-right));
  padding-left: max(18px, env(safe-area-inset-left));
}
.map { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.map-viewport {
  position: relative; flex: 1; min-height: 0; overflow: hidden; touch-action: none;
  user-select: none; cursor: grab; background: #172749;
}
.map-viewport.panning { cursor: grabbing; }
.map-viewport:focus-visible { outline-offset: -4px; }
#poster { max-width: none; position: absolute; top: 0; left: 0; transform-origin: 0 0; pointer-events: none; }
.map-toolbar {
  display: flex; justify-content: space-between; align-items: center; gap: 7px;
  padding: 6px max(10px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(14px, env(safe-area-inset-left));
  background: var(--panel);
}
.map-toolbar p { color: var(--muted); font-size: .875rem; line-height: 1.25; font-weight: 700; }
.zoom { display: flex; gap: 5px; }
.zoom .icon-button { width: 44px; height: 44px; background: var(--pale-blue); color: var(--blue); font-size: 1.65rem; font-weight: 800; }

@media (hover: hover) {
  .swap:hover, .close:hover, .collection-toggle:hover, .zoom .icon-button:hover:not(:disabled) { background: #d5e1ff; }
  .collection-close:hover { background: #34446f; }
  .help:hover { background: #233b77; }
  .submit:hover { background: #ffe58a; }
}
@media (max-width: 400px) {
  .topbar { gap: 6px; padding-inline: max(8px, env(safe-area-inset-left)) max(8px, env(safe-area-inset-right)); }
  .brand { font-size: .875rem; }
  .scores { gap: 6px; }
  .score + .score { border-left: 0; padding-left: 0; }
}
@media (max-height: 485px) and (orientation: landscape) {
  .topbar { padding-top: max(9px, env(safe-area-inset-top)); }
  .practice:not(.keyboard-open) { padding-top: 3px; padding-bottom: 18px; }
  .practice:not(.keyboard-open) .problem { min-height: 90px; }
  .practice:not(.keyboard-open) .multiplication { font-size: clamp(3.1rem, 20vh, 5.15rem); }
  .practice:not(.keyboard-open) #answer-form { margin-top: 4px; }
  .capture-portrait { height: 115px; }
}
@media (prefers-reduced-motion: reduce) {
  .confetti { display: none; }
}
