:root {
  --sky: #d6ecf3;
  --sky-deep: #b9dcea;
  --paper: #ffffff;
  --ink: #1d2a4f;
  --ink-soft: #4a5677;
  --glow: #ffe07a;
  --shelf: #b07a47;
  --shelf-dark: #82552d;

  /* phonics colors: the classroom convention of red vowels, blue consonants */
  --consonant: #2f6fdb;
  --short: #e0413a;
  --long: #d98a00;
  --team: #1e8f5c;
  --digraph: #0f8a9c;
  --r: #7c4dcc;
  --silent: #9aa3b2;

  --read-size: clamp(1.45rem, 1rem + 1.9vw, 2.35rem);
  --radius-page: 28px;
  font-family: "Andika", "Comic Sans MS", system-ui, sans-serif;
  color: var(--ink);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; -webkit-tap-highlight-color: transparent; }
button, .btn, .w, .tile, .chip, .book-card, .turn-btn { touch-action: manipulation; }
body { margin: 0; background: var(--sky); min-height: 100vh; min-height: 100dvh; line-height: 1.5; }
body:has(dialog[open]) { overflow: hidden; }
img { max-width: 100%; display: block; }
a { color: var(--ink); }
:focus-visible { outline: 4px solid var(--consonant); outline-offset: 3px; border-radius: 6px; }
svg { width: 24px; height: 24px; fill: currentColor; }
[hidden] { display: none !important; }

/* ---------- letter blocks ---------- */
.tile {
  --c: var(--consonant);
  display: inline-grid;
  place-items: center;
  min-width: 1.35em;
  padding: 0.05em 0.22em 0.12em;
  border: 0;
  border-radius: 0.22em;
  background: var(--c);
  color: #fff;
  font: inherit;
  font-weight: 700;
  line-height: 1.1;
  box-shadow: inset 0 -0.12em 0 rgb(0 0 0 / 0.22), 0 0.08em 0 rgb(0 0 0 / 0.12);
}
.k-consonant { --c: var(--consonant); }
.k-short { --c: var(--short); }
.k-long { --c: var(--long); }
.k-team { --c: var(--team); }
.k-digraph { --c: var(--digraph); }
.k-r { --c: var(--r); }
.k-silent { --c: var(--silent); }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45em;
  min-height: 44px;
  padding: 0.45em 1.1em;
  border: 3px solid var(--ink);
  border-radius: 999px;
  background: var(--ink);
  color: #fff;
  font: inherit;
  font-weight: 700;
  font-size: 1.05rem;
  text-decoration: none;
  cursor: pointer;
}
.btn.ghost { background: var(--paper); color: var(--ink); }
.btn.small { min-height: 36px; font-size: 0.95rem; padding: 0.25em 0.9em; }
.btn.big { min-height: 58px; font-size: 1.25rem; padding: 0.5em 1.4em; }
.btn:active { transform: translateY(2px); }
.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 50%;
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
}

/* ---------- home ---------- */
.masthead {
  max-width: 1100px;
  margin: 0 auto;
  padding: clamp(28px, 6vw, 64px) 16px 8px;
}
.logo {
  display: flex;
  flex-wrap: wrap;
  gap: 0.18em 0.4em;
  margin: 0;
  font-size: clamp(2.6rem, 1.5rem + 5.5vw, 5.2rem);
  line-height: 1.15;
}
.logo .word { display: inline-flex; gap: 0.07em; }
.logo .tile { transform: rotate(var(--tilt, 0deg)); }
.logo .word:nth-child(1) .tile:nth-child(2) { --tilt: -3deg; }
.logo .word:nth-child(1) .tile:nth-child(4) { --tilt: 2deg; }
.logo .word:nth-child(2) .tile:nth-child(1) { --tilt: 3deg; }
.logo .word:nth-child(3) .tile:nth-child(1) { --tilt: -2deg; }
.tagline { font-size: clamp(1.2rem, 1rem + 1vw, 1.6rem); margin: 0.6em 0 0; }
.masthead-tools { display: flex; align-items: center; gap: 12px; margin-top: 18px; }
.stars { font-weight: 700; font-size: 1.3rem; color: var(--long); background: var(--paper); padding: 4px 14px; border-radius: 999px; }

main { display: block; }
.shelf, .practice, .how { max-width: 1100px; margin: 0 auto; padding: 0 16px; }
.shelf { margin-top: 40px; }
.shelf-head h2, .practice h2, .how h2 { font-size: 1.7rem; margin: 0; }
.shelf-head p, .practice p { margin: 0.2em 0 0; color: var(--ink-soft); font-size: 1.1rem; }
.shelf-row {
  list-style: none;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 24px 20px;
  margin: 18px 0 0;
  padding: 0 4px 0;
  border-bottom: 16px solid var(--shelf);
  box-shadow: 0 6px 0 var(--shelf-dark);
  border-radius: 0 0 4px 4px;
}
.book-card {
  display: flex;
  flex-direction: column;
  height: 100%;
  text-decoration: none;
  padding-bottom: 10px;
}
.book-card img, .text-cover {
  aspect-ratio: 3 / 4;
  width: 100%;
  object-fit: cover;
  border-radius: 4px 10px 10px 4px;
  background: var(--paper);
  box-shadow: inset 6px 0 0 rgb(0 0 0 / 0.08), 0 6px 14px rgb(29 42 79 / 0.18);
  transition: transform 0.15s ease;
}
.text-cover { display: flex; align-items: center; justify-content: center; gap: 4px; font-size: 2.6rem; }
.book-card:focus-visible img { transform: translateY(-6px) rotate(-1deg); }
@media (hover: hover) {
  .book-card:hover img, .book-card:hover .text-cover { transform: translateY(-6px) rotate(-1deg); }
}
.book-title { margin-top: 10px; font-weight: 700; font-size: 1.1rem; line-height: 1.25; }
.book-progress { color: var(--ink-soft); font-size: 0.95rem; }

.practice { margin-top: 48px; }
.chips { list-style: none; display: flex; flex-wrap: wrap; gap: 10px; padding: 0; margin: 14px 0 0; }
.chip {
  border: 0;
  border-radius: 12px;
  background: var(--paper);
  color: var(--ink);
  font: inherit;
  font-size: 1.4rem;
  padding: 6px 16px;
  cursor: pointer;
}
.how { margin-top: 48px; }
.legend { list-style: none; padding: 0; margin: 14px 0 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(240px, 1fr)); gap: 10px 24px; font-size: 1.05rem; }
.legend li { display: flex; align-items: center; gap: 12px; }
.legend .tile { font-size: 1.5rem; min-width: 2em; }
.footer { max-width: 1100px; margin: 56px auto 0; padding: 24px 16px 40px; color: var(--ink-soft); }

/* ---------- reader ---------- */
.bar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px max(16px, env(safe-area-inset-right)) 10px max(16px, env(safe-area-inset-left));
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--sky);
}
.bar-title { flex: 1; min-width: 0; margin: 0; font-size: 1.15rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.counter { font-weight: 700; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.read-aloud.on { background: var(--short); border-color: var(--short); }

.book { max-width: 1180px; margin: 0 auto; padding: 4px 16px 120px; }
.page {
  display: grid;
  gap: clamp(16px, 3vw, 40px);
  align-items: center;
  min-height: calc(100vh - 200px);
  min-height: calc(100dvh - 200px);
  padding: clamp(18px, 4vw, 48px);
  background: var(--paper);
  border-radius: var(--radius-page);
  box-shadow: 0 2px 0 var(--sky-deep), 0 18px 40px rgb(29 42 79 / 0.12);
}
.art { margin: 0; display: grid; place-items: center; }
.art img { max-height: min(62vh, 640px); max-height: min(62dvh, 640px); width: auto; border-radius: 8px; }
.text { max-width: 30ch; margin: 0 auto; font-size: var(--read-size); line-height: 1.8; word-spacing: 0.12em; }
.text h2 { font-size: 1em; margin: 0 0 0.6em; color: var(--ink-soft); }
.text p { margin: 0 0 0.7em; }
.wordlist { display: flex; flex-wrap: wrap; gap: 0.3em; }
.wordlist .chip { background: var(--sky); font-size: 0.85em; padding: 0 0.45em; border-radius: 10px; cursor: default; }
@media (min-width: 860px) {
  .page.has-image { grid-template-columns: 1fr 1fr; }
  .page.has-image .text { max-width: 22ch; }
}

/* stop a long press from selecting text or popping up "Look Up" on iPad */
.book, .word-tiles { -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
.w { cursor: pointer; border-radius: 8px; padding: 0 0.04em; transition: background-color 0.12s; }
@media (hover: hover) { .w:hover { background: #fff3c4; } }
.w.active, .w.spoken { background: var(--glow); box-shadow: 0 0 0 0.08em var(--glow); }

.the-end .text { text-align: center; display: grid; gap: 16px; justify-items: center; }
.the-end h2 { font-size: 2em; color: var(--ink); }

.turn {
  position: fixed;
  inset: auto 0 0 0;
  display: flex;
  justify-content: space-between;
  padding: 16px max(16px, env(safe-area-inset-right)) max(16px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
  pointer-events: none;
}
.turn-btn {
  pointer-events: auto;
  display: grid;
  place-items: center;
  width: 72px;
  height: 72px;
  border: 0;
  border-radius: 50%;
  background: var(--ink);
  color: #fff;
  cursor: pointer;
  box-shadow: 0 6px 18px rgb(29 42 79 / 0.3);
}
.turn-btn svg { width: 36px; height: 36px; fill: none; stroke: currentColor; stroke-width: 3; stroke-linecap: round; stroke-linejoin: round; }
.turn-btn:disabled { opacity: 0; pointer-events: none; }
.turn-btn.next { background: var(--team); }

/* ---------- sheets (word helper, settings) ---------- */
.sheet {
  width: min(640px, 100vw);
  max-height: 92vh;
  max-height: 92dvh;
  overflow-y: auto;
  overscroll-behavior: contain;
  margin: auto auto 0;
  padding: 28px 20px 28px;
  padding-bottom: max(28px, env(safe-area-inset-bottom));
  border: 0;
  border-radius: 28px 28px 0 0;
  background: var(--paper);
  color: var(--ink);
  text-align: center;
}
.sheet::backdrop { background: rgb(29 42 79 / 0.45); }
.sheet[open] { animation: rise 0.22s ease-out; }
@keyframes rise { from { transform: translateY(40px); opacity: 0; } }
@media (min-width: 700px) {
  .sheet { margin: auto; border-radius: 28px; padding: 36px 32px; }
}
.sheet-close {
  position: absolute;
  top: 10px;
  right: 12px;
  width: 44px;
  height: 44px;
  border: 0;
  border-radius: 50%;
  background: var(--sky);
  color: var(--ink);
  font-size: 1.8rem;
  line-height: 1;
  cursor: pointer;
}
.lead { font-size: 1.5rem; font-weight: 700; margin: 0.2em 0 0.6em; }
.hint { font-size: 1.25rem; margin: 0.9em 0 0; }
.note { color: var(--ink-soft); margin: 0.6em 0 0; }
.tip { color: var(--ink-soft); margin: 1em 0 0; font-size: 0.95rem; }
.actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 12px; margin-top: 22px; }

.word-tiles {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 0.35em;
  font-size: clamp(2.6rem, 2rem + 4vw, 4rem);
  margin-top: 8px;
}
.word-tiles .syl { display: inline-flex; gap: 0.06em; }
.word-tiles .tile { cursor: pointer; transition: transform 0.15s ease, box-shadow 0.15s ease; }
.word-tiles .tile.lit { transform: translateY(-0.12em) scale(1.08); box-shadow: inset 0 -0.12em 0 rgb(0 0 0 / 0.22), 0 0 0 0.09em var(--glow); }
.word-tiles .tile.shush { opacity: 0.35; }
.word-tiles.plain .tile { --c: var(--paper); color: var(--ink); box-shadow: inset 0 0 0 3px var(--ink), inset 0 -0.12em 0 3px var(--ink); cursor: default; }
.word-tiles.plain .syl { gap: 0; }
.word-tiles.plain { gap: 0; }

.mic.listening { background: var(--short); border-color: var(--short); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { transform: scale(1.05); } }
.celebrate { font-size: 5rem; line-height: 1; color: var(--long); animation: pop 0.5s cubic-bezier(.3,1.6,.5,1); }
@keyframes pop { from { transform: scale(0.2) rotate(-40deg); } }

.settings { text-align: left; }
.settings-form h2 { margin: 0 40px 12px 0; }
.settings fieldset { border: 0; padding: 0; margin: 18px 0 0; display: grid; gap: 8px; }
.settings legend { font-weight: 700; margin-bottom: 6px; font-size: 1.05rem; }
.settings label { display: flex; align-items: center; gap: 10px; font-size: 1.05rem; }
.settings label.stack { flex-direction: column; align-items: stretch; gap: 4px; }
.settings input[type="radio"], .settings input[type="checkbox"] { width: 22px; height: 22px; accent-color: var(--ink); }
.settings select { font: inherit; padding: 8px; border-radius: 10px; border: 2px solid var(--ink-soft); }
.settings-form > .btn { margin-top: 24px; width: 100%; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

@media (max-width: 600px) {
  .bar-title { visibility: hidden; }
  .read-aloud span { display: none; }
  .read-aloud { width: 48px; padding: 0; }
  .art img { max-height: 36vh; }
  .page { min-height: 0; }
  .turn-btn { width: 60px; height: 60px; }
}
@media (max-width: 600px) {
  .word-tiles { font-size: clamp(2.2rem, 1.4rem + 4vw, 3rem); }
}
