/*
 * Pinned ribbon callout.
 * ----------------------
 * A single inline SVG fixed to the right of the viewport. It has no live text —
 * the wording is part of the artwork — so nothing here lays out children; the
 * whole block is one picture that moves.
 *
 * The motion is three keyframes on ranges of the same scroll timeline.
 *
 * settle — over the first --isembyld-pinned-callout-settle px of scroll:
 *
 *   rise   up by however much the header shrinks when it condenses, so the two
 *          resolve together
 *   shift  right, off the edge of the viewport, until the arrow and the fold in
 *          the ribbon are clipped and only the text on the gradient is left.
 *          It rests inset by --isembyld-pinned-callout-right, so the distance
 *          that actually ends up off-screen is the shift less that inset.
 *
 * fade — immediately after settle, #ribbon-background (the fold + shadow under
 *          the tip) fades to transparent over the next 80px of scroll. Needs the
 *          SVG inlined; an <img> cannot target that group.
 *
 * shrink — later, as the ribbon runs into the slab after the one it sits in:
 *
 *   scale  down about the point of the ribbon that sits on the viewport's right
 *          edge once it has slid, so exactly the same slice of the artwork
 *          stays on screen, just smaller, with the top edge holding. The
 *          ribbon retreats toward the corner and stops covering the page.
 *
 *          Where "later" is comes from view.js, which measures the scroll
 *          position at which that slab's top edge meets the ribbon's bottom
 *          edge; an authored `shrinkStart` overrides it. Either way it is
 *          clamped to no earlier than the end of the settle, so the slide is
 *          never disturbed.
 *
 * The viewport does the clipping — the block is `position: fixed`, so anything
 * past the right edge is simply not painted. No overflow container, no mask.
 *
 * Placement is authored per instance (see placement.js): this design lands in
 * two different places, the homepage differing from the interior pages, and
 * that is a content decision rather than a build one. Only the defaults live
 * here.
 */

/*
 * Registered so they resolve to real lengths. Two reasons: `translate` and
 * `scale` can then interpolate them, and the Firefox path in view.js can read a
 * settled value out of getComputedStyle instead of a literal calc() token
 * stream.
 */
@property --isembyld-pinned-callout-rise {
	syntax: "<length>";
	inherits: false;
	initial-value: 0px;
}

@property --isembyld-pinned-callout-shift {
	syntax: "<length>";
	inherits: false;
	initial-value: 0px;
}

@property --isembyld-pinned-callout-settle {
	syntax: "<length>";
	inherits: false;
	initial-value: 160px;
}

/* 1 is "never shrinks", which is what a block with no shrink authored gets. */
@property --isembyld-pinned-callout-shrink-scale {
	syntax: "<number>";
	inherits: false;
	initial-value: 1;
}

@property --isembyld-pinned-callout-shrink-over {
	syntax: "<length>";
	inherits: false;
	initial-value: 240px;
}

/* Written by view.js: where the next slab meets the ribbon, before the rise. */
@property --isembyld-pinned-callout-shrink-at {
	syntax: "<length>";
	inherits: false;
	initial-value: 0px;
}

/* The trigger as authored or derived, and the same clamped to after the settle. */
@property --isembyld-pinned-callout-shrink-start {
	syntax: "<length>";
	inherits: false;
	initial-value: 0px;
}

@property --isembyld-pinned-callout-shrink-range-start {
	syntax: "<length>";
	inherits: false;
	initial-value: 0px;
}

.wp-block-isembyld-pinned-image-callout.isembyld-pinned-image-callout {
	/*
	 * Default rise: exactly the distance the header travels between its resting
	 * and condensed heights. Both operands come from sass/_header.scss. An
	 * authored `rise` overrides this from the inline style.
	 */
	--isembyld-pinned-callout-rise: calc(
		var(--header-flow-height, 209px) - var(--header-bar-stuck-height, 73px)
	);

	/*
	 * Default shrink start: the scroll position view.js measured for the next
	 * slab's top edge meeting the ribbon's resting bottom edge, plus the rise
	 * the ribbon has made by then. The rise is added here rather than in the
	 * script so the trigger keeps tracking the header if it is re-measured. An
	 * authored `shrinkStart` overrides this from the inline style.
	 *
	 * The clamp keeps the shrink off the slide. It also covers the case of no
	 * slab to meet: view.js leaves --shrink-at at its initial 0px, the sum is
	 * just the rise, and the shrink follows straight on from the settle.
	 */
	--isembyld-pinned-callout-shrink-start: calc(
		var(--isembyld-pinned-callout-shrink-at) +
			var(--isembyld-pinned-callout-rise)
	);
	--isembyld-pinned-callout-shrink-range-start: max(
		var(--isembyld-pinned-callout-settle),
		var(--isembyld-pinned-callout-shrink-start)
	);

	box-sizing: border-box;
	position: fixed;
	top: var(--isembyld-pinned-callout-top, 120px);
	right: var(--isembyld-pinned-callout-right, 0px);
	z-index: 50;

	/*
	 * The inset comes out of the available width as well, so a ribbon wider than
	 * the viewport runs off the right the way it is meant to rather than off the
	 * left.
	 */
	width: min(
		var(--isembyld-pinned-callout-width, 675px),
		calc(100vw - var(--isembyld-pinned-callout-right, 0px))
	);
	pointer-events: none;

	/*
	 * The shrink's pivot: the point of the box that lands on the viewport's
	 * right edge after the shift. Everything past it is already clipped, so
	 * scaling about it keeps the visible slice the same. The top edge holds.
	 * Clamped for a shift smaller than the inset, where nothing is clipped and
	 * the ribbon simply shrinks toward its own right edge.
	 */
	transform-origin: clamp(
			0%,
			calc(
				100% - var(--isembyld-pinned-callout-shift) +
					var(--isembyld-pinned-callout-right, 0px)
			),
			100%
		)
		0;
}

.isembyld-pinned-image-callout__link,
.isembyld-pinned-image-callout__surface {
	display: block;
	pointer-events: auto;
	text-decoration: none;
}

.isembyld-pinned-image-callout__image {
	display: block;
	width: 100%;
	height: auto;
}

/*
 * The fold + shadow under the tip. Visible at rest; once the ribbon has risen
 * and slid (settle), it fades out so only the gradient face remains. Kept as a
 * separate scroll-driven animation so it can start after settle rather than
 * tracking the rise 1:1.
 */
@keyframes isembyld-pinned-callout-fade-ribbon-bg {
	to {
		opacity: 0;
	}
}

@keyframes isembyld-pinned-callout-settle {
	to {
		translate: var(--isembyld-pinned-callout-shift)
			calc(-1 * var(--isembyld-pinned-callout-rise));
	}
}

@keyframes isembyld-pinned-callout-shrink {
	to {
		scale: var(--isembyld-pinned-callout-shrink-scale);
	}
}

/* --------------------------------------------------------------------------
 * Preferred path: scroll-driven animation. The only JS involved is the one-off
 * measurement of where the shrink starts (view.js); the motion itself is CSS.
 * -------------------------------------------------------------------------- */

@supports (animation-timeline: scroll()) {
	.wp-block-isembyld-pinned-image-callout.isembyld-pinned-image-callout {
		animation:
			isembyld-pinned-callout-settle linear both,
			isembyld-pinned-callout-shrink linear both;
		animation-timeline: scroll(root block), scroll(root block);
		animation-range:
			0 var(--isembyld-pinned-callout-settle),
			var(--isembyld-pinned-callout-shrink-range-start)
				calc(
					var(--isembyld-pinned-callout-shrink-range-start) +
						var(--isembyld-pinned-callout-shrink-over)
				);
		will-change: translate, scale;
	}

	.isembyld-pinned-image-callout__image #ribbon-background {
		animation: isembyld-pinned-callout-fade-ribbon-bg ease-out both;
		animation-timeline: scroll(root block);
		/* Kick in once the rise/slide has finished. */
		animation-range: var(--isembyld-pinned-callout-settle)
			calc(var(--isembyld-pinned-callout-settle) + 80px);
	}
}

/* --------------------------------------------------------------------------
 * Firefox path. Firefox ships no `animation-timeline`, so the block above never
 * applies there and the ribbon would sit at its resting offset and size
 * forever. view.js toggles `.is-scrolled` and `.is-shrunk` from
 * IntersectionObserver sentinels — two callbacks per crossing, nothing per
 * frame — and these transitions ease each single hop to the same end state.
 *
 * The @supports guard is load-bearing in both directions: without it
 * `animation-name` would also apply here with no timeline and a 0s duration,
 * snapping the ribbon to its settled position at the top of the page.
 * -------------------------------------------------------------------------- */

@supports not (animation-timeline: scroll()) {
	.wp-block-isembyld-pinned-image-callout.isembyld-pinned-image-callout {
		transition:
			translate 420ms cubic-bezier(0.33, 0.9, 0.35, 1),
			scale 420ms cubic-bezier(0.33, 0.9, 0.35, 1);
		will-change: translate, scale;
	}

	.isembyld-pinned-image-callout__image #ribbon-background {
		transition: opacity 420ms cubic-bezier(0.33, 0.9, 0.35, 1);
	}

	.wp-block-isembyld-pinned-image-callout.is-scrolled {
		translate: var(--isembyld-pinned-callout-shift)
			calc(-1 * var(--isembyld-pinned-callout-rise));
	}

	.wp-block-isembyld-pinned-image-callout.is-scrolled
		.isembyld-pinned-image-callout__image
		#ribbon-background {
		opacity: 0;
	}

	.wp-block-isembyld-pinned-image-callout.is-shrunk {
		scale: var(--isembyld-pinned-callout-shrink-scale);
	}

	@media (prefers-reduced-motion: reduce) {
		.wp-block-isembyld-pinned-image-callout.isembyld-pinned-image-callout,
		.isembyld-pinned-image-callout__image #ribbon-background {
			transition-duration: 0s;
		}
	}
}

/* -------------------------------------------------------------------------- */

.wp-block-isembyld-pinned-image-callout[data-hide-mobile="true"] {
	display: block;
}

@media (max-width: 1024px) {
	.wp-block-isembyld-pinned-image-callout[data-hide-mobile="true"] {
		display: none;
	}

	.wp-block-isembyld-pinned-image-callout.isembyld-pinned-image-callout {
		top: var(
			--isembyld-pinned-callout-top-mobile,
			var(--isembyld-pinned-callout-top, 120px)
		);
	}
}

.isembyld-pinned-image-callout__image .ribbon-shadow {
	opacity: 0.15;
}

