<?php
/**
 * Enqueues all CSS files for the Boxyfy plugin.
 *
 * @package Boxyfy
 */

// Sicherheitscheck: Direkten Zugriff auf die Datei verhindern.
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Enqueue main Boxyfy-PHP CSS.
 */
function boxyfy_enqueue_main_style() {
	wp_enqueue_style(
		'boxyfy-main-style', // Handle
		plugins_url( 'assets/css/boxyfy-main-style.css', BOXYFY_PLUGIN_FILE ), // Pfad zur Datei
		array(), // Dependencies (leer, wenn keine)
		'1.0.0' // Version
	);
}
add_action( 'admin_enqueue_scripts', 'boxyfy_enqueue_main_style' );
add_action( 'wp_enqueue_scripts', 'boxyfy_enqueue_main_style' );


/* =============================================================================
 *                              ADMIN ENQUEUE SCRIPTS
 * ============================================================================= */

/**
 * Enqueues the CSS for the backend settings pages.
 *
 * This stylesheet is loaded in the admin area to support the styling of the backend settings pages.
 *
 * @package Boxyfy
 */
function boxyfy_enqueue_backend_setting_pages_css() {
    wp_enqueue_style(
        'boxyfy-backend-setting-pages-css', // Style handle
        plugins_url( 'assets/css/backend_setting_pages.css', BOXYFY_PLUGIN_FILE ), // Path to the CSS file
        array(), // Dependencies
        '1.0.0' // Version number
    );
}
add_action( 'admin_enqueue_scripts', 'boxyfy_enqueue_backend_setting_pages_css' );

/**
 * Enqueues the CSS for the backend heatmap feature.
 *
 * This stylesheet is loaded in the admin area to style the heatmap interface.
 *
 * @package Boxyfy
 */
function boxyfy_enqueue_backend_heatmap_css() {
    wp_enqueue_style(
        'boxyfy-backend-heatmap-css', // Style handle
        plugins_url( 'assets/css/heatmap.css', BOXYFY_PLUGIN_FILE ), // Pfad zur heatmap.css
        array(), // Dependencies
        '1.0.0' // Version
    );
}
add_action( 'admin_enqueue_scripts', 'boxyfy_enqueue_backend_heatmap_css' );


/**
 * Enqueues the CSS file for the Product Labels metabox in the Admin.
 *
 * @param string $hook The current admin page hook.
 */
function boxyfy_produktlabels_admin_enqueue_styles( $hook ) {
	global $post;

	// Nur auf den Edit-/New-Seiten und nur für den Post-Typ "boxyfy_tsprodukte" laden.
	if ( ( 'post.php' === $hook || 'post-new.php' === $hook ) && isset( $post ) && 'boxyfy_tsprodukte' === $post->post_type ) {
		wp_enqueue_style(
			'boxyfy-metabox-produktlabels-css',
			plugins_url( 'assets/css/metabox-produktlabels.css', BOXYFY_PLUGIN_FILE ),
			array(),
			'1.0.0'
		);
	}
}
add_action( 'admin_enqueue_scripts', 'boxyfy_produktlabels_admin_enqueue_styles' );

/**
 * Enqueue metabox CSS for additional images.
 *
 * @param string $hook The current admin hook.
 */
function boxyfy_add_images_metabox_enqueue_styles( $hook ) {
	global $post;
	// Only load on edit/new pages for the "boxyfy_tsprodukte" post type.
	if ( ( 'post.php' === $hook || 'post-new.php' === $hook ) && isset( $post ) && 'boxyfy_tsprodukte' === $post->post_type ) {
		wp_enqueue_style(
			'ts-metabox-weitere-bilder-css',
			plugins_url( 'assets/css/metabox_weitere_bilder.css', BOXYFY_PLUGIN_FILE ),
			array(),
			'1.0.0'
		);
	}
}
add_action( 'admin_enqueue_scripts', 'boxyfy_add_images_metabox_enqueue_styles' );

/**
 * Enqueue admin CSS for the "Weitere Preise" metabox.
 *
 * @param string $hook The current admin page hook.
 */
function boxyfy_weitere_preise_enqueue_styles( $hook ) {
	global $post;
	// Nur auf den Edit-/New-Seiten und nur für z. B. den Post-Typ "boxyfy_tsprodukte"
	if ( ( 'post.php' === $hook || 'post-new.php' === $hook ) && isset( $post ) && 'boxyfy_tsprodukte' === $post->post_type ) {
		wp_enqueue_style(
			'boxyfy-metabox-weitere-preise-css',
			plugins_url( 'assets/css/metabox_weitere_preise.css', BOXYFY_PLUGIN_FILE ),
			array(),
			'1.0.0'
		);
	}
}
add_action( 'admin_enqueue_scripts', 'boxyfy_weitere_preise_enqueue_styles' );

/**
 * Enqueue admin CSS for the "Vor- und Nachteile" metabox.
 *
 * @param string $hook The current admin page hook.
 */
function boxyfy_vor_nachteile_admin_assets_css( $hook ) {
	global $post;
	// Nur auf den Edit-/New-Seiten und nur für z. B. den Post-Typ "boxyfy_tsprodukte"
	if ( ( 'post.php' === $hook || 'post-new.php' === $hook ) && isset( $post ) && 'boxyfy_tsprodukte' === $post->post_type ) {
		wp_enqueue_style(
			'boxyfy-vorteile-nachteile-css',
			plugins_url( 'assets/css/metabox-vorteile-nachteile.css', BOXYFY_PLUGIN_FILE ),
			array(),
			'1.0.0'
		);
	}
}
add_action( 'admin_enqueue_scripts', 'boxyfy_vor_nachteile_admin_assets_css' );

/**
 * Enqueue admin CSS for Autoren-Subpage.
 *
 * @param string $hook Current admin page hook.
 */
function boxyfy_autoren_admin_enqueue_styles( $hook ) {
    // Rechte-Check (optional, aber empfohlen)
    if ( ! current_user_can( 'edit_posts' ) ) {
        return;
    }

    // Sicherer Zugriff auf $_GET['page']
    $requested_page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : '';

    // CSS nur laden, wenn auf Autoren-Seite oder beim Bearbeiten von tsprodukte
    if (
        'boxyfy_autoren' === $requested_page ||
        'post.php' === $hook ||
        'post-new.php' === $hook
    ) {
        wp_enqueue_style(
            'boxyfy-autoren-admin-css',
            plugins_url( 'assets/css/autoren-admin.css', BOXYFY_PLUGIN_FILE ),
            array(),
            '1.0.0'
        );
    }
}
add_action( 'admin_enqueue_scripts', 'boxyfy_autoren_admin_enqueue_styles' );

/**
 * Adds a CSS class to the "Info & License Purchase" menu item.
 */
function boxyfy_customize_admin_menu() {
    global $submenu;

    // Überprüfen, ob das Boxyfy-Menü existiert.
    if ( isset( $submenu['edit.php?post_type=boxyfy_tsprodukte'] ) ) {
        foreach ( $submenu['edit.php?post_type=boxyfy_tsprodukte'] as $key => $menu_item ) {
            // Suchen nach dem Menüpunkt "Info & Lizenz kaufen".
            if ( strpos( $menu_item[2], 'boxyfy_info_kaufen' ) !== false ) {
                // Füge eine CSS-Klasse hinzu
                $submenu['edit.php?post_type=boxyfy_tsprodukte'][$key][4] = 'boxyfy-license-highlight'; // Position 4: CSS-Klasse
                break;
            }
        }
    }
}
add_action( 'admin_menu', 'boxyfy_customize_admin_menu', 100 );

/**
 * Enqueue admin CSS for all Boxyfy admin pages.
 *
 * @param string $hook Current admin page hook.
 */
function boxyfy_enqueue_infopage_admin_styles( $hook ) {
    // Prüfen, ob die aktuelle Seite eine Boxyfy-Seite ist
    if ( strpos( $hook, 'boxyfy' ) !== false ) {
        wp_enqueue_style(
            'boxyfy-infopage-admin-css',
            plugins_url( 'assets/css/info-und-kaufen.css', BOXYFY_PLUGIN_FILE ),
            array(),
            '1.0.5'
        );
    }
}
add_action( 'admin_enqueue_scripts', 'boxyfy_enqueue_infopage_admin_styles' );


/**
 * Enqueue admin CSS for the "Bewertung" side-metabox.
 *
 * @param string $hook The current admin page hook (e.g., post.php, post-new.php).
 */
function boxyfy_enqueue_bewertung_metabox_css( $hook ) {
	global $post;
	// Only load on edit/new pages for the "boxyfy_tsprodukte" post type.
	if ( ( 'post.php' === $hook || 'post-new.php' === $hook ) && isset( $post ) && 'boxyfy_tsprodukte' === $post->post_type ) {
		wp_enqueue_style(
			'boxyfy-metabox-bewertung-css',
			plugins_url( 'assets/css/metabox-bewertung.css', BOXYFY_PLUGIN_FILE ),
			array(),
			'1.0.0'
		);
	}
}
add_action( 'admin_enqueue_scripts', 'boxyfy_enqueue_bewertung_metabox_css' );

/**
 * Enqueue admin CSS for the "Star Rating" side-metabox.
 *
 * @param string $hook The current admin page hook (e.g., post.php, post-new.php).
 */
function boxyfy_enqueue_star_rating_metabox_css( $hook ) {
	global $post;
	// Only load on edit/new pages for the "boxyfy_tsprodukte" post type.
	if ( ( 'post.php' === $hook || 'post-new.php' === $hook ) && isset( $post ) && 'boxyfy_tsprodukte' === $post->post_type ) {
		wp_enqueue_style(
			'boxyfy-metabox-star-rating-css',
			plugins_url( 'assets/css/metabox_products_star_rating.css', BOXYFY_PLUGIN_FILE ),
			array(),
			'1.0.0'
		);
	}
}
add_action( 'admin_enqueue_scripts', 'boxyfy_enqueue_star_rating_metabox_css' );

/**
 * Enqueue admin CSS for license Subpage.
 *
 * @param string $hook Current admin page hook.
 */
function boxyfy_enqueue_license_admin_assets( $hook ) {
	// Sicherer Zugriff auf $_GET['page']
	$page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : '';

	// Wenn wir auf der Lizenz-Unterseite sind, Styles einbinden
	if ( 'boxyfy_license' === $page ) {
		wp_enqueue_style(
			'boxyfy-license-admin-css',
			plugins_url( 'assets/css/license-functions.css', BOXYFY_PLUGIN_FILE ),
			array(),
			'1.0.0'
		);
	}
}
add_action( 'admin_enqueue_scripts', 'boxyfy_enqueue_license_admin_assets' );

/**
 * Enqueue CSS for the Amazon Price Metabox.
 *
 * Loads the external CSS file for the Amazon Price Metabox in the admin area
 * for posts of type TSProdukte.
 *
 * @param string $hook_suffix The current admin page hook suffix.
 */
function boxyfy_enqueue_singleproduktpreis_css( $hook_suffix ) {
    global $post;
    if ( isset( $post->post_type ) && 'boxyfy_tsprodukte' === $post->post_type ) {
        wp_enqueue_style(
            'boxyfy-singleproduktpreis-css',
            plugins_url( 'assets/css/singleproduktpreis.css', BOXYFY_PLUGIN_FILE ),
            array(),
            '1.0.0'
        );
    }
}
add_action( 'admin_enqueue_scripts', 'boxyfy_enqueue_singleproduktpreis_css' );

/**
 * Enqueue CSS for the Amazon Image Metabox.
 *
 * Loads the external CSS file for the Amazon Image Metabox in the admin area
 * for posts of type TSProdukte.
 *
 * @param string $hook_suffix The current admin page hook suffix.
 */
function boxyfy_enqueue_singleproduktimages_css( $hook_suffix ) {
    global $post;
    if ( isset( $post->post_type ) && 'boxyfy_tsprodukte' === $post->post_type ) {
        wp_enqueue_style(
            'boxyfy-singleproduktimages-css',
            plugins_url( 'assets/css/singleproduktimages.css', BOXYFY_PLUGIN_FILE ),
            array(),
            '1.0.0'
        );
    }
}
add_action( 'admin_enqueue_scripts', 'boxyfy_enqueue_singleproduktimages_css' );

/**
 * Enqueue CSS for the Product Details Metabox.
 *
 */
function boxyfy_enqueue_metabox_productdetails_style( $hook ) {
	global $post;
	if ( isset( $post ) && 'boxyfy_tsprodukte' === get_post_type( $post ) ) {
		wp_enqueue_style(
			'boxyfy-metabox-productdetails-style',
			plugins_url( 'assets/css/metabox-productdetails.css', BOXYFY_PLUGIN_FILE ),
			array(),
			'1.0.0'
		);
	}
}
add_action( 'admin_enqueue_scripts', 'boxyfy_enqueue_metabox_productdetails_style' );


/**
 * Enqueue CSS for the Boxyfy Welcome Screen.
 *
 */
add_action( 'admin_enqueue_scripts', 'boxyfy_enqueue_welcome_style' );
function boxyfy_enqueue_welcome_style( $hook ) {
	if ( isset( $_GET['page'] ) && 'boxyfy-welcome' === $_GET['page'] ) {
		wp_enqueue_style(
			'boxyfy-welcome-style',
			plugin_dir_url( BOXYFY_PLUGIN_FILE ) . 'assets/css/boxyfy-welcome-screen.css',
			array(),
			'1.0.0'
		);
	}
}


/* =============================================================================
 *                             FRONTEND ENQUEUE SCRIPTS
 * ============================================================================= */

/**
 * Enqueue Font Awesome for all Boxyfy pages and product pages.
 */
function boxyfy_enqueue_fontawesome() {
    // Check if Font Awesome is already enqueued to avoid duplication.
    if ( ! wp_style_is( 'boxyfy-fontawesome', 'enqueued' ) ) {
        wp_enqueue_style(
            'boxyfy-fontawesome',
            plugins_url( 'assets/vendor/fontawesome/all.min.css', BOXYFY_PLUGIN_FILE ),
            [],
            '6.7.2'
        );
    }
}
add_action( 'wp_enqueue_scripts', 'boxyfy_enqueue_fontawesome' );
add_action( 'admin_enqueue_scripts', 'boxyfy_enqueue_fontawesome' );

/**
 * Enqueue static CSS + dynamic inline CSS for the Amazon product box.
 */
function boxyfy_amazon_product_box_enqueue_styles() {

	/* ---------- statisches CSS ---------- */
	wp_enqueue_style(
		'amazon-product-box-css',
		plugins_url( 'assets/css/amazon-produktbox-simple.css', BOXYFY_PLUGIN_FILE ),
		array(),
		'1.0.0'
	);

	/* ---------- Optionen sicher abrufen ---------- */
	$product_amz_einzelbox_title_fontsize                 = absint( get_option( 'boxyfy_amz_einzelbox_title_fontsize', 20 ) );
	$product_amz_einzelbox_title_schriftcolor             = sanitize_hex_color( get_option( 'boxyfy_amz_einzelbox_title_schriftcolor', '#545353' ) );
	$product_amz_einzelbox_liste_fontsize                 = absint( get_option( 'boxyfy_amz_einzelbox_liste_fontsize', 16 ) );
	$product_amz_einzelbox_badge_backgroundcolor          = sanitize_hex_color( get_option( 'boxyfy_amz_einzelbox_badge_backgroundcolor', '#da1001' ) );
	$product_amz_einzelbox_taglabel_fontcolor             = sanitize_hex_color( get_option( 'boxyfy_amz_einzelbox_taglabel_fontcolor', '#ffffff' ) );
	$product_amz_einzelbox_taglabel_backgroundcolor       = sanitize_hex_color( get_option( 'boxyfy_amz_einzelbox_taglabel_backgroundcolor', '#0281af' ) );
	$product_amz_einzelbox_taglabel_fontsize              = absint( get_option( 'boxyfy_amz_einzelbox_taglabel_fontsize', 13 ) );
	$product_amz_einzelbox_erster_button_backgroundcolor  = sanitize_hex_color( get_option( 'boxyfy_amz_einzelbox_erster_button_backgroundcolor', '#098139' ) );
	$product_amz_einzelbox_erster_button_schriftcolor     = sanitize_hex_color( get_option( 'boxyfy_amz_einzelbox_erster_button_schriftcolor', '#ffffff' ) );
	$product_amz_einzelbox_zweiter_button_backgroundcolor = sanitize_hex_color( get_option( 'boxyfy_amz_einzelbox_zweiter_button_backgroundcolor', '#dddddd' ) );
	$product_amz_einzelbox_zweiter_button_schriftcolor    = sanitize_hex_color( get_option( 'boxyfy_amz_einzelbox_zweiter_button_schriftcolor', '#333333' ) );

	/* ---------- dynamisches CSS ---------- */
	$dynamic_css = "
    /* Discount-Badge */
    .boxyfy-simpleboxes .amazon-product-box-simple1 .amazon-discount-badge,
    .boxyfy-simpleboxes .amazon-product-box-simple2 .amazon-discount-badge2 {
        background-color: {$product_amz_einzelbox_badge_backgroundcolor} !important;
    }

    /* Label-Badges */
    .boxyfy-simpleboxes .tsprodukt-amazon-product-details .amz-simple-label-tsbox {
        background-color: {$product_amz_einzelbox_taglabel_backgroundcolor} !important;
        color: {$product_amz_einzelbox_taglabel_fontcolor};
        font-size: {$product_amz_einzelbox_taglabel_fontsize}px;
    }

    /* Listenpunkte */
    .boxyfy-simpleboxes .amazon-product-box-simple1 li,
    .boxyfy-simpleboxes .amazon-product-box-simple2 li {
        font-size: {$product_amz_einzelbox_liste_fontsize}px !important;
    }

    /* Titel-DIV (<div class=\"amazon-product-title-simpleX\">) */
    .boxyfy-simpleboxes .amazon-product-box-simple1 .amazon-product-title-simple1,
    .boxyfy-simpleboxes .amazon-product-box-simple2 .amazon-product-title-simple2 {
        font-size: {$product_amz_einzelbox_title_fontsize}px !important;
        color: {$product_amz_einzelbox_title_schriftcolor};
    }

    /* Haupt-Button */
    .boxyfy-simpleboxes .amazon-product-box-simple1 .amazon-produkt-tsbuttons-zwei a,
    .boxyfy-simpleboxes .amazon-product-box-simple2 .amazon-produkt-tsbuttons-zwei a {
        background-color: {$product_amz_einzelbox_erster_button_backgroundcolor} !important;
        color: {$product_amz_einzelbox_erster_button_schriftcolor} !important;
    }

    /* Extra-Button */
    .boxyfy-simpleboxes .amazon-produkt-tsbuttons-zwei a.extra-button {
        background-color: {$product_amz_einzelbox_zweiter_button_backgroundcolor} !important;
        color: {$product_amz_einzelbox_zweiter_button_schriftcolor} !important;
    }
";
	wp_add_inline_style( 'amazon-product-box-css', $dynamic_css );
}
add_action( 'wp_enqueue_scripts', 'boxyfy_amazon_product_box_enqueue_styles' );

/**
 * Enqueue the Amazon product box styles only if the shortcode is found in the post content.
 */
function boxyfy_maybe_enqueue_amazon_product_box_styles( $posts ) {
	if ( empty( $posts ) ) {
		return $posts;
	}

	foreach ( $posts as $post ) {
		if ( has_shortcode( $post->post_content, 'boxyfy_amazon_productbox_small' ) ) {
			// Wenn Shortcode erkannt, lade deine CSS-Funktion
			add_action( 'wp_enqueue_scripts', 'boxyfy_amazon_product_box_enqueue_styles' );
			break;
		}
	}

	return $posts;
}
add_filter( 'the_posts', 'boxyfy_maybe_enqueue_amazon_product_box_styles' );

/**
 * Enqueues static CSS and dynamic inline CSS for the Amazon product slider.
 */
function boxyfy_amazon_product_slider_enqueue_styles() {

	wp_enqueue_style(
		'amazon-product-slider-css',
		plugins_url( 'assets/css/amazon-produktbox-slider.css', BOXYFY_PLUGIN_FILE ),
		array(),
		'1.0.0'
	);

	$product_amz_slider_label_fontcolor             = sanitize_hex_color( get_option( 'boxyfy_amazon_slider_reduce_label_fontcolor', '#ffffff' ) );
	$product_amz_slider_button_and_pricereduce_fontcolor = sanitize_hex_color( get_option( 'boxyfy_amazon_slider_pricereduce_and_button_fontcolor', '#ffffff' ) );
	$product_amz_slider_label_backgroundcolor       = sanitize_hex_color( get_option( 'boxyfy_amazon_slider_reduce_label_color', '#ea0000' ) );
	$product_amz_slider_button_and_pricereduce_color = sanitize_hex_color( get_option( 'boxyfy_amazon_slider_pricereduce_and_button_color', '#0173ab' ) );

	$dynamic_css = "
		.boxyfy-sliderboxes .tsamazon-products-slider-wrapper .slider-ribbon.style1{ --c:" . esc_attr( $product_amz_slider_label_backgroundcolor ) . "!important; }
		.boxyfy-sliderboxes .tsamazon-products-slider-wrapper .slider-ribbon.style2{
			background-color:" . esc_attr( $product_amz_slider_label_backgroundcolor ) . "!important;
			color:" . esc_attr( $product_amz_slider_label_fontcolor ) . "!important;
		}
		.boxyfy-sliderboxes .tsamazon-products-slider-wrapper .slider-ribbon.style1{ color:" . esc_attr( $product_amz_slider_label_fontcolor ) . "!important; }
		.boxyfy-sliderboxes .product-add-to-cart-slider a.style1,
		.boxyfy-sliderboxes .product-add-to-cart-slider a.style2{
			background-color:" . esc_attr( $product_amz_slider_button_and_pricereduce_color ) . "!important;
			color:" . esc_attr( $product_amz_slider_button_and_pricereduce_fontcolor ) . "!important;
		}
		.boxyfy-sliderboxes .pagination-dot.active{ background-color:" . esc_attr( $product_amz_slider_button_and_pricereduce_color ) . "!important; }
	";
	wp_add_inline_style( 'amazon-product-slider-css', $dynamic_css );
}
add_action( 'wp_enqueue_scripts', 'boxyfy_amazon_product_slider_enqueue_styles' );

/**
 * Enqueues the Swiper.js stylesheet for the Amazon product slider.
 */
function boxyfy_enqueue_swiper_styles_if_shortcode_found( $posts ) {
    if ( empty( $posts ) ) {
        return $posts;
    }

    $shortcode_found = false;

    foreach ( $posts as $post ) {
        if ( has_shortcode( $post->post_content, 'boxyfy_amazon_products_slider' ) ) {
            $shortcode_found = true;
            break;
        }
    }

    if ( $shortcode_found ) {
        wp_enqueue_style(
            'swiper-css',
            plugins_url( 'assets/vendor/swiper/swiper-bundle.min.css', BOXYFY_PLUGIN_FILE ),
            array(),
            '9.0.0'
        );
    }

    return $posts;
}
add_filter( 'the_posts', 'boxyfy_enqueue_swiper_styles_if_shortcode_found' );

/**
 * Enqueue styles for the Amazon Bestseller List Shortcode.
 */
function boxyfy_amazon_bestseller_table2_enqueue_styles() {

	wp_enqueue_style(
		'amazon-bestseller-table2-css',
		plugins_url( 'assets/css/amazon-bestseller-table2.css', BOXYFY_PLUGIN_FILE ),
		array(),
		'1.0.0'
	);

	$product_amz_bestseller_boxes_titlecolor                    = sanitize_hex_color( get_option( 'boxyfy_amazon_bestseller_boxes_titlecolor', '#0173ab' ) );
	$product_amz_bestseller_boxes_colorofribbon                 = sanitize_hex_color( get_option( 'boxyfy_amazon_bestseller_boxes_colorofribbon', '#e00000' ) );
	$product_amz_bestseller_boxes_background_first_button       = sanitize_hex_color( get_option( 'boxyfy_amazon_bestseller_boxes_background_first_button', '#0173ab' ) );
	$product_amz_bestseller_boxes_font_size_title               = absint( get_option( 'boxyfy_amazon_bestseller_boxes_font_size_title', 20 ) );
	$product_amz_bestseller_boxes_font_size_listpoints          = absint( get_option( 'boxyfy_amazon_bestseller_boxes_font_size_listpoints', 15 ) );
	$product_amz_bestseller_boxes_buttontextcolor_first_button  = sanitize_hex_color( get_option( 'boxyfy_amazon_bestseller_boxes_buttontextcolor_first_button', '#ffffff' ) );
	$product_amz_bestseller_boxes_background_second_button      = sanitize_hex_color( get_option( 'boxyfy_amazon_bestseller_boxes_background_second_button', '#dddddd' ) );
	$product_amz_bestseller_boxes_buttontextcolor_second_button = sanitize_hex_color( get_option( 'boxyfy_amazon_bestseller_boxes_buttontextcolor_second_button', '#333333' ) );

	$dynamic_css = "
		.boxyfy-amz-bestseller-liste .ranking-label-box-zwei{ --c:" . esc_attr( $product_amz_bestseller_boxes_colorofribbon ) . "; 
		}
		.boxyfy-amz-bestseller-liste .amazon-bestseller-product-boxes-list-zwei .amazon-product-box-zwei{ 
			font-size:" . esc_attr( $product_amz_bestseller_boxes_font_size_title ) . "px!important;
			color:" . esc_attr( $product_amz_bestseller_boxes_titlecolor ) . "!important;
		}
		.boxyfy-amz-bestseller-liste .amazon-bestseller-product-boxes-list-zwei .amazon-product-box-zwei li{ 
			font-size:" . esc_attr( $product_amz_bestseller_boxes_font_size_listpoints ) . "px!important; 
		}
		.boxyfy-amz-bestseller-liste .amazon-bestseller-product-boxes-list-zwei .amazon-product-button-zwei{
			background-color:" . esc_attr( $product_amz_bestseller_boxes_background_first_button ) . "!important;
			color:" . esc_attr( $product_amz_bestseller_boxes_buttontextcolor_first_button ) . "!important;
		}
		.boxyfy-amz-bestseller-liste .amazon-bestseller-product-boxes-list-zwei .amazon-cart-button-zwei{
			background-color:" . esc_attr( $product_amz_bestseller_boxes_background_second_button ) . "!important;
			color:" . esc_attr( $product_amz_bestseller_boxes_buttontextcolor_second_button ) . "!important;
		}
	";
	wp_add_inline_style( 'amazon-bestseller-table2-css', $dynamic_css );
}
add_action( 'wp_enqueue_scripts', 'boxyfy_amazon_bestseller_table2_enqueue_styles' );

/**
 * Enqueue static CSS + dynamic inline CSS for the small Amazon product box.
 */
function boxyfy_amazon_product_box_small_enqueue_styles() {

	wp_enqueue_style(
		'amazon-produktbox-small-css',
		plugins_url( 'assets/css/amazon-produktbox-small.css', BOXYFY_PLUGIN_FILE ),
		array(),
		'1.0.0'
	);

	$product_amz_einzelbox_klein_label_backcolor       = sanitize_hex_color( get_option( 'boxyfy_amz_einzelbox_klein_label_backcolor', '#e32222' ) );
	$product_amz_einzelbox_klein_title_fontsize			= absint( get_option( 'boxyfy_amz_einzelbox_klein_title_fontsize', 17 ) );
	$product_amz_einzelbox_klein_hinweis_fontsize		= $product_amz_einzelbox_klein_title_fontsize - 2;
	$product_amz_einzelbox_klein_title_fontweight = absint( get_option( 'boxyfy_amz_einzelbox_title_klein_fontweight', 500 ) );
	$product_amz_einzelbox_klein_title_color           = sanitize_hex_color( get_option( 'boxyfy_amz_einzelbox_title_klein_schriftcolor', '#0173ab' ) );
	$product_amz_einzelbox_klein_title_hovercolor      = sanitize_hex_color( get_option( 'boxyfy_amz_einzelbox_klein_schriftcolor_hover', '#013559' ) );
	$product_amz_einzelbox_klein_bordercolor           = sanitize_hex_color( get_option( 'boxyfy_amz_einzelbox_klein_bordercolor', '#ddd' ) );
	$product_amz_einzelbox_klein_buttonbackgroundcolor = sanitize_hex_color( get_option( 'boxyfy_amz_einzelbox_klein_button_backcolor', '#014d73' ) );

	$product_amz_einzelbox_klein_title_fontsize_mobile = max( 1, $product_amz_einzelbox_klein_title_fontsize - 2 );

	$dynamic_css = "
		.boxyfy-productbox-alert .amazon-product-box-small{ 
			border:1px solid " . esc_attr( $product_amz_einzelbox_klein_bordercolor ) . "!important; 
		}
		.discount-percentage-box,
		.discount-percentage-box.style2,
		.discount-percentage-box.style3,
		.discount-percentage-box.style4{ 
			background:" . esc_attr( $product_amz_einzelbox_klein_label_backcolor ) . "!important; 
		}
		.boxyfy-productbox-alert .boxyfy-smallbox-title {
			font-size: " . esc_attr( $product_amz_einzelbox_klein_title_fontsize ) . "px !important;
			font-weight: " . esc_attr( $product_amz_einzelbox_klein_title_fontweight ) . " !important;
		}
		 .boxyfy-productbox-alert .amz-preiswecker-label {
		 	font-size: " . esc_attr( $product_amz_einzelbox_klein_hinweis_fontsize ) . "px !important;
			font-weight: " . esc_attr( $product_amz_einzelbox_klein_title_fontweight ) . " !important;
		 }
		.boxyfy-productbox-alert .boxyfy-smallbox-title a {
			color: " . esc_attr( $product_amz_einzelbox_klein_title_color ) . " !important;
		}
		.boxyfy-productbox-alert .boxyfy-smallbox-title a:hover {
			color: " . esc_attr( $product_amz_einzelbox_klein_title_hovercolor ) . " !important;
		}
		.boxyfy-productbox-alert .amz-preiswecker-toggle-btn {
			background: " . esc_attr( $product_amz_einzelbox_klein_buttonbackgroundcolor ) . " !important;
		}
		@media (max-width: 510px) {
		.boxyfy-productbox-alert .boxyfy-smallbox-title {
			font-size: " . esc_attr( $product_amz_einzelbox_klein_title_fontsize_mobile ) . "px !important;
		}
	}
	";
	wp_add_inline_style( 'amazon-produktbox-small-css', $dynamic_css );
}
add_action( 'wp_enqueue_scripts', 'boxyfy_amazon_product_box_small_enqueue_styles' );

/**
 * Enqueue static CSS + dynamic inline CSS for the Amazon bestseller table.
 */
function boxyfy_amazon_bestseller_table_enqueue_styles() {

	wp_enqueue_style(
		'amazon-bestseller-table-css',
		plugins_url( 'assets/css/amazon-bestseller-table.css', BOXYFY_PLUGIN_FILE ),
		array(),
		'1.0.0'
	);
	
	$boxyfy_amazon_besttable_main_fontsize		 = absint( get_option( 'boxyfy_amz_besttable_main_fontsize', 15 ) );
	$boxyfy_amazon_besttable_main_fontsize_mobile = $boxyfy_amazon_besttable_main_fontsize + 2;
	$boxyfy_amazon_besttable_headbackgroundcolor = sanitize_hex_color( get_option( 'boxyfy_amz_besttable_headbackground_color', '#dddddd' ) );
	$boxyfy_amazon_besttable_maincolor           = sanitize_hex_color( get_option( 'boxyfy_amz_besttable_headlink_color', '#000000' ) );
	$boxyfy_amazon_besttable_button_backcolor    = sanitize_hex_color( get_option( 'boxyfy_amz_besttable_button_backcolor', '#ededed' ) );
	$boxyfy_amazon_besttable_iconfont_color      = sanitize_hex_color( get_option( 'boxyfy_amz_besttable_iconfont_color', '#000000' ) );

	$dynamic_css = "
	
		.boxyfy-amz-table .amazon-bestseller-liste-table3 tr a, 
		.boxyfy-amz-table .amazon-bestseller-liste-table3 td:nth-child(1) {
			font-size: " . esc_attr( $boxyfy_amazon_besttable_main_fontsize ) . "px;
		}
					
		.boxyfy-amz-table .amazon-bestseller-liste-table3 thead tr th{
			background-color:" . esc_attr( $boxyfy_amazon_besttable_headbackgroundcolor ) . "!important;
			color:" . esc_attr( $boxyfy_amazon_besttable_maincolor ) . "!important;
			border:1px solid " . esc_attr( $boxyfy_amazon_besttable_headbackgroundcolor ) . "!important;
		}
		.boxyfy-amz-table .tssort-btn,.tsreset-btn{ color:" . esc_attr( $boxyfy_amazon_besttable_maincolor ) . "!important; }
		.boxyfy-amz-table .tsreset-btn{ font-size:26px; }
		.boxyfy-amz-table .amazon-bestseller-liste-table3 td:nth-child(5) a{
			background-color:" . esc_attr( $boxyfy_amazon_besttable_button_backcolor ) . "!important;
			color:" . esc_attr( $boxyfy_amazon_besttable_iconfont_color ) . "!important;
		}
		.boxyfy-amz-table .amazon-bestseller-liste-table3 td:nth-child(5) a:hover{ 
			filter:brightness(.62); 
		}
		.boxyfy-amz-table .sort-controls-mobile button {
			background-color:" . esc_attr( $boxyfy_amazon_besttable_headbackgroundcolor ) . "!important;
			color:" . esc_attr( $boxyfy_amazon_besttable_maincolor ) . "!important;
		}
		@media(max-width:500px){
			.boxyfy-amz-table .amazon-bestseller-liste-table3 td:nth-child(1) {
				font-size: " . esc_attr( $boxyfy_amazon_besttable_main_fontsize_mobile ) . "px !important;
			}
		}
	";
	wp_add_inline_style( 'amazon-bestseller-table-css', $dynamic_css );
}
add_action( 'wp_enqueue_scripts', 'boxyfy_amazon_bestseller_table_enqueue_styles' );

/**
 * Enqueue static CSS + dynamic inline CSS for the Single TSProdukt template.
 */
function boxyfy_enqueue_single_tsprodukt_styles() {
	if ( ! is_singular( 'boxyfy_tsprodukte' ) ) { return; }

	wp_enqueue_style(
		'boxyfy-single-tsprodukt-css',
		plugins_url( 'assets/css/single-tsprodukt.css', BOXYFY_PLUGIN_FILE ),
		array(),
		'1.0.0'
	);

	$title_font_size                              = absint( get_option( 'boxyfy_title_font_size', 24 ) );
	$title_font_size_mobile                       = max( 1, $title_font_size - 4 );
	$tsproduct_main_imageslider_dot_color         = sanitize_hex_color( get_option( 'boxyfy_main_imageslider_dot_color', '#c1e0ff' ) );
	$tsproduct_main_imageslider_dot_active_color  = sanitize_hex_color( get_option( 'boxyfy_main_imageslider_dot_active_color', '#0173ab' ) );
	$product_backgroundcolor_labels_top           = sanitize_hex_color( get_option( 'boxyfy_content_labeltop_background_color', '#0173ab' ) );
	$product_fontcolor_labels_top                 = sanitize_hex_color( get_option( 'boxyfy_content_labeltop_font_color', '#ffffff' ) );
	$content_font_size                            = absint( get_option( 'boxyfy_content_font_size', 15 ) );
	$product_content_background_color             = sanitize_hex_color( get_option( 'boxyfy_content_background_color', '#f9f9f9' ) );

	$product_info_top_font_size                   = absint( get_option( 'boxyfy_produktinfos_oben_font_size', 16 ) );
	$product_rankingplace_font_size               = $product_info_top_font_size + 1;
	$product_fontsize_other_rankings              = max( 1, $product_info_top_font_size - 2 );
	$product_fontsize_further_rank_infos          = max( 1, $product_info_top_font_size - 1 );

	$product_info_top_maincolor                   = sanitize_hex_color( get_option( 'boxyfy_produktinfos_oben_font_maincolor', '#000000' ) );
	$product_infolabels_top_fontweight            = absint( get_option( 'boxyfy_produktinfos_head_fontweight', 600 ) );
	$product_amazon_preis_zeile_background_color  = sanitize_hex_color( get_option( 'boxyfy_amazon_preis_zeile_background_color', '#ffffff' ) );
	$product_weitere_preise_zeilen_background_color = sanitize_hex_color( get_option( 'boxyfy_weitere_preise_zeilen_background_color', '#ffffff' ) );
	$product_all_buttons_icon_and_fontcolor       = sanitize_hex_color( get_option( 'boxyfy_all_buttons_icon_and_fontcolor', '#ffffff' ) );
	$product_box_lowest_price_backgroundcolor     = sanitize_hex_color( get_option( 'boxyfy_box_lowest_price_backgroundcolor', '#ededed' ) );
	$product_box_lowest_price_font_and_border     = sanitize_hex_color( get_option( 'boxyfy_box_lowest_price_border_and_font', '#0173ab' ) );
	$tsproduct_autorbox_expertise_tag             = sanitize_hex_color( get_option( 'boxyfy_autorbox_expertise_tag', '#0173ab' ) );
	$content_font_size_list                       = $content_font_size + 3;
	$button_color                                 = sanitize_hex_color( get_option( 'boxyfy_preise_buttons_background_color', '#016698' ) );
	
	$boxyfy_single_product_star_color 			  = sanitize_hex_color( get_option( 'boxyfy_default_star_color', '#FFD700' ) );
	$boxyfy_single_product_star_size = sanitize_text_field( get_post_meta( get_the_ID(), 'tsprodukte_star_rating_size', true ) );
    $boxyfy_single_product_star_size = $boxyfy_single_product_star_size ?: '20';
	$button_gradient                              = sanitize_text_field( get_option( 'boxyfy_preise_buttons_gradient' ) );
	$button_style                                 = $button_gradient ? 'background:' . esc_attr( $button_gradient ) . ';'
	                                                                : 'background-color:' . esc_attr( $button_color ) . ';';

	$dynamic_css = "
		@media(max-width:600px){
			.tsprodukt-content-h1{ 
				font-size:" . esc_attr( $title_font_size_mobile ) . "px!important;
				margin:48px 25px 20px 15px; }
		}
		.ts-main .tsprodukt-label{ 
			background:" . esc_attr( $product_backgroundcolor_labels_top ) . ";
			color:" . esc_attr( $product_fontcolor_labels_top ) . "; }
		.tsprodukt-content-h1{ 
			font-size:" . esc_attr( $title_font_size ) . "px; }
		.tsprodukt-pagination .dot{ 
			background:" . esc_attr( $tsproduct_main_imageslider_dot_color ) . "; }
		.tsprodukt-pagination .dot.active{ 
			background:" . esc_attr( $tsproduct_main_imageslider_dot_active_color ) . "; }
		.tsprodukt-post-content{ 
			background:" . esc_attr( $product_content_background_color ) . ";
			font-size:" . esc_attr( $content_font_size ) . "px; }
		.tsplatz-noten-container span{ 
			font-size:" . esc_attr( $product_rankingplace_font_size ) . "px;
			color:" . esc_attr( $product_info_top_maincolor ) . "; }
		.produktseite-ranking-ausgabe-container{ 
			font-size:" . esc_attr( $product_info_top_font_size ) . "px;
			color:" . esc_attr( $product_info_top_maincolor ) . "; }
		.boxyfy-rating-true-grid .boxyfy-rating-cell .tsrating,
		.boxyfy-rating-true-grid .boxyfy-rating-cell .boxyfy-rating-label,
		.boxyfy-rating-true-grid .boxyfy-rating-cell .tsverkaufsrang-label{
			font-size:" . esc_attr( $product_fontsize_further_rank_infos ) . "px;
			color:" . esc_attr( $product_info_top_maincolor ) . ";
		}
		.boxyfy-rating-true-grid .boxyfy-rating-cell .boxyfy-rating-stars {
			color:" . esc_attr( $boxyfy_single_product_star_color ) . ";
			font-size: " . esc_attr( $boxyfy_single_product_star_size ) . "px;
		}
		.boxyfy-rating-true-grid .boxyfy-rating-cell .tsverkaufsrang-wert{ 
			font-size:" . esc_attr( $product_info_top_font_size ) . "px; 
		}
		.tsproduct-tsbox-rec p{ 
			font-size:" . esc_attr( $product_fontsize_other_rankings ) . "px; }
		.tsprodukt-produktinfo-list{ 
			color:" . esc_attr( $product_info_top_maincolor ) . ";
			font-size:" . esc_attr( $product_info_top_font_size ) . "px; }
		.amazon-button-produktseite,.produkt-weitere-buttons-shops{" . $button_style . "color:" . esc_attr( $product_all_buttons_icon_and_fontcolor ) . "; }
		.guenstigster-gesamtpreis{
			background:" . esc_attr( $product_box_lowest_price_backgroundcolor ) . ";
			color:" . esc_attr( $product_box_lowest_price_font_and_border ) . ";
			border:1px solid " . esc_attr( $product_box_lowest_price_font_and_border ) . ";
		}
		.boxyfy_vorteile h2,.boxyfy_nachteile h2{ 
			font-size:" . esc_attr( $content_font_size_list ) . "px; }
		.boxyfy_vorteile,.boxyfy_nachteile{ 
			font-size:" . esc_attr( $content_font_size ) . "px; }
		.tsprodukt-autoren-info .expertise-ausgabe strong{ 
			color:" . esc_attr( $tsproduct_autorbox_expertise_tag ) . "; }
		.tsprodukt-preisvergleich-container p{
			background:" . esc_attr( $product_amazon_preis_zeile_background_color ) . ";
			color:" . esc_attr( $product_info_top_maincolor ) . ";
		}
		.produkt-button-weitere-preise{
			background:" . esc_attr( $product_weitere_preise_zeilen_background_color ) . ";
			color:" . esc_attr( $product_info_top_maincolor ) . ";
		}
		.tsprodukt-info-label{ 
			font-weight:" . esc_attr( $product_infolabels_top_fontweight ) . "; }
	";
	wp_add_inline_style( 'boxyfy-single-tsprodukt-css', $dynamic_css );
}
add_action( 'wp_enqueue_scripts', 'boxyfy_enqueue_single_tsprodukt_styles' );

/**
 * Enqueue static CSS + dynamic inline CSS for preistracker-chart/price alert.
 */
function boxyfy_enqueue_preistracker_css() {
	if ( ! is_singular( 'boxyfy_tsprodukte' ) ) { return; }

	wp_enqueue_style(
		'preistracker-single',
		plugins_url( 'assets/css/preistracker-single.css', BOXYFY_PLUGIN_FILE ),
		array(),
		'1.0.0'
	);
	
	$singleproductbox_ai_gradient_color1 = sanitize_hex_color( get_option( 'boxyfy_ai_gradient_color1', '#0fb9ad' ) );
	$singleproductbox_ai_gradient_color2 = sanitize_hex_color( get_option( 'boxyfy_ai_gradient_color2', '#6e8dec' ) );
	$product_info_top_font_size_alert     = absint( get_option( 'boxyfy_produktinfos_oben_font_size', 16 ) );
	$product_info_top_font_size_alert_top = $product_info_top_font_size_alert + 2;
	$product_price_alert_maincolor        = sanitize_hex_color( get_option( 'boxyfy_preiswecker_farben', '#0173ab' ) );
	$product_price_alert_fontcolor        = sanitize_hex_color( get_option( 'boxyfy_produktinfos_oben_font_maincolor', '#000000' ) );
	$product_price_alert_filterbuttons    = max( 1, $product_info_top_font_size_alert - 1 );

	$dynamic_css = "
		:root{
			--preiswecker-font-size-alert:" . esc_attr( $product_info_top_font_size_alert ) . "px;
			--preiswecker-font-size-alert-top:" . esc_attr( $product_info_top_font_size_alert_top ) . "px;
			--preiswecker-main-color:" . esc_attr( $product_price_alert_maincolor ) . ";
			--preiswecker-font-color:" . esc_attr( $product_price_alert_fontcolor ) . ";
		}
		.preiswecker-range-btn.active,
		.preiswecker-range-btn:hover{ background-color:" . esc_attr( $product_price_alert_maincolor ) . "; }
		.preiswecker-range-btn{
			font-size:" . esc_attr( $product_price_alert_filterbuttons ) . "px;
			border:1px solid " . esc_attr( $product_price_alert_maincolor ) . ";
		}
		
		.boxyfy-singleproduct-ai-summary {
			background: linear-gradient(141deg, " . esc_attr( $singleproductbox_ai_gradient_color1 ) . " 0%, " . esc_attr( $singleproductbox_ai_gradient_color2 ) . " 100%);
		}

	";
	wp_add_inline_style( 'preistracker-single', $dynamic_css );
}
add_action( 'wp_enqueue_scripts', 'boxyfy_enqueue_preistracker_css' );

/**
 * Enqueue static CSS + dynamic inline CSS for the Single Produkt Shortcode.
 */
function boxyfy_enqueue_single_produkt_shortcode_styles() {

	/* 1.1  Grund-CSS */
	wp_enqueue_style(
		'boxyfy-single-produkt-shortcode-css',
		plugins_url( 'assets/css/single-produkt-shortcode.css', BOXYFY_PLUGIN_FILE ),
		array(),
		'1.0.0'
	);

	/* 1.2  Optionen beziehen  */
	$box_header_font_size          = absint( get_option( 'boxyfy_box_header_font_size', 21 ) );
	$box_header_fontweights        = absint( get_option( 'boxyfy_box_header_fontweights', 600 ) );
	$box_info_font_size            = absint( get_option( 'boxyfy_box_info_font_size', 16 ) );
	$box_info_font_size_p		   = $box_info_font_size - 2;
	$box_title_color               = sanitize_hex_color( get_option( 'boxyfy_box_farbe_title', '#004e75' ) );
	$box_info_text_color           = sanitize_hex_color( get_option( 'boxyfy_box_info_text_color', '#000000' ) );
	$box_shop_button_background    = sanitize_hex_color( get_option( 'boxyfy_box_shop_button_background', '#0173ab' ) );
	$box_tsproduktlabel            = sanitize_hex_color( get_option( 'boxyfy_box_tsproduktlabel', '#0173ab' ) );
	$box_tsproduktlabel_color      = sanitize_hex_color( get_option( 'boxyfy_box_tsproduktlabel_color', '#ffffff' ) );
	$tsproduct_star_rating_size    = absint( get_post_meta( get_the_ID(), 'tsprodukte_star_rating_size', true ) ?: 20 );
	$default_star_color_shortcode  = sanitize_hex_color( get_option( 'boxyfy_default_star_color', '#FFD700' ) );

	/* 1.3  KI-Gradient-Farben */
	$grad1 = sanitize_hex_color( get_option( 'boxyfy_ai_gradient_color1', '#0fb9ad' ) );
	$grad2 = sanitize_hex_color( get_option( 'boxyfy_ai_gradient_color2', '#6e8dec' ) );

	/* 1.4  Inline-CSS zusammensetzen */
	$dynamic_css = "
		.boxyfy-rating-stars-shortcode{color:{$default_star_color_shortcode};font-size:{$tsproduct_star_rating_size}px;}
		.tsprodukt-box .tsprodukt-box-header{font-size:{$box_header_font_size}px;}
		.tsprodukt-box .tsprodukt-box-header a{
			color:{$box_title_color} !important;
			font-weight:{$box_header_fontweights} !important;
		}
		.tsprodukt-box .tsprodukt-box-info ul li {
			font-size:{$box_info_font_size}px !important;
		}
		.tsprodukt-box .tsprodukt-box-info ul li::marker,
		.tsprodukt-box-info ul strong,
		.tsprodukt-box-info ul li svg{
			color:{$box_info_text_color} !important;
		}
		.tsprodukt-weitere-preise-wrapper .tsprodukt-weitere-preise-item a{background:{$box_shop_button_background};}
		.tsprodukt-box .ts-produktlabel-einzelbox{
			background:{$box_tsproduktlabel} !important;
			color:{$box_tsproduktlabel_color} !important;
		}
		.boxyfy-singleproduct-ai-accordion .boxyfy-singleproduct-ai-chatbox span,
		.boxyfy-singleproduct-ai-button,
		.boxyfy-chat-question-button,
		.boxyfy-singleproduct-ai-submit,
		.boxyfy-singleproduct-ai-response{
			font-size:{$box_info_font_size}px !important;
		}
		/* KI-Accordion-Verlauf */
		.boxyfy-singleproduct-ai-summary{
			background:linear-gradient(141deg,{$grad1} 0%,{$grad2} 100%);
		}
		.boxyfy-singleproduct-ai-chatbox p {
			font-size:{$box_info_font_size_p}px !important;
		}
	";
	wp_add_inline_style( 'boxyfy-single-produkt-shortcode-css', $dynamic_css );
}

/* 2)  Prüfen, ob der Shortcode im Beitrag vorkommt – nur dann Styles laden */
function boxyfy_maybe_enqueue_single_produkt_shortcode_styles( $posts ) {

	if ( empty( $posts ) ) { return $posts; }

	foreach ( $posts as $post ) {
		if ( has_shortcode( $post->post_content, 'boxyfy_produktbox' ) ) {
			/* Styles beim Frontend-Hook anhängen */
			add_action( 'wp_enqueue_scripts', 'boxyfy_enqueue_single_produkt_shortcode_styles' );
			break;
		}
	}
	return $posts;
}
add_filter( 'the_posts', 'boxyfy_maybe_enqueue_single_produkt_shortcode_styles' );

/* =======================================================================
 *  Boxyfy – CSS für die Vergleichstabelle/Filter
 * =====================================================================*/

/**
 * Enqueue CSS (statisch + Inline-Variablen).
 */
function boxyfy_enqueue_produkt_vergleichstabelle_filter_css() {

	/* --- statisches CSS --- */
	wp_enqueue_style(
		'boxyfy-produkts-vergleichstabelle-filter-css',
		plugins_url( 'assets/css/singleproduktvergleichtabellefilter.css', BOXYFY_PLUGIN_FILE ),
		[], '1.0.0'
	);

	/* --- Optionen laden (unverändert) --- */
	$product_boxes_compare_filterhead_fontcolor       = sanitize_hex_color( get_option( 'boxyfy_vergleich_boxen_filterhead_schriftcolor', '#000000' ) );
	$product_boxes_compare_filterhead_backgroundcolor = sanitize_hex_color( get_option( 'boxyfy_vergleich_boxen_filterhead_background', '#ffffff' ) );
	$product_boxes_compare_filter_backgroundcolor     = sanitize_hex_color( get_option( 'boxyfy_vergleich_boxen_filter_background', '#ffffff' ) );
	$product_boxes_compare_fontsize_listen_slider_header       = absint( get_option( 'boxyfy_vergleich_boxen_listen_slider_kopf_fontsize', 16 ) );
	$product_boxes_compare_fontsize_listentries       = absint( get_option( 'boxyfy_vergleich_boxen_listen_entries_fontsize', 14 ) );
	$product_boxes_compare_schriftcolor_listentries   = sanitize_hex_color( get_option( 'boxyfy_vergleich_boxen_listen_entries_schriftcolor', '#000000' ) );
	$product_boxes_compare_fontcolor_listenheader     = sanitize_hex_color( get_option( 'boxyfy_vergleich_boxen_listen_slider_kopf_schriftcolor', '#deeafc' ) );	
	$product_boxes_compare_hovercolor_listentries     = sanitize_hex_color( get_option( 'boxyfy_vergleich_boxen_listen_entries_hovercolor', '#deeafc' ) );
	$product_boxes_compare_slider_background          = sanitize_hex_color( get_option( 'boxyfy_vergleich_boxen_slider_background', '#C7DAFF' ) );
	$product_boxes_compare_slider_kreis_background    = sanitize_hex_color( get_option( 'boxyfy_vergleich_boxen_slider_kreis_background', '#0173ab' ) );
	$product_boxes_compare_ranking_background         = sanitize_hex_color( get_option( 'boxyfy_vergleich_boxen_rankinglabel_background', '#f0f0f0' ) );
	$product_boxes_compare_ranking_fontcolor          = sanitize_hex_color( get_option( 'boxyfy_vergleich_boxen_rankinglabel_fontcolor', '#ffffff' ) );
	$product_boxes_compare_tsproductlabel             = sanitize_hex_color( get_option( 'boxyfy_vergleich_boxen_tsproduktlabel', '#0173ab' ) );
	$tstitle_vergleich_boxen_title_fontsize           = absint( get_option( 'boxyfy_vergleich_boxen_title_fontsize', 13 ) );
	$tslabels_vergleich_boxen_labels_fontsize         = absint( get_option( 'boxyfy_vergleich_boxen_labels_fontsize', 13 ) );
	$tsvornach_vergleich_boxen_fontsize               = absint( get_option( 'boxyfy_vergleich_boxen_vornach_fontsize', 13 ) );

	/* --- Inline-CSS zusammenbauen --- */
	$dynamic_css = "
	
		.filter-checkbox:checked,
		.deal-filter-checkbox:checked {
			background-color: " . esc_attr( $product_boxes_compare_fontcolor_listenheader ) . ";
			border: 1px solid " . esc_attr( $product_boxes_compare_fontcolor_listenheader ) . ";
		}
		.filter-header .sort-button,
		.filter-and-products-wrapper .filter-header .accordion {
			color: " . esc_attr( $product_boxes_compare_filterhead_fontcolor ) . ";
			background-color: " . esc_attr( $product_boxes_compare_filterhead_backgroundcolor ) . ";
		}
		.filter-header .sort-button:hover,
		.filter-and-products-wrapper .filter-header .accordion:hover {
			background-color: " . esc_attr( $product_boxes_compare_filterhead_backgroundcolor ) . ";
		}
		.filter-and-products-wrapper .filter-container,
		.ts-main .filter-and-products-wrapper .accordion-content {
			background: " . esc_attr( $product_boxes_compare_filter_backgroundcolor ) . ";
		}
		.filter-and-products-wrapper .listen-liste strong,
		.filter-and-products-wrapper .slider-label strong {
			color: " . esc_attr( $product_boxes_compare_fontcolor_listenheader ) . ";
			font-size: " . esc_attr( $product_boxes_compare_fontsize_listen_slider_header ) . "px;
		}
		.filter-and-products-wrapper .listen-liste ul li {
    		font-size: " . esc_attr( $product_boxes_compare_fontsize_listentries ) . "px !important;
    		color: " . esc_attr( $product_boxes_compare_schriftcolor_listentries ) . " !important;
		}
		.filter-and-products-wrapper .listen-liste ul li label, 
		.filter-and-products-wrapper .listen-liste ul li span:not(.tsfilter-count) {
    		font-size: " . esc_attr( $product_boxes_compare_fontsize_listentries ) . "px !important;
    		color: " . esc_attr( $product_boxes_compare_schriftcolor_listentries ) . " !important;
		}
		.filter-and-products-wrapper .listen-liste ul li:hover {
			background: " . esc_attr( $product_boxes_compare_hovercolor_listentries ) . ";
		}
		.filter-and-products-wrapper .range-slider {
			background: " . esc_attr( $product_boxes_compare_slider_background ) . ";
		}
		.filter-and-products-wrapper .range-slider input[type='range']::-webkit-slider-thumb {
			background: " . esc_attr( $product_boxes_compare_slider_kreis_background ) . ";
		}
		.produkt-box .produkt-ranking-compare {
			background: " . esc_attr( $product_boxes_compare_ranking_background ) . ";
			color: " . esc_attr( $product_boxes_compare_ranking_fontcolor ) . ";
		}
		.produkt-box .produkt-deal-ribbon {
			font-size: " . esc_attr( $tslabels_vergleich_boxen_labels_fontsize ) . "px;
		}
		.produkt-label-tsboxes {
			background: " . esc_attr( $product_boxes_compare_tsproductlabel ) . ";
			font-size: " . esc_attr( $tslabels_vergleich_boxen_labels_fontsize ) . "px;
		}
		.produkt-box .produkt-titel {
			font-size: " . esc_attr( $tstitle_vergleich_boxen_title_fontsize ) . "px;
		}
		.filter-and-products-wrapper .produkt-container .produkt-box .produkt-details .box-pro-kontra-list li,
		.filter-and-products-wrapper .produkt-container .produkt-box .produkt-details .box-pro-kontra-list li span {
    		font-size: " . esc_attr( $tsvornach_vergleich_boxen_fontsize ) . "px !important;
		}
		.reset-filter-button:hover {
			background-color: " . esc_attr( $product_boxes_compare_filterhead_backgroundcolor ) . ";
		}
		.filter-and-products-wrapper .button-group .close-filter-button {
    		background-color: #fff;
    		color: " . esc_attr( $product_boxes_compare_filterhead_backgroundcolor ) . ";
    		border: 1px solid " . esc_attr( $product_boxes_compare_filterhead_backgroundcolor ) . ";
		}
		.filter-and-products-wrapper .button-group .close-filter-button:hover {
			background-color: " . esc_attr( $product_boxes_compare_filterhead_backgroundcolor ) . ";
			color: #fff;
		}
	";
	wp_add_inline_style( 'boxyfy-produkts-vergleichstabelle-filter-css', $dynamic_css );
}

/* 1)  Automatisch auf Single-TSProdukt-Seiten ---------------------------*/
add_action( 'wp_enqueue_scripts', function () {
	if ( is_singular( 'boxyfy_tsprodukte' ) ) {
		boxyfy_enqueue_produkt_vergleichstabelle_filter_css();
	}
}, 20 );

/* 2)  Zusätzlich bei Shortcode [boxyfy_produkte_vergleich] --------------*/
add_filter( 'the_posts', function ( $posts ) {

	if ( empty( $posts ) ) { return $posts; }

	foreach ( $posts as $post ) {
		if ( has_shortcode( $post->post_content, 'boxyfy_produkte_vergleich' ) ) {
			add_action( 'wp_enqueue_scripts', 'boxyfy_enqueue_produkt_vergleichstabelle_filter_css', 99 );
			break;
		}
	}
	return $posts;
} );