/* The whole visual layer.
   =====================================================================

   This file is the single source of truth for colour, shape, elevation and
   motion. Nothing anywhere else in the repo may contain a hex colour, a
   radius, a shadow or a duration: JS that needs one imports the mirrored
   value from src/constants.js, and every other rule here uses a var().

   The world of the app is drawing on paper: warm paper, real ink, crayon
   pastel colour, and lines that draw themselves. The drawings are the
   loudest thing on every screen; everything else stays quiet. */

/* ------------------------------------------------------------------ */
/* fonts: vendored, no CDN, precached by the service worker            */
/* ------------------------------------------------------------------ */

@font-face {
  font-family: "Fredoka";
  src: url("../assets/fonts/fredoka-latin-var.woff2") format("woff2");
  font-weight: 300 700;
  font-stretch: 75% 125%;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "Nunito";
  src: url("../assets/fonts/nunito-latin-var.woff2") format("woff2");
  font-weight: 200 1000;
  font-style: normal;
  font-display: swap;
}

/* ------------------------------------------------------------------ */
/* design tokens                                                       */
/* ------------------------------------------------------------------ */

:root {
  /* surfaces and ink */
  --paper: #FDFAF3;        /* app background, unchanged from RUN1 */
  --ink: #1A1A1A;          /* drawing strokes, primary text */
  --pencil: #B9B2A6;       /* inactive states, hairlines, captions */

  /* pastel accent set, rotated across drawing cards in this order */
  --rose: #F6C6D0;
  --peach: #FFD9B8;
  --butter: #FBEFB2;
  --mint: #C9E8CF;
  --sky: #BFE0F2;
  --lilac: #D9CDF0;

  /* interactive */
  --primary: #6F5BB5;      /* buttons, active toggle, focus ring: deep lilac */
  --primary-soft: #EFEAF9; /* pressed and hover fills */
  --star: #D9A521;         /* difficulty stars */

  /* shape and elevation */
  --radius-lg: 20px;
  --radius-md: 14px;
  --shadow-soft: 0 2px 12px rgba(56, 44, 25, 0.10);

  /* motion */
  --ease: cubic-bezier(0.25, 0.9, 0.35, 1);
  --t-fast: 160ms;
  --t-med: 260ms;

  /* motion, continued: the self-drawing motifs and the all-done moment.
     Mirrored in constants.js as SELF_DRAW_MS and CELEBRATE_MS. */
  --t-draw: 600ms;
  --t-celebrate: 2000ms;

  /* type */
  --font-display: "Fredoka", "Trebuchet MS", "Segoe UI", sans-serif;
  --font-body: "Nunito", "Segoe UI", system-ui, sans-serif;
  --fs-title: 34px;
  --fs-step: 26px;
  --fs-card: 18px;
  --fs-body: 16px;
  --fs-caption: 13px;
  --lh-display: 1.15;
  --lh-body: 1.35;

  /* touch and layout */
  --tap: 48px;            /* ground rule: nothing interactive is smaller */
  --tap-player: 56px;     /* player controls */
  --tap-play: 64px;       /* the primary play/pause button */
  --gutter: 24px;
  --pill-gap: 8px;
}

/* Every drawing card, and the player opened from it, carries one accent from
   the set above. The rotation is applied by index, in token order. */
.accent-0 { --accent: var(--rose); }
.accent-1 { --accent: var(--peach); }
.accent-2 { --accent: var(--butter); }
.accent-3 { --accent: var(--mint); }
.accent-4 { --accent: var(--sky); }
.accent-5 { --accent: var(--lilac); }

/* ------------------------------------------------------------------ */
/* base                                                                */
/* ------------------------------------------------------------------ */

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  font-size: var(--fs-body);
  line-height: var(--lh-body);
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  overscroll-behavior: none;
}

#app {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
}

button {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
  cursor: pointer;
}

/* One focus treatment, on everything, keyboard and touch alike. */
:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 3px;
}

.screen {
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* The self-drawing underline motif. One authored stroke, animated by the
   app's own stroke renderer. */
.underline {
  display: block;
  width: 100%;
  height: 16px;
}

/* ------------------------------------------------------------------ */
/* library                                                             */
/* ------------------------------------------------------------------ */

.library-head {
  padding: 22px var(--gutter) 10px;
  padding-top: max(22px, env(safe-area-inset-top));
  flex: 0 0 auto;
}

.wordmark {
  display: inline-block;
  min-width: 0;
}

.library-head h1 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: var(--fs-title);
  line-height: var(--lh-display);
  letter-spacing: 0.2px;
}

.library-head .subtitle {
  margin: 6px 0 0;
  font-size: var(--fs-body);
  color: var(--ink);
}

.grid {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(165px, 1fr));
  gap: 16px;
  padding: 14px var(--gutter) 8px;
  /* Every row is exactly as tall as its content. The grid is a flex child
     with a definite height, so without this the row tracks are sized from
     the leftover space instead: cards stretched to the viewport on a tall
     window, and squashed under their own thumbnail on a short one. */
  grid-auto-rows: max-content;
  align-content: start;
  align-items: stretch;
}

.card {
  appearance: none;
  border: none;
  background: var(--accent);
  border-radius: var(--radius-lg);
  padding: 10px 10px 12px;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 8px;
  min-height: var(--tap);
  text-align: left;
  transition: transform var(--t-fast) var(--ease);
}

.card:active { transform: scale(0.97); }

/* the paper inset the thumbnail sits on, with its hand-drawn border */
.thumb {
  position: relative;
  border-radius: var(--radius-md);
  background: var(--paper);
  overflow: hidden;
}

.thumb canvas {
  width: 100%;
  height: auto;
  aspect-ratio: 1 / 1;
  display: block;
}

.thumb .frame {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}

.thumb .frame path {
  fill: none;
  stroke: var(--pencil);
  stroke-width: 1.6;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.card .card-title {
  font-size: var(--fs-card);
  font-weight: 700;
  line-height: var(--lh-body);
}

.stars {
  display: flex;
  gap: 3px;
  line-height: 1;
  /* pinned to the bottom, so a title that wraps does not shove the stars
     out of line with the neighbouring cards */
  margin-top: auto;
}

.stars svg { width: 15px; height: 15px; display: block; }
.stars .on { color: var(--star); }
.stars .off { color: var(--pencil); }

.library-foot {
  flex: 0 0 auto;
  padding: 4px var(--gutter) 12px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  text-align: right;
}

.version {
  color: var(--pencil);
  font-size: var(--fs-caption);
  letter-spacing: 0.4px;
}

/* ------------------------------------------------------------------ */
/* player                                                              */
/* ------------------------------------------------------------------ */

.player {
  position: relative;
  height: 100%;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.player-top {
  padding: 12px 16px 2px;
  padding-top: max(12px, env(safe-area-inset-top));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  pointer-events: none;
  flex: 0 0 auto;
}

.step-label {
  font-family: var(--font-display);
  font-weight: 500;
  font-size: var(--fs-step);
  line-height: var(--lh-display);
  text-align: center;
  min-height: calc(var(--fs-step) * var(--lh-display));
}

.player-top .underline {
  width: 150px;
  height: 14px;
}

.dots {
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: center;
  min-height: 14px;
  margin-top: 2px;
}

.dot {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid var(--pencil);
  background: transparent;
  transition: background-color var(--t-fast) var(--ease),
              border-color var(--t-fast) var(--ease);
}

.dot.done {
  background: var(--ink);
  border-color: var(--ink);
}

.canvas-wrap {
  flex: 1 1 auto;
  min-height: 0;
  position: relative;
  /* room for the floating control pill, so the drawing is never behind it */
  margin-bottom: calc(var(--tap-play) + 28px + env(safe-area-inset-bottom));
}

.canvas-wrap > canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

/* the drawing settles when the last stroke lands */
.canvas-wrap.settled > canvas {
  animation: settle var(--t-celebrate) var(--ease) 1;
}

@keyframes settle {
  0%   { transform: scale(1); }
  14%  { transform: scale(1.02); }
  34%  { transform: scale(1); }
  100% { transform: scale(1); }
}

/* ---- controls: a floating paper pill, centred at the bottom ---- */

.control-bar {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: max(14px, env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  gap: var(--pill-gap);
  padding: 8px;
  background: var(--paper);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-soft);
  max-width: calc(100% - 20px);
}

.ctl {
  appearance: none;
  border: none;
  background: transparent;
  border-radius: var(--radius-md);
  min-width: var(--tap-player);
  min-height: var(--tap-player);
  padding: 4px 6px;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  color: var(--ink);
  transition: background-color var(--t-fast) var(--ease),
              transform var(--t-fast) var(--ease);
}

.ctl:hover { background: var(--primary-soft); }
.ctl:active { transform: scale(0.97); background: var(--primary-soft); }
.ctl svg { width: 26px; height: 26px; display: block; }

.ctl .value {
  font-size: var(--fs-caption);
  font-weight: 700;
  color: var(--pencil);
  line-height: 1;
}

.ctl.primary {
  min-width: var(--tap-play);
  min-height: var(--tap-play);
  background: var(--primary);
  color: var(--paper);
  border-radius: 50%;
}

.ctl.primary:hover,
.ctl.primary:active { background: var(--primary); }
.ctl.primary svg { width: 30px; height: 30px; }

.ctl[disabled] { color: var(--pencil); }

/* the big "next" arrow used in tap mode */
.next-arrow {
  position: absolute;
  right: 20px;
  bottom: 20px;
  width: 96px;
  height: 96px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: var(--paper);
  display: none;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-soft);
  transition: transform var(--t-fast) var(--ease);
}

.next-arrow.show { display: inline-flex; }
.next-arrow svg { width: 46px; height: 46px; }
.next-arrow:active { transform: scale(0.97); }

/* ---- the all-done moment ---- */

.celebrate {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.drift {
  position: absolute;
  bottom: calc(12% + var(--y) * 1px);
  left: var(--x);
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0;
  animation: drift var(--t-celebrate) var(--ease) 1;
  animation-delay: calc(var(--i) * var(--t-celebrate) / 24);
}

/* The dots hold their colour for most of the drift: a pastel that spends its
   whole life fading is a smudge on paper, not a dot. */
@keyframes drift {
  0%   { opacity: 0; transform: translateY(0) scale(0.5); }
  12%  { opacity: 1; transform: translateY(-12px) scale(1); }
  65%  { opacity: 1; }
  100% { opacity: 0; transform: translateY(-200px) scale(0.9); }
}

/* The two buttons sit across the full width and centre themselves: an
   absolutely positioned box anchored at the middle would only be allowed
   half the width, which wraps "Draw it again" onto three lines on a phone. */
.done-actions {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 14px;
  display: none;
  justify-content: center;
  flex-wrap: wrap;
  gap: 12px;
  pointer-events: none;
  animation: rise var(--t-med) var(--ease) 1;
}

.done-actions.show { display: flex; }
.done-actions .btn { pointer-events: auto; }

@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.btn {
  appearance: none;
  border: 2px solid var(--primary);
  background: var(--paper);
  color: var(--primary);
  border-radius: var(--radius-lg);
  min-height: var(--tap);
  padding: 10px 20px;
  font-weight: 700;
  white-space: nowrap;
  box-shadow: var(--shadow-soft);
  transition: background-color var(--t-fast) var(--ease),
              transform var(--t-fast) var(--ease);
}

.btn:active { transform: scale(0.97); }
.btn:hover { background: var(--primary-soft); }

.btn.primary {
  background: var(--primary);
  color: var(--paper);
}

.btn.primary:hover { background: var(--primary); }

/* ------------------------------------------------------------------ */
/* chooser: which way would she like to draw it                        */
/* ------------------------------------------------------------------ */

.chooser {
  padding: 18px var(--gutter) max(18px, env(safe-area-inset-bottom));
  padding-top: max(18px, env(safe-area-inset-top));
  gap: 18px;
}

.chooser-head {
  display: flex;
  align-items: center;
  gap: 14px;
  flex: 0 0 auto;
}

.chooser-back {
  flex: 0 0 auto;
  background: var(--paper);
  box-shadow: var(--shadow-soft);
}

.chooser-title { min-width: 0; }

.chooser-title h1 {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: var(--fs-title);
  line-height: var(--lh-display);
}

.chooser-title .underline { width: 100%; height: 14px; }

.chooser-body {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 24px;
  flex-wrap: wrap;
  align-content: center;
  overflow-y: auto;
}

/* The drawing is here to say which drawing this is, not to be the loudest
   thing on a screen whose whole job is the two choices. */
.chooser-thumb {
  flex: 0 1 auto;
  width: min(210px, 32vw);
  border-radius: var(--radius-lg);
  background: var(--paper);
  box-shadow: var(--shadow-soft);
  padding: 10px;
}

.chooser-thumb canvas {
  width: 100%;
  height: auto;
  aspect-ratio: 1 / 1;
  display: block;
  border-radius: var(--radius-md);
}

.choices {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  justify-content: center;
}

.choice {
  appearance: none;
  border: none;
  background: var(--accent);
  border-radius: var(--radius-lg);
  width: 240px;
  min-height: 210px;
  padding: 24px 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  text-align: center;
  transition: transform var(--t-fast) var(--ease),
              box-shadow var(--t-fast) var(--ease);
}

.choice:active { transform: scale(0.97); }

.choice-art {
  width: 72px;
  height: 72px;
  border-radius: 50%;
  background: var(--paper);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.choice-art svg { width: 42px; height: 42px; display: block; }

.choice-label {
  font-size: var(--fs-card);
  font-weight: 700;
  line-height: var(--lh-body);
}

/* Marked, not chosen: the way she drew last time. She still taps. */
.choice.preselected {
  box-shadow: 0 0 0 3px var(--primary), var(--shadow-soft);
}

/* ------------------------------------------------------------------ */
/* trace                                                              */
/* ------------------------------------------------------------------ */

/* Her finger owns this canvas: no scrolling, no zooming, no gestures. */
.trace-canvas {
  touch-action: none;
  cursor: crosshair;
}

/* Press and hold a control to be told what it does. */
.ctl { position: relative; }

.press-label {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  padding: 6px 10px;
  border-radius: var(--radius-md);
  background: var(--ink);
  color: var(--paper);
  font-size: var(--fs-caption);
  font-weight: 700;
  line-height: 1;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--t-fast) var(--ease);
}

.ctl.pressing .press-label { opacity: 1; }

/* ------------------------------------------------------------------ */
/* messages (loading, refused pack)                                    */
/* ------------------------------------------------------------------ */

.message {
  margin: auto;
  max-width: 46rem;
  padding: var(--gutter);
  text-align: left;
}

.message h2 {
  margin-top: 0;
  font-family: var(--font-display);
  font-weight: 600;
  font-size: var(--fs-step);
  line-height: var(--lh-display);
}

.message pre {
  white-space: pre-wrap;
  background: var(--paper);
  border: 1px solid var(--pencil);
  border-radius: var(--radius-md);
  padding: 12px;
  font-size: var(--fs-caption);
  overflow-x: auto;
}

/* ------------------------------------------------------------------ */
/* narrow screens                                                      */
/* ------------------------------------------------------------------ */

@media (max-width: 560px) {
  :root {
    --gutter: 16px;
    --fs-title: 30px;
  }

  .grid {
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    gap: 12px;
  }

  .next-arrow { width: 76px; height: 76px; right: 14px; bottom: 14px; }
  .control-bar { gap: 4px; padding: 6px; }
  .ctl { min-width: var(--tap-player); padding: 4px 2px; }

  .chooser-body { gap: 16px; }
  .chooser-thumb { width: min(200px, 46vw); }
  .choice { width: min(300px, 100%); min-height: 150px; padding: 18px; }
  .choice-art { width: 60px; height: 60px; }
  .choice-art svg { width: 34px; height: 34px; }
}

/* ------------------------------------------------------------------ */
/* reduced motion: decoration stops, content does not                  */
/* ------------------------------------------------------------------ */

@media (prefers-reduced-motion: reduce) {
  /* Stroke playback in the player is content, not decoration, and is
     exempt: it is drawn to a canvas and is not affected by any rule here.
     Everything decorative is switched off. */
  *,
  *::before,
  *::after {
    animation-duration: 1ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 1ms !important;
  }

  .drift { display: none; }
  .canvas-wrap.settled > canvas { animation: none; }
}
