/**
 * Loading feedback: page loader overlay and local busy indicators.
 *
 * Loaded on every page except cart, checkout and My account (inc/loading-feedback.php).
 * Behavior: assets/js/loading-feedback.js and the inline <head> script.
 */

/* ------------------------------------------------------------------ Page loader */

/*
 * Rendered hidden at the top of <body>. Shown only while <html> has
 * .alatinidza-is-loading (direct arrival, until the HTML is parsed) or
 * .alatinidza-is-leaving (after a click on an internal link, until the next page).
 * The fade-in waits a threshold, so pages that are ready sooner never flash it.
 * It never takes clicks or focus, and disappears after 10 s whatever happens.
 */
.alatinidza-loader {
	--alatinidza-loader-delay: 400ms;
	--alatinidza-loader-dim: var(--wp--preset--color--on-accent);
	--alatinidza-logo-light: #ecece7; /* Light logo colour, sampled from logo-symbol-light.png. */

	position: fixed;
	inset: 0;
	z-index: 100001; /* Above the filter drawer (catalog.css) and the admin bar. */
	display: none;
	gap: var(--wp--preset--spacing--30);
	place-content: center;
	justify-items: center;
	pointer-events: none;
	background:
		radial-gradient(closest-side, color-mix(in srgb, var(--alatinidza-loader-dim) 34%, transparent), transparent),
		color-mix(in srgb, var(--alatinidza-loader-dim) 60%, transparent);
}

.alatinidza-is-leaving .alatinidza-loader {
	--alatinidza-loader-delay: 150ms;
}

.alatinidza-is-loading .alatinidza-loader,
.alatinidza-is-leaving .alatinidza-loader {
	display: grid;
	animation:
		alatinidza-loader-in 0.2s ease-out var(--alatinidza-loader-delay) both,
		alatinidza-loader-expire 0s 10s forwards;
}

.alatinidza-loader__mark,
.alatinidza-loader__wordmark {
	display: block;
}

.alatinidza-loader__mark {
	width: clamp(6rem, 26vw, 8.5rem);
}

.alatinidza-loader__wordmark {
	width: clamp(11rem, 48vw, 14.75rem);
}

.alatinidza-loader svg {
	display: block;
	width: 100%;
	height: auto;
	overflow: visible;
}

@keyframes alatinidza-loader-in {
	from {
		opacity: 0;
	}

	to {
		opacity: 1;
	}
}

@keyframes alatinidza-loader-expire {
	to {
		visibility: hidden;
		opacity: 0;
	}
}

/*
 * Drawing (variant 1): each line of the symbol is drawn as an outline, then filled;
 * the screw parts arrive last, top to bottom (about 1.65 s). The script repeats it
 * after a short hold while the next page is still loading, by switching between two
 * identical sets of keyframes (.is-repeat). Reduced motion: the finished logo, still.
 */
@media (prefers-reduced-motion: no-preference) {
	.alatinidza-loader {
		--alatinidza-draw-delay: var(--alatinidza-loader-delay);
	}

	:is(.alatinidza-is-loading, .alatinidza-is-leaving) .alatinidza-loader__line {
		fill-opacity: 0;
		stroke: var(--alatinidza-logo-light);
		stroke-width: 7;
		stroke-dasharray: 1 1;
		stroke-dashoffset: 1;
		animation:
			alatinidza-draw 0.62s cubic-bezier(0.65, 0, 0.35, 1) calc(var(--alatinidza-draw-delay) + var(--i) * 60ms) forwards,
			alatinidza-fill 0.45s ease-out calc(var(--alatinidza-draw-delay) + 560ms + var(--i) * 60ms) forwards;
	}

	:is(.alatinidza-is-loading, .alatinidza-is-leaving) .alatinidza-loader__screw {
		opacity: 0;
		transform: translateY(-60px);
		animation: alatinidza-screw 0.38s cubic-bezier(0.2, 0.7, 0.3, 1) calc(var(--alatinidza-draw-delay) + 1050ms + var(--i) * 55ms) forwards;
	}

	.alatinidza-loader.is-repeat .alatinidza-loader__line {
		animation-name: alatinidza-draw-repeat, alatinidza-fill-repeat;
	}

	.alatinidza-loader.is-repeat .alatinidza-loader__screw {
		animation-name: alatinidza-screw-repeat;
	}
}

@keyframes alatinidza-draw {
	to {
		stroke-dashoffset: 0;
	}
}

@keyframes alatinidza-draw-repeat {
	to {
		stroke-dashoffset: 0;
	}
}

@keyframes alatinidza-fill {
	to {
		fill-opacity: 1;
		stroke-opacity: 0;
	}
}

@keyframes alatinidza-fill-repeat {
	to {
		fill-opacity: 1;
		stroke-opacity: 0;
	}
}

@keyframes alatinidza-screw {
	to {
		opacity: 1;
		transform: none;
	}
}

@keyframes alatinidza-screw-repeat {
	to {
		opacity: 1;
		transform: none;
	}
}

/* ------------------------------------------------------------ Local indicators */

@keyframes alatinidza-spin {
	to {
		transform: rotate(1turn);
	}
}

/*
 * Add-to-cart buttons while their request is pending: WooCommerce's product button
 * (data attribute from loading-feedback.js) and the product page form (.is-loading
 * from product.js). A small ring at the end of the button; the label stays.
 */
.wc-block-components-product-button__button[data-alatinidza-busy],
.single_add_to_cart_button.is-loading {
	position: relative;
	cursor: progress;
}

.wc-block-components-product-button__button[data-alatinidza-busy]::after,
.single_add_to_cart_button.is-loading::after {
	position: absolute;
	inset-block-start: 50%;
	inset-inline-end: 0.75em;
	width: 1em;
	height: 1em;
	margin-block-start: -0.5em;
	content: "";
	border: 2px solid currentColor;
	border-inline-end-color: transparent;
	border-radius: 50%;
	animation: alatinidza-spin 0.7s linear infinite;
}

/*
 * Product results while WooCommerce filters, sorts or paginates in place: the
 * current results fade back and a small ring stays in view over them (it takes no
 * layout space). aria-busy is set by loading-feedback.js.
 */
.wp-block-woocommerce-product-collection[aria-busy="true"] > * {
	opacity: 0.55;
	transition: opacity 0.2s ease-out;
}

.wp-block-woocommerce-product-collection[aria-busy="true"]::before {
	position: sticky;
	inset-block-start: 40vh;
	z-index: 2;
	display: block;
	width: 2.5rem;
	height: 2.5rem;
	margin: 0 auto -2.5rem;
	content: "";
	border: 3px solid var(--wp--preset--color--primary);
	border-inline-end-color: transparent;
	border-radius: 50%;
	animation: alatinidza-spin 0.8s linear infinite;
}

@media (prefers-reduced-motion: reduce) {
	.wc-block-components-product-button__button[data-alatinidza-busy]::after,
	.single_add_to_cart_button.is-loading::after,
	.wp-block-woocommerce-product-collection[aria-busy="true"]::before {
		animation: none;
	}

	.wp-block-woocommerce-product-collection[aria-busy="true"] > * {
		transition: none;
	}
}
