/**
 * Masonry Photo Gallery and its lightbox (photo-gallery block, masonry layout,
 * template-parts/gallery/masonry.php, assets/js/gallery-lightbox.js). Loads
 * after gallery.css, which supplies the filter chips.
 *
 * Photos keep their natural shape in CSS columns: 1 on narrow phones, 2 from
 * 480px, 3 from 900px, 4 from 1200px.
 */

.gallery__grid.masonry {
	display: block;
	columns: 1;
	column-gap: var(--sp-3);
}

.masonry__item {
	break-inside: avoid;
	margin-bottom: var(--sp-3);
}

.masonry__link {
	position: relative;
	display: block;
	overflow: hidden;
	background: var(--surface-alt);
	cursor: zoom-in;
	text-decoration: none;
}

.masonry__link:focus-visible {
	outline: 3px solid var(--brand-red);
	outline-offset: 3px;
}

.masonry__img {
	display: block;
	width: 100%;
	height: auto;
	transition: transform 0.5s cubic-bezier(0.2, 0.7, 0.2, 1);
}

.masonry__name {
	position: absolute;
	inset: auto 0 0;
	padding: var(--sp-8) var(--sp-4) var(--sp-3);
	background: linear-gradient(to top, rgb(17 17 17 / 0.65), rgb(17 17 17 / 0));
	color: var(--ink-invert);
	font-family: var(--font-serif);
	font-size: var(--fs-base);
	line-height: var(--lh-tight);
	opacity: 0;
	transition: opacity 0.25s;
}

.masonry__link:hover .masonry__img,
.masonry__link:focus-visible .masonry__img {
	transform: scale(1.03);
}

.masonry__link:hover .masonry__name,
.masonry__link:focus-visible .masonry__name {
	opacity: 1;
}

/* No hover on touch screens: keep the names visible. */
@media (hover: none) {
	.masonry__name {
		opacity: 1;
	}
}

@media (min-width: 480px) {
	.gallery__grid.masonry {
		columns: 2;
	}
}

@media (min-width: 900px) {
	.gallery__grid.masonry {
		columns: 3;
	}
}

@media (min-width: 1200px) {
	.gallery__grid.masonry {
		columns: 4;
	}
}

/* --- Lightbox --- */

:root:has(.lightbox[open]) {
	overflow: hidden;
}

.lightbox {
	width: 100vw;
	max-width: none;
	height: 100dvh;
	max-height: none;
	margin: 0;
	padding: 0;
	border: 0;
	background: rgb(12 14 24 / 0.95);
	color: var(--ink-invert);
}

.lightbox[open] {
	display: grid;
	place-items: center;
}

.lightbox::backdrop {
	background: rgb(12 14 24 / 0.95);
}

.lightbox__figure {
	--lightbox-max-w: calc(100vw - 144px);
	--lightbox-max-h: calc(100dvh - 140px);

	display: flex;
	flex-direction: column;
	align-items: center;
	gap: var(--sp-3);
	max-width: var(--lightbox-max-w);
	margin: 0;
}

/*
 * The photo's box, sized from its width and height (--w and --h, set by
 * assets/js/gallery-lightbox.js) before a byte of it arrives, so the
 * caption never moves when it loads. The faint ground marks the space.
 */
.lightbox__frame {
	width: min(var(--lightbox-max-w), var(--lightbox-max-h) * var(--w, 3) / var(--h, 2));
	height: min(var(--lightbox-max-h), var(--lightbox-max-w) * var(--h, 2) / var(--w, 3));
	background: rgb(255 255 255 / 0.04);
}

.lightbox__img {
	display: block;
	width: 100%;
	height: 100%;
	object-fit: contain;
}

/* Hidden until the photo is decoded (assets/js/gallery-lightbox.js). */
.lightbox__img.is-loading {
	opacity: 0;
}

.lightbox__caption {
	display: flex;
	flex-wrap: wrap;
	align-items: baseline;
	justify-content: center;
	gap: var(--sp-1) var(--sp-4);
	padding-inline: var(--sp-4);
	text-align: center;
}

.lightbox__name {
	font-family: var(--font-serif);
	font-size: var(--fs-lg);
}

.lightbox__line,
.lightbox__link,
.lightbox__count {
	font-size: var(--fs-xs);
	letter-spacing: var(--tracking-wider);
	text-transform: uppercase;
}

.lightbox__line,
.lightbox__count {
	color: rgb(255 255 255 / 0.7);
}

.lightbox__count {
	font-variant-numeric: tabular-nums;
}

.lightbox__link {
	color: inherit;
	font-weight: 600;
	text-decoration: underline;
	text-underline-offset: 3px;
}

.lightbox__close,
.lightbox__prev,
.lightbox__next {
	position: absolute;
	display: grid;
	place-items: center;
	width: 48px;
	height: 48px;
	padding: 0;
	border: 0;
	border-radius: 50%;
	background: rgb(255 255 255 / 0.1);
	color: inherit;
	font-size: 28px;
	line-height: 1;
	cursor: pointer;
	transition: background-color 0.2s;
}

.lightbox__close:hover,
.lightbox__prev:hover,
.lightbox__next:hover {
	background: rgb(255 255 255 / 0.22);
}

.lightbox__close:focus-visible,
.lightbox__prev:focus-visible,
.lightbox__next:focus-visible,
.lightbox__link:focus-visible {
	outline: 2px solid var(--ink-invert);
	outline-offset: 2px;
}

.lightbox__close {
	top: var(--sp-4);
	right: var(--sp-4);
}

.lightbox__prev,
.lightbox__next {
	top: 50%;
	transform: translateY(-50%);
}

.lightbox__prev {
	left: var(--sp-4);
}

.lightbox__next {
	right: var(--sp-4);
}

.lightbox__prev[hidden],
.lightbox__next[hidden] {
	display: none;
}

/* Phones: the photo takes the full width; the arrows sit low, over the dark band. */
@media (max-width: 639px) {
	.lightbox__figure {
		--lightbox-max-w: 100vw;
	}

	.lightbox__prev,
	.lightbox__next {
		top: auto;
		bottom: var(--sp-4);
		transform: none;
	}
}

@media (prefers-reduced-motion: no-preference) {
	.lightbox[open] {
		animation: lightbox-in 0.25s ease-out;
	}

	/* The photo and caption settle in as the dark surround fades up. */
	.lightbox[open] .lightbox__figure {
		animation: lightbox-rise 0.4s cubic-bezier(0.16, 1, 0.3, 1);
	}

	/* Stepping to a photo of another shape, the box eases to its new size. */
	.lightbox__frame {
		transition: width 0.3s cubic-bezier(0.16, 1, 0.3, 1), height 0.3s cubic-bezier(0.16, 1, 0.3, 1);
	}

	.lightbox__img {
		transition: opacity 0.3s ease-out;
	}
}

@keyframes lightbox-in {
	from {
		opacity: 0;
	}
}

@keyframes lightbox-rise {
	from {
		opacity: 0;
		transform: scale(0.97);
	}
}

@media (prefers-reduced-motion: reduce) {
	.masonry__img,
	.masonry__name {
		transition: none;
	}

	.masonry__link:hover .masonry__img,
	.masonry__link:focus-visible .masonry__img {
		transform: none;
	}
}
