/*
 * The playful layer (docs/motion.md): the two characters playing peekaboo, and the
 * flowers, balloons and circles that gently move. Loaded on every page.
 *
 *   1. Characters: .pe-mascot (any character picture)
 *   2. Edge peek:  .pe-peek (Section settings > Character)
 *   3. Runner:     .pe-runner (the character that hops along the footer band)
 *   4. Living decorations: flowers turn, balloons sway, circles drift
 *   5. Reduced motion: everything stands still
 *
 * The animations use the `translate`, `rotate` and `scale` properties, not `transform`,
 * so they never fight with a transform a section already uses to place something.
 */

/* ==========================================================================
   1. Characters
   ========================================================================== */
/* No width here on purpose: each section sets the size with its own class.
   :where() gives this rule no weight at all, so ANY class on the picture can set its
   width or height, even though this file loads after the section styles. */
:where(.pe-mascot) {
	max-width: none;
	height: auto;
	user-select: none;
	-webkit-user-drag: none;
	/* A soft shadow that follows the character's outline, so it sits on the page. */
	filter: drop-shadow(0 0.6rem 0.6rem rgb(32 61 54 / 0.22));
}

/* A gentle breathing bob, for a character that stands in one place. */
@keyframes pe-bob {
	0%, 100% { translate: 0 0; rotate: -1.5deg; }
	50% { translate: 0 -0.4rem; rotate: 1.5deg; }
}

/* A little jump for joy. */
@keyframes pe-hop {
	0%, 100% { translate: 0 0; scale: 1 1; }
	12% { translate: 0 0; scale: 1.06 0.92; }
	45% { translate: 0 -1.1rem; scale: 0.97 1.04; }
	80% { translate: 0 0; scale: 1.04 0.95; }
}

.pe-mascot--bob {
	transform-origin: 50% 100%;
	animation: pe-bob 4.5s ease-in-out infinite;
}

.pe-mascot--hop {
	transform-origin: 50% 100%;
	animation: pe-hop 1.6s ease-in-out infinite;
}

/* ==========================================================================
   2. Edge peek: a character looks in from the left or right edge of a section.
      .pe-peek is a window fixed to the edge; it hides whatever is outside it, so the
      character can slide in from off screen without making the page scroll sideways.
      assets/js/play.js adds .is-in when the section scrolls into view, and .is-hiding
      for a moment when the character is hovered or tapped (it ducks away).
   ========================================================================== */
.pe-peek {
	/* As wide as the page's side gutter plus a little, so it never covers the text. */
	--pe-peek-width: clamp(3.5rem, calc(var(--pe-gutter) + 1.75rem), 8.5rem);

	position: absolute;
	top: 50%;
	right: 0;
	z-index: 3;
	width: var(--pe-peek-width);
	overflow: hidden;
	translate: 0 -50%;
	cursor: pointer;
}

.pe-peek--left {
	right: auto;
	left: 0;
	/* The picture peeks from the right; mirrored, it peeks from the left. */
	scale: -1 1;
}

.pe-peek__slide {
	transition: translate 0.7s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* Hidden off the edge until the section is on screen. Only when JavaScript runs
   (.pe-js is set by motion.js), so without it the character is simply there. */
.pe-js .pe-peek__slide {
	translate: 105% 0;
}

.pe-js .pe-peek.is-in .pe-peek__slide {
	translate: 0 0;
}

/* Ducking away is quick; coming back uses the springy curve above. */
.pe-js .pe-peek.is-in.is-hiding .pe-peek__slide {
	translate: 105% 0;
	transition: translate 0.25s ease-in;
}

.pe-peek__img {
	/* A little wider than the window, so the flat "wall" side of the picture (and the
	   hand holding it) is cut off by the screen edge. */
	width: 114%;
	filter: none;
	transform-origin: 100% 100%;
	animation: pe-peek-sway 5s ease-in-out infinite;
}

/* Leaning out a little further, then back. */
@keyframes pe-peek-sway {
	0%, 100% { rotate: 0deg; translate: 0 0; }
	50% { rotate: -2.5deg; translate: -4% 0; }
}

/* Phones: smaller, and up in the section's top padding so it stays clear of the text. */
@media (max-width: 767px) {
	.pe-peek {
		--pe-peek-width: 3.25rem;

		top: 0.75rem;
		translate: 0 0;
	}
}

/* ==========================================================================
   3. Runner: a small character hops across the top of the sage footer band once,
      then stays near the right and keeps bouncing (sections/cta-band.php).
   ========================================================================== */
.pe-runner {
	position: absolute;
	/* The line the character hops along: just inside the top of the band, above the
	   heading. The band clips anything above its own top edge, so the character must
	   be shorter than this. */
	top: clamp(6.25rem, 12.2vw, 10.75rem);
	right: 0;
	left: 0;
	z-index: 1;
	height: 0;
	/* Hide the character while it is still off the left edge; let it stand above. */
	overflow-x: clip;
	overflow-y: visible;
	pointer-events: none;
}

.pe-runner__track {
	position: absolute;
	bottom: 0;
	left: 0;
	width: clamp(3.5rem, 6.4vw, 5.75rem);
	/* Start off the left edge. */
	translate: -120% 0;
}

.pe-runner.is-in .pe-runner__track {
	animation: pe-run 5s cubic-bezier(0.3, 0, 0.3, 1) forwards;
}

/* 100vw is the width of the screen; the character stops 14% in from the right. */
@keyframes pe-run {
	from { translate: -120% 0; }
	to { translate: calc(86vw - 100%) 0; }
}

.pe-runner__img {
	width: 100%;
	transform-origin: 50% 100%;
	animation: pe-hop 0.62s ease-in-out infinite;
}

/* ==========================================================================
   4. Living decorations. The selectors match the file name of the SVG, so every
      flower, balloon and circle on the site moves without touching each section.
   ========================================================================== */
@keyframes pe-spin {
	to { rotate: 360deg; }
}

@keyframes pe-sway {
	0%, 100% { rotate: -4deg; }
	50% { rotate: 4deg; }
}

@keyframes pe-drift {
	0%, 100% { translate: 0 0; }
	50% { translate: 0 -0.75rem; }
}

.pe-decoration[src*="/flower-"] {
	animation: pe-spin 28s linear infinite;
}

/* The big pale flowers behind the footer band turn more slowly. */
.pe-decoration[src*="/flower-sage-big"] {
	animation-duration: 70s;
}

/* Balloons swing from the bottom of their strings. */
.pe-decoration[src*="/balloons"] {
	transform-origin: 50% 100%;
	animation: pe-sway 5.5s ease-in-out infinite;
}

.pe-decoration[src*="/circle-"] {
	animation: pe-drift 7s ease-in-out infinite;
}

/* Every second one starts later, so they don't all move together. */
.pe-decoration:nth-of-type(even) {
	animation-delay: -2.5s;
}

/* ==========================================================================
   5. Reduced motion: no movement at all. Characters are simply there.
   ========================================================================== */
@media (prefers-reduced-motion: reduce) {
	.pe-mascot,
	.pe-decoration,
	.pe-peek__img,
	.pe-runner__img {
		animation: none !important;
	}

	.pe-js .pe-peek .pe-peek__slide,
	.pe-js .pe-peek.is-in.is-hiding .pe-peek__slide {
		translate: 0 0;
		transition: none;
	}

	.pe-runner__track,
	.pe-runner.is-in .pe-runner__track {
		animation: none;
		translate: calc(86vw - 100%) 0;
	}
}

/* ==========================================================================
   6. Find the bear. The bear hides at the bottom edge of one section on every page
      (assets/js/play.js picks the section and the spot). Only the top of his head shows
      until he is clicked; then he stands up with a thumbs up and a message pops up.
   ========================================================================== */
.pe-hide-bear {
	position: absolute;
	bottom: 0;
	z-index: 3;
	width: 3.5rem;
	/* Only this much of him shows: crown, ears and the hands over his eyes. */
	height: 2.9rem;
	padding: 0;
	border: 0;
	background: none;
	overflow: hidden;
	translate: -50% 0;
	cursor: pointer;
	transition: height 0.25s ease;
}

.pe-hide-bear__img {
	width: 100%;
	filter: none;
}

.pe-hide-bear__img--found {
	display: none;
}

/* He rises a little when you get close. */
@media (hover: hover) {
	.pe-hide-bear:hover {
		height: 3.6rem;
	}
}

/* Found: the whole bear, thumbs up. */
.pe-hide-bear.is-found {
	width: 4.75rem;
	height: auto;
	overflow: visible;
	cursor: default;
}

.pe-hide-bear.is-found .pe-hide-bear__img--hiding {
	display: none;
}

.pe-hide-bear.is-found .pe-hide-bear__img--found {
	display: block;
	transform-origin: 50% 100%;
}

/* Three happy jumps, only at the moment he is found. */
.pe-hide-bear.is-celebrating .pe-hide-bear__img--found {
	animation: pe-hop 0.6s ease-in-out 3;
}

/* The message that pops up at the bottom of the screen. */
.pe-toast {
	position: fixed;
	bottom: 1.25rem;
	left: 50%;
	z-index: 60;
	width: max-content;
	max-width: calc(100vw - 2rem);
	padding: 0.9rem 1.5rem;
	border: 0.15rem solid #fffaf0;
	border-radius: var(--pe-radius-card);
	background: var(--pe-sunny);
	box-shadow: var(--pe-shadow-lift);
	color: var(--pe-ink);
	text-align: center;
	translate: -50% 150%;
	transition: translate 0.45s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.pe-toast.is-in {
	translate: -50% 0;
}

.pe-toast__title {
	display: block;
	font: 600 1.375rem / 1.2 var(--pe-font-display);
}

.pe-toast__text {
	display: block;
	margin-top: 0.15rem;
	font-size: 1rem;
	line-height: 1.35;
}

/* Confetti when every bear has been found: small coloured bits falling down the screen. */
.pe-confetti {
	position: fixed;
	inset: 0;
	z-index: 59;
	overflow: hidden;
	pointer-events: none;
}

.pe-confetti__bit {
	position: absolute;
	top: -1rem;
	width: 0.6rem;
	height: 0.9rem;
	border-radius: 0.15rem;
	animation: pe-confetti-fall 3s linear forwards;
}

@keyframes pe-confetti-fall {
	to {
		translate: var(--pe-drift, 0) 105vh;
		rotate: 720deg;
	}
}

@media (prefers-reduced-motion: reduce) {
	.pe-hide-bear,
	.pe-toast {
		transition: none;
	}

	.pe-hide-bear.is-celebrating .pe-hide-bear__img--found {
		animation: none;
	}
}

/* ==========================================================================
   The peekaboo video (pe_peekaboo_video() in inc/helpers.php).
   The video is not see-through: the characters stand on a pale background. So the
   frame around it gets that same pale colour (--pe-video-bg), and the video's edges
   fade out, which hides any small difference between the two.
   ========================================================================== */
:root {
	--pe-video-bg: #f5f4f1; /* the background colour inside peekaboo.mp4 */
}

.pe-peekaboo-video {
	display: block;
	height: auto;
	background: var(--pe-video-bg);
	pointer-events: none; /* decoration: nothing to click or pause */
}
