/* Spec 052: Home's polish on the web, the twin of the phone's. The sky card is the way to the
   studio, friends stay whole in their tile corners in their own poses, art beside them, sparkles
   twinkle, friends wiggle or hop now and then (most of each cycle is still), tiles sink under a
   finger, Home's delights (spec 177: the butterfly, friends peeking, a shooting star), confetti for a saved picture or drawing, a toast that goes, and a
   gentle slide as a page arrives. Hidden tabs (html.is-still) and reduced motion stop all of it.
   Tokens only; poses use the individual transform properties so animations compose with them. */

/* --- The sky card opens the studio. --- */
/* Spec 170: as on the phone, the card itself does not sink; its yellow "Make a story" does (app.css's press). A mouse still
   lifts it a little. */
.kid-sky:has(.kid-draw:not([hidden])) {
  cursor: pointer;
  transition: translate 160ms ease;
}

@media (hover: hover) {
  .kid-sky:has(.kid-draw:not([hidden])):hover {
    translate: 0 -2px;
  }
}

/* Spec 170: the app's "Make a story" on the sky card, flat. Spec 181: the one yellow, the app's KidButton (64, 24 round,
   22 bold, 14 by 24), as the phone's "Make a story" now is. */
.kid-draw {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  margin-top: var(--space-3);
  min-height: var(--ui-height-kid-button);
  padding: var(--ui-pad-kid-button);
  border-radius: var(--ui-radius-button);
  background: var(--crayon-yellow);
  color: var(--on-crayon);
  font-size: var(--ui-font-kid-button);
  font-weight: 700;
  text-decoration: none;
}

.kid-draw[hidden] {
  display: none;
}

/* The whole card is the link's tap area (the link is the only thing a screen reader reads). */
.kid-draw::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1;
}

.kid-draw .icon {
  width: 1.4em;
  height: 1.4em;
}

.kid-art {
  position: absolute;
  pointer-events: none;
}

/* Spec 112 (the owner, build 22: "I love the rainbow in make a story pull but overlaps with the text, fix that"): the
   sky card is a grid, and the rainbow has its own cell, top right beside "Hi, {name}!" ("art"), never an absolute spot
   over the words. The greeting's column gives it room (the greeting wraps, never under it); the question and Make a
   story are in the rows under both, so no width and no language puts the art on a word or the button. Its float drifts
   right and up only, into the card's padding. The star sits low on the left, beside the friends, under every word. */
.kid-sky {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas:
    "hello art"
    "prompt prompt"
    "cta cta";
  column-gap: var(--space-3);
  align-content: start;
  align-items: start;
}

.kid-sky .kid-hello {
  grid-area: hello;
  margin: 0;
  overflow-wrap: anywhere;
}

.kid-sky .kid-prompt {
  grid-area: prompt;
  margin-top: var(--space-2);
}

.kid-sky .kid-draw {
  grid-area: cta;
  justify-self: start;
}

.kid-sky .kid-rainbow {
  grid-area: art;
  position: relative;
  align-self: start;
  justify-self: end;
  width: clamp(64px, 11vw, 112px);
  height: auto;
  margin-inline-end: var(--space-2);
  animation: art-float 7s ease-in-out infinite;
}

.kid-star {
  bottom: var(--space-6);
  left: 3%;
  width: 34px;
  height: auto;
  rotate: -12deg;
  animation: twinkle-now-and-then 5s ease-in-out infinite;
}

.kid-sky .sparkle {
  animation: twinkle-now-and-then 4.5s ease-in-out infinite;
}

.kid-sky .sparkle-2 { animation-delay: -1.6s; }
.kid-sky .sparkle-3 { animation-delay: -3.1s; }

.kid-friend {
  animation: friend-hop 9s ease-in-out infinite;
}

.kid-dragon {
  animation-name: friend-wiggle;
  animation-duration: 8s;
  animation-delay: -2s;
}

.kid-deer {
  animation-delay: -5s;
}

/* --- Tiles: friends whole in their top corner, each in its own pose, with a piece of art. --- */
.tile-friend {
  top: 4px;
  right: 4px;
  height: calc(var(--friend-height, 100px) * var(--friend-scale, 1));
  transform: none;
  rotate: 6deg;
  transform-origin: 50% 100%;
  animation: friend-wiggle 10s ease-in-out infinite;
}

.tile-friend.pose-deer { --friend-height: 96px; rotate: 6deg; }
.tile-friend.pose-deer-flip { --friend-height: 84px; rotate: -10deg; scale: -1 1; animation-name: friend-hop; animation-delay: -3s; }
.tile-friend.pose-unicorn { --friend-height: 96px; rotate: -8deg; animation-name: friend-hop; animation-delay: -6s; }
.tile-friend.pose-unicorn-flip { --friend-height: 104px; rotate: -5deg; scale: -1 1; animation-delay: -4s; }
.tile-friend.pose-dragon { --friend-height: 100px; rotate: 6deg; animation-delay: -7s; }
.tile-friend.pose-dragon-flip { --friend-height: 96px; rotate: -4deg; scale: -1 1; animation-name: friend-hop; animation-delay: -1s; }
/* Spec 075: the supporting friends on the other tiles, so each of the three main friends peeks from one tile only.
   The turtle is wide and low, so it is shorter. */
.tile-friend.pose-fairy { --friend-height: 100px; rotate: 6deg; animation-delay: -5s; }
.tile-friend.pose-fox { --friend-height: 92px; rotate: 4deg; animation-name: friend-hop; animation-delay: -2s; }
.tile-friend.pose-turtle-flip { --friend-height: 60px; rotate: -4deg; scale: -1 1; animation-delay: -8s; }
.tile-friend.pose-bunny { --friend-height: 96px; rotate: -6deg; animation-name: friend-hop; animation-delay: -4s; }
/* Spec 103: Bedtime's owl, awake under the stars (the phone's tile: rotated -8, bobbing). */

/* The art sits in the tile's free top-left corner (the friend has the right, the word the bottom). */
.tile-art {
  position: absolute;
  pointer-events: none;
  top: 12px;
  left: 14px;
  width: 36px;
  height: auto;
}

.tile-art.art-rainbow { width: 54px; animation: art-float 7s ease-in-out infinite; }
.tile-art.art-cloud { width: 50px; top: 16px; animation: art-float 8s ease-in-out infinite -3s; }
.tile-art.art-star { width: 30px; animation: twinkle-now-and-then 5s ease-in-out infinite -2s; }
.tile-art.art-knight { rotate: -8deg; }

/* The tile's word never runs under the friend: it wraps instead. */
.tile > span:not(.soon) {
  max-width: 100%;
  overflow-wrap: anywhere;
}

/* Spec 095 (replacing spec 094's line under the name): a tile best used with a grown-up carries a round badge, a
   picture only, in its top-left corner, where the art was; the art moves under the friend, clear of the badge, the
   heart and the friend. The legend under the tiles says what it means, in the quiet caption colour. */
.grown-up-badge {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--elevation-raised);
}

.grown-up-badge .icon {
  width: 22px;
  height: 22px;
}

.tile > .grown-up-badge {
  position: absolute;
  top: 10px;
  left: 10px;
  pointer-events: none;
}

.tile.with-grown-up > .tile-art {
  top: 64px;
  left: auto;
  right: 72px;
}

.grown-up-badge.small {
  width: 24px;
  height: 24px;
  box-shadow: none;
}

.grown-up-badge.small .icon {
  width: 16px;
  height: 16px;
}

.grown-up-legend {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin: var(--space-2) 0 0;
  font-size: var(--font-xs);
  color: var(--text-secondary);
}

.grown-up-legend[hidden] {
  display: none;
}

@media (max-width: 640px) {
  .tile-friend { --friend-scale: 0.84; }
  .tile-art { top: 8px; left: 10px; scale: 0.85; transform-origin: 0 0; }
}

/* --- Occasional life: long cycles, still for most of each. --- */
@keyframes friend-wiggle {
  0%, 88%, 100% { transform: rotate(0); }
  91% { transform: rotate(5deg); }
  95% { transform: rotate(-4deg); }
}

@keyframes friend-hop {
  0%, 86%, 100% { transform: translateY(0); }
  90% { transform: translateY(-8px); }
  94% { transform: translateY(0); }
  97% { transform: translateY(-4px); }
}

@keyframes twinkle-now-and-then {
  0%, 70%, 100% { opacity: 1; transform: scale(1); }
  85% { opacity: var(--ui-motion-twinkle-dim); transform: scale(var(--ui-motion-twinkle-scale)); }
}

@keyframes art-float {
  0%, 100% { transform: translate(0, 0); }
  50% { transform: translate(6px, -3px); }
}

/* Home's own width holds what it draws (the tiles' friends, the sky's art): never a sideways scroll. */
.kid {
  position: relative;
  overflow-x: clip;
}

/* --- Spec 177: Home's delights (js/delights.js), one at a time, now and then. One layer fixed to the window that takes
   no pointer and holds the moment playing; each moment is a box placed by custom properties (--x, --y, set by script
   through the CSS object model) with its art inside. Transforms and opacity only. A new moment: .delight-<id> and its
   keyframes here, its row in js/delights.js MOMENTS. --- */
.delights {
  position: fixed;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 4;
  transition: opacity 200ms ease-out;
}

.delights.is-leaving {
  opacity: 0;
}

.delight {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
}

.delight img {
  display: block;
  width: 100%;
  height: auto;
}

/* The butterfly (spec 052): across the sky card under its words, on a gentle wave in seven seconds, flapping, then gone. */
.delight-butterfly {
  width: 44px;
  opacity: 0;
  translate: var(--from) var(--y);
  animation: delight-across var(--delight-ms) linear 1 both;
}

.delight-butterfly img {
  animation: flap 0.2s ease-in-out 35;
}

.delight-butterfly.is-rtl img {
  animation-name: flap-back;
}

@keyframes delight-across {
  0% { opacity: 0; translate: var(--from) var(--y); }
  6% { opacity: 1; }
  25% { translate: calc(var(--from) + (var(--to) - var(--from)) * 0.25) calc(var(--y) - 30px); }
  50% { translate: calc(var(--from) + (var(--to) - var(--from)) * 0.5) calc(var(--y) + 20px); }
  75% { translate: calc(var(--from) + (var(--to) - var(--from)) * 0.75) calc(var(--y) - 25px); }
  94% { opacity: 1; }
  100% { opacity: 0; translate: var(--to) var(--y); }
}

@keyframes flap {
  0%, 100% { scale: 1 1; }
  50% { scale: 0.35 1; }
}

@keyframes flap-back {
  0%, 100% { scale: -1 1; }
  50% { scale: -0.35 1; }
}

/* A friend peeking through the menu: the box is the menu bar above its rainbow edge, so the friend comes up from behind
   that edge, looks one way and the other, gives a little hop, and slips back down. Its head shows (--head, the share of
   the picture that is the head), never more than the bar's height. */
.delight-peek-top,
.delight-sleepy-owl {
  width: 92px;
  height: var(--h);
  overflow: hidden;
  translate: var(--x) var(--y);
}

.delight-peek-top img,
.delight-sleepy-owl img {
  --rest: max(calc((1 - var(--head)) * 100%), calc(100% - var(--h) + 10px));
  position: absolute;
  left: 14px;
  bottom: 0;
  width: 64px;
  transform-origin: 50% 100%;
  translate: 0 100%;
  animation: delight-peek var(--delight-ms) ease-in-out 1 both;
}

@keyframes delight-peek {
  0% { translate: 0 100%; rotate: 0deg; }
  18% { translate: 0 var(--rest); rotate: 0deg; }
  32% { rotate: -9deg; }
  48% { rotate: 9deg; }
  60% { translate: 0 var(--rest); rotate: 0deg; }
  68% { translate: 0 calc(var(--rest) - 6px); }
  76% { translate: 0 var(--rest); }
  86% { translate: 0 var(--rest); rotate: 0deg; }
  100% { translate: 0 100%; rotate: 0deg; }
}

/* In Moon, the owl: slower, a sleepy sway and a nod, no hop. */
.delight-sleepy-owl img {
  animation-name: delight-doze;
  animation-timing-function: ease-in-out;
}

@keyframes delight-doze {
  0% { translate: 0 100%; rotate: 0deg; }
  22% { translate: 0 var(--rest); rotate: 0deg; }
  38% { rotate: -4deg; }
  52% { translate: 0 calc(var(--rest) + 4px); rotate: 3deg; }
  66% { translate: 0 var(--rest); rotate: 0deg; }
  82% { translate: 0 var(--rest); }
  100% { translate: 0 100%; rotate: 0deg; }
}

/* A friend leaning in from the side of a wide window (only where the margin beside Home is free), head tilted in. */
.delight-peek-side {
  width: 96px;
  translate: 0 var(--y);
}

.delight-peek-side.is-right {
  left: auto;
  right: 0;
  scale: -1 1;
}

.delight-peek-side img {
  rotate: 62deg;
  translate: -150% 0;
  animation: delight-lean var(--delight-ms) ease-in-out 1 both;
}

/* Lying behind the window's edge, head first: out until the head shows, a tilt one way and back, then away. */
@keyframes delight-lean {
  0% { translate: -150% 0; rotate: 62deg; }
  20% { translate: -46% 0; rotate: 62deg; }
  40% { rotate: 52deg; }
  55% { rotate: 68deg; }
  70% { translate: -46% 0; rotate: 60deg; }
  84% { translate: -46% 0; rotate: 62deg; }
  100% { translate: -150% 0; rotate: 62deg; }
}

/* A shooting star by night: across the sky card's band under its words, down to the right, its trail fading behind it. */
.delight-shooting-star {
  width: 22px;
  height: 22px;
  opacity: 0;
  translate: var(--x) var(--y);
  animation: delight-shoot var(--delight-ms) cubic-bezier(0.3, 0, 0.7, 1) 1 both;
}

.delight-shooting-star::before {
  content: "";
  position: absolute;
  top: 50%;
  right: 50%;
  width: 96px;
  height: 3px;
  margin-top: -1.5px;
  border-radius: 999px;
  background: linear-gradient(90deg, transparent, var(--crayon-yellow));
  transform-origin: 100% 50%;
  rotate: 21.8deg;
}

.delight-shooting-star img {
  position: relative;
  animation: delight-turn var(--delight-ms) linear 1 both;
}

@keyframes delight-shoot {
  0% { opacity: 0; translate: var(--x) var(--y); }
  12% { opacity: 1; }
  70% { opacity: 1; }
  100% { opacity: 0; translate: calc(var(--x) + 200px) calc(var(--y) + 80px); }
}

@keyframes delight-turn {
  from { rotate: 0deg; }
  to { rotate: 90deg; }
}

/* --- Confetti and the cheering friend (js/play.js). --- */
.confetti {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  pointer-events: none;
  z-index: 50;
}

.cheer-friend {
  position: fixed;
  left: 50%;
  bottom: 24px;
  height: 150px;
  width: auto;
  translate: -50% 0;
  pointer-events: none;
  z-index: 51;
  filter: drop-shadow(0 8px 12px rgb(11 24 48 / 25%));
  animation: cheer 1.9s ease-out 1 both;
}

.cheer-friend.is-still {
  animation: cheer-fade 1.9s ease-in-out 1 both;
}

@keyframes cheer {
  0% { transform: translateY(220px); }
  18% { transform: translateY(0); }
  33% { transform: translateY(-24px) rotate(6deg); }
  49% { transform: translateY(0) rotate(0); }
  64% { transform: translateY(-24px) rotate(-6deg); }
  80% { transform: translateY(0) rotate(0); }
  100% { transform: translateY(240px); }
}

@keyframes cheer-fade {
  0%, 100% { opacity: 0; }
  15%, 80% { opacity: 1; }
}

/* --- The toast: at the bottom, over everything, gone after a moment or at a tap. --- */
.toast {
  position: fixed;
  left: 50%;
  bottom: max(16px, env(safe-area-inset-bottom));
  translate: -50% 0;
  width: min(560px, calc(100vw - 32px));
  margin: 0;
  /* Spec 170: the app's toast is a Panel: 20 round, a 2 px Hairline edge, its words at 17. It floats, so it keeps a shadow. */
  padding: var(--space-3) var(--space-5);
  border-radius: var(--ui-radius-panel);
  background: var(--surface);
  color: var(--text);
  border: var(--ui-stroke-panel) solid var(--panel-edge);
  box-shadow: var(--elevation-raised);
  text-align: center;
  font-size: var(--ui-font-toast);
  font-weight: 600;
  cursor: pointer;
  z-index: 40;
  animation: toast-in 220ms ease-out both;
}

.toast[hidden] {
  display: none;
}

.toast[data-kind="success"] { border-color: var(--success); }
.toast[data-kind="error"] { border-color: var(--danger); }

@keyframes toast-in {
  from { opacity: 0; translate: -50% 12px; }
  to { opacity: 1; translate: -50% 0; }
}

/* --- Pages arrive with a gentle slide. --- */
/* backwards only: once in, the page keeps no transform (a fixed toast stays fixed to the window). */
/* Spec 170: the app's PageEntrance: 16 px up and from 55% to full in 260 ms. */
#view > section {
  animation: page-in var(--ui-motion-page-in) cubic-bezier(0.33, 1, 0.68, 1) backwards;
}

@keyframes page-in {
  from { opacity: var(--ui-motion-page-from); translate: 0 var(--ui-motion-page-rise); }
  to { opacity: 1; translate: 0 0; }
}

/* --- Rest: a hidden tab pauses; reduced motion stops it all. --- */
html.is-still .kid *,
html.is-still .delight,
html.is-still .delight *,
html.is-still .tile-friend,
html.is-still .tile-art {
  animation-play-state: paused;
}

@media (prefers-reduced-motion: reduce) {
  /* Spec 177: no delight is ever made with reduced motion; if one were, it would not show. */
  .delights {
    display: none;
  }

  .kid-sky .sparkle,
  .kid-friend,
  .kid-art,
  .tile-friend,
  .tile-art,
  .delight,
  .delight img,
  .toast,
  #view > section {
    animation: none;
  }

  .kid-sky {
    transition: none;
  }

  .kid-sky:has(.kid-draw:not([hidden])):hover {
    scale: none;
    translate: none;
  }
}
