:root {
  --text: #2d2a55;
  --soft: #7d7aa6;
  --accent: #6c5ce7;
  --accent-deep: #5143c4;
  --gold: #ffbe1a;
  --go: #2fb350;
  --shade: rgba(64, 54, 130, 0.16);
  font-family: ui-rounded, "SF Pro Rounded", "Nunito", "Quicksand", "Arial Rounded MT Bold",
    system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }

html, body { height: 100%; margin: 0; }

/* The page never scrolls or zooms: a finger dragged across it is always drawing. */
body {
  color: var(--text);
  background: linear-gradient(160deg, #bfe3ff 0%, #d9ccff 55%, #ffd3e4 100%);
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: manipulation;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

button {
  font: inherit;
  color: inherit;
  border: 0;
  cursor: pointer;
  touch-action: manipulation;
}
button:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.sr {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.star {
  width: 1em;
  height: 1em;
  flex: none;
  fill: #dcd9ee;
  stroke: #dcd9ee;
  stroke-width: 2;
  stroke-linejoin: round;
}
.star.on { fill: var(--gold); stroke: var(--gold); }

/* ---------- layout ---------- */

.app {
  height: 100vh;
  height: 100dvh;
  max-width: 1180px;
  margin: 0 auto;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  display: grid;
  gap: 10px;
  grid-template:
    "bar" auto
    "stage" minmax(0, 1fr)
    "tools" auto
    "picker" auto
    / minmax(0, 1fr);
}

.bar {
  grid-area: bar;
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand {
  margin: 0 auto 0 0;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 24px;
  font-weight: 800;
  white-space: nowrap;
}

.pill {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 14px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.85);
  box-shadow: 0 2px 0 var(--shade);
  font-size: 18px;
  font-weight: 800;
}
.pill .star { font-size: 20px; }

.seg {
  display: flex;
  min-width: 0;
  padding: 3px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.6);
}
.seg button {
  flex: 1 1 auto;
  min-height: 38px;
  padding: 0 13px;
  border-radius: 999px;
  background: transparent;
  font-size: 16px;
  font-weight: 800;
  white-space: nowrap;
}
.seg button[aria-pressed="true"] {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 2px 0 var(--accent-deep);
}

.icon-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.85);
  box-shadow: 0 2px 0 var(--shade);
  font-size: 20px;
}
.icon-btn[aria-pressed="false"] { opacity: 0.6; }

.brand a { display: flex; align-items: center; gap: inherit; color: inherit; text-decoration: none; }
.brand a:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; border-radius: 8px; }

/* The way to the other game: its picture, and its name when there is room. */
.hop {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 44px;
  height: 44px;
  padding: 0 7px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.85);
  box-shadow: 0 2px 0 var(--shade);
  color: inherit;
  font-size: 17px;
  font-weight: 800;
  text-decoration: none;
  white-space: nowrap;
}
.hop:active { transform: translateY(2px); box-shadow: none; }
.hop:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.hop img { width: 30px; height: 30px; }
.hop-icon { width: 30px; font-size: 22px; line-height: 1; text-align: center; }
.hop .lbl { display: none; }
@media (min-width: 1000px) {
  .hop { padding: 0 16px 0 8px; }
  .hop .lbl { display: inline; }
}

/* ---------- the tracing board ---------- */

.stage { grid-area: stage; position: relative; min-height: 0; }

#board {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  touch-action: none;
  cursor: crosshair;
}

.paper-shadow { fill: var(--shade); }
.paper { fill: #fff; }

.guide { stroke-width: 1.2; stroke-linecap: round; }
.guide.top { stroke: #b9dcff; }
.guide.mid { stroke: #ffb0c4; stroke-dasharray: 4 5; }
.guide.base { stroke: #6fb4ff; stroke-width: 1.8; }
.guide.tail { stroke: #e3e6f2; }

/* The grey road is the letter; the stroke to trace next is a shade darker. */
.road {
  fill: none;
  stroke: #e7eaf5;
  stroke-width: 22;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.road.now { stroke: #d5daee; }
circle.road { fill: #e7eaf5; stroke: none; }
circle.road.now { fill: #d5daee; }

.fill {
  fill: none;
  stroke-width: 22;
  stroke-linecap: round;
  stroke-linejoin: round;
}
circle.fill { stroke: none; }

.dash {
  fill: none;
  stroke: #fff;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-dasharray: 0.1 7.9;
}

.ink polyline {
  fill: none;
  stroke-width: 7;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.ink polyline.off {
  stroke: #b6b9cc;
  stroke-width: 4;
  stroke-dasharray: 0.1 8;
}
.ink circle { stroke: none; }

.demo-line {
  fill: none;
  stroke-width: 8;
  stroke-linecap: round;
  stroke-linejoin: round;
  opacity: 0.85;
}

#arrow {
  fill: none;
  stroke: #1f8f3d;
  stroke-width: 3.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

#ghost { fill: #fff; stroke-width: 3.5; }

#marker .halo,
#marker .dot { transform-box: fill-box; transform-origin: center; }
#marker .halo { fill: var(--go); animation: halo 1.5s ease-out infinite; }
#marker .dot { fill: var(--go); stroke: #fff; stroke-width: 2.5; }
#marker text { fill: #fff; font-size: 12px; font-weight: 800; text-anchor: middle; }
#marker.nudge .dot { animation: nudge 0.5s ease; }

@keyframes halo {
  from { transform: scale(1); opacity: 0.45; }
  to { transform: scale(2.2); opacity: 0; }
}
@keyframes nudge {
  0%, 100% { transform: scale(1); }
  35% { transform: scale(1.7); }
  70% { transform: scale(0.9); }
}

/* ---------- buttons under the board ---------- */

.tools {
  grid-area: tools;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-height: 48px;
  padding: 0 18px;
  border-radius: 999px;
  background: #fff;
  box-shadow: 0 3px 0 var(--shade);
  font-size: 17px;
  font-weight: 800;
  white-space: nowrap;
}
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--shade); }
.btn.primary { background: var(--accent); color: #fff; box-shadow: 0 3px 0 var(--accent-deep); }
.btn.primary:active { box-shadow: 0 1px 0 var(--accent-deep); }
.btn.round { width: 48px; padding: 0 0 3px; font-size: 28px; line-height: 1; }

/* ---------- the letter picker ---------- */

.picker {
  grid-area: picker;
  display: grid;
  gap: 6px;
  grid-auto-flow: column;
  grid-auto-columns: max-content;
  padding: 2px 2px 4px;
  overflow-x: auto;
  scroll-snap-type: x proximity;
  scrollbar-width: none;
}
.picker::-webkit-scrollbar { display: none; }

.tile {
  display: grid;
  place-items: center;
  align-content: center;
  gap: 2px;
  min-width: 52px;
  min-height: 56px;
  padding: 3px 0 5px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.78);
  box-shadow: 0 2px 0 var(--shade);
  font-weight: 800;
  scroll-snap-align: center;
}
.tile-ch { font-size: 23px; line-height: 1.1; white-space: nowrap; }
.picker[data-kind="word"] .tile { padding-left: 12px; padding-right: 12px; }
.picker[data-kind="word"] .tile-ch { font-size: 20px; line-height: 1.25; }
.tile-stars { display: flex; gap: 1px; font-size: 11px; }
.tile[aria-current="true"] {
  background: var(--accent);
  color: #fff;
  box-shadow: 0 2px 0 var(--accent-deep);
}
.tile[aria-current="true"] .star:not(.on) { fill: #9a8df0; stroke: #9a8df0; }

/* ---------- the reward card ---------- */

.cheer {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  place-items: center;
  padding: 12px;
}
.cheer[hidden] { display: none; }

.cheer-card {
  width: min(330px, 100%);
  max-height: 100%;
  overflow: auto;
  padding: 16px 20px 20px;
  border-radius: 28px;
  background: rgba(255, 255, 255, 0.94);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  box-shadow: 0 16px 40px rgba(64, 54, 130, 0.3);
  text-align: center;
  container-type: inline-size;
  animation: pop 0.45s cubic-bezier(0.2, 1.4, 0.4, 1) both;
}
.cheer-emoji { font-size: 64px; line-height: 1.15; white-space: pre-line; overflow-wrap: anywhere; }
.cheer-emoji.as-word { color: var(--accent); font-size: 56px; font-weight: 800; }
/* Counting pictures shrink so a row of five or ten always fits the card. */
.cheer-emoji.medium { font-size: min(44px, 14cqw); }
.cheer-emoji.small { font-size: min(36px, 13cqw); line-height: 1.25; }
.cheer-emoji.tiny { font-size: min(22px, 6.5cqw); line-height: 1.3; }
.cheer-word { margin: 2px 0 8px; font-size: 22px; font-weight: 700; }
.cheer-word b { color: var(--accent); font-size: 1.25em; font-weight: 800; }
.cheer-stars { display: flex; justify-content: center; gap: 6px; font-size: 56px; }
.cheer-stars .star.on { animation: star-pop 0.5s cubic-bezier(0.2, 1.6, 0.4, 1) both; }
.cheer-stars .star.on:nth-child(1) { animation-delay: 0.2s; }
.cheer-stars .star.on:nth-child(2) { animation-delay: 0.45s; }
.cheer-stars .star.on:nth-child(3) { animation-delay: 0.7s; }
.cheer-card h2 { margin: 6px 0 2px; font-size: 28px; font-weight: 800; }
.cheer-meter { margin: 0 0 14px; color: var(--soft); font-size: 15px; font-weight: 700; }
.cheer-actions { display: flex; justify-content: center; gap: 10px; }
.cheer-actions .btn { flex: 1 1 0; min-width: 0; padding: 0 10px; background: #f0eefc; box-shadow: 0 3px 0 #d9d4f5; }
.cheer-actions .btn.primary { background: var(--accent); box-shadow: 0 3px 0 var(--accent-deep); }

@keyframes pop {
  from { transform: scale(0.7); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}
@keyframes star-pop {
  from { transform: scale(0) rotate(-40deg); }
  to { transform: scale(1) rotate(0); }
}

#fx {
  position: fixed;
  inset: 0;
  z-index: 5;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

/* ---------- other screen shapes ---------- */

/* Roomy portrait screens (a tablet held upright): the alphabet fits in two rows. */
@media (min-width: 720px) {
  .picker {
    grid-auto-flow: row;
    grid-template-columns: repeat(var(--columns, 13), minmax(0, 1fr));
    overflow: visible;
  }
  .tile { min-width: 0; }
  .picker[data-kind="word"] .tile { padding-left: 2px; padding-right: 2px; }
}

/* Landscape: the board takes the full height and the alphabet sits beside it. */
@media (orientation: landscape) and (min-width: 640px) {
  .app {
    column-gap: 18px;
    grid-template:
      "bar bar" auto
      "stage picker" minmax(0, 1fr)
      "tools picker" auto
      / minmax(0, 1fr) min(340px, 40vw);
  }
  .picker {
    grid-auto-flow: row;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    grid-auto-rows: minmax(0, 66px);
    align-content: center;
    overflow: hidden;
  }
  .tile { min-width: 0; min-height: 0; }
  .picker[data-kind="word"] .tile { padding-left: 2px; padding-right: 2px; }
}

/* A phone on its side has very little height, so the board gets all of it and
   everything else stacks beside it. */
@media (orientation: landscape) and (max-height: 480px) {
  .app {
    column-gap: 14px;
    grid-template:
      "stage bar" auto
      "stage picker" minmax(0, 1fr)
      "stage tools" auto
      / minmax(0, 1fr) min(340px, 46vw);
  }
  .picker {
    grid-auto-flow: row;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    grid-auto-rows: minmax(0, 66px);
    align-content: center;
    overflow: hidden;
  }
  .tile { min-width: 0; min-height: 0; }
  .picker[data-kind="word"] .tile { padding-left: 2px; padding-right: 2px; }
  .brand { display: none; }
  .seg { flex: 1; }
  .seg button { padding: 0 4px; font-size: 15px; }
  .bar { gap: 6px; }
  .pill { gap: 4px; padding: 0 8px; }
}

/* Without room for the name, the tabs stretch to fill the top bar. */
@media (max-width: 660px) {
  .brand { display: none; }
  .seg { flex: 1; }
  .seg button { padding: 0 4px; font-size: 15px; }
  .pill { padding: 0 10px; }
}

/* Narrow phones: the top bar also holds the star count, sound and the other
   game, so it tightens up, and the narrowest drop the star count. */
@media (max-width: 420px) {
  .bar { gap: 6px; }
  .pill { gap: 4px; padding: 0 8px; font-size: 16px; }
  .seg button { font-size: 14px; }
}

/* Narrow phones: keep the buttons under the board on one line. */
@media (max-width: 460px) {
  .tools { gap: 8px; }
  .btn { padding: 0 12px; font-size: 16px; }
}

/* The very smallest screens: pictures only, and no star count. */
@media (max-width: 350px) {
  .app { padding-left: 8px; padding-right: 8px; }
  .btn .lbl { display: none; }
  .btn { min-width: 48px; }
}
@media (max-width: 360px) {
  .pill { display: none; }
}

/* Short screens (a phone on its side): shrink the chrome, keep the board big. */
@media (max-height: 480px) {
  .app { gap: 6px; padding-top: 6px; }
  .brand { font-size: 20px; }
  .pill, .icon-btn, .hop { min-height: 38px; height: 38px; }
  .icon-btn { width: 38px; }
  .hop { min-width: 38px; }
  .hop img { width: 26px; height: 26px; }
  .seg button { min-height: 32px; }
  .btn { min-height: 40px; }
  .btn.round { width: 40px; }
  .tile-ch { font-size: 18px; }
  .picker[data-kind="word"] .tile-ch { font-size: 16px; }
  .tile-stars { font-size: 9px; }
  .cheer-card { padding: 10px 16px 14px; }
  .cheer-emoji, .cheer-emoji.as-word, .cheer-emoji.medium { font-size: 36px; }
  .cheer-emoji.small { font-size: 24px; }
  .cheer-emoji.tiny { font-size: 18px; }
  .cheer-word { font-size: 17px; margin-bottom: 4px; }
  .cheer-stars { font-size: 36px; }
  .cheer-card h2 { font-size: 20px; }
  .cheer-meter { margin-bottom: 8px; font-size: 13px; }
}

@media (prefers-reduced-motion: reduce) {
  #marker .halo { animation: none; opacity: 0; }
  .cheer-card, .cheer-stars .star.on { animation: none; }
}
