<?php
namespace Softech_Addons_for_Elementor\Widgets;

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}
if ( ! class_exists( '\\Softech_Addons_for_Elementor\\Includes\\Softech_Post_Query_Manager' ) ) {
    require_once SITS_ELEMENTOR_ADDONS_PLUGIN_PATH . 'includes/class-post-query-manager.php';
}
use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Background;
use Softech_Addons_for_Elementor\Includes\Softech_Post_Query_Manager;

/**
 * Sitsel Taxonomy Filter Grid Widget Class
 */
class sitsel_loop_grid_slider extends \Elementor\Widget_Base 
{
    /**
     * Cache group for templates
     */
    private const CACHE_GROUP = 'sitsel_templates';
    
    /**
     * Cache expiration time (1 hour)
     */
    private const CACHE_EXPIRATION = 3600;
    
    /**
     * Post types to exclude from selection
     */
    private const EXCLUDED_POST_TYPES = [
        'attachment', 
        'elementor_library', 
        'wp_block', 
        'elementor_snippet', 
        'elementor_font', 
        'elementor_icons', 
        'floating-elements', 
        'floating_elements'
    ];

    /**
     * Widget name
     *
     * @return string Widget name
     */
    public function get_name() {
        return 'sitsel_loop_slider';
    }

    /**
     * Widget title
     *
     * @return string Widget title
     */
    public function get_title() {
        return __('SITS Loop Grid / Slider', 'softech-addons-for-elementor');
    }

    /**
     * Widget icon
     *
     * @return string Widget icon
     */
    public function get_icon() {
        return 'eicon-posts-grid';
    }

    /**
     * Widget categories
     *
     * @return array Widget categories
     */
    public function get_categories() {
        return ['sits-category'];
    }

    /**
     * Widget keywords
     *
     * @return array Widget keywords
     */
    public function get_keywords() {
        return ['grid', 'taxonomy', 'filter', 'posts', 'custom post type', 'query'];
    }

    /**
 * Register widget assets
 */
private function register_widget_assets() {
    $plugin_url = SITS_ELEMENTOR_ADDONS_PLUGIN_URL;
    $version    = SITS_ELEMENTOR_ADDONS_VERSION;

    // Register CSS
    if (!wp_style_is('sitsel-common-css', 'registered')) {
        wp_register_style(
            'sitsel-common-css',
            $plugin_url . 'assets/css/sitsel-comman.css',
            [],
            $version
        );
    }

    // Register JS
    if (!wp_script_is('sitsel-post-slider-js', 'registered')) {
        wp_register_script(
            'sitsel-post-slider-js',
            $plugin_url . 'assets/js/sitsel-post-slider.js',
            ['jquery'], // dependencies
            $version,
            true        // load in footer
        );
    }
    
//     wp_localize_script(
//     'sitsel-post-slider-js',
//     'ajaxurl',
//     admin_url('admin-ajax.php')
// );

}

/**
 * Get style dependencies
 *
 * @return array
 */
public function get_style_depends() {
    $this->register_widget_assets();
    return ['sitsel-common-css'];
}


/**
 * Get script dependencies
 *
 * @return array
 */
public function get_script_depends() {
    $this->register_widget_assets();
    return ['sitsel-post-slider-js'];
}


    /**
     * Get available post layouts
     *
     * @return array Layout options
     */
    private function softech_post_layouts_array() {
        return [
            'grid' => esc_html__('Grid', 'softech-addons-for-elementor'),
            'pro-slider' => esc_html__('Slider (Pro)', 'softech-addons-for-elementor'),
        ];
    }

    /**
     * Get all public post types
     *
     * @return array Post types array
     */
    private function get_post_types() {
        $post_types_objects = get_post_types(['public' => true], 'objects');
        $post_types = [];

        foreach ($post_types_objects as $post_type) {
            if (!in_array($post_type->name, self::EXCLUDED_POST_TYPES, true)) {
                $post_types[$post_type->name] = $post_type->label;
            }
        }

        return $post_types;
    }

    /**
     * Get all public taxonomies
     *
     * @return array Taxonomies array
     */
    private function get_post_taxonomies() {
        $taxonomy_objects = get_taxonomies(['public' => true], 'objects');
        $exclude = ['post_format', 'elementor_library_category', 'elementor_library_tag'];
        $taxonomies = [];

        foreach ($taxonomy_objects as $taxonomy) {
            if (!in_array($taxonomy->name, $exclude, true)) {
                $taxonomies[$taxonomy->name] = $taxonomy->label;
            }
        }

        return $taxonomies;
    }

    /**
     * Get all categories
     *
     * @return array Categories array
     */
    private function get_the_category() {
        $categories = get_categories(['hide_empty' => false]);
        $options = [];

        foreach ($categories as $category) {
            $options[$category->slug] = $category->name;
        }

        return $options;
    }

    /**
     * Get all posts for manual selection
     *
     * @return array Posts array
     */
    private function get_all_posts() {
        $posts = get_posts([
            'numberposts' => 100,
            'post_type' => 'any',
            'post_status' => 'publish',
        ]);

        $options = [];
        foreach ($posts as $post) {
            $options[$post->ID] = $post->post_title;
        }

        return $options;
    }

    /**
     * Get all tags
     *
     * @return array Tags array
     */
    private function get_all_tags() {
        $tags = get_tags(['hide_empty' => false]);
        $options = [];

        foreach ($tags as $tag) {
            $options[$tag->slug] = $tag->name;
        }

        return $options;
    }

    /**
     * Get Elementor Templates (Cached)
     */
    protected function sitsel_get_elementor_templates_without_default_kit() {
        if (!class_exists('\Elementor\Plugin')) {
            return [];
        }

        $cache_key = 'sitsel_elementor_templates';
        $templates = wp_cache_get($cache_key, self::CACHE_GROUP);

        if (false === $templates) {
            $args = [
                'post_type' => 'elementor_library',
                'post_status' => 'publish',
                'posts_per_page' => 100,
                'fields' => 'ids',
                'orderby' => 'title',
                'order' => 'ASC',
            ];

            $query = new \WP_Query($args);
            $templates = [];

            foreach ($query->posts as $post_id) {
                $post = get_post($post_id);
                if ($post && $post->post_title !== 'Default Kit') {
                    $templates[$post_id] = $post->post_title;
                }
            }

            wp_cache_set($cache_key, $templates, self::CACHE_GROUP, self::CACHE_EXPIRATION);
            wp_reset_postdata();
        }

        return $templates;
    }

    /**
     * Get dynamic template type options
     *
     * @return array Template type options
     */
    private function get_dynamic_template_type_options() {
        $post_types = $this->get_post_types();
        $options = [];

        foreach ($post_types as $post_type_slug => $post_type_label) {
            $options[$post_type_slug] = $post_type_label;
            $options[$post_type_slug . '_taxonomy'] = sprintf(
                /* translators: %s: Post type label */
                esc_html__('%s Taxonomy', 'softech-addons-for-elementor'),
                $post_type_label
            );
        }

        return $options;
    }

    /**
     * Register widget controls
     */
    protected function register_controls() {
        // CHANGE TO:
        Softech_Post_Query_Manager::register_controls( $this, [
            'source_control' => '',
            'section_id'     => 'query_section',
            'section_label'  => esc_html__( 'Query', 'softech-addons-for-elementor' ),
            'mode'           => 'full',
        ] );
        $this->register_layout_controls();
        $this->register_items_controls();
        $this->register_style_controls();
    }

 

    /**
     * Register Layout Controls Section
     */
    private function register_layout_controls() {
        $this->start_controls_section(
            'layout_section',
            [
                'label' => esc_html__('Layout', 'softech-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_CONTENT,
            ]
        );

        // Layout Type
        $this->add_control(
            'layout',
            [
                'label' => esc_html__('Layout', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'default' => 'grid',
                'options' => $this->softech_post_layouts_array(),
            ]
        );

        // Upgrade to Pro Notice
        Utilities::upgrade_pro_notice($this, Controls_Manager::RAW_HTML, 'taxonomy_filter_slider', 'layout', [
            'pro-slider', 
        ]);

        // Gap
        
		$this->add_responsive_control(
			'gap',
			[
				'label'      => esc_html__('Gap', 'softech-addons-for-elementor'),
				'type'       => Controls_Manager::SLIDER,
				'size_units' => ['px', '%', 'em', 'rem', 'custom'],
				'range'      => [
					'px'  => [ 'min' => 0, 'max' => 100, 'step' => 1  ],
					'%'   => [ 'min' => 0, 'max' => 10,  'step' => 0.1 ],
					'em'  => [ 'min' => 0, 'max' => 10,  'step' => 0.1 ],
					'rem' => [ 'min' => 0, 'max' => 10,  'step' => 0.1 ],
				],
				'default'    => [
					'size' => 15,
					'unit' => 'px',
				],
				'selectors'  => [
					'{{WRAPPER}} .sitsel-grid' => 'gap: {{SIZE}}{{UNIT}};',
				],
				'condition'  => [
					'layout' => 'grid',
				],
			]
		);

        // Columns
        $this->add_responsive_control(
            'columns',
            [
                'label' => esc_html__('Columns', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'default' => '3',
                'tablet_default' => '2',
                'mobile_default' => '1',
                'options' => [
                    '1' => '1',
                    '2' => '2',
                    '3' => '3',
                    '4' => '4',
                    '5' => '5',
                    '6' => '6',
                ],
                'selectors' => [
                    '{{WRAPPER}} .sitsel-grid' => 'display: grid; grid-template-columns: repeat({{VALUE}}, 1fr);',
                ],
                'condition' => [
                    'layout' => 'grid',
                ],
            ]
        );

        // Pagination
        $this->add_control(
        'pagination',
        [
            'label'   => esc_html__('Navigation', 'softech-addons-for-elementor'),
            'type'    => Controls_Manager::SELECT,
            'default' => 'yes',
            'options' => [
                ''          => esc_html__('None', 'softech-addons-for-elementor'),
                'yes'       => esc_html__('Pagination', 'softech-addons-for-elementor'),
                'load_more' => esc_html__('Load More Button', 'softech-addons-for-elementor'),
            ],
            'condition' => [ 'layout' => 'grid' ],
        ]
    );

        // Load More button label control
        $this->add_control(
            'load_more_text',
            [
                'label'     => esc_html__('Load More Text', 'softech-addons-for-elementor'),
                'type'      => Controls_Manager::TEXT,
                'default'   => esc_html__('Load More', 'softech-addons-for-elementor'),
                'condition' => [ 'layout' => 'grid', 'pagination' => 'load_more' ],
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Register Items Controls Section
     */
    private function register_items_controls() {
        $this->start_controls_section(
            'section_post_elements',
            [
                'label' => esc_html__('Items', 'softech-addons-for-elementor'),
            ]
        );

        // Layout Source
        $this->add_control(
            'layout_source',
            [
                'label' => esc_html__('Item Source', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::CHOOSE,
                'options' => [
                    'template' => [
                        'title' => esc_html__('Template', 'softech-addons-for-elementor'),
                        'icon' => 'eicon-folder',
                    ],
                    'elements' => [
                        'title' => esc_html__('Elements', 'softech-addons-for-elementor'),
                        'icon' => 'eicon-editor-list-ul',
                    ],
                ],
                'default' => 'elements',
                'toggle' => true,
            ]
        );

        // Template Selection
        $this->add_control(
            'template_id',
            [
                'label' => esc_html__('Select Layout Template', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::SELECT2,
                'label_block' => true,
                'options' => $this->sitsel_get_elementor_templates_without_default_kit(),
                'default' => '',
                'condition' => [
                    'layout_source' => 'template',
                ],
            ]
        );

        // Edit Template Button
        $this->add_control(
            'edit_template_button',
            [
                'type' => Controls_Manager::RAW_HTML,
                'raw' => '<a href="#" id="sitsel-edit-template-button" class="elementor-button elementor-button-success" target="_blank" style="display:none;">' .
                    esc_html__('Edit Template', 'softech-addons-for-elementor') .
                    '</a>',
                'content_classes' => 'sitsel-edit-template-button-wrap',
                'condition' => [
                    'layout_source' => 'template',
                ],
            ]
        );

        // Show Image
        $this->add_control(
            'show_image',
            [
                'label' => esc_html__('Show Image', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::SWITCHER,
                'label_on' => esc_html__('Yes', 'softech-addons-for-elementor'),
                'label_off' => esc_html__('No', 'softech-addons-for-elementor'),
                'return_value' => 'yes',
                'default' => 'yes',
                'condition' => [
                    'layout_source' => 'elements',
                ],
            ]
        );

        // Image Source Type
        $this->add_control(
            'image_source_type',
            [
                'label' => esc_html__('Image Source', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'default' => 'featured',
                'options' => [
                    'featured' => esc_html__('Featured Image', 'softech-addons-for-elementor'),
                    'acf_image' => esc_html__('Custom Field (Image)', 'softech-addons-for-elementor'),
                ],
                'condition' => [
                    'layout_source' => 'elements',
                    'show_image' => 'yes',
                ],
            ]
        );

        // Custom Image Field Key
        $this->add_control(
            'custom_image_field_key',
            [
                'label' => esc_html__('Custom Image Field Key', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'placeholder' => esc_attr__('e.g., my_image_field', 'softech-addons-for-elementor'),
                'label_block' => true,
                'condition' => [
                    'layout_source' => 'elements',
                    'show_image' => 'yes',
                    'image_source_type' => 'acf_image',
                ],
            ]
        );

        // Post Elements Repeater (for post-type templates)
        $this->register_post_elements_repeater();

        // Archive Elements Repeater (for taxonomy archive templates)
        $this->register_archive_repeater();

        $this->end_controls_section();
    }

    /**
     * Register Post Elements Repeater
     */
    private function register_post_elements_repeater() {
        $repeater = new Repeater();

        $element_options = [
            'title' => __('Title', 'softech-addons-for-elementor'),
            'content' => __('Content', 'softech-addons-for-elementor'),
            'excerpt' => __('Excerpt', 'softech-addons-for-elementor'),
            'date' => __('Date', 'softech-addons-for-elementor'),
            'time' => __('Time', 'softech-addons-for-elementor'),
            'author' => __('Author', 'softech-addons-for-elementor'),
            'comments' => __('Comments', 'softech-addons-for-elementor'),
            'read_more' => __('Read More', 'softech-addons-for-elementor'),
            'category' => __('Category', 'softech-addons-for-elementor'),
            'tag' => __('Tags', 'softech-addons-for-elementor'),
            'featured_image' => __('Featured Image', 'softech-addons-for-elementor'),
            'custom_field' => __('Custom Field (Text)', 'softech-addons-for-elementor'),
            'acf_image' => __('ACF Image Field', 'softech-addons-for-elementor'),
        ];

        // Element Type
        $repeater->add_control(
            'element_type',
            [
                'label' => esc_html__('Select Element', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'options' => $element_options,
                'default' => 'title',
            ]
        );

        // Custom Field Key (Text)
        $repeater->add_control(
            'custom_field_key',
            [
                'label' => esc_html__('Custom Field Key', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'placeholder' => 'e.g., my_custom_field',
                'label_block' => true,
                'condition' => [
                    'element_type' => 'custom_field',
                ],
            ]
        );

        // Custom Field Key (Image)
        $repeater->add_control(
            'custom_field_key_image',
            [
                'label' => esc_html__('Field Key (for Custom or ACF)', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'placeholder' => 'e.g., my_custom_image',
                'label_block' => true,
                'condition' => [
                    'element_type' => 'acf_image',
                ],
            ]
        );

        // HTML Tag for Title
        $repeater->add_control(
            'html_tag',
            [
                'label' => __('Title HTML Tag', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'default' => 'h3',
                'options' => [
                    'h1' => 'H1',
                    'h2' => 'H2',
                    'h3' => 'H3',
                    'h4' => 'H4',
                    'h5' => 'H5',
                    'h6' => 'H6',
                    'div' => 'div',
                    'span' => 'span',
                    'p' => 'p',
                ],
                'condition' => [
                    'element_type' => 'title',
                ],
            ]
        );

        // Text Align for specific elements
        $this->add_repeater_text_align_controls($repeater, $element_options);

        // Excerpt Line Clamp
        $repeater->add_responsive_control(
            'excerpt_line_clamp',
            [
                'label' => esc_html__('Line Clamp', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::NUMBER,
                'min' => 1,
                'max' => 10,
                'default' => 2,
                'selectors' => [
                    '{{WRAPPER}} .sitsel-post-excerpt' => '-webkit-line-clamp: {{VALUE}};',
                ],
                'condition' => [
                    'element_type' => 'excerpt',
                ],
            ]
        );

        // Read More Controls
        $this->add_repeater_read_more_controls($repeater);

        // Add Repeater to Widget
        $this->add_control(
            'post_elements',
            [
                'label' => __('Post Elements', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'title_field' => '{{ element_type }}',
                'default' => [
                    ['element_type' => 'title'],
                    ['element_type' => 'date'],
                    ['element_type' => 'excerpt'],
                ],
                'condition' => [
                    'layout_source' => 'elements',
                    'choose_template_type' => array_keys($this->get_post_types()),
                ],
            ]
        );
    }

    /**
     * Register Archive Elements Repeater (shown for taxonomy templates)
     */
    private function register_archive_repeater() {
        $repeater = new Repeater();

        $archive_element_options = [
            'archive_title'       => __('Archive Title', 'softech-addons-for-elementor'),
            'archive_description' => __('Archive Description', 'softech-addons-for-elementor'),
            'archive_posts'       => __('Archive Posts', 'softech-addons-for-elementor'),
            'custom_field'        => __('Custom Field (Text)', 'softech-addons-for-elementor'),
        ];

        $repeater->add_control(
            'element_type',
            [
                'label' => esc_html__('Select Element', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'options' => $archive_element_options,
                'default' => 'archive_title',
            ]
        );

        // Custom Field Key
        $repeater->add_control(
            'custom_field_key',
            [
                'label' => esc_html__('Custom Field Key', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'placeholder' => 'e.g., my_custom_field',
                'label_block' => true,
                'condition' => [
                    'element_type' => 'custom_field',
                ],
            ]
        );

        // Add Repeater to Widget (archive_elements)
        $this->add_control(
            'archive_elements',
            [
                'label' => __('Archive Elements', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::REPEATER,
                'fields' => $repeater->get_controls(),
                'title_field' => '{{ element_type }}',
                'default' => [
                    ['element_type' => 'archive_title'],
                    ['element_type' => 'archive_description'],
                    ['element_type' => 'archive_posts'],
                ],
                'condition' => [
                    'layout_source' => 'elements',
                    'choose_template_type' => array_map(function($pt) { return $pt . '_taxonomy'; }, array_keys($this->get_post_types())),
                ],
            ]
        );
    }

    /**
     * Add Text Align Controls to Repeater
     */
    private function add_repeater_text_align_controls($repeater, $element_options) {
        $text_align_elements = ['title', 'date', 'tag', 'time', 'content', 'excerpt', 'custom_field', 'category', 'author'];

        foreach ($element_options as $key => $label) {
            if (!in_array($key, $text_align_elements)) {
                continue;
            }

            $readable = ucwords(str_replace(['-', '_'], ' ', $key));

            $repeater->add_control(
                "{$key}_text_align",
                [
                    'label' => sprintf(
                        esc_html('%s Text Align', 'softech-addons-for-elementor'),
                        esc_html($readable)
                    ),
                    'type' => Controls_Manager::CHOOSE,
                    'options' => [
                        'left' => [
                            'title' => esc_html__('Left', 'softech-addons-for-elementor'),
                            'icon' => 'eicon-text-align-left',
                        ],
                        'center' => [
                            'title' => esc_html__('Center', 'softech-addons-for-elementor'),
                            'icon' => 'eicon-text-align-center',
                        ],
                        'right' => [
                            'title' => esc_html__('Right', 'softech-addons-for-elementor'),
                            'icon' => 'eicon-text-align-right',
                        ],
                    ],
                    'condition' => [
                        'element_type' => $key,
                    ],
                    'selectors' => [
                        "{{WRAPPER}} .{$label}" => 'text-align: {{VALUE}};',
                    ],
                ]
            );
        }
    }

    /**
     * Add Read More Controls to Repeater
     */
    private function add_repeater_read_more_controls($repeater) {
        $repeater->add_control(
            'read_more_text',
            [
                'label' => esc_html__('Read More Text', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::TEXT,
                'default' => esc_html__('Read More', 'softech-addons-for-elementor'),
                'placeholder' => esc_html__('Enter text', 'softech-addons-for-elementor'),
                'condition' => [
                    'element_type' => 'read_more',
                ],
            ]
        );

        $repeater->add_control(
            'read_more_icon',
            [
                'label' => esc_html__('Select Icon', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::ICONS,
                'label_block' => true,
                'skin' => 'inline',
                'default' => [
                    'value' => 'fas fa-arrow-right',
                    'library' => 'fa-solid',
                ],
                'condition' => [
                    'element_type' => 'read_more',
                ],
            ]
        );

        $repeater->add_control(
            'read_more_icon_position',
            [
                'label' => esc_html__('Icon Position', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'default' => 'after',
                'options' => [
                    'before' => esc_html__('Before Text', 'softech-addons-for-elementor'),
                    'after' => esc_html__('After Text', 'softech-addons-for-elementor'),
                ],
                'condition' => [
                    'element_type' => 'read_more',
                ],
            ]
        );
    }

    /**
     * Register Style Controls
     */
    private function register_style_controls() {
        $this->register_wrapper_style_controls();
        $this->register_inner_content_style_controls();
        $this->register_title_style_controls();
        $this->register_featured_image_style_controls();
        $this->register_date_time_style_controls();
        $this->register_excerpt_style_controls();
        $this->register_author_style_controls();
        $this->register_category_style_controls();
        $this->register_comments_style_controls();
        $this->register_custom_field_style_controls();
        $this->register_read_more_style_controls();
        $this->register_pagination_style_controls();
        $this->register_load_more_style_controls();
    }

    /**
     * Register Wrapper Style Controls
     */
    private function register_wrapper_style_controls() {
        $this->start_controls_section(
            'section_grid_item_style_wrapper',
            [
                'label' => __('Item - Wrapper', 'softech-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
            ]
        );

        $this->add_control(
            'wrapper_background',
            [
                'label' => __('Background Color', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sitsel-grid-item-inner' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'wrapper_box',
                'selector' => '{{WRAPPER}} .sitsel-grid-item-inner',
                'fields_options' => [
                    'border' => [
                        'default' => 'solid',
                    ],
                    'width' => [
                        'default' => [
                            'top' => 1,
                            'right' => 1,
                            'bottom' => 1,
                            'left' => 1,
                        ],
                    ],
                    'color' => [
                        'default' => '#CCCCCC',
                    ],
                ],
            ]
        );

        $this->add_responsive_control(
            'wrapper_box_radius',
            [
                'label' => esc_html__('Border Radius', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em'],
                'default' => [
                    'top' => 10,
                    'right' => 10,
                    'bottom' => 10,
                    'left' => 10,
                    'unit' => 'px',
                ],
                'selectors' => [
                    '{{WRAPPER}} .sitsel-grid-item-inner' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'wrapper_box_shadow',
                'selector' => '{{WRAPPER}} .sitsel-grid-item-inner',
            ]
        );

        $this->add_responsive_control(
            'wrapper_padding',
            [
                'label' => __('Padding', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em'],
                'selectors' => [
                    '{{WRAPPER}} .sitsel-grid-item-inner' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Register Inner Content Style Controls
     */
    private function register_inner_content_style_controls() {
        $this->start_controls_section(
            'section_grid_item_style_inner',
            [
                'label' => __('Item - Inner Content', 'softech-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'layout_source' => 'elements',
                ],
            ]
        );

        $this->add_control(
            'inner_background',
            [
                'label' => __('Background Color', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sitsel-post-content-wrapper' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'inner_box',
                'selector' => '{{WRAPPER}} .sitsel-post-content-wrapper',
            ]
        );

        $this->add_responsive_control(
            'inner_box_radius',
            [
                'label' => esc_html__('Border Radius', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em'],
                'selectors' => [
                    '{{WRAPPER}} .sitsel-post-content-wrapper' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'inner_box_shadow',
                'selector' => '{{WRAPPER}} .sitsel-post-content-wrapper',
            ]
        );

        $this->add_responsive_control(
            'inner_padding',
            [
                'label' => __('Padding', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em'],
                'default' => [
                    'top' => 15,
                    'right' => 15,
                    'bottom' => 15,
                    'left' => 15,
                    'unit' => 'px',
                ],
                'selectors' => [
                    '{{WRAPPER}} .sitsel-post-content-wrapper' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Register Title Style Controls
     */
    private function register_title_style_controls() {
        $this->start_controls_section(
            'style_title',
            [
                'label' => esc_html__('Title', 'softech-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'layout_source' => 'elements',
                ],
            ]
        );

        $this->start_controls_tabs('tabs_title_color');

        $this->start_controls_tab(
            'tab_title_normal',
            [
                'label' => esc_html__('Normal', 'softech-addons-for-elementor'),
            ]
        );

        $this->add_control(
            'title_color',
            [
                'label' => esc_html__('Text Color', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sitsel-post-title a' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_tab();

        $this->start_controls_tab(
            'tab_title_hover',
            [
                'label' => esc_html__('Hover', 'softech-addons-for-elementor'),
            ]
        );

        $this->add_control(
            'title_hover_color',
            [
                'label' => esc_html__('Text Color', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sitsel-post-title a:hover' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->end_controls_tab();
        $this->end_controls_tabs();

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'title_typography',
                'selector' => '{{WRAPPER}} .sitsel-post-title a',
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Register Featured Image Style Controls
     */
    private function register_featured_image_style_controls() {
        $this->start_controls_section(
            'sitsel_featured_image_style',
            [
                'label' => esc_html__('Featured Image', 'softech-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'layout_source' => 'elements',
                ],
            ]
        );

        $this->add_responsive_control(
            'sitsel_featured_image_height',
            [
                'label' => esc_html__('Height', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'size_units' => ['px', '%', 'vh'],
                'range' => [
                    'px' => [
                        'max' => 400,
                    ],
                ],
                'default' => [
                    'size' => 200,
                    'unit' => 'px',
                ],
                'selectors' => [
                    '{{WRAPPER}} .sitsel-post-featured-image img' => 'height: {{SIZE}}{{UNIT}}; object-fit: cover;',
                ],
            ]
        );

        $this->add_responsive_control(
            'sitsel_featured_image_object_fit',
            [
                'label' => esc_html__('Object Fit', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'options' => [
                    'cover' => __('Cover', 'softech-addons-for-elementor'),
                    'contain' => __('Contain', 'softech-addons-for-elementor'),
                    'fill' => __('Fill', 'softech-addons-for-elementor'),
                    'none' => __('None', 'softech-addons-for-elementor'),
                    'scale-down' => __('Scale Down', 'softech-addons-for-elementor'),
                ],
                'default' => 'cover',
                'selectors' => [
                    '{{WRAPPER}} .sitsel-post-featured-image img' => 'object-fit: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'sitsel_featured_image_border',
                'selector' => '{{WRAPPER}} .sitsel-image-overlay-wrap',
            ]
        );

        $this->add_responsive_control(
            'sitsel_featured_image_radius',
            [
                'label' => esc_html__('Border Radius', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%'],
                'selectors' => [
                    '{{WRAPPER}} .sitsel-image-overlay-wrap' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_control(
            'sitsel_featured_image_overlay_animation',
            [
                'label' => esc_html__('Overlay Animation', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::SELECT,
                'default' => 'fade-in',
                'options' => [
                    'none' => __('None', 'softech-addons-for-elementor'),
                    'fade-in' => __('Fade In', 'softech-addons-for-elementor'),
                    'fade-out' => __('Fade Out', 'softech-addons-for-elementor'),
                    'slide-up' => __('Slide Up', 'softech-addons-for-elementor'),
                    'zoom-in' => __('Zoom In', 'softech-addons-for-elementor'),
                ],
            ]
        );

        $this->add_control(
            'sitsel_overlay_background_color',
            [
                'label' => esc_html__('Overlay Background Color', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'default' => 'rgba(0, 0, 0, 0.4)',
                'selectors' => [
                    '{{WRAPPER}} .sitsel-image-overlay' => 'background-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_control(
            'sitsel_overlay_transition_duration',
            [
                'label' => esc_html__('Transition Duration (s)', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'default' => [
                    'size' => 0.4,
                ],
                'range' => [
                    'px' => [
                        'min' => 0.1,
                        'max' => 5,
                        'step' => 0.1,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .sitsel-image-overlay' => 'transition-duration: {{SIZE}}s;',
                ],
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Register Date Time Style Controls
     */
    private function register_date_time_style_controls() {
        $this->start_controls_section(
            'style_date_time',
            [
                'label' => esc_html__('Date Time', 'softech-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'layout_source' => 'elements',
                ],
            ]
        );

        $this->add_control(
            'date_time_color',
            [
                'label' => esc_html__('Color', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sitsel-post-date' => 'color: {{VALUE}};',
                    '{{WRAPPER}} .sitsel-post-time' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'date_time_typography',
                'selector' => '{{WRAPPER}} .sitsel-post-date, {{WRAPPER}} .sitsel-post-time',
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Register Excerpt Style Controls
     */
    private function register_excerpt_style_controls() {
        $this->start_controls_section(
            'style_excerpt',
            [
                'label' => esc_html__('Excerpt', 'softech-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'layout_source' => 'elements',
                ],
            ]
        );

        $this->add_control(
            'excerpt_color',
            [
                'label' => esc_html__('Color', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sitsel-post-excerpt' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'excerpt_typography',
                'selector' => '{{WRAPPER}} .sitsel-post-excerpt',
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Register Author Style Controls
     */
    private function register_author_style_controls() {
        $this->start_controls_section(
            'style_author',
            [
                'label' => esc_html__('Author', 'softech-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'layout_source' => 'elements',
                ],
            ]
        );

        $this->add_control(
            'author_color',
            [
                'label' => esc_html__('Color', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sitsel-post-author' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'author_typography',
                'selector' => '{{WRAPPER}} .sitsel-post-author',
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Register Category Style Controls
     */
    private function register_category_style_controls() {
        $this->start_controls_section(
            'style_category',
            [
                'label' => esc_html__('Category', 'softech-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'layout_source' => 'elements',
                ],
            ]
        );

        $this->add_control(
            'category_color',
            [
                'label' => esc_html__('Color', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sitsel-post-category' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'category_typography',
                'selector' => '{{WRAPPER}} .sitsel-post-category',
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Register Comments Style Controls
     */
    private function register_comments_style_controls() {
        $this->start_controls_section(
            'style_comments',
            [
                'label' => esc_html__('Comments', 'softech-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'layout_source' => 'elements',
                ],
            ]
        );

        $this->add_control(
            'comments_color',
            [
                'label' => esc_html__('Color', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sitsel-post-comments' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'comments_typography',
                'selector' => '{{WRAPPER}} .sitsel-post-comments',
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Register Custom Field Style Controls
     */
    private function register_custom_field_style_controls() {
        $this->start_controls_section(
            'style_custom_field',
            [
                'label' => esc_html__('Custom Field', 'softech-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'layout_source' => 'elements',
                ],
            ]
        );

        $this->add_control(
            'custom_field_text_color',
            [
                'label' => esc_html__('Text Color', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sitsel-post-custom-field' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'custom_field_typography',
                'label' => esc_html__('Typography', 'softech-addons-for-elementor'),
                'selector' => '{{WRAPPER}} .sitsel-post-custom-field',
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Register Read More Style Controls
     */
    private function register_read_more_style_controls() {
        $this->start_controls_section(
            'style_readmore',
            [
                'label' => esc_html__('Read More', 'softech-addons-for-elementor'),
                'tab' => Controls_Manager::TAB_STYLE,
                'condition' => [
                    'layout_source' => 'elements',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Typography::get_type(),
            [
                'name' => 'readmore_typography',
                'selector' => '{{WRAPPER}} .sitsel-read-more a',
            ]
        );

        $this->start_controls_tabs('tabs_readmore_style');

        // Normal Tab
        $this->start_controls_tab(
            'tab_readmore_style_normal',
            [
                'label' => esc_html__('Normal', 'softech-addons-for-elementor'),
            ]
        );

        $this->add_control(
            'readmore_text_color',
            [
                'label' => esc_html__('Text Color', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sitsel-read-more a' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'readmore_bg_normal',
                'types' => ['classic', 'gradient'],
                'selector' => '{{WRAPPER}} .sitsel-read-more',
            ]
        );

        $this->add_control(
            'readmoretab_border_color',
            [
                'label' => esc_html__('Border Color', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sitsel-read-more' => 'border-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'readmore_box_shadow',
                'selector' => '{{WRAPPER}} .sitsel-read-more',
            ]
        );

        $this->end_controls_tab();

        // Hover Tab
        $this->start_controls_tab(
            'tab_readmore_style_hover',
            [
                'label' => esc_html__('Hover', 'softech-addons-for-elementor'),
            ]
        );

        $this->add_control(
            'readmore_text_hover_color',
            [
                'label' => esc_html__('Text Color', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sitsel-read-more a:hover' => 'color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Background::get_type(),
            [
                'name' => 'readmore_bg_hover',
                'types' => ['classic', 'gradient'],
                'selector' => '{{WRAPPER}} .sitsel-read-more:hover',
            ]
        );

        $this->add_control(
            'readmore_border_hover_color',
            [
                'label' => esc_html__('Border Color', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'selectors' => [
                    '{{WRAPPER}} .sitsel-read-more:hover' => 'border-color: {{VALUE}};',
                ],
            ]
        );

        $this->add_group_control(
            Group_Control_Box_Shadow::get_type(),
            [
                'name' => 'readmore_box_shadow_hover',
                'selector' => '{{WRAPPER}} .sitsel-read-more:hover',
            ]
        );

        $this->add_control(
            'readmore_transition_duration',
            [
                'label' => esc_html__('Transition Duration', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 0,
                        'max' => 3,
                        'step' => 0.1,
                    ],
                ],
                'selectors' => [
                    '{{WRAPPER}} .sitsel-read-more' => 'transition-duration: {{SIZE}}s;',
                ],
            ]
        );

        $this->end_controls_tab();
        $this->end_controls_tabs();

        $this->add_group_control(
            Group_Control_Border::get_type(),
            [
                'name' => 'readmore_border',
                'selector' => '{{WRAPPER}} .sitsel-read-more',
            ]
        );

        $this->add_responsive_control(
            'readmore_border_radius',
            [
                'label' => esc_html__('Border Radius', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em'],
                'selectors' => [
                    '{{WRAPPER}} .sitsel-read-more' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'readmore_margin',
            [
                'label' => esc_html__('Margin', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em'],
                'selectors' => [
                    '{{WRAPPER}} .sitsel-read-more' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'readmore_padding',
            [
                'label' => esc_html__('Padding', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::DIMENSIONS,
                'size_units' => ['px', '%', 'em'],
                'selectors' => [
                    '{{WRAPPER}} .sitsel-read-more' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'read_more_icon_color',
            [
                'label' => esc_html__('Icon Color', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::COLOR,
                'default' => '#c36',
                'selectors' => [
                    '{{WRAPPER}} .sitsel-read-more i, {{WRAPPER}} .sitsel-read-more svg' => 'color: {{VALUE}}; fill: {{VALUE}};',
                ],
            ]
        );

        $this->add_responsive_control(
            'read_more_icon_size',
            [
                'label' => esc_html__('Icon Size', 'softech-addons-for-elementor'),
                'type' => Controls_Manager::SLIDER,
                'range' => [
                    'px' => [
                        'min' => 8,
                        'max' => 100,
                    ],
                ],
                'default' => [
                    'size' => 16,
                    'unit' => 'px',
                ],
                'selectors' => [
                    '{{WRAPPER}} .sitsel-read-more i, {{WRAPPER}} .sitsel-read-more svg' => 'font-size: {{SIZE}}{{UNIT}}; width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
                ],
            ]
        );

        $this->end_controls_section();
    }

    /**
     * Register Pagination Style Controls
     */
  /**
 * Register Pagination Style Controls
 */
private function register_pagination_style_controls() {
    $this->start_controls_section(
        'style_pagination_section',
        [
            'label' => esc_html__('Pagination', 'softech-addons-for-elementor'),
            'tab'   => Controls_Manager::TAB_STYLE,
        ]
    );

    // ── Wrapper ───────────────────────────────────────────────────────────

    $this->add_control(
        'pagination_wrapper_heading',
        [
            'label'     => esc_html__('Wrapper', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::HEADING,
        ]
    );

    $this->add_responsive_control(
        'pagination_alignment',
        [
            'label'     => esc_html__('Alignment', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::CHOOSE,
            'options'   => [
                'flex-start' => [
                    'title' => esc_html__('Left', 'softech-addons-for-elementor'),
                    'icon'  => 'eicon-text-align-left',
                ],
                'center'     => [
                    'title' => esc_html__('Center', 'softech-addons-for-elementor'),
                    'icon'  => 'eicon-text-align-center',
                ],
                'flex-end'   => [
                    'title' => esc_html__('Right', 'softech-addons-for-elementor'),
                    'icon'  => 'eicon-text-align-right',
                ],
            ],
            'default'   => 'center',
            'selectors' => [
                '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers' => 'justify-content: {{VALUE}};',
                '{{WRAPPER}} .sitsel-pagination ul'            => 'justify-content: {{VALUE}};',
                '{{WRAPPER}} .sitsel-pagination'               => 'display: flex; justify-content: {{VALUE}};',
            ],
        ]
    );

    $this->add_responsive_control(
        'pagination_gap',
        [
            'label'      => esc_html__('Gap Between Items', 'softech-addons-for-elementor'),
            'type'       => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range'      => [
                'px' => [ 'min' => 0, 'max' => 50 ],
            ],
            'default'    => [ 'size' => 5, 'unit' => 'px' ],
            'selectors'  => [
                '{{WRAPPER}} .sitsel-pagination'            => 'gap: {{SIZE}}{{UNIT}};',
                '{{WRAPPER}} .sitsel-pagination ul'         => 'gap: {{SIZE}}{{UNIT}};',
                '{{WRAPPER}} .sitsel-pagination ul li'      => 'margin: 0;',
            ],
        ]
    );

    $this->add_responsive_control(
        'pagination_wrapper_margin',
        [
            'label'      => esc_html__('Wrapper Margin', 'softech-addons-for-elementor'),
            'type'       => Controls_Manager::DIMENSIONS,
            'size_units' => ['px', '%', 'em'],
            'default'    => [
                'top'    => 20,
                'right'  => 0,
                'bottom' => 0,
                'left'   => 0,
                'unit'   => 'px',
            ],
            'selectors'  => [
                '{{WRAPPER}} .sitsel-pagination' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
            ],
        ]
    );

    // ── Item: shared ──────────────────────────────────────────────────────

    $this->add_control(
        'pagination_item_heading',
        [
            'label'     => esc_html__('Items', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::HEADING,
            'separator' => 'before',
        ]
    );

    $this->add_group_control(
        Group_Control_Typography::get_type(),
        [
            'name'     => 'sitsel_pagination_typography',
            'label'    => esc_html__('Typography', 'softech-addons-for-elementor'),
            'selector' => '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers',
        ]
    );

    $this->add_responsive_control(
        'pagination_item_min_width',
        [
            'label'      => esc_html__('Min Width', 'softech-addons-for-elementor'),
            'type'       => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range'      => [
                'px' => [ 'min' => 20, 'max' => 100 ],
            ],
            'default'    => [ 'size' => 36, 'unit' => 'px' ],
            'selectors'  => [
                '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers' => 'min-width: {{SIZE}}{{UNIT}}; display: inline-flex; align-items: center; justify-content: center;',
            ],
        ]
    );

    $this->add_responsive_control(
        'pagination_item_height',
        [
            'label'      => esc_html__('Height', 'softech-addons-for-elementor'),
            'type'       => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range'      => [
                'px' => [ 'min' => 20, 'max' => 100 ],
            ],
            'default'    => [ 'size' => 36, 'unit' => 'px' ],
            'selectors'  => [
                '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers' => 'height: {{SIZE}}{{UNIT}};',
            ],
        ]
    );

    $this->add_responsive_control(
        'pagination_padding',
        [
            'label'      => esc_html__('Padding', 'softech-addons-for-elementor'),
            'type'       => Controls_Manager::DIMENSIONS,
            'size_units' => ['px', '%', 'em'],
            'default'    => [
                'top'    => 6,
                'right'  => 12,
                'bottom' => 6,
                'left'   => 12,
                'unit'   => 'px',
            ],
            'selectors'  => [
                '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
            ],
        ]
    );

    $this->add_responsive_control(
        'pagination_border_radius',
        [
            'label'      => esc_html__('Border Radius', 'softech-addons-for-elementor'),
            'type'       => Controls_Manager::DIMENSIONS,
            'size_units' => ['px', '%', 'em'],
            'default'    => [
                'top'    => 4,
                'right'  => 4,
                'bottom' => 4,
                'left'   => 4,
                'unit'   => 'px',
            ],
            'selectors'  => [
                '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
            ],
        ]
    );

    // ── Normal / Hover / Active tabs ──────────────────────────────────────

    $this->start_controls_tabs('pagination_tabs_style');

    // ── Normal ────────────────────────────────────────────────────────────
    $this->start_controls_tab(
        'pagination_tab_normal',
        [ 'label' => esc_html__('Normal', 'softech-addons-for-elementor') ]
    );

    $this->add_control(
        'sitsel_pagination_color',
        [
            'label'     => esc_html__('Text Color', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers:not(.current)' => 'color: {{VALUE}};',
            ],
        ]
    );

    $this->add_group_control(
        Group_Control_Background::get_type(),
        [
            'name'     => 'pagination_bg_normal',
            'types'    => ['classic', 'gradient'],
            'selector' => '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers:not(.current)',
        ]
    );

    $this->add_group_control(
        Group_Control_Border::get_type(),
        [
            'name'     => 'pagination_border_normal',
            'selector' => '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers:not(.current)',
        ]
    );

    $this->add_group_control(
        Group_Control_Box_Shadow::get_type(),
        [
            'name'     => 'pagination_box_shadow_normal',
            'selector' => '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers:not(.current)',
        ]
    );

    $this->end_controls_tab();

    // ── Hover ─────────────────────────────────────────────────────────────
    $this->start_controls_tab(
        'pagination_tab_hover',
        [ 'label' => esc_html__('Hover', 'softech-addons-for-elementor') ]
    );

    $this->add_control(
        'pagination_hover_color',
        [
            'label'     => esc_html__('Text Color', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers:not(.current):hover' => 'color: {{VALUE}};',
            ],
        ]
    );

    $this->add_group_control(
        Group_Control_Background::get_type(),
        [
            'name'     => 'pagination_bg_hover',
            'types'    => ['classic', 'gradient'],
            'selector' => '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers:not(.current):hover',
        ]
    );

    $this->add_control(
        'pagination_border_hover_color',
        [
            'label'     => esc_html__('Border Color', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers:not(.current):hover' => 'border-color: {{VALUE}};',
            ],
        ]
    );

    $this->add_group_control(
        Group_Control_Box_Shadow::get_type(),
        [
            'name'     => 'pagination_box_shadow_hover',
            'selector' => '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers:not(.current):hover',
        ]
    );

    $this->add_control(
        'pagination_hover_transition',
        [
            'label'     => esc_html__('Transition Duration (s)', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::SLIDER,
            'range'     => [
                'px' => [ 'min' => 0, 'max' => 3, 'step' => 0.1 ],
            ],
            'default'   => [ 'size' => 0.3 ],
            'selectors' => [
                '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers' => 'transition: all {{SIZE}}s ease;',
            ],
        ]
    );

    $this->end_controls_tab();

    // ── Active ────────────────────────────────────────────────────────────
   // ── Active ────────────────────────────────────────────────────────────
    $this->start_controls_tab(
        'pagination_tab_active',
        [ 'label' => esc_html__('Active', 'softech-addons-for-elementor') ]
    );

    $this->add_control(
        'sitsel_pagination_active_color',
        [
            'label'     => esc_html__('Text Color', 'softech-addons-for-elementor'),
            'type'      => Controls_Manager::COLOR,
            'selectors' => [
                '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers.current' => 'color: {{VALUE}};',
            ],
        ]
    );

    $this->add_group_control(
        Group_Control_Background::get_type(),
        [
            'name'     => 'pagination_bg_active',
            'types'    => ['classic', 'gradient'],
            'selector' => '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers.current',
        ]
    );

    // Full border group (replaces the old single border-color control)
    $this->add_group_control(
        Group_Control_Border::get_type(),
        [
            'name'     => 'pagination_border_active',
            'selector' => '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers.current',
        ]
    );

    $this->add_group_control(
        Group_Control_Box_Shadow::get_type(),
        [
            'name'     => 'pagination_box_shadow_active',
            'selector' => '{{WRAPPER}} .sitsel-pagination ul.page-numbers li .page-numbers.current',
        ]
    );

    $this->end_controls_tab();

    $this->end_controls_tabs();

    $this->end_controls_section();
}

private function register_load_more_style_controls() {
    $this->start_controls_section('style_load_more', [
        'label'     => esc_html__('Load More Button', 'softech-addons-for-elementor'),
        'tab'       => Controls_Manager::TAB_STYLE,
        'condition' => [ 'pagination' => 'load_more' ],
    ]);

    $this->add_group_control( Group_Control_Typography::get_type(), [
        'name'     => 'load_more_typography',
        'selector' => '{{WRAPPER}} .sitsel-load-more-btn',
    ]);

    $this->add_control('load_more_text_color', [
        'label'     => esc_html__('Text Color', 'softech-addons-for-elementor'),
        'type'      => Controls_Manager::COLOR,
        'selectors' => [ '{{WRAPPER}} .sitsel-load-more-btn' => 'color: {{VALUE}};' ],
    ]);

    $this->add_group_control( Group_Control_Background::get_type(), [
        'name'     => 'load_more_bg',
        'types'    => ['classic', 'gradient'],
        'selector' => '{{WRAPPER}} .sitsel-load-more-btn',
    ]);

    $this->add_group_control( Group_Control_Border::get_type(), [
        'name'     => 'load_more_border',
        'selector' => '{{WRAPPER}} .sitsel-load-more-btn',
    ]);

    $this->add_responsive_control('load_more_border_radius', [
        'label'      => esc_html__('Border Radius', 'softech-addons-for-elementor'),
        'type'       => Controls_Manager::DIMENSIONS,
        'size_units' => ['px', '%'],
        'selectors'  => [ '{{WRAPPER}} .sitsel-load-more-btn' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
    ]);

    $this->add_responsive_control('load_more_padding', [
        'label'      => esc_html__('Padding', 'softech-addons-for-elementor'),
        'type'       => Controls_Manager::DIMENSIONS,
        'size_units' => ['px', '%', 'em'],
        'selectors'  => [ '{{WRAPPER}} .sitsel-load-more-btn' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
    ]);

    $this->add_responsive_control('load_more_align', [
        'label'     => esc_html__('Alignment', 'softech-addons-for-elementor'),
        'type'      => Controls_Manager::CHOOSE,
        'options'   => [
            'flex-start' => [ 'title' => 'Left',   'icon' => 'eicon-text-align-left' ],
            'center'     => [ 'title' => 'Center', 'icon' => 'eicon-text-align-center' ],
            'flex-end'   => [ 'title' => 'Right',  'icon' => 'eicon-text-align-right' ],
        ],
        'default'   => 'center',
        'selectors' => [ '{{WRAPPER}} .sitsel-load-more-wrap' => 'display:flex; justify-content: {{VALUE}};' ],
    ]);

    $this->end_controls_section();
}

    /**
     * Render widget output
     */
    protected function render() {
        $settings = $this->get_settings_for_display();

        // Only render if layout is not slider
        if ($settings['layout'] !== 'grid') {
            echo '<div class="sitsel-pro-notice">' . esc_html__('Slider layout is a Pro feature.', 'softech-addons-for-elementor') . '</div>';
            return;
        }

        $template_type = $settings['choose_template_type'];
       

        $widget_id = $this->get_id();
        echo '<div class="sitsel-grid-wrapper" data-widget-id="' . esc_attr( $widget_id ) . '">';

        if ( in_array( $template_type, Softech_Post_Query_Manager::get_post_type_keys(), true ) ) {
    $this->render_post_type_grid( $settings, $template_type );
} elseif ( in_array( $template_type, Softech_Post_Query_Manager::get_taxonomy_type_keys(), true ) ) {
    $this->render_taxonomy_grid( $settings );
}

        echo '</div>';
    }

    /**
     * Render post type grid
     *
     * @param array  $settings  Widget settings
     * @param string $post_type Post type slug
     */
private function render_post_type_grid($settings, $post_type) {
    $paged      = Softech_Post_Query_Manager::get_paged();
    $query_args = Softech_Post_Query_Manager::build_query_args( $settings, $paged );
    $query_args['no_found_rows'] = ! in_array( $settings['pagination'] ?? '', [ 'yes', 'load_more' ], true );

    $query = new \WP_Query( $query_args );

    if ( ! $query->have_posts() ) {
        echo '<div class="sitsel-not-found">' . esc_html( $settings['not_found_text'] ) . '</div>';
        wp_reset_postdata();
        return;
    }

    $animation_class = $this->get_animation_class( $settings );

    // Build grid HTML as string — avoids ob_start conflict with Elementor's internal buffers
    $output = '<div class="sitsel-grid">';

    while ( $query->have_posts() ) {
        $query->the_post();

        // Capture each item into string
        ob_start();
        $this->render_post_item( $settings, $animation_class );
        $output .= ob_get_clean();
    }

    $output .= '</div>';

    // Capture pagination into string
    if ( $settings['pagination'] === 'yes' ) {
        ob_start();
        $this->render_pagination( $query->max_num_pages, $paged );
        $output .= ob_get_clean();
    } elseif ( $settings['pagination'] === 'load_more' ) {
        ob_start();
        $this->render_load_more_button( $settings, $query->max_num_pages, $paged );
        $output .= ob_get_clean();
    }

    wp_reset_postdata();

    // Single echo at the end — safe from buffer conflicts
    echo $output; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}

    /**
     * Render taxonomy grid
     *
     * @param array $settings Widget settings
     */
    private function render_taxonomy_grid($settings) {
        $taxonomy = sanitize_key($settings['source_post_taxonomy'] ?? '');

        if (empty($taxonomy)) {
            echo '<div class="sitsel-not-found">' . esc_html($settings['not_found_text']) . '</div>';
            return;
        }

        

        // Manual Pagination
       $result      = Softech_Post_Query_Manager::build_taxonomy_query( $settings, Softech_Post_Query_Manager::get_paged() );
        $paged       = $result['paged'];
        $total_pages = $result['total_pages'];
        $terms       = $result['terms'];
        
        if ( empty( $terms ) ) {
            echo '<div class="sitsel-not-found">' . esc_html( $settings['not_found_text'] ) . '</div>';
            return;
        }
        // Build animation class
        $animation_class = $this->get_animation_class($settings);

        echo '<div class="sitsel-grid">';

        foreach ($terms as $term) {
            echo '<div class="sitsel-grid-item ' . esc_attr($term->slug) . '">';
            echo '<div class="sitsel-grid-item-inner">';

            if ($settings['layout_source'] === 'template' && !empty($settings['template_id'])) {
                $this->render_taxonomy_template($term, absint($settings['template_id']));
            } else {
                $this->render_taxonomy_elements($settings, $term, $animation_class);
            }

            echo '</div></div>';
        }

        echo '</div>';

        // Pagination
        if ($settings['pagination'] === 'yes') {
            $this->render_pagination($total_pages, $paged);
        }
    }

    /**
     * Get Animation Class
     */
    private function get_animation_class($settings) {
        if (empty($settings['sitsel_featured_image_overlay_animation']) || $settings['sitsel_featured_image_overlay_animation'] === 'none') {
            return '';
        }
        return 'sitsel-anim-' . esc_attr($settings['sitsel_featured_image_overlay_animation']);
    }

   

    /**
     * Render Post Item
     */
    private function render_post_item($settings, $animation_class) {
        echo '<div class="sitsel-grid-item">';
        echo '<div class="sitsel-grid-item-inner">';

        if ($settings['layout_source'] === 'template' && !empty($settings['template_id'])) {
            $this->render_template_content($settings['template_id']);
        } else {
            $this->render_element_content($settings, $animation_class);
        }

        echo '</div></div>';
    }

    /**
     * Render Template Content
     */
    private function render_template_content($template_id) {
        if (!class_exists('\Elementor\Plugin')) {
            return;
        }

        $instance = \Elementor\Plugin::instance();
        if (!$instance || !isset($instance->frontend)) {
            return;
        }

        echo '<div class="sitsel-template-content">';
        // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
        echo $instance->frontend->get_builder_content_for_display($template_id, true);
        echo '</div>';
    }

    /**
     * Render taxonomy template with term context
     *
     * @param object $term        Term object
     * @param int    $template_id Template ID
     */
   private function render_taxonomy_template($term, $template_id) {
    global $wp_query, $wp_the_query;

    // Save originals from BOTH query objects
    $original_queried_object      = $wp_the_query->queried_object;
    $original_queried_object_id   = $wp_the_query->queried_object_id;
    $original_is_tax              = $wp_the_query->is_tax;
    $original_is_archive          = $wp_the_query->is_archive;
    $original_is_category         = $wp_the_query->is_category;
    $original_is_tag              = $wp_the_query->is_tag;
    $original_is_singular         = $wp_the_query->is_singular;
    $original_is_page             = $wp_the_query->is_page;

    // Inject term context into BOTH (get_queried_object() reads $wp_the_query)
    foreach ( [ $wp_query, $wp_the_query ] as $q ) {
        $q->queried_object    = $term;
        $q->queried_object_id = $term->term_id;
        $q->is_tax            = ( $term->taxonomy !== 'category' && $term->taxonomy !== 'post_tag' );
        $q->is_archive        = true;
        $q->is_category       = ( $term->taxonomy === 'category' );
        $q->is_tag            = ( $term->taxonomy === 'post_tag' );
        $q->is_singular       = false;
        $q->is_page           = false;
    }

    $GLOBALS['sitsel_current_term'] = $term;

    $this->render_template_content($template_id);

    // Restore originals on BOTH
    foreach ( [ $wp_query, $wp_the_query ] as $q ) {
        $q->queried_object    = $original_queried_object;
        $q->queried_object_id = $original_queried_object_id;
        $q->is_tax            = $original_is_tax;
        $q->is_archive        = $original_is_archive;
        $q->is_category       = $original_is_category;
        $q->is_tag            = $original_is_tag;
        $q->is_singular       = $original_is_singular;
        $q->is_page           = $original_is_page;
    }

    unset( $GLOBALS['sitsel_current_term'] );
}

    /**
     * Render Element Content
     */
    private function render_element_content($settings, $animation_class) {
        // Render featured image if enabled
        if (!empty($settings['show_image']) && $settings['show_image'] === 'yes') {
            $this->render_featured_image($settings, $animation_class);
        }

        // Render post content wrapper
        echo '<div class="sitsel-post-content-wrapper">';

        if (!empty($settings['post_elements'])) {
            foreach ($settings['post_elements'] as $element) {
                $this->render_element($element);
            }
        }

        echo '</div>';
    }

    /**
     * Render taxonomy elements
     *
     * @param array  $settings        Widget settings
     * @param object $term            Term object
     * @param string $animation_class Animation class
     */
    private function render_taxonomy_elements($settings, $term, $animation_class) {
        // Term Image (if available)
        if (!empty($settings['show_image']) && $settings['show_image'] === 'yes') {
            $this->render_term_image($settings, $term, $animation_class);
        }

        // Term Content Wrapper
        echo '<div class="sitsel-post-content-wrapper">';

        if (!empty($settings['archive_elements'])) {
            foreach ($settings['archive_elements'] as $element) {
                $this->render_archive_element($element, $term);
            }
        } elseif (!empty($settings['post_elements'])) {
            foreach ($settings['post_elements'] as $element) {
                $this->render_taxonomy_element($element, $term);
            }
        }

        echo '</div>';
    }

    /**
     * Render Featured Image
     */
    private function render_featured_image($settings, $animation_class) {
        $img_url = '';

        if ($settings['image_source_type'] === 'featured') {
            $img_url = get_the_post_thumbnail_url(get_the_ID(), 'large');
        } elseif ($settings['image_source_type'] === 'acf_image' && !empty($settings['custom_image_field_key'])) {
            $img_url = $this->get_acf_image_url($settings['custom_image_field_key']);
        }

        if (!$img_url) {
            return;
        }

        echo '<div class="sitsel-post-featured-image">';
        echo '<div class="sitsel-image-overlay-wrap ' . esc_attr($animation_class) . '">';
        echo '<a href="' . esc_url(get_permalink()) . '">';
        echo '<img src="' . esc_url($img_url) . '" alt="' . esc_attr(get_the_title()) . '" loading="lazy" decoding="async"/>';
        echo '<div class="sitsel-image-overlay"></div>';
        echo '</a>';
        echo '</div>';
        echo '</div>';
    }

    /**
     * Render term image
     *
     * @param array  $settings        Widget settings
     * @param object $term            Term object
     * @param string $animation_class Animation class
     */
    private function render_term_image($settings, $term, $animation_class) {
        $img_url = '';

        if ($settings['image_source_type'] === 'acf_image' && !empty($settings['custom_image_field_key'])) {
            $field_value = function_exists('get_field') ? get_field($settings['custom_image_field_key'], $term) : '';

            if (is_array($field_value) && isset($field_value['url'])) {
                $img_url = $field_value['url'];
            } elseif (is_string($field_value)) {
                $img_url = $field_value;
            }
        }

        if ($img_url) {
            echo '<div class="sitsel-post-featured-image">';
            echo '<div class="sitsel-image-overlay-wrap ' . esc_attr($animation_class) . '">';
            echo '<a href="' . esc_url(get_term_link($term)) . '">';
            echo '<img src="' . esc_url($img_url) . '" alt="' . esc_attr($term->name) . '" loading="lazy" decoding="async"/>';
            echo '<div class="sitsel-image-overlay"></div>';
            echo '</a>';
            echo '</div>';
            echo '</div>';
        }
    }

    /**
     * Get ACF Image URL
     */
    private function get_acf_image_url($field_key) {
        if (!function_exists('get_field')) {
            return '';
        }

        $field_value = get_field($field_key, get_the_ID());

        if (is_array($field_value) && isset($field_value['url'])) {
            return $field_value['url'];
        } elseif (is_string($field_value)) {
            return $field_value;
        }

        return '';
    }

    /**
     * Render Individual Element
     */
    private function render_element($element) {
        $type = $element['element_type'] ?? '';

        switch ($type) {
            case 'title':
                $this->render_title($element);
                break;
            case 'date':
                echo '<div class="sitsel-post-date Date">' . esc_html(get_the_date()) . '</div>';
                break;
            case 'time':
                echo '<div class="sitsel-post-time Time">' . esc_html(get_the_time()) . '</div>';
                break;
            case 'author':
                echo '<div class="sitsel-post-author Author">' . esc_html(get_the_author()) . '</div>';
                break;
            case 'excerpt':
                echo '<div class="sitsel-post-excerpt Excerpt">' . esc_html(get_the_excerpt()) . '</div>';
                break;
            case 'content':
                echo '<div class="sitsel-post-content Content">' . wp_kses_post(get_the_content()) . '</div>';
                break;
            case 'category':
                $this->render_categories();
                break;
            case 'tag':
                $this->render_tags();
                break;
            case 'comments':
                echo '<div class="sitsel-post-comments Comments">' . esc_html(get_comments_number()) . ' ' . esc_html__('Comments', 'softech-addons-for-elementor') . '</div>';
                break;
            case 'read_more':
                $this->render_read_more($element);
                break;
            case 'featured_image':
                $this->render_inline_featured_image();
                break;
            case 'acf_image':
                $this->render_acf_image($element);
                break;
            case 'custom_field':
                $this->render_custom_field($element);
                break;
        }
    }

    /**
     * Render single taxonomy element
     *
     * @param array  $element Element settings
     * @param object $term    Term object
     */
    private function render_taxonomy_element($element, $term) {
        $type = $element['element_type'] ?? '';

        switch ($type) {
            case 'title':
                $this->render_term_title($element, $term);
                break;

            case 'content':
                echo '<div class="sitsel-post-content Content">' .
                     wp_kses_post(wp_trim_words($term->description, 15)) .
                     '</div>';
                break;

            case 'read_more':
                $this->render_term_read_more($element, $term);
                break;

            case 'custom_field':
                $this->render_term_custom_field($element, $term);
                break;
        }
    }

    /**
     * Render Title
     */
    private function render_title($element) {
        $tag = $element['html_tag'] ?? 'h3';
        echo '<' . esc_attr($tag) . ' class="sitsel-post-title Title">';
        echo '<a href="' . esc_url(get_permalink()) . '">' . esc_html(get_the_title()) . '</a>';
        echo '</' . esc_attr($tag) . '>';
    }

    /**
     * Render term title
     *
     * @param array  $element Element settings
     * @param object $term    Term object
     */
    private function render_term_title($element, $term) {
        $tag = isset($element['html_tag']) ? sanitize_key($element['html_tag']) : 'h3';
        $allowed_tags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'div', 'span', 'p'];

        if (!in_array($tag, $allowed_tags, true)) {
            $tag = 'h3';
        }

        printf(
            '<%1$s class="sitsel-post-title Title"><a href="%2$s">%3$s</a></%1$s>',
            esc_attr($tag),
            esc_url(get_term_link($term)),
            esc_html($term->name)
        );
    }

    /**
     * Render Categories
     */
    private function render_categories() {
        $categories = get_the_category();
        if (empty($categories)) {
            return;
        }

        echo '<div class="sitsel-post-category Category">';
        foreach ($categories as $cat) {
            echo '<span>' . esc_html($cat->name) . '</span> ';
        }
        echo '</div>';
    }

    /**
     * Render Tags
     */
    private function render_tags() {
        $tags = get_the_tags();
        if (empty($tags)) {
            return;
        }

        echo '<div class="sitsel-post-tags Tags">';
        foreach ($tags as $tag) {
            echo '<span>' . esc_html($tag->name) . '</span> ';
        }
        echo '</div>';
    }

    /**
     * Render Read More
     */
    private function render_read_more($element) {
        echo '<div class="sitsel-read-more">';
        echo '<a href="' . esc_url(get_permalink()) . '">';

        // Icon before text
        if (($element['read_more_icon_position'] ?? 'after') === 'before' && !empty($element['read_more_icon']['value'])) {
            \Elementor\Icons_Manager::render_icon($element['read_more_icon'], ['aria-hidden' => 'true']);
            echo ' ';
        }

        // Read more text
        if (!empty($element['read_more_text'])) {
            echo esc_html($element['read_more_text']);
        }

        // Icon after text
        if (($element['read_more_icon_position'] ?? 'after') === 'after' && !empty($element['read_more_icon']['value'])) {
            echo ' ';
            \Elementor\Icons_Manager::render_icon($element['read_more_icon'], ['aria-hidden' => 'true']);
        }

        echo '</a>';
        echo '</div>';
    }

    /**
     * Render term read more button
     *
     * @param array  $element Element settings
     * @param object $term    Term object
     */
    private function render_term_read_more($element, $term) {
        $text = isset($element['read_more_text']) ? sanitize_text_field($element['read_more_text']) : '';
        $icon_position = isset($element['read_more_icon_position']) ? $element['read_more_icon_position'] : 'after';

        echo '<div class="sitsel-read-more">';
        echo '<a href="' . esc_url(get_term_link($term)) . '">';

        if ($icon_position === 'before' && !empty($element['read_more_icon']['value'])) {
            \Elementor\Icons_Manager::render_icon($element['read_more_icon'], ['aria-hidden' => 'true']);
        }

        if (!empty($text)) {
            echo esc_html($text);
        }

        if ($icon_position === 'after' && !empty($element['read_more_icon']['value'])) {
            \Elementor\Icons_Manager::render_icon($element['read_more_icon'], ['aria-hidden' => 'true']);
        }

        echo '</a>';
        echo '</div>';
    }

    /**
     * Render Inline Featured Image
     */
    private function render_inline_featured_image() {
        $img_url = get_the_post_thumbnail_url(get_the_ID(), 'large');
        if (!$img_url) {
            return;
        }

        echo '<div class="sitsel-featured-image">';
        echo '<a href="' . esc_url(get_permalink()) . '">';
        echo '<img src="' . esc_url($img_url) . '" alt="' . esc_attr(get_the_title()) . '" loading="lazy" decoding="async" />';
        echo '</a>';
        echo '</div>';
    }

    /**
     * Render ACF Image
     */
    private function render_acf_image($element) {
        $acf_key = $element['custom_field_key_image'] ?? '';
        if (empty($acf_key)) {
            return;
        }

        $acf_img_url = $this->get_acf_image_url($acf_key);
        if (!$acf_img_url) {
            return;
        }

        echo '<div class="sitsel-acf-image">';
        echo '<a href="' . esc_url(get_permalink()) . '">';
        echo '<img src="' . esc_url($acf_img_url) . '" alt="' . esc_attr(get_the_title()) . '" loading="lazy" decoding="async"/>';
        echo '</a>';
        echo '</div>';
    }

    /**
     * Render Custom Field
     */
    private function render_custom_field($element) {
        $key = $element['custom_field_key'] ?? '';
        if (empty($key)) {
            return;
        }

        $value = get_post_meta(get_the_ID(), $key, true);
        if (empty($value)) {
            return;
        }

        echo '<div class="sitsel-post-custom-field Custom-Field">' . esc_html($value) . '</div>';
    }

    /**
     * Render archive element (for taxonomy archive repeater)
     *
     * @param array  $element Element settings
     * @param object $term    Term object
     */
    private function render_archive_element($element, $term) {
        $type = $element['element_type'] ?? '';

        switch ($type) {
            case 'archive_title':
                $this->render_term_title($element, $term);
                break;

            case 'archive_description':
                echo '<div class="sitsel-archive-description">' . wp_kses_post(wp_trim_words($term->description, 25)) . '</div>';
                break;

            case 'archive_posts':
                $posts = get_posts([
                    'post_type' => 'any',
                    'tax_query' => [
                        [
                            'taxonomy' => $term->taxonomy,
                            'field' => 'term_id',
                            'terms' => $term->term_id,
                        ],
                    ],
                    'posts_per_page' => 3,
                    'post_status' => 'publish',
                ]);

                if (!empty($posts)) {
                    echo '<div class="sitsel-archive-posts">';
                    foreach ($posts as $p) {
                        echo '<div class="sitsel-archive-post"><a href="' . esc_url(get_permalink($p)) . '">' . esc_html(get_the_title($p)) . '</a></div>';
                    }
                    echo '</div>';
                }
                break;

            case 'custom_field':
                $this->render_term_custom_field($element, $term);
                break;
        }
    }

    /**
     * Render term custom field
     *
     * @param array  $element Element settings
     * @param object $term    Term object
     */
    private function render_term_custom_field($element, $term) {
        if (!function_exists('get_field')) {
            return;
        }

        $key = $element['custom_field_key'] ?? '';

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

        $value = get_field($key, $term);

        if ($value) {
            echo '<div class="sitsel-custom-field">' . esc_html($value) . '</div>';
        }
    }

    /**
     * Render Pagination
     */
    private function render_pagination($total_pages, $paged) {
        if ($total_pages <= 1) {
            return;
        }

        $big = 999999999; // need an unlikely integer
        $base = str_replace($big, '%#%', esc_url(get_pagenum_link($big)));

        $pagination = paginate_links([
            'base' => $base,
            'format' => (get_option('permalink_structure') ? 'page/%#%/' : '?paged=%#%'),
            'current' => max(1, intval($paged)),
            'total' => intval($total_pages),
            'type' => 'list',
            'prev_text' => esc_html__('« Prev', 'softech-addons-for-elementor'),
            'next_text' => esc_html__('Next »', 'softech-addons-for-elementor'),
        ]);

        if ($pagination) {
            echo '<div class="sitsel-pagination">' . wp_kses_post($pagination) . '</div>';
        }
    }
    
    private function render_load_more_button($settings, $total_pages, $paged) {
    if ($total_pages <= 1) return;

    $label = !empty($settings['load_more_text']) ? $settings['load_more_text'] : __('Load More', 'softech-addons-for-elementor');

    echo '<div class="sitsel-load-more-wrap">';
    echo '<button class="sitsel-load-more-btn" data-page="' . esc_attr($paged + 1) . '" data-max-pages="' . esc_attr($total_pages) . '"' . ($paged >= $total_pages ? ' disabled' : '') . '>' . esc_html($label) . '</button>';
    echo '</div>';
}
}