<?php
/**
 * Firefly Blog — Theme Customizer Settings Page
 *
 * Include this file from functions.php ONLY when posts are enabled:
 *
 *   if ( get_option('my_plugin_show_posts') === 'yes' ) {
 *       require_once get_template_directory() . '/firefly-blog.php';
 *   }
 */

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

/* -----------------------------------------------------------------------
 * 1.  REGISTER CUSTOMIZER PANEL, SECTIONS & SETTINGS
 * --------------------------------------------------------------------- */
add_action( 'customize_register', 'firefly_blog_customize_register' );

function firefly_blog_customize_register( $wp_customize ) {

    /* ── Panel ── */
    $wp_customize->add_panel( 'firefly_blog_panel', [
        'title'    => __( 'Firefly Blog', 'firefly' ),
        'priority' => 160,
    ] );

    /* ====================================================================
     * SECTION A — Single Post Layout
     * (merged: Post Header & Featured Image + Single Post Layout)
     * ================================================================== */
    $wp_customize->add_section( 'firefly_blog_single', [
        'title' => __( 'Single Post Layout', 'firefly' ),
        'panel' => 'firefly_blog_panel',
    ] );

    // --- Post Header ---

    // Show post header (title + date + divider)
    $wp_customize->add_setting( 'firefly_show_post_header', [
        'default'           => 'yes',
        'sanitize_callback' => 'firefly_sanitize_radio',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_show_post_header', [
        'label'   => __( 'Show post header (title, date, divider)', 'firefly' ),
        'section' => 'firefly_blog_single',
        'type'    => 'radio',
        'choices' => [ 'yes' => __( 'Yes', 'firefly' ), 'no' => __( 'No', 'firefly' ) ],
    ] );

    // Post title colour
    $wp_customize->add_setting( 'firefly_post_title_color', [
        'default'           => '#ffffff',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'firefly_post_title_color', [
        'label'   => __( 'Post title colour', 'firefly' ),
        'section' => 'firefly_blog_single',
    ] ) );

    // Post title font size (clamp min / max)
    $wp_customize->add_setting( 'firefly_post_title_size_min', [
        'default'           => '2',
        'sanitize_callback' => 'firefly_sanitize_float',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_post_title_size_min', [
        'label'       => __( 'Post title min size (rem)', 'firefly' ),
        'section'     => 'firefly_blog_single',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 1, 'max' => 6, 'step' => 0.1 ],
    ] );

    $wp_customize->add_setting( 'firefly_post_title_size_max', [
        'default'           => '3',
        'sanitize_callback' => 'firefly_sanitize_float',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_post_title_size_max', [
        'label'       => __( 'Post title max size (rem)', 'firefly' ),
        'section'     => 'firefly_blog_single',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 1, 'max' => 8, 'step' => 0.1 ],
    ] );

    // Post title font weight
    $wp_customize->add_setting( 'firefly_post_title_weight', [
        'default'           => '500',
        'sanitize_callback' => 'firefly_sanitize_font_weight',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_post_title_weight', [
        'label'   => __( 'Post title font weight', 'firefly' ),
        'section' => 'firefly_blog_single',
        'type'    => 'select',
        'choices' => [
            '200' => '200 — Thin',
            '300' => '300 — Light',
            '400' => '400 — Regular',
            '500' => '500 — Medium',
            '600' => '600 — Semi-bold',
            '700' => '700 — Bold',
        ],
    ] );

    // Divider colour
    $wp_customize->add_setting( 'firefly_post_divider_color', [
        'default'           => '#e20613',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'firefly_post_divider_color', [
        'label'   => __( 'Post header divider colour', 'firefly' ),
        'section' => 'firefly_blog_single',
    ] ) );

    // --- Featured Image (on single post page) ---

    // Show featured image on post page
    $wp_customize->add_setting( 'my_plugin_show_post_featuredimage', [
        'default'           => 'no',
        'sanitize_callback' => 'firefly_sanitize_radio',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'my_plugin_show_post_featuredimage', [
        'label'       => __( 'Show featured image on post page', 'firefly' ),
        'description' => __( 'No = hidden. Yes = full-width hero image.', 'firefly' ),
        'section'     => 'firefly_blog_single',
        'type'        => 'radio',
        'choices'     => [ 'yes' => __( 'Yes', 'firefly' ), 'no' => __( 'No (hidden)', 'firefly' ) ],
    ] );

    // Featured image max height
    $wp_customize->add_setting( 'firefly_hero_max_height', [
        'default'           => '500',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_hero_max_height', [
        'label'       => __( 'Featured image max height (px)', 'firefly' ),
        'section'     => 'firefly_blog_single',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 100, 'max' => 1200, 'step' => 10 ],
    ] );

    // Featured image grayscale
    $wp_customize->add_setting( 'firefly_hero_grayscale', [
        'default'           => 'yes',
        'sanitize_callback' => 'firefly_sanitize_radio',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_hero_grayscale', [
        'label'   => __( 'Apply greyscale filter to featured image', 'firefly' ),
        'section' => 'firefly_blog_single',
        'type'    => 'radio',
        'choices' => [ 'yes' => __( 'Yes', 'firefly' ), 'no' => __( 'No', 'firefly' ) ],
    ] );

    // Dark overlay on featured image
    $wp_customize->add_setting( 'firefly_hero_overlay', [
        'default'           => 'yes',
        'sanitize_callback' => 'firefly_sanitize_radio',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_hero_overlay', [
        'label'   => __( 'Show dark overlay on featured image', 'firefly' ),
        'section' => 'firefly_blog_single',
        'type'    => 'radio',
        'choices' => [ 'yes' => __( 'Yes', 'firefly' ), 'no' => __( 'No', 'firefly' ) ],
    ] );

    // --- Single Post Layout ---

    // Full-width single post
    $wp_customize->add_setting( 'firefly_single_full_width', [
        'default'           => 'yes',
        'sanitize_callback' => 'firefly_sanitize_radio',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_single_full_width', [
        'label'       => __( 'Single post full width', 'firefly' ),
        'description' => __( 'Removes max-width constraint on .single-post .site-main', 'firefly' ),
        'section'     => 'firefly_blog_single',
        'type'        => 'radio',
        'choices'     => [ 'yes' => __( 'Yes', 'firefly' ), 'no' => __( 'No', 'firefly' ) ],
    ] );

    // Mobile top padding for single post (to clear fixed header)
    $wp_customize->add_setting( 'firefly_single_mobile_top_padding', [
        'default'           => '80',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_single_mobile_top_padding', [
        'label'       => __( 'Mobile top padding on single post (px)', 'firefly' ),
        'description' => __( 'Clears the fixed mobile header', 'firefly' ),
        'section'     => 'firefly_blog_single',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 200, 'step' => 4 ],
    ] );

    // Entry content top margin
    $wp_customize->add_setting( 'firefly_entry_content_margin_top', [
        'default'           => '2',
        'sanitize_callback' => 'firefly_sanitize_float',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_entry_content_margin_top', [
        'label'       => __( 'Entry content top margin (rem)', 'firefly' ),
        'section'     => 'firefly_blog_single',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 10, 'step' => 0.25 ],
    ] );

    /* ====================================================================
     * SECTION B — Post List Page
     * (merged: Blog Feed Grid + Post Card Style; reordered logically)
     * ================================================================== */
    $wp_customize->add_section( 'firefly_blog_feed', [
        'title' => __( 'Post List Page', 'firefly' ),
        'panel' => 'firefly_blog_panel',
    ] );

    // ── Post list style selector ──────────────────────────────────────────
    // Controls whether the grid options or list options are shown below.
    // JS in customize_controls_enqueue_scripts toggles visibility.

    $wp_customize->add_setting( 'firefly_post_list_style', [
        'default'           => 'grid',
        'sanitize_callback' => 'firefly_sanitize_list_style',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_post_list_style', [
        'label'       => __( 'Post list style', 'firefly' ),
        'description' => __( 'Grid = card grid layout. List = single-column list layout.', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'radio',
        'choices'     => [
            'grid' => __( 'Grid', 'firefly' ),
            'list' => __( 'List', 'firefly' ),
        ],
    ] );

    // ── POST TEMPLATE MAX WIDTH (shared by both modes) ────────────────────

    $wp_customize->add_setting( 'firefly_post_template_max_width', [
        'default'           => '1600',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_post_template_max_width', [
        'label'       => __( 'Post template max width (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 600, 'max' => 2400, 'step' => 50 ],
    ] );

    // ====================================================================
    // GRID OPTIONS  (shown when post list style = grid)
    // ====================================================================

    // Grid layout: columns
    $wp_customize->add_setting( 'firefly_grid_cols_desktop', [
        'default'           => '4',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_grid_cols_desktop', [
        'label'   => __( '[Grid] Desktop columns (≥1350px)', 'firefly' ),
        'section' => 'firefly_blog_feed',
        'type'    => 'select',
        'choices' => [ '1' => '1', '2' => '2', '3' => '3', '4' => '4' ],
    ] );

    $wp_customize->add_setting( 'firefly_grid_cols_tablet', [
        'default'           => '2',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_grid_cols_tablet', [
        'label'   => __( '[Grid] Tablet columns (≤1350px)', 'firefly' ),
        'section' => 'firefly_blog_feed',
        'type'    => 'select',
        'choices' => [ '1' => '1', '2' => '2', '3' => '3' ],
    ] );

    $wp_customize->add_setting( 'firefly_grid_cols_mobile', [
        'default'           => '2',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_grid_cols_mobile', [
        'label'   => __( '[Grid] Mobile columns (≤768px)', 'firefly' ),
        'section' => 'firefly_blog_feed',
        'type'    => 'select',
        'choices' => [ '1' => '1', '2' => '2' ],
    ] );

    $wp_customize->add_setting( 'firefly_grid_gap', [
        'default'           => '20',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_grid_gap', [
        'label'       => __( '[Grid] Grid gap (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 80, 'step' => 2 ],
    ] );

    // Grid card border
    $wp_customize->add_setting( 'firefly_card_border_width', [
        'default'           => '0',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_border_width', [
        'label'       => __( '[Grid] Card border width (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 20, 'step' => 1 ],
    ] );

    $wp_customize->add_setting( 'firefly_card_border_color', [
        'default'           => '#dddddd',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'firefly_card_border_color', [
        'label'   => __( '[Grid] Card border colour', 'firefly' ),
        'section' => 'firefly_blog_feed',
    ] ) );

    $wp_customize->add_setting( 'firefly_card_border_radius', [
        'default'           => '10',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_border_radius', [
        'label'       => __( '[Grid] Card border radius (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 40, 'step' => 1 ],
    ] );

    $wp_customize->add_setting( 'firefly_card_shadow', [
        'default'           => 'yes',
        'sanitize_callback' => 'firefly_sanitize_radio',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_shadow', [
        'label'   => __( '[Grid] Show card drop shadow', 'firefly' ),
        'section' => 'firefly_blog_feed',
        'type'    => 'radio',
        'choices' => [ 'yes' => __( 'Yes', 'firefly' ), 'no' => __( 'No', 'firefly' ) ],
    ] );

    $wp_customize->add_setting( 'firefly_card_shadow_color', [
        'default'           => '#354059',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'firefly_card_shadow_color', [
        'label'   => __( '[Grid] Card shadow colour', 'firefly' ),
        'section' => 'firefly_blog_feed',
    ] ) );

    // Grid image options
    $wp_customize->add_setting( 'firefly_card_padding_top', [
        'default'           => '10',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_padding_top', [
        'label'       => __( '[Grid] Space above image / card top padding (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 80, 'step' => 2 ],
    ] );

    $wp_customize->add_setting( 'firefly_card_show_image', [
        'default'           => 'yes',
        'sanitize_callback' => 'firefly_sanitize_radio',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_show_image', [
        'label'   => __( '[Grid] Show featured image on card', 'firefly' ),
        'section' => 'firefly_blog_feed',
        'type'    => 'radio',
        'choices' => [ 'yes' => __( 'Yes', 'firefly' ), 'no' => __( 'No', 'firefly' ) ],
    ] );

    $wp_customize->add_setting( 'firefly_card_image_height', [
        'default'           => '200',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_image_height', [
        'label'       => __( '[Grid] Card image height (px)', 'firefly' ),
        'description' => __( 'Image is cropped (object-fit: cover) to this height.', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 50, 'max' => 600, 'step' => 10 ],
    ] );

    $wp_customize->add_setting( 'firefly_card_image_width', [
        'default'           => '100',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_image_width', [
        'label'       => __( '[Grid] Card image width (%)', 'firefly' ),
        'description' => __( 'As a percentage of the card width.', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 10, 'max' => 100, 'step' => 5 ],
    ] );

    $wp_customize->add_setting( 'firefly_card_image_full_width', [
        'default'           => 'yes',
        'sanitize_callback' => 'firefly_sanitize_radio',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_image_full_width', [
        'label'       => __( '[Grid] Image full width (no side padding)', 'firefly' ),
        'description' => __( 'Yes = image bleeds edge to edge. No = image respects left & right card padding.', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'radio',
        'choices'     => [ 'yes' => __( 'Yes', 'firefly' ), 'no' => __( 'No', 'firefly' ) ],
    ] );

    $wp_customize->add_setting( 'firefly_card_image_space_above', [
        'default'           => '0',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_image_space_above', [
        'label'       => __( '[Grid] Space above image within card (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 100, 'step' => 2 ],
    ] );

    // Grid heading
    $wp_customize->add_setting( 'firefly_card_title_space_above', [
        'default'           => '0',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_title_space_above', [
        'label'       => __( '[Grid] Space above heading (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 100, 'step' => 2 ],
    ] );

    $wp_customize->add_setting( 'firefly_card_title_size', [
        'default'           => '18',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_title_size', [
        'label'       => __( '[Grid] Heading font size (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 8, 'max' => 72, 'step' => 1 ],
    ] );

    $wp_customize->add_setting( 'firefly_card_title_color', [
        'default'           => '#000000',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'firefly_card_title_color', [
        'label'   => __( '[Grid] Heading colour', 'firefly' ),
        'section' => 'firefly_blog_feed',
    ] ) );

    $wp_customize->add_setting( 'firefly_card_title_space_below', [
        'default'           => '0',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_title_space_below', [
        'label'       => __( '[Grid] Space below heading (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 100, 'step' => 2 ],
    ] );

    // Grid summary
    $wp_customize->add_setting( 'firefly_card_summary_size', [
        'default'           => '14',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_summary_size', [
        'label'       => __( '[Grid] Summary font size (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 8, 'max' => 48, 'step' => 1 ],
    ] );

    $wp_customize->add_setting( 'firefly_card_summary_color', [
        'default'           => '#444444',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'firefly_card_summary_color', [
        'label'   => __( '[Grid] Summary colour', 'firefly' ),
        'section' => 'firefly_blog_feed',
    ] ) );

    // Grid link / read-more
    $wp_customize->add_setting( 'firefly_card_link_space_above', [
        'default'           => '0',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_link_space_above', [
        'label'       => __( '[Grid] Space above link (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 100, 'step' => 2 ],
    ] );

    $wp_customize->add_setting( 'firefly_card_link_size', [
        'default'           => '14',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_link_size', [
        'label'       => __( '[Grid] Link font size (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 8, 'max' => 48, 'step' => 1 ],
    ] );

    $wp_customize->add_setting( 'firefly_card_link_color', [
        'default'           => '#e20613',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'firefly_card_link_color', [
        'label'   => __( '[Grid] Link colour', 'firefly' ),
        'section' => 'firefly_blog_feed',
    ] ) );

    // Grid card padding (bottom)
    $wp_customize->add_setting( 'firefly_card_padding_v', [
        'default'           => '10',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_padding_v', [
        'label'       => __( '[Grid] Card padding bottom (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 80, 'step' => 2 ],
    ] );

    $wp_customize->add_setting( 'firefly_card_padding_h', [
        'default'           => '20',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_card_padding_h', [
        'label'       => __( '[Grid] Card padding left & right (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 80, 'step' => 2 ],
    ] );

    // ====================================================================
    // LIST OPTIONS  (shown when post list style = list)
    // All option keys are prefixed firefly_list_ to avoid collision with grid.
    // ====================================================================

    // List separator line
    $wp_customize->add_setting( 'firefly_list_show_divider', [
        'default'           => 'yes',
        'sanitize_callback' => 'firefly_sanitize_radio',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_show_divider', [
        'label'   => __( '[List] Separate posts with a line', 'firefly' ),
        'section' => 'firefly_blog_feed',
        'type'    => 'radio',
        'choices' => [ 'yes' => __( 'Yes', 'firefly' ), 'no' => __( 'No', 'firefly' ) ],
    ] );

    $wp_customize->add_setting( 'firefly_list_divider_thickness', [
        'default'           => '1',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_divider_thickness', [
        'label'       => __( '[List] Divider line thickness (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 1, 'max' => 10, 'step' => 1 ],
    ] );

    $wp_customize->add_setting( 'firefly_list_divider_color', [
        'default'           => '#dddddd',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'firefly_list_divider_color', [
        'label'   => __( '[List] Divider line colour', 'firefly' ),
        'section' => 'firefly_blog_feed',
    ] ) );

    $wp_customize->add_setting( 'firefly_list_divider_space_above', [
        'default'           => '20',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_divider_space_above', [
        'label'       => __( '[List] Space above divider line (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 80, 'step' => 2 ],
    ] );

    $wp_customize->add_setting( 'firefly_list_divider_space_below', [
        'default'           => '20',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_divider_space_below', [
        'label'       => __( '[List] Space below divider line (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 80, 'step' => 2 ],
    ] );

    // List thumbnail
    $wp_customize->add_setting( 'firefly_list_show_thumbnail', [
        'default'           => 'yes',
        'sanitize_callback' => 'firefly_sanitize_radio',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_show_thumbnail', [
        'label'   => __( '[List] Show thumbnail image', 'firefly' ),
        'section' => 'firefly_blog_feed',
        'type'    => 'radio',
        'choices' => [ 'yes' => __( 'Yes', 'firefly' ), 'no' => __( 'No', 'firefly' ) ],
    ] );

    $wp_customize->add_setting( 'firefly_list_thumbnail_width', [
        'default'           => '200',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_thumbnail_width', [
        'label'       => __( '[List] Thumbnail width (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 60, 'max' => 600, 'step' => 10 ],
    ] );

    $wp_customize->add_setting( 'firefly_list_thumbnail_height', [
        'default'           => '140',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_thumbnail_height', [
        'label'       => __( '[List] Thumbnail height (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 40, 'max' => 400, 'step' => 10 ],
    ] );

    $wp_customize->add_setting( 'firefly_list_thumbnail_align', [
        'default'           => 'left',
        'sanitize_callback' => 'firefly_sanitize_thumb_align',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_thumbnail_align', [
        'label'   => __( '[List] Thumbnail position', 'firefly' ),
        'section' => 'firefly_blog_feed',
        'type'    => 'radio',
        'choices' => [ 'left' => __( 'Left', 'firefly' ), 'right' => __( 'Right', 'firefly' ) ],
    ] );

    // List heading
    $wp_customize->add_setting( 'firefly_list_title_space_above', [
        'default'           => '0',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_title_space_above', [
        'label'       => __( '[List] Space above heading (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 80, 'step' => 2 ],
    ] );

    $wp_customize->add_setting( 'firefly_list_title_size', [
        'default'           => '20',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_title_size', [
        'label'       => __( '[List] Heading font size (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 8, 'max' => 72, 'step' => 1 ],
    ] );

    $wp_customize->add_setting( 'firefly_list_title_color', [
        'default'           => '#000000',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'firefly_list_title_color', [
        'label'   => __( '[List] Heading colour', 'firefly' ),
        'section' => 'firefly_blog_feed',
    ] ) );

    $wp_customize->add_setting( 'firefly_list_title_space_below', [
        'default'           => '8',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_title_space_below', [
        'label'       => __( '[List] Space below heading (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 80, 'step' => 2 ],
    ] );

    // List summary
    $wp_customize->add_setting( 'firefly_list_summary_words', [
        'default'           => '30',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_summary_words', [
        'label'       => __( '[List] Number of words in summary (followed by "…")', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 200, 'step' => 5 ],
    ] );

    $wp_customize->add_setting( 'firefly_list_summary_size', [
        'default'           => '14',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_summary_size', [
        'label'       => __( '[List] Summary font size (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 8, 'max' => 48, 'step' => 1 ],
    ] );

    $wp_customize->add_setting( 'firefly_list_summary_color', [
        'default'           => '#444444',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'firefly_list_summary_color', [
        'label'   => __( '[List] Summary colour', 'firefly' ),
        'section' => 'firefly_blog_feed',
    ] ) );

    // List read-more link
    $wp_customize->add_setting( 'firefly_list_link_space_above', [
        'default'           => '8',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_link_space_above', [
        'label'       => __( '[List] Space above read-more link (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 80, 'step' => 2 ],
    ] );

    $wp_customize->add_setting( 'firefly_list_link_size', [
        'default'           => '14',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_link_size', [
        'label'       => __( '[List] Read-more link font size (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 8, 'max' => 48, 'step' => 1 ],
    ] );

    $wp_customize->add_setting( 'firefly_list_link_color', [
        'default'           => '#e20613',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'firefly_list_link_color', [
        'label'   => __( '[List] Read-more link colour', 'firefly' ),
        'section' => 'firefly_blog_feed',
    ] ) );

    // List bottom spacing
    $wp_customize->add_setting( 'firefly_list_item_padding_bottom', [
        'default'           => '20',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_item_padding_bottom', [
        'label'       => __( '[List] Space at the bottom of each post (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 100, 'step' => 4 ],
    ] );

    // List date display
    $wp_customize->add_setting( 'firefly_list_show_date', [
        'default'           => 'no',
        'sanitize_callback' => 'firefly_sanitize_radio',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_show_date', [
        'label'   => __( '[List] Show post date', 'firefly' ),
        'section' => 'firefly_blog_feed',
        'type'    => 'radio',
        'choices' => [ 'yes' => __( 'Yes', 'firefly' ), 'no' => __( 'No', 'firefly' ) ],
    ] );

    $wp_customize->add_setting( 'firefly_list_date_size', [
        'default'           => '13',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_date_size', [
        'label'       => __( '[List] Date font size (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 8, 'max' => 32, 'step' => 1 ],
    ] );

    $wp_customize->add_setting( 'firefly_list_date_color', [
        'default'           => '#888888',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'firefly_list_date_color', [
        'label'   => __( '[List] Date colour', 'firefly' ),
        'section' => 'firefly_blog_feed',
    ] ) );

    $wp_customize->add_setting( 'firefly_list_date_space_above', [
        'default'           => '0',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_list_date_space_above', [
        'label'       => __( '[List] Space above date (px)', 'firefly' ),
        'section'     => 'firefly_blog_feed',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 60, 'step' => 2 ],
    ] );

    /* ====================================================================
     * SECTION C — Post Navigation
     * ================================================================== */
    $wp_customize->add_section( 'firefly_blog_nav', [
        'title' => __( 'Post Navigation', 'firefly' ),
        'panel' => 'firefly_blog_panel',
    ] );

    // Nav max width
    $wp_customize->add_setting( 'firefly_post_nav_max_width', [
        'default'           => '1500',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_post_nav_max_width', [
        'label'       => __( 'Post navigation max width (px)', 'firefly' ),
        'section'     => 'firefly_blog_nav',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 600, 'max' => 2400, 'step' => 50 ],
    ] );

    // Nav padding top/bottom
    $wp_customize->add_setting( 'firefly_post_nav_padding', [
        'default'           => '100',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_post_nav_padding', [
        'label'       => __( 'Post navigation top & bottom padding (px)', 'firefly' ),
        'section'     => 'firefly_blog_nav',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 200, 'step' => 10 ],
    ] );

    // Nav gap
    $wp_customize->add_setting( 'firefly_post_nav_gap', [
        'default'           => '60',
        'sanitize_callback' => 'absint',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( 'firefly_post_nav_gap', [
        'label'       => __( 'Post navigation gap between links (px)', 'firefly' ),
        'section'     => 'firefly_blog_nav',
        'type'        => 'number',
        'input_attrs' => [ 'min' => 0, 'max' => 200, 'step' => 5 ],
    ] );

    // Nav link colour
    $wp_customize->add_setting( 'firefly_post_nav_link_color', [
        'default'           => '#000000',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'refresh',
    ] );
    $wp_customize->add_control( new WP_Customize_Color_Control( $wp_customize, 'firefly_post_nav_link_color', [
        'label'   => __( 'Post navigation link colour', 'firefly' ),
        'section' => 'firefly_blog_nav',
    ] ) );

}

/* -----------------------------------------------------------------------
 * 1c. CUSTOMIZER JS — show/hide grid vs list controls
 * Must be registered on customize_controls_enqueue_scripts, which fires
 * BEFORE customize_register, so we hook it at the top level (not inside
 * the register callback).
 * --------------------------------------------------------------------- */
add_action( 'customize_controls_enqueue_scripts', 'firefly_blog_customizer_toggle_js' );

function firefly_blog_customizer_toggle_js() {
    wp_add_inline_script( 'customize-controls', '
( function( api ) {
    "use strict";

    var GRID_CONTROLS = [
        "firefly_grid_cols_desktop","firefly_grid_cols_tablet","firefly_grid_cols_mobile",
        "firefly_grid_gap",
        "firefly_card_border_width","firefly_card_border_color","firefly_card_border_radius",
        "firefly_card_shadow","firefly_card_shadow_color",
        "firefly_card_padding_top",
        "firefly_card_show_image","firefly_card_image_height","firefly_card_image_width",
        "firefly_card_image_full_width","firefly_card_image_space_above",
        "firefly_card_title_space_above","firefly_card_title_size","firefly_card_title_color",
        "firefly_card_title_space_below",
        "firefly_card_summary_size","firefly_card_summary_color",
        "firefly_card_link_space_above","firefly_card_link_size","firefly_card_link_color",
        "firefly_card_padding_v","firefly_card_padding_h"
    ];

    var LIST_CONTROLS = [
        "firefly_list_show_divider","firefly_list_divider_thickness","firefly_list_divider_color",
        "firefly_list_divider_space_above","firefly_list_divider_space_below",
        "firefly_list_show_thumbnail","firefly_list_thumbnail_width","firefly_list_thumbnail_height","firefly_list_thumbnail_align",
        "firefly_list_title_space_above","firefly_list_title_size","firefly_list_title_color",
        "firefly_list_title_space_below",
        "firefly_list_summary_words","firefly_list_summary_size","firefly_list_summary_color",
        "firefly_list_link_space_above","firefly_list_link_size","firefly_list_link_color",
        "firefly_list_item_padding_bottom",
        "firefly_list_show_date","firefly_list_date_size","firefly_list_date_color","firefly_list_date_space_above"
    ];

    function applyVisibility( style ) {
        var isGrid = ( style === "grid" );
        GRID_CONTROLS.forEach( function( id ) {
            var ctrl = api.control( id );
            if ( ctrl ) { ctrl.container.toggle( isGrid ); }
        } );
        LIST_CONTROLS.forEach( function( id ) {
            var ctrl = api.control( id );
            if ( ctrl ) { ctrl.container.toggle( ! isGrid ); }
        } );
    }

    api.bind( "ready", function() {
        api( "firefly_post_list_style", function( setting ) {
            setting.bind( applyVisibility );
            applyVisibility( setting.get() );
        } );
    } );

}( wp.customize ) );
' );
}

/* -----------------------------------------------------------------------
 * 1b. INJECT / RELOCATE FEATURED IMAGE IN QUERY LOOP CARDS
 *
 * The image must live OUTSIDE .wp-block-group so that:
 *   - Full-width mode works (not clipped by group overflow)
 *   - Card padding does not affect the image
 * We extract it from inside the group if present, or inject it fresh.
 * We also add inline style="display:block" to defeat any global
 * display:none the theme applies when my_plugin_show_post_thumbnails=no.
 * --------------------------------------------------------------------- */
set_theme_mod( 'my_plugin_show_post_thumbnails', 'yes' );

add_filter( 'render_block', 'firefly_maybe_inject_card_image', 10, 2 );

function firefly_maybe_inject_card_image( $block_content, $block ) {

    if ( 'core/null' !== $block['blockName'] ) {
        return $block_content;
    }

    $show_image = get_theme_mod( 'firefly_card_show_image', 'yes' );

    // ── Case 1: image already in HTML (inside .wp-block-group) ───────────
    if ( false !== strpos( $block_content, 'wp-block-post-featured-image' ) ) {

        if ( 'yes' !== $show_image ) {
            // Hide it — remove the image div entirely from the output
            $block_content = preg_replace(
                '/<div\b[^>]*class="[^"]*wp-block-post-featured-image[^"]*"[^>]*>.*?<\/div>/is',
                '',
                $block_content
            );
            return $block_content;
        }

        // Extract the image div from wherever it is
        preg_match(
            '/(<div\b[^>]*class="[^"]*wp-block-post-featured-image[^"]*"[^>]*>.*?<\/div>)/is',
            $block_content,
            $img_match
        );

        if ( empty( $img_match[1] ) ) {
            return $block_content;
        }

        $img_div = $img_match[1];

        // Ensure it has display:block inline (beats theme's global hide)
        if ( false === strpos( $img_div, 'display:block' ) && false === strpos( $img_div, 'display: block' ) ) {
            $img_div = preg_replace( '/(<div\b[^>]*class="[^"]*wp-block-post-featured-image[^"]*")/', '$1 style="display:block;margin:0;padding:0;line-height:0;overflow:hidden;"', $img_div, 1 );
        }

        // Remove from original position
        $block_content = preg_replace(
            '/<div\b[^>]*class="[^"]*wp-block-post-featured-image[^"]*"[^>]*>.*?<\/div>/is',
            '',
            $block_content,
            1
        );

        // Place before .wp-block-group
        return preg_replace(
            '/(<div\b[^>]*class="[^"]*wp-block-group[^"]*")/',
            $img_div . '$1',
            $block_content,
            1
        );
    }

    // ── Case 2: no image block in template — inject fresh ────────────────
    if ( 'yes' !== $show_image ) {
        return $block_content;
    }

    preg_match( '/\bpost-(\d+)\b/', $block_content, $id_match );
    $post_id = ! empty( $id_match[1] ) ? (int) $id_match[1] : 0;

    if ( ! $post_id ) {
        return $block_content . '<!-- firefly-img: no post_id -->';
    }

    if ( ! has_post_thumbnail( $post_id ) ) {
        return $block_content . '<!-- firefly-img: no thumbnail post ' . $post_id . ' -->';
    }

    $img_height = (int) get_theme_mod( 'firefly_card_image_height', 200 );

    $img = get_the_post_thumbnail( $post_id, 'large', [
        'style' => "display:block;width:100%;height:{$img_height}px;object-fit:cover;object-position:center;",
        'class' => 'firefly-card-img',
    ] );

    $permalink = get_permalink( $post_id );
    $wrapped   = '<div class="wp-block-post-featured-image firefly-injected-img" style="display:block;margin:0;padding:0;line-height:0;overflow:hidden;">'
               . '<a href="' . esc_url( $permalink ) . '" tabindex="-1" aria-hidden="true" style="display:block;line-height:0;">'
               . $img
               . '</a></div>';

    // Place before .wp-block-group
    return preg_replace(
        '/(<div\b[^>]*class="[^"]*wp-block-group[^"]*")/',
        $wrapped . '$1',
        $block_content,
        1
    );
}

/* -----------------------------------------------------------------------
 * 2.  OUTPUT DYNAMIC CSS
 * --------------------------------------------------------------------- */
add_action( 'wp_head', 'firefly_blog_output_css', 99 );

function firefly_blog_output_css() {

    /* Helpers */
    $show_header       = get_theme_mod( 'firefly_show_post_header', 'yes' );
    $show_thumbs       = "yes";
    $hero_height       = (int) get_theme_mod( 'firefly_hero_max_height', 500 );
    $hero_grayscale    = get_theme_mod( 'firefly_hero_grayscale', 'yes' );
    $hero_overlay      = get_theme_mod( 'firefly_hero_overlay', 'yes' );

    $title_color       = sanitize_hex_color( get_theme_mod( 'firefly_post_title_color', '#ffffff' ) );
    $title_min         = (float) get_theme_mod( 'firefly_post_title_size_min', 2 );
    $title_max         = (float) get_theme_mod( 'firefly_post_title_size_max', 3 );
    $title_weight      = get_theme_mod( 'firefly_post_title_weight', '500' );
    $divider_color     = sanitize_hex_color( get_theme_mod( 'firefly_post_divider_color', '#e20613' ) );

    $grid_desktop      = (int) get_theme_mod( 'firefly_grid_cols_desktop', 4 );
    $grid_tablet       = (int) get_theme_mod( 'firefly_grid_cols_tablet', 2 );
    $grid_mobile       = (int) get_theme_mod( 'firefly_grid_cols_mobile', 2 );
    $grid_gap          = (int) get_theme_mod( 'firefly_grid_gap', 20 );
    $tpl_max_width     = (int) get_theme_mod( 'firefly_post_template_max_width', 1600 );

    $card_radius       = (int) get_theme_mod( 'firefly_card_border_radius', 10 );
    $card_shadow       = get_theme_mod( 'firefly_card_shadow', 'yes' );
    $card_shadow_color = sanitize_hex_color( get_theme_mod( 'firefly_card_shadow_color', '#354059' ) );

    $card_show_image        = get_theme_mod( 'firefly_card_show_image', 'yes' );
    $card_img_width         = (int) get_theme_mod( 'firefly_card_image_width', 100 );
    $card_img_height        = (int) get_theme_mod( 'firefly_card_image_height', 200 );
    $card_img_full_width    = get_theme_mod( 'firefly_card_image_full_width', 'yes' );
    $card_img_space_above   = (int) get_theme_mod( 'firefly_card_image_space_above', 0 );

    $card_title_size        = (int) get_theme_mod( 'firefly_card_title_size', 18 );
    $card_title_color       = sanitize_hex_color( get_theme_mod( 'firefly_card_title_color', '#000000' ) );
    $card_title_space_above = (int) get_theme_mod( 'firefly_card_title_space_above', 0 );
    $card_title_space_below = (int) get_theme_mod( 'firefly_card_title_space_below', 0 );
    $card_summary_size      = (int) get_theme_mod( 'firefly_card_summary_size', 14 );
    $card_summary_color     = sanitize_hex_color( get_theme_mod( 'firefly_card_summary_color', '#444444' ) );
    $card_link_size         = (int) get_theme_mod( 'firefly_card_link_size', 14 );
    $card_link_color        = sanitize_hex_color( get_theme_mod( 'firefly_card_link_color', '#e20613' ) );
    $card_link_space_above  = (int) get_theme_mod( 'firefly_card_link_space_above', 0 );
    $card_padding_v    = (int) get_theme_mod( 'firefly_card_padding_v', 20 );
     $card_padding_top    = (int) get_theme_mod( 'firefly_card_padding_top', 20 );
    $card_padding_h    = (int) get_theme_mod( 'firefly_card_padding_h', 20 );
    $card_border_width = (int) get_theme_mod( 'firefly_card_border_width', 0 );
    $card_border_color = sanitize_hex_color( get_theme_mod( 'firefly_card_border_color', '#dddddd' ) );

    $single_full       = get_theme_mod( 'firefly_single_full_width', 'yes' );
    $single_mob_pad    = (int) get_theme_mod( 'firefly_single_mobile_top_padding', 80 );
    $content_margin    = (float) get_theme_mod( 'firefly_entry_content_margin_top', 2 );

    $nav_max_width     = (int) get_theme_mod( 'firefly_post_nav_max_width', 1500 );
    $nav_padding       = (int) get_theme_mod( 'firefly_post_nav_padding', 100 );
    $nav_gap           = (int) get_theme_mod( 'firefly_post_nav_gap', 60 );
    $nav_link_color    = sanitize_hex_color( get_theme_mod( 'firefly_post_nav_link_color', '#000000' ) );

    /* List-mode settings */
    $post_list_style            = get_theme_mod( 'firefly_post_list_style', 'grid' );
    $list_show_divider          = get_theme_mod( 'firefly_list_show_divider', 'yes' );
    $list_divider_thickness     = (int) get_theme_mod( 'firefly_list_divider_thickness', 1 );
    $list_divider_color         = sanitize_hex_color( get_theme_mod( 'firefly_list_divider_color', '#dddddd' ) );
    $list_divider_space_above   = (int) get_theme_mod( 'firefly_list_divider_space_above', 20 );
    $list_divider_space_below   = (int) get_theme_mod( 'firefly_list_divider_space_below', 20 );
    $list_show_thumb            = get_theme_mod( 'firefly_list_show_thumbnail', 'yes' );
    $list_thumb_width           = (int) get_theme_mod( 'firefly_list_thumbnail_width', 200 );
    $list_thumb_height          = (int) get_theme_mod( 'firefly_list_thumbnail_height', 140 );
    $list_thumb_align           = get_theme_mod( 'firefly_list_thumbnail_align', 'left' );
    $list_title_space_above     = (int) get_theme_mod( 'firefly_list_title_space_above', 0 );
    $list_title_size            = (int) get_theme_mod( 'firefly_list_title_size', 20 );
    $list_title_color           = sanitize_hex_color( get_theme_mod( 'firefly_list_title_color', '#000000' ) );
    $list_title_space_below     = (int) get_theme_mod( 'firefly_list_title_space_below', 8 );
    $list_summary_words         = (int) get_theme_mod( 'firefly_list_summary_words', 30 );
    $list_summary_size          = (int) get_theme_mod( 'firefly_list_summary_size', 14 );
    $list_summary_color         = sanitize_hex_color( get_theme_mod( 'firefly_list_summary_color', '#444444' ) );
    $list_link_space_above      = (int) get_theme_mod( 'firefly_list_link_space_above', 8 );
    $list_link_size             = (int) get_theme_mod( 'firefly_list_link_size', 14 );
    $list_link_color            = sanitize_hex_color( get_theme_mod( 'firefly_list_link_color', '#e20613' ) );
    $list_item_padding_bottom   = (int) get_theme_mod( 'firefly_list_item_padding_bottom', 20 );
    $list_show_date             = get_theme_mod( 'firefly_list_show_date', 'no' );
    $list_date_size             = (int) get_theme_mod( 'firefly_list_date_size', 13 );
    $list_date_color            = sanitize_hex_color( get_theme_mod( 'firefly_list_date_color', '#888888' ) );
    $list_date_space_above      = (int) get_theme_mod( 'firefly_list_date_space_above', 0 );

    /* Derived values */
    $shadow_css        = 'yes' === $card_shadow
        ? "0 4px 20px {$card_shadow_color}80"
        : 'none';

    $grayscale_css     = 'yes' === $hero_grayscale
        ? 'filter: grayscale(100%) contrast(1.1);'
        : '';

    $overlay_css       = 'yes' === $hero_overlay
        ? "background: rgba(0,0,0,0.25);"
        : "display: none;";

    ?>
    <style id="firefly-blog-css">

        /* ── Post template max-width & centering ────────────────────── */
        .wp-block-post-template {
            max-width: <?php echo $tpl_max_width; ?>px;
            margin-left: auto !important;
            margin-right: auto !important;
        }

        /* ── Post header styles ──────────────────────────────────────── */
        .post-title {
            color: <?php echo $title_color; ?>;
            font-size: clamp(<?php echo $title_min; ?>rem, 4vw, <?php echo $title_max; ?>rem);
            font-weight: <?php echo esc_attr( $title_weight ); ?>;
            margin: 0 0 0.5rem 0;
        }

        .post-divider {
            border: 0;
            height: 1px;
            background-color: <?php echo $divider_color; ?>;
            max-width: 100% !important;
            margin-bottom: 2rem;
        }

        /* ── Featured hero image ─────────────────────────────────────── */
        <?php if ( 'yes' === $show_thumbs ) : ?>
        .post-hero-full {
            width: 100vw;
            max-height: <?php echo $hero_height; ?>px;
            overflow: hidden;
            position: relative;
            margin-left: 50%;
            transform: translateX(-50%);
        }
        .post-hero-full img {
            width: 100%;
            height: <?php echo $hero_height; ?>px;
            object-fit: cover;
            <?php echo $grayscale_css; ?>
        }
        .post-hero-full::after {
            content: "";
            position: absolute;
            inset: 0;
            pointer-events: none;
            <?php echo $overlay_css; ?>
        }
        <?php else : ?>
        .post-hero-full { display: none !important; }
        <?php endif; ?>

        /* ── Entry content ───────────────────────────────────────────── */
         .single-post .entry-content {
            margin-top: <?php echo $content_margin; ?>rem !important;
            line-height: 1.7;
        }

        /* ── Single post full width ──────────────────────────────────── */
        <?php if ( 'yes' === $single_full ) : ?>
        .single-post .site-main {
            max-width: 100% !important;
            width: 100% !important;
            margin: 0 !important;
            padding: 0 !important;
        }
        <?php endif; ?>

        /* ── Blog feed grid ──────────────────────────────────────────── */
        .wp-block-post-template.is-layout-grid {
            display: grid;
            grid-template-columns: repeat(<?php echo $grid_desktop; ?>, 1fr);
            gap: <?php echo $grid_gap; ?>px;
            justify-content: center;
        }

        @media (max-width: 1350px) {
            .wp-block-post-template.is-layout-grid {
                grid-template-columns: repeat(<?php echo $grid_tablet; ?>, 1fr);
                max-width: 97vw !important;
                margin-left: auto !important;
                margin-right: auto !important;
                padding-left: 0 !important;
                justify-content: center;
            }
        }

        @media (max-width: 768px) {
            .wp-block-post-template.is-layout-grid {
                grid-template-columns: repeat(<?php echo $grid_mobile; ?>, 1fr);
                max-width: 97vw !important;
                margin-left: auto !important;
                margin-right: auto !important;
                padding-left: 0 !important;
                justify-content: center;
            }
        }

        /* ── Post cards (grid mode only) ────────────────────────────── */
        <?php if ( 'grid' === $post_list_style ) : ?>
        .wp-block-post {
            display: flex !important;
            flex-direction: column !important;
            border-radius: <?php echo $card_radius; ?>px !important;
            box-shadow: <?php echo $shadow_css; ?> !important;
            overflow: hidden !important;
            <?php if ( $card_border_width > 0 ) : ?>
            border: <?php echo $card_border_width; ?>px solid <?php echo $card_border_color; ?> !important;
            <?php else : ?>
            border: none !important;
            <?php endif; ?>
        }
        <?php endif; ?>

        /* ── Card featured image ─────────────────────────────────────────── */
        /*
         * Image is now always placed BEFORE .wp-block-group by the PHP filter,
         * so it is a direct child of the <li> and never inside the padded group.
         * Full-width therefore works naturally — no negative margins needed.
         * We still output display:block !important as a belt-and-braces override.
         */
     <?php if ( 'yes' === $card_show_image ) : ?>
.wp-block-post-template .wp-block-post .wp-block-post-featured-image,
.wp-block-post-template .wp-block-post .firefly-injected-img {
    display: block !important;
    overflow: hidden !important;
    flex-shrink: 0 !important;
    margin-top: <?php echo $card_img_space_above; ?>px !important;
    margin-bottom: 0 !important;
    padding: 0 !important;
    line-height: 0 !important;
    box-sizing: border-box !important;
     margin-left: <?php echo $card_padding_h; ?>px !important;
    margin-right: <?php echo $card_padding_h; ?>px !important;
}

<?php if ( 'yes' === $card_img_full_width ) : ?>
    
.wp-block-post-template .wp-block-post .wp-block-post-featured-image {
    width: calc(100% + <?php echo $card_padding_h * 2; ?>px) !important;
    margin-left:0px !important;
    margin-right:0px !important;
margin-left: -<?php echo $card_padding_h; ?>px !important;
     margin-right: -<?php echo $card_padding_h; ?>px !important;
    /*  border: 5px solid yellow !important;*/
    margin-top:<?php echo $card_padding_top; ?>px !important;
}





ul.wp-block-post-template {
    margin-left: auto !important;
    margin-right: auto !important;
    padding-left: 20px !important;
    padding-right: 20px !important;
    width: 100%;
    list-style: none;
    box-sizing: border-box;
}

/* Also centre the query block's outer wrapper */
.wp-block-query {
    text-align: center;
}
.wp-block-query > ul.wp-block-post-template,
.wp-block-query > .wp-block-post-template {
    margin-left: auto !important;
    margin-right: auto !important;
    text-align: left;
}
/*is-layout-flow wp-block-query-is-layout-flow">< class="  is-layout-grid wp-container-core-post-template-is-layout-aad3ae65 wp-block-post-template-is-layout-grid"><li class="t post-372 post type-post status-publish format-standard has-post-thumbnail hentry category-uncategorized">*/


.wp-block-post-template .wp-block-post .firefly-injected-img {
    width: calc(100% + <?php echo $card_padding_h * 2; ?>px) !important;
}





<?php else : ?>
.wp-block-post-template .wp-block-post .wp-block-post-featured-image,
.wp-block-post-template .wp-block-post .firefly-injected-img {
    width: 100% !important;
    margin-left: <?php echo $card_padding_h; ?>px !important;
    margin-right: <?php echo $card_padding_h; ?>px !important;
}
<?php endif; ?>

.wp-block-post-template .wp-block-post .wp-block-post-featured-image a,
.wp-block-post-template .wp-block-post .firefly-injected-img a {
    display: block !important;
    line-height: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
}

.wp-block-post-template .wp-block-post .wp-block-post-featured-image img,
.wp-block-post-template .wp-block-post .wp-block-post-featured-image a img,
.wp-block-post-template .wp-block-post .firefly-injected-img img,
.wp-block-post-template .wp-block-post .firefly-injected-img a img {
    display: block !important;
    width: 100% !important;
    height: <?php echo $card_img_height; ?>px !important;
    max-width: none !important;
    object-fit: cover !important;
    object-position: center !important;
    border-radius: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
}
<?php else : ?>
.wp-block-post-template .wp-block-post .wp-block-post-featured-image,
.wp-block-post-template .wp-block-post .firefly-injected-img {
    display: none !important;
}
<?php endif; ?>

        /* ── Card content padding ────────────────────────────────────── */
        /*
         * The block editor writes padding as INLINE styles on .wp-block-group
         * e.g. style="padding-top:30px; padding-right:30px; ..."
         * The only way to beat inline styles is with !important on the same properties.
         * We set padding-top to 0 here and use margin-top on the title instead,
         * so that "space above title" works independently of the group padding.
         */
        .wp-block-post-template .wp-block-post .wp-block-group {
            padding-left:   0px !important;
            padding-top:   0px !important;
            padding-right:  0px !important;
            padding-bottom:  0px !important;
            box-sizing: border-box !important;
        
        }
        .wp-block-post-template .wp-block-post .wp-block-group__inner-container {
            padding-top: 0px !important;
            padding-bottom: 0px !important;
            margin-bottom: <?php echo $card_padding_v; ?>px !important;
            padding-left:  <?php echo $card_padding_h; ?>px !important;
            padding-right:  <?php echo $card_padding_h; ?>px !important;
            box-sizing: border-box !important;
      
            
        }
        /* Zero out margins/padding on inner blocks so we control all spacing */
        .wp-block-post-template .wp-block-post .wp-block-post-title,
        .wp-block-post-template .wp-block-post .wp-block-post-excerpt,
        .wp-block-post-template .wp-block-post .wp-block-post-excerpt p,
        .wp-block-post-template .wp-block-post .wp-block-read-more {
            margin-left:   0 !important;
            margin-right:  0 !important;
            padding-left:  0 !important;
            padding-right: 0 !important;
        }

        /* ── Card typography ─────────────────────────────────────────── */
        /* Title — target the wrapper AND every heading tag inside it */
        .wp-block-post-template .wp-block-post .wp-block-post-title,
        .wp-block-post-template .wp-block-post .wp-block-post-title a,
        .wp-block-post-template .wp-block-post .wp-block-post-title a:hover,
        .wp-block-post-template .wp-block-post .wp-block-post-title a:visited,
        .wp-block-post-template .wp-block-post .wp-block-post-title h1,
        .wp-block-post-template .wp-block-post .wp-block-post-title h2,
        .wp-block-post-template .wp-block-post .wp-block-post-title h3,
        .wp-block-post-template .wp-block-post .wp-block-post-title h4,
        .wp-block-post-template .wp-block-post .wp-block-post-title h1 a,
        .wp-block-post-template .wp-block-post .wp-block-post-title h2 a,
        .wp-block-post-template .wp-block-post .wp-block-post-title h3 a,
        .wp-block-post-template .wp-block-post .wp-block-post-title h4 a {
            font-size:   <?php echo $card_title_size; ?>px !important;
            color:       <?php echo $card_title_color; ?> !important;
            line-height: 1.3 !important;
            /* spacing controlled separately below — do NOT use margin shorthand here */
        }
        /* Space above title (replaces group padding-top) and below title */
        .wp-block-post-template .wp-block-post .wp-block-post-title {
            margin-top:    <?php echo $card_title_space_above; ?>px !important;
            margin-bottom: <?php echo $card_title_space_below; ?>px !important;
            padding-top:   0 !important;
        }

        /* Summary */
        .wp-block-post-template .wp-block-post .wp-block-post-excerpt,
        .wp-block-post-template .wp-block-post .wp-block-post-excerpt p,
        .wp-block-post-template .wp-block-post .wp-block-post-excerpt__excerpt,
        .wp-block-post-template .wp-block-post .wp-block-post-excerpt__excerpt p {
            font-size:     <?php echo $card_summary_size; ?>px !important;
            color:         <?php echo $card_summary_color; ?> !important;
            margin-top:    0 !important;
            margin-bottom: 0 !important;
            padding-bottom: 0 !important;
        }
        /* The block editor wraps the Read More link in a <p class="wp-block-post-excerpt__more-text">
           which has its own top margin — zero it so space-above-link is fully in our control */
        .wp-block-post-template .wp-block-post .wp-block-post-excerpt__more-text {
            margin-top:    0 !important;
            margin-bottom: 0 !important;
            padding-top:   0 !important;
        }

        /* Read more link */
        .wp-block-post-template .wp-block-post .wp-block-post-excerpt__more-link,
        .wp-block-post-template .wp-block-post .wp-block-post-excerpt__more-link:visited,
        .wp-block-post-template .wp-block-post .wp-block-read-more,
        .wp-block-post-template .wp-block-post .wp-block-read-more:visited,
        .wp-block-post-template .wp-block-post .wp-block-read-more:hover {
            font-size:        <?php echo $card_link_size; ?>px !important;
            color:            <?php echo $card_link_color; ?> !important;
            text-decoration:  none !important;
            margin-top:       <?php echo $card_link_space_above; ?>px !important;
            display:          block !important;
        }

        /* ── Post navigation ─────────────────────────────────────────── */
        .post-navigation-group,
        .custom-post-nav,
        .wp-block-post-navigation-links {
            max-width: <?php echo $nav_max_width; ?>px !important;
            margin-left: auto !important;
            margin-right: auto !important;
            display: flex !important;
            flex-direction: row !important;
            flex-wrap: nowrap !important;
            align-items: center !important;
            justify-content: space-between !important;
            padding: <?php echo $nav_padding; ?>px 20px !important;
            box-sizing: border-box !important;
            border-top: 1px solid #e0e0e0 !important;
            width: 100% !important;
            gap: 0 !important;
        }

        .post-navigation-group a,
        .custom-post-nav a,
        .wp-block-post-navigation-link a {
            text-decoration: none !important;
            color: <?php echo $nav_link_color; ?> !important;
            font-weight: 600 !important;
        }

        /* Each link takes exactly half the row */
        .wp-block-post-navigation-link {
            display: block !important;
            flex: 0 0 auto !important;
            font-weight: 600 !important;
            max-width: 48% !important;
        }

        /* Previous — left-aligned text */
        .wp-block-post-navigation-link.has-text-align-left,
        .wp-block-post-navigation-link:first-child {
            text-align: left !important;
            margin-right: auto !important;
            margin-left: 0 !important;
        }

        /* Next — right-aligned text */
        .wp-block-post-navigation-link.has-text-align-right,
        .wp-block-post-navigation-link:last-child {
            text-align: right !important;
            margin-left: auto !important;
            margin-right: 0 !important;
        }

        /* Only-child (first AND last): show on left */
        .wp-block-post-navigation-link:only-child {
            margin-left: 0 !important;
            margin-right: auto !important;
            text-align: left !important;
        }

        @media (max-width: 768px) {
            .post-navigation-group,
            .custom-post-nav,
            .wp-block-post-navigation-links {
                flex-direction: column !important;
                align-items: center !important;
                gap: 10px !important;
                flex-wrap: wrap !important;
            }

            .wp-block-post-navigation-link {
                max-width: 100% !important;
                text-align: center !important;
                margin-left: 0 !important;
                margin-right: 0 !important;
            }

            /* Mobile top padding for single post */
            .single-post main#primary {
                padding-top: <?php echo $single_mob_pad; ?>px;
            }
        }

        /* ── List mode ───────────────────────────────────────────────── */
        <?php if ( 'list' === $post_list_style ) : ?>

        /* Single column — override grid */
        .wp-block-post-template.is-layout-grid {
            display: block !important;
        }

        /* Each post row: block with clearfix so it wraps the float */
        .wp-block-post-template .wp-block-post {
            display: block !important;
            overflow: hidden !important;
            border-radius: 0 !important;
            box-shadow: none !important;
            border: none !important;
            padding-bottom: <?php echo $list_item_padding_bottom; ?>px !important;
        }

        <?php if ( 'yes' === $list_show_divider ) : ?>
        .wp-block-post-template .wp-block-post + .wp-block-post {
            margin-top: <?php echo $list_divider_space_above; ?>px !important;
            border-top: <?php echo $list_divider_thickness; ?>px solid <?php echo $list_divider_color; ?> !important;
            padding-top: <?php echo $list_divider_space_below; ?>px !important;
        }
        <?php endif; ?>

        /* Float the image — override the inline style with !important */
        <?php if ( 'yes' === $list_show_thumb ) : ?>
        .wp-block-post-template .wp-block-post .wp-block-post-featured-image,
        .wp-block-post-template .wp-block-post .firefly-injected-img {
            display: block !important;
            float: <?php echo ( 'right' === $list_thumb_align ) ? 'right' : 'left'; ?> !important;
            width: <?php echo $list_thumb_width; ?>px !important;
            height: <?php echo $list_thumb_height; ?>px !important;
            overflow: hidden !important;
            margin: <?php echo ( 'right' === $list_thumb_align ) ? '0 0 8px 16px' : '0 16px 8px 0'; ?> !important;
            flex-shrink: unset !important;
            /* Override inline overflow:hidden that the injector sets */
        }
        .wp-block-post-template .wp-block-post .wp-block-post-featured-image a,
        .wp-block-post-template .wp-block-post .firefly-injected-img a {
            display: block !important;
            line-height: 0 !important;
        }
        .wp-block-post-template .wp-block-post .wp-block-post-featured-image img,
        .wp-block-post-template .wp-block-post .wp-block-post-featured-image a img,
        .wp-block-post-template .wp-block-post .firefly-injected-img img,
        .wp-block-post-template .wp-block-post .firefly-injected-img a img {
            display: block !important;
            width: <?php echo $list_thumb_width; ?>px !important;
            height: <?php echo $list_thumb_height; ?>px !important;
            max-width: none !important;
            object-fit: cover !important;
            object-position: center !important;
        }
        <?php else : ?>
        .wp-block-post-template .wp-block-post .wp-block-post-featured-image,
        .wp-block-post-template .wp-block-post .firefly-injected-img {
            display: none !important;
        }
        <?php endif; ?>

        /* Content block: sits beside the float naturally */
        .wp-block-post-template .wp-block-post .wp-block-group {
            overflow: visible !important;
            padding: 0 !important;
            /* Do NOT set width — let it flow around the float */
        }
        .wp-block-post-template .wp-block-post .wp-block-group__inner-container {
            padding: 0 !important;
            margin: 0 !important;
        }

        /* List heading */
        .wp-block-post-template .wp-block-post .wp-block-post-title,
        .wp-block-post-template .wp-block-post .wp-block-post-title a,
        .wp-block-post-template .wp-block-post .wp-block-post-title a:hover,
        .wp-block-post-template .wp-block-post .wp-block-post-title a:visited,
        .wp-block-post-template .wp-block-post .wp-block-post-title h1,
        .wp-block-post-template .wp-block-post .wp-block-post-title h2,
        .wp-block-post-template .wp-block-post .wp-block-post-title h3,
        .wp-block-post-template .wp-block-post .wp-block-post-title h4,
        .wp-block-post-template .wp-block-post .wp-block-post-title h1 a,
        .wp-block-post-template .wp-block-post .wp-block-post-title h2 a,
        .wp-block-post-template .wp-block-post .wp-block-post-title h3 a,
        .wp-block-post-template .wp-block-post .wp-block-post-title h4 a {
            font-size: <?php echo $list_title_size; ?>px !important;
            color: <?php echo $list_title_color; ?> !important;
            line-height: 1.3 !important;
        }
        .wp-block-post-template .wp-block-post .wp-block-post-title {
            margin-top: <?php echo $list_title_space_above; ?>px !important;
            margin-bottom: <?php echo $list_title_space_below; ?>px !important;
        }

        /* List summary */
        .wp-block-post-template .wp-block-post .wp-block-post-excerpt,
        .wp-block-post-template .wp-block-post .wp-block-post-excerpt p,
        .wp-block-post-template .wp-block-post .wp-block-post-excerpt__excerpt,
        .wp-block-post-template .wp-block-post .wp-block-post-excerpt__excerpt p {
            font-size: <?php echo $list_summary_size; ?>px !important;
            color: <?php echo $list_summary_color; ?> !important;
            margin: 0 !important;
        }
        .wp-block-post-template .wp-block-post .wp-block-post-excerpt__more-text {
            margin-top: 0 !important;
            margin-bottom: 0 !important;
        }

        /* List read-more link */
        .wp-block-post-template .wp-block-post .wp-block-post-excerpt__more-link,
        .wp-block-post-template .wp-block-post .wp-block-post-excerpt__more-link:visited,
        .wp-block-post-template .wp-block-post .wp-block-read-more,
        .wp-block-post-template .wp-block-post .wp-block-read-more:visited,
        .wp-block-post-template .wp-block-post .wp-block-read-more:hover {
            font-size: <?php echo $list_link_size; ?>px !important;
            color: <?php echo $list_link_color; ?> !important;
            text-decoration: none !important;
            margin-top: <?php echo $list_link_space_above; ?>px !important;
            display: block !important;
        }

        /* List post date */
        .firefly-list-date {
            display: <?php echo ( 'yes' === $list_show_date ) ? 'block' : 'none'; ?> !important;
            font-size: <?php echo $list_date_size; ?>px !important;
            color: <?php echo $list_date_color; ?> !important;
            margin-top: <?php echo $list_date_space_above; ?>px !important;
            margin-bottom: 4px !important;
        }

        <?php endif; /* end list mode */ ?>

    </style>
    <?php
}

/* -----------------------------------------------------------------------
 * 2c.  INJECT POST DATE IN LIST MODE
 *
 * Hooks core/post-title render_block and inserts a date span INSIDE the
 * rendered title HTML (before the <h2>/<h3> tag) so it sits inside the
 * .wp-block-group flex content column, not outside it.
 * --------------------------------------------------------------------- */
add_filter( 'render_block', 'firefly_list_inject_date', 10, 2 );

function firefly_list_inject_date( $block_content, $block ) {

    if ( 'core/post-title' !== $block['blockName'] ) {
        return $block_content;
    }
    if ( 'list' !== get_theme_mod( 'firefly_post_list_style', 'grid' ) ) {
        return $block_content;
    }
    if ( 'yes' !== get_theme_mod( 'firefly_list_show_date', 'no' ) ) {
        return $block_content;
    }

    $post = get_post();
    if ( ! $post ) {
        return $block_content;
    }

    $date      = get_the_date( get_option( 'date_format' ), $post );
    $date_span = '<span class="firefly-list-date">' . esc_html( $date ) . '</span>';

    // Insert date span before the first <h1>/<h2>/<h3>/<h4> inside the block
    $block_content = preg_replace(
        '/(<h[1-6]\b)/i',
        $date_span . '$1',
        $block_content,
        1
    );

    return $block_content;
}

/* -----------------------------------------------------------------------
 * 2b.  TRUNCATE EXCERPT IN LIST MODE
 *
 * Neither excerpt_length nor get_the_excerpt filters reliably affect the
 * Query Loop block — it either uses the post's saved manual excerpt field
 * (which is already a fixed string) or its own internal word-limit attr.
 *
 * The only reliable approach is to intercept the rendered HTML in
 * render_block for core/post-excerpt, look up the current post, strip
 * the excerpt/content to plain text ourselves, slice to $max_words, and
 * rewrite the paragraph in the output.
 * --------------------------------------------------------------------- */
add_filter( 'render_block', 'firefly_list_truncate_excerpt', 10, 2 );

function firefly_list_truncate_excerpt( $block_content, $block ) {

    if ( 'core/post-excerpt' !== $block['blockName'] ) {
        return $block_content;
    }
    if ( 'list' !== get_theme_mod( 'firefly_post_list_style', 'grid' ) ) {
        return $block_content;
    }

    $max_words = (int) get_theme_mod( 'firefly_list_summary_words', 30 );
    if ( $max_words <= 0 ) {
        return $block_content;
    }

    // Get the current post in the loop
    $post = get_post();
    if ( ! $post ) {
        return $block_content;
    }

    // Build our own plain-text excerpt from the post:
    // Prefer the manual excerpt; fall back to post content.
    if ( ! empty( $post->post_excerpt ) ) {
        $source = $post->post_excerpt;
    } else {
        // Strip shortcodes and blocks, then get plain text
        $source = strip_shortcodes( $post->post_content );
        $source = strip_tags( $source );
    }

    $source = wp_strip_all_tags( $source );
    $source = preg_replace( '/\s+/', ' ', trim( $source ) );
    $words  = explode( ' ', $source );
    $words  = array_filter( $words ); // remove empties
    $words  = array_values( $words );

    $suffix    = ( count( $words ) > $max_words ) ? '&hellip;' : '';
    $trimmed   = implode( ' ', array_slice( $words, 0, $max_words ) ) . $suffix;

    // Now splice $trimmed into the rendered block HTML.
    // The excerpt paragraph has class wp-block-post-excerpt__excerpt.
    // Replace whatever text is inside it with our trimmed version.
    $block_content = preg_replace_callback(
        '/(<p\b[^>]*class="[^"]*wp-block-post-excerpt__excerpt[^"]*"[^>]*>)(.*?)(<\/p>)/is',
        function( $m ) use ( $trimmed ) {
            return $m[1] . $trimmed . $m[3];
        },
        $block_content
    );

    // Fallback: if that class wasn't present, replace the first <p> that
    // is NOT the "more" paragraph.
    if ( strpos( $block_content, $trimmed ) === false ) {
        $replaced = false;
        $block_content = preg_replace_callback(
            '/(<p\b[^>]*>)(.*?)(<\/p>)/is',
            function( $m ) use ( $trimmed, &$replaced ) {
                if ( $replaced ) return $m[0];
                if ( strpos( $m[0], 'wp-block-post-excerpt__more' ) !== false ) return $m[0];
                $replaced = true;
                return $m[1] . $trimmed . $m[3];
            },
            $block_content
        );
    }

    return $block_content;
}

/* -----------------------------------------------------------------------
 * 3.  SANITIZE CALLBACKS
 * --------------------------------------------------------------------- */
function firefly_sanitize_radio( $value ) {
    return in_array( $value, [ 'yes', 'no' ], true ) ? $value : 'yes';
}

function firefly_sanitize_list_style( $value ) {
    return in_array( $value, [ 'grid', 'list' ], true ) ? $value : 'grid';
}

function firefly_sanitize_thumb_align( $value ) {
    return in_array( $value, [ 'left', 'right' ], true ) ? $value : 'left';
}

function firefly_sanitize_float( $value ) {
    return is_numeric( $value ) ? (float) $value : 0;
}

function firefly_sanitize_font_weight( $value ) {
    $allowed = [ '100','200','300','400','500','600','700','800','900' ];
    return in_array( (string) $value, $allowed, true ) ? $value : '400';
}
