:root {
  color-scheme: light;
  --ink: #27253a;
  --muted: #6f6c85;
  --purple: #6750d8;
  --purple-dark: #5038c1;
  --purple-soft: #eeeaff;
  --mint: #dff7ed;
  --green: #237a5a;
  --peach: #fff0e1;
  --amber: #b25b13;
  --line: #e8e5f1;
  --surface: #ffffff;
  --bg: #f8f7fc;
  --shadow: 0 18px 50px rgba(64, 49, 117, .12);
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html { min-height: 100%; background: var(--bg); }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  min-height: 100dvh;
  color: var(--ink);
  background:
    radial-gradient(circle at 10% 15%, rgba(255, 215, 154, .28), transparent 23rem),
    radial-gradient(circle at 90% 85%, rgba(159, 224, 206, .25), transparent 25rem),
    var(--bg);
  -webkit-tap-highlight-color: transparent;
}

button, input { font: inherit; }
button { touch-action: manipulation; }

button:focus-visible, input:focus-visible {
  outline: 4px solid rgba(103, 80, 216, .25);
  outline-offset: 3px;
}

.app-shell {
  width: min(100%, 1040px);
  min-height: 100vh;
  min-height: 100dvh;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) max(18px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(18px, env(safe-area-inset-left));
  display: flex;
  flex-direction: column;
}

.topbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.brand, .progress-button, .text-button, .danger-text-button, .icon-button {
  border: 0;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}

.brand { min-height: 48px; padding: 8px 4px; font-weight: 900; font-size: 1.1rem; }
.brand span { color: #f3a638; font-size: 1.3em; }
.progress-button {
  min-height: 48px;
  padding: 8px 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(255,255,255,.8);
  font-weight: 800;
}

.screen { flex: 1; }
.welcome-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: clamp(28px, 6vh, 64px) 0;
}

.hero-orbit {
  width: 116px;
  height: 116px;
  border-radius: 36px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  align-items: center;
  padding: 14px;
  margin-bottom: 24px;
  background: linear-gradient(145deg, #765ee3, #553ac6);
  color: white;
  box-shadow: 0 18px 40px rgba(103, 80, 216, .28);
  font-size: 1.8rem;
  font-weight: 900;
  transform: rotate(-3deg);
}
.hero-orbit span:nth-child(2) { color: #ffd37e; }
.eyebrow { margin: 0 0 8px; color: var(--purple); font-weight: 900; letter-spacing: .04em; text-transform: uppercase; font-size: .8rem; }
.welcome-screen h1 { margin: 0; font-size: clamp(2.6rem, 10vw, 5.1rem); line-height: .98; letter-spacing: -.055em; }
.welcome-screen h1 span { color: var(--purple); }
.welcome-copy { margin: 22px 0 28px; color: var(--muted); font-size: clamp(1rem, 3vw, 1.2rem); line-height: 1.55; }

.primary-button, .secondary-button, .reset-button {
  min-height: 58px;
  border: 0;
  border-radius: 18px;
  padding: 14px 26px;
  font-weight: 900;
  cursor: pointer;
}
.primary-button { background: var(--purple); color: white; box-shadow: 0 10px 22px rgba(103, 80, 216, .25); }
.primary-button:hover { background: var(--purple-dark); transform: translateY(-1px); }
.primary-button:active { transform: translateY(1px); }
.start-button { width: min(100%, 290px); font-size: 1.25rem; }
.start-button span { margin-left: 10px; }
.welcome-stats { margin-top: 20px; display: flex; gap: 10px; color: var(--muted); font-size: .95rem; }

.trainer-screen { padding: clamp(24px, 5vh, 52px) 0 16px; }
.quick-stats { display: flex; justify-content: center; gap: 8px; margin-bottom: 16px; }
.quick-stats > span { min-height: 38px; padding: 8px 13px; border-radius: 999px; display: inline-flex; align-items: center; gap: 5px; font-size: .9rem; }
.level-pill { background: var(--purple-soft); color: var(--purple-dark); }
.star-pill { background: #fff5d9; color: #805c00; }
.streak-pill { background: var(--peach); color: var(--amber); }

.practice-card {
  width: min(100%, 620px);
  margin: 0 auto;
  padding: clamp(24px, 5vw, 48px);
  border: 1px solid rgba(232,229,241,.9);
  border-radius: 34px;
  background: rgba(255,255,255,.92);
  box-shadow: var(--shadow);
  text-align: center;
}
.prompt { margin: 0; color: var(--muted); font-weight: 800; }
.problem {
  margin: clamp(22px, 4vh, 34px) 0;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: clamp(7px, 2.5vw, 18px);
  font-weight: 950;
  font-size: clamp(3.6rem, 15vw, 7.2rem);
  letter-spacing: -.06em;
  line-height: 1;
}
.operator { color: var(--purple); font-size: .72em; }
.equals { color: #a6a2b6; font-size: .65em; }
.question { color: #e09d2d; }

#answer-form label { display: block; margin-bottom: 8px; color: var(--muted); font-weight: 800; }
#answer-input {
  display: block;
  width: min(100%, 260px);
  height: 74px;
  margin: 0 auto;
  border: 3px solid var(--line);
  border-radius: 20px;
  background: #fbfaff;
  color: var(--ink);
  text-align: center;
  font-size: 2.2rem;
  font-weight: 900;
  caret-color: var(--purple);
}
#answer-input:focus { border-color: var(--purple); background: white; outline: 4px solid rgba(103,80,216,.12); }
#answer-input[disabled] { opacity: 1; -webkit-text-fill-color: var(--ink); }
.feedback { min-height: 48px; margin: 10px 0 2px; display: flex; justify-content: center; align-items: center; color: var(--muted); font-weight: 850; line-height: 1.25; }
.feedback.correct { color: var(--green); }
.feedback.incorrect { color: var(--amber); }
.feedback.validation { color: var(--purple-dark); }
.check-button { width: min(100%, 310px); font-size: 1.1rem; }
.practice-card.is-correct { border-color: #a8e1cc; box-shadow: 0 18px 50px rgba(35,122,90,.13); }
.practice-card.is-incorrect { border-color: #f2c99e; }
.restart-button { display: block; min-height: 48px; margin: 12px auto 0; padding: 10px 16px; color: var(--muted); font-weight: 800; }
.restart-button:hover { color: var(--purple); }

.celebration {
  position: fixed;
  z-index: 20;
  top: calc(env(safe-area-inset-top) + 76px);
  left: 50%;
  transform: translateX(-50%);
  width: min(calc(100% - 32px), 460px);
  padding: 14px 18px;
  border-radius: 18px;
  background: #35256f;
  color: white;
  text-align: center;
  box-shadow: 0 14px 34px rgba(42,30,91,.25);
  animation: celebration-in .45s cubic-bezier(.2,.8,.2,1);
}
.celebration strong { margin: 0 8px; }

::backdrop { background: rgba(38, 31, 67, .42); backdrop-filter: blur(3px); }
dialog { color: var(--ink); }
.progress-dialog, .confirm-dialog {
  width: min(calc(100% - 28px), 480px);
  max-height: min(760px, calc(100dvh - 32px));
  border: 0;
  border-radius: 28px;
  padding: 24px;
  background: var(--surface);
  box-shadow: 0 28px 80px rgba(38,31,67,.25);
}
.dialog-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; }
.dialog-head h2, .confirm-dialog h2 { margin: 0; font-size: 1.65rem; }
.icon-button { min-width: 48px; min-height: 48px; border-radius: 50%; background: var(--bg); font-size: 1.8rem; }
.level-card { margin: 20px 0 14px; padding: 18px; border-radius: 20px; display: flex; align-items: center; gap: 14px; background: var(--purple-soft); }
.level-badge { width: 54px; height: 54px; display: grid; place-items: center; border-radius: 16px; background: white; font-size: 1.7rem; }
.level-card div { display: flex; flex-direction: column; color: var(--purple-dark); }
.level-card strong { font-size: 2rem; line-height: 1; }
.stat-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stat-grid div { min-height: 120px; padding: 14px; border-radius: 18px; display: flex; flex-direction: column; justify-content: center; background: var(--bg); text-align: center; }
.stat-grid div > span { font-size: 1.35rem; }
.stat-grid strong { margin: 3px 0; font-size: 1.65rem; }
.stat-grid small { color: var(--muted); }
.danger-text-button { display: block; min-height: 48px; margin: 18px auto 0; padding: 10px; color: #9a4b53; text-decoration: underline; text-underline-offset: 3px; font-weight: 800; }

.confirm-dialog { text-align: center; }
.confirm-icon { font-size: 2.8rem; }
.confirm-dialog p { color: var(--muted); line-height: 1.5; }
.confirm-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 22px; }
.secondary-button { border: 2px solid var(--line); background: white; color: var(--ink); }
.reset-button { background: #8f4852; color: white; }

.topbar-actions { display: flex; align-items: center; gap: 8px; }
.player-button {
  min-height: 48px;
  max-width: 150px;
  padding: 8px 12px;
  overflow: hidden;
  border: 0;
  border-radius: 999px;
  background: var(--purple-soft);
  color: var(--purple-dark);
  font-weight: 900;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}

.login-screen {
  flex: 1;
  display: grid;
  place-items: center;
  padding: clamp(28px, 6vh, 64px) 0;
}
.login-card {
  width: min(100%, 500px);
  padding: clamp(24px, 5vw, 42px);
  border: 1px solid var(--line);
  border-radius: 32px;
  background: rgba(255,255,255,.94);
  box-shadow: var(--shadow);
  text-align: center;
}
.hero-orbit.small { width: 86px; height: 86px; margin: 0 auto 22px; border-radius: 26px; font-size: 1.35rem; }
.login-card h1 { margin: 0; font-size: clamp(2rem, 8vw, 3rem); line-height: 1.05; letter-spacing: -.04em; }
.login-copy { margin: 14px auto 22px; max-width: 360px; color: var(--muted); line-height: 1.5; }
#player-form label { display: block; margin-bottom: 8px; color: var(--muted); font-weight: 850; text-align: left; }
#player-name {
  width: 100%;
  min-height: 62px;
  border: 3px solid var(--line);
  border-radius: 18px;
  padding: 10px 16px;
  color: var(--ink);
  background: #fbfaff;
  font-size: 1.25rem;
  font-weight: 850;
}
#player-name:focus { border-color: var(--purple); background: white; outline: 4px solid rgba(103,80,216,.12); }
.name-feedback { min-height: 38px; margin: 8px 0; color: var(--muted); font-size: .92rem; }
.name-feedback.error { color: #9a4b53; font-weight: 800; }
.name-suggestions { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; margin-bottom: 10px; }
.legacy-progress-option { min-height: 48px; margin: 2px 0 12px; padding: 10px 12px; display: flex !important; align-items: center; gap: 10px; border-radius: 14px; background: #fff8e8; color: #76520a !important; text-align: left !important; cursor: pointer; }
.legacy-progress-option[hidden] { display: none !important; }
.legacy-progress-option input { width: 24px; height: 24px; flex: 0 0 auto; accent-color: var(--purple); }
.name-suggestions button, .known-player-button {
  min-height: 46px;
  border: 2px solid #d9d2ff;
  border-radius: 999px;
  padding: 8px 16px;
  background: var(--purple-soft);
  color: var(--purple-dark);
  font-weight: 900;
  cursor: pointer;
}
.known-players > p { margin: 0 0 10px; color: var(--muted); font-weight: 800; }
#known-player-buttons { display: flex; flex-wrap: wrap; justify-content: center; gap: 8px; }
.or-divider { display: flex; align-items: center; gap: 10px; margin: 16px 0; color: var(--muted); font-size: .85rem; }
.or-divider::before, .or-divider::after { content: ''; flex: 1; height: 1px; background: var(--line); }

.round-panel { width: min(100%, 620px); margin: 0 auto 12px; }
.round-label { display: flex; justify-content: space-between; margin-bottom: 7px; color: var(--muted); font-weight: 850; }
.round-label strong, #round-count { color: var(--purple-dark); }
.round-track { height: 13px; overflow: hidden; border-radius: 999px; background: #e8e5f1; }
.round-track span { display: block; height: 100%; border-radius: inherit; background: linear-gradient(90deg, #6750d8, #9a83f1); transition: width .35s ease; }
.round-dialog { width: min(calc(100% - 28px), 460px); border: 0; border-radius: 30px; padding: 32px 26px; text-align: center; color: var(--ink); box-shadow: 0 28px 80px rgba(38,31,67,.28); }
.round-trophy { width: 82px; height: 82px; margin: 0 auto 16px; display: grid; place-items: center; border-radius: 28px; background: #fff5d9; color: #d18a11; font-size: 2.8rem; box-shadow: 0 12px 28px rgba(209,138,17,.18); }
.round-dialog h2 { margin: 0; font-size: 2rem; }
.round-score { margin: 22px 0 12px; display: flex; flex-direction: column; }
.round-score strong { color: var(--purple); font-size: 2.7rem; line-height: 1; }
.round-score span, #round-message { color: var(--muted); }
.round-dialog .primary-button { width: 100%; margin-top: 12px; }
.rounds-done { margin: 15px 0 0; text-align: center; color: var(--muted); }

.grade-summary {
  width: min(100%, 360px);
  min-height: 72px;
  margin: 0 0 18px;
  padding: 10px 14px;
  display: grid;
  grid-template-columns: 48px 1fr auto;
  align-items: center;
  gap: 11px;
  border: 2px solid #ddd6ff;
  border-radius: 20px;
  background: linear-gradient(135deg, #f4f1ff, #fff9e9);
  color: var(--ink);
  text-align: left;
  cursor: pointer;
}
.grade-summary:hover { border-color: var(--purple); transform: translateY(-1px); }
.grade-icon, .grade-card-icon {
  width: 48px;
  height: 48px;
  display: grid;
  place-items: center;
  border-radius: 15px;
  background: var(--purple);
  color: white;
  font-size: 1.7rem;
  font-weight: 900;
}
.grade-summary > span:nth-child(2) { display: flex; flex-direction: column; min-width: 0; }
.grade-summary small { color: var(--muted); font-size: .78rem; }
.grade-summary strong { font-size: 1.08rem; }
.rank-chip { padding: 7px 10px; border-radius: 999px; background: white; color: var(--purple-dark); font-size: .8rem; white-space: nowrap; }

.rewards-dialog {
  width: min(calc(100% - 24px), 620px);
  max-height: calc(100dvh - 24px);
  overflow-y: auto;
  border: 0;
  border-radius: 28px;
  padding: 24px;
  color: var(--ink);
  background: var(--surface);
  box-shadow: 0 28px 80px rgba(38,31,67,.28);
}
.rewards-dialog h2 { margin: 0; font-size: 1.65rem; }
.grade-card { margin: 18px 0; padding: 16px; display: flex; align-items: center; gap: 14px; border-radius: 20px; background: var(--purple-soft); }
.grade-card-icon { flex: 0 0 auto; width: 58px; height: 58px; font-size: 2rem; }
.grade-card div { display: flex; flex-direction: column; min-width: 0; }
.grade-card small, .grade-card span { color: var(--muted); }
.grade-card strong { color: var(--purple-dark); font-size: 1.4rem; }
.rewards-section { margin-top: 20px; }
.section-title { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.section-title h3 { margin: 0; font-size: 1.15rem; }
.section-title > span { color: var(--muted); font-size: .9rem; }
.badges-grid { margin-top: 10px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.badge-card { min-height: 116px; padding: 11px 8px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px; border-radius: 16px; background: #f7f5fc; text-align: center; }
.badge-card.locked { opacity: .48; filter: grayscale(1); }
.badge-symbol { width: 40px; height: 40px; display: grid; place-items: center; border-radius: 13px; background: #fff1c9; color: #9a690e; font-size: 1.35rem; font-weight: 900; }
.badge-card strong { font-size: .82rem; line-height: 1.15; }
.badge-card small { color: var(--muted); font-size: .7rem; line-height: 1.2; }
.leaderboard-note { margin: 7px 0 10px; color: var(--muted); font-size: .82rem; line-height: 1.35; }
.leaderboard-list { margin: 0; padding: 0; list-style: none; display: grid; gap: 6px; }
.leaderboard-row, .my-rank-row { min-height: 50px; padding: 8px 10px; display: grid; grid-template-columns: 34px 1fr auto; align-items: center; gap: 8px; border-radius: 14px; background: var(--bg); }
.leaderboard-row.is-me, .my-rank-row { border: 2px solid #cfc5ff; background: var(--purple-soft); }
.leaderboard-position { color: var(--purple-dark); font-weight: 950; text-align: center; }
.leaderboard-player { min-width: 0; display: flex; flex-direction: column; }
.leaderboard-player strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.leaderboard-player small { color: var(--muted); }
.leaderboard-stars { color: #805c00; font-weight: 900; white-space: nowrap; }
.my-rank-row { margin-top: 8px; }
.round-reward { display: inline-block; margin: 4px 0 8px; padding: 7px 11px; border-radius: 999px; background: var(--purple-soft); color: var(--purple-dark); font-weight: 850; }

.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
[hidden] { display: none !important; }

@keyframes celebration-in { from { opacity: 0; transform: translate(-50%, -18px) scale(.94); } to { opacity: 1; transform: translate(-50%, 0) scale(1); } }
@keyframes level-pulse { 50% { transform: scale(1.035); box-shadow: 0 22px 60px rgba(103,80,216,.26); } }
.practice-card.level-up { animation: level-pulse .7s ease-in-out 2; }

@media (max-width: 430px) {
  .app-shell { padding-left: 14px; padding-right: 14px; }
  .brand { font-size: 1rem; }
  .progress-button { padding: 7px 10px; font-size: .86rem; }
  .practice-card { border-radius: 26px; padding: 24px 18px; }
  .trainer-screen { padding-top: 18px; }
  .problem { margin: 24px 0; }
  .rewards-dialog { padding: 20px 16px; }
  .badges-grid { grid-template-columns: repeat(2, 1fr); }
  .leaderboard-row, .my-rank-row { grid-template-columns: 30px minmax(0, 1fr) auto; padding-left: 6px; padding-right: 8px; }
}

@media (max-height: 650px) {
  .trainer-screen { padding-top: 8px; }
  .practice-card { padding-top: 18px; padding-bottom: 18px; }
  .problem { margin: 16px 0; }
  #answer-input { height: 64px; }
  .feedback { min-height: 38px; margin-top: 6px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; transition: none !important; }
}
