<?php
/**
 * File Path: includes/compat/compat-betheme.php
 * BeTheme (Muffin Group) Compatibility -- loaded when get_template() === 'betheme'.
 * Findings reference: PATCHES/COMPAT-WORKLOG.md -- BT-001 through BT-005.
 * @package My_Book_Showroom
 * @since   1.0.0
 */

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


/**
 * BT-001 + BT-002 — BeTheme #Content specificity override.
 *
 * BT-001: BeTheme scopes typography rules to #Content:
 *   #Content .entry-content p { ... }  — specificity 1,2,1
 * mbs-compat.css uses .entry-content .mbs-wrapper p (0,3,1) — cannot beat
 * an ID-prefixed chain.
 *
 * BT-002: BeTheme dynamic CSS may load inline at wp_head priority 1 or
 * attached to a registered handle. !important on BT-001 rules is the safe
 * fix in both cases.
 *
 * Fix: attach inline CSS to 'mbs-front' at priority 99. Reassert paragraph
 * margin rules with !important. All other properties use load-order advantage
 * from the inline block printing after linked stylesheets.
 *
 * Scope: .entry-content .mbs-wrapper — BeTheme keeps .entry-content on
 * standard posts and (likely) on Muffin Builder pages as an outer wrapper.
 * If BT-004 is confirmed (Muffin Builder pages lack .entry-content), a
 * parallel scope will be needed — see BT-004 note below.
 *
 * Notes:
 * [AC] Architecture-confirmed: BeTheme's #Content wrapper is well-documented
 * public architecture used across BeTheme versions.
 * Verify paragraph margins on an actual BeTheme install, especially in
 * child themes that remove or alter #Content.
 */
add_action( 'wp_enqueue_scripts', static function () {

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

	$css = '
		/* BT-001: override BeTheme #Content ID-prefixed typography (specificity 1,2,1) */
		/* BT-002: !important makes load-order vs. BeTheme dynamic styles irrelevant */

		/* Paragraph cascade — !important required to beat #Content .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 — load order wins here for standard heading rules */
		.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 */
		.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 */
		.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 */
		.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 );

/**
 * BT-004 note -- Muffin Builder pages and .entry-content parent. [AI]
 * If MBS shortcodes in a Muffin Builder Text element lose styling, verify whether
 * .entry-content is absent and add a parallel scope per compat-divi.php pattern.
 * Full analysis in PATCHES/COMPAT-WORKLOG.md -- BT-004.
 */
