/*
 * Ibiza — the effects layer.
 *
 * Everything here is CSS. No JavaScript, no library, no observer: scroll
 * reveals ride `animation-timeline: view()`, page transitions ride
 * `@view-transition`, the aurora and the glow are gradients with an animated
 * custom property. Browsers without those features get the page with the
 * motion left out, which is a design, not a fallback.
 *
 * Every animation sits behind `prefers-reduced-motion: no-preference`.
 *
 * @package Ibiza
 * @since   0.4.0
 */

/* -------------------------------------------------------------------------
 * Tokens
 * ---------------------------------------------------------------------- */

/*
 * Zero specificity on purpose.
 *
 * These are defaults, and a style variation sets its own tokens from the
 * global stylesheet, which is printed before this file. `:where()` scores
 * nothing, so the variation wins wherever it has an opinion and these fill in
 * everywhere else — regardless of which stylesheet the browser read first.
 */
:where(:root) {
	--fx-ease: cubic-bezier(0.2, 0.65, 0.3, 1);
	--fx-rise: 2.5rem;
	--fx-round: 1.25rem;

	/*
	 * Accent trio: every effect on the page takes its colour from these — the
	 * aurora, the glow, the gradient text.
	 *
	 * They default to the palette of whichever style variation is active, so
	 * a template tunes its own effects without being told to. The literals are
	 * only reached if a palette omits those slugs, and a variation can still
	 * set the trio outright when it wants a specific pairing.
	 */
	--fx-c1: var(--wp--preset--color--cala, oklch(0.72 0.19 25));
	--fx-c2: var(--wp--preset--color--higo, oklch(0.66 0.25 330));
	--fx-c3: var(--wp--preset--color--mar, oklch(0.78 0.15 195));

	/*
	 * Gradient text runs on its own ramp, starting at the ink colour.
	 *
	 * Reusing the aurora trio looked right on a dark palette and failed on a
	 * light one: three bright accents on near-white left the opening words
	 * barely readable. Starting from the text colour keeps the first and most
	 * important part legible in every variation, and a dark variation can
	 * override the ramp to something more luminous.
	 */
	--fx-g1: var(--wp--preset--color--tinta, currentColor);
	--fx-g2: var(--wp--preset--color--higo, oklch(0.66 0.25 330));
	--fx-g3: var(--wp--preset--color--mar, oklch(0.78 0.15 195));
}

@property --fx-spin {
	syntax: "<angle>";
	inherits: false;
	initial-value: 0deg;
}

html {
	scroll-behavior: smooth;
	scroll-padding-top: 5rem;
}

@media (prefers-reduced-motion: reduce) {
	html {
		scroll-behavior: auto;
	}
}

h1, h2, h3 {
	text-wrap: balance;
}

p {
	text-wrap: pretty;
}

/* -------------------------------------------------------------------------
 * Page transitions
 * ---------------------------------------------------------------------- */

@view-transition {
	navigation: auto;
}

@media (prefers-reduced-motion: no-preference) {
	::view-transition-old(root) {
		animation: fx-out 0.14s var(--fx-ease) both;
	}

	::view-transition-new(root) {
		animation: fx-in 0.2s var(--fx-ease) both;
	}
}

@keyframes fx-out {
	to {
		opacity: 0;
		transform: translateY(-0.75rem);
	}
}

@keyframes fx-in {
	from {
		opacity: 0;
		transform: translateY(1.25rem);
	}
}

/* The chrome stays put while the page beneath it changes. */
.ibiza-header {
	view-transition-name: ibiza-header;
}

.ibiza-footer {
	view-transition-name: ibiza-footer;
}

/* -------------------------------------------------------------------------
 * Scroll reveals
 * ---------------------------------------------------------------------- */

@supports (animation-timeline: view()) {
	@media (prefers-reduced-motion: no-preference) {
		.fx-reveal > *,
		.fx-rise {
			animation: fx-rise-in linear both;
			animation-timeline: view();
			animation-range: entry 10% entry 85%;
		}

		.fx-reveal > .fx-still {
			animation: none;
		}

		/* Children of a grid reveal one after another rather than as a slab. */
		.fx-reveal-stagger > * {
			animation: fx-rise-in linear both;
			animation-timeline: view();
			animation-range: entry 6% entry 80%;
		}
	}
}

/*
 * Opacity and transform only.
 *
 * This used to animate `filter: blur(6px)` as well, which cost two things: the
 * compositor re-rasterised every revealing element on every frame — dozens of
 * them on a long page — and the text stayed soft long enough to read as a
 * fault rather than an entrance. Both complaints were the same line of CSS.
 */
@keyframes fx-rise-in {
	from {
		opacity: 0;
		transform: translateY(var(--fx-rise)) scale(0.99);
	}

	to {
		opacity: 1;
		transform: none;
	}
}

/* A hero that drifts and dims as the page moves past it. */
@supports (animation-timeline: scroll()) {
	@media (prefers-reduced-motion: no-preference) {
		.fx-parallax {
			animation: fx-drift-up linear both;
			animation-timeline: scroll(root);
			animation-range: 0 85vh;
		}
	}
}

@keyframes fx-drift-up {
	to {
		transform: translateY(14%);
		opacity: 0.2;
	}
}

/* -------------------------------------------------------------------------
 * Aurora
 * ---------------------------------------------------------------------- */

.fx-aurora {
	position: relative;
	isolation: isolate;
	overflow: clip;
}

.fx-aurora::before {
	content: "";
	position: absolute;
	inset: -25% -15%;
	z-index: -2;
	/*
	 * Soft by construction, not by post-processing.
	 *
	 * This used to be three hard-edged gradients under `filter: blur(36px)`.
	 * A blur of that radius over a band wider than the viewport is the single
	 * most expensive thing a page can carry: measured on a throttled CPU it
	 * held the whole site at 12fps, and no amount of layer promotion helped,
	 * because the blur is re-rasterised as the visible region changes.
	 *
	 * Three stops per lobe with falling alpha give the same bloom for nothing.
	 * The eye cannot tell; the scroll can.
	 */
	background:
		radial-gradient(
			48% 52% at 18% 24%,
			color-mix(in oklab, var(--fx-c1) 90%, transparent) 0%,
			color-mix(in oklab, var(--fx-c1) 55%, transparent) 30%,
			color-mix(in oklab, var(--fx-c1) 18%, transparent) 56%,
			transparent 78%
		),
		radial-gradient(
			44% 50% at 84% 14%,
			color-mix(in oklab, var(--fx-c2) 90%, transparent) 0%,
			color-mix(in oklab, var(--fx-c2) 52%, transparent) 28%,
			color-mix(in oklab, var(--fx-c2) 16%, transparent) 54%,
			transparent 76%
		),
		radial-gradient(
			58% 62% at 62% 92%,
			color-mix(in oklab, var(--fx-c3) 88%, transparent) 0%,
			color-mix(in oklab, var(--fx-c3) 50%, transparent) 32%,
			color-mix(in oklab, var(--fx-c3) 15%, transparent) 58%,
			transparent 80%
		);
	filter: saturate(130%);
	opacity: 0.88;
	/* Melt into whatever comes next instead of ending on a hard line. */
	-webkit-mask-image: linear-gradient(to bottom, #000 0%, #000 58%, transparent 100%);
	mask-image: linear-gradient(to bottom, #000 0%, #000 58%, transparent 100%);
}

@media (prefers-reduced-motion: no-preference) {
	.fx-aurora::before {
		/*
		 * will-change: opacity, not transform. The layer is promoted so the
		 * blur is rasterised once; see the keyframes below for why nothing
		 * geometric is animated here.
		 */
		animation: fx-swell 19s var(--fx-ease) infinite alternate;
		will-change: opacity;
	}
}

/*
 * Opacity, not movement — and the reason is worth keeping.
 *
 * The aurora is a 36px blur over a band wider than the viewport. Animating
 * *any* geometric property on a blurred layer makes the browser rasterise that
 * blur again every frame, and measured on a throttled CPU it took the whole
 * page from 60fps to 12 — scrolling anywhere on the site stuttered because of
 * one decorative gradient. Translate alone did not help; nor did a smaller
 * blur. Opacity is a compositor property: the blur is rasterised once and then
 * only faded, which measures identically to no animation at all.
 *
 * So the light breathes instead of drifting. On a long page that reads much
 * the same, and the page scrolls.
 */
@keyframes fx-swell {
	from {
		opacity: 0.74;
	}

	to {
		opacity: 1;
	}
}

/* Film grain keeps big gradients from looking like plastic. */
.fx-grain::after {
	content: "";
	position: absolute;
	inset: 0;
	z-index: -1;
	pointer-events: none;
	/*
	 * No mix-blend-mode. A blend mode forces its own compositing pass across
	 * the whole band on every scroll frame, and plain low opacity looks near
	 * enough identical over a gradient.
	 */
	opacity: 0.16;
	/*
	 * Tiled, not stretched. feTurbulence is a procedural filter, so asking for
	 * it at the size of a full-bleed band means generating noise for every
	 * pixel of it. At 180px the browser rasterises one small tile and repeats
	 * it, which looks identical and costs almost nothing.
	 */
	background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.82' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='180' height='180' filter='url(%23n)'/%3E%3C/svg%3E");
	background-size: 180px 180px;
	background-repeat: repeat;
}

/* -------------------------------------------------------------------------
 * Surfaces
 * ---------------------------------------------------------------------- */

.fx-glass {
	background: color-mix(in oklab, canvastext 6%, transparent);
	border: 1px solid color-mix(in oklab, canvastext 12%, transparent);
	border-radius: var(--fx-round);
	backdrop-filter: blur(8px) saturate(140%);
	-webkit-backdrop-filter: blur(8px) saturate(140%);
}

/* On a dark band the glass is lit from the page, not the text colour. */
.has-background.has-text-color .fx-glass,
.fx-aurora .fx-glass {
	background: color-mix(in oklab, white 7%, transparent);
	border-color: color-mix(in oklab, white 16%, transparent);
}

.fx-glow {
	position: relative;
	border-radius: var(--fx-round);
	isolation: isolate;
}

.fx-glow::before {
	content: "";
	position: absolute;
	inset: -1.5px;
	z-index: -1;
	border-radius: inherit;
	padding: 1.5px;
	background: conic-gradient(
		from var(--fx-spin),
		var(--fx-c2),
		var(--fx-c3),
		var(--fx-c1),
		var(--fx-c2)
	);
	/* Paint the border box and knock the middle out, so only the ring shows.
	   Filling the whole box would flood any translucent panel behind it. */
	-webkit-mask:
		linear-gradient(#000 0 0) content-box,
		linear-gradient(#000 0 0);
	mask:
		linear-gradient(#000 0 0) content-box,
		linear-gradient(#000 0 0);
	-webkit-mask-composite: xor;
	mask-composite: exclude;
}

.fx-glow::after {
	content: "";
	position: absolute;
	inset: -14px;
	z-index: -2;
	border-radius: inherit;
	background: conic-gradient(
		from var(--fx-spin),
		var(--fx-c2),
		var(--fx-c3),
		var(--fx-c1),
		var(--fx-c2)
	);
	filter: blur(22px);
	opacity: 0.32;
}

/*
 * The halo is static until someone reaches for the card.
 *
 * `--fx-spin` is a custom property feeding a conic gradient, so animating it
 * repaints the gradient and re-applies a 22px blur on every frame — two
 * pseudo-elements per card, forever, on a page nobody was touching. The colour
 * is the effect; the rotation was costing a continuous repaint to add
 * something no one can consciously see.
 */
@media (prefers-reduced-motion: no-preference) and (hover: hover) {
	.fx-glow:hover::after,
	.fx-glow:hover::before,
	.fx-glow:focus-within::after,
	.fx-glow:focus-within::before {
		animation: fx-spin 9s linear infinite;
	}
}

@keyframes fx-spin {
	to {
		--fx-spin: 360deg;
	}
}

/* Hover lift. Cheap, and it makes a grid feel like objects. */
.fx-lift {
	transition: transform 0.35s var(--fx-ease), box-shadow 0.35s var(--fx-ease);
}

.fx-lift:hover,
.fx-lift:focus-within {
	transform: translateY(-6px);
	box-shadow: 0 1.5rem 3rem -1.5rem rgb(0 0 0 / 0.55);
}

@media (prefers-reduced-motion: reduce) {
	.fx-lift {
		transition: none;
	}

	.fx-lift:hover,
	.fx-lift:focus-within {
		transform: none;
	}
}

/* -------------------------------------------------------------------------
 * Type
 * ---------------------------------------------------------------------- */

.fx-gradient-text {
	background-image: linear-gradient(
		100deg,
		var(--fx-g1) 0%,
		var(--fx-g2) 52%,
		var(--fx-g3) 100%
	);
	-webkit-background-clip: text;
	background-clip: text;
	color: transparent;
	/* A gradient on transparent text disappears if the clip is unsupported,
	   so it is only applied where both halves are understood. */
}

@supports not ((background-clip: text) or (-webkit-background-clip: text)) {
	.fx-gradient-text {
		background-image: none;
		color: inherit;
	}
}

.fx-display {
	letter-spacing: -0.035em;
	line-height: 0.95;
}

/* -------------------------------------------------------------------------
 * Layout effects
 * ---------------------------------------------------------------------- */

/* Cards that stack under one another as the page scrolls. */
.fx-stack > * {
	position: sticky;
	top: 14vh;
}

.fx-marquee {
	overflow: clip;
	-webkit-mask-image: linear-gradient(90deg, transparent, black 8%, black 92%, transparent);
	mask-image: linear-gradient(90deg, transparent, black 8%, black 92%, transparent);
}

.fx-marquee__track {
	display: flex;
	gap: 3rem;
	width: max-content;
}

@media (prefers-reduced-motion: no-preference) {
	.fx-marquee__track {
		animation: fx-slide 42s linear infinite;
	}
}

@keyframes fx-slide {
	to {
		transform: translateX(-50%);
	}
}

/* -------------------------------------------------------------------------
 * Depth
 *
 * Real three-dimensional transforms, composited on the GPU, with no library
 * and no runtime. What this cannot do is a lit, textured, free-camera scene —
 * that needs WebGL, and WebGL is several hundred kilobytes of JavaScript. The
 * honest line is drawn here: everything below is geometry and light faked
 * well enough that most people cannot tell, at no cost.
 * ---------------------------------------------------------------------- */

.fx-stage {
	perspective: 1200px;
	perspective-origin: 50% 40%;
}

/* A card that leans toward the reader as it arrives. */
@supports (animation-timeline: view()) {
	@media (prefers-reduced-motion: no-preference) {
		.fx-stage .fx-deck > * {
			animation: fx-lean linear both;
			animation-timeline: view();
			animation-range: entry 8% entry 85%;
			transform-style: preserve-3d;
		}
	}
}

@keyframes fx-lean {
	from {
		opacity: 0;
		transform: perspective(1200px) rotateX(14deg) translate3d(0, 3rem, -9rem);
	}

	to {
		opacity: 1;
		transform: perspective(1200px) rotateX(0) translate3d(0, 0, 0);
	}
}

/*
 * Tilt toward the pointer.
 *
 * Without JavaScript there is no pointer position, so this leans a fixed
 * amount on hover instead of tracking. At these angles the difference is not
 * visible, and it costs nothing.
 */
.fx-tilt {
	transform-style: preserve-3d;
	transition: transform 0.45s var(--fx-ease);
	will-change: transform;
}

.fx-tilt:hover,
.fx-tilt:focus-within {
	transform: perspective(900px) rotateX(3deg) rotateY(-4deg) translateZ(1.5rem);
}

/* A specular sweep, so a tilted surface reads as a surface. */
.fx-shine {
	position: relative;
	overflow: clip;
	isolation: isolate;
}

.fx-shine::after {
	content: "";
	position: absolute;
	inset: -50%;
	z-index: 1;
	pointer-events: none;
	background: linear-gradient(
		100deg,
		transparent 42%,
		color-mix(in oklab, white 26%, transparent) 50%,
		transparent 58%
	);
	translate: -60% 0;
}

@media (prefers-reduced-motion: no-preference) {
	.fx-shine:hover::after,
	.fx-shine:focus-within::after {
		transition: translate 0.9s var(--fx-ease);
		translate: 60% 0;
	}
}

/* Layers that part as the page moves, at different depths. */
@supports (animation-timeline: view()) {
	@media (prefers-reduced-motion: no-preference) {
		.fx-depth > * {
			animation: fx-recede linear both;
			animation-timeline: view();
			animation-range: entry 0% exit 100%;
		}

		.fx-depth > :nth-child(2) { animation-name: fx-recede-mid; }
		.fx-depth > :nth-child(3) { animation-name: fx-recede-far; }
	}
}

@keyframes fx-recede     { from { translate: 0 4rem; } to { translate: 0 -4rem; } }
@keyframes fx-recede-mid { from { translate: 0 7rem; } to { translate: 0 -7rem; } }
@keyframes fx-recede-far { from { translate: 0 11rem; } to { translate: 0 -11rem; } }

/*
 * Chromatic fringe on display type.
 *
 * Two offset colour copies behind the glyphs, the way a lens splits light.
 * Kept small: past a pixel or two it stops looking optical and starts looking
 * broken.
 */
.fx-chroma {
	/*
	 * Fixed pixels, not em. In em the fringe scaled with the font, so at
	 * display sizes it became a two-pixel halo on every stem and the headline
	 * stopped being readable. Capped at one pixel, it reads as a lens and
	 * disappears the moment you stop looking for it.
	 */
	text-shadow:
		1px 0 0 color-mix(in oklab, var(--fx-c1) 32%, transparent),
		-1px 0 0 color-mix(in oklab, var(--fx-c3) 32%, transparent);
}

/* -------------------------------------------------------------------------
 * Video
 * ---------------------------------------------------------------------- */

.ibiza-video {
	position: relative;
	margin: 0;
	border-radius: var(--fx-round);
	overflow: clip;
}

/*
 * The box is reserved before the file is.
 *
 * With preload="none" the browser knows nothing about the video until someone
 * presses play, so `height: auto` collapsed it to the 300x150 default and the
 * whole page jumped when playback began. The real dimensions are written as
 * attributes when the video is in the media library; this is the fallback for
 * when it is not.
 */
.ibiza-video video {
	display: block;
	width: 100%;
	height: auto;
	aspect-ratio: var(--ibiza-video-ratio, 16 / 9);
	object-fit: cover;
	background: var(--wp--preset--color--tinta);
}

.ibiza-video figcaption {
	margin-top: 0.75rem;
	font-size: var(--wp--preset--font-size--small);
	color: var(--wp--preset--color--muted);
}

/*
 * A hero with moving film behind the words.
 *
 * The video is decoration: it is muted, it carries no information, and the
 * poster stands in wherever it does not play. Anyone who has asked for less
 * motion gets the poster and nothing else.
 */
.ibiza-hero--video {
	position: relative;
	isolation: isolate;
}

.ibiza-hero--video video.ibiza-hero__film {
	position: absolute;
	inset: 0;
	z-index: -2;
	width: 100%;
	height: 100%;
	object-fit: cover;
}

/*
 * The scrim.
 *
 * Footage is not a design decision the page gets to make — someone will put
 * a bright clip behind dark text and the headline will vanish. This keeps
 * enough of the background colour over the video that the text contrast is
 * the variation's contrast, not the video's. Erring heavy on purpose: a
 * slightly muted clip beats an unreadable headline.
 */
.ibiza-hero--video::before {
	content: "";
	position: absolute;
	inset: 0;
	z-index: -1;
	background:
		linear-gradient(
			to bottom,
			color-mix(in oklab, var(--wp--preset--color--sal) 90%, transparent),
			color-mix(in oklab, var(--wp--preset--color--sal) 74%, transparent) 55%,
			color-mix(in oklab, var(--wp--preset--color--sal) 92%, transparent)
		),
		color-mix(in oklab, var(--wp--preset--color--sal) 35%, transparent);
	backdrop-filter: blur(3px) saturate(115%);
	-webkit-backdrop-filter: blur(3px) saturate(115%);
	/*
	 * ::after is already spoken for by the film grain, and an element has one
	 * of each. Sharing it meant the grain's opacity and blend mode silently
	 * applied to the scrim, which looked like the scrim simply not working.
	 * These two are belt and braces against the same thing happening again.
	 */
	opacity: 1;
	mix-blend-mode: normal;
}

@media (prefers-reduced-motion: reduce) {
	.ibiza-hero--video video.ibiza-hero__film {
		display: none;
	}
}
