/*
 * Motion layer, level 3, direction E "Luz" (docs/motion-spec.md). Loaded on home, /livro/ and /autora/ only.
 * Only transform and opacity animate. Plays for every visitor like the approved prototype, also when the
 * system asks for reduced motion (user decision 2026-11-29, docs/decisions.md). Hidden start states exist
 * only on screen and (for scroll reveals) under html.js-motion, which motion.js sets once it runs.
 * html.m-off (footer switch) stops everything. Base styles are always the final, visible state.
 */

:root {
	--motion-ease: var(--wp--custom--ease--out, cubic-bezier(0.22, 1, 0.36, 1));
	--motion-ease-io: cubic-bezier(0.65, 0, 0.35, 1);
	--motion-reveal: 800ms;
	--motion-image: 1000ms;
	--motion-rise: 32px;
	--motion-step: 70ms;
	--motion-curtain: var(--wp--preset--color--bone);
}

@media (max-width: 47.99rem) {
	:root {
		--motion-reveal: 650ms;
		--motion-rise: 20px;
	}
}

.has-sand-background-color { --motion-curtain: var(--wp--preset--color--sand); }
.is-style-dark { --motion-curtain: var(--wp--preset--color--ebony-deep); }

@keyframes m-rise { from { opacity: var(--m-o, 0); transform: translateY(var(--m-from, 0.35em)); } }
@keyframes m-fade { from { opacity: 0; } }
@keyframes m-curtain { from { transform: none; } to { transform: scaleY(0); } }
@keyframes m-settle { from { transform: scale(1.08); } }
@keyframes m-arrow {
	0% { transform: none; }
	45% { transform: translateX(0.7em); opacity: 0; }
	46% { transform: translateX(-0.5em); opacity: 0; }
	100% { transform: translateX(4px); opacity: 1; }
}
@keyframes m-marquee { to { transform: translateX(-50%); } }
@keyframes m-parallax { from { translate: 0 var(--m-par, 32px); } to { translate: 0 calc(var(--m-par, 32px) * -1); } }

@media screen {

	/* ── Signature: home hero on load (CSS only, once per session) ── */

	html:not(.motion-seen) [data-motion~="hero"] > :not(.opds-hero__video) {
		--m-from: 28px;
		--m-o: 0.25; /* never fully transparent: the title and the book can be the LCP element */
		animation: m-rise 1000ms var(--motion-ease) backwards;
		animation-delay: calc(var(--i, 0) * 110ms);
	}

	[data-motion~="hero"] > :nth-child(2) { --i: 1; }
	[data-motion~="hero"] > :nth-child(3) { --i: 2; }
	[data-motion~="hero"] > :nth-child(4) { --i: 3; }
	[data-motion~="hero"] > :nth-child(5) { --i: 4; }

	/* The book rises from lower down, after the title. */
	html:not(.motion-seen) [data-motion~="hero"] > .opds-hero__book {
		--m-from: 80px;
		animation-duration: 1400ms;
		animation-delay: 350ms;
	}

	/* Portrait on /autora/: a mask lifts from the bottom up while the plate settles. */
	[data-motion~="intro-image"],
	html.js-motion [data-motion~="image"] {
		position: relative;
		overflow: clip;
	}

	[data-motion~="intro-image"]::after {
		content: "";
		position: absolute;
		inset: 0;
		z-index: 1;
		background: var(--motion-curtain);
		transform: scaleY(0);
		transform-origin: 50% 0;
		pointer-events: none;
		animation: m-curtain var(--motion-image) var(--motion-ease-io) 120ms backwards;
	}

	[data-motion~="intro-image"] img {
		animation: m-settle 1400ms var(--motion-ease) backwards;
	}

	/* ── /autora/ intro (CSS only, on load) ── */

	[data-motion~="intro"] > * {
		--m-from: 16px;
		--m-o: 0.25; /* never fully transparent: the h1 can be the LCP element */
		animation: m-rise 700ms var(--motion-ease) backwards;
		animation-delay: calc(var(--i, 0) * 90ms);
	}

	/* ── Scroll reveals (only after motion.js has run) ── */

	[data-motion~="stagger"] > :nth-child(2),
	[data-motion~="intro"] > :nth-child(2) { --i: 1; }
	[data-motion~="stagger"] > :nth-child(3) { --i: 2; }
	[data-motion~="stagger"] > :nth-child(4) { --i: 3; }
	[data-motion~="stagger"] > :nth-child(n+5) { --i: 4; }
	.opds-tagline[data-motion~="stagger"] { --motion-step: 150ms; }

	html.js-motion [data-motion~="reveal"]:not(.is-in):not(:focus-within),
	html.js-motion [data-motion~="stagger"]:not(.is-in):not(:focus-within) > :not([data-motion]),
	html.js-motion [data-motion~="split"]:not(.is-in):not(:focus-within) .m-w {
		opacity: 0;
		transform: translateY(var(--motion-rise));
	}

	/* Direction E: reveals also come out of a light blur. */
	html.js-motion [data-motion~="reveal"]:not(.is-in):not(:focus-within),
	html.js-motion [data-motion~="stagger"]:not(.is-in):not(:focus-within) > :not([data-motion]) {
		filter: blur(6px);
	}

	/* Phones: no blur in the reveals (animating a filter is costly there); they still fade and rise. */
	@media (pointer: coarse), (max-width: 47.99rem) {
		html.js-motion [data-motion~="reveal"]:not(.is-in):not(:focus-within),
		html.js-motion [data-motion~="stagger"]:not(.is-in):not(:focus-within) > :not([data-motion]) {
			filter: none;
		}
	}

	html.js-motion [data-motion~="split"]:not(.is-in):not(:focus-within) .m-w { transform: translateY(0.45em); }

	html.js-motion [data-motion~="reveal"].is-in,
	html.js-motion [data-motion~="stagger"].is-in > :not([data-motion]),
	html.js-motion [data-motion~="split"].is-in .m-w {
		transition: opacity var(--motion-reveal) var(--motion-ease), transform var(--motion-reveal) var(--motion-ease), filter var(--motion-reveal) var(--motion-ease);
		transition-delay: calc(var(--i, 0) * var(--motion-step) + var(--w, 0) * 60ms);
	}

	.m-w { display: inline-block; }

	/* Image reveal: frame fades in, a mask lifts, the photo settles. */
	html.js-motion [data-motion~="image"]::after {
		content: "";
		position: absolute;
		inset: 0;
		z-index: 1;
		background: var(--motion-curtain);
		transform: scaleY(0);
		transform-origin: 50% 0;
		pointer-events: none;
	}

	html.js-motion [data-motion~="image"]:not(.is-in):not(:focus-within) { opacity: 0; }
	html.js-motion [data-motion~="image"]:not(.is-in):not(:focus-within)::after { transform: none; }
	html.js-motion [data-motion~="image"]:not(.is-in):not(:focus-within) img { transform: scale(1.08); }

	html.js-motion [data-motion~="image"].is-in {
		transition: opacity 500ms var(--motion-ease) var(--m-d, 0ms), translate 250ms var(--motion-ease);
	}

	html.js-motion [data-motion~="image"].is-in::after {
		transition: transform var(--motion-image) var(--motion-ease-io) calc(var(--m-d, 0ms) + 100ms);
	}

	html.js-motion [data-motion~="image"].is-in img {
		transition: transform 1400ms var(--motion-ease) var(--m-d, 0ms);
	}

	/* Fleuron: the two rules draw outward from the mark, then the mark fades in. */
	html.js-motion [data-motion~="fleuron"]:not(.is-in)::before { transform: scaleX(0); }
	html.js-motion [data-motion~="fleuron"]:not(.is-in)::after { opacity: 0; }
	html.js-motion [data-motion~="fleuron"].is-in::before { transition: transform 700ms var(--motion-ease); }
	html.js-motion [data-motion~="fleuron"].is-in::after { transition: opacity 500ms var(--motion-ease) 150ms; }

	/* Variation photo cross-fade (/livro/): the old image is a ghost laid over the new one. */
	html.js-motion [data-motion~="swap"] { position: relative; }

	.m-ghost {
		position: absolute;
		z-index: 2;
		margin: 0;
		object-fit: contain;
		pointer-events: none;
	}

	/* ── Hover (motion-owned) ── */

	@media (hover: hover) {
		.opds-button-arrow > .wp-block-button__link:hover::after { animation: m-arrow 520ms var(--motion-ease) backwards; }
	}

	.opds-button-arrow > .wp-block-button__link:focus-visible::after { animation: m-arrow 520ms var(--motion-ease) backwards; }

	/* ── Marquee of book phrases (built by motion.js) ── */

	[data-motion~="marquee"].is-ready {
		display: flex;
		align-items: center;
		gap: var(--wp--preset--spacing--20);
	}

	.m-view {
		flex: 1;
		min-width: 0;
		overflow: clip;
		-webkit-mask: linear-gradient(90deg, transparent, #000 8%, #000 92%, transparent);
		mask: linear-gradient(90deg, transparent, #000 8%, #000 92%, transparent);
	}

	.m-rail {
		display: flex;
		width: max-content;
		animation: m-marquee var(--m-dur, 40s) linear infinite;
	}

	[data-motion~="marquee"].is-ready .opds-marquee__track {
		flex-wrap: nowrap;
		justify-content: flex-start;
		gap: 0;
		padding-inline: 0; /* the two copies meet with the same spacing as the phrases */
	}

	[data-motion~="marquee"].is-ready .opds-marquee__track > p:last-child::after { display: inline-block; }

	[data-motion~="marquee"].is-off .m-rail,
	[data-motion~="marquee"]:focus-within .m-rail { animation-play-state: paused; }

	@media (hover: hover) {
		.m-view:hover .m-rail { animation-play-state: paused; }
	}
}

/* ── Scroll-linked, direction E (--p from motion.js: 0 entering at the bottom, 1 leaving at the top) ── */

@media screen {

	/* Hero book: tilted back as it enters, flat and a little larger as you scroll. */
	html.js-motion .opds-hero__book[data-motion~="progress"] .opds-card-photo {
		transform: rotateX(calc((1 - var(--p, 0) * 2.2) * 14deg)) scale(calc(0.94 + var(--p, 0) * 0.12));
		transition: transform 200ms linear;
	}

	/* Video card grows into place. */
	html.js-motion .opds-hero__video[data-motion~="progress"] {
		scale: calc(0.86 + var(--p, 0) * 0.28);
		transition: scale 200ms linear;
	}

	/* "A ideia do livro": the phrase stays while five cards fly up past it. */
	html.js-motion .opds-idea[data-motion~="pin"] {
		height: 270svh;
	}

	html.js-motion [data-motion~="pin"] .opds-idea__stage {
		position: sticky;
		top: 0;
		display: grid;
		place-items: center;
		height: 100svh;
		padding-block: 0;
		overflow: hidden;
	}

	html.js-motion [data-motion~="pin"] .opds-fly {
		display: flex;
		top: 100%;
		--t: clamp(0, calc((var(--p, 0) - var(--o)) * 2.6), 1);
		translate: 0 calc(var(--t) * (-100svh - 120%));
		rotate: var(--rot);
	}

	html.js-motion [data-motion~="pin"] .opds-fly:nth-child(2) { left: 4%; --o: 0; --rot: -6deg; }
	html.js-motion [data-motion~="pin"] .opds-fly:nth-child(3) { right: 5%; --o: 0.12; --rot: 5deg; }
	html.js-motion [data-motion~="pin"] .opds-fly:nth-child(4) { left: 20%; --o: 0.26; --rot: 3deg; }
	html.js-motion [data-motion~="pin"] .opds-fly:nth-child(5) { right: 18%; --o: 0.4; --rot: -4deg; }
	html.js-motion [data-motion~="pin"] .opds-fly:nth-child(6) { left: 6%; --o: 0.52; --rot: -2deg; }

	.opds-fly:nth-child(2) .opds-fly__img img { object-position: 50% 40%; }
	.opds-fly:nth-child(4) .opds-fly__img img { object-position: 50% 70%; }
	.opds-fly:nth-child(6) .opds-fly__img img { object-position: 50% 85%; }

	/* Phones: smaller cards at the edges, clear of the phrase. */
	@media (max-width: 40rem) {
		html.js-motion [data-motion~="pin"] .opds-fly {
			width: 7.5rem;
			padding: 0.8rem;
		}

		html.js-motion [data-motion~="pin"] .opds-fly .opds-fly__word { font-size: 1.05rem; }
		html.js-motion [data-motion~="pin"] .opds-fly:nth-child(even) { left: -1.5rem; right: auto; }
		html.js-motion [data-motion~="pin"] .opds-fly:nth-child(odd) { right: -1.5rem; left: auto; }
	}

	/* Author: the outlined name slides, the two cards straighten. */
	html.js-motion .opds-author-e[data-motion~="progress"] > .opds-author-e__name {
		translate: calc((var(--p, 0) - 0.5) * -30%) 0;
	}

	/* Phones: the wrapped name drifts only slightly, so it never leaves the screen. */
	@media (max-width: 39.99rem) {
		html.js-motion .opds-author-e[data-motion~="progress"] > .opds-author-e__name {
			translate: calc((var(--p, 0) - 0.5) * -6%) 0;
		}
	}

	html.js-motion .opds-author-e[data-motion~="progress"] .opds-duo__a { rotate: calc(-4deg + var(--p, 0) * 4deg); }
	html.js-motion .opds-author-e[data-motion~="progress"] .opds-duo__b { rotate: calc(5deg - var(--p, 0) * 5deg); }

	/* Final CTA: the blurred book drifts behind the card. */
	html.js-motion .opds-final[data-motion~="progress"] > .opds-final__bg {
		translate: 0 calc((var(--p, 0) - 0.5) * -8%);
		will-change: translate; /* Own layer: the blur is drawn once, then only moved. */
	}
}

/* ── Scroll-linked (CSS scroll-driven animations; browsers without them get none) ── */

@supports (animation-timeline: scroll()) {
	@media screen and (min-width: 64rem) and (hover: hover) and (pointer: fine) {
		.opds-detail[data-motion~="parallax"] { --m-par: 16px; }

		[data-motion~="parallax"] {
			animation: m-parallax linear both;
			animation-timeline: view();
			animation-range: cover;
		}
	}
}

/* Off switch: nothing from this file moves under html.m-off (the visitor turned animations off in the footer,
   or the system asks for reduced motion and the visitor has not turned them on). */
html.m-off [data-motion],
html.m-off [data-motion]::after,
html.m-off [data-motion] img,
html.m-off [data-motion~="hero"] > *,
html.m-off [data-motion~="intro"] > *,
html.m-off .m-rail {
	animation: none !important;
}

html.m-off .m-w,
html.m-off [data-motion~="image"],
html.m-off [data-motion~="image"] img,
html.m-off [data-motion~="reveal"],
html.m-off [data-motion~="stagger"] > *,
html.m-off [data-motion~="fleuron"]::before,
html.m-off [data-motion~="fleuron"]::after,
html.m-off [data-motion~="progress"],
html.m-off [data-motion~="tilt"] {
	transition: none !important;
	translate: none !important;
	rotate: none !important;
	scale: none !important;
}

html.m-off [data-motion~="tilt"],
html.m-off [data-motion~="progress"] .opds-card-photo {
	transform: none !important;
}


/* ── The book in 3D (assets/js/book3d.js): the canvas sits over the photo, which stays for its alt text ── */

.opds-card-photo,
.opds-ed__photo {
	position: relative;
}

.opds-book3d {
	position: absolute;
	inset: 0;
	display: block;
	width: 100%;
	height: 100%;
}

.is-3d > img {
	opacity: 0;
}

/* The model turns by itself: no CSS tilt or zoom on the photo underneath. */
html.js-motion .opds-hero__book[data-motion~="progress"] .opds-card-photo.is-3d,
.opds-ed .opds-ed__photo.is-3d img {
	transform: none !important;
}

/* A soft floor under the hero book. */
.opds-hero__book .opds-card-photo.is-3d {
	background:
		radial-gradient(ellipse 90% 45% at 50% 100%, color-mix(in srgb, var(--wp--preset--color--dun) 55%, transparent), transparent 70%),
		var(--wp--preset--color--sand);
}

html.m-off .opds-book3d {
	display: none;
}

html.m-off .is-3d > img {
	opacity: 1;
}

/* "A ideia do livro" with 3D books (book3d.js adds .has-books): no card, only the book. */
html.js-motion .opds-idea.has-books .opds-book3d--flock {
	z-index: 2;
	pointer-events: none;
}

html.js-motion .opds-idea.has-books .opds-fly {
	justify-content: flex-end;
	align-items: center;
	padding: 0;
	overflow: visible;
	background: none;
	box-shadow: none;
	color: var(--wp--preset--color--ebony);
}

html.js-motion .opds-idea.has-books .opds-fly::after,
html.js-motion .opds-idea.has-books .opds-fly__img,
html.js-motion .opds-idea.has-books .opds-fly__num {
	display: none;
}

/* No word under the books (user, 2026-12-01): the phrase in the middle says it. */
html.js-motion .opds-idea.has-books .opds-fly__word {
	display: none;
}

/* Phones: the books stay inside the screen so their words can be read. */
@media screen and (max-width: 40rem) {
	html.js-motion .opds-idea.has-books .opds-fly:nth-child(even) { left: 0.25rem; }
	html.js-motion .opds-idea.has-books .opds-fly:nth-child(odd) { right: 0.25rem; }
}

/* Smooth scrolling (assets/js/smooth.bundle.js, Lenis): Lenis drives the scroll, so the browser's own smoothing is off. */
html.lenis,
html.lenis body {
	height: auto;
}

.lenis.lenis-smooth {
	scroll-behavior: auto !important;
}

.lenis.lenis-smooth [data-lenis-prevent] {
	overscroll-behavior: contain;
}

.lenis.lenis-stopped {
	overflow: hidden;
}
