/**
 * Image Popup — front end styles.
 *
 * Sizing contract: the image is laid out at its natural pixel size and only
 * ever scales DOWN to fit the viewport. width/height stay `auto` so the
 * browser preserves the intrinsic aspect ratio while both max-* caps apply.
 */

.ipop {
	--ipop-gutter: 2rem;
	--ipop-chrome: 0px;
	--ipop-backdrop: rgba(12, 14, 18, 0.82);
	--ipop-radius: 4px;
	--ipop-speed: 220ms;

	position: fixed;
	inset: 0;
	z-index: 999999;
	display: flex;
	align-items: center;
	justify-content: center;
	padding: var(--ipop-gutter);
	box-sizing: border-box;
}

.ipop[hidden] {
	display: none;
}

.ipop *,
.ipop *::before,
.ipop *::after {
	box-sizing: border-box;
}

/* ---------------------------------------------------------------- backdrop */

.ipop__backdrop {
	position: absolute;
	inset: 0;
	background: var(--ipop-backdrop);
	opacity: 0;
	transition: opacity var(--ipop-speed) ease;
	cursor: pointer;
}

.ipop.is-open .ipop__backdrop {
	opacity: 1;
}

/* ------------------------------------------------------------------ dialog */

.ipop__dialog {
	position: relative;
	max-width: 100%;
	max-height: 100%;
	opacity: 0;
	transform: scale(0.96);
	transition: opacity var(--ipop-speed) ease, transform var(--ipop-speed) ease;
}

.ipop.is-open .ipop__dialog {
	opacity: 1;
	transform: none;
}

.ipop__figure {
	margin: 0;
	padding: 0;
	width: -moz-fit-content;
	width: fit-content;
	max-width: 100%;
}

/* ------------------------------------------------------------------- image */

.ipop__image {
	display: block;
	width: auto;
	height: auto;
	margin: 0 auto;
	border-radius: var(--ipop-radius);
	background: rgba(255, 255, 255, 0.06);

	/* Fallback for browsers without min(): fit the viewport, never upscale via
	   the width/height attributes the browser already knows about. */
	max-width: 100%;
	max-height: calc(100vh - (var(--ipop-gutter) * 2) - var(--ipop-chrome));

	/* Natural size wins whenever it is smaller than the available space. */
	max-width: min(var(--ipop-natural-w, 100%), calc(100vw - (var(--ipop-gutter) * 2)));
	max-height: min(var(--ipop-natural-h, 100%), calc(100vh - (var(--ipop-gutter) * 2) - var(--ipop-chrome)));
}

/* Opt in: let large screens scale the image up past its natural size.
   max-width alone can only shrink, and an explicit width plus max-height
   clamps the height without recomputing the width — which distorts. So the
   contain-fit width is derived from the aspect ratio directly, and height
   stays auto so the browser keeps the ratio exact. */
.ipop--upscale .ipop__image {
	width: min(
		calc(100vw - (var(--ipop-gutter) * 2)),
		calc((100vh - (var(--ipop-gutter) * 2) - var(--ipop-chrome)) * var(--ipop-aspect, 1))
	);
	height: auto;
	max-width: none;
	max-height: none;
}

.ipop--upscale .ipop__caption {
	max-width: calc(100vw - (var(--ipop-gutter) * 2));
}

/* Mobile browser chrome: dvh tracks the visible viewport, vh does not. */
@supports (height: 100dvh) {
	.ipop__image {
		max-height: min(var(--ipop-natural-h, 100%), calc(100dvh - (var(--ipop-gutter) * 2) - var(--ipop-chrome)));
	}

	.ipop--upscale .ipop__image {
		width: min(
			calc(100vw - (var(--ipop-gutter) * 2)),
			calc((100dvh - (var(--ipop-gutter) * 2) - var(--ipop-chrome)) * var(--ipop-aspect, 1))
		);
	}
}

/* Hold the layout at the correct ratio while the file is still downloading. */
.ipop.is-loading .ipop__image {
	aspect-ratio: var(--ipop-ratio, auto);
	min-width: 120px;
	min-height: 80px;
}

.ipop__link {
	display: block;
	line-height: 0;
	text-decoration: none;
}

.ipop__link:focus-visible {
	outline: 3px solid #fff;
	outline-offset: 3px;
}

/* ----------------------------------------------------------------- caption */

.ipop__caption {
	margin: 0.75rem auto 0;
	max-width: min(var(--ipop-natural-w, 100%), calc(100vw - (var(--ipop-gutter) * 2)));
	color: #f2f4f7;
	font-size: 0.875rem;
	line-height: 1.45;
	text-align: center;
	text-shadow: 0 1px 2px rgba(0, 0, 0, 0.6);
}

/* ------------------------------------------------------------------- close */

.ipop__close {
	position: absolute;
	top: 0;
	right: 0;
	z-index: 2;
	transform: translate(40%, -40%);
	display: flex;
	align-items: center;
	justify-content: center;
	width: 2.5rem;
	height: 2.5rem;
	padding: 0;
	border: 0;
	border-radius: 50%;
	background: #fff;
	color: #14161a;
	box-shadow: 0 2px 12px rgba(0, 0, 0, 0.45);
	cursor: pointer;
	transition: background var(--ipop-speed) ease, transform var(--ipop-speed) ease;
}

.ipop__close:hover,
.ipop__close:focus-visible {
	background: #e9edf2;
	transform: translate(40%, -40%) scale(1.06);
}

/* The button is white, so the ring has to be dark to read at all. */
.ipop__close:focus-visible {
	outline: 3px solid #14161a;
	outline-offset: 2px;
}

.ipop__close svg {
	width: 1.15rem;
	height: 1.15rem;
	stroke: currentColor;
	stroke-width: 2.25;
	stroke-linecap: round;
	fill: none;
}

/* ------------------------------------------------------------------ spinner */

.ipop.is-loading .ipop__dialog::after {
	content: "";
	position: absolute;
	top: 50%;
	left: 50%;
	width: 2rem;
	height: 2rem;
	margin: -1rem 0 0 -1rem;
	border: 2px solid rgba(255, 255, 255, 0.25);
	border-top-color: #fff;
	border-radius: 50%;
	animation: ipop-spin 700ms linear infinite;
}

@keyframes ipop-spin {
	to {
		transform: rotate(360deg);
	}
}

/* ------------------------------------------------------------------ mobile */

@media (max-width: 782px) {
	.ipop {
		--ipop-gutter: 1rem;
	}

	.ipop__close {
		width: 2.25rem;
		height: 2.25rem;
		transform: translate(25%, -25%);
	}

	.ipop__close:hover,
	.ipop__close:focus-visible {
		transform: translate(25%, -25%) scale(1.06);
	}
}

/* Body lock applied while a popup is open. */
.ipop-lock {
	overflow: hidden !important;
}

@media (prefers-reduced-motion: reduce) {
	.ipop__backdrop,
	.ipop__dialog,
	.ipop__close {
		transition: none;
	}

	.ipop__dialog {
		transform: none;
	}

	.ipop.is-loading .ipop__dialog::after {
		animation-duration: 2s;
	}
}
