/**
 * Gallery fields.
 *
 * Everything here keys off attributes carried by the gallery element itself,
 * so no per-view selector has to be generated and kept in step with the markup.
 */

/* Shared. */

.acf-view__gallery img {
	max-width: 100%;
}

/*
 * The tag carries width and height attributes, so a theme that does not reset
 * the height leaves the image distorted. These two layouts size their images
 * from the width alone.
 */
.acf-view__gallery--type--plain .acf-view__image,
.acf-view__gallery--type--masonry .acf-view__image {
	height: auto;
}

.acf-view__gallery--type--plain .acf-view__image-outer,
.acf-view__gallery--type--masonry .acf-view__image-outer {
	margin: 0;
	padding: 0;
}

/*
 * Grid of thumbnails.
 *
 * Every property the layout depends on is set here, and the selectors are
 * specific enough to win against the usual theme rules on img and figure. The
 * per-gallery settings arrive as custom properties on the element itself.
 */

.acf-view__gallery--type--grid {
	display: grid;
	grid-template-columns: repeat(var(--av-columns, 3), minmax(0, 1fr));
	gap: var(--av-gap, 16px);
	margin: 0;
	padding: 0;
	list-style: none;
}

.acf-view__gallery--type--grid figure.acf-view__image-outer {
	margin: 0;
	padding: 0;
	position: relative;
	min-width: 0;
	max-width: 100%;
}

/*
 * The tag name is part of the selector on purpose: these four properties decide
 * the layout, and a theme rule such as ".entry-content figure img" would
 * otherwise win and leave the images stacked and cropped to a sliver.
 *
 * width and height are also written as attributes on the tag, so both are
 * overridden here.
 */
.acf-view__gallery--type--grid figure.acf-view__image-outer img.acf-view__image {
	display: block;
	width: 100%;
	height: auto;
	max-width: 100%;
	aspect-ratio: var(--av-ratio, 3 / 2);
	object-fit: cover;
	object-position: center;
}

.acf-view__gallery--type--grid .acf-view__image-caption {
	margin: 6px 0 0;
	font-size: 0.85em;
	line-height: 1.4;
}

@media (max-width: 781px) {
	.acf-view__gallery--type--grid {
		grid-template-columns: repeat(var(--av-columns-mobile, 2), minmax(0, 1fr));
		gap: var(--av-gap-mobile, var(--av-gap, 16px));
	}
}

@media (max-width: 480px) {
	.acf-view__gallery--type--grid {
		grid-template-columns: minmax(0, 1fr);
	}
}

/* Justified rows. */

[data-acf-viewer-gallery="masonry"] {
	transition: opacity 0.1s ease;
}

[data-acf-viewer-gallery="masonry"]::after {
	content: "";
	display: block;
	clear: both;
}

/*
 * Hidden while the script measures the images, so they do not jump into place.
 * The delayed animation is the safety net: if the script never runs, the
 * gallery appears anyway instead of staying invisible.
 */
[data-acf-viewer-gallery="masonry"]:not([data-masonry]) {
	opacity: 0;
	animation: acf-viewer-reveal 0s linear 1s forwards;
}

@keyframes acf-viewer-reveal {
	to {
		opacity: 1;
	}
}

/* Lightbox trigger. */

[data-acf-viewer-lightbox] .acf-view__image-outer {
	position: relative;
	cursor: pointer;
	overflow: hidden;
}

/* The justified layout sizes the wrapper, so the image fills it. The grid rules
   above are more specific and keep their own aspect ratio. */
[data-acf-viewer-lightbox] .acf-view__image-outer > .acf-view__image {
	display: block;
	width: 100%;
	height: 100%;
	object-fit: cover;
}

.acf-view__gallery--type--masonry .acf-view__image {
	object-fit: cover;
}

[data-acf-viewer-lightbox] .acf-view__image-inner {
	position: absolute;
	inset: 0;
	background: rgba(0, 0, 0, 0.4);
	opacity: 0;
	transition: opacity 0.3s ease;
}

[data-acf-viewer-lightbox] .acf-view__image-outer:hover .acf-view__image-inner,
[data-acf-viewer-lightbox] .acf-view__image-outer:focus-within .acf-view__image-inner {
	opacity: 1;
}

[data-acf-viewer-lightbox] .acf-view__image-zoom-icon {
	width: 20px;
	height: 20px;
	position: absolute;
	left: 50%;
	top: 50%;
	transform: translate(-50%, -50%);
}

/* Lightbox popup. */

.acf-viewer-lightbox {
	position: fixed;
	inset: 0;
	z-index: 999999;
	display: flex;
	justify-content: center;
	align-items: center;
	background: rgba(0, 0, 0, 0.9);
	padding: 5%;
}

.acf-viewer-lightbox__image {
	max-width: 100%;
	max-height: 100%;
}

.acf-viewer-lightbox__icon {
	stroke: currentColor;
	stroke-linecap: square;
	stroke-width: 6px;
	fill: none;
	position: absolute;
	z-index: 9;
	bottom: 15px;
	left: 50%;
	transform: translateX(50%);
	color: #fff;
	opacity: 0.5;
	transition: opacity 0.3s ease;
}

.acf-viewer-lightbox__icon:hover {
	cursor: pointer;
	opacity: 0.7;
}

.acf-viewer-lightbox__icon--left {
	transform: scaleX(-1) translateX(150%);
}

.acf-viewer-lightbox__icon--inactive {
	opacity: 0.3;
	pointer-events: none;
}

.acf-viewer-lightbox__close {
	position: absolute;
	top: 15px;
	right: 20px;
	color: #fff;
	opacity: 0.6;
	font-size: 32px;
	line-height: 1;
	background: none;
	border: 0;
	cursor: pointer;
}

.acf-viewer-lightbox__close:hover {
	opacity: 1;
}
