/*
 * REACH Supply cart refinements.
 *
 * RS-DS-014: compact cart purchase controls and deterministic shipping.
 * RS-DS-015: the plugin owns one valid purchase bar, one quantity-update
 * pipeline, and guards native shipping/tax summary rows from legacy removal.
 *
 * WooCommerce remains responsible for quantities, prices, shipping rates,
 * taxes, coupons, totals, payment buttons, and checkout navigation.
 */

body.reach-design-system.woocommerce-cart {
	--rs-cart-quantity-width: 6.75rem; /* 108px */
	--rs-cart-media-size: 6rem;
}

/* Keep the accepted neutral card treatment and remove decorative gradients. */
body.reach-design-system.woocommerce-cart .reach-cart-totals-card::before,
body.reach-design-system.woocommerce-cart table.shop_table.cart tr.cart_item::before {
	background: var(--rs-color-gold) !important;
}

/* The canonical runtime keeps a valid direct <td> sentinel so the older page
 * script cannot create another invalid <div><td> purchase wrapper. */
body.reach-design-system.woocommerce-cart
.woocommerce-cart-form table.shop_table.cart tr.cart_item
> .reach-cart-purchase-footer-v21.rs-cart-footer-sentinel,
body.reach-design-system.woocommerce-cart
.woocommerce-cart-form table.shop_table.cart tr.cart_item
> .rs-cart-source-cell {
	display: none !important;
	height: 0 !important;
	margin: 0 !important;
	max-height: 0 !important;
	min-height: 0 !important;
	overflow: hidden !important;
	padding: 0 !important;
	visibility: hidden !important;
	width: 0 !important;
}

/* If legacy JavaScript runs before the canonical runtime, do not allow its
 * wrapper to create an oversized implicit grid row while normalization waits. */
body.reach-design-system.woocommerce-cart
.woocommerce-cart-form table.shop_table.cart tr.cart_item
> .reach-cart-purchase-footer-v21:not(.rs-cart-footer-sentinel) {
	grid-area: purchase !important;
	margin: 0 !important;
	min-height: 0 !important;
	padding: 0 !important;
}

/* =========================================================
 * Valid purchase bar
 * ========================================================= */

body.reach-design-system.woocommerce-cart
.woocommerce-cart-form table.shop_table.cart tr.cart_item.rs-cart-item-v2 {
	align-content: center !important;
	align-items: center !important;
	background: var(--rs-color-surface) !important;
	column-gap: 0.875rem !important;
	display: grid !important;
	grid-auto-rows: min-content !important;
	grid-template-areas: "thumb details purchase remove" !important;
	grid-template-columns:
		var(--rs-cart-media-size)
		minmax(0, 1fr)
		minmax(13.5rem, 15.5rem)
		2.125rem !important;
	height: auto !important;
	min-height: 0 !important;
	padding: 1rem !important;
	row-gap: 0 !important;
}

body.reach-design-system.woocommerce-cart
.woocommerce-cart-form table.shop_table.cart tr.cart_item.rs-cart-item-v2
.product-thumbnail {
	grid-area: thumb !important;
}

body.reach-design-system.woocommerce-cart
.woocommerce-cart-form table.shop_table.cart tr.cart_item.rs-cart-item-v2
.product-name {
	grid-area: details !important;
}

body.reach-design-system.woocommerce-cart
.woocommerce-cart-form table.shop_table.cart tr.cart_item.rs-cart-item-v2
.product-remove {
	grid-area: remove !important;
}

body.reach-design-system.woocommerce-cart
.woocommerce-cart-form table.shop_table.cart tr.cart_item.rs-cart-item-v2
> .rs-cart-purchase-bar {
	align-self: center !important;
	display: block !important;
	grid-area: purchase !important;
	justify-self: end !important;
	margin: 0 !important;
	min-height: 0 !important;
	padding: 0 !important;
	width: 100% !important;
}

body.reach-design-system.woocommerce-cart .rs-cart-purchase-bar__inner {
	align-items: end;
	display: grid;
	gap: 1rem;
	grid-template-columns: var(--rs-cart-quantity-width) minmax(5rem, 1fr);
	justify-content: end;
	min-width: 0;
	width: 100%;
}

body.reach-design-system.woocommerce-cart .rs-cart-purchase-group {
	display: flex;
	flex-direction: column;
	min-width: 0;
}

body.reach-design-system.woocommerce-cart .rs-cart-purchase-group--quantity {
	align-items: center;
}

body.reach-design-system.woocommerce-cart .rs-cart-purchase-group--total {
	align-items: flex-end;
	text-align: right;
}

body.reach-design-system.woocommerce-cart .rs-cart-purchase-label {
	color: var(--rs-color-subtle);
	font-size: 0.625rem;
	font-weight: var(--rs-font-weight-bold);
	letter-spacing: 0.055em;
	line-height: 1.2;
	margin-bottom: 0.375rem;
	text-transform: uppercase;
}

body.reach-design-system.woocommerce-cart .rs-cart-purchase-total {
	color: var(--rs-color-ink);
	display: block;
	font-size: 1.125rem;
	font-weight: var(--rs-font-weight-bold);
	line-height: 1.2;
	white-space: nowrap;
}

body.reach-design-system.woocommerce-cart .rs-cart-purchase-total :where(
	.woocommerce-Price-amount,
	.amount
) {
	color: inherit !important;
	font: inherit !important;
	line-height: inherit !important;
}

/* Compact, familiar quantity stepper. The 44px height remains touch-friendly
 * while the width no longer dominates the purchase row. */
body.reach-design-system.woocommerce-cart .woocommerce-cart-form .rs-cart-purchase-bar .quantity {
	background: var(--rs-color-surface) !important;
	border: var(--rs-border-strong) !important;
	border-radius: var(--rs-radius-sm) !important;
	box-shadow: var(--rs-shadow-xs) !important;
	display: grid !important;
	grid-template-columns: 2.125rem 2.5rem 2.125rem !important;
	height: 2.75rem !important;
	margin: 0 !important;
	max-width: var(--rs-cart-quantity-width) !important;
	min-height: 2.75rem !important;
	min-width: var(--rs-cart-quantity-width) !important;
	overflow: hidden !important;
	padding: 0 !important;
	width: var(--rs-cart-quantity-width) !important;
}

body.reach-design-system.woocommerce-cart
.woocommerce-cart-form .rs-cart-purchase-bar .quantity :where(
	.minus,
	.plus,
	button,
	.bde-quantity-button
) {
	background: var(--rs-color-surface-soft) !important;
	border: 0 !important;
	border-radius: 0 !important;
	color: var(--rs-color-ink) !important;
	display: grid !important;
	font-size: 1.25rem !important;
	height: 2.625rem !important;
	margin: 0 !important;
	max-width: 2.125rem !important;
	min-height: 2.625rem !important;
	min-width: 2.125rem !important;
	padding: 0 !important;
	place-items: center !important;
	width: 2.125rem !important;
}

body.reach-design-system.woocommerce-cart
.woocommerce-cart-form .rs-cart-purchase-bar .quantity :where(
	.minus,
	.plus,
	button,
	.bde-quantity-button
):hover {
	background: var(--rs-color-warning-soft) !important;
	color: var(--rs-color-warning) !important;
}

body.reach-design-system.woocommerce-cart
.woocommerce-cart-form .rs-cart-purchase-bar .quantity .qty {
	appearance: textfield !important;
	background: var(--rs-color-surface) !important;
	border: 0 !important;
	border-left: var(--rs-border-default) !important;
	border-radius: 0 !important;
	border-right: var(--rs-border-default) !important;
	color: var(--rs-color-ink) !important;
	font-size: 1rem !important;
	font-weight: var(--rs-font-weight-bold) !important;
	height: 2.625rem !important;
	line-height: 2.625rem !important;
	margin: 0 !important;
	max-width: 2.5rem !important;
	min-height: 2.625rem !important;
	min-width: 2.5rem !important;
	padding: 0 !important;
	text-align: center !important;
	width: 2.5rem !important;
}

body.reach-design-system.woocommerce-cart
.woocommerce-cart-form .rs-cart-purchase-bar .quantity .qty::-webkit-inner-spin-button,
body.reach-design-system.woocommerce-cart
.woocommerce-cart-form .rs-cart-purchase-bar .quantity .qty::-webkit-outer-spin-button {
	appearance: none !important;
	margin: 0 !important;
}

body.reach-design-system.woocommerce-cart
.woocommerce-cart-form .rs-cart-purchase-bar .quantity:focus-within {
	border-color: var(--rs-color-focus) !important;
	box-shadow: var(--rs-focus-ring) !important;
}

/* Product rows remain visually stable while WooCommerce recalculates. */
body.reach-design-system.woocommerce-cart tr.cart_item.is-reach-cart-updating {
	opacity: 0.68 !important;
}

body.reach-design-system.woocommerce-cart
tr.cart_item.is-reach-cart-updating .rs-cart-purchase-bar {
	pointer-events: none !important;
}

/* Mobile cart card: one identity row and one compact purchase bar. */
@media (max-width: 767px) {
	body.reach-design-system.woocommerce-cart {
		--rs-cart-media-size: 4.75rem;
		--rs-cart-quantity-width: 6.75rem;
	}

	body.reach-design-system.woocommerce-cart
	.woocommerce-cart-form table.shop_table.cart tr.cart_item.rs-cart-item-v2 {
		align-content: start !important;
		align-items: start !important;
		column-gap: 0.625rem !important;
		grid-template-areas:
			"thumb details remove"
			"purchase purchase purchase" !important;
		grid-template-columns: var(--rs-cart-media-size) minmax(0, 1fr) 2.125rem !important;
		grid-template-rows: auto min-content !important;
		padding: 0.8125rem !important;
		row-gap: 0.75rem !important;
	}

	body.reach-design-system.woocommerce-cart
	.woocommerce-cart-form table.shop_table.cart tr.cart_item.rs-cart-item-v2
	> .rs-cart-purchase-bar {
		align-self: start !important;
		justify-self: stretch !important;
		width: 100% !important;
	}

	body.reach-design-system.woocommerce-cart .rs-cart-purchase-bar__inner {
		align-items: end;
		border-top: var(--rs-border-default);
		gap: 0.875rem;
		grid-template-columns: var(--rs-cart-quantity-width) minmax(0, 1fr);
		padding-top: 0.75rem;
	}

	body.reach-design-system.woocommerce-cart .rs-cart-purchase-group--quantity {
		align-items: flex-start;
	}

	body.reach-design-system.woocommerce-cart .rs-cart-purchase-total {
		font-size: 1.25rem;
	}

	/* Fallback for the short interval before JavaScript normalization. */
	body.reach-design-system.woocommerce-cart
	.woocommerce-cart-form table.shop_table.cart tr.cart_item:has(
		> .reach-cart-purchase-footer-v21:not(.rs-cart-footer-sentinel)
	) {
		grid-template-areas:
			"thumb details remove"
			"purchase purchase purchase" !important;
		grid-template-columns: var(--rs-cart-media-size) minmax(0, 1fr) 2.125rem !important;
	}

	body.reach-design-system.woocommerce-cart
	.woocommerce-cart-form table.shop_table.cart tr.cart_item
	> .reach-cart-purchase-footer-v21:not(.rs-cart-footer-sentinel) {
		align-items: end !important;
		border-top: var(--rs-border-default) !important;
		display: grid !important;
		gap: 0.875rem !important;
		grid-template-columns: var(--rs-cart-quantity-width) minmax(0, 1fr) !important;
		padding-top: 0.75rem !important;
		width: 100% !important;
	}
}

@media (max-width: 390px) {
	body.reach-design-system.woocommerce-cart {
		--rs-cart-media-size: 4.375rem;
	}

	body.reach-design-system.woocommerce-cart
	.woocommerce-cart-form table.shop_table.cart tr.cart_item.rs-cart-item-v2 {
		column-gap: 0.5rem !important;
		padding: 0.6875rem !important;
	}
}

/* =========================================================
 * Stable order summary and deterministic shipping disclosure
 * ========================================================= */

/* Shipping and tax rows are native WooCommerce summary data. Legacy visual
 * scripts may not collapse or remove them while the current table is live. */
body.reach-design-system.woocommerce-cart .cart_totals table.shop_table :where(
	tr.woocommerce-shipping-totals.shipping,
	tr.shipping,
	tr.tax-rate,
	tr.woocommerce-tax-total
) {
	display: grid !important;
	height: auto !important;
	max-height: none !important;
	min-height: 0 !important;
	opacity: 1 !important;
	visibility: visible !important;
}

body.reach-design-system.woocommerce-cart
.woocommerce-shipping-calculator.rs-cart-shipping {
	background: var(--rs-color-surface-soft) !important;
	border: var(--rs-border-default) !important;
	border-radius: var(--rs-radius-md) !important;
	box-shadow: none !important;
	overflow: clip !important;
}

/* Closed means closed even if old jQuery slide styles or .is-open remain. */
body.reach-design-system.woocommerce-cart
.woocommerce-shipping-calculator.rs-cart-shipping
.shipping-calculator-form[hidden],
body.reach-design-system.woocommerce-cart
.woocommerce-shipping-calculator.rs-cart-shipping:not(.rs-cart-shipping-open)
.shipping-calculator-form {
	border: 0 !important;
	display: none !important;
	height: 0 !important;
	margin: 0 !important;
	max-height: 0 !important;
	min-height: 0 !important;
	opacity: 0 !important;
	overflow: hidden !important;
	padding: 0 !important;
	pointer-events: none !important;
	visibility: hidden !important;
}

/* Open state is owned exclusively by the canonical cart runtime. */
body.reach-design-system.woocommerce-cart
.woocommerce-shipping-calculator.rs-cart-shipping.rs-cart-shipping-open
.shipping-calculator-form:not([hidden]) {
	align-items: start !important;
	border: 0 !important;
	border-top: var(--rs-border-default) !important;
	display: grid !important;
	grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
	height: auto !important;
	margin: var(--rs-space-3) 0 0 !important;
	max-height: none !important;
	opacity: 1 !important;
	overflow: visible !important;
	padding: var(--rs-space-3) 0 0 !important;
	pointer-events: auto !important;
	position: static !important;
	row-gap: var(--rs-space-3) !important;
	visibility: visible !important;
}

body.reach-design-system.woocommerce-cart
.woocommerce-shipping-calculator.rs-cart-shipping
.shipping-calculator-button {
	background: var(--rs-color-warning-soft) !important;
	border: 1px solid rgb(226 166 60 / 55%) !important;
	color: var(--rs-color-warning) !important;
	cursor: pointer;
}

body.reach-design-system.woocommerce-cart
.woocommerce-shipping-calculator.rs-cart-shipping
.shipping-calculator-button::after {
	content: "+" !important;
}

body.reach-design-system.woocommerce-cart
.woocommerce-shipping-calculator.rs-cart-shipping.rs-cart-shipping-open
.shipping-calculator-button::after {
	content: "−" !important;
}

body.reach-design-system.woocommerce-cart
.woocommerce-shipping-calculator.rs-cart-shipping
.shipping-calculator-form :where(.form-row, p) {
	float: none !important;
	margin: 0 !important;
	max-width: 100% !important;
	min-width: 0 !important;
	width: 100% !important;
}

body.reach-design-system.woocommerce-cart
.woocommerce-shipping-calculator.rs-cart-shipping
.shipping-calculator-form > :last-child {
	grid-column: 1 / -1 !important;
}

@media (max-width: 767px) {
	body.reach-design-system.woocommerce-cart
	.woocommerce-shipping-calculator.rs-cart-shipping.rs-cart-shipping-open
	.shipping-calculator-form:not([hidden]) {
		grid-template-columns: 1fr !important;
	}

	body.reach-design-system.woocommerce-cart
	.woocommerce-shipping-calculator.rs-cart-shipping
	.select2-container {
		display: none !important;
	}

	body.reach-design-system.woocommerce-cart
	.woocommerce-shipping-calculator.rs-cart-shipping
	select.select2-hidden-accessible,
	body.reach-design-system.woocommerce-cart
	.woocommerce-shipping-calculator.rs-cart-shipping
	select {
		clip: auto !important;
		clip-path: none !important;
		display: block !important;
		height: var(--rs-control-height) !important;
		inset: auto !important;
		margin: 0 !important;
		max-height: var(--rs-control-height) !important;
		max-width: 100% !important;
		min-height: var(--rs-control-height) !important;
		min-width: 0 !important;
		opacity: 1 !important;
		overflow: visible !important;
		pointer-events: auto !important;
		position: static !important;
		visibility: visible !important;
		width: 100% !important;
	}

	body.reach-design-system.woocommerce-cart
	.woocommerce-shipping-calculator.rs-cart-shipping
	.shipping-calculator-form > * {
		grid-column: 1 !important;
	}
}

@media (prefers-reduced-motion: reduce) {
	body.reach-design-system.woocommerce-cart :where(
		.rs-cart-purchase-bar,
		.shipping-calculator-form
	) {
		transition: none !important;
	}
}
