/**
 * AFHT Slider — front-end and editor styles.
 *
 * Slides are stacked in a single grid cell, so the slider is always as tall as
 * its tallest slide and never jumps when switching. Slide 1 is active in the
 * server-rendered markup, so the block looks right with JavaScript disabled.
 */

.afht-slider {
	--afht-min-height: clamp(20rem, 40vw, 32rem);
	/*
	 * Share of the slider the image covers, overridden per block from the
	 * Image width field. The divider is curved: the image bulges half of
	 * --afht-image-curve further out at its vertical middle and sits that much
	 * shorter at the top and bottom, so the field value is the curve's average
	 * reach. The depth is a length rather than a percentage so the arc keeps
	 * the same shape whatever image width the editor picks.
	 */
	--afht-image-width: 53%;
	--afht-image-curve: clamp(4rem, 12vw, 10rem);
	--afht-image-curve: 5vw;
	--afht-panel: linear-gradient(
		135deg,
		var(--wp--preset--color--primary, #0069ac),
		var(--wp--preset--color--primary-2, #004979)
	);
	--afht-duration: 600ms;
	--afht-shift: 3rem;
	--afht-dir: 1;
	--afht-dot-size: 1.25rem;
	--afht-dot-gap: 0px;
	--afht-dot-target: 1.625rem;
	/*
	 * The active square is white rather than the brand blue: the scrim behind
	 * the pagination is itself brand blue, so a blue marker disappeared into
	 * it. White also gives a luminance (not just hue) difference, so "which
	 * slide am I on" does not rely on colour perception alone.
	 */
	--afht-dot: rgba(2, 24, 43, 0.72);
	--afht-dot-hover: rgba(2, 24, 43, 0.9);
	--afht-dot-active: var(--wp--preset--color--white, #fff);
	--afht-ink: var(--wp--preset--color--white, #fff);
	--afht-pad-inline: clamp(1.25rem, 5vw, 4rem);
	--afht-pad-block: clamp(2rem, 5vw, 3.5rem);

	position: relative;
	isolation: isolate;
	overflow: hidden;
	color: var(--afht-ink);
}

/* -- Slides ------------------------------------------------------------- */

.afht-slider__slides {
	display: grid;
}

.afht-slider__slide {
	grid-area: 1 / 1;
	position: relative;
	display: grid;
	align-items: center;
	min-height: var(--afht-min-height);
	padding: var(--afht-pad-block) var(--afht-pad-inline);
	padding-bottom: calc(var(--afht-pad-block) + var(--afht-dot-size) + 1.5rem);
	background: var(--afht-panel);
	opacity: 0;
	visibility: hidden;
	transition:
		opacity var(--afht-duration) ease,
		visibility 0s linear var(--afht-duration);
}

.afht-slider__slide.is-active {
	opacity: 1;
	visibility: visible;
	transition:
		opacity var(--afht-duration) ease,
		visibility 0s linear 0s;
}

/* No pagination to clear when there is only one slide. */
.afht-slider:not(:has(.afht-slider__pagination)) .afht-slider__slide {
	padding-bottom: var(--afht-pad-block);
}

.afht-slider__slide:focus-visible {
	outline: 3px solid var(--afht-ink);
	outline-offset: -6px;
}

/* -- Media -------------------------------------------------------------- */

/*
 * No negative z-index here: the slide paints a background panel now, and a
 * negative index would drop the image behind it. Ordinary paint order (media
 * first in the DOM, content lifted to z-index 1) keeps the image above the
 * panel and below the text.
 */
.afht-slider__media {
	position: absolute;
	inset: 0;
	overflow: hidden;
}

.afht-slider__image {
	display: block;
	width: 100%;
	height: 100%;
	object-fit: cover;
}

/* Scrim: keeps text contrast predictable whatever image is uploaded. */
.afht-slider__media::after {
	content: "";
	position: absolute;
	inset: 0;
	background: linear-gradient(180deg, rgba(2, 24, 43, 0.45), rgba(2, 24, 43, 0.75));
}

/*
 * Wide screens: the image is masked to a panel on the left with a curved right
 * edge, and the copy sits on the solid panel beside it, so the image no longer
 * needs a scrim.
 *
 * The media box is narrowed to the curve's widest point, so the image is
 * fitted into the panel rather than showing through as a slice of a
 * full-bleed image. The mask is two layers added together:
 *   1. a solid block from the left edge up to where the curve starts;
 *   2. the arc itself, an SVG stretched over the last --afht-image-curve of the
 *      box. It is a quadratic curve whose control point sits at twice the box
 *      width, so the bow peaks exactly at the right edge halfway down.
 * The solid layer overlaps the arc by 1px so no hairline seam shows between
 * them.
 */
@media (min-width: 782px) {
	.afht-slider__media {
		right: auto;
		width: calc(var(--afht-image-width) + var(--afht-image-curve) / 2);
		mask-image:
			linear-gradient(#000, #000),
			url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100' preserveAspectRatio='none'%3E%3Cpath d='M0 0Q200 50 0 100Z'/%3E%3C/svg%3E");
		mask-size:
			max(0px, calc(100% - var(--afht-image-curve) + 1px)) 100%,
			var(--afht-image-curve) 100%;
		mask-position:
			left top,
			right top;
		mask-repeat: no-repeat;
	}

	.afht-slider__media::after {
		background: none;
	}
}

/* -- Content ------------------------------------------------------------ */

.afht-slider__content {
	position: relative;
	z-index: 1;
	display: flex;
	flex-direction: column;
	align-items: center;
	gap: 0.75rem;
	text-align: center;
	/* Long words break rather than escaping a narrow panel. */
	overflow-wrap: break-word;
}

/*
 * Line length is capped on the children, not on this container. An auto margin
 * or a non-stretch justify-self would switch the container to fit-content
 * sizing, which is driven by the longest unbreakable word — at high image
 * widths that grows the box straight back over the image.
 */
.afht-slider__content > * {
	max-width: min(100%, 40rem);
}

/*
 * The copy sits in a second grid column starting at the image's *widest*
 * point (the peak of the curve), so it can never collide with the image. The media is absolutely
 * positioned, so it is out of grid flow and still spans the whole slide.
 */
@media (min-width: 782px) {
	/*
	 * minmax(0, 1fr), not 1fr: a plain fr track floors at min-content, so one
	 * long word in a heading would widen the column and push the copy back
	 * over the image at high image widths.
	 */
	.afht-slider__slide {
		grid-template-columns: calc(var(--afht-image-width) + var(--afht-image-curve) / 2) minmax(0, 1fr);
		padding-inline: 0;
	}

	.afht-slider__content {
		box-sizing: border-box;
		grid-column: 2;
		min-width: 0;
		padding-inline: var(--afht-pad-inline);
	}
}

.afht-slider__subheading {
	margin: 0;
	font-size: var(--wp--preset--font-size--x-small, 0.85rem);
	font-weight: 600;
	letter-spacing: 0.12em;
	text-transform: uppercase;
	opacity: 0.92;
}

.afht-slider__heading {
	margin: 0;
	color: inherit;
	font-size: var(--wp--preset--font-size--xx-large, 2.2rem);
	font-weight: 700;
	line-height: 1.1;
	letter-spacing: 0.01em;
	text-transform: uppercase;
	text-wrap: balance;
}

.afht-slider__text {
	margin: 0;
	font-size: var(--wp--preset--font-size--medium, 1.2rem);
	line-height: 1.5;
	text-wrap: pretty;
}

.afht-slider__text a {
	color: inherit;
	text-decoration: underline;
}

.afht-slider__button {
	display: inline-block;
	margin-top: 0.5rem;
	padding: 0.85em 1.7em;
	background-color: var(--wp--preset--color--white, #fff);
	color: var(--wp--preset--color--contrast);
	font-size: var(--wp--preset--font-size--small);
	font-weight: bold;
	border-radius: 4px;
	line-height: 1;
	text-decoration: none;
	transition:
		background-color 200ms ease,
		color 200ms ease;
}

.afht-slider__button:hover,
.afht-slider__button:focus-visible {
	background-color: var(--wp--preset--color--primary);
	color: var(--afht-ink);
	text-decoration: none;
}

.afht-slider__button:focus-visible {
	outline: 3px solid var(--afht-ink);
	outline-offset: 3px;
}

/* -- Pagination --------------------------------------------------------- */

/* Dead controls are worse than no controls — see view.js. */
html:not(.afht-slider-has-js) .afht-slider__pagination {
	display: none;
}

/*
 * The controls come before the slides in the markup, so the pause button is
 * the first thing keyboard users reach, but they are drawn over the bottom of
 * the slider.
 */
.afht-slider__controls {
	position: absolute;
	right: var(--afht-pad-inline);
	bottom: clamp(1rem, 3vw, 2rem);
	z-index: 2;
	display: flex;
	align-items: center;
	gap: 0.5rem;
}

.afht-slider__pagination {
	display: flex;
	gap: var(--afht-dot-gap);
}

/*
 * The button is the hit target and stays above the 24px WCAG 2.2 target-size
 * minimum; the ::before is the smaller square you actually see.
 */
.afht-slider__dot,
.afht-slider__toggle {
	display: grid;
	place-items: center;
	box-sizing: border-box;
	width: var(--afht-dot-target);
	height: var(--afht-dot-target);
	margin: 0;
	padding: 0;
	appearance: none;
	border: 0;
	border-radius: 0;
	background: none;
	cursor: pointer;
}

.afht-slider__dot::before {
	content: "";
	width: var(--afht-dot-size);
	height: var(--afht-dot-size);
	background-color: var(--afht-dot);
	box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.45);
	transition:
		background-color 250ms ease,
		box-shadow 250ms ease;
}

.afht-slider__dot:hover::before {
	background-color: var(--afht-dot-hover);
	box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.8);
}

.afht-slider__dot.is-active::before {
	background-color: var(--afht-dot-active);
	box-shadow: inset 0 0 0 1px rgba(2, 24, 43, 0.2);
}

.afht-slider__dot:focus-visible,
.afht-slider__toggle:focus-visible {
	outline: 3px solid var(--afht-ink);
	outline-offset: -2px;
}

/* -- Pause / play ------------------------------------------------------- */

.afht-slider__toggle[hidden] {
	display: none;
}

/* Same square as an inactive dot, with the glyph stacked on top of it. */
.afht-slider__toggle::before,
.afht-slider__toggle::after {
	content: "";
	grid-area: 1 / 1;
}

.afht-slider__toggle::before {
	width: var(--afht-dot-size);
	height: var(--afht-dot-size);
	background-color: var(--afht-dot);
	box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.45);
	transition:
		background-color 250ms ease,
		box-shadow 250ms ease;
}

.afht-slider__toggle:hover::before {
	background-color: var(--afht-dot-hover);
	box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.8);
}

/* Pause: two bars. */
.afht-slider__toggle::after {
	width: 0.5rem;
	height: 0.55rem;
	background: linear-gradient(
		90deg,
		var(--afht-ink) 0 35%,
		transparent 35% 65%,
		var(--afht-ink) 65%
	);
}

/* Play: a triangle. */
.afht-slider__toggle.is-paused::after {
	background: var(--afht-ink);
	clip-path: polygon(15% 0, 100% 50%, 15% 100%);
}

/* -- Animation ---------------------------------------------------------- */

@keyframes afht-slide-in {
	from {
		opacity: 0;
		transform: translateX(calc(var(--afht-shift) * var(--afht-dir)));
	}

	to {
		opacity: 1;
		transform: none;
	}
}

@keyframes afht-media-in {
	from {
		transform: scale(1.06);
	}

	to {
		transform: none;
	}
}

.afht-slider[data-direction="prev"] {
	--afht-dir: -1;
}

.afht-slider__slide.is-active .afht-slider__content > * {
	animation: afht-slide-in var(--afht-duration) cubic-bezier(0.22, 0.61, 0.36, 1) both;
}

.afht-slider__slide.is-active .afht-slider__content > :nth-child(1) {
	animation-delay: 60ms;
}

.afht-slider__slide.is-active .afht-slider__content > :nth-child(2) {
	animation-delay: 130ms;
}

.afht-slider__slide.is-active .afht-slider__content > :nth-child(3) {
	animation-delay: 200ms;
}

.afht-slider__slide.is-active .afht-slider__content > :nth-child(4) {
	animation-delay: 270ms;
}

.afht-slider__slide.is-active .afht-slider__image {
	animation: afht-media-in calc(var(--afht-duration) * 2) ease-out both;
}

@media (prefers-reduced-motion: reduce) {
	.afht-slider {
		--afht-duration: 1ms;
		--afht-shift: 0px;
	}

	.afht-slider__slide.is-active .afht-slider__content > *,
	.afht-slider__slide.is-active .afht-slider__image {
		animation: none;
	}
}

/* -- Utilities ---------------------------------------------------------- */

.afht-slider__sr-only {
	position: absolute !important;
	width: 1px;
	height: 1px;
	margin: -1px;
	padding: 0;
	overflow: hidden;
	clip-path: inset(50%);
	white-space: nowrap;
	border: 0;
}

.afht-slider__placeholder {
	margin: 0;
	padding: 2rem;
	border: 1px dashed currentColor;
	text-align: center;
	opacity: 0.7;
}
