/* ===========================================================
   Product gallery (Swiper)

   Presentation is driven by custom properties set inline on the root by the
   shortcode, so this file stays static and cacheable:

     --pg-ratio     aspect-ratio of a main slide ('auto' = image's own)
     --pg-ratio-sm  optional override below 550px
     --pg-fit       object-fit for the main image (cover | contain)
     --pg-radius    corner radius
     --pg-gap       gap between slides (Swiper handles the actual spacing)
     --pg-thumb     thumbnail edge length

   Every var has a fallback, so a stale cached copy still renders.
   =========================================================== */

/* ---------- Root ---------- */

.kw-product-gallery {
	width: 100%;
}

.kw-swiper-gallery .swiper {
	width: 100%;
}

/* The main image and its overlaid controls share a positioning context. */
.kw-gallery-main {
	position: relative;
	min-width: 0;
}

/* ---------- Main slider ---------- */

.kw-swiper-gallery .kw-main-swiper .swiper-slide {
	aspect-ratio: var(--pg-ratio, 1 / 1);
	width: 100%;
	overflow: hidden;
	border-radius: var(--pg-radius, 12px);
	background: var(--kw-surface-alt, #f1f4f6);
}

.kw-swiper-gallery .kw-main-img {
	width: 100%;
	height: 100%;
	object-fit: var(--pg-fit, cover);
	display: block;
}

/* `contain` on a tinted slide reads as a deliberate frame, not a mistake. */
.kw-swiper-gallery .kw-main-swiper .kw-pswp-item {
	display: block;
	width: 100%;
	height: 100%;
}

@media (max-width: 549px) {
	.kw-swiper-gallery .kw-main-swiper .swiper-slide {
		aspect-ratio: var(--pg-ratio-sm, var(--pg-ratio, 1 / 1));
	}
}

/* ---------- Thumbnails ---------- */

.kw-swiper-gallery .kw-thumbs-swiper {
	margin-top: 12px;
}

.kw-swiper-gallery .kw-thumbs-swiper .swiper-slide {
	cursor: pointer;
	width: var(--pg-thumb, 80px);
	height: var(--pg-thumb, 80px);
	aspect-ratio: 1 / 1;
	border-radius: var(--pg-radius, 12px);
	position: relative;
	border: 2px solid transparent;
	box-shadow: inset 0 0 0 1px var(--kw-line, #dce2e5);
	opacity: 0.55;
	overflow: hidden;
	transition: opacity 150ms ease-out, border-color 150ms ease-out;
}

.kw-swiper-gallery .kw-thumbs-swiper .swiper-slide:hover {
	opacity: 0.85;
}

/* Active thumb: accent border AND full opacity — color is never the only
   carrier of state. */
.kw-swiper-gallery .kw-thumbs-swiper .swiper-slide.swiper-slide-thumb-active {
	opacity: 1;
	border-color: var(--kw-accent, #34aeeb);
}

.kw-swiper-gallery .kw-thumb-img {
	width: 100%;
	height: 100%;
	object-fit: cover;
	display: block;
}

/*
 * Count mode ("thumbnails per view").
 *
 * Swiper only writes an inline width when `slidesPerView` is a number. When it
 * resolves to 'auto' — which is what a tier with no count means — Swiper reads
 * the width from CSS instead, so the slide has to carry one. It previously
 * carried `width: auto`, so in that case the slide sized to its own 280px image
 * and the strip rendered ~19,000px wide: before the script ran at every width,
 * and permanently at any width whose tier had no count.
 *
 * --pg-thumbs-w is that width, worked out per tier in PHP (see the note there).
 * Where Swiper does supply an inline width, its own value simply wins.
 */
.kw-product-gallery--thumbs-fit .kw-thumbs-swiper .swiper-slide {
	width: var(--pg-thumbs-w-sm, var(--pg-thumb, 80px));
	height: auto;
}

@media (min-width: 550px) {
	.kw-product-gallery--thumbs-fit .kw-thumbs-swiper .swiper-slide {
		width: var(--pg-thumbs-w, var(--pg-thumb, 80px));
	}
}

/* ---------- Before Swiper initialises ---------- */
/*
 * Swiper adds `swiper-initialized` when it takes over, so everything below
 * describes the first paint only.
 *
 * The widths above hold at every moment, but the row itself does not: Swiper's
 * own stylesheet makes `.swiper-wrapper` a flex row and it is loaded from a CDN,
 * so between HTML and JS the slides can still stack. These rules lay the strip
 * out the way Swiper will, so it takes over from a layout that already matches
 * instead of one the page has to jump out of.
 */

.kw-swiper-gallery .kw-thumbs-swiper:not(.swiper-initialized) {
	overflow: hidden;
}

.kw-swiper-gallery .kw-thumbs-swiper:not(.swiper-initialized) .swiper-wrapper {
	display: flex;
	gap: var(--pg-thumbs-gap-sm, 12px);
}

.kw-swiper-gallery .kw-thumbs-swiper:not(.swiper-initialized) .swiper-slide {
	flex: 0 0 auto;
}

@media (min-width: 550px) {
	.kw-swiper-gallery .kw-thumbs-swiper:not(.swiper-initialized) .swiper-wrapper {
		gap: var(--pg-thumbs-gap, 12px);
	}
}

/* Side positions — desktop only. Below 850px thumbnails always sit under the
   image, where a horizontal strip is the only shape that fits. */
@media (min-width: 850px) {
	.kw-product-gallery--thumbs-left,
	.kw-product-gallery--thumbs-right {
		display: flex;
		align-items: flex-start;
		gap: 12px;
	}

	.kw-product-gallery--thumbs-left .kw-gallery-main,
	.kw-product-gallery--thumbs-right .kw-gallery-main {
		flex: 1 1 auto;
	}

	.kw-product-gallery--thumbs-left .kw-thumbs-swiper,
	.kw-product-gallery--thumbs-right .kw-thumbs-swiper {
		margin-top: 0;
		flex: 0 0 auto;
		width: calc(var(--pg-thumb, 80px) + 4px);
		/* A vertical Swiper needs a definite height; stretching to the flex row
		   gives it exactly the main image's height. */
		align-self: stretch;
	}

	/* The column direction comes from Swiper's own .swiper-vertical, set per
	   breakpoint in the JS — forcing flex-direction here would fight the
	   transforms it uses to slide. */
	.kw-product-gallery--thumbs-left .kw-thumbs-swiper.swiper-vertical .swiper-slide,
	.kw-product-gallery--thumbs-right .kw-thumbs-swiper.swiper-vertical .swiper-slide {
		width: 100%;
	}

	.kw-product-gallery--thumbs-left {
		flex-direction: row-reverse;
	}
}

/* ---------- Arrows ---------- */

.kw-swiper-gallery .kw-gallery-arrow {
	position: absolute;
	top: 50%;
	z-index: 10;
	width: 44px;
	height: 44px;
	/*
	 * The shared blue arrow (--kw-accent-dark). Its glyph fills its viewBox,
	 * where the old dropdown.svg filled about 60% of a 33px box — so 15px here
	 * draws the same size mark the 25px dropdown.svg did.
	 */
	background-image: url(../../assets/images/arrow.svg);
	background-repeat: no-repeat;
	background-position: center;
	background-size: 15px;
	background-color: rgba(255, 255, 255, 0.9);
	box-shadow: var(--kw-shadow-card, 0 4px 10px rgba(0, 0, 0, .04));
	border-radius: 999px !important;
	border: 0;
	cursor: pointer;
	margin: 0;
	padding: 0;
	transform: translateY(-50%) rotate(180deg);
	transition: background-color 150ms ease-out;
}

.kw-swiper-gallery .kw-gallery-arrow:hover {
	background-color: #fff;
}

.kw-swiper-gallery .kw-gallery-arrow:focus-visible {
	outline: 3px solid var(--kw-accent, #34aeeb);
	outline-offset: 2px;
}

.kw-swiper-gallery .kw-gallery-arrow--prev {
	left: 10px;
}

.kw-swiper-gallery .kw-gallery-arrow--next {
	right: 10px;
	transform: translateY(-50%) rotate(0deg);
}

/* Swiper adds this when looping is off and there is nowhere left to go. */
.kw-swiper-gallery .kw-gallery-arrow.swiper-button-disabled {
	opacity: 0.35;
	pointer-events: none;
}

/* ---------- Pagination dots ---------- */

.kw-swiper-gallery .kw-gallery-dots {
	position: absolute;
	left: 0;
	right: 0;
	bottom: 12px;
	z-index: 10;
	display: flex;
	justify-content: center;
	gap: 6px;
}

.kw-swiper-gallery .kw-gallery-dots .swiper-pagination-bullet {
	width: 8px;
	height: 8px;
	border-radius: 999px;
	background: #fff;
	opacity: 0.55;
	box-shadow: 0 1px 3px rgba(0, 0, 0, .25);
	margin: 0;
	transition: opacity 150ms ease-out, width 150ms ease-out;
}

.kw-swiper-gallery .kw-gallery-dots .swiper-pagination-bullet-active {
	opacity: 1;
	width: 20px;
	background: var(--kw-accent, #34aeeb);
}

/* ---------- Slide counter ---------- */

.kw-swiper-gallery .kw-gallery-counter {
	position: absolute;
	right: 12px;
	bottom: 12px;
	z-index: 10;
	padding: 4px 10px;
	border-radius: 999px;
	background: rgba(17, 24, 39, 0.6);
	color: #fff;
	font-size: 12px;
	font-weight: 700;
	line-height: 1.4;
	font-variant-numeric: tabular-nums;
	pointer-events: none;
}

/* ---------- Reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
	.kw-swiper-gallery .swiper-wrapper {
		transition-duration: 1ms !important;
	}
	.kw-swiper-gallery .kw-thumbs-swiper .swiper-slide,
	.kw-swiper-gallery .kw-gallery-arrow,
	.kw-swiper-gallery .kw-gallery-dots .swiper-pagination-bullet {
		transition: none;
	}
}
