/* WickedBeforeAfter slider. The before image sits on top, clipped to --wba-pos; the after image is underneath. */
.wba {
	--wba-pos: 50%;
	--wba-over-l: 20px; /* how far the handle may hang out past each edge; JS lowers these where the page or a parent clips */
	--wba-over-r: 20px;
	--wba-n: 0.5; /* same as --wba-pos but a plain 0..1 number, for opacity maths */
	--wba-color: #fff;
	position: relative;
	overflow: visible; /* lets the handle sit half outside the image at either end */
	width: 100%;
	max-width: 100%;
	aspect-ratio: 16 / 9;
	margin: 1em auto 0;
	line-height: 0;
	user-select: none;
	-webkit-user-select: none;
}

.wba .wba__img {
	position: absolute;
	inset: 0;
	display: block;
	width: 100%;
	height: 100%;
	max-width: none;
	margin: 0;
	padding: 0;
	border: 0;
	object-fit: cover;
	pointer-events: none;
	-webkit-user-drag: none;
}

.wba .wba__img--before {
	clip-path: inset(0 calc(100% - var(--wba-pos)) 0 0);
}

/* Reversed: the before image is on the right, so "show only before" parks the handle on the left. */
.wba--reverse .wba__img--before {
	clip-path: inset(0 0 0 var(--wba-pos));
}

.wba__divider {
	position: absolute;
	top: 0;
	bottom: 0;
	left: var(--wba-pos);
	width: 2px;
	margin-left: -1px;
	background: var(--wba-color);
	box-shadow: 0 0 4px rgba(0, 0, 0, 0.4);
	pointer-events: none;
}

/* Sibling of the divider; centered on it, so at 0% / 100% it straddles the image edge. */
.wba__handle {
	position: absolute;
	z-index: 2;
	top: 50%;
	left: clamp(calc(20px - var(--wba-over-l)), var(--wba-pos), calc(100% - 20px + var(--wba-over-r)));
	cursor: ew-resize;
	touch-action: none;
	width: 40px;
	height: 40px;
	box-sizing: border-box;
	border: 2px solid var(--wba-color);
	border-radius: 50%;
	background: rgba(0, 0, 0, 0.45);
	/* Dark halo so the white ring stays visible where the handle hangs over a white page. */
	box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.35), 0 0 6px rgba(0, 0, 0, 0.5);
	/* Individual `translate` is applied before `scale`; a `transform` here would be scaled by the pulse and drift off-center. */
	translate: -50% -50%;
}

.wba__handle::before,
.wba__handle::after {
	content: "";
	position: absolute;
	top: 50%;
	margin-top: -5px;
	border: 5px solid transparent;
}

.wba__handle::before {
	left: 6px;
	border-right-color: var(--wba-color);
}

.wba__handle::after {
	right: 6px;
	border-left-color: var(--wba-color);
}

/* Invisible native range input covers the whole box: gives drag, touch, click-to-jump and keyboard for free. */
.wba .wba__range {
	position: absolute;
	z-index: 1;
	inset: 0;
	width: 100%;
	height: 100%;
	margin: 0;
	padding: 0;
	opacity: 0;
	cursor: ew-resize;
	touch-action: pan-y;
	-webkit-appearance: none;
	appearance: none;
	background: transparent;
}

.wba:has(.wba__range:focus-visible) .wba__handle {
	outline: 3px solid #2271b1;
	outline-offset: 2px;
}

/* Hidden while the slider is being dragged, and when toggled off with the "H" key: leaves just the line. */
.wba__handle {
	transition: opacity 0.2s;
}

.wba.is-clean .wba__handle,
.wba.is-dragging .wba__handle {
	opacity: 0;
	pointer-events: none; /* the range input underneath still handles dragging */
	animation: none;
}

.wba__tag {
	position: absolute;
	bottom: 10px;
	padding: 3px 8px;
	border-radius: 3px;
	background: rgba(0, 0, 0, 0.55);
	color: #fff;
	font-size: 13px;
	line-height: 1.3;
	pointer-events: none;
}

/* Tags sit above their "show only" links (before left, after right) and fade with how much of their image shows. */
.wba__tag--before {
	left: 10px;
	opacity: var(--wba-n);
}

.wba__tag--after {
	right: 10px;
	opacity: calc(1 - var(--wba-n));
}

.wba--reverse .wba__tag--before {
	opacity: calc(1 - var(--wba-n));
}

.wba--reverse .wba__tag--after {
	opacity: var(--wba-n);
}

.wba-links {
	display: flex;
	justify-content: space-between;
	gap: 1em;
	margin: 0.4em 0 1em;
}

.wba-links__btn {
	padding: 0;
	border: 0;
	background: none;
	color: inherit;
	font: inherit;
	text-decoration: underline;
	cursor: pointer;
}

.wba-error {
	padding: 0.5em 0.75em;
	border-left: 4px solid #d63638;
	background: #fcf0f1;
	color: #1d2327;
}

/* Pulse the handle while the pointer is elsewhere, to hint that it can be dragged. */
@keyframes wba-pulse {
	0% {
		box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.35), 0 0 6px rgba(0, 0, 0, 0.5), 0 0 0 0 rgba(255, 255, 255, 0.75);
		scale: 1;
	}
	50% {
		scale: 1.12;
	}
	70% {
		box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.35), 0 0 6px rgba(0, 0, 0, 0.5), 0 0 0 14px rgba(255, 255, 255, 0);
	}
	100% {
		box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.35), 0 0 6px rgba(0, 0, 0, 0.5), 0 0 0 0 rgba(255, 255, 255, 0);
		scale: 1;
	}
}

/* The arrows trade places: one fades to black as the other fades to the divider color, then they swap back. */
@keyframes wba-arrow-a {
	0%, 100% { border-right-color: var(--wba-color); }
	50% { border-right-color: #000; }
}

@keyframes wba-arrow-b {
	0%, 100% { border-left-color: #000; }
	50% { border-left-color: var(--wba-color); }
}

@media (prefers-reduced-motion: no-preference) {
	.wba:not(:hover):not(:focus-within):not(.is-dragging) .wba__handle {
		animation: wba-pulse 1.8s ease-out infinite;
	}

	.wba:not(:hover):not(:focus-within):not(.is-dragging) .wba__handle::before {
		animation: wba-arrow-a 1.8s ease-in-out infinite;
	}

	.wba:not(:hover):not(:focus-within):not(.is-dragging) .wba__handle::after {
		animation: wba-arrow-b 1.8s ease-in-out infinite;
	}
}
