<?php
/**
 * WordPress Customizer settings
 * Colors, fonts, logo, and basic options
 */

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

/**
 * Sanitize font size: must be a positive float, or empty string.
 */
function wpbs_sanitize_font_size($value) {
    if ($value === '' || $value === false) {
        return '';
    }
    $value = floatval($value);
    return ($value > 0) ? $value : '';
}

/**
 * Check if a hex color is dark (luminance < 0.5).
 */
function wpbs_is_dark_color($hex) {
    $hex = ltrim($hex, '#');
    if (strlen($hex) === 3) {
        $hex = $hex[0].$hex[0].$hex[1].$hex[1].$hex[2].$hex[2];
    }
    $r = hexdec(substr($hex, 0, 2)) / 255;
    $g = hexdec(substr($hex, 2, 2)) / 255;
    $b = hexdec(substr($hex, 4, 2)) / 255;
    $luminance = 0.2126 * $r + 0.7152 * $g + 0.0722 * $b;
    return $luminance < 0.5;
}

function wpbs_customize_register($wp_customize) {
    
    // ─── Colors Section ───
    
    $wp_customize->add_setting('wpbs_primary_color', [
        'default' => '#0d6efd',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport' => 'postMessage',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_primary_color', [
        'label' => __('Primary Color', 'wpbs-theme'),
        'section' => 'colors',
        'priority' => 10,
    ]));

    $wp_customize->add_setting('wpbs_secondary_color', [
        'default' => '#6c757d',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport' => 'postMessage',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_secondary_color', [
        'label' => __('Secondary Color', 'wpbs-theme'),
        'section' => 'colors',
        'priority' => 11,
    ]));

    $wp_customize->add_setting('wpbs_accent_color', [
        'default' => '#0dcaf0',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport' => 'postMessage',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_accent_color', [
        'label' => __('Accent Color', 'wpbs-theme'),
        'section' => 'colors',
        'priority' => 12,
    ]));

    $wp_customize->add_setting('wpbs_navbar_bg', [
        'default' => '#ffffff',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport' => 'postMessage',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_navbar_bg', [
        'label' => __('Navbar Background', 'wpbs-theme'),
        'section' => 'colors',
        'priority' => 13,
    ]));

    $wp_customize->add_setting('wpbs_footer_bg', [
        'default' => '#212529',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport' => 'postMessage',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_footer_bg', [
        'label' => __('Footer Background', 'wpbs-theme'),
        'section' => 'colors',
        'priority' => 14,
    ]));

    $wp_customize->add_setting('wpbs_heading_color', [
        'default' => '#212529',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport' => 'postMessage',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_heading_color', [
        'label' => __('Heading Color', 'wpbs-theme'),
        'section' => 'colors',
        'priority' => 15,
    ]));

    // Button Colors
    $wp_customize->add_setting('wpbs_btn_primary_bg', [
        'default' => '',
        'sanitize_callback' => 'sanitize_hex_color',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_btn_primary_bg', [
        'label' => __('Button Primary Background', 'wpbs-theme'),
        'description' => __('Leave empty to use Primary Color.', 'wpbs-theme'),
        'section' => 'colors',
        'priority' => 20,
    ]));

    $wp_customize->add_setting('wpbs_btn_primary_text', [
        'default' => '#ffffff',
        'sanitize_callback' => 'sanitize_hex_color',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_btn_primary_text', [
        'label' => __('Button Primary Text', 'wpbs-theme'),
        'section' => 'colors',
        'priority' => 21,
    ]));

    $wp_customize->add_setting('wpbs_btn_primary_hover_bg', [
        'default' => '',
        'sanitize_callback' => 'sanitize_hex_color',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_btn_primary_hover_bg', [
        'label' => __('Button Primary Hover Background', 'wpbs-theme'),
        'description' => __('Leave empty to auto-darken.', 'wpbs-theme'),
        'section' => 'colors',
        'priority' => 22,
    ]));

    $wp_customize->add_setting('wpbs_btn_secondary_bg', [
        'default' => '',
        'sanitize_callback' => 'sanitize_hex_color',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_btn_secondary_bg', [
        'label' => __('Button Secondary Background', 'wpbs-theme'),
        'description' => __('Leave empty to use Secondary Color.', 'wpbs-theme'),
        'section' => 'colors',
        'priority' => 23,
    ]));

    $wp_customize->add_setting('wpbs_btn_secondary_text', [
        'default' => '#ffffff',
        'sanitize_callback' => 'sanitize_hex_color',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_btn_secondary_text', [
        'label' => __('Button Secondary Text', 'wpbs-theme'),
        'section' => 'colors',
        'priority' => 24,
    ]));

    // ─── Typography Section ───
    
    $wp_customize->add_section('wpbs_typography', [
        'title' => __('Typography', 'wpbs-theme'),
        'priority' => 45,
    ]);

    $font_choices = [
        "'Source Sans 3', sans-serif" => 'Source Sans 3',
        "'Inter', sans-serif" => 'Inter',
        "'Montserrat', sans-serif" => 'Montserrat',
        "'Raleway', sans-serif" => 'Raleway',
        "'Open Sans', sans-serif" => 'Open Sans',
        "'Lato', sans-serif" => 'Lato',
        "'Roboto', sans-serif" => 'Roboto',
        "'DM Sans', sans-serif" => 'DM Sans',
        "'Poppins', sans-serif" => 'Poppins',
        "'Red Hat Display', sans-serif" => 'Red Hat Display',
        "'Josefin Sans', sans-serif" => 'Josefin Sans',
        "'Oswald', sans-serif" => 'Oswald',
        "'Outfit', sans-serif" => 'Outfit',
        "'Playfair Display', serif" => 'Playfair Display',
        "'Merriweather', serif" => 'Merriweather',
        "'DM Serif Display', serif" => 'DM Serif Display',
        "'Cormorant Garamond', serif" => 'Cormorant Garamond',
        "'Crimson Pro', serif" => 'Crimson Pro',
        "'Bebas Neue', sans-serif" => 'Bebas Neue',
        "'Pacifico', cursive" => 'Pacifico',
    ];

    $wp_customize->add_setting('wpbs_heading_font', [
        'default' => "'Source Sans 3', sans-serif",
        'sanitize_callback' => 'sanitize_text_field',
    ]);
    $wp_customize->add_control('wpbs_heading_font', [
        'label' => __('Heading Font', 'wpbs-theme'),
        'section' => 'wpbs_typography',
        'type' => 'select',
        'choices' => $font_choices,
    ]);

    $wp_customize->add_setting('wpbs_body_font', [
        'default' => "'Source Sans 3', sans-serif",
        'sanitize_callback' => 'sanitize_text_field',
    ]);
    $wp_customize->add_control('wpbs_body_font', [
        'label' => __('Body Font', 'wpbs-theme'),
        'section' => 'wpbs_typography',
        'type' => 'select',
        'choices' => $font_choices,
    ]);

    // Font Sizes
    $font_size_defaults = [
        'h1' => '2.5',
        'h2' => '2',
        'h3' => '1.75',
        'h4' => '1.5',
        'h5' => '1.25',
        'h6' => '1',
        'p'  => '1',
    ];
    foreach ($font_size_defaults as $tag => $default_val) {
        $setting_id = 'wpbs_font_size_' . $tag;
        $label = strtoupper($tag) . ' Font Size (rem)';

        $wp_customize->add_setting($setting_id, [
            'default' => '',
            'sanitize_callback' => 'wpbs_sanitize_font_size',
        ]);
        $wp_customize->add_control($setting_id, [
            'label' => __($label, 'wpbs-theme'),
            'description' => sprintf(__('Bootstrap default: %s rem', 'wpbs-theme'), $default_val),
            'section' => 'wpbs_typography',
            'type' => 'number',
            'input_attrs' => [
                'min' => '0.5',
                'max' => '10',
                'step' => '0.125',
            ],
        ]);
    }

    // ─── Navbar Section ───

    $wp_customize->add_section('wpbs_navbar', [
        'title' => __('Navbar', 'wpbs-theme'),
        'priority' => 50,
    ]);

    $wp_customize->add_setting('wpbs_navbar_style', [
        'default' => 'light',
        'sanitize_callback' => 'sanitize_text_field',
    ]);
    $wp_customize->add_control('wpbs_navbar_style', [
        'label' => __('Navbar Style', 'wpbs-theme'),
        'section' => 'wpbs_navbar',
        'type' => 'select',
        'choices' => [
            'light' => 'Light',
            'dark' => 'Dark',
            'transparent' => 'Transparent',
        ],
    ]);

    $wp_customize->add_setting('wpbs_navbar_fixed', [
        'default' => false,
        'sanitize_callback' => 'wp_validate_boolean',
    ]);
    $wp_customize->add_control('wpbs_navbar_fixed', [
        'label' => __('Fixed Navbar (sticky top)', 'wpbs-theme'),
        'section' => 'wpbs_navbar',
        'type' => 'checkbox',
    ]);

    $wp_customize->add_setting('wpbs_navbar_cta_text', [
        'default' => '',
        'sanitize_callback' => 'sanitize_text_field',
    ]);
    $wp_customize->add_control('wpbs_navbar_cta_text', [
        'label' => __('CTA Button Text', 'wpbs-theme'),
        'description' => __('Leave empty to hide. E.g., "Free Estimate"', 'wpbs-theme'),
        'section' => 'wpbs_navbar',
        'type' => 'text',
    ]);

    $wp_customize->add_setting('wpbs_navbar_cta_url', [
        'default' => '',
        'sanitize_callback' => 'esc_url_raw',
    ]);
    $wp_customize->add_control('wpbs_navbar_cta_url', [
        'label' => __('CTA Button URL', 'wpbs-theme'),
        'section' => 'wpbs_navbar',
        'type' => 'url',
    ]);

    $wp_customize->add_setting('wpbs_navbar_phone', [
        'default' => '',
        'sanitize_callback' => 'sanitize_text_field',
    ]);
    $wp_customize->add_control('wpbs_navbar_phone', [
        'label' => __('Phone Number', 'wpbs-theme'),
        'description' => __('Displayed in navbar. Leave empty to hide.', 'wpbs-theme'),
        'section' => 'wpbs_navbar',
        'type' => 'text',
    ]);

    // Navbar Text & Link Colors
    $wp_customize->add_setting('wpbs_navbar_text_color', [
        'default' => '#212529',
        'sanitize_callback' => 'sanitize_hex_color',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_navbar_text_color', [
        'label' => __('Navbar Text Color', 'wpbs-theme'),
        'section' => 'wpbs_navbar',
    ]));

    $wp_customize->add_setting('wpbs_navbar_link_color', [
        'default' => '#212529',
        'sanitize_callback' => 'sanitize_hex_color',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_navbar_link_color', [
        'label' => __('Navbar Link Color', 'wpbs-theme'),
        'section' => 'wpbs_navbar',
    ]));

    // Offcanvas Colors
    $wp_customize->add_setting('wpbs_offcanvas_bg', [
        'default' => '#ffffff',
        'sanitize_callback' => 'sanitize_hex_color',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_offcanvas_bg', [
        'label' => __('Offcanvas Background', 'wpbs-theme'),
        'section' => 'wpbs_navbar',
    ]));

    $wp_customize->add_setting('wpbs_offcanvas_text_color', [
        'default' => '#212529',
        'sanitize_callback' => 'sanitize_hex_color',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_offcanvas_text_color', [
        'label' => __('Offcanvas Text Color', 'wpbs-theme'),
        'section' => 'wpbs_navbar',
    ]));

    // Dropdown Colors
    $wp_customize->add_setting('wpbs_dropdown_bg', [
        'default' => '',
        'sanitize_callback' => 'sanitize_hex_color',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_dropdown_bg', [
        'label' => __('Dropdown Background', 'wpbs-theme'),
        'description' => __('Leave empty to inherit Navbar Background.', 'wpbs-theme'),
        'section' => 'wpbs_navbar',
    ]));

    $wp_customize->add_setting('wpbs_dropdown_link_color', [
        'default' => '',
        'sanitize_callback' => 'sanitize_hex_color',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_dropdown_link_color', [
        'label' => __('Dropdown Link Color', 'wpbs-theme'),
        'description' => __('Leave empty to inherit Navbar Link Color.', 'wpbs-theme'),
        'section' => 'wpbs_navbar',
    ]));

    // CTA Button Colors
    $wp_customize->add_setting('wpbs_navbar_cta_bg', [
        'default' => '',
        'sanitize_callback' => 'sanitize_hex_color',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_navbar_cta_bg', [
        'label' => __('CTA Button Background', 'wpbs-theme'),
        'description' => __('Leave empty to inherit Primary Color.', 'wpbs-theme'),
        'section' => 'wpbs_navbar',
    ]));

    $wp_customize->add_setting('wpbs_navbar_cta_text_color', [
        'default' => '#ffffff',
        'sanitize_callback' => 'sanitize_hex_color',
    ]);
    $wp_customize->add_control(new WP_Customize_Color_Control($wp_customize, 'wpbs_navbar_cta_text_color', [
        'label' => __('CTA Button Text Color', 'wpbs-theme'),
        'section' => 'wpbs_navbar',
    ]));

    // ─── Footer Section ───
    
    $wp_customize->add_section('wpbs_footer', [
        'title' => __('Footer', 'wpbs-theme'),
        'priority' => 55,
    ]);

    $wp_customize->add_setting('wpbs_footer_text', [
        'default' => '',
        'sanitize_callback' => 'wp_kses_post',
    ]);
    $wp_customize->add_control('wpbs_footer_text', [
        'label' => __('Footer Copyright Text', 'wpbs-theme'),
        'description' => __('Use {year} for current year, {site} for site name.', 'wpbs-theme'),
        'section' => 'wpbs_footer',
        'type' => 'textarea',
    ]);

    $wp_customize->add_setting('wpbs_footer_columns', [
        'default' => '3',
        'sanitize_callback' => 'sanitize_text_field',
    ]);
    $wp_customize->add_control('wpbs_footer_columns', [
        'label' => __('Footer Widget Columns', 'wpbs-theme'),
        'section' => 'wpbs_footer',
        'type' => 'select',
        'choices' => ['2' => '2 Columns', '3' => '3 Columns', '4' => '4 Columns'],
    ]);
}
add_action('customize_register', 'wpbs_customize_register');

/**
 * Output customizer CSS as inline styles
 */
function wpbs_customizer_css() {
    // Existing settings
    $primary   = get_theme_mod('wpbs_primary_color', '#0d6efd');
    $secondary = get_theme_mod('wpbs_secondary_color', '#6c757d');
    $accent    = get_theme_mod('wpbs_accent_color', '#0dcaf0');
    $navbar_bg = get_theme_mod('wpbs_navbar_bg', '#ffffff');
    $footer_bg = get_theme_mod('wpbs_footer_bg', '#212529');
    $heading_color = get_theme_mod('wpbs_heading_color', '#212529');
    $heading_font = get_theme_mod('wpbs_heading_font', "'Source Sans 3', sans-serif");
    $body_font    = get_theme_mod('wpbs_body_font', "'Source Sans 3', sans-serif");

    // Navbar colors
    $navbar_text   = get_theme_mod('wpbs_navbar_text_color', '#212529');
    $navbar_link   = get_theme_mod('wpbs_navbar_link_color', '#212529');

    // Offcanvas colors
    $offcanvas_bg   = get_theme_mod('wpbs_offcanvas_bg', '#ffffff');
    $offcanvas_text = get_theme_mod('wpbs_offcanvas_text_color', '#212529');

    // CTA button
    $cta_bg   = get_theme_mod('wpbs_navbar_cta_bg', '');
    $cta_text = get_theme_mod('wpbs_navbar_cta_text_color', '#ffffff');

    // Button colors
    $btn_pri_bg       = get_theme_mod('wpbs_btn_primary_bg', '');
    $btn_pri_text     = get_theme_mod('wpbs_btn_primary_text', '#ffffff');
    $btn_pri_hover_bg = get_theme_mod('wpbs_btn_primary_hover_bg', '');
    $btn_sec_bg       = get_theme_mod('wpbs_btn_secondary_bg', '');
    $btn_sec_text     = get_theme_mod('wpbs_btn_secondary_text', '#ffffff');

    // Resolve button backgrounds (fall back to primary/secondary)
    $btn_pri_bg_resolved = $btn_pri_bg ?: $primary;
    $btn_sec_bg_resolved = $btn_sec_bg ?: $secondary;

    $css = ":root {
        --wpbs-primary: {$primary};
        --wpbs-secondary: {$secondary};
        --wpbs-accent: {$accent};
        --wpbs-navbar-bg: {$navbar_bg};
        --wpbs-footer-bg: {$footer_bg};
        --wpbs-heading-color: {$heading_color};
        --wpbs-heading-font: {$heading_font};
        --wpbs-body-font: {$body_font};
        --wpbs-navbar-text: {$navbar_text};
        --wpbs-navbar-link: {$navbar_link};
        --wpbs-offcanvas-bg: {$offcanvas_bg};
        --bs-primary: {$primary};
    }
    body { font-family: {$body_font}; }
    h1, h2, h3, h4, h5, h6 { font-family: {$heading_font}; color: {$heading_color}; }
    .wpbs-navbar { background-color: {$navbar_bg}; }
    .wpbs-footer { background-color: {$footer_bg}; }
    a { color: {$primary}; }";

    // Navbar text & link colors
    $css .= "
    .wpbs-navbar, .wpbs-navbar .navbar-brand { color: {$navbar_text}; }
    .wpbs-navbar .nav-link { color: {$navbar_link}; }
    .wpbs-navbar .nav-link:hover, .wpbs-navbar .nav-link:focus { color: color-mix(in srgb, {$navbar_link} 70%, black); }
    .wpbs-navbar .nav-link.active, .wpbs-navbar .nav-link.show { color: {$navbar_link}; }
    .wpbs-navbar .wpbs-navbar-phone { color: #ffffff; }
    .wpbs-navbar .wpbs-navbar-phone:hover { color: #ffffff; }
    .wpbs-navbar .navbar-toggler { border-color: {$navbar_link}; }
    .wpbs-navbar .navbar-toggler-icon { background-image: url(\"data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 30 30'%3e%3cpath stroke='" . str_replace('#', '%23', $navbar_link) . "' stroke-linecap='round' stroke-miterlimit='10' stroke-width='2' d='M4 7h22M4 15h22M4 23h22'/%3e%3c/svg%3e\"); }";

    // Offcanvas
    $css .= "
    #wpbs-offcanvas { background-color: {$offcanvas_bg}; }
    #wpbs-offcanvas, #wpbs-offcanvas .nav-link, #wpbs-offcanvas .offcanvas-title { color: {$offcanvas_text}; }
    #wpbs-offcanvas .nav-link:hover { color: color-mix(in srgb, {$offcanvas_text} 70%, black); }
    #wpbs-offcanvas a[href^='tel:'] { color: {$offcanvas_text}; }
    #wpbs-offcanvas a[href^='tel:']:hover { color: color-mix(in srgb, {$offcanvas_text} 70%, black); }";

    // Auto-invert close button on dark offcanvas backgrounds
    if (wpbs_is_dark_color($offcanvas_bg)) {
        $css .= "
    #wpbs-offcanvas .btn-close { filter: invert(1) grayscale(100%) brightness(200%); }";
    }

    // Dropdown menu (falls back to navbar colors)
    $dropdown_bg   = get_theme_mod('wpbs_dropdown_bg', '') ?: $navbar_bg;
    $dropdown_link = get_theme_mod('wpbs_dropdown_link_color', '') ?: $navbar_link;
    $css .= "
    .wpbs-navbar .dropdown-menu { background-color: {$dropdown_bg}; border-color: rgba(0,0,0,0.1); }
    .wpbs-navbar .dropdown-menu .dropdown-item { color: {$dropdown_link}; }
    .wpbs-navbar .dropdown-menu .dropdown-item:hover,
    .wpbs-navbar .dropdown-menu .dropdown-item:focus { color: {$dropdown_link}; background-color: rgba(0,0,0,0.08); }";
    if (wpbs_is_dark_color($dropdown_bg)) {
        $css .= "
    .wpbs-navbar .dropdown-menu .dropdown-item:hover,
    .wpbs-navbar .dropdown-menu .dropdown-item:focus { background-color: rgba(255,255,255,0.1); }";
    }

    // Primary button
    $css .= "
    .btn-primary { background-color: {$btn_pri_bg_resolved}; border-color: {$btn_pri_bg_resolved}; color: {$btn_pri_text}; }";

    if ($btn_pri_hover_bg) {
        $css .= "
    .btn-primary:hover { background-color: {$btn_pri_hover_bg}; border-color: {$btn_pri_hover_bg}; color: {$btn_pri_text}; }";
    } else {
        $css .= "
    .btn-primary:hover { background-color: color-mix(in srgb, {$btn_pri_bg_resolved} 85%, black); border-color: color-mix(in srgb, {$btn_pri_bg_resolved} 85%, black); color: {$btn_pri_text}; }";
    }

    // Secondary button
    $css .= "
    .btn-secondary { background-color: {$btn_sec_bg_resolved}; border-color: {$btn_sec_bg_resolved}; color: {$btn_sec_text}; }
    .btn-secondary:hover { background-color: color-mix(in srgb, {$btn_sec_bg_resolved} 85%, black); border-color: color-mix(in srgb, {$btn_sec_bg_resolved} 85%, black); color: {$btn_sec_text}; }";

    // CTA button (only when a custom BG is set)
    if ($cta_bg) {
        $css .= "
    .wpbs-cta-btn { background-color: {$cta_bg} !important; border-color: {$cta_bg} !important; color: {$cta_text} !important; }
    .wpbs-cta-btn:hover { background-color: color-mix(in srgb, {$cta_bg} 85%, black) !important; border-color: color-mix(in srgb, {$cta_bg} 85%, black) !important; color: {$cta_text} !important; }";
    }

    // Font sizes (only output when a value is set)
    $font_size_tags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'p'];
    foreach ($font_size_tags as $tag) {
        $size = get_theme_mod('wpbs_font_size_' . $tag, '');
        if ($size !== '' && $size !== false) {
            $size = floatval($size);
            if ($size > 0) {
                $css .= "\n    {$tag} { font-size: {$size}rem; }";
            }
        }
    }

    wp_add_inline_style('wpbs-theme', $css);
}
add_action('wp_enqueue_scripts', 'wpbs_customizer_css', 20);

/**
 * Enqueue Google Fonts based on Customizer selection
 */
function wpbs_google_fonts() {
    $heading = get_theme_mod('wpbs_heading_font', "'Source Sans 3', sans-serif");
    $body    = get_theme_mod('wpbs_body_font', "'Source Sans 3', sans-serif");
    
    $fonts = [];
    
    // Extract font family name from the CSS value
    $font_map = [
        'Source Sans 3' => 'Source+Sans+3:wght@300;400;600;700',
        'Inter' => 'Inter:wght@300;400;600;700',
        'Montserrat' => 'Montserrat:wght@300;400;600;700',
        'Raleway' => 'Raleway:wght@300;400;600;700',
        'Open Sans' => 'Open+Sans:wght@300;400;600;700',
        'Lato' => 'Lato:wght@300;400;700',
        'Roboto' => 'Roboto:wght@300;400;500;700',
        'DM Sans' => 'DM+Sans:wght@300;400;500;700',
        'Poppins' => 'Poppins:wght@300;400;500;600;700',
        'Red Hat Display' => 'Red+Hat+Display:wght@300;400;600;700',
        'Josefin Sans' => 'Josefin+Sans:wght@300;400;600;700',
        'Oswald' => 'Oswald:wght@300;400;500;600;700',
        'Outfit' => 'Outfit:wght@300;400;500;600;700',
        'Playfair Display' => 'Playfair+Display:wght@400;600;700',
        'Merriweather' => 'Merriweather:wght@300;400;700',
        'DM Serif Display' => 'DM+Serif+Display:wght@400',
        'Cormorant Garamond' => 'Cormorant+Garamond:wght@300;400;600;700',
        'Crimson Pro' => 'Crimson+Pro:wght@300;400;600;700',
        'Bebas Neue' => 'Bebas+Neue:wght@400',
        'Pacifico' => 'Pacifico:wght@400',
    ];
    
    foreach ($font_map as $name => $url_part) {
        if (strpos($heading, $name) !== false || strpos($body, $name) !== false) {
            $fonts[] = $url_part;
        }
    }
    
    if (!empty($fonts)) {
        $url = 'https://fonts.googleapis.com/css2?' . implode('&', array_map(function($f) {
            return 'family=' . $f;
        }, array_unique($fonts))) . '&display=swap';
        
        wp_enqueue_style('wpbs-google-fonts', $url, [], null);
    }
}
add_action('wp_enqueue_scripts', 'wpbs_google_fonts', 5);
