:root {
  --bg: #fff8ec;
  --ink: #2b2140;
  --great: #1fb85a;
  --ok: #2f8cff;
  --again: #ff8a3d;
  --letter: #ff7a00;
  --soft: color-mix(in srgb, var(--letter) 14%, var(--bg));
  font-family: "Nunito", ui-rounded, "SF Pro Rounded", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--ink);
  display: grid;
  grid-template-rows: 1fr auto;
  height: 100dvh;
  overflow: hidden;
  touch-action: manipulation;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

/* ---- the card: toddler-sized ------------------------------------------------------- */
#stage {
  display: grid;
  place-items: center;
  padding: 2vmin;
  background: radial-gradient(circle at 50% 45%, var(--soft) 0 45%, var(--bg) 75%);
  transition: background .4s;
}
.card {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 4vmin;
  width: 100%;
  height: 100%;
}
.card.enter { animation: pop-in .35s cubic-bezier(.2, 1.6, .4, 1) both; }
.letter {
  font-weight: 800;   /* Nunito: rounded but with open counters - B, R, A stay readable */
  line-height: .9;
  font-size: min(62dvh, 58vw);
  color: var(--letter);
  /* No -webkit-text-stroke: it outlines every contour of the glyph, so letters built from
     overlapping strokes (R, B, A) show seams inside. Shadows only follow the outside edge. */
  text-shadow:
    0 .03em 0 color-mix(in srgb, var(--letter) 65%, black),
    0 .07em .09em rgb(0 0 0 / .18);
}
.picture {
  display: grid;
  place-items: center;
  justify-items: center;
  gap: .5vmin;
}
.picture .emoji { font-size: min(30dvh, 26vw); line-height: 1; }
.picture img { width: min(38dvh, 34vw); height: auto; border-radius: 6vmin; }
.picture .word {
  font-size: min(6dvh, 5vw);
  color: color-mix(in srgb, var(--ink) 75%, transparent);
}
.picture .word b { color: var(--letter); }

@media (orientation: portrait) {
  .card { flex-direction: column; gap: 1vmin; }
  .letter { font-size: min(42dvh, 80vw); }
  .picture .emoji { font-size: min(20dvh, 40vw); }
  .picture img { width: min(26dvh, 52vw); }
}

.card.wobble { animation: wobble .45s ease-in-out; }

/* ---- grade buttons: adult-sized ---------------------------------------------------- */
#grades {
  display: grid;
  grid-template-columns: 1.4fr 1fr 1fr;
  gap: 12px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
}
.grade {
  font: inherit;
  font-size: 1.35rem;
  font-weight: 600;
  min-height: 64px;
  border: 0;
  border-radius: 18px;
  color: white;
  cursor: pointer;
  box-shadow: 0 4px 0 rgb(0 0 0 / .18);
  transition: transform .08s, filter .2s;
}
.grade:active { transform: translateY(3px); box-shadow: 0 1px 0 rgb(0 0 0 / .18); }
.grade:disabled { filter: grayscale(.6) opacity(.6); }
.grade.great { background: var(--great); }
.grade.ok { background: var(--ok); }
.grade.again { background: var(--again); }

/* ---- settings --------------------------------------------------------------------- */
.corner {
  position: fixed;
  top: 10px;
  right: 12px;
  display: flex;
  gap: 4px;
}
.icon-btn {
  font-size: 1.5rem;
  background: none;
  border: 0;
  color: color-mix(in srgb, var(--ink) 35%, transparent);
  cursor: pointer;
  padding: 8px;
  opacity: .55;
}
#stats-btn svg { vertical-align: -.12em; }
dialog {
  border: 0;
  border-radius: 20px;
  padding: 20px 22px;
  min-width: min(360px, 90vw);
  color: var(--ink);
  background: white;
  font-size: 1.1rem;
  box-shadow: 0 20px 60px rgb(0 0 0 / .25);
}
dialog::backdrop { background: rgb(40 30 60 / .35); }
dialog h2 { margin: 0 0 12px; font-size: 1.3rem; }
dialog label { display: flex; align-items: center; justify-content: space-between; gap: 16px; padding: 8px 0; }
dialog input[type=checkbox] { width: 26px; height: 26px; accent-color: var(--great); }
dialog input[type=range] { width: 50%; accent-color: var(--ok); }
dialog .row { display: flex; gap: 10px; justify-content: flex-end; margin-top: 14px; }
dialog button { font: inherit; padding: 8px 16px; border-radius: 12px; border: 0; background: #eee; cursor: pointer; }

/* ---- stats panel (js/stats/) -------------------------------------------------------- */
.stats-dialog { width: min(680px, 94vw); max-height: 90dvh; overflow: auto; }
.panel { padding: 10px 0 14px; border-top: 1px solid #eee; }
.panel h3 { margin: 0 0 8px; font-size: 1rem; text-transform: uppercase; letter-spacing: .06em; opacity: .6; }
.panel p { margin: 4px 0; }
.hint { font-size: .9rem; opacity: .7; min-height: 1.3em; }

.rec-head { font-size: 1.35rem; font-weight: 800; }
.rec.now .rec-head { color: var(--ok); }

.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.tile { background: #f6f3fb; border-radius: 12px; padding: 8px 10px; display: grid; }
.tile b { font-size: 1.5rem; }
.tile span { font-size: .85rem; opacity: .7; }

.letters { display: grid; grid-template-columns: repeat(auto-fill, minmax(72px, 1fr)); gap: 8px; }
.lt { background: #f6f3fb; border-radius: 12px; padding: 6px; text-align: center; cursor: pointer; }
.lt.unseen { opacity: .45; }
.lt-letter { font-size: 2rem; font-weight: 900; line-height: 1.1; }
.lt-bar { height: 8px; background: #e2dcec; border-radius: 4px; overflow: hidden; margin: 2px 0; }
.lt-bar i { display: block; height: 100%; background: var(--ok); }
.lt-num { font-weight: 800; font-size: .95rem; }
.lt-level { font-size: .75rem; opacity: .75; white-space: nowrap; }

.hist { display: grid; grid-template-columns: repeat(14, 1fr); gap: 4px; align-items: end; height: 140px; }
.h-col { display: grid; grid-template-rows: auto 1fr auto; height: 100%; text-align: center; font-size: .75rem; }
.h-stack { display: flex; flex-direction: column-reverse; justify-content: flex-start; height: 100%;
  background: #f6f3fb; border-radius: 4px; overflow: hidden; }
.h-stack i { display: block; }
.h-day { opacity: .6; }
.h-total { min-height: 1.1em; }
.g-great { background: var(--great); }
.g-ok { background: var(--ok); }
.g-again { background: repeating-linear-gradient(45deg, var(--again) 0 3px, #fff 3px 5px); }
.legend { display: flex; gap: 14px; font-size: .85rem; margin-top: 6px; }
.legend i { display: inline-block; width: 12px; height: 12px; border-radius: 3px; margin-right: 4px; vertical-align: -1px; }

.hardest { margin: 0; padding-left: 0; list-style: none; }
.hardest b { font-size: 1.3rem; margin-right: 4px; }

/* ---- celebration layer -------------------------------------------------------------- */
.fx { position: fixed; inset: 0; pointer-events: none; overflow: hidden; z-index: 10; }
.fx canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
.balloon {
  position: absolute;
  bottom: -30vh;
  width: 18vmin;
  height: 22vmin;
  border-radius: 50% 50% 48% 52% / 55% 55% 45% 45%;
  animation: rise var(--dur, .9s) cubic-bezier(.3, .1, .4, 1) forwards;
}
.balloon::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 100%;
  width: 2px;
  height: 9vmin;
  background: rgb(0 0 0 / .25);
}
.balloon::before {
  content: "";
  position: absolute;
  left: 22%; top: 16%;
  width: 22%; height: 26%;
  border-radius: 50%;
  background: rgb(255 255 255 / .45);
}
.yay-word {
  position: absolute;
  left: 50%;
  top: 40%;
  font-weight: 900;
  font-size: min(30vmin, 17vw);
  white-space: nowrap;
  color: white;
  -webkit-text-stroke: .035em rgb(0 0 0 / .45);
  paint-order: stroke fill;
  text-shadow: 0 .06em 0 rgb(0 0 0 / .25);
  animation: yay var(--dur, 2.6s) cubic-bezier(.2, 1.4, .4, 1) forwards;
}

@keyframes rise {
  0% { transform: translate(0, 0) rotate(0); }
  100% { transform: translate(var(--dx, 0), -150vh) rotate(var(--rot, 10deg)); }
}
@keyframes yay {
  0% { transform: translate(-50%, -50%) scale(.2) rotate(-14deg); opacity: 0; }
  14% { transform: translate(-50%, -50%) scale(1.15) rotate(5deg); opacity: 1; }
  30% { transform: translate(-50%, -50%) scale(.95) rotate(-3deg); }
  46% { transform: translate(-50%, -50%) scale(1.1) rotate(3deg); }
  62% { transform: translate(-50%, -50%) scale(1) rotate(0); opacity: 1; }
  85% { transform: translate(-50%, -50%) scale(1.05); opacity: 1; }
  100% { transform: translate(-50%, -70%) scale(.9); opacity: 0; }
}
@keyframes pop-in {
  0% { transform: scale(.6); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}
@keyframes wobble {
  0%, 100% { transform: rotate(0); }
  25% { transform: rotate(-4deg); }
  75% { transform: rotate(4deg); }
}

@media (prefers-reduced-motion: reduce) {
  .card.enter, .card.wobble { animation: none; }
}
