/**
 * WPS Gallery 1.0 — the product gallery, on its own markup.
 *
 * No FlexSlider, no clones: a CSS grid lays the gallery out and scroll-snap
 * moves it.
 *
 * Mobile first: the base rules are the phone, where every layout looks the
 * same — the big image, and under it the thumbnails or the dots. The layouts
 * themselves live in `@media (min-width: 769px)`.
 *
 * The geometry rule: the thumbnail strip is exactly as long as the main image
 * (as wide below it, as tall beside it). The width of the main image and the
 * visible count are settings; the thumbnail follows. The sums are done with
 * container units against `.wpsgal`, so a narrower column shrinks everything
 * in proportion. Support/Geometry.php does the same sums for the full width.
 *
 * Variables. Page-wide, from the settings (Channel::inline_vars): --wpsgal-n,
 * --wpsgal-n-mobile, --wpsgal-space (between images), --wpsgal-gap (strip to
 * image), --wpsgal-min-thumb. Per gallery (Renderer): --wpsgal-r (box shape,
 * width / height), --wpsgal-total (the whole gallery's width).
 * Look: WPS Design tokens with a fallback, so the plugin works on its own.
 */

/* ------------------------------------------------------------------
   1. Frame
   ------------------------------------------------------------------ */

.wpsgal {
	container-type: inline-size;
	position: relative;
	inline-size: 100%;
	max-inline-size: var(--wpsgal-total, none);
	margin: 0;
}

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

.wpsgal__frame {
	display: grid;
	grid-template-columns: minmax(0, 1fr);
	row-gap: var(--wpsgal-gap, 1px);
}

/* ------------------------------------------------------------------
   2. The stage: track, slides, arrows, badges
   ------------------------------------------------------------------ */

.wpsgal__stage {
	position: relative;
	min-inline-size: 0;
}

.wpsgal__track {
	display: flex;
	overflow-x: auto;
	overflow-y: hidden;
	scroll-snap-type: x mandatory;
	overscroll-behavior-x: contain;
	scrollbar-width: none;
}

.wpsgal__track::-webkit-scrollbar {
	display: none;
}

.wpsgal__track:focus-visible {
	outline: var(--wps-focus-width, 2px) solid var(--wps-focus, currentColor);
	outline-offset: calc(var(--wps-focus-width, 2px) * -1);
}

/* The box has the chosen shape; the photo inside is fitted whole and centred
   both ways — a square photo in a 2:3 box stays square with air above and
   below. The shape reserves the space before the image
   loads: nothing jumps. */
.wpsgal__slide {
	display: grid;
	place-items: center;
	flex: 0 0 100%;
	aspect-ratio: var(--wpsgal-r, 1);
	overflow: hidden;
	scroll-snap-align: start;
	scroll-snap-stop: always;
	background: var(--wpsgal-slide-bg, transparent);
}

.wpsgal__slide > a {
	display: grid;
	place-items: center;
	inline-size: 100%;
	block-size: 100%;
	cursor: zoom-in;
}

.wpsgal[data-wpsgal-lightbox="0"] .wpsgal__slide > a {
	cursor: default;
}

/* Two classes: WooCommerce and the theme size product images with
   `.woocommerce img { height: auto }`. */
.wpsgal .wpsgal__slide img {
	display: block;
	inline-size: 100%;
	block-size: 100%;
	max-inline-size: 100%;
	object-fit: contain;
	object-position: center;
}

.wpsgal__slide--video {
	background: var(--wpsgal-video-bg, #000);
}

.wpsgal .wpsgal__player {
	display: block;
	inline-size: 100%;
	block-size: 100%;
	border: 0;
	object-fit: contain;
}

/* The badge stack: top corner of the main image, one under the other. The
   gallery gives it its place; the badges keep their own look. */
.wpsgal__badges {
	position: absolute;
	z-index: 3;
	inset-block-start: var(--wps-badge-inset, 10px);
	inset-inline-start: var(--wps-badge-inset, 10px);
	display: grid;
	justify-items: start;
	gap: var(--wps-badge-gap, 1px);
	pointer-events: none;
}

/* WooCommerce and the theme pin the sale flash to the corner of the gallery
   block; inside the stack it is an ordinary item. The `!important` answers
   the theme's own: `body .woocommerce span.onsale { position: absolute
   !important; inset: … !important }` (wps-theme theme.css:376). Left absolute
   inside a stack with no width of its own, the badge shrinks to one letter per
   line. */
.wpsgal .wpsgal__badges .onsale.onsale {
	position: static !important;
	inset: auto !important;
	margin: 0 !important;
	white-space: nowrap;
	pointer-events: auto;
}

/* ------------------------------------------------------------------
   3. Arrows — black on a white ground
   ------------------------------------------------------------------ */

/* The arrows sit ON the photos, so they are never red and never bare: a black
   glyph on a white ground whose transparency is a setting (Channel prints
   --wpsgal-arrow-alpha). WPS Design's arrow STYLE (bare / circle / outline in
   the accent) is for arrows on the page and is deliberately not followed
   here; its icon and its size still are. Hover makes the ground solid. */
.wpsgal__arrow,
.wpsgal__thumb-arrow {
	position: absolute;
	z-index: 2;
	display: grid;
	place-items: center;
	min-inline-size: var(--wps-control-height, 44px);
	min-block-size: var(--wps-control-height, 44px);
	padding: 0;
	border: 0;
	border-radius: var(--wps-radius-button, var(--wps-radius, 0));
	background: rgb(255 255 255 / var(--wpsgal-arrow-alpha, 0.8));
	color: var(--wpsgal-arrow-ink, #000);
	cursor: pointer;
	transition: background-color var(--wps-duration, 0.15s) var(--wps-ease, ease);
}

.wpsgal__arrow:hover,
.wpsgal__thumb-arrow:hover {
	background: rgb(255 255 255 / 1);
}

.wpsgal__arrow:focus-visible,
.wpsgal__thumb-arrow:focus-visible,
.wpsgal__thumb:focus-visible,
.wpsgal__dot:focus-visible {
	outline: var(--wps-focus-width, 2px) solid var(--wps-focus, currentColor);
	outline-offset: var(--wps-focus-offset, 2px);
}

.wpsgal__arrow:disabled,
.wpsgal__thumb-arrow:disabled {
	opacity: var(--wps-disabled-opacity, 0.35);
	cursor: default;
}

/* The glyph is a mask, so it takes `currentColor` and the icon chosen in WPS
   Design; the fallback is the same chevron. */
.wpsgal__glyph {
	display: block;
	inline-size: var(--wps-arrow-size, 18px);
	block-size: var(--wps-arrow-size, 18px);
	background: currentColor;
	-webkit-mask: var(--wps-arrow-left, var(--wpsgal-chevron-left)) center / contain no-repeat;
	mask: var(--wps-arrow-left, var(--wpsgal-chevron-left)) center / contain no-repeat;
}

[data-dir="next"] > .wpsgal__glyph {
	-webkit-mask-image: var(--wps-arrow-right, var(--wpsgal-chevron-right));
	mask-image: var(--wps-arrow-right, var(--wpsgal-chevron-right));
}

.wpsgal {
	--wpsgal-chevron-left: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 320 512'%3E%3Cpath d='M9.4 233.4c-12.5 12.5-12.5 32.8 0 45.3l192 192c12.5 12.5 32.8 12.5 45.3 0s12.5-32.8 0-45.3L77.3 256 246.6 86.6c12.5-12.5 12.5-32.8 0-45.3s-32.8-12.5-45.3 0l-192 192z'/%3E%3C/svg%3E");
	--wpsgal-chevron-right: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 320 512'%3E%3Cpath d='M310.6 233.4c12.5 12.5 12.5 32.8 0 45.3l-192 192c-12.5 12.5-32.8 12.5-45.3 0s-12.5-32.8 0-45.3L242.7 256 73.4 86.6c-12.5-12.5-12.5-32.8 0-45.3s32.8-12.5 45.3 0l192 192z'/%3E%3C/svg%3E");
}

/* Over the big image: centred on its sides. */
.wpsgal__arrow {
	inset-block-start: 50%;
	translate: 0 -50%;
}

.wpsgal__arrow--prev {
	inset-inline-start: var(--wpsgal-arrow-inset, 8px);
}

.wpsgal__arrow--next {
	inset-inline-end: var(--wpsgal-arrow-inset, 8px);
}

/* ------------------------------------------------------------------
   4. Thumbnail strip
   ------------------------------------------------------------------ */

.wpsgal__nav {
	position: relative;
	min-inline-size: 0;
	min-block-size: 0;
}

.wpsgal__thumbs {
	position: relative; /* the thumbnails' offsets are measured against it */
	display: flex;
	gap: var(--wpsgal-space, 1px);
	overflow-x: auto;
	overflow-y: hidden;
	scroll-snap-type: x mandatory;
	overscroll-behavior-x: contain;
	scrollbar-width: none;
}

.wpsgal__thumbs::-webkit-scrollbar {
	display: none;
}

/* Phone: the visible count of the phone, never below a size a finger can hit
   — below it fewer thumbnails show and the strip scrolls. */
.wpsgal__thumb {
	position: relative;
	display: grid;
	place-items: center;
	flex: 0 0 max(
		var(--wpsgal-min-thumb, 48px),
		calc((100cqi - (var(--wpsgal-n-mobile, 5) - 1) * var(--wpsgal-space, 1px)) / var(--wpsgal-n-mobile, 5))
	);
	aspect-ratio: var(--wpsgal-r, 1);
	padding: 0;
	border: 0;
	border-radius: 0;
	background: var(--wpsgal-thumb-bg, transparent);
	cursor: pointer;
	scroll-snap-align: start;
}

/* Only the picture dims, not the button: a mark on top (the video's play
   sign) stays at full strength on an inactive thumbnail. */
.wpsgal .wpsgal__thumb img {
	display: block;
	inline-size: 100%;
	block-size: 100%;
	max-inline-size: 100%;
	object-fit: contain;
	object-position: center;
	opacity: var(--wpsgal-thumb-dim, 0.5);
	transition: opacity var(--wps-duration, 0.15s) var(--wps-ease, ease);
}

.wpsgal .wpsgal__thumb:hover img,
.wpsgal .wpsgal__thumb[aria-current="true"] img {
	opacity: 1;
}

/* The video's still is YouTube's 4:3 frame with black bars of its own —
   `contain` would show the bars, so this one thumbnail fills its box. The
   play mark says it is a video. */
.wpsgal__thumb--video {
	background: var(--wpsgal-video-bg, #000);
}

.wpsgal .wpsgal__thumb--video img {
	object-fit: cover;
}

.wpsgal__thumb--video::before,
.wpsgal__thumb--video::after {
	content: "";
	position: absolute;
	/* Only the start edges are set, so the height follows aspect-ratio
	   instead of being stretched between two insets. */
	inset-block-start: 50%;
	inset-inline-start: 50%;
	z-index: 1; /* over the dimmed picture: its opacity makes it a layer of its own, painted after ::before */
	translate: -50% -50%;
	pointer-events: none;
}

/* The play sign is YouTube's own button — the red rounded shape with the
   white triangle, the mark every shopper already reads as "video". A bare white
   triangle vanishes on a light frame. Two masks on one box, so
   both colours stay tokens: the shape and the triangle are drawn in the same
   68 × 48 frame and cannot drift apart. */
.wpsgal__thumb--video::before,
.wpsgal__thumb--video::after {
	inline-size: var(--wpsgal-video-mark, clamp(28px, 42%, 60px));
	aspect-ratio: 68 / 48;
	-webkit-mask-position: center;
	mask-position: center;
	-webkit-mask-size: contain;
	mask-size: contain;
	-webkit-mask-repeat: no-repeat;
	mask-repeat: no-repeat;
}

.wpsgal__thumb--video::before {
	background: var(--wpsgal-play-bg, #f00);
	-webkit-mask-image: var(--wpsgal-play-shape);
	mask-image: var(--wpsgal-play-shape);
}

.wpsgal__thumb--video::after {
	background: var(--wpsgal-play-ink, #fff);
	-webkit-mask-image: var(--wpsgal-play-triangle);
	mask-image: var(--wpsgal-play-triangle);
}

.wpsgal {
	--wpsgal-play-shape: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 68 48'%3E%3Cpath d='M66.52 7.74c-.78-2.93-2.49-5.41-5.42-6.19C55.79.13 34 0 34 0S12.21.13 6.9 1.55C3.97 2.33 2.27 4.81 1.48 7.74.06 13.05 0 24 0 24s.06 10.95 1.48 16.26c.78 2.93 2.49 5.41 5.42 6.19C12.21 47.87 34 48 34 48s21.79-.13 27.1-1.55c2.93-.78 4.64-3.26 5.42-6.19C67.94 34.95 68 24 68 24s-.06-10.95-1.48-16.26z'/%3E%3C/svg%3E");
	--wpsgal-play-triangle: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 68 48'%3E%3Cpath d='M45 24 27 14v20z'/%3E%3C/svg%3E");
}

/* The other mark (setting "Mark on the thumbnail"): a plain white play sign
   on the picture, darkened just enough for the sign to read on any frame.
   The shade covers the whole thumbnail; the sign sits in the middle. */
.wpsgal[data-wpsgal-play-icon="plain"] .wpsgal__thumb--video::before {
	inset: 0;
	translate: none;
	inline-size: auto;
	aspect-ratio: auto;
	background: var(--wpsgal-video-shade, rgb(0 0 0 / 0.35));
	-webkit-mask: none;
	mask: none;
}

.wpsgal[data-wpsgal-play-icon="plain"] .wpsgal__thumb--video::after {
	inline-size: var(--wpsgal-video-mark-plain, clamp(16px, 28%, 40px));
	aspect-ratio: 1;
	translate: -42% -50%;
	background: var(--wpsgal-play-ink, #fff);
	-webkit-mask-image: var(--wpsgal-play-plain);
	mask-image: var(--wpsgal-play-plain);
}

.wpsgal {
	--wpsgal-play-plain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M3 1.5v13l11-6.5z'/%3E%3C/svg%3E");
}

/* The strip's arrows sit over its ends, so the strip itself stays exactly as
   long as the image. They appear only when there is more than fits — the
   script marks that with data-overflow. */
.wpsgal__nav:not([data-overflow]) > .wpsgal__thumb-arrow {
	display: none;
}

.wpsgal__thumb-arrow {
	inset-block-start: 50%;
	translate: 0 -50%;
}

/* The strip's arrows are smaller than the big image's — a strip of 100 px
   thumbnails does not carry a 44 px button. */
.wpsgal .wpsgal__thumb-arrow {
	min-inline-size: var(--wpsgal-thumb-arrow, 28px);
	min-block-size: var(--wpsgal-thumb-arrow, 28px);
}

.wpsgal__thumb-arrow .wpsgal__glyph {
	inline-size: var(--wpsgal-thumb-arrow-glyph, 12px);
	block-size: var(--wpsgal-thumb-arrow-glyph, 12px);
}

.wpsgal__thumb-arrow--prev {
	inset-inline-start: 0;
}

.wpsgal__thumb-arrow--next {
	inset-inline-end: 0;
}

/* ------------------------------------------------------------------
   5. Dots (phone, when chosen)
   ------------------------------------------------------------------ */

.wpsgal__dots {
	display: none;
	flex-wrap: wrap;
	justify-content: center;
}

/* The dot is small, the target is not: the padding is the finger's area and
   the paint stays in the content box. */
.wpsgal__dot {
	inline-size: calc(var(--wps-dot-size, 8px) + 2 * var(--wpsgal-dot-pad, 8px));
	block-size: calc(var(--wps-dot-size, 8px) + 2 * var(--wpsgal-dot-pad, 8px));
	padding: var(--wpsgal-dot-pad, 8px);
	border: 0;
	border-radius: var(--wps-radius-full, 999px);
	background: var(--wps-border, #d4d4d4) content-box;
	cursor: pointer;
}

.wpsgal__dot[aria-current="true"] {
	background: var(--wps-accent, currentColor) content-box;
}

@media (max-width: 768px) {
	/* Both gaps are halved on a phone, whole pixels, never below 1 — the
	   halves are worked out in PHP (Options::mobile_gap). */
	.wpsgal {
		--wpsgal-space: var(--wpsgal-space-mobile, 1px);
		--wpsgal-gap: var(--wpsgal-gap-mobile, 1px);
	}

	/* The strip moves with the finger on a phone; its arrows are absent. The
	   big image's arrows only when the setting asks for them. */
	.wpsgal .wpsgal__thumb-arrow,
	.wpsgal[data-wpsgal-arrows-mobile="0"] .wpsgal__arrow {
		display: none;
	}

	.wpsgal[data-wpsgal-nav="bullets"] .wpsgal__nav {
		display: none;
	}

	.wpsgal[data-wpsgal-nav="bullets"] .wpsgal__dots {
		display: flex;
	}

	/* The dots carry their own air; the frame's row gap would double it. */
	.wpsgal[data-wpsgal-nav="bullets"] .wpsgal__frame {
		row-gap: 0;
	}
}

/* ------------------------------------------------------------------
   6. The layouts, from a tablet up
   ------------------------------------------------------------------ */

@media (min-width: 769px) {

	/* Horizontal: the strip under the image, exactly as wide. */
	.wpsgal[data-wpsgal-layout="horizontal"] .wpsgal__thumb {
		flex-basis: calc((100cqi - (var(--wpsgal-n, 6) - 1) * var(--wpsgal-space, 1px)) / var(--wpsgal-n, 6));
	}

	/* Fewer thumbnails than fit: centred when asked. `safe` falls back to
	   the start the moment the strip overflows, so no thumbnail is pushed
	   out of reach on the left. */
	.wpsgal[data-wpsgal-layout="horizontal"][data-wpsgal-align="center"] .wpsgal__thumbs {
		justify-content: safe center;
	}

	/* Vertical: the strip left of the image, exactly as tall. With the
	   gallery C wide, n visible, gap g between thumbnails, gap t to the image
	   and shape r (width / height), the image m and the strip s satisfy
	   s = (m / r - (n - 1) g) / n × r and C = s + t + m, hence
	   m = (n (C - t) + (n - 1) g r) / (n + 1). */
	.wpsgal[data-wpsgal-layout="vertical"] .wpsgal__frame {
		--wpsgal-m: calc(
			(var(--wpsgal-n, 6) * (100cqi - var(--wpsgal-gap, 1px)) + (var(--wpsgal-n, 6) - 1) * var(--wpsgal-space, 1px) * var(--wpsgal-r, 1))
			/ (var(--wpsgal-n, 6) + 1)
		);
		grid-template-columns: calc(100cqi - var(--wpsgal-gap, 1px) - var(--wpsgal-m)) var(--wpsgal-m);
		grid-template-rows: calc(var(--wpsgal-m) / var(--wpsgal-r, 1));
		column-gap: var(--wpsgal-gap, 1px);
		row-gap: 0;
	}

	.wpsgal[data-wpsgal-layout="vertical"] .wpsgal__stage {
		grid-column: 2;
		grid-row: 1;
	}

	.wpsgal[data-wpsgal-layout="vertical"] .wpsgal__nav {
		grid-column: 1;
		grid-row: 1;
	}

	.wpsgal[data-wpsgal-layout="vertical"] .wpsgal__thumbs {
		flex-direction: column;
		block-size: 100%;
		overflow-x: hidden;
		overflow-y: auto;
		scroll-snap-type: y mandatory;
		overscroll-behavior-y: contain;
	}

	.wpsgal[data-wpsgal-layout="vertical"] .wpsgal__thumb {
		flex: 0 0 auto;
		inline-size: 100%;
	}

	.wpsgal[data-wpsgal-layout="vertical"] .wpsgal__thumb-arrow {
		inset-block-start: auto;
		inset-inline-start: 50%;
		translate: -50% 0;
	}

	.wpsgal[data-wpsgal-layout="vertical"] .wpsgal__thumb-arrow--prev {
		inset-block-start: 0;
	}

	.wpsgal[data-wpsgal-layout="vertical"] .wpsgal__thumb-arrow--next {
		inset-block-end: 0;
		inset-inline-end: auto;
	}

	.wpsgal[data-wpsgal-layout="vertical"] .wpsgal__thumb-arrow .wpsgal__glyph {
		rotate: 90deg;
	}

	/* Column and grid: every image at once, starting from the first. There is
	   nothing to move, so the navigation is absent, not hidden in place. */
	.wpsgal:is([data-wpsgal-layout="column"], [data-wpsgal-layout="grid2"]) .wpsgal__track {
		display: grid;
		grid-template-columns: minmax(0, 1fr);
		gap: var(--wpsgal-space, 1px);
		overflow: visible;
		scroll-snap-type: none;
	}

	.wpsgal[data-wpsgal-layout="grid2"] .wpsgal__track {
		grid-template-columns: repeat(2, minmax(0, 1fr));
	}

	.wpsgal:is([data-wpsgal-layout="column"], [data-wpsgal-layout="grid2"]) :is(.wpsgal__arrow, .wpsgal__nav, .wpsgal__dots) {
		display: none;
	}

	.wpsgal:is([data-wpsgal-layout="column"], [data-wpsgal-layout="grid2"]) .wpsgal__frame {
		row-gap: 0;
	}
}

/* ------------------------------------------------------------------
   7. The lightbox's close button — WPS Design's close button
   ------------------------------------------------------------------ */

/* PhotoSwipe draws its × from a white sprite, which cannot take a colour.
   The sprite gives way to a masked glyph in `currentColor`, on the close
   button's own fill, size and corners (Components › Close button). Without
   WPS Design the fallbacks are PhotoSwipe's own look: 44 px, white, no fill. */
:root[data-wpsgal] .pswp__button--close {
	display: grid;
	place-items: center;
	inline-size: var(--wps-close-size, 44px);
	block-size: var(--wps-close-size, 44px);
	border-radius: var(--wps-close-radius, 0);
	background: var(--wps-close-bg, transparent);
	color: var(--wps-close-text, #fff);
	opacity: 1;
}

:root[data-wpsgal] .pswp__button--close::after {
	content: "";
	inline-size: 45%;
	block-size: 45%;
	background: currentColor;
	-webkit-mask: var(--wpsgal-close) center / contain no-repeat;
	mask: var(--wpsgal-close) center / contain no-repeat;
}

:root[data-wpsgal] {
	--wpsgal-close: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Cpath d='M2 2l12 12M14 2 2 14' stroke='%23000' stroke-width='2' stroke-linecap='round'/%3E%3C/svg%3E");
}
