:root {
  --bg: #eef4ff;
  --card: #ffffff;
  --ink: #22314f;
  --muted: #7a89a8;
  --brand: #4a6cf7;
  --brand-dark: #3450c4;
  --good: #22b07d;
  --bad: #e8636e;
  --warn: #f0a63a;
  --box: #f1f5ff;
  --box-line: #c9d6ff;
  --radius: 18px;
  --shadow: 0 10px 30px rgba(40, 66, 130, 0.12);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: "Baloo 2", "Comic Sans MS", "Segoe UI", system-ui, sans-serif;
  background: linear-gradient(160deg, #eaf1ff 0%, #f6f0ff 100%);
  color: var(--ink);
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  padding: 20px;
}

.app {
  width: 100%;
  max-width: 560px;
}

/* Top bar */
.topbar {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px 20px;
  box-shadow: var(--shadow);
  margin-bottom: 18px;
}
.topbar h1 {
  margin: 0 0 12px;
  font-size: 1.6rem;
  text-align: center;
  color: var(--brand-dark);
}
.stats {
  display: flex;
  justify-content: space-around;
  gap: 10px;
  margin-bottom: 12px;
}
.stat { text-align: center; }
.stat-label {
  display: block;
  font-size: 0.75rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.stat-value {
  display: block;
  font-size: 1.4rem;
  font-weight: 700;
}
.level-bar {
  height: 12px;
  background: var(--box);
  border-radius: 999px;
  overflow: hidden;
}
.level-bar-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--brand), #8a6cf7);
  border-radius: 999px;
  transition: width 0.4s ease;
}

/* Card */
.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 26px 22px 30px;
  box-shadow: var(--shadow);
  text-align: center;
}
.prompt {
  margin: 0 0 18px;
  font-size: 1.1rem;
  color: var(--muted);
}

/* Speak buttons */
.speak-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin-bottom: 26px;
}

/* Letter boxes */
.letters {
  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
  gap: 6px;
  min-height: 66px;
  margin-bottom: 8px;
  overflow-x: auto;      /* fallback: scroll rather than wrap for huge words */
  padding-bottom: 4px;
}
.letter-box {
  flex: 0 0 auto;        /* size is set inline by fitLetters() */
  width: 52px;
  height: 62px;
  font-size: 2rem;
  font-weight: 700;
  text-align: center;
  text-transform: lowercase;
  border: 3px solid var(--box-line);
  border-radius: 12px;
  background: var(--box);
  color: var(--ink);
  outline: none;
  transition: border-color 0.15s, transform 0.1s, background 0.15s;
}
.letter-box:focus {
  border-color: var(--brand);
  transform: translateY(-2px);
  background: #fff;
}
.letter-box.given {
  background: #eaf7f1;
  border-color: #bfe6d5;
  color: var(--good);
  display: flex;
  align-items: center;
  justify-content: center;
}
.letter-box.space { border: none; background: transparent; width: 20px; }
.letter-box.correct { border-color: var(--good); background: #eaf7f1; }
.letter-box.wrong { border-color: var(--bad); background: #fdeaec; }

/* Feedback */
.feedback {
  min-height: 28px;
  font-size: 1.15rem;
  font-weight: 700;
  margin: 6px 0 18px;
}
.feedback.good { color: var(--good); }
.feedback.bad { color: var(--bad); }

/* Buttons */
.btn {
  font-family: inherit;
  font-size: 1rem;
  font-weight: 700;
  border: none;
  border-radius: 999px;
  padding: 12px 20px;
  cursor: pointer;
  transition: transform 0.08s ease, filter 0.15s ease, box-shadow 0.15s;
  box-shadow: 0 4px 0 rgba(0,0,0,0.08);
}
.btn:active { transform: translateY(2px); box-shadow: 0 2px 0 rgba(0,0,0,0.08); }
.btn-speak { background: var(--brand); color: #fff; font-size: 1.15rem; }
.btn-ghost { background: var(--box); color: var(--brand-dark); box-shadow: 0 4px 0 var(--box-line); }
.btn-check { background: var(--good); color: #fff; font-size: 1.1rem; }
.btn-next { background: var(--brand); color: #fff; font-size: 1.1rem; }
.btn-hint { background: var(--warn); color: #fff; }
.btn-tiny { background: transparent; color: var(--muted); box-shadow: none; font-size: 0.85rem; padding: 6px 10px; }
.btn-tiny:active { transform: none; }
.btn:disabled { opacity: 0.45; cursor: default; transform: none; }

.action-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 12px;
}

.hidden { display: none !important; }

/* Footer */
.footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 16px;
  padding: 0 6px;
  color: var(--muted);
  font-size: 0.9rem;
}
.rate-control {
  display: flex;
  align-items: center;
  gap: 8px;
}
.rate-control input { accent-color: var(--brand); }

/* Confetti */
.confetti {
  position: fixed;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
  z-index: 50;
}
.confetti span {
  position: absolute;
  top: -20px;
  font-size: 1.4rem;
  animation: fall linear forwards;
}
@keyframes fall {
  to { transform: translateY(105vh) rotate(540deg); opacity: 0.9; }
}

/* Start / level-select screen */
.start-screen {
  position: fixed;
  inset: 0;
  background: linear-gradient(160deg, #eaf1ff 0%, #f6f0ff 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 60;
}
.start-screen.hidden { display: none; }
.start-card {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 30px 26px;
  max-width: 460px;
  width: 100%;
  text-align: center;
}
.start-card h2 {
  margin: 0 0 6px;
  font-size: 1.7rem;
  color: var(--brand-dark);
}
.start-sub {
  margin: 0 0 22px;
  color: var(--muted);
}
.level-choices {
  display: grid;
  gap: 12px;
  margin-bottom: 16px;
}
.level-choice {
  font-family: inherit;
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--ink);
  background: var(--box);
  border: 3px solid var(--box-line);
  border-radius: 14px;
  padding: 14px 18px;
  cursor: pointer;
  text-align: left;
  display: flex;
  align-items: center;
  gap: 12px;
  transition: transform 0.08s, border-color 0.15s, background 0.15s;
}
.level-choice:hover { border-color: var(--brand); background: #fff; }
.level-choice:active { transform: translateY(2px); }
.level-choice .num {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--brand);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.2rem;
}
.level-choice .sample {
  display: block;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--muted);
}

@media (max-width: 420px) {
  .letter-box { width: 42px; height: 52px; font-size: 1.6rem; }
  .topbar h1 { font-size: 1.3rem; }
}
