/*
 * Motion (inc/motion.php): scroll-linked movement with CSS scroll-driven animations. A block's root carries
 * data-rtthemes-motion and its values as custom properties; the animation runs from those values to the block's own
 * look while the block enters the viewport (start and end are how far its top has come up the screen, in vh, so short
 * and tall blocks move alike), and scrolling back reverses it. Only translate, scale, rotate and opacity move: the
 * compositor animates them without layout work.
 * An ancestor with overflow: hidden is a scroll container that view() would follow; use overflow: clip there.
 */
@supports (animation-timeline: view()) {
	@media (prefers-reduced-motion: no-preference) {
		[data-rtthemes-motion="enter"]:not([data-rtthemes-motion-stagger]),
		[data-rtthemes-motion="enter"][data-rtthemes-motion-stagger] > * {
			--rtthemes-motion-offset: 0vh;
			animation: rtthemes-motion-in ease-out both;
			animation-timeline: view();
			animation-range:
				cover calc(var(--rtthemes-motion-start) + var(--rtthemes-motion-offset))
				cover calc(var(--rtthemes-motion-end) + var(--rtthemes-motion-offset));
		}

		/* One child after another: each next child starts a step later. */
		[data-rtthemes-motion="enter"][data-rtthemes-motion-stagger] > * {
			--rtthemes-motion-offset: calc((sibling-index() - 1) * var(--rtthemes-motion-step));
		}

		/* Leaving the viewport plays the same movement backwards: the same distances from the top of the screen. */
		[data-rtthemes-motion-exit]:not([data-rtthemes-motion-stagger]),
		[data-rtthemes-motion-exit][data-rtthemes-motion-stagger] > * {
			animation: rtthemes-motion-in ease-out both, rtthemes-motion-out ease-in forwards;
			animation-timeline: view(), view();
			animation-range:
				cover calc(var(--rtthemes-motion-start) + var(--rtthemes-motion-offset))
				cover calc(var(--rtthemes-motion-end) + var(--rtthemes-motion-offset)),
				cover calc(100% - var(--rtthemes-motion-end)) cover calc(100% - var(--rtthemes-motion-start));
		}

		/* Parallax: moves from y to -y while the block crosses the viewport. */
		[data-rtthemes-motion="parallax"] {
			animation: rtthemes-motion-parallax linear both;
			animation-timeline: view();
			animation-range: cover 0% cover 100%;
		}
	}
}

/*
 * Timed: a scroll trigger starts the movement once the block's top (each child's, one after another) has come up the
 * screen by the start, then it plays over its duration. Without Replay it plays once; with Replay it resets when the
 * block has left the screen. Browsers without CSS triggers show the block as it is.
 */
@supports (timeline-trigger: --rtthemes-motion view()) {
	@media (prefers-reduced-motion: no-preference) {
		[data-rtthemes-motion="timed"]:not([data-rtthemes-motion-stagger]),
		[data-rtthemes-motion="timed"][data-rtthemes-motion-stagger] > * {
			--rtthemes-motion-offset: 0ms;
			timeline-trigger: --rtthemes-motion view() cover var(--rtthemes-motion-start) cover 100%;
			animation: rtthemes-motion-in var(--rtthemes-motion-duration) var(--rtthemes-motion-easing)
				calc(var(--rtthemes-motion-delay) + var(--rtthemes-motion-offset)) both;
			animation-trigger: --rtthemes-motion play-forwards;
		}

		[data-rtthemes-motion="timed"][data-rtthemes-motion-stagger] > * {
			--rtthemes-motion-offset: calc((sibling-index() - 1) * var(--rtthemes-motion-step));
		}

		[data-rtthemes-motion-replay]:not([data-rtthemes-motion-stagger]),
		[data-rtthemes-motion-replay][data-rtthemes-motion-stagger] > * {
			animation-trigger: --rtthemes-motion play-forwards reset;
		}
	}
}

@keyframes rtthemes-motion-in {
	from {
		opacity: var(--rtthemes-motion-opacity);
		translate: var(--rtthemes-motion-x) var(--rtthemes-motion-y);
		rotate: var(--rtthemes-motion-rotate);
		scale: var(--rtthemes-motion-scale);
	}
}

@keyframes rtthemes-motion-out {
	to {
		opacity: var(--rtthemes-motion-opacity);
		translate: var(--rtthemes-motion-x) var(--rtthemes-motion-y);
		rotate: var(--rtthemes-motion-rotate);
		scale: var(--rtthemes-motion-scale);
	}
}

@keyframes rtthemes-motion-parallax {
	from {
		translate: var(--rtthemes-motion-x) var(--rtthemes-motion-y);
	}
	to {
		translate: calc(var(--rtthemes-motion-x) * -1) calc(var(--rtthemes-motion-y) * -1);
	}
}
