/**
 * JezPress Woo Variation Swatches - front end.
 *
 * Selectors for the swatch button are 0,3,0+ and list :hover/:focus/:active so
 * theme and page-builder button rules (e.g. Elementor kit `button:focus`, 0,2,1)
 * cannot repaint them. Custom properties come from the settings via an inline
 * style on this handle; the values below are fallbacks.
 *
 * @package JezPress\WooVariationSwatches
 * @since   1.0.0
 */

/*
 * Swatches replace the dropdown only when the script runs (`jpwvs-js` on
 * <html>: printed in <head> on variable product pages, added by the script
 * everywhere else). Without it the dropdown stays and the inert swatches are
 * hidden, so the product can always be bought. A fragment-rendered (AJAX
 * quick view) wrap keeps its dropdown until the script activates it.
 */
.jpwvs-js .jpwvs-wrap:not(.jpwvs-pending) > select,
.jpwvs-js .jpwvs-wrap:not(.jpwvs-pending) > .select2-container {
	display: none !important;
}

html:not(.jpwvs-js) .jpwvs-swatches,
.jpwvs-wrap.jpwvs-pending > .jpwvs-swatches {
	display: none !important;
}

.jpwvs-swatches {
	--jpwvs-size: 36px;
	--jpwvs-radius: 6px;
	--jpwvs-accent: #111111;
	--jpwvs-accent-text: #ffffff;
	--jpwvs-border: rgba(0, 0, 0, 0.18);
	--jpwvs-bg: #ffffff;
	display: flex;
	flex-wrap: wrap;
	gap: 8px;
	margin: 0;
	padding: 0;
}

/*
 * Base + reset: everything a theme might inject on <button>, in every state.
 * Variants and states never compete with this rule on specificity: they set
 * the --jpwvs-* properties below on the element, and this rule reads them.
 */
.jpwvs-swatches .jpwvs-swatch.jpwvs-swatch,
.jpwvs-swatches .jpwvs-swatch.jpwvs-swatch:hover,
.jpwvs-swatches .jpwvs-swatch.jpwvs-swatch:focus,
.jpwvs-swatches .jpwvs-swatch.jpwvs-swatch:active {
	position: relative;
	display: inline-flex;
	align-items: center;
	justify-content: center;
	box-sizing: border-box;
	margin: 0;
	padding: var(--jpwvs-pad, 0);
	width: var(--jpwvs-w, auto);
	min-width: var(--jpwvs-size);
	max-width: none;
	height: var(--jpwvs-h, auto);
	min-height: var(--jpwvs-size);
	border: 1px solid var(--jpwvs-line, var(--jpwvs-border));
	border-radius: var(--jpwvs-radius);
	background: var(--jpwvs-fill, var(--jpwvs-bg));
	color: var(--jpwvs-ink, inherit);
	box-shadow: var(--jpwvs-ring, none);
	outline: none;
	font: inherit;
	font-size: 14px;
	font-weight: 500;
	line-height: 1.2;
	letter-spacing: normal;
	text-transform: none;
	text-decoration: none;
	text-shadow: none;
	white-space: nowrap;
	cursor: pointer;
	-webkit-appearance: none;
	appearance: none;
	transition: border-color 0.15s ease, box-shadow 0.15s ease, opacity 0.15s ease;
}

.jpwvs-swatches .jpwvs-swatch.jpwvs-swatch:focus-visible {
	outline: 2px solid var(--jpwvs-accent);
	outline-offset: 2px;
}

.jpwvs-swatch:hover {
	--jpwvs-line: var(--jpwvs-accent);
}

/* Labels */
.jpwvs-swatch--label {
	--jpwvs-pad: 4px 14px;
}

/* Long label values may wrap instead of overflowing narrow screens. */
.jpwvs-swatches .jpwvs-swatch--label.jpwvs-swatch,
.jpwvs-swatches .jpwvs-swatch--label.jpwvs-swatch:hover,
.jpwvs-swatches .jpwvs-swatch--label.jpwvs-swatch:focus,
.jpwvs-swatches .jpwvs-swatch--label.jpwvs-swatch:active {
	max-width: 100%;
	white-space: normal;
	text-align: center;
}

.jpwvs-swatch--label.is-selected {
	--jpwvs-line: var(--jpwvs-accent);
	--jpwvs-fill: var(--jpwvs-accent);
	--jpwvs-ink: var(--jpwvs-accent-text);
}

/* Colours + images: fixed square box with a ring when selected. */
.jpwvs-swatch--color,
.jpwvs-swatch--image {
	--jpwvs-pad: 2px;
	--jpwvs-w: var(--jpwvs-size);
	--jpwvs-h: var(--jpwvs-size);
}

.jpwvs-swatch--color.is-selected,
.jpwvs-swatch--image.is-selected {
	--jpwvs-line: var(--jpwvs-accent);
	--jpwvs-ring: 0 0 0 1px var(--jpwvs-accent);
}

.jpwvs-swatch__color,
.jpwvs-swatch .jpwvs-swatch__image {
	display: block;
	flex: 0 0 auto;
	width: 100%;
	height: 100%;
	max-width: none;
	margin: 0;
	border-radius: calc(var(--jpwvs-radius) - 2px);
	object-fit: cover;
}

.jpwvs-swatch__color {
	background: var(--jpwvs-c1, #cccccc);
	box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.08);
}

.jpwvs-swatch__color.is-dual {
	background: linear-gradient(135deg, var(--jpwvs-c1) 50%, var(--jpwvs-c2) 50%);
}

/* Unavailable (no variation with this combination) / disabled (out of stock). */
.jpwvs-swatches .jpwvs-swatch.jpwvs-swatch.is-unavailable,
.jpwvs-swatches .jpwvs-swatch.jpwvs-swatch.is-disabled {
	cursor: not-allowed;
	opacity: 0.4;
}

.jpwvs-unavail-cross .jpwvs-swatch.is-unavailable::after,
.jpwvs-unavail-cross .jpwvs-swatch.is-disabled::after {
	content: "";
	position: absolute;
	inset: 0;
	border-radius: inherit;
	background: linear-gradient(to top left, transparent calc(50% - 1px), currentColor calc(50% - 1px), currentColor calc(50% + 1px), transparent calc(50% + 1px));
	pointer-events: none;
}

.jpwvs-unavail-hide .jpwvs-swatches .jpwvs-swatch.jpwvs-swatch.is-unavailable,
.jpwvs-unavail-hide .jpwvs-swatches .jpwvs-swatch.jpwvs-swatch.is-disabled {
	display: none;
}

/* CSS-only tooltip for colour / image swatches. */
.jpwvs-swatch[data-jpwvs-tip]::before {
	content: attr(data-jpwvs-tip);
	position: absolute;
	bottom: calc(100% + 8px);
	left: 50%;
	z-index: 20;
	padding: 4px 8px;
	border-radius: 4px;
	background: #1d2327;
	color: #ffffff;
	font-size: 12px;
	line-height: 1.3;
	white-space: nowrap;
	pointer-events: none;
	opacity: 0;
	visibility: hidden;
	transform: translate(-50%, 4px);
	transition: opacity 0.12s ease, transform 0.12s ease, visibility 0.12s;
}

.jpwvs-swatch[data-jpwvs-tip]:hover::before,
.jpwvs-swatch[data-jpwvs-tip]:focus-visible::before {
	opacity: 1;
	visibility: visible;
	transform: translate(-50%, 0);
}

/* Chosen value appended to the attribute label by the script. */
.jpwvs-selected-value {
	font-weight: 400;
}

.jpwvs-selected-value::before {
	content: ": ";
}

/*
 * Product cards (shop, category pages, Elementor Products / Archive Products).
 * Layout only; the button keeps the theme's / Elementor's loop button style.
 * Set --jpwvs-loop-justify on the card (e.g. center) to align the swatches.
 */
.jpwvs-loop-form {
	margin: 12px 0 0;
	text-align: start;
}

.jpwvs-loop-form .jpwvs-loop-attribute {
	margin: 0 0 12px;
}

.jpwvs-loop-form .jpwvs-loop-label {
	display: block;
	margin: 0 0 6px;
	font-size: 12px;
	font-weight: 600;
	letter-spacing: 0.06em;
	line-height: 1.3;
	text-transform: uppercase;
}

/* The chosen value keeps its own case: "Size: 350mm", not "SIZE: 350MM". */
.jpwvs-loop-form .jpwvs-loop-label .jpwvs-selected-value {
	font-weight: 400;
	letter-spacing: normal;
	text-transform: none;
}

.jpwvs-loop-form .jpwvs-swatches {
	justify-content: var(--jpwvs-loop-justify, flex-start);
}

/*
 * The card's own price follows the chosen variation, so WooCommerce's
 * price / stock block stays out of the layout (no card height jump) but is
 * still read out: it is a live region.
 */
.jpwvs-loop-form .single_variation {
	position: absolute !important;
	width: 1px !important;
	height: 1px !important;
	margin: -1px !important;
	padding: 0 !important;
	overflow: hidden !important;
	clip: rect(0, 0, 0, 0) !important;
	white-space: nowrap !important;
	border: 0 !important;
}

.jpwvs-loop-form .woocommerce-variation-add-to-cart {
	display: flex;
	flex-wrap: wrap;
	align-items: stretch;
	gap: 8px;
	margin: 0;
}

/*
 * Quantity and button share one row at the button's height. Margins are
 * !important: loop button rules (WooCommerce's `ul.products li.product .button
 * { margin-top: 1em }`, Elementor's per-widget button spacing) would otherwise
 * push the button below the quantity box. Colours, padding and fonts stay the
 * theme's / Elementor's.
 */
.jpwvs-loop-form .woocommerce-variation-add-to-cart .quantity {
	display: flex;
	flex: 0 0 auto;
	float: none;
	margin: 0 !important;
}

.jpwvs-loop-form .woocommerce-variation-add-to-cart .quantity.hidden {
	display: none;
}

.jpwvs-loop-form .woocommerce-variation-add-to-cart .quantity .qty {
	align-self: stretch;
	height: auto;
	min-height: 0;
	margin: 0;
}

.jpwvs-loop-form .woocommerce-variation-add-to-cart .single_add_to_cart_button {
	flex: 1 1 auto;
	margin: 0 !important;
}

.jpwvs-loop-form .woocommerce-variation-add-to-cart .added_to_cart {
	flex-basis: 100%;
	margin: 0;
}

/*
 * A plain box for the quantity where the theme styles inputs on product pages
 * only (the browser's inset default otherwise). Zero specificity: any theme or
 * Elementor input style wins.
 */
:where(.jpwvs-loop-form .quantity .qty) {
	box-sizing: border-box;
	min-width: 3.5em;
	padding: 0 6px;
	border: 1px solid rgba(0, 0, 0, 0.2);
	border-radius: 3px;
	background: #fff;
	color: #111;
	font: inherit;
	text-align: center;
}

@media (prefers-reduced-motion: reduce) {
	.jpwvs-swatches .jpwvs-swatch.jpwvs-swatch,
	.jpwvs-swatch[data-jpwvs-tip]::before {
		transition: none;
	}
}
