/**
 * Ravenswood Addify Linked Products Tweaks
 * https://ravenswood.media/
 *
 * Reconciles Addify Linked Products with the Elementor Pro
 * "Add To Cart" widget and corrects its cart-facing UX.
 *
 * NOTE ON CLASS NAMES
 * WooCommerce builds the item-data class with sanitize_html_class(),
 * not sanitize_title(): "Included free with" becomes
 * "variation-Includedfreewith", case preserved, spaces stripped.
 * The hyphenated form is kept alongside it as a fallback in case a
 * future WooCommerce release changes the helper. If you filter
 * rwlpt_linked_item_label, update both spellings below.
 */

/* ---------------------------------------------------------------
 * 1. Restore the single-row quantity + button layout.
 *
 * Elementor's "Stacked" layout applies:
 *     form.cart { display: block }                  <- wanted
 *     .e-atc-qty-button-holder { display:flex; flex-wrap:wrap }
 *     .e-atc-qty-button-holder > * { flex-basis:100% }   <- unwanted
 *
 * The 100% basis plus wrap is what pushes the quantity box onto
 * its own line. Only the holder is corrected; form.cart stays
 * block so the linked-products panel sits above it.
 * ------------------------------------------------------------- */
.elementor-widget-woocommerce-product-add-to-cart.elementor-add-to-cart--layout-stacked .e-atc-qty-button-holder {
	flex-wrap: nowrap;
	align-items: stretch;
	gap: 10px;
}

.elementor-widget-woocommerce-product-add-to-cart.elementor-add-to-cart--layout-stacked .e-atc-qty-button-holder > .quantity {
	flex: 0 0 auto;
}

.elementor-widget-woocommerce-product-add-to-cart.elementor-add-to-cart--layout-stacked .e-atc-qty-button-holder > .button,
.elementor-widget-woocommerce-product-add-to-cart.elementor-add-to-cart--layout-stacked .e-atc-qty-button-holder > button[type="submit"] {
	flex: 1 1 auto;
	margin-left: 0;
}

/* Single-stock products render an empty .quantity wrapper around a
 * hidden input. Collapse it so the gap above does not show. */
.elementor-widget-woocommerce-product-add-to-cart .e-atc-qty-button-holder > .quantity:not(:has(input.qty:not([type="hidden"]))) {
	display: none;
}

/* ---------------------------------------------------------------
 * 2. Breathing room between the linked-products panel and the
 *    add-to-cart row, now that form.cart is a block.
 * ------------------------------------------------------------- */
.elementor-widget-woocommerce-product-add-to-cart form.cart > .af-cp-chain-product {
	margin-bottom: 15px;
}

/* ---------------------------------------------------------------
 * 3. Remove the "Linked products total:" line.
 *
 * The plugin hard-includes templates/linked-products.php with no
 * wc_get_template() lookup and no filter, so a theme override or
 * template edit would be lost on update. Hidden in CSS instead,
 * which also removes it from the accessibility tree.
 * ------------------------------------------------------------- */
.af-cpfw-price-html {
	display: none !important;
}

/* ---------------------------------------------------------------
 * 4. Product page: drop the permanently ticked, disabled checkbox
 *    on mandatory linked items.
 *
 * templates/linked-products.php renders a checked + disabled input
 * for required items. It cannot be interacted with and reads as a
 * broken control. Optional linked products use a real, enabled
 * checkbox, which is left alone.
 *
 * .af-cp-compact-item is display:flex with a gap, so removing one
 * child simply closes the space.
 * ------------------------------------------------------------- */
.af-cp-chain-product .af-cp-compact-select:has(input[disabled]),
.af-cp-chain-product .af-cp-grid-card .af-cp-compact-select:has(input[disabled]) {
	display: none;
}

/* ---------------------------------------------------------------
 * 5. Cart and mini-cart: stack the "Included free with" label above
 *    the parent product name.
 *
 * WooCommerce renders item data as <dl class="variation"> with a
 * <dt> label and <dd> value. Themes and the Elementor Menu Cart lay
 * these out as two columns, which in a narrow off-canvas panel
 * gives a short label beside a one-word-wide column of text and a
 * tall block of empty space.
 *
 * Scoped to the linked-item row, so a variable product's own
 * attribute rows keep the theme's styling.
 * ------------------------------------------------------------- */
dl.variation:has(dt.variation-Includedfreewith),
dl.variation:has(dt.variation-included-free-with) {
	display: block;
	float: none;
	margin: 4px 0 6px;
	padding: 0;
	overflow: visible;
	line-height: 1.4;
}

dl.variation:has(dt.variation-Includedfreewith) > dt,
dl.variation:has(dt.variation-Includedfreewith) > dd,
dl.variation:has(dt.variation-included-free-with) > dt,
dl.variation:has(dt.variation-included-free-with) > dd {
	display: block;
	float: none;
	clear: both;
	width: auto;
	max-width: none;
	min-height: 0;
	margin: 0;
	padding: 0;
}

dl.variation:has(dt.variation-Includedfreewith) > dd p,
dl.variation:has(dt.variation-included-free-with) > dd p {
	margin: 0;
}

/* ---------------------------------------------------------------
 * 6. The label itself: bold, in the cart and the mini-cart.
 * ------------------------------------------------------------- */
dl.variation > dt.variation-Includedfreewith,
dl.variation > dt.variation-included-free-with {
	font-weight: 700;
}

/* ---------------------------------------------------------------
 * 7. "FREE" in the brand price red.
 *
 * var(--e-global-color-07421aa) is the Elementor global colour used
 * by .price, so this tracks any change made in Site Settings. The
 * hex is its current value, used where the variable is unavailable.
 * ------------------------------------------------------------- */
.rwlpt-free {
	font-weight: 700;
	text-transform: uppercase;
	letter-spacing: 0.02em;
	color: var( --e-global-color-07421aa, #b3001b );
}

/* ---------------------------------------------------------------
 * 8. Hide the mini-cart remove control on a mandatory linked item.
 *
 * Addify blanks woocommerce_cart_item_remove_link, which the cart
 * page honours, but the Elementor Menu Cart widget renders its own
 * control. Clicking it is harmless (Af_C_P_Cart::item_removed calls
 * restore_cart_item) but it reads as though the free gift can be
 * declined, and it reappears on the next refresh.
 * ------------------------------------------------------------- */
.elementor-menu-cart__product:has(dt.variation-Includedfreewith) .remove,
.elementor-menu-cart__product:has(dt.variation-Includedfreewith) .remove_from_cart_button,
.elementor-menu-cart__product:has(dt.variation-included-free-with) .remove,
.elementor-menu-cart__product:has(dt.variation-included-free-with) .remove_from_cart_button,
.woocommerce-mini-cart-item:has(dt.variation-Includedfreewith) .remove,
.woocommerce-mini-cart-item:has(dt.variation-Includedfreewith) .remove_from_cart_button,
.woocommerce-mini-cart-item:has(dt.variation-included-free-with) .remove,
.woocommerce-mini-cart-item:has(dt.variation-included-free-with) .remove_from_cart_button {
	display: none !important;
}

/* ---------------------------------------------------------------
 * 9. Mobile: allow the add-to-cart row to wrap if it cannot fit.
 * ------------------------------------------------------------- */
@media (max-width: 480px) {
	.elementor-widget-woocommerce-product-add-to-cart.elementor-add-to-cart--layout-stacked .e-atc-qty-button-holder > .button,
	.elementor-widget-woocommerce-product-add-to-cart.elementor-add-to-cart--layout-stacked .e-atc-qty-button-holder > button[type="submit"] {
		min-width: 0;
	}
}
