/*
 * Mascot themes: hidden themes that put animals on the page
 * (docs/how-to-mascots.md). This file is the part every theme shares: where
 * each animal goes and how it moves. A theme's own file (rainbow.css is the
 * first) adds its colors and names a drawing for each role it fills, on
 * :root[data-mascots="<theme>"]:
 *
 *   --mascot-walk, --mascot-sit   walkers on tile and button edges, and the
 *                                 one on the Outlook temperature line
 *   --mascot-walk-rare, -sit-rare one walker a page wears these instead
 *   --mascot-sleep                asleep on three 7-day night cards
 *   --mascot-wake                 a sleeper once tapped; plays once
 *   --mascot-chew                 eating the moon on the Sun tile (sits, in
 *                                 --mascot-sit, at new moon)
 *   --mascot-hang                 hanging from a moon in a 7-day night icon;
 *                                 falls off the screen when tapped
 *   --mascot-wave                 waving at the foot of the forecast page
 *   --mascot-fly, --mascot-fly-shy  the flier, and its pose once clicked
 *   --mascot-puff                 what comes out of the flier when clicked
 *   --mascot-puff-burst           a shape laid over the puff, painted the
 *                                 next of --mascot-puff-colors each click
 *   --mascot-bounce               two things bouncing behind the page
 *
 * A role a theme leaves out is skipped (mascot_controller.js). Drawings are
 * SVGs that animate themselves with transform and opacity only.
 *
 * How it stays smooth. Measured in Chrome on the forecast page, an
 * animation of left, offset-distance, or background-image, a script that
 * moves something every frame, or a drawing that animates itself, as a CSS
 * background or inline, each made the browser redo the whole page on every
 * frame: together about 46% of the main thread on a laptop, and more than a
 * phone has. So everything moves by transform and opacity alone, which the
 * compositor plays without the page, and each drawing is taken apart into
 * layers that move that way too (mascots/drawings.js). Keep new movement to
 * those two. Take a script (web) animation off any box that gets hidden: on
 * a display: none box it keeps running on the main thread, and that, like
 * anything else the compositor cannot play, brings the per-frame page
 * update back for every animation on the page.
 */

.mascot-art {
  display: block;
  pointer-events: none;
}

/* Left on the page after a theme is turned off, until the next visit. */
:root:not([data-mascots]) :is(.mascot-art, .mascot-walker) {
  display: none;
}

/* ---- Walkers on the tiles and buttons ---------------------------------- */

/* Nothing here says which tiles get an animal. On each page view
   mascot_controller.js picks six or seven tiles for walkers, one of them
   the rare one, and three night cards for sleepers, gives each a random
   size, pace, starting point, and spot, and adds the animal to the tile. A
   couple of buttons on other pages get a small walker too. */
[data-mascots] .forecast-page .tile,
[data-mascots] .button.has-mascot {
  position: relative;
}

/* Stands on the tile's top edge, walks halfway, sits a while, walks all the
   way to the far edge, turns, walks most of the way back, sits again, and
   goes home to the near edge. The walker box is as wide as the room to
   walk, so a translate in percent of its own width is a share of that room
   and needs nothing the compositor cannot work out. It starts out wholly
   left of the tile, with the animal hanging off its right end, so however
   far it goes it never reaches past the tile's right edge: a box out past
   the page's right edge made the page wider, scrollable sideways, and
   measured again on every frame. The animal is drawn twice, walking and
   sitting, and the pose not in use is see-through. --mascot-aspect is the
   drawings' height over width. */
[data-mascots] .mascot-walker {
  --m-w: calc(2.6rem * var(--m-size, 1));
  display: block;
  position: absolute;
  right: 100%;
  bottom: 100%;
  z-index: 1;
  width: max(0px, 100% - var(--m-w));
  height: calc(var(--m-w) * var(--mascot-aspect, 0.875));
  margin-bottom: calc(-0.3rem * var(--m-size, 1));
  pointer-events: none;
  animation: mascot-walk var(--m-time, 30s) linear var(--m-delay, 0s) infinite;
}

[data-mascots] .mascot-walker > .mascot-art,
[data-mascots] .mascot-line > .mascot-art {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--m-w, 100%);
  height: 100%;
  animation: mascot-pose-walk var(--m-time, 30s) linear var(--m-delay, 0s) infinite;
}

[data-mascots] .mascot-walker > .mascot-art {
  left: 100%;
}

[data-mascots] .mascot-walker > .is-sit,
[data-mascots] .mascot-line > .is-sit {
  animation-name: mascot-pose-sit;
}

@keyframes mascot-walk {
  0% { transform: translateX(0%); }
  17.5%, 32.5% { transform: translateX(50%); }
  49.9%, 50% { transform: translateX(100%); }
  74.5%, 89.5% { transform: translateX(30%); }
  100% { transform: translateX(0%); }
}

/* Walking, or sitting, and which way it faces, on the same clock as
   mascot-walk (and as the Outlook line walker's path, mascots/line.js). */
@keyframes mascot-pose-walk {
  0%, 17.5% { opacity: 1; transform: scaleX(1); }
  17.6%, 32.5% { opacity: 0; transform: scaleX(1); }
  32.6%, 49.9% { opacity: 1; transform: scaleX(1); }
  50%, 74.5% { opacity: 1; transform: scaleX(-1); }
  74.6%, 89.5% { opacity: 0; transform: scaleX(-1); }
  89.6%, 99.9% { opacity: 1; transform: scaleX(-1); }
  100% { opacity: 1; transform: scaleX(1); }
}

@keyframes mascot-pose-sit {
  0%, 17.5% { opacity: 0; transform: scaleX(1); }
  17.6%, 32.5% { opacity: 1; transform: scaleX(1); }
  32.6%, 49.9% { opacity: 0; transform: scaleX(1); }
  50%, 74.5% { opacity: 0; transform: scaleX(-1); }
  74.6%, 89.5% { opacity: 1; transform: scaleX(-1); }
  89.6%, 99.9% { opacity: 0; transform: scaleX(-1); }
  100% { opacity: 0; transform: scaleX(1); }
}

/* Some walkers sit 3 or 6 times as long (mascot_controller.js pace). The
   loop is longer by as much, so the walking legs keep their speed. */
[data-mascots] [data-m-sit="3"] > .mascot-walker { animation-name: mascot-walk-3; }
[data-mascots] [data-m-sit="3"] > .mascot-walker > .mascot-art,
[data-mascots] .mascot-line[data-m-sit="3"] > .mascot-art { animation-name: mascot-pose-walk-3; }
[data-mascots] [data-m-sit="3"] > .mascot-walker > .is-sit,
[data-mascots] .mascot-line[data-m-sit="3"] > .is-sit { animation-name: mascot-pose-sit-3; }
[data-mascots] [data-m-sit="6"] > .mascot-walker { animation-name: mascot-walk-6; }
[data-mascots] [data-m-sit="6"] > .mascot-walker > .mascot-art,
[data-mascots] .mascot-line[data-m-sit="6"] > .mascot-art { animation-name: mascot-pose-walk-6; }
[data-mascots] [data-m-sit="6"] > .mascot-walker > .is-sit,
[data-mascots] .mascot-line[data-m-sit="6"] > .is-sit { animation-name: mascot-pose-sit-6; }

@keyframes mascot-walk-3 {
  0% { transform: translateX(0%); }
  10.94%, 39.06% { transform: translateX(50%); }
  49.94%, 50% { transform: translateX(100%); }
  65.31%, 93.44% { transform: translateX(30%); }
  100% { transform: translateX(0%); }
}

@keyframes mascot-pose-walk-3 {
  0%, 10.94% { opacity: 1; transform: scaleX(1); }
  11.04%, 39.06% { opacity: 0; transform: scaleX(1); }
  39.16%, 49.94% { opacity: 1; transform: scaleX(1); }
  50%, 65.31% { opacity: 1; transform: scaleX(-1); }
  65.41%, 93.44% { opacity: 0; transform: scaleX(-1); }
  93.54%, 99.9% { opacity: 1; transform: scaleX(-1); }
  100% { opacity: 1; transform: scaleX(1); }
}

@keyframes mascot-pose-sit-3 {
  0%, 10.94% { opacity: 0; transform: scaleX(1); }
  11.04%, 39.06% { opacity: 1; transform: scaleX(1); }
  39.16%, 49.94% { opacity: 0; transform: scaleX(1); }
  50%, 65.31% { opacity: 0; transform: scaleX(-1); }
  65.41%, 93.44% { opacity: 1; transform: scaleX(-1); }
  93.54%, 99.9% { opacity: 0; transform: scaleX(-1); }
  100% { opacity: 0; transform: scaleX(1); }
}

@keyframes mascot-walk-6 {
  0% { transform: translateX(0%); }
  7%, 43% { transform: translateX(50%); }
  49.96%, 50% { transform: translateX(100%); }
  59.8%, 95.8% { transform: translateX(30%); }
  100% { transform: translateX(0%); }
}

@keyframes mascot-pose-walk-6 {
  0%, 7% { opacity: 1; transform: scaleX(1); }
  7.1%, 43% { opacity: 0; transform: scaleX(1); }
  43.1%, 49.96% { opacity: 1; transform: scaleX(1); }
  50%, 59.8% { opacity: 1; transform: scaleX(-1); }
  59.9%, 95.8% { opacity: 0; transform: scaleX(-1); }
  95.9%, 99.9% { opacity: 1; transform: scaleX(-1); }
  100% { opacity: 1; transform: scaleX(1); }
}

@keyframes mascot-pose-sit-6 {
  0%, 7% { opacity: 0; transform: scaleX(1); }
  7.1%, 43% { opacity: 1; transform: scaleX(1); }
  43.1%, 49.96% { opacity: 0; transform: scaleX(1); }
  50%, 59.8% { opacity: 0; transform: scaleX(-1); }
  59.9%, 95.8% { opacity: 1; transform: scaleX(-1); }
  95.9%, 99.9% { opacity: 0; transform: scaleX(-1); }
  100% { opacity: 0; transform: scaleX(1); }
}

/* The radar tile clips to its edges once the map loads, which hid its
   walker. The map stage clips itself, so the tile can let it stand. */
[data-mascots] .forecast-page .forecast-radar-tile:has(.forecast-radar-stage) {
  overflow: visible;
}

/* ---- The rest of the forecast page ------------------------------------- */

/* Asleep on a night card's top edge, at a random spot (--sleep-at).
   Tapping it wakes it for a moment (mascot_controller.js). */
[data-mascots] .mascot-sleeper {
  pointer-events: auto;
  position: absolute;
  bottom: 100%;
  left: var(--sleep-at, 55%);
  z-index: 1;
  width: 2.9rem;
  height: calc(2.9rem * var(--mascot-aspect, 0.875));
  margin-bottom: -0.35rem;
}

/* The Outlook tile's walker walks the temperature line
   (day_line_controller.js draws the line and mascot_controller.js turns it
   into keyframes for this box: where on the line, and its slope there).
   Its poses and turns are the tile walkers'. */
.mascot-line {
  display: none;
}

[data-mascots] .today-chart {
  position: relative;
}

[data-mascots] .mascot-line {
  display: block;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  width: 2.3rem;
  height: 2rem;
  transform-origin: 50% 92%;
  pointer-events: none;
}

/* Eating the moon on the Sun tile. Where the mouth and the bite go depends
   on the drawing, so the theme places .mascot-chewer and masks
   .forecast-moon-symbol, using --bite, where the lit part of the moon
   starts (mascot_controller.js; rainbow.css shows how). */
[data-mascots] .forecast-moon-current:has(.mascot-chewer) {
  --bite: 0.1875;
  position: relative;
}

[data-mascots] .mascot-chewer {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  width: 2.3rem;
  height: 2rem;
}

/* Hanging from a moon in one of the 7-day night icons.
   mascot_controller.js picks the moon and sets --hx and --hy, the bottom
   of the moon inside the icon. The theme sets the margin that puts the
   drawing's grip there. Tapped, it lets go: the controller moves it to the
   body, fixed where it was, and drops it off the bottom of the screen. */
.mascot-hang {
  display: none;
}

[data-mascots] .forecast-page .forecast-period .weather-icon {
  position: relative;
}

[data-mascots] .mascot-hang {
  display: block;
  position: absolute;
  top: var(--hy);
  left: var(--hx);
  z-index: 2;
  width: 1.6rem;
  height: 2.56rem;
  margin-left: -0.8rem;
  pointer-events: auto;
  cursor: pointer;
}

[data-mascots] .mascot-hang.is-falling {
  position: fixed;
  z-index: 3;
  margin: 0;
  pointer-events: none;
}

/* Sitting at the foot of the forecast page, waving, at a random spot
   across it (--wave-at). */
[data-mascots] .mascot-waver {
  width: 3.2rem;
  height: calc(3.2rem * var(--mascot-aspect, 0.875));
  margin: 0 0 0.4rem calc(var(--wave-at, 50%) - 1.6rem);
}

/* ---- Bouncers and the flier -------------------------------------------- */

.mascot-decor,
.mascot-flier {
  display: none;
}

[data-mascots] .mascot-decor {
  display: block;
  position: fixed;
  inset: 0;
  z-index: -1;
  overflow: hidden;
  pointer-events: none;
}

/* mascot_bounce_controller.js works out their bounces some seconds ahead
   and hands them over as keyframes, so the compositor moves them.
   --mascot-bounce-aspect is the drawing's height over width. */
.mascot-bounce {
  position: absolute;
  top: 0;
  left: 0;
  width: var(--w);
  height: calc(var(--w) * var(--mascot-bounce-aspect, 1));
  background: var(--mascot-bounce, none) center / contain no-repeat;
}

.mascot-bounce:nth-child(1) { --w: 7rem; }
.mascot-bounce:nth-child(2) { --w: 10rem; }

/* The flier flies in front of the page, loops through the window, and
   turns while it is off screen. Clicking it makes it puff, stop and go shy,
   then speed away (mascot_controller.js). Empty until its drawing loads,
   and for a theme with no flier. */
[data-mascots] .mascot-flier {
  display: block;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 3;
  width: 5rem;
  height: 3.7rem;
  animation: mascot-fly 40s linear infinite;
}

[data-mascots] .mascot-flier:empty {
  display: none;
}

/* Copies of the drawing, flapping in step: the flier, its shy pose, and
   the blush tint (mascots/drawings.js), each only if the theme has it. */
[data-mascots] .mascot-flier > .mascot-art {
  position: absolute;
  inset: 0;
  pointer-events: auto;
}

[data-mascots] .mascot-flier > .is-shy,
[data-mascots] .mascot-flier.is-shy > .is-plain:has(~ .is-shy) {
  visibility: hidden;
}

[data-mascots] .mascot-flier.is-shy > .is-shy {
  visibility: visible;
}

/* The blush: the drawing's shape in its blush color multiplied over it, so
   white takes the color and the outline stays dark. It comes on quickly as
   the flier stops and fades as it speeds off. Hidden the rest of the time,
   so it costs nothing. */
[data-mascots] .mascot-flier > .is-tint {
  mix-blend-mode: multiply;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 2.7s ease-in, visibility 0s linear 2.7s;
}

[data-mascots] .mascot-flier.is-shy > .is-tint {
  opacity: 1;
  visibility: visible;
  transition: opacity 0.35s ease-out, visibility 0s;
}

@keyframes mascot-fly {
  0% { translate: -7rem 30vh; transform: scaleX(1); }
  14% { translate: 22vw 12vh; }
  28% { translate: 48vw 38vh; }
  38% { translate: 70vw 18vh; }
  48% { translate: calc(100vw + 2rem) 34vh; transform: scaleX(1); }
  50% { translate: calc(100vw + 2rem) 34vh; transform: scaleX(-1); }
  63% { translate: 72vw 66vh; }
  76% { translate: 45vw 48vh; }
  88% { translate: 18vw 72vh; }
  98% { translate: -7rem 50vh; transform: scaleX(-1); }
  100% { translate: -7rem 30vh; transform: scaleX(1); }
}

/* The puff stays where it came out while the flier gets away. --puff grows
   with each click in a streak (mascot_controller.js). It sizes the box, not
   the keyframes, so the puff stays on the compositor. Its drawing is a
   still background; one that moved would cost the main thread every frame.
   It sits behind the flier, a layer below it. The drawing faces a flier
   facing right: it comes out at the middle of its right edge, which sits
   on the point it came out of, and trails left. It pops out of that point,
   then drifts back and spreads as it fades. For a flier facing left the
   controller mirrors it with scale, which turns it about the same point. */
.mascot-puff {
  display: none;
}

[data-mascots] .mascot-puff {
  display: block;
  position: fixed;
  z-index: 2;
  width: calc(2.8rem * var(--puff, 1));
  height: calc(2.1rem * var(--puff, 1));
  margin: calc(-1.05rem * var(--puff, 1)) 0 0 calc(-2.8rem * var(--puff, 1));
  background: var(--mascot-puff, none) center / contain no-repeat;
  pointer-events: none;
  transform-origin: 100% 50%;
  animation: mascot-puff 3.2s ease-out forwards;
}

/* The burst over the puff: the theme's --mascot-puff-burst cut out of a
   block of --puff-color, which the controller sets from the theme's
   --mascot-puff-colors on each click. A theme without one shows nothing. */
[data-mascots] .mascot-puff::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--puff-color, transparent);
  mask: var(--mascot-puff-burst, linear-gradient(transparent, transparent)) center / contain no-repeat;
  opacity: 0.8;
}

@keyframes mascot-puff {
  0% { opacity: 0; transform: scale(0.15); }
  7% { opacity: 1; transform: scale(1.12); }
  14% { opacity: 1; transform: scale(1); }
  100% { opacity: 0; transform: translateX(-1.4rem) scale(1.6); }
}

@media (prefers-reduced-motion: reduce) {
  [data-mascots] .mascot-walker,
  [data-mascots] .mascot-walker > .mascot-art,
  [data-mascots] .mascot-line > .mascot-art,
  [data-mascots] [data-m-sit] > .mascot-walker,
  [data-mascots] [data-m-sit] > .mascot-walker > .mascot-art,
  [data-mascots] .mascot-line[data-m-sit] > .mascot-art { animation: none; }
  [data-mascots] .mascot-walker > .is-sit,
  [data-mascots] .mascot-line > .is-sit { display: none; }
  [data-mascots] .mascot-decor,
  [data-mascots] .mascot-flier { display: none; }
}
