<?php
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Palace Properties Customizer Settings.
 * Registers all customizer options directly with the WP Customizer API.
 * No custom panels/sections/controls — just standard WP API for reliability.
 */
class IPP_Customizer_Settings {

    public static function init() {
        add_action( 'customize_register', array( __CLASS__, 'register' ), 20 );
        add_action( 'wp_head', array( __CLASS__, 'output_css' ), 99 );
        add_action( 'customize_controls_enqueue_scripts', array( __CLASS__, 'customizer_preview_nav' ) );
    }

    /**
     * Auto-navigate the Customizer preview to the relevant page when opening a section.
     */
    public static function customizer_preview_nav() {
        $archive_url = get_post_type_archive_link( 'palace-properties' );
        $single_url  = '';

        // Get a sample single property URL
        $sample = get_posts( array( 'post_type' => 'palace-properties', 'posts_per_page' => 1, 'post_status' => 'publish', 'fields' => 'ids' ) );
        if ( ! empty( $sample ) ) {
            $single_url = get_permalink( $sample[0] );
        }

        if ( ! $archive_url ) return;

        wp_add_inline_script( 'customize-controls', '
            (function() {
                var archiveUrl = ' . wp_json_encode( $archive_url ) . ';
                var singleUrl = ' . wp_json_encode( $single_url ?: $archive_url ) . ';

                var sectionMap = {
                    "ipp_section_colors": archiveUrl,
                    "ipp_section_buttons": singleUrl,
                    "ipp_section_typography": archiveUrl,
                    "ipp_section_cards": archiveUrl,
                    "ipp_section_archive": archiveUrl,
                    "ipp_section_single": singleUrl,
                    "ipp_section_agent": singleUrl,
                    "ipp_section_search": archiveUrl
                };

                wp.customize.bind("ready", function() {
                    Object.keys(sectionMap).forEach(function(sectionId) {
                        if (wp.customize.section(sectionId)) {
                            wp.customize.section(sectionId).expanded.bind(function(expanded) {
                                if (expanded) {
                                    wp.customize.previewer.previewUrl.set(sectionMap[sectionId]);
                                }
                            });
                        }
                    });
                });
            })();
        ' );
    }

    /**
     * All setting definitions with defaults.
     */
    public static function defaults() {
        return array(
            // Colors
            'ipp_color_primary'      => '#1a2a4a',
            'ipp_color_accent'       => '#2563eb',
            'ipp_color_accent_hover' => '#1d4ed8',
            'ipp_color_text'         => '#1f2937',
            'ipp_color_text_light'   => '#6b7280',
            'ipp_color_background'   => '#ffffff',
            'ipp_color_surface'      => '#f9fafb',
            'ipp_color_border'       => '#e5e7eb',
            'ipp_color_success'      => '#059669',
            'ipp_color_card_bg'      => '#ffffff',

            // Buttons
            'ipp_btn_bg'             => '#2563eb',
            'ipp_btn_text'           => '#ffffff',
            'ipp_btn_bg_hover'       => '#1d4ed8',
            'ipp_btn_text_hover'     => '#ffffff',
            'ipp_btn_radius'         => '8',
            'ipp_btn_padding_v'      => '12',
            'ipp_btn_padding_h'      => '24',

            // Typography
            'ipp_font_heading'       => 'inherit',
            'ipp_font_body'          => 'inherit',
            'ipp_font_size_base'     => '16',
            'ipp_heading_weight'     => '700',

            // Card Layout
            'ipp_card_radius'        => '12',
            'ipp_card_shadow'        => 'medium',
            'ipp_card_image_ratio'   => '4/3',
            'ipp_card_show_badge'    => true,
            'ipp_card_show_price'    => true,
            'ipp_card_show_address'  => true,
            'ipp_card_show_meta'     => true,
            'ipp_card_show_date'     => true,

            // Archive Layout
            'ipp_archive_columns'    => '3',
            'ipp_archive_gap'        => '24',
            'ipp_archive_per_page'   => '9',
            'ipp_archive_show_search' => true,
            'ipp_archive_show_sort'  => true,
            'ipp_archive_show_count' => true,
            'ipp_archive_show_toggle' => true,
            'ipp_archive_container_width' => '1280',

            // Single Layout
            'ipp_single_sidebar_width' => '380',
            'ipp_single_show_gallery' => true,
            'ipp_single_show_map'    => true,
            'ipp_single_show_agent'  => true,
            'ipp_single_show_share'  => true,
            'ipp_single_show_schema' => true,
            'ipp_single_show_enquiry' => true,
            'ipp_single_container_width' => '1200',

            // Agent Card
            'ipp_agent_card_bg'      => '#ffffff',
            'ipp_agent_card_radius'  => '16',
            'ipp_agent_avatar_bg'    => '#dbeafe',
            'ipp_agent_avatar_color' => '#2563eb',

            // Search Bar
            'ipp_search_bg'          => '#ffffff',
            'ipp_search_border'      => '#e5e7eb',
            'ipp_search_radius'      => '12',
            'ipp_search_input_bg'    => '#ffffff',
        );
    }

    public static function register( $wp_customize ) {
        $defaults = self::defaults();

        // ─── Panel ───────────────────────────────────────────────────
        $wp_customize->add_panel( 'ipp_panel', array(
            'title'    => __( 'Palace Properties', 'integrate-palace-properties' ),
            'priority' => 30,
        ) );

        // ─── Section: Colors ─────────────────────────────────────────
        $wp_customize->add_section( 'ipp_section_colors', array(
            'title' => __( 'Colors', 'integrate-palace-properties' ),
            'panel' => 'ipp_panel',
            'priority' => 10,
        ) );

        $colors = array(
            'ipp_color_primary'      => __( 'Primary Color', 'integrate-palace-properties' ),
            'ipp_color_accent'       => __( 'Accent / Link Color', 'integrate-palace-properties' ),
            'ipp_color_accent_hover' => __( 'Accent Hover', 'integrate-palace-properties' ),
            'ipp_color_text'         => __( 'Text Color', 'integrate-palace-properties' ),
            'ipp_color_text_light'   => __( 'Text Light / Muted', 'integrate-palace-properties' ),
            'ipp_color_background'   => __( 'Background', 'integrate-palace-properties' ),
            'ipp_color_surface'      => __( 'Surface / Card Background', 'integrate-palace-properties' ),
            'ipp_color_border'       => __( 'Border Color', 'integrate-palace-properties' ),
            'ipp_color_success'      => __( 'Success / Active Badge', 'integrate-palace-properties' ),
            'ipp_color_card_bg'      => __( 'Property Card Background', 'integrate-palace-properties' ),
        );

        foreach ( $colors as $id => $label ) {
            $wp_customize->add_setting( $id, array(
                'default'           => $defaults[ $id ],
                'sanitize_callback' => 'sanitize_hex_color',
                'transport'         => 'refresh',
            ) );
            $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, $id, array(
                'label'   => $label,
                'section' => 'ipp_section_colors',
            ) ) );
        }

        // ─── Section: Buttons ────────────────────────────────────────
        $wp_customize->add_section( 'ipp_section_buttons', array(
            'title' => __( 'Buttons', 'integrate-palace-properties' ),
            'panel' => 'ipp_panel',
            'priority' => 15,
        ) );

        $btn_colors = array(
            'ipp_btn_bg'         => __( 'Button Background', 'integrate-palace-properties' ),
            'ipp_btn_text'       => __( 'Button Text', 'integrate-palace-properties' ),
            'ipp_btn_bg_hover'   => __( 'Button Hover Background', 'integrate-palace-properties' ),
            'ipp_btn_text_hover' => __( 'Button Hover Text', 'integrate-palace-properties' ),
        );
        foreach ( $btn_colors as $id => $label ) {
            $wp_customize->add_setting( $id, array( 'default' => $defaults[ $id ], 'sanitize_callback' => 'sanitize_hex_color', 'transport' => 'postMessage' ) );
            $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, $id, array( 'label' => $label, 'section' => 'ipp_section_buttons' ) ) );
        }

        self::add_range( $wp_customize, 'ipp_btn_radius', __( 'Border Radius (px)', 'integrate-palace-properties' ), 'ipp_section_buttons', $defaults, 0, 30 );
        self::add_range( $wp_customize, 'ipp_btn_padding_v', __( 'Vertical Padding (px)', 'integrate-palace-properties' ), 'ipp_section_buttons', $defaults, 4, 24 );
        self::add_range( $wp_customize, 'ipp_btn_padding_h', __( 'Horizontal Padding (px)', 'integrate-palace-properties' ), 'ipp_section_buttons', $defaults, 8, 48 );

        // ─── Section: Typography ─────────────────────────────────────
        $wp_customize->add_section( 'ipp_section_typography', array(
            'title' => __( 'Typography', 'integrate-palace-properties' ),
            'panel' => 'ipp_panel',
            'priority' => 20,
        ) );

        $fonts = array( 'inherit' => 'Theme Default', 'Inter' => 'Inter', 'Roboto' => 'Roboto', 'Open Sans' => 'Open Sans', 'Lato' => 'Lato', 'Montserrat' => 'Montserrat', 'Poppins' => 'Poppins', 'Playfair Display' => 'Playfair Display', 'Georgia' => 'Georgia', 'system-ui' => 'System UI' );

        self::add_select( $wp_customize, 'ipp_font_heading', __( 'Heading Font', 'integrate-palace-properties' ), 'ipp_section_typography', $defaults, $fonts );
        self::add_select( $wp_customize, 'ipp_font_body', __( 'Body Font', 'integrate-palace-properties' ), 'ipp_section_typography', $defaults, $fonts );
        self::add_range( $wp_customize, 'ipp_font_size_base', __( 'Base Font Size (px)', 'integrate-palace-properties' ), 'ipp_section_typography', $defaults, 12, 22 );
        self::add_select( $wp_customize, 'ipp_heading_weight', __( 'Heading Weight', 'integrate-palace-properties' ), 'ipp_section_typography', $defaults, array( '400' => 'Normal', '500' => 'Medium', '600' => 'Semibold', '700' => 'Bold', '800' => 'Extra Bold', '900' => 'Black' ) );

        // ─── Section: Property Cards ─────────────────────────────────
        $wp_customize->add_section( 'ipp_section_cards', array(
            'title' => __( 'Property Cards', 'integrate-palace-properties' ),
            'panel' => 'ipp_panel',
            'priority' => 25,
        ) );

        self::add_range( $wp_customize, 'ipp_card_radius', __( 'Card Border Radius (px)', 'integrate-palace-properties' ), 'ipp_section_cards', $defaults, 0, 24 );
        self::add_select( $wp_customize, 'ipp_card_shadow', __( 'Card Shadow', 'integrate-palace-properties' ), 'ipp_section_cards', $defaults, array( 'none' => 'None', 'small' => 'Subtle', 'medium' => 'Medium', 'large' => 'Prominent' ) );
        self::add_select( $wp_customize, 'ipp_card_image_ratio', __( 'Image Aspect Ratio', 'integrate-palace-properties' ), 'ipp_section_cards', $defaults, array( '4/3' => '4:3', '16/9' => '16:9', '3/2' => '3:2', '1/1' => '1:1', 'auto' => 'Auto' ) );

        $card_toggles = array(
            'ipp_card_show_badge'   => __( 'Show Status Badge', 'integrate-palace-properties' ),
            'ipp_card_show_price'   => __( 'Show Price on Card', 'integrate-palace-properties' ),
            'ipp_card_show_address' => __( 'Show Address', 'integrate-palace-properties' ),
            'ipp_card_show_meta'    => __( 'Show Bed/Bath/Car Icons', 'integrate-palace-properties' ),
            'ipp_card_show_date'    => __( 'Show Date Available', 'integrate-palace-properties' ),
        );
        foreach ( $card_toggles as $id => $label ) {
            self::add_checkbox( $wp_customize, $id, $label, 'ipp_section_cards', $defaults );
        }

        // ─── Section: Archive Layout ─────────────────────────────────
        $wp_customize->add_section( 'ipp_section_archive', array(
            'title' => __( 'Archive / Listings Page', 'integrate-palace-properties' ),
            'panel' => 'ipp_panel',
            'priority' => 30,
        ) );

        self::add_select( $wp_customize, 'ipp_archive_columns', __( 'Grid Columns (Desktop)', 'integrate-palace-properties' ), 'ipp_section_archive', $defaults, array( '2' => '2 Columns', '3' => '3 Columns', '4' => '4 Columns' ) );
        self::add_range( $wp_customize, 'ipp_archive_gap', __( 'Grid Gap (px)', 'integrate-palace-properties' ), 'ipp_section_archive', $defaults, 8, 48 );
        self::add_range( $wp_customize, 'ipp_archive_per_page', __( 'Properties Per Page', 'integrate-palace-properties' ), 'ipp_section_archive', $defaults, 3, 24 );
        self::add_range( $wp_customize, 'ipp_archive_container_width', __( 'Container Max Width (px)', 'integrate-palace-properties' ), 'ipp_section_archive', $defaults, 960, 1600 );

        $archive_toggles = array(
            'ipp_archive_show_search' => __( 'Show Search/Filter Bar', 'integrate-palace-properties' ),
            'ipp_archive_show_sort'   => __( 'Show Sort Dropdown', 'integrate-palace-properties' ),
            'ipp_archive_show_count'  => __( 'Show Property Count', 'integrate-palace-properties' ),
            'ipp_archive_show_toggle' => __( 'Show Grid/List Toggle', 'integrate-palace-properties' ),
        );
        foreach ( $archive_toggles as $id => $label ) {
            self::add_checkbox( $wp_customize, $id, $label, 'ipp_section_archive', $defaults );
        }

        // ─── Section: Single Property ────────────────────────────────
        $wp_customize->add_section( 'ipp_section_single', array(
            'title' => __( 'Single Property Page', 'integrate-palace-properties' ),
            'panel' => 'ipp_panel',
            'priority' => 35,
        ) );

        self::add_range( $wp_customize, 'ipp_single_sidebar_width', __( 'Sidebar Width (px)', 'integrate-palace-properties' ), 'ipp_section_single', $defaults, 280, 480 );
        self::add_range( $wp_customize, 'ipp_single_container_width', __( 'Container Max Width (px)', 'integrate-palace-properties' ), 'ipp_section_single', $defaults, 960, 1600 );

        $single_toggles = array(
            'ipp_single_show_gallery'  => __( 'Show Gallery', 'integrate-palace-properties' ),
            'ipp_single_show_map'      => __( 'Show Map', 'integrate-palace-properties' ),
            'ipp_single_show_agent'    => __( 'Show Agent Card', 'integrate-palace-properties' ),
            'ipp_single_show_share'    => __( 'Show Share Buttons', 'integrate-palace-properties' ),
            'ipp_single_show_schema'   => __( 'Output Schema.org Markup', 'integrate-palace-properties' ),
            'ipp_single_show_enquiry'  => __( 'Show Enquiry Button', 'integrate-palace-properties' ),
        );
        foreach ( $single_toggles as $id => $label ) {
            self::add_checkbox( $wp_customize, $id, $label, 'ipp_section_single', $defaults );
        }

        // ─── Section: Agent Card ─────────────────────────────────────
        $wp_customize->add_section( 'ipp_section_agent', array(
            'title' => __( 'Agent Card', 'integrate-palace-properties' ),
            'panel' => 'ipp_panel',
            'priority' => 40,
        ) );

        $wp_customize->add_setting( 'ipp_agent_card_bg', array( 'default' => $defaults['ipp_agent_card_bg'], 'sanitize_callback' => 'sanitize_hex_color', 'transport' => 'postMessage' ) );
        $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'ipp_agent_card_bg', array( 'label' => __( 'Card Background', 'integrate-palace-properties' ), 'section' => 'ipp_section_agent' ) ) );

        $wp_customize->add_setting( 'ipp_agent_avatar_bg', array( 'default' => $defaults['ipp_agent_avatar_bg'], 'sanitize_callback' => 'sanitize_hex_color', 'transport' => 'postMessage' ) );
        $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'ipp_agent_avatar_bg', array( 'label' => __( 'Avatar Background', 'integrate-palace-properties' ), 'section' => 'ipp_section_agent' ) ) );

        $wp_customize->add_setting( 'ipp_agent_avatar_color', array( 'default' => $defaults['ipp_agent_avatar_color'], 'sanitize_callback' => 'sanitize_hex_color', 'transport' => 'postMessage' ) );
        $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'ipp_agent_avatar_color', array( 'label' => __( 'Avatar Icon Color', 'integrate-palace-properties' ), 'section' => 'ipp_section_agent' ) ) );

        self::add_range( $wp_customize, 'ipp_agent_card_radius', __( 'Card Radius (px)', 'integrate-palace-properties' ), 'ipp_section_agent', $defaults, 0, 24 );

        // ─── Section: Search Bar ─────────────────────────────────────
        $wp_customize->add_section( 'ipp_section_search', array(
            'title' => __( 'Search / Filter Bar', 'integrate-palace-properties' ),
            'panel' => 'ipp_panel',
            'priority' => 45,
        ) );

        $wp_customize->add_setting( 'ipp_search_bg', array( 'default' => $defaults['ipp_search_bg'], 'sanitize_callback' => 'sanitize_hex_color', 'transport' => 'postMessage' ) );
        $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'ipp_search_bg', array( 'label' => __( 'Search Bar Background', 'integrate-palace-properties' ), 'section' => 'ipp_section_search' ) ) );

        $wp_customize->add_setting( 'ipp_search_border', array( 'default' => $defaults['ipp_search_border'], 'sanitize_callback' => 'sanitize_hex_color', 'transport' => 'postMessage' ) );
        $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'ipp_search_border', array( 'label' => __( 'Search Bar Border', 'integrate-palace-properties' ), 'section' => 'ipp_section_search' ) ) );

        $wp_customize->add_setting( 'ipp_search_input_bg', array( 'default' => $defaults['ipp_search_input_bg'], 'sanitize_callback' => 'sanitize_hex_color', 'transport' => 'postMessage' ) );
        $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'ipp_search_input_bg', array( 'label' => __( 'Input Background', 'integrate-palace-properties' ), 'section' => 'ipp_section_search' ) ) );

        self::add_range( $wp_customize, 'ipp_search_radius', __( 'Border Radius (px)', 'integrate-palace-properties' ), 'ipp_section_search', $defaults, 0, 24 );
    }

    // ─── Helpers ──────────────────────────────────────────────────

    private static function add_range( $wp_customize, $id, $label, $section, $defaults, $min, $max ) {
        $wp_customize->add_setting( $id, array(
            'default'           => $defaults[ $id ] ?? $min,
            'sanitize_callback' => 'absint',
            'transport'         => 'refresh',
        ) );
        $wp_customize->add_control( $id, array(
            'label'       => $label,
            'section'     => $section,
            'type'        => 'range',
            'input_attrs' => array( 'min' => $min, 'max' => $max, 'step' => 1 ),
            'description' => '<span class="ipp-range-val">' . ( $defaults[ $id ] ?? $min ) . '</span>',
        ) );
    }

    private static function add_select( $wp_customize, $id, $label, $section, $defaults, $choices ) {
        $wp_customize->add_setting( $id, array(
            'default'           => $defaults[ $id ] ?? '',
            'sanitize_callback' => 'sanitize_text_field',
            'transport'         => 'refresh',
        ) );
        $wp_customize->add_control( $id, array(
            'label'   => $label,
            'section' => $section,
            'type'    => 'select',
            'choices' => $choices,
        ) );
    }

    private static function add_checkbox( $wp_customize, $id, $label, $section, $defaults ) {
        $wp_customize->add_setting( $id, array(
            'default'           => $defaults[ $id ] ?? true,
            'sanitize_callback' => function( $val ) { return (bool) $val; },
            'transport'         => 'refresh',
        ) );
        $wp_customize->add_control( $id, array(
            'label'   => $label,
            'section' => $section,
            'type'    => 'checkbox',
        ) );
    }

    // ─── CSS Output ──────────────────────────────────────────────

    public static function output_css() {
        if ( ! is_singular( 'palace-properties' ) && ! is_post_type_archive( 'palace-properties' ) && ! is_tax( array( 'palace-property-type', 'palace-property-status', 'palace-property-region' ) ) ) {
            return;
        }

        $d = self::defaults();
        $get = function( $key ) use ( $d ) {
            return get_theme_mod( $key, $d[ $key ] ?? '' );
        };

        $shadow_map = array(
            'none'   => 'none',
            'small'  => '0 1px 3px rgba(0,0,0,.08)',
            'medium' => '0 4px 20px rgba(0,0,0,.08)',
            'large'  => '0 10px 40px rgba(0,0,0,.12)',
        );

        $font_heading = $get('ipp_font_heading');
        $font_body = $get('ipp_font_body');

        // Enqueue Google Fonts if needed
        $google_fonts = array();
        if ( $font_heading && $font_heading !== 'inherit' && $font_heading !== 'system-ui' && $font_heading !== 'Georgia' ) {
            $google_fonts[] = $font_heading;
        }
        if ( $font_body && $font_body !== 'inherit' && $font_body !== 'system-ui' && $font_body !== 'Georgia' && $font_body !== $font_heading ) {
            $google_fonts[] = $font_body;
        }
        if ( ! empty( $google_fonts ) ) {
            $families = implode( '&family=', array_map( function($f) { return str_replace(' ', '+', $f) . ':wght@400;500;600;700'; }, $google_fonts ) );
            echo '<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=' . esc_attr( $families ) . '&display=swap" />' . "\n";
        }

        $card_shadow = $shadow_map[ $get('ipp_card_shadow') ] ?? $shadow_map['medium'];
        $font_heading_stack = $font_heading === 'inherit' ? 'inherit' : '"' . $font_heading . '", sans-serif';
        $font_body_stack = $font_body === 'inherit' ? 'inherit' : '"' . $font_body . '", sans-serif';

        // Scope prefix — all rules scoped to plugin containers only
        $s = '.ipp-archive-page, .ipp-single-page, .ipp-tpl-palace';

        echo '<style id="ipp-customizer-css">' . "\n";

        // CSS custom properties — scoped to plugin containers, NOT :root
        echo '.ipp-archive-page, .ipp-single-page, .ipp-tpl-palace {' . "\n";
        echo '  --ipp-color-primary: ' . esc_attr( $get('ipp_color_primary') ) . ';' . "\n";
        echo '  --ipp-color-accent: ' . esc_attr( $get('ipp_color_accent') ) . ';' . "\n";
        echo '  --ipp-color-accent-hover: ' . esc_attr( $get('ipp_color_accent_hover') ) . ';' . "\n";
        echo '  --ipp-color-text: ' . esc_attr( $get('ipp_color_text') ) . ';' . "\n";
        echo '  --ipp-color-text-light: ' . esc_attr( $get('ipp_color_text_light') ) . ';' . "\n";
        echo '  --ipp-color-bg: ' . esc_attr( $get('ipp_color_background') ) . ';' . "\n";
        echo '  --ipp-color-surface: ' . esc_attr( $get('ipp_color_surface') ) . ';' . "\n";
        echo '  --ipp-color-border: ' . esc_attr( $get('ipp_color_border') ) . ';' . "\n";
        echo '  --ipp-color-success: ' . esc_attr( $get('ipp_color_success') ) . ';' . "\n";
        echo '  --ipp-radius: ' . absint( $get('ipp_card_radius') ) . 'px;' . "\n";
        echo '  --ipp-shadow: ' . esc_attr( $card_shadow ) . ';' . "\n";
        echo '  --ipp-font-heading: ' . esc_attr( $font_heading_stack ) . ';' . "\n";
        echo '  --ipp-font-body: ' . esc_attr( $font_body_stack ) . ';' . "\n";
        echo '  --ipp-container-width: ' . absint( $get('ipp_archive_container_width') ) . 'px;' . "\n";
        echo '}' . "\n";

        // Buttons — scoped
        echo '.ipp-archive-page .ipp-btn--primary, .ipp-single-page .ipp-btn--primary, .ipp-tpl-palace .ipp-agent-card__enquire, .ipp-tpl-palace .ipp-tour__btn {' . "\n";
        echo '  background: ' . esc_attr( $get('ipp_btn_bg') ) . ' !important;' . "\n";
        echo '  color: ' . esc_attr( $get('ipp_btn_text') ) . ' !important;' . "\n";
        echo '  border-radius: ' . absint( $get('ipp_btn_radius') ) . 'px !important;' . "\n";
        echo '  padding: ' . absint( $get('ipp_btn_padding_v') ) . 'px ' . absint( $get('ipp_btn_padding_h') ) . 'px !important;' . "\n";
        echo '}' . "\n";
        echo '.ipp-archive-page .ipp-btn--primary:hover, .ipp-single-page .ipp-btn--primary:hover, .ipp-tpl-palace .ipp-agent-card__enquire:hover, .ipp-tpl-palace .ipp-tour__btn:hover {' . "\n";
        echo '  background: ' . esc_attr( $get('ipp_btn_bg_hover') ) . ' !important;' . "\n";
        echo '  color: ' . esc_attr( $get('ipp_btn_text_hover') ) . ' !important;' . "\n";
        echo '}' . "\n";

        // Card — scoped
        echo '.ipp-archive-page .ipp-card { border-radius: ' . absint( $get('ipp_card_radius') ) . 'px; background: ' . esc_attr( $get('ipp_color_card_bg') ) . '; box-shadow: ' . esc_attr( $card_shadow ) . '; }' . "\n";
        echo '.ipp-archive-page .ipp-card__image { aspect-ratio: ' . esc_attr( $get('ipp_card_image_ratio') ) . '; }' . "\n";

        // Archive grid — scoped
        echo '.ipp-archive-page .ipp-grid { grid-template-columns: repeat(' . absint( $get('ipp_archive_columns') ) . ', 1fr) !important; gap: ' . absint( $get('ipp_archive_gap') ) . 'px !important; }' . "\n";
        echo '@media (max-width: 768px) { .ipp-archive-page .ipp-grid { grid-template-columns: 1fr !important; } }' . "\n";
        echo '@media (min-width: 769px) and (max-width: 1024px) { .ipp-archive-page .ipp-grid { grid-template-columns: repeat(2, 1fr) !important; } }' . "\n";

        // Font — scoped
        echo '.ipp-archive-page, .ipp-single-page, .ipp-tpl-palace { font-family: ' . esc_attr( $font_body_stack ) . '; font-size: ' . absint( $get('ipp_font_size_base') ) . 'px; }' . "\n";
        echo '.ipp-archive-page h1, .ipp-archive-page h2, .ipp-archive-page h3, .ipp-single-page h1, .ipp-single-page h2, .ipp-single-page h3, .ipp-tpl-palace h1, .ipp-tpl-palace h2, .ipp-tpl-palace h3 { font-family: ' . esc_attr( $font_heading_stack ) . '; font-weight: ' . esc_attr( $get('ipp_heading_weight') ) . '; }' . "\n";

        // Single layout — scoped
        echo '.ipp-tpl-palace .ipp-layout { max-width: ' . absint( $get('ipp_single_container_width') ) . 'px !important; grid-template-columns: 1fr ' . absint( $get('ipp_single_sidebar_width') ) . 'px !important; }' . "\n";
        echo '@media (max-width: 768px) { .ipp-tpl-palace .ipp-layout { grid-template-columns: 1fr !important; } }' . "\n";

        // Search — scoped
        echo '.ipp-archive-page .ipp-search { background: ' . esc_attr( $get('ipp_search_bg') ) . '; border-color: ' . esc_attr( $get('ipp_search_border') ) . '; border-radius: ' . absint( $get('ipp_search_radius') ) . 'px; }' . "\n";
        echo '.ipp-archive-page .ipp-search__input, .ipp-archive-page .ipp-search__select { background: ' . esc_attr( $get('ipp_search_input_bg') ) . ' !important; }' . "\n";

        // Agent — scoped
        echo '.ipp-tpl-palace .ipp-agent-card { background: ' . esc_attr( $get('ipp_agent_card_bg') ) . '; border-radius: ' . absint( $get('ipp_agent_card_radius') ) . 'px; }' . "\n";
        echo '.ipp-tpl-palace .ipp-agent-card__avatar { background: ' . esc_attr( $get('ipp_agent_avatar_bg') ) . '; }' . "\n";
        echo '.ipp-tpl-palace .ipp-agent-card__avatar svg { color: ' . esc_attr( $get('ipp_agent_avatar_color') ) . '; }' . "\n";

        // Conditional visibility — scoped
        if ( ! $get('ipp_card_show_badge') ) echo '.ipp-archive-page .ipp-card__badge { display: none !important; }' . "\n";
        if ( ! $get('ipp_card_show_price') ) echo '.ipp-archive-page .ipp-card__price { display: none !important; }' . "\n";
        if ( ! $get('ipp_card_show_address') ) echo '.ipp-archive-page .ipp-card__address { display: none !important; }' . "\n";
        if ( ! $get('ipp_card_show_meta') ) echo '.ipp-archive-page .ipp-card__meta { display: none !important; }' . "\n";
        if ( ! $get('ipp_card_show_date') ) echo '.ipp-archive-page .ipp-card__footer { display: none !important; }' . "\n";
        if ( ! $get('ipp_archive_show_search') ) echo '.ipp-archive-page .ipp-search { display: none !important; }' . "\n";
        if ( ! $get('ipp_archive_show_count') ) echo '.ipp-archive-page .ipp-archive-count { display: none !important; }' . "\n";
        if ( ! $get('ipp_archive_show_toggle') ) echo '.ipp-archive-page .ipp-archive-view-toggle { display: none !important; }' . "\n";
        if ( ! $get('ipp_single_show_map') ) echo '.ipp-tpl-palace .ipp-map-wrap, .ipp-tpl-palace .ipp-map { display: none !important; }' . "\n";
        if ( ! $get('ipp_single_show_agent') ) echo '.ipp-tpl-palace .ipp-agent-card { display: none !important; }' . "\n";
        if ( ! $get('ipp_single_show_share') ) echo '.ipp-tpl-palace .ipp-share { display: none !important; }' . "\n";
        if ( ! $get('ipp_single_show_gallery') ) echo '.ipp-tpl-palace .ipp-hero, .ipp-tpl-palace .ipp-gallery { display: none !important; }' . "\n";
        if ( ! $get('ipp_single_show_enquiry') ) echo '.ipp-tpl-palace .ipp-agent-card__enquire { display: none !important; }' . "\n";

        echo '</style>' . "\n";
    }
}

IPP_Customizer_Settings::init();
