<?php
/**
 * File Path: includes/compat/compat-avada.php
 * Avada Theme (ThemeFusion) Compatibility -- loaded when get_template() === 'Avada'.
 * Findings reference: PATCHES/COMPAT-WORKLOG.md -- AV-001 through AV-005.
 * @package My_Book_Showroom
 * @since   1.0.0
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * AV-001 + AV-002 -- Avada #wrapper specificity override.
 * Avada's dynamic CSS uses #wrapper-prefixed selectors (specificity 1,2,1),
 * beating mbs-compat.css class-only rules. !important on paragraph margins
 * beats both the specificity issue and fusion-dynamic-css load-order uncertainty.
 */
add_action( 'wp_enqueue_scripts', static function () {

	if ( ! wp_style_is( 'mbs-front', 'enqueued' ) ) {
		return;
	}

	$css = '
		/* AV-001: override Avada #wrapper ID-prefixed typography (specificity 1,2,1) */
		/* AV-002: !important makes load-order vs. fusion-dynamic-css irrelevant */

		/* Paragraph cascade — !important required to beat #wrapper .entry-content p (1,2,1) */
		.entry-content .mbs-wrapper p {
			margin: 0 0 0.75em !important;
			line-height: 1.6;
		}
		.entry-content .mbs-wrapper p:last-child {
			margin-bottom: 0 !important;
		}

		/* Heading cascade — class-only chain (0,3,1) beats Avada heading rules at equal spec */
		.entry-content .mbs-wrapper h1,
		.entry-content .mbs-wrapper h2,
		.entry-content .mbs-wrapper h3,
		.entry-content .mbs-wrapper h4,
		.entry-content .mbs-wrapper h5,
		.entry-content .mbs-wrapper h6 {
			margin: 0 0 0.5em;
			line-height: 1.3;
		}

		/* List cascade — load order wins here; no !important needed */
		.entry-content .mbs-wrapper ul,
		.entry-content .mbs-wrapper ol {
			padding: 0 0 0 1.25em;
			margin: 0 0 0.75em;
		}
		.entry-content .mbs-wrapper li {
			margin-bottom: 0.25em;
		}
		.entry-content .mbs-wrapper .mbs-per-format-meta {
			padding: 0;
			margin: 0;
			list-style: none;
		}

		/* Link color isolation — Avada may inherit theme-set link colors */
		.entry-content .mbs-wrapper a {
			color: var(--mbs-link, #4AABB8);
		}
		.entry-content .mbs-wrapper a:hover,
		.entry-content .mbs-wrapper a:focus {
			color: var(--mbs-accent, #4AABB8);
		}

		/* Retailer buy button color restoration */
		.entry-content .mbs-wrapper a.mbs-retailer-btn {
			color: var(--mbs-button-text, #FFFFFF);
			background-color: var(--mbs-button-bg, #2C3E6B);
			text-decoration: none;
		}

		/* Format tabs */
		.entry-content .mbs-wrapper a.mbs-format-tab {
			text-decoration: none;
		}

		/* Image containment — Avada may add box-shadow or border via dynamic CSS */
		.entry-content .mbs-wrapper img {
			border: 0 none;
			box-shadow: none;
			float: none;
			margin: 0;
			vertical-align: middle;
		}
	';

	wp_add_inline_style( 'mbs-front', $css );

}, 99 );
