<?php

namespace App\Customizer;

add_action('customize_register', function (\WP_Customize_Manager $wp_customize) {

    $sanitize_select = function (array $allowed, $default) {
        return function ($value) use ($allowed, $default) {
            return in_array($value, $allowed, true) ? $value : $default;
        };
    };

    $sanitize_float_range = function (float $min, float $max, float $fallback) {
        return function ($value) use ($min, $max, $fallback) {
            if (! is_numeric($value)) {
                return $fallback;
            }
            $value = (float) $value;
            if ($value < $min) {
                return $min;
            }
            if ($value > $max) {
                return $max;
            }
            return $value;
        };
    };

    $format_rem = function ($value): string {
        $value = is_numeric($value) ? (float) $value : 0.0;
        $formatted = rtrim(rtrim(number_format($value, 2, '.', ''), '0'), '.');
        return $formatted === '' ? '0' : $formatted;
    };

    $active_when = function (string $setting_id, $equals) {
        return function ($control) use ($setting_id, $equals) {
            $setting = $control->manager->get_setting($setting_id);
            $value   = $setting ? $setting->value() : null;
            return $value === $equals;
        };
    };

    $active_when_footer_variant = function (string $variant) {
        return function ($control) use ($variant) {
            $setting = $control->manager->get_setting('hayden_footer_variant');
            return $setting && $setting->value() === $variant;
        };
    };

    $is_turbo_blocks_pro_active = function_exists('turbo_blocks_is_pro_active') && turbo_blocks_is_pro_active();

    /**
     * MASTER PANEL
     */
    $wp_customize->add_panel('hayden_theme_panel', [
        'title'       => __('Theme Settings', 'turbo'),
        'description' => __('Global layout, footer, grid and colours.', 'turbo'),
        'priority'    => 10,
    ]);

    /**
     * HEADER
     */
    $wp_customize->add_section('hayden_header_section', [
        'title'       => __('Header Layout', 'turbo'),
        'description' => __('Choose a layout style for the header.', 'turbo'),
        'priority'    => 20,
        'panel'       => 'hayden_theme_panel',
    ]);

    $header_layout_allowed = $is_turbo_blocks_pro_active
        ? ['default', 'logo-top', 'nav-center-cta', 'none']
        : ['default', 'logo-top'];

    $header_layout_choices = [
        'default'  => __('Default – logo left, nav right', 'turbo'),
        'logo-top' => __('Logo top, nav below', 'turbo'),
    ];

    if ($is_turbo_blocks_pro_active) {
        $header_layout_choices['nav-center-cta'] = __('Logo left, nav right, CTA right', 'turbo');
        $header_layout_choices['none'] = __('None (disable theme header)', 'turbo');
    }

    $wp_customize->add_setting('hayden_header_layout', [
        'default'           => 'default',
        'transport'         => 'refresh',
        'sanitize_callback' => $sanitize_select($header_layout_allowed, 'default'),
    ]);

    $wp_customize->add_control('hayden_header_layout_control', [
        'label'    => __('Header layout style', 'turbo'),
        'section'  => 'hayden_header_section',
        'settings' => 'hayden_header_layout',
        'type'     => 'select',
        'choices'  => $header_layout_choices,
    ]);

    if (! $is_turbo_blocks_pro_active) {
        $wp_customize->add_setting('turbo_blocks_pro_notice_header_layout', [
            'sanitize_callback' => '__return_null',
        ]);

        $wp_customize->add_control(new \WP_Customize_Control(
            $wp_customize,
            'turbo_blocks_pro_notice_header_layout_control',
            [
                'section'     => 'hayden_header_section',
                'settings'    => 'turbo_blocks_pro_notice_header_layout',
                'type'        => 'custom',
                'priority'    => 80,
                'description' => function_exists(__NAMESPACE__ . '\\turbo_blocks_render_pro_notice')
                    ? turbo_blocks_render_pro_notice(__('Unlock header CTA layout, disabled header layout, and sticky header in Turbo Blocks Pro.', 'turbo'))
                    : '<p style="margin:8px 0 0;padding:10px 12px;border-left:4px solid #f97316;background:#111827;color:#f9fafb;">'
                        . esc_html__('Unlock header CTA layout, disabled header layout, and sticky header in Turbo Blocks Pro.', 'turbo')
                        . '</p>',
            ]
        ));
    }

    $wp_customize->add_setting('hayden_header_content_page_id', [
        'default'           => (int) get_option('hayden_header_page_id', 0),
        'transport'         => 'refresh',
        'sanitize_callback' => 'absint',
    ]);

    $wp_customize->add_control('hayden_header_content_page_id_control', [
        'label'           => __('Header content page (blocks)', 'turbo'),
        'description'     => __('Choose the page whose blocks will render as the header when Header Layout is set to “None”.', 'turbo'),
        'section'         => 'hayden_header_section',
        'settings'        => 'hayden_header_content_page_id',
        'type'            => 'dropdown-pages',
        'active_callback' => $active_when('hayden_header_layout', 'none'),
    ]);

    $wp_customize->add_setting('hayden_header_sticky', [
        'default'   => 0,
        'transport' => 'refresh',
        'sanitize_callback' => function ($value) use ($is_turbo_blocks_pro_active) {
            if (! $is_turbo_blocks_pro_active) {
                return 0;
            }

            return absint($value) ? 1 : 0;
        },
    ]);

    if ($is_turbo_blocks_pro_active) {
        $wp_customize->add_control('hayden_header_sticky', [
            'type'        => 'checkbox',
            'section'     => 'hayden_header_section',
            'label'       => __('Sticky header', 'turbo'),
            'description' => __('Keep the header visible while scrolling (works with any header layout).', 'turbo'),
        ]);
    }

    $wp_customize->add_setting('hayden_header_full_width', [
        'default'           => 0,
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ]);

    $wp_customize->add_control('hayden_header_full_width', [
        'type'        => 'checkbox',
        'section'     => 'hayden_header_section',
        'label'       => __('Full-width header', 'turbo'),
        'description' => __('Header spans full width (navigation container becomes fluid).', 'turbo'),
    ]);

    $wp_customize->add_setting('hayden_nav_link_style', [
        'default'           => 'basic',
        'transport'         => 'refresh',
        'sanitize_callback' => $sanitize_select(['basic', 'pill', 'underline'], 'basic'),
    ]);

    $wp_customize->add_control('hayden_nav_link_style_control', [
        'label'       => __('Navigation link style', 'turbo'),
        'description' => __('Applies to top-level menu links only (dropdown styling stays consistent).', 'turbo'),
        'section'     => 'hayden_header_section',
        'settings'    => 'hayden_nav_link_style',
        'type'        => 'radio',
        'choices'     => [
            'basic'     => __('Basic (text hover)', 'turbo'),
            'pill'      => __('Pilled (background hover)', 'turbo'),
            'underline' => __('Underlined (underline hover)', 'turbo'),
        ],
    ]);

    $wp_customize->add_setting('hayden_header_cta_label', [
        'default'           => __('Start a Project', 'turbo'),
        'transport'         => 'refresh',
        'sanitize_callback' => 'sanitize_text_field',
    ]);

    $wp_customize->add_control('hayden_header_cta_label_control', [
        'label'           => __('CTA button text', 'turbo'),
        'section'         => 'hayden_header_section',
        'settings'        => 'hayden_header_cta_label',
        'type'            => 'text',
        'active_callback' => $active_when('hayden_header_layout', 'nav-center-cta'),
    ]);

    $wp_customize->add_setting('hayden_header_cta_url', [
        'default'           => home_url('/start-a-project'),
        'transport'         => 'refresh',
        'sanitize_callback' => 'esc_url_raw',
    ]);

    $wp_customize->add_control('hayden_header_cta_url_control', [
        'label'           => __('CTA button URL', 'turbo'),
        'section'         => 'hayden_header_section',
        'settings'        => 'hayden_header_cta_url',
        'type'            => 'url',
        'active_callback' => $active_when('hayden_header_layout', 'nav-center-cta'),
    ]);

    $wp_customize->add_setting('hayden_logo_max_height', [
        'default'           => 80,
        'transport'         => 'refresh',
        'sanitize_callback' => 'absint',
    ]);

    $wp_customize->add_control('hayden_logo_max_height_control', [
        'label'       => __('Logo max height (px)', 'turbo'),
        'section'     => 'hayden_header_section',
        'settings'    => 'hayden_logo_max_height',
        'type'        => 'number',
        'input_attrs' => ['min' => 40, 'max' => 200, 'step' => 4],
        'description' => __('Controls the rendered logo height when a custom logo is set.', 'turbo'),
    ]);

    /**
     * BORDER RADIUS
     */
    $wp_customize->add_section('hayden_design_section', [
        'title'       => __('Border Radius', 'turbo'),
        'description' => __('Control rounded corners across buttons, cards, widgets, and other elements.', 'turbo'),
        'priority'    => 25,
        'panel'       => 'hayden_theme_panel',
    ]);

    $wp_customize->add_setting('hayden_radius_style', [
        'default'           => 'soft',
        'transport'         => 'postMessage',
        'sanitize_callback' => $sanitize_select(['sharp', 'soft', 'round'], 'soft'),
    ]);

    $wp_customize->add_control('hayden_radius_style_control', [
        'label'       => __('Radius style', 'turbo'),
        'description' => __('Controls rounded corners across buttons, cards, widgets, menus, dropdowns, etc.', 'turbo'),
        'section'     => 'hayden_design_section',
        'settings'    => 'hayden_radius_style',
        'type'        => 'radio',
        'choices'     => [
            'sharp' => __('Sharp (0)', 'turbo'),
            'soft'  => __('Soft (default)', 'turbo'),
            'round' => __('Rounded', 'turbo'),
        ],
    ]);

    $radius_defaults = presets_radius();
    $soft_default = (float) str_replace('rem', '', (string) ($radius_defaults['soft']['2xl'] ?? '1'));
    $round_default = (float) str_replace('rem', '', (string) ($radius_defaults['round']['2xl'] ?? '1.25'));

    $current_soft = get_theme_mod('hayden_radius_soft_scale', $soft_default);
    $current_round = get_theme_mod('hayden_radius_round_scale', $round_default);

    $wp_customize->add_setting('hayden_radius_scale_intro', ['sanitize_callback' => '__return_null']);
    $wp_customize->add_control(new \WP_Customize_Control(
        $wp_customize,
        'hayden_radius_scale_intro_control',
        [
            'section'     => 'hayden_design_section',
            'settings'    => 'hayden_radius_scale_intro',
            'type'        => 'custom',
            'description' => '<p class="hayden-customizer-intro" style="margin:10px 0 6px;color:#ffffff;">'
                . esc_html__('Radius scale values', 'turbo')
                . '</p><p style="margin:0 0 10px;color:#9ca3af;">'
                . esc_html__('Adjust the Soft and Rounded radius presets used across the theme.', 'turbo')
                . '</p>',
        ]
    ));

    $wp_customize->add_setting('hayden_radius_soft_scale', [
        'default'           => $soft_default,
        'transport'         => 'postMessage',
        'sanitize_callback' => function ($value) use ($sanitize_float_range, $soft_default, $is_turbo_blocks_pro_active) {
            if (! $is_turbo_blocks_pro_active) {
                return $soft_default;
            }

            $sanitize = $sanitize_float_range(0.25, 2.5, $soft_default);
            return $sanitize($value);
        },
    ]);

    $wp_customize->add_control('hayden_radius_soft_scale_control', [
        'label'       => __('Soft radius', 'turbo'),
        'section'     => 'hayden_design_section',
        'settings'    => 'hayden_radius_soft_scale',
        'type'        => 'range',
        'input_attrs' => [
            'min'   => 0.25,
            'max'   => 2.5,
            'step'  => 0.05,
            'class' => 'hayden-radius-soft-range',
        ],
        'description' => sprintf('<span id="hayden-radius-soft-value">%s</span>rem', $format_rem($current_soft)),
    ]);

    $wp_customize->add_setting('hayden_radius_round_scale', [
        'default'           => $round_default,
        'transport'         => 'postMessage',
        'sanitize_callback' => function ($value) use ($sanitize_float_range, $round_default, $is_turbo_blocks_pro_active) {
            if (! $is_turbo_blocks_pro_active) {
                return $round_default;
            }

            $sanitize = $sanitize_float_range(0.5, 3, $round_default);
            return $sanitize($value);
        },
    ]);

    $wp_customize->add_control('hayden_radius_round_scale_control', [
        'label'       => __('Rounded radius', 'turbo'),
        'section'     => 'hayden_design_section',
        'settings'    => 'hayden_radius_round_scale',
        'type'        => 'range',
        'input_attrs' => [
            'min'   => 0.5,
            'max'   => 3,
            'step'  => 0.05,
            'class' => 'hayden-radius-round-range',
        ],
        'description' => sprintf('<span id="hayden-radius-round-value">%s</span>rem', $format_rem($current_round)),
    ]);

    $radius_reset_defaults = [
        'soft'  => $soft_default,
        'round' => $round_default,
    ];

    $wp_customize->add_setting('hayden_radius_scale_reset', ['sanitize_callback' => '__return_null']);
    $wp_customize->add_control(new \WP_Customize_Control(
        $wp_customize,
        'hayden_radius_scale_reset_control',
        [
            'section'     => 'hayden_design_section',
            'settings'    => 'hayden_radius_scale_reset',
            'type'        => 'custom',
            'description' => '<button type="button" class="button button-secondary hayden-radius-reset" data-defaults="'
                . esc_attr(wp_json_encode($radius_reset_defaults))
                . '">' . esc_html__('Reset', 'turbo') . '</button>',
        ]
    ));

    /**
     * LAYOUT / CONTAINER
     */
    $wp_customize->add_section('hayden_layout_section', [
        'title'       => __('Site Width', 'turbo'),
        'description' => __('Control the maximum width of the main content.', 'turbo'),
        'priority'    => 23,
        'panel'       => 'hayden_theme_panel',
    ]);

    $default_container_width = 1120;

    $wp_customize->add_setting('hayden_container_width', [
        'default'           => $default_container_width,
        'transport'         => 'refresh',
        'sanitize_callback' => 'absint',
    ]);

    $current_width = (int) get_theme_mod('hayden_container_width', $default_container_width);

    $wp_customize->add_control(new \WP_Customize_Control(
        $wp_customize,
        'hayden_container_width_control',
        [
            'label'       => __('Site content max width (px)', 'turbo'),
            'section'     => 'hayden_layout_section',
            'settings'    => 'hayden_container_width',
            'type'        => 'range',
            'input_attrs' => [
                'min'   => 960,
                'max'   => 1920,
                'step'  => 10,
                'class' => 'hayden-container-width-range',
            ],
            'description' => sprintf('<span id="hayden-container-width-value">%dpx</span>', $current_width),
        ]
    ));

    /**
     * BLOG OPTIONS
     */
    $wp_customize->add_section('hayden_blog_section', [
        'title'       => __('Blog Options', 'turbo'),
        'description' => __('Global defaults for single posts. Individual posts can override these settings.', 'turbo'),
        'priority'    => 24,
        'panel'       => 'hayden_theme_panel',
    ]);

    $wp_customize->add_setting('hayden_single_show_sidebar', [
        'default'           => 0,
        'transport'         => 'refresh',
        'sanitize_callback' => 'absint',
    ]);

    $wp_customize->add_control('hayden_single_show_sidebar_control', [
        'label'    => __('Single post sidebar', 'turbo'),
        'section'  => 'hayden_blog_section',
        'settings' => 'hayden_single_show_sidebar',
        'type'     => 'radio',
        'choices'  => [
            1 => __('Show sidebar', 'turbo'),
            0 => __('Hide sidebar', 'turbo'),
        ],
    ]);

    $wp_customize->add_setting('hayden_single_show_featured', [
        'default'           => 1,
        'transport'         => 'refresh',
        'sanitize_callback' => 'absint',
    ]);

    $wp_customize->add_control('hayden_single_show_featured_control', [
        'label'    => __('Single post featured image', 'turbo'),
        'section'  => 'hayden_blog_section',
        'settings' => 'hayden_single_show_featured',
        'type'     => 'radio',
        'choices'  => [
            1 => __('Show featured image', 'turbo'),
            0 => __('Hide featured image', 'turbo'),
        ],
    ]);

    /**
     * GLOBAL SPACING
     */
    $wp_customize->add_section('hayden_spacing_section', [
        'title'       => __('Block Spacing', 'turbo'),
        'description' => __('Controls vertical spacing between blocks and internal padding.', 'turbo'),
        'priority'    => 25,
        'panel'       => 'hayden_theme_panel',
    ]);

    $wp_customize->add_setting('hayden_spacing_scale', [
        'default'           => 'medium',
        'transport'         => 'postMessage',
        'sanitize_callback' => $sanitize_select(['none', 'small', 'medium', 'large'], 'medium'),
    ]);

    $wp_customize->add_control('hayden_spacing_scale_control', [
        'label'       => __('Vertical spacing scale', 'turbo'),
        'section'     => 'hayden_spacing_section',
        'settings'    => 'hayden_spacing_scale',
        'type'        => 'radio',
        'choices'     => [
            'none'   => __('None', 'turbo'),
            'small'  => __('Small', 'turbo'),
            'medium' => __('Medium', 'turbo'),
            'large'  => __('Large', 'turbo'),
        ],
        'description' => __('Affects global section/block spacing via CSS variables.', 'turbo'),
    ]);


/**
 * Block inner spacing (padding) - Vertical and Horizontal
 */
$wp_customize->add_setting('hayden_block_padding_y', [
    'default'           => 'medium',
    'transport'         => 'postMessage',
    'sanitize_callback' => $sanitize_select(['small', 'medium', 'large'], 'medium'),
]);

$wp_customize->add_control('hayden_block_padding_y_control', [
    'label'       => __('Block inner spacing - Top/Bottom', 'turbo'),
    'description' => __('Controls vertical padding inside blocks such as hero sections and content cards.', 'turbo'),
    'section'     => 'hayden_spacing_section',
    'settings'    => 'hayden_block_padding_y',
    'type'        => 'radio',
    'choices'     => [
        'small'  => __('Small', 'turbo'),
        'medium' => __('Medium', 'turbo'),
        'large'  => __('Large', 'turbo'),
    ],
]);

$wp_customize->add_setting('hayden_block_padding_x', [
    'default'           => 'medium',
    'transport'         => 'postMessage',
    'sanitize_callback' => $sanitize_select(['small', 'medium', 'large'], 'medium'),
]);

$wp_customize->add_control('hayden_block_padding_x_control', [
    'label'       => __('Block inner spacing - Left/Right', 'turbo'),
    'description' => __('Controls horizontal padding inside blocks.', 'turbo'),
    'section'     => 'hayden_spacing_section',
    'settings'    => 'hayden_block_padding_x',
    'type'        => 'radio',
    'choices'     => [
        'small'  => __('Small', 'turbo'),
        'medium' => __('Medium', 'turbo'),
        'large'  => __('Large', 'turbo'),
    ],
]);

    $wp_customize->add_setting('hayden_spacing_scale_advanced', [
        'default'           => 0,
        'transport'         => 'refresh',
        'sanitize_callback' => 'absint',
    ]);

    $wp_customize->add_control('hayden_spacing_scale_advanced_control', [
        'type'        => 'checkbox',
        'section'     => 'hayden_spacing_section',
        'settings'    => 'hayden_spacing_scale_advanced',
        'label'       => __('Customize spacing scales (advanced)', 'turbo'),
        'description' => __('Show additional sliders for fine-tuning spacing scale values.', 'turbo'),
    ]);

    $spacing_defaults = presets_spacing();

    $spacing_small_default = (float) str_replace('rem', '', (string) ($spacing_defaults['small']['desktop'] ?? '4'));
    $spacing_medium_default = (float) str_replace('rem', '', (string) ($spacing_defaults['medium']['desktop'] ?? '6'));
    $spacing_large_default = (float) str_replace('rem', '', (string) ($spacing_defaults['large']['desktop'] ?? '8'));

    $block_padding_small_default = (float) str_replace('rem', '', (string) ($spacing_defaults['compact']['desktop'] ?? '3'));
    $block_padding_medium_default = (float) str_replace('rem', '', (string) ($spacing_defaults['comfortable']['desktop'] ?? '4'));
    $block_padding_large_default = (float) str_replace('rem', '', (string) ($spacing_defaults['spacious']['desktop'] ?? '6'));

    $current_spacing_small = get_theme_mod('hayden_spacing_value_small', $spacing_small_default);
    $current_spacing_medium = get_theme_mod('hayden_spacing_value_medium', $spacing_medium_default);
    $current_spacing_large = get_theme_mod('hayden_spacing_value_large', $spacing_large_default);

    $current_block_padding_y_small = get_theme_mod('hayden_block_padding_value_small', $block_padding_small_default);
    $current_block_padding_y_medium = get_theme_mod('hayden_block_padding_value_medium', $block_padding_medium_default);
    $current_block_padding_y_large = get_theme_mod('hayden_block_padding_value_large', $block_padding_large_default);

    $current_block_padding_x_small = get_theme_mod('hayden_block_padding_x_value_small', $block_padding_small_default);
    $current_block_padding_x_medium = get_theme_mod('hayden_block_padding_x_value_medium', $block_padding_medium_default);
    $current_block_padding_x_large = get_theme_mod('hayden_block_padding_x_value_large', $block_padding_large_default);

    $wp_customize->add_setting('hayden_spacing_scale_intro', ['sanitize_callback' => '__return_null']);
    $wp_customize->add_control(new \WP_Customize_Control(
        $wp_customize,
        'hayden_spacing_scale_intro_control',
        [
            'section'     => 'hayden_spacing_section',
            'settings'    => 'hayden_spacing_scale_intro',
            'type'        => 'custom',
            'active_callback' => $active_when('hayden_spacing_scale_advanced', 1),
            'description' => '<p class="hayden-customizer-intro" style="margin:10px 0 6px;color:#ffffff;">'
                . esc_html__('Vertical spacing scale values', 'turbo')
                . '</p><p style="margin:0 0 10px;color:#9ca3af;">'
                . esc_html__('These values define what Small / Medium / Large mean for vertical spacing.', 'turbo')
                . '</p>',
        ]
    ));

    $wp_customize->add_setting('hayden_spacing_value_small', [
        'default'           => $spacing_small_default,
        'transport'         => 'postMessage',
        'sanitize_callback' => $sanitize_float_range(1, 6, $spacing_small_default),
    ]);

    $wp_customize->add_control('hayden_spacing_value_small_control', [
        'label'       => __('Small spacing', 'turbo'),
        'section'     => 'hayden_spacing_section',
        'settings'    => 'hayden_spacing_value_small',
        'type'        => 'range',
        'active_callback' => $active_when('hayden_spacing_scale_advanced', 1),
        'input_attrs' => [
            'min'   => 1,
            'max'   => 6,
            'step'  => 0.25,
            'class' => 'hayden-spacing-small-range',
        ],
        'description' => sprintf('<span id="hayden-spacing-small-value">%s</span>rem', $format_rem($current_spacing_small)),
    ]);

    $wp_customize->add_setting('hayden_spacing_value_medium', [
        'default'           => $spacing_medium_default,
        'transport'         => 'postMessage',
        'sanitize_callback' => $sanitize_float_range(2, 8, $spacing_medium_default),
    ]);

    $wp_customize->add_control('hayden_spacing_value_medium_control', [
        'label'       => __('Medium spacing', 'turbo'),
        'section'     => 'hayden_spacing_section',
        'settings'    => 'hayden_spacing_value_medium',
        'type'        => 'range',
        'active_callback' => $active_when('hayden_spacing_scale_advanced', 1),
        'input_attrs' => [
            'min'   => 2,
            'max'   => 8,
            'step'  => 0.25,
            'class' => 'hayden-spacing-medium-range',
        ],
        'description' => sprintf('<span id="hayden-spacing-medium-value">%s</span>rem', $format_rem($current_spacing_medium)),
    ]);

    $wp_customize->add_setting('hayden_spacing_value_large', [
        'default'           => $spacing_large_default,
        'transport'         => 'postMessage',
        'sanitize_callback' => $sanitize_float_range(4, 12, $spacing_large_default),
    ]);

    $wp_customize->add_control('hayden_spacing_value_large_control', [
        'label'       => __('Large spacing', 'turbo'),
        'section'     => 'hayden_spacing_section',
        'settings'    => 'hayden_spacing_value_large',
        'type'        => 'range',
        'active_callback' => $active_when('hayden_spacing_scale_advanced', 1),
        'input_attrs' => [
            'min'   => 4,
            'max'   => 12,
            'step'  => 0.25,
            'class' => 'hayden-spacing-large-range',
        ],
        'description' => sprintf('<span id="hayden-spacing-large-value">%s</span>rem', $format_rem($current_spacing_large)),
    ]);

    $reset_defaults = [
        'small'  => $spacing_small_default,
        'medium' => $spacing_medium_default,
        'large'  => $spacing_large_default,
    ];

    $wp_customize->add_setting('hayden_spacing_scale_reset', ['sanitize_callback' => '__return_null']);
    $wp_customize->add_control(new \WP_Customize_Control(
        $wp_customize,
        'hayden_spacing_scale_reset_control',
        [
            'section'  => 'hayden_spacing_section',
            'settings' => 'hayden_spacing_scale_reset',
            'type'     => 'custom',
            'active_callback' => $active_when('hayden_spacing_scale_advanced', 1),
            'description' => '<button type="button" class="button button-secondary hayden-spacing-reset" data-defaults="'
                . esc_attr(wp_json_encode($reset_defaults))
                . '">' . esc_html__('Reset', 'turbo') . '</button>',
        ]
    ));

    $wp_customize->add_setting('hayden_block_padding_scale_intro', ['sanitize_callback' => '__return_null']);
    $wp_customize->add_control(new \WP_Customize_Control(
        $wp_customize,
        'hayden_block_padding_scale_intro_control',
        [
            'section'     => 'hayden_spacing_section',
            'settings'    => 'hayden_block_padding_scale_intro',
            'type'        => 'custom',
            'active_callback' => $active_when('hayden_spacing_scale_advanced', 1),
            'description' => '<hr style="margin:10px 0;border-color:rgba(255,255,255,0.2);">'
                . '<p class="hayden-customizer-intro" style="margin:10px 0 6px;color:#ffffff;">'
                . esc_html__('Block inner spacing scale values - Top/Bottom', 'turbo')
                . '</p><p style="margin:0 0 10px;color:#9ca3af;">'
                . esc_html__('These values define what Small / Medium / Large mean for block inner spacing (Top/Bottom).', 'turbo')
                . '</p>',
        ]
    ));

    $wp_customize->add_setting('hayden_block_padding_value_small', [
        'default'           => $block_padding_small_default,
        'transport'         => 'postMessage',
        'sanitize_callback' => $sanitize_float_range(1, 6, $block_padding_small_default),
    ]);

    $wp_customize->add_control('hayden_block_padding_value_small_control', [
        'label'       => __('Small inner spacing', 'turbo'),
        'section'     => 'hayden_spacing_section',
        'settings'    => 'hayden_block_padding_value_small',
        'type'        => 'range',
        'active_callback' => $active_when('hayden_spacing_scale_advanced', 1),
        'input_attrs' => [
            'min'   => 1,
            'max'   => 6,
            'step'  => 0.25,
            'class' => 'hayden-block-padding-small-range',
        ],
        'description' => sprintf('<span id="hayden-block-padding-small-value">%s</span>rem', $format_rem($current_block_padding_y_small)),
    ]);

    $wp_customize->add_setting('hayden_block_padding_value_medium', [
        'default'           => $block_padding_medium_default,
        'transport'         => 'postMessage',
        'sanitize_callback' => $sanitize_float_range(2, 8, $block_padding_medium_default),
    ]);

    $wp_customize->add_control('hayden_block_padding_value_medium_control', [
        'label'       => __('Medium inner spacing', 'turbo'),
        'section'     => 'hayden_spacing_section',
        'settings'    => 'hayden_block_padding_value_medium',
        'type'        => 'range',
        'active_callback' => $active_when('hayden_spacing_scale_advanced', 1),
        'input_attrs' => [
            'min'   => 2,
            'max'   => 8,
            'step'  => 0.25,
            'class' => 'hayden-block-padding-medium-range',
        ],
        'description' => sprintf('<span id="hayden-block-padding-medium-value">%s</span>rem', $format_rem($current_block_padding_y_medium)),
    ]);

    $wp_customize->add_setting('hayden_block_padding_value_large', [
        'default'           => $block_padding_large_default,
        'transport'         => 'postMessage',
        'sanitize_callback' => $sanitize_float_range(4, 12, $block_padding_large_default),
    ]);

    $wp_customize->add_control('hayden_block_padding_value_large_control', [
        'label'       => __('Large inner spacing', 'turbo'),
        'section'     => 'hayden_spacing_section',
        'settings'    => 'hayden_block_padding_value_large',
        'type'        => 'range',
        'active_callback' => $active_when('hayden_spacing_scale_advanced', 1),
        'input_attrs' => [
            'min'   => 4,
            'max'   => 12,
            'step'  => 0.25,
            'class' => 'hayden-block-padding-large-range',
        ],
        'description' => sprintf('<span id="hayden-block-padding-large-value">%s</span>rem', $format_rem($current_block_padding_y_large)),
    ]);

    $block_padding_reset_defaults = [
        'small'  => $block_padding_small_default,
        'medium' => $block_padding_medium_default,
        'large'  => $block_padding_large_default,
    ];

    $wp_customize->add_setting('hayden_block_padding_scale_reset', ['sanitize_callback' => '__return_null']);
    $wp_customize->add_control(new \WP_Customize_Control(
        $wp_customize,
        'hayden_block_padding_scale_reset_control',
        [
            'section'  => 'hayden_spacing_section',
            'settings' => 'hayden_block_padding_scale_reset',
            'type'     => 'custom',
            'active_callback' => $active_when('hayden_spacing_scale_advanced', 1),
            'description' => '<button type="button" class="button button-secondary hayden-block-padding-reset" data-defaults="'
                . esc_attr(wp_json_encode($block_padding_reset_defaults))
                . '">' . esc_html__('Reset', 'turbo') . '</button>',
        ]
    ));

    $wp_customize->add_setting('hayden_block_padding_x_scale_intro', ['sanitize_callback' => '__return_null']);
    $wp_customize->add_control(new \WP_Customize_Control(
        $wp_customize,
        'hayden_block_padding_x_scale_intro_control',
        [
            'section'     => 'hayden_spacing_section',
            'settings'    => 'hayden_block_padding_x_scale_intro',
            'type'        => 'custom',
            'active_callback' => $active_when('hayden_spacing_scale_advanced', 1),
            'description' => '<hr style="margin:10px 0;border-color:rgba(255,255,255,0.2);">'
                . '<p class="hayden-customizer-intro" style="margin:10px 0 6px;color:#ffffff;">'
                . esc_html__('Block inner spacing scale values - Left/Right', 'turbo')
                . '</p><p style="margin:0 0 10px;color:#9ca3af;">'
                . esc_html__('These values define what Small / Medium / Large mean for block inner spacing (Left/Right).', 'turbo')
                . '</p>',
        ]
    ));

    $wp_customize->add_setting('hayden_block_padding_x_value_small', [
        'default'           => $block_padding_small_default,
        'transport'         => 'postMessage',
        'sanitize_callback' => $sanitize_float_range(1, 6, $block_padding_small_default),
    ]);

    $wp_customize->add_control('hayden_block_padding_x_value_small_control', [
        'label'       => __('Small inner spacing (Left/Right)', 'turbo'),
        'section'     => 'hayden_spacing_section',
        'settings'    => 'hayden_block_padding_x_value_small',
        'type'        => 'range',
        'active_callback' => $active_when('hayden_spacing_scale_advanced', 1),
        'input_attrs' => [
            'min'   => 1,
            'max'   => 6,
            'step'  => 0.25,
            'class' => 'hayden-block-padding-x-small-range',
        ],
        'description' => sprintf('<span id="hayden-block-padding-x-small-value">%s</span>rem', $format_rem($current_block_padding_x_small)),
    ]);

    $wp_customize->add_setting('hayden_block_padding_x_value_medium', [
        'default'           => $block_padding_medium_default,
        'transport'         => 'postMessage',
        'sanitize_callback' => $sanitize_float_range(2, 8, $block_padding_medium_default),
    ]);

    $wp_customize->add_control('hayden_block_padding_x_value_medium_control', [
        'label'       => __('Medium inner spacing (Left/Right)', 'turbo'),
        'section'     => 'hayden_spacing_section',
        'settings'    => 'hayden_block_padding_x_value_medium',
        'type'        => 'range',
        'active_callback' => $active_when('hayden_spacing_scale_advanced', 1),
        'input_attrs' => [
            'min'   => 2,
            'max'   => 8,
            'step'  => 0.25,
            'class' => 'hayden-block-padding-x-medium-range',
        ],
        'description' => sprintf('<span id="hayden-block-padding-x-medium-value">%s</span>rem', $format_rem($current_block_padding_x_medium)),
    ]);

    $wp_customize->add_setting('hayden_block_padding_x_value_large', [
        'default'           => $block_padding_large_default,
        'transport'         => 'postMessage',
        'sanitize_callback' => $sanitize_float_range(4, 12, $block_padding_large_default),
    ]);

    $wp_customize->add_control('hayden_block_padding_x_value_large_control', [
        'label'       => __('Large inner spacing (Left/Right)', 'turbo'),
        'section'     => 'hayden_spacing_section',
        'settings'    => 'hayden_block_padding_x_value_large',
        'type'        => 'range',
        'active_callback' => $active_when('hayden_spacing_scale_advanced', 1),
        'input_attrs' => [
            'min'   => 4,
            'max'   => 12,
            'step'  => 0.25,
            'class' => 'hayden-block-padding-x-large-range',
        ],
        'description' => sprintf('<span id="hayden-block-padding-x-large-value">%s</span>rem', $format_rem($current_block_padding_x_large)),
    ]);

    $block_padding_x_reset_defaults = [
        'small'  => $block_padding_small_default,
        'medium' => $block_padding_medium_default,
        'large'  => $block_padding_large_default,
    ];

    $wp_customize->add_setting('hayden_block_padding_x_scale_reset', ['sanitize_callback' => '__return_null']);
    $wp_customize->add_control(new \WP_Customize_Control(
        $wp_customize,
        'hayden_block_padding_x_scale_reset_control',
        [
            'section'  => 'hayden_spacing_section',
            'settings' => 'hayden_block_padding_x_scale_reset',
            'type'     => 'custom',
            'active_callback' => $active_when('hayden_spacing_scale_advanced', 1),
            'description' => '<button type="button" class="button button-secondary hayden-block-padding-x-reset" data-defaults="'
                . esc_attr(wp_json_encode($block_padding_x_reset_defaults))
                . '">' . esc_html__('Reset', 'turbo') . '</button>',
        ]
    ));




    /**
     * THEME COLOURS
     */
    $wp_customize->add_section('hayden_color_section', [
        'title'       => __('Theme Palette', 'turbo'),
        'description' => '',
        'priority'    => 27,
        'panel'       => 'hayden_theme_panel',
    ]);

    $wp_customize->add_setting('hayden_color_intro', ['sanitize_callback' => '__return_null']);
    $wp_customize->add_control(new \WP_Customize_Control(
        $wp_customize,
        'hayden_color_intro_control',
        [
            'section'     => 'hayden_color_section',
            'settings'    => 'hayden_color_intro',
            'type'        => 'custom',
            'description' => '<p class="hayden-customizer-intro" style="margin:0 0 8px;color:#ffffff;">'
                . esc_html__('Control global brand styling.', 'turbo')
                . '</p>',
        ]
    ));

    $add_color = function (string $id, string $label, string $default, array $args = []) use ($wp_customize) {
        $wp_customize->add_setting($id, [
            'default'           => $default,
            'transport'         => 'postMessage',
            'sanitize_callback' => 'sanitize_hex_color',
        ]);

        $wp_customize->add_control(new \WP_Customize_Color_Control(
            $wp_customize,
            "{$id}_control",
            array_merge([
                'label'    => __($label, 'turbo'),
                'section'  => 'hayden_color_section',
                'settings' => $id,
            ], $args)
        ));
    };

    $add_color('hayden_primary_color',        'Primary',                 '#f97316');
    $add_color('hayden_surface_color',        'Background',              '#FFFAF8');
    $add_color('hayden_heading_color',        'Heading text',            '#f97316');
    $add_color('hayden_body_color',           'Body text',               '#111111');

    $wp_customize->add_setting('hayden_color_heading_widgets', ['sanitize_callback' => '__return_null']);
    $wp_customize->add_control(new \WP_Customize_Control(
        $wp_customize,
        'hayden_color_heading_widgets_control',
        [
            'section'     => 'hayden_color_section',
            'settings'    => 'hayden_color_heading_widgets',
            'type'        => 'custom',
            'description' => '<hr style="margin:10px 0;border-color:rgba(255,255,255,0.2);">'
                . '<h3 style="margin:4px 0 4px;font-weight:600;color:#ffffff;">'
                . esc_html__('Widgets', 'turbo')
                . '</h3>',
        ]
    ));

    $add_color('hayden_widget_bg_color',      'Widget background',       '#000000');
    $add_color('hayden_widget_title_color',   'Widget title',            '#f97316');
    $add_color('hayden_widget_text_color',    'Widget text',             '#ffffff');
    $add_color('hayden_widget_link_color',    'Widget link',             '#f97316');

    $wp_customize->add_setting('hayden_color_heading_footer_widgets', ['sanitize_callback' => '__return_null']);
    $wp_customize->add_control(new \WP_Customize_Control(
        $wp_customize,
        'hayden_color_heading_footer_widgets_control',
        [
            'section'     => 'hayden_color_section',
            'settings'    => 'hayden_color_heading_footer_widgets',
            'type'        => 'custom',
            'description' => '<hr style="margin:10px 0;border-color:rgba(255,255,255,0.2);">'
                . '<h3 style="margin:4px 0 4px;font-weight:600;color:#ffffff;">'
                . esc_html__('Footer', 'turbo')
                . '</h3>',
        ]
    ));

    // Footer colours (global)
    $add_color('hayden_footer_color',         'Footer background',       '#020617');
    $add_color('hayden_footer_text_color',    'Legal text',              '#94a3b8');

    $add_color('hayden_footer_widget_title_color', 'Footer widget title', '#f97316');
    $add_color('hayden_footer_widget_text_color',  'Footer widget text',  '#ffffff');
    $add_color('hayden_footer_widget_link_color',  'Footer widget link',  '#f97316');

    $wp_customize->add_setting('hayden_color_heading_nav', ['sanitize_callback' => '__return_null']);
    $wp_customize->add_control(new \WP_Customize_Control(
        $wp_customize,
        'hayden_color_heading_nav_control',
        [
            'section'     => 'hayden_color_section',
            'settings'    => 'hayden_color_heading_nav',
            'type'        => 'custom',
            'description' => '<hr style="margin:10px 0;border-color:rgba(255,255,255,0.2);">'
                . '<h3 style="margin:4px 0 4px;font-weight:600;color:#ffffff;">'
                . esc_html__('Navigation', 'turbo')
                . '</h3>',
        ]
    ));

    $add_color('hayden_nav_link_color',         'Nav parent link',         '#111111');
    $add_color('hayden_nav_link_hover_color',   'Nav link hover/active',   '#f97316');
    $add_color('hayden_nav_sub_bg_color',       'Dropdown background',     '#020617');
    $add_color('hayden_nav_sub_link_color',     'Dropdown link',           '#f97316');
    $add_color('hayden_nav_sub_hover_bg_color', 'Dropdown link hover background', '#3b1d08');

    /**
     * CARDS & GRID
     */
    $wp_customize->add_section('hayden_cards_grid_section', [
        'title'       => __('Cards & Grid', 'turbo'),
        'description' => __('Card styling and grid display controls for blog, portfolio and other content.', 'turbo'),
        'priority'    => 28,
        'panel'       => 'hayden_theme_panel',
    ]);

    $card_colors = [
        'hayden_card_bg'         => ['Card Background',     '#000000'],
        'hayden_card_bg_alt'     => ['Card Background Alt', '#111111'],
        'hayden_card_heading'    => ['Card Heading',        '#f97316'],
        'hayden_card_text'       => ['Card Body Text',      '#ffffff'],
        'hayden_card_link'       => ['Card Link',           '#ffffff'],
    ];

    foreach ($card_colors as $id => [$label, $default]) {
        $wp_customize->add_setting($id, [
            'default'           => $default,
            'transport'         => 'postMessage',
            'sanitize_callback' => 'sanitize_hex_color',
        ]);

        $wp_customize->add_control(new \WP_Customize_Color_Control(
            $wp_customize,
            "{$id}_control",
            [
                'label'    => __($label, 'turbo'),
                'section'  => 'hayden_cards_grid_section',
                'settings' => $id,
            ]
        ));
    }

    $wp_customize->add_setting('grid_projects_initial_items', [
        'default'           => 6,
        'sanitize_callback' => function ($value) use ($is_turbo_blocks_pro_active) {
            if (! $is_turbo_blocks_pro_active) {
                return 6;
            }

            $value = absint($value);
            if ($value < 1) {
                return 1;
            }

            if ($value > 48) {
                return 48;
            }

            return $value;
        },
        'transport'         => 'refresh',
    ]);

    $wp_customize->add_control('grid_projects_initial_items_control', [
        'label'       => __('Projects: initial items', 'turbo'),
        'section'     => 'hayden_cards_grid_section',
        'settings'    => 'grid_projects_initial_items',
        'type'        => 'number',
        'input_attrs' => ['min' => 1, 'max' => 48],
    ]);

    $wp_customize->add_setting('grid_blog_initial_items', [
        'default'           => 6,
        'sanitize_callback' => function ($value) use ($is_turbo_blocks_pro_active) {
            if (! $is_turbo_blocks_pro_active) {
                return 6;
            }

            $value = absint($value);
            if ($value < 1) {
                return 1;
            }

            if ($value > 48) {
                return 48;
            }

            return $value;
        },
        'transport'         => 'refresh',
    ]);

    $wp_customize->add_control('grid_blog_initial_items_control', [
        'label'       => __('Blog: initial items', 'turbo'),
        'section'     => 'hayden_cards_grid_section',
        'settings'    => 'grid_blog_initial_items',
        'type'        => 'number',
        'input_attrs' => ['min' => 1, 'max' => 48],
    ]);

    /**
     * TYPOGRAPHY
     */
    $wp_customize->add_section('hayden_typography_section', [
        'title'       => __('Typography', 'turbo'),
        'description' => __('Upload custom fonts. Leave empty to use the default theme fonts.', 'turbo'),
        'panel'       => 'hayden_theme_panel',
        'priority'    => 26,
    ]);

    $wp_customize->add_setting('hayden_font_serif_file', [
        'default'           => '',
        'transport'         => 'refresh',
        'sanitize_callback' => 'absint',
    ]);

    $wp_customize->add_control(new \WP_Customize_Media_Control(
        $wp_customize,
        'hayden_font_serif_file',
        [
            'label'       => __('Heading font (serif)', 'turbo'),
            'description' => __('Upload a WOFF2/WOFF/TTF file. This will replace the default --font-serif stack (your heading fonts).', 'turbo'),
            'section'     => 'hayden_typography_section',
            'mime_type'   => '',
        ]
    ));

    $wp_customize->add_setting('hayden_font_sans_file', [
        'default'           => '',
        'transport'         => 'refresh',
        'sanitize_callback' => 'absint',
    ]);

    $wp_customize->add_control(new \WP_Customize_Media_Control(
        $wp_customize,
        'hayden_font_sans_file',
        [
            'label'       => __('Body font (sans-serif)', 'turbo'),
            'description' => __('Upload a WOFF2/WOFF/TTF file. This will replace the default --font-sans stack (your body text fonts).', 'turbo'),
            'section'     => 'hayden_typography_section',
            'mime_type'   => '',
        ]
    ));

    $tw_font_sizes = [
        'text-sm'   => __('Small (Tailwind text-sm)', 'turbo'),
        'text-base' => __('Base (Tailwind text-base)', 'turbo'),
        'text-lg'   => __('Large (Tailwind text-lg)', 'turbo'),
        'text-xl'   => __('XL (Tailwind text-xl)', 'turbo'),
        'text-2xl'  => __('2XL (Tailwind text-2xl)', 'turbo'),
        'text-3xl'  => __('3XL (Tailwind text-3xl)', 'turbo'),
        'text-4xl'  => __('4XL (Tailwind text-4xl)', 'turbo'),
    ];

    foreach ([
        'hayden_body_font_size' => ['Body font size', 'text-lg'],
        'hayden_h1_font_size'   => ['H1 font size',   'text-4xl'],
        'hayden_h2_font_size'   => ['H2 font size',   'text-3xl'],
        'hayden_h3_font_size'   => ['H3 font size',   'text-2xl'],
    ] as $id => [$label, $default]) {
        $wp_customize->add_setting($id, [
            'default'           => $default,
            'transport'         => 'refresh',
            'sanitize_callback' => __NAMESPACE__ . '\\sanitize_font_scale',
        ]);

        $wp_customize->add_control("{$id}_control", [
            'label'    => __($label, 'turbo'),
            'section'  => 'hayden_typography_section',
            'settings' => $id,
            'type'     => 'select',
            'choices'  => $tw_font_sizes,
        ]);
    }

    /**
     * FOOTER LAYOUT
     */
    $wp_customize->add_section('hayden_footer_section', [
        'title'       => __('Footer Layout', 'turbo'),
        'description' => __('Choose a footer variant or use the widget-column layout.', 'turbo'),
        'priority'    => 21,
        'panel'       => 'hayden_theme_panel',
    ]);

    $footer_variant_allowed = $is_turbo_blocks_pro_active
        ? ['none', 'theme-default', 'footer-a', 'footer-b', 'footer-c']
        : ['theme-default', 'footer-a'];

    $footer_variant_choices = [
        'theme-default' => __('Theme default (widgets)', 'turbo'),
        'footer-a'      => __('Footer A', 'turbo'),
    ];

    if ($is_turbo_blocks_pro_active) {
        $footer_variant_choices['footer-b'] = __('Footer B', 'turbo');
        $footer_variant_choices['footer-c'] = __('Footer C', 'turbo');
        $footer_variant_choices['none'] = __('None (disable theme footer)', 'turbo');
    }

    $wp_customize->add_setting('hayden_footer_variant', [
        'default'           => 'theme-default',
        'transport'         => 'refresh',
        'sanitize_callback' => $sanitize_select($footer_variant_allowed, 'theme-default'),
    ]);

    $wp_customize->add_control('hayden_footer_variant_control', [
        'label'       => __('Footer variant', 'turbo'),
        'description' => __('Select which footer template to render.', 'turbo'),
        'section'     => 'hayden_footer_section',
        'settings'    => 'hayden_footer_variant',
        'type'        => 'select',
        'choices'     => $footer_variant_choices,
    ]);

    if (! $is_turbo_blocks_pro_active) {
        $wp_customize->add_setting('turbo_blocks_pro_notice_footer_layout', [
            'sanitize_callback' => '__return_null',
        ]);

        $wp_customize->add_control(new \WP_Customize_Control(
            $wp_customize,
            'turbo_blocks_pro_notice_footer_layout_control',
            [
                'section'     => 'hayden_footer_section',
                'settings'    => 'turbo_blocks_pro_notice_footer_layout',
                'type'        => 'custom',
                'priority'    => 80,
                'description' => function_exists(__NAMESPACE__ . '\\turbo_blocks_render_pro_notice')
                    ? turbo_blocks_render_pro_notice(__('Unlock Footer B, Footer C, and disabled footer layout in Turbo Blocks Pro.', 'turbo'))
                    : '<p style="margin:8px 0 0;padding:10px 12px;border-left:4px solid #f97316;background:#111827;color:#f9fafb;">'
                        . esc_html__('Unlock Footer B, Footer C, and disabled footer layout in Turbo Blocks Pro.', 'turbo')
                        . '</p>',
            ]
        ));
    }

    $wp_customize->add_setting('hayden_footer_content_page_id', [
        'default'           => (int) get_option('hayden_footer_page_id', 0),
        'transport'         => 'refresh',
        'sanitize_callback' => 'absint',
    ]);

    $wp_customize->add_control('hayden_footer_content_page_id_control', [
        'label'           => __('Footer content page (blocks)', 'turbo'),
        'description'     => __('Choose the page whose blocks will render as the footer when Footer Variant is set to “None”.', 'turbo'),
        'section'         => 'hayden_footer_section',
        'settings'        => 'hayden_footer_content_page_id',
        'type'            => 'dropdown-pages',
        'active_callback' => function ($control) use ($active_when, $is_turbo_blocks_pro_active) {
            return $is_turbo_blocks_pro_active && $active_when('hayden_footer_variant', 'none')($control);
        },
    ]);

    $wp_customize->add_setting('hayden_footer_c_cta_enabled', [
        'default'           => 1,
        'transport'         => 'refresh',
        'sanitize_callback' => 'absint',
    ]);

    $wp_customize->add_control('hayden_footer_c_cta_enabled_control', [
        'label'           => __('Footer C: Enable CTA panel', 'turbo'),
        'section'         => 'hayden_footer_section',
        'settings'        => 'hayden_footer_c_cta_enabled',
        'type'            => 'checkbox',
        'active_callback' => function ($control) use ($active_when_footer_variant, $is_turbo_blocks_pro_active) {
            return $is_turbo_blocks_pro_active && $active_when_footer_variant('footer-c')($control);
        },
    ]);

    $fields = [
        'kicker'     => ['Footer C: CTA kicker', 'Ready when you are'],
        'title'      => ['Footer C: CTA title', "Want a WordPress build that's fast, tidy, and easy to manage?"],
        'text'       => ['Footer C: CTA text', "Let's turn your design into a lightweight, high-performing site — with a clean editor experience for clients."],
        'btn1_label' => ['Footer C: Button 1 label', 'Start a project'],
        'btn1_url'   => ['Footer C: Button 1 URL', home_url('/contact')],
        'btn2_label' => ['Footer C: Button 2 label', 'View work'],
        'btn2_url'   => ['Footer C: Button 2 URL', home_url('/projects')],
    ];

    foreach ($fields as $key => [$label, $default]) {
        $setting_id = "hayden_footer_c_cta_{$key}";
        $is_url     = (substr($key, -4) === '_url');

        $wp_customize->add_setting($setting_id, [
            'default'           => $default,
            'transport'         => 'refresh',
            'sanitize_callback' => $is_url ? 'esc_url_raw' : 'sanitize_text_field',
        ]);

        $wp_customize->add_control("{$setting_id}_control", [
            'label'           => __($label, 'turbo'),
            'section'         => 'hayden_footer_section',
            'settings'        => $setting_id,
            'type'            => $is_url ? 'url' : 'text',
            'active_callback' => function ($control) use ($active_when_footer_variant, $is_turbo_blocks_pro_active) {
                if (! $is_turbo_blocks_pro_active) {
                    return false;
                }

                $is_footer_c = $active_when_footer_variant('footer-c')($control);

                $cta_setting = $control->manager->get_setting('hayden_footer_c_cta_enabled');
                $cta_on      = $cta_setting ? (bool) $cta_setting->value() : false;

                return $is_footer_c && $cta_on;
            },
        ]);
    }

    $wp_customize->add_setting('hayden_footer_columns', [
        'default'           => 3,
        'transport'         => 'refresh',
        'sanitize_callback' => __NAMESPACE__ . '\\sanitize_columns',
    ]);

    $wp_customize->add_control('hayden_footer_columns_control', [
        'label'       => __('Footer columns (widgets)', 'turbo'),
        'description' => __('Used by the theme default widget footer.', 'turbo'),
        'section'     => 'hayden_footer_section',
        'settings'    => 'hayden_footer_columns',
        'type'        => 'select',
        'choices'     => [
            1 => __('1 column', 'turbo'),
            2 => __('2 columns', 'turbo'),
            3 => __('3 columns', 'turbo'),
            4 => __('4 columns', 'turbo'),
        ],
    ]);

    $wp_customize->add_setting('hayden_footer_copyright_text', [
        'default'           => '',
        'transport'         => 'refresh',
        'sanitize_callback' => 'sanitize_text_field',
    ]);

    $wp_customize->add_control('hayden_footer_copyright_text_control', [
        'label'       => __('Footer copyright text', 'turbo'),
        'description' => __('Displayed in footer bottom bars. Leave blank to use the site title.', 'turbo'),
        'section'     => 'hayden_footer_section',
        'settings'    => 'hayden_footer_copyright_text',
        'type'        => 'text',
    ]);

    // Move widget sections into panel
    $footer_sidebars = ['sidebar-footer-1','sidebar-footer-2','sidebar-footer-3','sidebar-footer-4'];
    $priority        = 50;

    foreach ($footer_sidebars as $index => $sidebar_id) {
        $section_id = 'sidebar-widgets-' . $sidebar_id;
        $section    = $wp_customize->get_section($section_id);

        if ($section) {
            $section->panel    = 'hayden_theme_panel';
            $section->priority = $priority + $index;
            $section->title    = sprintf(__('Footer Column %d Widgets', 'turbo'), $index + 1);
        }
    }

    /**
     * Panel footer note
     */
    $wp_customize->add_section('hayden_panel_footer_section', [
        'title'       => __('Need Help?', 'turbo'),
        'description' => '',
        'priority'    => 200,
        'panel'       => 'hayden_theme_panel',
    ]);

    $wp_customize->add_setting('hayden_panel_footer_note', ['sanitize_callback' => '__return_null']);

    $site_url = esc_url('https://wp.bbi.co.uk');

    $html  = '<div class="hayden-customizer-panel-footer" style="margin:16px 0 24px;padding:12px 14px;border-radius:8px;background:#020617;border:1px solid rgba(148,163,184,0.4);color:#e5e7eb;font-size:13px;line-height:1.5;">';
    $html .= '  <div style="display:flex;align-items:center;gap:10px;">';
    $html .= '    <div style="flex:1 1 auto;">';
    $html .= '      <strong style="display:block;margin-bottom:2px;font-weight:600;">Hayden Sage Starter</strong>';
    $html .= '      <span style="display:block;margin-bottom:4px;opacity:.9;">Need help or want a custom build based on this theme?</span>';
    $html .= '      <a href="' . $site_url . '" target="_blank" rel="noopener" style="color:#f97316;text-decoration:none;">Visit wp.bbi.co.uk →</a>';
    $html .= '    </div>';
    $html .= '  </div>';
    $html .= '  <div style="margin-top:16px;display:flex;flex-direction:column;gap:10px;">';
    $html .= '    <a href="https://wp.bbi.co.uk/blog" target="_blank" rel="noopener" style="display:block;width:100%;text-align:center;padding:10px 14px;background:#1e293b;color:#fff;border-radius:6px;text-decoration:none;font-weight:600;">Blog</a>';
    $html .= '    <a href="https://wp.bbi.co.uk/contact" target="_blank" rel="noopener" style="display:block;width:100%;text-align:center;padding:10px 14px;background:#f97316;color:#000;border-radius:6px;text-decoration:none;font-weight:600;">Contact Us</a>';
    $html .= '  </div>';
    $html .= '</div>';

    $wp_customize->add_control(new \WP_Customize_Control(
        $wp_customize,
        'hayden_panel_footer_note_control',
        [
            'section'     => 'hayden_panel_footer_section',
            'settings'    => 'hayden_panel_footer_note',
            'type'        => 'custom',
            'description' => $html,
        ]
    ));
});

/**
 * Body class: nav style.
 */
add_filter('body_class', function (array $classes): array {
    $style = get_theme_mod('hayden_nav_link_style', 'basic');
    if (!in_array($style, ['basic', 'pill', 'underline'], true)) {
        $style = 'basic';
    }
    $classes[] = 'nav-style-' . $style;
    return $classes;
});

/**
 * Apply Customizer logo max height to the custom logo markup.
 */
add_filter('get_custom_logo', function ($html) {
    $height = absint(get_theme_mod('hayden_logo_max_height', 80));
    if (!$height || !$html) return $html;

    return preg_replace(
        '/<img([^>]+)>/',
        '<img$1 style="max-height:' . $height . 'px;height:auto;width:auto;">',
        $html,
        1
    );
});
