/*
 * Instalaunch Toolkit — shared image-effects library (front end + editor canvas).
 *
 * NEUTRAL, host-agnostic hover effects shared by every image surface: the core/image
 * visual layer now, the marquees after their migration. Nothing in here may reference
 * a block-specific class (.ilm-imarquee-*, .wp-block-image, …) — the host opts in via
 * the neutral class pair set by its own render path:
 *
 *   .ilm-fx-host        the element the effect lives on (core/image: the figure)
 *   .ilm-fx-{effect}    the effect body (same element)
 *
 * Effect values are CSS custom properties with the three-level chain from BRIEF:
 * per-instance override → --ilm-* token → hardcoded fallback.
 *
 * NAMING LOCK (BRIEF, CSS custom property naming): every property name in this file
 * uses --ilm-fx-*; radius, when an effect ever needs it, is --ilm-img-rad. Never use
 * border-width / border-color / border-style / border-radius / radius / shape as
 * substrings in a property NAME — WP core ships permanent [style*=border-*] substring
 * traps, and radius/shape have bitten before.
 *
 * Motion rules:
 *  - Compositor-only: transform + opacity, never layout/paint properties.
 *  - Hover motion (and its transition) lives inside @media (hover:hover) and
 *    (pointer:fine) — touch devices keep the static rest image, outside the gate.
 *  - prefers-reduced-motion: reduce freezes everything at rest.
 *  - No persistent will-change: these are pure hover transitions — the browser
 *    promotes a layer for the transition's lifetime; pinning one for the page's
 *    life is exactly what BRIEF's release pattern exists to avoid.
 *
 * twShape precedence (future effects): tilt transforms, it never clips, so it cannot
 * collide with Twentig's shape clip-path. Any future CLIP-BASED effect (zoom's
 * overflow clip, reveal masks) MUST defer to twShape — skip our clip when the block
 * has a shape set ([style*="--shape"] on the figure / the twShape attribute in PHP).
 *
 * @package instalaunch-toolkit
 * @since   1.0.0
 */

/* ── Host contract ───────────────────────────────────────────────────────────
 * Only what the current effects need. Later effects extend this (zoom adds its
 * owned clip on a child scope, overlay positions against it).
 */
.ilm-fx-host {
	position: relative;
}

/* ── Tilt ────────────────────────────────────────────────────────────────────
 * Technique copied from the image-marquee 3D tilt (perspective + rotateX) and
 * decoupled from the marquee wrapper; here it is a pointer-driven hover state
 * rather than a static strip transform. Rest state is the element's natural
 * box — no rule needed outside the gate, so touch devices ship zero motion CSS.
 */
@media (hover: hover) and (pointer: fine) {
	.ilm-fx-tilt {
		transition: transform
			var( --ilm-fx-duration, var( --ilm-duration-medium, 300ms ) )
			var( --ilm-fx-ease, var( --ilm-ease-out, ease ) );
	}

	.ilm-fx-tilt:hover {
		transform: perspective( 600px )
		           rotateX( var( --ilm-fx-tilt-rotate, 4deg ) )
		           translateY( var( --ilm-fx-tilt-lift, -6px ) )
		           scale( var( --ilm-fx-tilt-scale, 1.02 ) );
	}
}

/* ── Lift ────────────────────────────────────────────────────────────────────
 * Rise + shadow. The shadow rides the WordPress preset shadow token with a
 * hardcoded fallback (BRIEF: prefer native presets; three-level chain). The
 * box-shadow paints on hover only — the motion itself stays transform-only.
 */
@media (hover: hover) and (pointer: fine) {
	.ilm-fx-lift {
		transition: transform
				var( --ilm-fx-duration, var( --ilm-duration-medium, 300ms ) )
				var( --ilm-fx-ease, var( --ilm-ease-out, ease ) ),
			box-shadow
				var( --ilm-fx-duration, var( --ilm-duration-medium, 300ms ) )
				var( --ilm-fx-ease, var( --ilm-ease-out, ease ) );
	}

	.ilm-fx-lift:hover {
		transform: translateY( var( --ilm-fx-lift-y, -8px ) );
		box-shadow: var( --wp--preset--shadow--natural, 0 8px 24px rgba( 0, 0, 0, 0.15 ) );
	}
}

/* ── Zoom ────────────────────────────────────────────────────────────────────
 * The first CLIP effect: the HOST owns a static overflow clip and the img
 * scales inside it (compositor-only; the clip never animates). Rounded corners
 * are honoured two ways: overflow:hidden already follows the host's own
 * border-radius (core's inline style from the block's Radius control), and a
 * per-instance override may set the SAFE-named --ilm-img-rad (naming lock: never
 * radius / border-* / shape substrings in a property name).
 *
 * twShape precedence: when the block carries a Twentig shape, Twentig clips the
 * img itself via clip-path:var(--shape) — a rectangular host clip would cut the
 * scaled shape at the figure edges and the two would fight. The render path adds
 * ilm-fx-zoom--shaped in that case, deferring the clip entirely to the shape
 * (which scales with the img, so pixels still never escape it).
 */
.ilm-fx-zoom {
	overflow: hidden;
	border-radius: var( --ilm-img-rad, 0px );
}

.ilm-fx-zoom.ilm-fx-zoom--shaped {
	overflow: visible;
}

@media (hover: hover) and (pointer: fine) {
	.ilm-fx-zoom img {
		transition: transform
			var( --ilm-fx-duration, var( --ilm-duration-medium, 300ms ) )
			var( --ilm-fx-ease, var( --ilm-ease-out, ease ) );
	}

	.ilm-fx-zoom:hover img {
		transform: scale( var( --ilm-fx-zoom-scale, 1.08 ) );
	}
}

/* ── Grayscale → colour (treatment, composes with any motion) ────────────────
 * The ENTIRE effect lives inside the pointer gate: touch devices get the
 * natural colour image, never a stuck-grey rest state. filter is the accepted
 * non-compositor exception at single-image hover scale (like lift's shadow).
 * Under reduced motion the treatment keeps both end states (grey at rest,
 * colour on hover) — only the transition is removed by the freeze block, which
 * never touches filter.
 */
@media (hover: hover) and (pointer: fine) {
	.ilm-fx-grayscale img {
		filter: grayscale( 1 );
		transition: filter
			var( --ilm-fx-duration, var( --ilm-duration-medium, 300ms ) )
			var( --ilm-fx-ease, var( --ilm-ease-out, ease ) );
	}

	.ilm-fx-grayscale:hover img {
		filter: grayscale( 0 );
	}

	/* Composition: zoom and grayscale both declare `transition` on the same img
	 * (transform vs filter) — at equal specificity the later rule would clobber
	 * the earlier and one layer would snap. The combined selector transitions
	 * both. */
	.ilm-fx-zoom.ilm-fx-grayscale img {
		transition: transform
				var( --ilm-fx-duration, var( --ilm-duration-medium, 300ms ) )
				var( --ilm-fx-ease, var( --ilm-ease-out, ease ) ),
			filter
				var( --ilm-fx-duration, var( --ilm-duration-medium, 300ms ) )
				var( --ilm-fx-ease, var( --ilm-ease-out, ease ) );
	}
}

/* ── Overlay + caption reveal (treatment, composes with any motion) ──────────
 * Scrim: a ::after on the host with a bottom-weighted gradient, hidden at rest,
 * revealed on hover (pointer-gated). Colour rides the theme's contrast preset
 * via color-mix, with a plain rgba fallback through the same custom property.
 *
 * Caption: a native <figcaption>, when present, is repositioned over the image
 * bottom (above the scrim). Hidden at rest via opacity (NOT display:none — it
 * stays in the accessibility tree) and revealed on hover. Outside the gate
 * (touch) the rest state is VISIBLE, so touch users always see the caption; its
 * own translucent strip keeps it legible there without the scrim (M02
 * precedent). Without a figcaption the scrim simply ships alone.
 */
.ilm-fx-overlay::after {
	content: '';
	position: absolute;
	inset: 0;
	z-index: 1; /* above the img, below the caption */
	pointer-events: none;
	background: linear-gradient(
		to top,
		var( --ilm-fx-overlay-color, color-mix( in srgb, var( --wp--preset--color--contrast, #000 ) 65%, transparent ) ),
		transparent 60%
	);
	opacity: 0;
}

.ilm-fx-overlay figcaption {
	position: absolute;
	left: 0;
	right: 0;
	bottom: 0;
	z-index: 2;
	margin: 0;
	padding: 8px 12px;
	text-align: center;
	color: var( --ilm-fx-caption-color, #fff );
	background: var( --ilm-fx-caption-bg, rgba( 0, 0, 0, 0.45 ) );
}

@media (hover: hover) and (pointer: fine) {
	.ilm-fx-overlay::after {
		transition: opacity
			var( --ilm-fx-duration, var( --ilm-duration-medium, 300ms ) )
			var( --ilm-fx-ease, var( --ilm-ease-out, ease ) );
	}

	.ilm-fx-overlay:hover::after {
		opacity: 1;
	}

	.ilm-fx-overlay figcaption {
		opacity: 0;
		transform: translateY( 6px );
		transition: opacity
				var( --ilm-fx-duration, var( --ilm-duration-medium, 300ms ) )
				var( --ilm-fx-ease, var( --ilm-ease-out, ease ) ),
			transform
				var( --ilm-fx-duration, var( --ilm-duration-medium, 300ms ) )
				var( --ilm-fx-ease, var( --ilm-ease-out, ease ) );
	}

	.ilm-fx-overlay:hover figcaption {
		opacity: 1;
		transform: none;
	}
}

/* ── Parallax (scroll-driven CSS; composes with everything except zoom) ──────
 * The host figure is the clip frame; the img is oversized via transform scale
 * (never width/height — the layout box is untouched, so zero CLS) and drifts on
 * translateY along its own view() timeline. Compositor-only, no JS, no
 * observer, no off-screen-pause (scroll-driven animations only run while the
 * element progresses through the viewport).
 *
 * EVERYTHING lives inside @supports (animation-timeline: view()) and
 * @media (prefers-reduced-motion: no-preference): outside either gate there is
 * no oversize, no clip, no drift — the image is its normal fully-visible self.
 *
 * Zoom exclusion is enforced upstream (render + editor guard — parallax wins):
 * both animate the img's transform and could never compose.
 *
 * twShape precedence, same rule as zoom: with a shape set, the --shaped
 * modifier drops our overflow clip and Twentig's clip-path (which rides the
 * img, scaling and drifting with it) does the clipping.
 */
@supports (animation-timeline: view()) {
	@media (prefers-reduced-motion: no-preference) {
		.ilm-fx-parallax {
			/* clip, NOT hidden: overflow:hidden makes the figure a scroll container,
			 * which would become the img's view() timeline source — the img never
			 * moves inside its own clip frame, so the timeline sits inactive and the
			 * drift freezes (measured). overflow:clip clips identically (including
			 * to border-radius) without creating a scroll container, leaving the
			 * viewport as the timeline. */
			overflow: clip;
			border-radius: var( --ilm-img-rad, 0px );
		}

		.ilm-fx-parallax.ilm-fx-parallax--shaped {
			overflow: visible;
		}

		.ilm-fx-parallax img {
			animation: ilm-fx-parallax-drift linear both;
			animation-timeline: view(); /* must follow the shorthand — it resets timeline */
		}

		@keyframes ilm-fx-parallax-drift {
			from {
				transform: translateY( calc( -1 * var( --ilm-fx-parallax-drift, 6% ) ) )
					scale( var( --ilm-fx-parallax-scale, 1.15 ) );
			}
			to {
				transform: translateY( var( --ilm-fx-parallax-drift, 6% ) )
					scale( var( --ilm-fx-parallax-scale, 1.15 ) );
			}
		}
	}
}

/* ── Parallax inside a rounded group: clip, don't scroll ──────────────────────
 * The bundled Twentig fork CSS (dist/blocks/common.css, dist/style-index.css) gives every
 * rounded group that contains an image `overflow: hidden`, so the image is clipped to the
 * corners. `hidden` makes the group a scroll container, which becomes the parallax image's
 * view() timeline source — the group never scrolls, so the drift freezes (measured on the
 * staging homepage: timeline progress stuck at 62% while scrolling). `overflow: clip` clips
 * identically, border-radius included, without creating a scroll container.
 *
 * Scoped to groups that actually contain a parallax image: every other rounded group keeps
 * the fork rule exactly as it is. dist/ is untouched. (0,3,0) beats the fork's (0,2,1).
 * A browser without overflow:clip ignores this and keeps the fork's hidden.
 */
@supports (overflow: clip) {
	.wp-block-group[style*=radius]:has( .ilm-fx-parallax ) {
		overflow: clip;
	}
}

/* ── Reduced motion — freeze every effect at rest ────────────────────────────
 * transform/transition ONLY (the descendant img covers zoom's scale; the
 * ::after/figcaption selectors cover the overlay's transitions). Treatments'
 * end states (grayscale filter, scrim/caption opacity) are NOT motion and stay
 * live — they just become instant, per the M02 precedent.
 */
@media (prefers-reduced-motion: reduce) {
	[class*="ilm-fx-"],
	[class*="ilm-fx-"]:hover,
	[class*="ilm-fx-"] img,
	[class*="ilm-fx-"]:hover img {
		transition: none !important;
		transform: none !important;
	}

	[class*="ilm-fx-"]::after,
	[class*="ilm-fx-"] figcaption {
		transition: none !important;
	}
}

/* ── Stickers (1.6.1) ────────────────────────────────────────────────────────
 * Small decorative images with their own motion, and optional pinned placement
 * over a section. Every value arrives clamped from the render filter as a
 * custom property on the figure; the fallbacks here are the same defaults.
 *
 * MOTION RIDES THE IMG'S INDEPENDENT `rotate` / `translate` PROPERTIES, never
 * `transform`. That is the whole composition story: the hover effects (tilt,
 * lift) and entrance reveals use the figure's `transform`, zoom uses the img's
 * `transform`, and a parent section's parallax uses `translate` on the figure —
 * so a sticker adds motion to the img without clobbering any of them. Neither
 * property affects layout, so a sticker can never cause a shift.
 *
 * Mutually exclusive with Hover Zoom (its clip would cut a rotating image's
 * corners) and Image Parallax (its oversize-and-clip window is a different
 * thing); the render filter and the editor both enforce it.
 *
 * Reduced motion: every motion rule sits inside
 * `@media (prefers-reduced-motion: no-preference)`, so under `reduce` a sticker
 * is simply a static image at its resting position. Off-screen: the continuous
 * motions are paused by the shared observer (ilm-offscreen-pause.css); the
 * scroll motion needs nothing, since it only advances while the page moves.
 */

@keyframes ilm-stk-spin {
	from { rotate: 0deg; }
	to   { rotate: 360deg; }
}

@keyframes ilm-stk-rock {
	from { rotate: calc( -1 * var( --ilm-stk-amp, 6deg ) ); }
	to   { rotate: var( --ilm-stk-amp, 6deg ); }
}

@keyframes ilm-stk-float-y {
	from { translate: 0 calc( -1 * var( --ilm-stk-amp, 10px ) ); }
	to   { translate: 0 var( --ilm-stk-amp, 10px ); }
}

@keyframes ilm-stk-float-x {
	from { translate: calc( -1 * var( --ilm-stk-amp, 10px ) ) 0; }
	to   { translate: var( --ilm-stk-amp, 10px ) 0; }
}

/* Scroll-driven: the rotation is spread over the element's own pass through the
   viewport — the same timeline the parallax drift uses, so "turns per screen"
   means the same thing in both. */
@keyframes ilm-stk-turn {
	from { rotate: 0deg; }
	to   { rotate: var( --ilm-stk-turn, 360deg ); }
}

@media ( prefers-reduced-motion: no-preference ) {

	.ilm-stk--spin img,
	.ilm-stk--rock img,
	.ilm-stk--float img {
		animation-duration: var( --ilm-stk-dur, 12s );
		animation-delay: var( --ilm-stk-delay, 0s );
		animation-iteration-count: infinite;
		animation-fill-mode: both;
	}

	.ilm-stk--spin img {
		animation-name: ilm-stk-spin;
		animation-timing-function: linear;
	}

	/* Rock and Float swing back and forth: alternate + ease-in-out, so the ends
	   ease rather than snap. A pill-shaped sticker rocking about its centre is
	   exactly "the ends rise and fall while the centre stays still". */
	.ilm-stk--rock img,
	.ilm-stk--float img {
		animation-direction: alternate;
		animation-timing-function: ease-in-out;
	}

	.ilm-stk--rock img  { animation-name: ilm-stk-rock; }
	.ilm-stk--float img { animation-name: ilm-stk-float-y; }
	.ilm-stk--float.ilm-stk--side img { animation-name: ilm-stk-float-x; }

	/* Anticlockwise: the same keyframes, played backwards. */
	.ilm-stk--ccw img { animation-direction: reverse; }

	@supports ( animation-timeline: view() ) {
		.ilm-stk--scroll img {
			animation: ilm-stk-turn linear both;
			animation-timeline: view(); /* must follow the shorthand — it resets the timeline */
		}

		.ilm-stk--scroll.ilm-stk--ccw img {
			animation-direction: reverse;
		}
	}
}

/* ── Pinned placement ────────────────────────────────────────────────────────
 * The sticker leaves the flow and sits at a percentage position over its
 * section, centred on that point. Percentages only — never pixel structure —
 * so it holds its place as the section resizes.
 *
 * The section becomes the positioning context through :has(), scoped to
 * sections that actually contain a pinned sticker; every other block is
 * untouched. `pointer-events: none` keeps decoration from eating clicks meant
 * for the content underneath, and the whole thing is static markup rendered on
 * the server, so there is no layout shift.
 */
.ilm-stk-pin {
	position: absolute;
	/* Placement is one expression for both modes. An ANCHOR sets the percentage AND the
	   translate origin together (left/top 0% with translate 0, right/bottom 100% with
	   translate -100%), so the sticker's own edge lands on the section's edge and it sits
	   fully inside. CUSTOM leaves the translate at -50% and centres the sticker on its
	   X/Y point, which is what pinning did before presets. The nudge is a length added to
	   whichever the mode produced; its sign is decided in PHP (positive moves away from
	   the anchored edge), so this stays one rule. Every fallback is the centred case, so
	   an unset pin renders exactly as it always did. */
	left: calc( var( --ilm-stk-x, 50% ) + var( --ilm-stk-nx, 0px ) );
	top: calc( var( --ilm-stk-y, 50% ) + var( --ilm-stk-ny, 0px ) );
	/* Width must not depend on WHERE the sticker is pinned. An absolutely positioned
	   box with an auto width shrink-to-fits into the space between its inset and the
	   opposite edge of its containing block — so `left: 92%` leaves it 8% of the
	   section to live in, and a 110px sticker rendered 30px wide on a phone (measured
	   on staging, 1.6.1). It hid on desktop by arithmetic luck: 8% of a 1366px section
	   is 109px, a pixel under the same sticker's 110px cap.

	   max-content takes the size from the image instead of from the leftover space, so
	   the sticker is the same size at 5% and at 95%. It must be the bare keyword: a
	   percentage anywhere in `width` (min( max-content, 100% ) was tried) is resolved
	   against that same leftover space, which puts the squeeze straight back — measured
	   19px at 95%. The section bound therefore lives in max-width, below. Height was
	   never squeezed: only width shrink-to-fits, and the height followed the wrong
	   width through the aspect ratio. */
	width: max-content;
	translate: var( --ilm-stk-tx, -50% ) var( --ilm-stk-ty, -50% );
	margin: 0;
	z-index: 2;
	pointer-events: none;
}

/* Keeps an UNCAPPED pinned sticker inside its section (a 1000px image on a 375px phone
   otherwise stays 1000px and scrolls the document sideways — measured). Two classes,
   (0,2,0): that beats the constrained-layout rule
   `.is-layout-constrained > :where(...)`, which sets max-width to the theme's content
   size at (0,1,0) later in the cascade, and still loses to the author's own Max width
   at `figure.wp-block-image.ilm-img-sized` (0,2,1). */
.wp-block-image.ilm-stk-pin {
	max-width: 100%;
	/* The same cascade trap, twice. `.is-layout-constrained > :where(...)` also sets
	   margin-left/right to auto at (0,1,0), later in the cascade than `.ilm-stk-pin`,
	   so it wins there too. On the front end that is harmless — `right` is auto, so
	   auto margins resolve to 0 — but the EDITOR gives the block wrapper `right: 0`,
	   and a box with both insets, a definite width and auto margins is centred: every
	   pinned sticker sat in the middle of the canvas whatever its anchor (measured:
	   margin-left 273.5px on a `left: 0` sticker). Pinning `right` and the margins here,
	   at (0,2,0), makes the canvas place stickers exactly as the front end does. */
	right: auto;
	margin: 0;
}

/* ── Editor canvas ───────────────────────────────────────────────────────────
 * In the canvas the block wrapper IS the figure, and the editor's own block-list
 * styles give every wrapper `position: relative` and `right: 0`. With the
 * constrained layout's `margin: auto` on top, every pinned sticker was centred in
 * the canvas whatever its anchor — measured: `margin-left: 273.5px` on a sticker
 * whose own rule says `left: 0`. The canvas showed a column of stacked images
 * while the front end placed them correctly, which is the worst kind of preview.
 *
 * Three classes (0,3,0) outrank the editor's rules; the selector is built from the
 * editor's own class, so none of this can reach the front end. `pointer-events` is
 * restored because a decoration that ignores clicks is right for a visitor and
 * wrong for an author trying to select it.
 */
.block-editor-block-list__block.wp-block-image.ilm-stk-pin {
	position: absolute;
	right: auto;
	margin: 0;
	pointer-events: auto;
}

/* Behind the content instead of over it. */
.ilm-stk-pin.ilm-stk-back {
	z-index: 0;
}

:where( .wp-block-group, .wp-block-cover, .wp-block-columns, .wp-block-column, .wp-block-media-text ):has( > .ilm-stk-pin ) {
	position: relative;
}

/* Cover paints its own overlay and content above the background; a sticker
   pinned inside one belongs above the overlay, not under it. */
.wp-block-cover:has( > .ilm-stk-pin ) > .ilm-stk-pin {
	z-index: 2;
}

@media ( max-width: 767px ) {
	.ilm-stk-hide-m {
		display: none;
	}
}
