<?php
/**
 * GSAP Animation Manager
 *
 * @package Softech_Addons_for_Elementor
 */
if ( ! defined( 'ABSPATH' ) ) exit;
class SITS_GSAP_Manager {

    /* ── Global option key ── */
    private static $opt = 'sits_elementor_addons_options';

    public static function init() {
        add_action( 'elementor/element/after_section_end', [ __CLASS__, 'inject_gsap_controls' ], 10, 3 );
        add_action( 'elementor/frontend/before_render',    [ __CLASS__, 'add_gsap_wrapper_attributes' ] );
        add_action( 'elementor/preview/enqueue_scripts',   [ __CLASS__, 'enqueue_preview_assets' ] );
        add_action( 'wp_head',                             [ __CLASS__, 'output_global_split_inline_css' ] );
        // In init(), ADD this line:
add_action( 'wp_footer', [ __CLASS__, 'maybe_enqueue_gsap_fallback' ], 1 );
        add_action( 'wp_enqueue_scripts',                  [ __CLASS__, 'register_gsap_scripts' ] );
    }
    public static function register_gsap_scripts() {
        $v        = self::$gsap_v;
        $base_url = SITS_ELEMENTOR_ADDONS_PLUGIN_URL;
    
        // Register only — don't enqueue yet. maybe_enqueue_gsap() will enqueue on demand.
        wp_register_script( 'sits-gsap',    $base_url . 'assets/js/gsap.min.js',         [], $v, true );
        wp_register_script( 'sits-gsap-st', $base_url . 'assets/js/ScrollTrigger.min.js', [ 'sits-gsap' ], $v, true );
        wp_register_script( 'sits-gsap-handler',       $base_url . 'assets/js/gsap-handler.js',       [ 'jquery','sits-gsap','sits-gsap-st' ], SITS_ELEMENTOR_ADDONS_VERSION, true );
        wp_register_script( 'sits-gsap-stack-handler', $base_url . 'assets/js/gsap-stack-handler.js', [ 'jquery','sits-gsap','sits-gsap-st' ], SITS_ELEMENTOR_ADDONS_VERSION, true );
    }

 public static function maybe_enqueue_gsap_fallback() {
    if ( self::$gsap_enqueued ) return;
    // If any GSAP-enabled element was rendered, enqueue now
    global $wp_query;
    if ( did_action( 'elementor/frontend/before_render' ) ) {
        self::maybe_enqueue_gsap();
    }
}
    public static function output_global_split_inline_css() {
        $options = get_option( self::$opt, [] );
        // if ( empty( $options['gsap_global_split_enable'] ) || 'yes' !== $options['gsap_global_split_enable'] ) {
        //     return; 
        // }
        
        $class   = self::get_global_class( $options );

        $tags  = [ 'h1','h2','h3','h4','h5','h6','p' ];
        $rules = implode( ',', array_map( function ( $tag ) use ( $class ) {
            return $class . ' ' . $tag;
        }, $tags ) );
// ADD this after the existing $rules echo:
echo '<style id="sits-split-hide">[data-gsap-split-standalone]{visibility:hidden}</style>' . "\n";
       if ( ! empty( $rules ) ) {
          echo '<style id="sits-global-split-hide">' . esc_html( wp_strip_all_tags( $rules ) ) . '{visibility:hidden}</style>' . "\n";
        }
    }

    private static $gsap_enqueued = false;
private static $gsap_v        = '3.12.5';


public static function maybe_enqueue_gsap() {
    if ( self::$gsap_enqueued ) return;
    self::$gsap_enqueued = true;

    $v       = self::$gsap_v;
    $base_url = SITS_ELEMENTOR_ADDONS_PLUGIN_URL;

    wp_enqueue_script( 'sits-gsap',
        $base_url . 'assets/js/gsap.min.js',
        [],
        $v,
        true
    );

    wp_enqueue_script( 'sits-gsap-st',
        $base_url . 'assets/js/ScrollTrigger.min.js',
        [ 'sits-gsap' ],
        $v,
        true
    );

    wp_enqueue_script( 'sits-gsap-handler',
        SITS_ELEMENTOR_ADDONS_PLUGIN_URL . 'assets/js/gsap-handler.js',
        [ 'jquery', 'sits-gsap', 'sits-gsap-st' ],
        SITS_ELEMENTOR_ADDONS_VERSION,
        true
    );

    wp_enqueue_script( 'sits-gsap-stack-handler',
    $base_url . 'assets/js/gsap-stack-handler.js',
    [ 'jquery', 'sits-gsap', 'sits-gsap-st' ],
    SITS_ELEMENTOR_ADDONS_VERSION,
    true
);

    $options = get_option( self::$opt, [] );
    wp_localize_script( 'sits-gsap-handler', 'sitsGsapSettings', [
        'globalSplitEnable'      => $options['gsap_global_split_enable']       ?? '',
        'globalSplitClass'       => self::get_global_class( $options ),
        'globalSplitType'        => $options['gsap_global_split_type']         ?? 'chars',
        'globalSplitAnimation' => $options['gsap_global_split_animation'] ?? 'slideUp',
        'globalSplitStagger'     => $options['gsap_global_split_stagger']      ?? 0.08,
        'globalSplitLineStagger' => $options['gsap_global_split_line_stagger'] ?? 0.08,
        'globalSplitScrollStart' => $options['gsap_global_split_scroll_start'] ?? 100,
    ] );
}

    /* =====================================================================
     * ENQUEUE SCRIPTS (editor preview iframe)
     * ===================================================================== */
    public static function enqueue_preview_assets() {
    self::$gsap_enqueued = false; // reset so preview always loads fresh
    self::maybe_enqueue_gsap();
}

    /* =====================================================================
     * INJECT GSAP PANEL CONTROLS (Style tab of every element)
     * ===================================================================== */
    public static function inject_gsap_controls( $element, $section_id, $args ) {
        if ( ! $element instanceof \Elementor\Element_Base ) return;

        static $done = [];
        $name = $element->get_name();
        if ( isset( $done[ $name ] ) ) return;

        $tab = '';
        if ( is_array( $args ) && isset( $args['tab'] ) )        $tab = $args['tab'];
        elseif ( is_object( $args ) && isset( $args->tab ) )     $tab = $args->tab;
        if ( \Elementor\Controls_Manager::TAB_STYLE !== $tab ) return;

        $done[ $name ]  = true;
        $target_tab     = self::get_target_tab( $element );

        /* ── Entrance Animation ── */
        $no_entrance_widgets = [
    'sitsel_dual_color_heading',
    // add more widget names here if needed
];

if ( ! in_array( $name, $no_entrance_widgets, true ) ) {
        self::open_section( $element, 'sits_gsap_section',
            'eicon-animation', __( 'GSAP Animation', 'softech-addons-for-elementor' ), $target_tab );

        $element->add_control( 'sits_gsap_enable', [
            'label' => __( 'Enable GSAP Animation', 'softech-addons-for-elementor' ),
            'type'  => \Elementor\Controls_Manager::SWITCHER,
            'return_value' => 'yes', 'default' => '',
        ] );
        $element->add_control( 'sits_gsap_animation', [
            'label'   => __( 'Animation Type', 'softech-addons-for-elementor' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'fadeIn',
            'options' => [
                'fadeIn'       => 'Fade In',       'fadeInUp'    => 'Fade In Up',
                'fadeInDown'   => 'Fade In Down',  'fadeInLeft'  => 'Fade In Left',
                'fadeInRight'  => 'Fade In Right', 'zoomIn'      => 'Zoom In',
                'zoomInUp'     => 'Zoom In Up',    'slideInUp'   => 'Slide In Up',
                'slideInLeft'  => 'Slide In Left', 'slideInRight'=> 'Slide In Right',
                'rotateIn'     => 'Rotate In',     'bounceIn'    => 'Bounce In',
                'flipInX'      => 'Flip In X',     'flipInY'     => 'Flip In Y',
            ],
            'condition' => [ 'sits_gsap_enable' => 'yes' ],
        ] );
        $element->add_control( 'sits_gsap_duration', [
            'label'     => __( 'Duration (s)', 'softech-addons-for-elementor' ),
            'type'      => \Elementor\Controls_Manager::SLIDER,
            'range'     => [ 'px' => [ 'min' => 0.1, 'max' => 3, 'step' => 0.1 ] ],
            'default'   => [ 'size' => 0.8 ],
            'condition' => [ 'sits_gsap_enable' => 'yes' ],
        ] );
        $element->add_control( 'sits_gsap_delay', [
            'label'     => __( 'Delay (s)', 'softech-addons-for-elementor' ),
            'type'      => \Elementor\Controls_Manager::SLIDER,
            'range'     => [ 'px' => [ 'min' => 0, 'max' => 3, 'step' => 0.1 ] ],
            'default'   => [ 'size' => 0 ],
            'condition' => [ 'sits_gsap_enable' => 'yes' ],
        ] );
        $element->add_control( 'sits_gsap_ease', [
            'label'   => __( 'Easing', 'softech-addons-for-elementor' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'none',
            'options' => [
                'none'               => 'None (Linear)', 'power1.out' => 'Power 1 Out',
                'power2.out'         => 'Power 2 Out',   'power3.out' => 'Power 3 Out',
                'power4.out'         => 'Power 4 Out',   'back.out(1.7)' => 'Back Out',
                'elastic.out(1,0.3)' => 'Elastic Out',   'bounce.out' => 'Bounce Out',
                'circ.out'           => 'Circ Out',      'expo.out'   => 'Expo Out',
            ],
            'condition' => [ 'sits_gsap_enable' => 'yes' ],
        ] );
        $element->add_control( 'sits_gsap_trigger', [
            'label'   => __( 'Trigger', 'softech-addons-for-elementor' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'scroll',
            'options' => [ 'scroll' => 'On Scroll (ScrollTrigger)', 'load' => 'On Page Load' ],
            'condition' => [ 'sits_gsap_enable' => 'yes' ],
        ] );
        $element->add_control( 'sits_gsap_scroll_start', [
            'label'     => __( 'Trigger Point (%)', 'softech-addons-for-elementor' ),
            'type'      => \Elementor\Controls_Manager::SLIDER,
            'range'     => [ 'px' => [ 'min' => 0, 'max' => 100, 'step' => 5 ] ],
            'default'   => [ 'size' => 20 ],
            'condition' => [ 'sits_gsap_enable' => 'yes', 'sits_gsap_trigger' => 'scroll' ],
        ] );
        $element->end_controls_section();
}
        /* ── Split Text ── */
        self::open_section( $element, 'sits_gsap_splittext_section',
            'eicon-t-letter', __( 'GSAP Split Text', 'softech-addons-for-elementor' ), $target_tab );

        $element->add_control( 'sits_gsap_split_global_enable', [
                'label'        => __( 'Global Split Text', 'softech-addons-for-elementor' ),
                'type'         => \Elementor\Controls_Manager::SWITCHER,
                'return_value' => 'yes',
                'default'      => '',
            ] );
            $element->add_control( 'sits_gsap_split_global_link', [
                'type'      => \Elementor\Controls_Manager::RAW_HTML,
                'raw'       => sprintf(
                    '<a href="%s" target="_blank" class="elementor-button elementor-button-success" style="display:inline-flex;align-items:center;gap:5px"><i class="eicon-external-link-square"></i> %s</a>',
                    esc_url( admin_url( 'admin.php?page=softech-addons-for-elementor&tab=settings' ) ),
                    esc_html__( 'Edit Global Settings', 'softech-addons-for-elementor' )
                ),
                'condition' => [ 'sits_gsap_split_global_enable' => 'yes' ],
            ] );
            $element->add_control( 'sits_gsap_split_custom_enable', [
                'label'        => __( 'Custom Split Text', 'softech-addons-for-elementor' ),
                'type'         => \Elementor\Controls_Manager::SWITCHER,
                'return_value' => 'yes',
                'default'      => '',
                'condition'    => [ 'sits_gsap_split_global_enable' => '' ], // hidden when global is on
            ] );
        $element->add_control( 'sits_gsap_split_standalone_selector', [
            'label'       => __( 'Text Selector(s)', 'softech-addons-for-elementor' ),
            'type'        => \Elementor\Controls_Manager::TEXTAREA,
            'rows'        => 3,
            'placeholder' => '.elementor-heading-title, .my-class, h2',
            'description' => __( 'CSS selectors to split. Separate multiple with commas. e.g. <code>.elementor-heading-title, .my-title, h2</code>', 'softech-addons-for-elementor' ),
            'condition'   => [ 'sits_gsap_split_custom_enable' => 'yes', 'sits_gsap_split_global_enable' => '' ],
        ] );
        $element->add_control( 'sits_gsap_split_standalone_type', [
            'label'     => __( 'Split Type', 'softech-addons-for-elementor' ),
            'type'      => \Elementor\Controls_Manager::SELECT,
            'default'   => 'chars',
            'options'   => [
                'chars' => __( 'Characters', 'softech-addons-for-elementor' ),
                'words' => __( 'Words',      'softech-addons-for-elementor' ),
            ],
            'condition' => [ 'sits_gsap_split_custom_enable' => 'yes', 'sits_gsap_split_global_enable' => '' ],
        ] );
        
        $element->add_control( 'sits_gsap_split_animation', [
            'label'     => __( 'Split Animation Style', 'softech-addons-for-elementor' ),
            'type'      => \Elementor\Controls_Manager::SELECT,
            'default'   => 'slideUp',
            'options'   => [
                'slideUp'    => __( 'Slide Up (default)',    'softech-addons-for-elementor' ),
                'fadeIn'     => __( 'Fade In',               'softech-addons-for-elementor' ),
                'rotateIn'   => __( 'Rotate In (3D)',        'softech-addons-for-elementor' ),
                'scaleIn'    => __( 'Scale In',              'softech-addons-for-elementor' ),
                'blurIn'     => __( 'Blur In',               'softech-addons-for-elementor' ),
                'slideDown'  => __( 'Slide Down',            'softech-addons-for-elementor' ),
                'flipIn'     => __( 'Flip In (X-axis)',      'softech-addons-for-elementor' ),
                'waveIn'     => __( 'Wave (chars only)',     'softech-addons-for-elementor' ),
            ],
            'condition' => [ 'sits_gsap_split_custom_enable' => 'yes', 'sits_gsap_split_global_enable' => '' ],
        ] );
        
        $element->add_control( 'sits_gsap_split_standalone_stagger', [
            'label'     => __( 'Char Stagger (s)', 'softech-addons-for-elementor' ),
            'type'      => \Elementor\Controls_Manager::SLIDER,
            'range'     => [ 'px' => [ 'min' => 0.001, 'max' => 0.05, 'step' => 0.001 ] ],
            'default'   => [ 'size' => 0.08 ],
            'condition' => [ 'sits_gsap_split_custom_enable' => 'yes', 'sits_gsap_split_global_enable' => '' ],
        ] );
        $element->add_control( 'sits_gsap_split_standalone_line_stagger', [
            'label'     => __( 'Line Stagger (s)', 'softech-addons-for-elementor' ),
            'type'      => \Elementor\Controls_Manager::SLIDER,
            'range'     => [ 'px' => [ 'min' => 0, 'max' => 0.5, 'step' => 0.01 ] ],
            'default'   => [ 'size' => 0.08 ],
            'condition' => [ 'sits_gsap_split_custom_enable' => 'yes', 'sits_gsap_split_global_enable' => '' ],
        ] );
        $element->add_control( 'sits_gsap_split_trigger', [
            'label'   => __( 'Trigger', 'softech-addons-for-elementor' ),
            'type'    => \Elementor\Controls_Manager::SELECT,
            'default' => 'scroll',
            'options' => [
                'scroll' => __( 'On Scroll',     'softech-addons-for-elementor' ),
                'load'   => __( 'On Page Load',  'softech-addons-for-elementor' ),
                'hover'  => __( 'On Hover',      'softech-addons-for-elementor' ),
            ],
            'condition' => [ 'sits_gsap_split_custom_enable' => 'yes', 'sits_gsap_split_global_enable' => '' ],
        ] );
        
        $element->add_control( 'sits_gsap_split_scroll_start', [
            'label'     => __( 'Trigger Point from Top (%)', 'softech-addons-for-elementor' ),
            'type'      => \Elementor\Controls_Manager::SLIDER,
            'range'     => [ 'px' => [ 'min' => 0, 'max' => 100, 'step' => 5 ] ],
            'default'   => [ 'size' => 100 ],
            'description' => __( '100% = animates as soon as element enters viewport bottom', 'softech-addons-for-elementor' ),
            'condition' => [ 'sits_gsap_split_custom_enable' => 'yes',  'sits_gsap_split_trigger' => 'scroll' ],
        ] );
        
        $element->end_controls_section();

        /* ── Stacking Cards ── */
        self::open_section( $element, 'sits_gsap_stack_section',
            'eicon-scroll', __( 'GSAP Stacking Cards', 'softech-addons-for-elementor' ), $target_tab );

        $element->add_control( 'sits_gsap_stack_enable', [
            'label'        => __( 'Enable Stacking Cards', 'softech-addons-for-elementor' ),
            'type'         => \Elementor\Controls_Manager::SWITCHER,
            'return_value' => 'yes', 'default' => '',
            'description'  => __( 'Sticky stacking scroll effect on child items.', 'softech-addons-for-elementor' ),
        ] );
       


        $element->add_control( 'sits_gsap_stack_pin_start', [
            'label'       => __( 'Pin Start', 'softech-addons-for-elementor' ),
            'type'        => \Elementor\Controls_Manager::TEXT,
            'default'     => 'top top',
            'placeholder' => 'top top',
            'description' => __( 'When each card pins to viewport. e.g. <code>top top</code> or <code>top top+=80</code> (adds offset for fixed header)', 'softech-addons-for-elementor' ),
            'condition'   => [ 'sits_gsap_stack_enable' => 'yes' ],
        ] );
        
        $element->add_control( 'sits_gsap_stack_item_selector', [
            'label'       => __( 'Item Selector', 'softech-addons-for-elementor' ),
            'type'        => \Elementor\Controls_Manager::TEXT,
            'default'     => '',
            'placeholder' => '.my-item',
            'description' => __( 'CSS selector for child elements to stack.', 'softech-addons-for-elementor' ),
            'condition'   => [ 'sits_gsap_stack_enable' => 'yes' ],
        ] );
        $element->add_control(
            'sits_gsap_stack_ease',
            [
                'label' => __( 'Transition Ease', 'softech-addons-for-elementor' ),
                'type' => \Elementor\Controls_Manager::SELECT,
                'default' => 'none',
                'options' => [
                    'none'        => 'None (Linear)',
                    'power1.out'  => 'Power 1 Out',
                    'power1.in'   => 'Power 1 In',
                    'power1.inOut'=> 'Power 1 InOut',
                    'power2.out'  => 'Power 2 Out',
                    'power3.out'  => 'Power 3 Out',
                    'power4.out'  => 'Power 4 Out',
                    'expo.out'    => 'Expo Out',
                    'circ.out'    => 'Circ Out',
                    'back.out(1.7)' => 'Back Out',
                    'elastic.out(1, 0.3)' => 'Elastic Out',
                ],
                'condition' => [
                    'sits_gsap_stack_enable' => 'yes',
                ],
            ]
        );
        $element->add_control( 'sits_gsap_stack_markers', [
            'label'        => __( 'Show Markers (Debug)', 'softech-addons-for-elementor' ),
            'type'         => \Elementor\Controls_Manager::SWITCHER,
            'return_value' => 'yes', 'default' => '',
            'description'  => __( 'Show ScrollTrigger debug markers. Disable before going live.', 'softech-addons-for-elementor' ),
            'condition'    => [ 'sits_gsap_stack_enable' => 'yes' ],
        ] );
        $element->add_control( 'sits_gsap_stack_mobile_disable', [
            'label'        => __( 'Disable on Mobile', 'softech-addons-for-elementor' ),
            'type'         => \Elementor\Controls_Manager::SWITCHER,
            'return_value' => 'yes',
            'default'      => 'yes',
            'description'  => __( 'Turn off sticky stacking below the breakpoint.', 'softech-addons-for-elementor' ),
            'condition'    => [ 'sits_gsap_stack_enable' => 'yes' ],
        ] );
        $element->add_control( 'sits_gsap_stack_mobile_bp', [
            'label'       => __( 'Mobile Breakpoint (px)', 'softech-addons-for-elementor' ),
            'type'        => \Elementor\Controls_Manager::NUMBER,
            'default'     => 768,
            'min'         => 320, 'max' => 1200, 'step' => 1,
            'condition'   => [ 'sits_gsap_stack_enable' => 'yes', 'sits_gsap_stack_mobile_disable' => 'yes' ],
        ] );

        // REPLACE the scrub_anim block and all 3 translate controls with:

        $element->add_control( 'sits_gsap_stack_scrub_anim', [
            'label'        => __( 'Scale Animation', 'softech-addons-for-elementor' ),
            'type'         => \Elementor\Controls_Manager::SWITCHER,
            'return_value' => 'yes', 'default' => '',
            'description'  => __( 'Scale down previous cards as next card scrolls over them.', 'softech-addons-for-elementor' ),
            'condition'    => [ 'sits_gsap_stack_enable' => 'yes' ],
            'separator'    => 'before',
        ] );
        $element->add_control( 'sits_gsap_stack_scrub_scale', [
            'label'     => __( 'Scale To', 'softech-addons-for-elementor' ),
            'type'      => \Elementor\Controls_Manager::SLIDER,
            'range'     => [ 'px' => [ 'min' => 0.5, 'max' => 0.99, 'step' => 0.01 ] ],
            'default'   => [ 'size' => 0.85 ],
            'condition' => [ 'sits_gsap_stack_enable' => 'yes', 'sits_gsap_stack_scrub_anim' => 'yes' ],
        ] );
        
        // NEW: Translate 3D toggle (separate from scale)
        $element->add_control( 'sits_gsap_stack_translate_enable', [
            'label'        => __( 'Translate 3D', 'softech-addons-for-elementor' ),
            'type'         => \Elementor\Controls_Manager::SWITCHER,
            'return_value' => 'yes', 'default' => '',
            'description'  => __( 'Shift previous cards in 3D space as next card scrolls over them.', 'softech-addons-for-elementor' ),
            'condition'    => [ 'sits_gsap_stack_enable' => 'yes' ],
        ] );
        $element->add_control( 'sits_gsap_stack_translate_x', [
            'label'      => __( 'Translate X', 'softech-addons-for-elementor' ),
            'type'       => \Elementor\Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range'      => [ 'px' => ['min' => -500, 'max' => 500] ],
            'default'    => ['size' => 12],
            'condition'  => [ 'sits_gsap_stack_enable' => 'yes', 'sits_gsap_stack_translate_enable' => 'yes' ],
        ] );
        $element->add_control( 'sits_gsap_stack_translate_y', [
            'label'      => __( 'Translate Y', 'softech-addons-for-elementor' ),
            'type'       => \Elementor\Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range'      => [ 'px' => ['min' => -500, 'max' => 500] ],
            'default'    => ['size' => -41],
            'condition'  => [ 'sits_gsap_stack_enable' => 'yes', 'sits_gsap_stack_translate_enable' => 'yes' ],
        ] );
        $element->add_control( 'sits_gsap_stack_translate_z', [
            'label'      => __( 'Translate Z', 'softech-addons-for-elementor' ),
            'type'       => \Elementor\Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range'      => [ 'px' => ['min' => -500, 'max' => 500] ],
            'default'    => ['size' => 0],
            'condition'  => [ 'sits_gsap_stack_enable' => 'yes', 'sits_gsap_stack_translate_enable' => 'yes' ],
        ] );
        $element->add_control( 'sits_gsap_stack_push_brightness_enable', [
            'label'        => __( 'Opacity Enable', 'softech-addons-for-elementor' ),
            'type'         => \Elementor\Controls_Manager::SWITCHER,
            'return_value' => 'yes', 'default' => '',
            'description'  => __( 'Dim previous cards as next card scrolls over them.', 'softech-addons-for-elementor' ),
            'condition'    => [ 'sits_gsap_stack_enable' => 'yes' ],
        ] );
        $element->add_control( 'sits_gsap_stack_push_brightness', [
            'label'     => __( 'Opacity', 'softech-addons-for-elementor' ),
            'type'      => \Elementor\Controls_Manager::SLIDER,
            'range'     => [ 'px' => [ 'min' => 0.1, 'max' => 1, 'step' => 0.05 ] ],
            'default'   => [ 'size' => 0.75 ],
            'condition' => [ 'sits_gsap_stack_enable' => 'yes', 'sits_gsap_stack_push_brightness_enable' => 'yes' ],
        ] );
        $element->add_control( 'sits_gsap_stack_scrub_bg', [
            'label'       => __( 'Background Color Tint', 'softech-addons-for-elementor' ),
            'type'        => \Elementor\Controls_Manager::COLOR,
            'default'     => '',
            'description' => __( 'Optional. Card background tints to this color as it gets stacked under.', 'softech-addons-for-elementor' ),
            'condition'   => [ 'sits_gsap_stack_enable' => 'yes' ],
        ] );
      
        $element->end_controls_section();
    }

    /* =====================================================================
     * ADD DATA-ATTRIBUTES TO WIDGET WRAPPER (frontend render)
     * ===================================================================== */
    public static function add_gsap_wrapper_attributes( $element ) {
        if ( ! $element instanceof \Elementor\Element_Base ||
             ! method_exists( $element, 'add_render_attribute' ) ) return;

        $s = $element->get_settings_for_display();

        $has_gsap = ( ! empty( $s['sits_gsap_enable'] )                  && 'yes' === $s['sits_gsap_enable'] )
         || ( ! empty( $s['sits_gsap_stack_enable'] )             && 'yes' === $s['sits_gsap_stack_enable'] )
         || ( ! empty( $s['sits_gsap_split_global_enable'] )      && 'yes' === $s['sits_gsap_split_global_enable'] )
         || ( ! empty( $s['sits_gsap_split_custom_enable'] )      && 'yes' === $s['sits_gsap_split_custom_enable'] );

if ( $has_gsap ) self::maybe_enqueue_gsap();

        /* ── Entrance animation ── */
        $gsap_on  = ! empty( $s['sits_gsap_enable'] ) && 'yes' === $s['sits_gsap_enable'];
        $stack_on = ! empty( $s['sits_gsap_stack_enable'] ) && 'yes' === $s['sits_gsap_stack_enable'];

        if ( $gsap_on && ! $stack_on ) {
            $element->add_render_attribute( '_wrapper', 'class', 'sits-gsap-hidden' );
            $element->add_render_attribute( '_wrapper', [
                'data-gsap'              => 'true',
                'data-gsap-animation'    => esc_attr( $s['sits_gsap_animation']           ?? 'fadeIn' ),
                'data-gsap-duration'     => esc_attr( $s['sits_gsap_duration']['size']    ?? 0.8 ),
                'data-gsap-delay'        => esc_attr( $s['sits_gsap_delay']['size']       ?? 0 ),
                'data-gsap-ease'         => esc_attr( $s['sits_gsap_ease']                ?? 'power2.out' ),
                'data-gsap-trigger'      => esc_attr( $s['sits_gsap_trigger']             ?? 'scroll' ),
                'data-gsap-scroll-start' => esc_attr( $s['sits_gsap_scroll_start']['size'] ?? 20 ),
                'data-gsap-split-scroll-start' => esc_attr( $s['sits_gsap_split_scroll_start']['size'] ?? 100 ),
            ] );
        }

        /* ── Stacking cards ── */
        if ( $stack_on ) {
            $element->add_render_attribute( '_wrapper', [
                'data-gsap-stack'           => 'yes',
                'data-gsap-stack-pin-start' => esc_attr( $s['sits_gsap_stack_pin_start']      ?? 'top top' ),
                'data-gsap-stack-selector'  => esc_attr( $s['sits_gsap_stack_item_selector']  ?? '' ),                
                'data-gsap-stack-markers'   => esc_attr( $s['sits_gsap_stack_markers']        ?? '' ),
                'data-gsap-stack-ease'      => esc_attr( $s['sits_gsap_stack_ease']             ?? 'none' ),
                'data-gsap-stack-mobile-disable' => esc_attr( $s['sits_gsap_stack_mobile_disable'] ?? 'yes' ),
                'data-gsap-stack-mobile-bp'      => esc_attr( $s['sits_gsap_stack_mobile_bp']      ?? 768 ),
                'data-gsap-stack-scrub-anim'     => esc_attr( $s['sits_gsap_stack_scrub_anim']     ?? '' ),
                'data-gsap-stack-scrub-scale'    => esc_attr( $s['sits_gsap_stack_scrub_scale']['size'] ?? 0.85 ),
                'data-gsap-stack-translate-enable' => esc_attr( $s['sits_gsap_stack_translate_enable'] ?? '' ),
                'data-gsap-stack-x'              => esc_attr( $s['sits_gsap_stack_translate_x']['size'] ?? 12 ),
                'data-gsap-stack-y'              => esc_attr( $s['sits_gsap_stack_translate_y']['size'] ?? -41 ),
                'data-gsap-stack-z'              => esc_attr( $s['sits_gsap_stack_translate_z']['size'] ?? 41 ),
                'data-gsap-stack-scrub-bg'       => esc_attr( $s['sits_gsap_stack_scrub_bg']       ?? '' ),
                'data-gsap-stack-push-brightness-enable'  => esc_attr( $s['sits_gsap_stack_push_brightness_enable'] ?? '' ),
                'data-gsap-stack-push-brightness'         => esc_attr( $s['sits_gsap_stack_push_brightness']['size'] ?? 0.75 ),
                
            ] );
            
        }

        /* ── Split text ── */
        $global_on = ! empty( $s['sits_gsap_split_global_enable'] ) && 'yes' === $s['sits_gsap_split_global_enable'];
        $custom_on = ! empty( $s['sits_gsap_split_custom_enable'] ) && 'yes' === $s['sits_gsap_split_custom_enable'];

        if ( $global_on ) {
            $options    = get_option( self::$opt, [] );
            $class_name = ltrim( self::get_global_class( $options ), '.' );
            $element->add_render_attribute( '_wrapper', 'class', $class_name );
            $element->add_render_attribute( '_wrapper', [ 'data-gsap-split-standalone' => 'global' ] );
        } elseif ( $custom_on ) {
            $element->add_render_attribute( '_wrapper', [
                'data-gsap-split-standalone'   => 'yes',
                'data-gsap-split-selector'     => esc_attr( $s['sits_gsap_split_standalone_selector']             ?? '' ),
                'data-gsap-split-type'         => esc_attr( $s['sits_gsap_split_standalone_type']                 ?? 'chars' ),
                'data-gsap-split-stagger'      => esc_attr( $s['sits_gsap_split_standalone_stagger']['size']      ?? 0.08 ),
                'data-gsap-split-line-stagger' => esc_attr( $s['sits_gsap_split_standalone_line_stagger']['size'] ?? 0.08 ),
                'data-gsap-split-animation' => esc_attr( $s['sits_gsap_split_animation'] ?? 'slideUp' ),
                'data-gsap-split-trigger' => esc_attr( $s['sits_gsap_split_trigger'] ?? 'scroll' ),
                
            ] );
        }
    }

    /* =====================================================================
     * PRIVATE HELPERS
     * ===================================================================== */

    /** Return the configured global class (always with leading dot). */
    private static function get_global_class( array $options ): string {
        $class = ! empty( $options['gsap_global_split_class'] )
            ? sanitize_text_field( $options['gsap_global_split_class'] )
            : '.global-split-text';
        return ( strpos( $class, '.' ) !== 0 ) ? '.' . $class : $class;
    }

    /** Open a styled controls section. */
    private static function open_section( $element, string $id, string $icon, string $label, string $tab ) {
        $element->start_controls_section( $id, [
            'label' => '<i class="' . esc_attr( $icon ) . '" style="margin-right:5px;background:#0f3d4a;padding:5px;border-radius:5px;font-size:16px;color:#fff"></i> ' . esc_html( $label ),
            'tab'   => $tab,
        ] );
    }

    private static function get_target_tab( $element ): string {
        return ( $element instanceof \Elementor\Widget_Base )
            ? \Elementor\Controls_Manager::TAB_CONTENT
            : \Elementor\Controls_Manager::TAB_LAYOUT;
    }
}