<?php

namespace Unlockafe_addons\Widgets;

use Elementor\Controls_Manager;
use Elementor\Repeater;
use Elementor\Widget_Base;
use Elementor\Group_Control_Typography;
use Elementor\Group_Control_Background;
use Elementor\Group_Control_Border;
use Elementor\Icons_Manager;
use Unlockafe_addons\Classes\Mega_Menu_Nav_Settings;

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

/**
 * Advance Mega Menu – 5 layouts (Blocks, Features, Solutions, Posts, Pages), categories per submenu.
 */
class Advance_Mega_Menu extends Widget_Base {

	public function get_name() {
		return 'unlockafe-advance-mega-menu';
	}

	public function get_title() {
		return __( 'Advance Mega Menu', 'unlock-addons-for-elementor' );
	}

	public function get_icon() {
		return 'eicon-nav-menu ulicon';
	}

	public function get_categories() {
		return [ 'unlockafe-addons' ];
	}

	public function get_keywords() {
		return [ 'mega menu', 'advance', 'dropdown', 'navigation', 'categories' ];
	}

	public function get_style_depends() {
		return [ 'unlockafe-global', 'unlockafe-advance-mega-menu' ];
	}

	public function get_script_depends() {
		return [ 'unlockafe-advance-mega-menu' ];
	}

	protected function register_controls() {
		$this->register_content_controls();
		$this->register_style_controls();
	}

	protected function register_content_controls() {
		// ——— General ———
		$this->start_controls_section( 'section_general', [
			'label' => __( 'General', 'unlock-addons-for-elementor' ),
			'tab'   => Controls_Manager::TAB_CONTENT,
		] );
		$this->add_control( 'menu_source', [
			'label'   => __( 'Menu source', 'unlock-addons-for-elementor' ),
			'type'    => Controls_Manager::SELECT,
			'options' => [
				'repeater' => __( 'Repeater (build here)', 'unlock-addons-for-elementor' ),
				'wp_menu'  => __( 'WordPress menu', 'unlock-addons-for-elementor' ),
			],
			'default' => 'repeater',
		] );
		$menus = wp_get_nav_menus();
		$menu_options = [ '' => __( '— Select menu —', 'unlock-addons-for-elementor' ) ];
		foreach ( $menus as $menu ) {
			$menu_options[ $menu->term_id ] = $menu->name;
		}
		$this->add_control( 'wp_menu_id', [
			'label'     => __( 'WordPress menu', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::SELECT,
			'options'   => $menu_options,
			'condition' => [ 'menu_source' => 'wp_menu' ],
		] );
		$this->add_control( 'import_dummy_data', [
			'label'        => __( 'Import dummy data', 'unlock-addons-for-elementor' ),
			'type'         => Controls_Manager::SWITCHER,
			'return_value' => 'yes',
			'default'      => '',
			'condition'    => [ 'menu_source' => 'repeater' ],
			'description'  => __( 'The menu always shows exactly what is in Menu Items below. Use "Load dummy data into menu" there to fill demo data; any change you make (menu, submenu, category, image) will appear in the menu. Dummy images use placeholder images with the item title.', 'unlock-addons-for-elementor' ),
		] );
		$this->add_control( 'trigger', [
			'label'   => __( 'Trigger', 'unlock-addons-for-elementor' ),
			'type'    => Controls_Manager::SELECT,
			'options' => [
				'hover' => __( 'Hover', 'unlock-addons-for-elementor' ),
				'click' => __( 'Click', 'unlock-addons-for-elementor' ),
			],
			'default' => 'hover',
		] );
		$this->end_controls_section();

		// ——— Logo (Image only) – hidden from control panel; uncomment block to restore ———
		/*
		$this->start_controls_section( 'section_logo', [
			'label' => __( 'Logo', 'unlock-addons-for-elementor' ),
			'tab'   => Controls_Manager::TAB_CONTENT,
		] );
		$this->add_control( 'show_logo', [
			'label'        => __( 'Show Logo', 'unlock-addons-for-elementor' ),
			'type'         => Controls_Manager::SWITCHER,
			'return_value' => 'yes',
			'default'      => 'yes',
		] );
		$this->add_control( 'logo_image', [
			'label'     => __( 'Logo Image', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::MEDIA,
			'condition' => [ 'show_logo' => 'yes' ],
		] );
		$this->add_control( 'logo_link', [
			'label'       => __( 'Logo Link', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::URL,
			'placeholder' => home_url( '/' ),
			'show_external' => true,
			'default'     => [ 'url' => '', 'is_external' => false, 'nofollow' => false ],
			'condition'   => [ 'show_logo' => 'yes' ],
		] );
		$this->end_controls_section();
		*/

		// ——— Header Button – hidden from control panel; uncomment block to restore ———
		/*
		$this->start_controls_section( 'section_header_button', [
			'label' => __( 'Header Button', 'unlock-addons-for-elementor' ),
			'tab'   => Controls_Manager::TAB_CONTENT,
		] );
		$this->add_control( 'show_button', [
			'label'        => __( 'Show Button', 'unlock-addons-for-elementor' ),
			'type'         => Controls_Manager::SWITCHER,
			'return_value' => 'yes',
			'default'      => 'yes',
		] );
		$this->add_control( 'button_text', [
			'label'     => __( 'Button Text', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::TEXT,
			'default'   => __( 'Get Started', 'unlock-addons-for-elementor' ),
			'condition' => [ 'show_button' => 'yes' ],
		] );
		$this->add_control( 'button_link', [
			'label'         => __( 'Button Link', 'unlock-addons-for-elementor' ),
			'type'          => Controls_Manager::URL,
			'placeholder'   => 'https://',
			'show_external' => true,
			'default'       => [ 'url' => '#', 'is_external' => false, 'nofollow' => false ],
			'condition'     => [ 'show_button' => 'yes' ],
		] );
		$this->end_controls_section();
		*/

		// ——— Spacing (Logo, Menu, Button) ———
		$this->start_controls_section( 'section_spacing', [
			'label' => __( 'Spacing', 'unlock-addons-for-elementor' ),
			'tab'   => Controls_Manager::TAB_CONTENT,
		] );
		$this->add_control( 'gap_logo_menu', [
			'label'   => __( 'Space Between Logo & Menu (px)', 'unlock-addons-for-elementor' ),
			'type'    => Controls_Manager::NUMBER,
			'default' => 24,
			'min'     => 0,
			'max'     => 100,
		] );
		$this->add_control( 'gap_menu_items', [
			'label'   => __( 'Space Between Menu Items (px)', 'unlock-addons-for-elementor' ),
			'type'    => Controls_Manager::NUMBER,
			'default' => 24,
			'min'     => 0,
			'max'     => 80,
		] );
		$this->add_control( 'gap_menu_button', [
			'label'   => __( 'Space Between Menu & Button (px)', 'unlock-addons-for-elementor' ),
			'type'    => Controls_Manager::NUMBER,
			'default' => 24,
			'min'     => 0,
			'max'     => 100,
		] );
		$this->end_controls_section();

		// ——— Dropdown sub-items (for simple dropdown menu type) ———
		$dropdown_item_repeater = new Repeater();
		$dropdown_item_repeater->add_control( 'label', [
			'label'       => __( 'Label', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::TEXT,
			'default'     => '',
			'label_block' => true,
		] );
		$dropdown_item_repeater->add_control( 'link', [
			'label'         => __( 'Link', 'unlock-addons-for-elementor' ),
			'type'          => Controls_Manager::URL,
			'placeholder'   => 'https://',
			'show_external' => true,
			'default'       => [ 'url' => '', 'is_external' => false, 'nofollow' => false ],
		] );
		$dropdown_item_repeater->add_control( 'dropdown_item_style_enable', [
			'label'        => __( 'Enable Sub Item Style', 'unlock-addons-for-elementor' ),
			'type'         => Controls_Manager::SWITCHER,
			'return_value' => 'yes',
			'default'      => '',
		] );
		$dropdown_item_repeater->add_control( 'dropdown_item_style_heading', [
			'label'     => __( 'Sub Item Style', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::HEADING,
			'separator' => 'before',
			'condition' => [ 'dropdown_item_style_enable' => 'yes' ],
		] );
		$dropdown_item_repeater->add_group_control( Group_Control_Typography::get_type(), [
			'name'      => 'dropdown_item_typography',
			'selector'  => '{{WRAPPER}} {{CURRENT_ITEM}} .unlockafe-amm-dropdown__link',
			'condition' => [ 'dropdown_item_style_enable' => 'yes' ],
		] );
		$dropdown_item_repeater->add_control( 'dropdown_item_color', [
			'label'     => __( 'Text Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [
				'{{WRAPPER}} {{CURRENT_ITEM}} .unlockafe-amm-dropdown__link' => 'color: {{VALUE}};',
			],
			'condition' => [ 'dropdown_item_style_enable' => 'yes' ],
		] );
		$dropdown_item_repeater->add_control( 'dropdown_item_padding', [
			'label'      => __( 'Item Padding', 'unlock-addons-for-elementor' ),
			'type'       => Controls_Manager::DIMENSIONS,
			'size_units' => [ 'px', 'em', '%' ],
			'selectors'  => [
				'{{WRAPPER}} {{CURRENT_ITEM}} .unlockafe-amm-dropdown__link' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			],
			'condition'  => [ 'dropdown_item_style_enable' => 'yes' ],
		] );
		$dropdown_item_repeater->add_control( 'dropdown_item_margin', [
			'label'      => __( 'Item Margin', 'unlock-addons-for-elementor' ),
			'type'       => Controls_Manager::DIMENSIONS,
			'size_units' => [ 'px', 'em', '%' ],
			'selectors'  => [
				'{{WRAPPER}} {{CURRENT_ITEM}}' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			],
			'condition'  => [ 'dropdown_item_style_enable' => 'yes' ],
		] );
		$dropdown_item_repeater->add_group_control( Group_Control_Border::get_type(), [
			'name'      => 'dropdown_item_border',
			'selector'  => '{{WRAPPER}} {{CURRENT_ITEM}} .unlockafe-amm-dropdown__link',
			'condition' => [ 'dropdown_item_style_enable' => 'yes' ],
		] );
		$dropdown_item_repeater->add_control( 'dropdown_item_border_radius', [
			'label'      => __( 'Border Radius', 'unlock-addons-for-elementor' ),
			'type'       => Controls_Manager::DIMENSIONS,
			'size_units' => [ 'px', '%' ],
			'selectors'  => [
				'{{WRAPPER}} {{CURRENT_ITEM}} .unlockafe-amm-dropdown__link' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			],
			'condition'  => [ 'dropdown_item_style_enable' => 'yes' ],
		] );

		// ——— Menu items (each menu = simple | dropdown | mega) ———
		$cat_repeater = new Repeater();
		$cat_repeater->add_control( 'category_id', [
			'label'       => __( 'Category ID', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::TEXT,
			'default'     => '',
			'label_block' => true,
			'description' => __( 'Unique key (e.g. ai, layout). Used for filtering.', 'unlock-addons-for-elementor' ),
		] );
		$cat_repeater->add_control( 'category_label', [
			'label'       => __( 'Label', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::TEXT,
			'default'     => '',
			'label_block' => true,
		] );
		$cat_repeater->add_control( 'category_icon_type', [
			'label'   => __( 'Category Icon', 'unlock-addons-for-elementor' ),
			'type'    => Controls_Manager::SELECT,
			'default' => 'icon',
			'options' => [
				'none'     => __( 'None', 'unlock-addons-for-elementor' ),
				'icon'     => __( 'Icon', 'unlock-addons-for-elementor' ),
				'image'    => __( 'Image', 'unlock-addons-for-elementor' ),
				'svg_code' => __( 'SVG Code', 'unlock-addons-for-elementor' ),
			],
		] );
		$cat_repeater->add_control( 'category_icon', [
			'label'     => __( 'Icon', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::ICONS,
			'default'   => [ 'value' => 'fas fa-folder', 'library' => 'fa-solid' ],
			'condition' => [ 'category_icon_type' => 'icon' ],
		] );
		$cat_repeater->add_control( 'category_icon_image', [
			'label'     => __( 'Image', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::MEDIA,
			'default'   => [ 'url' => '' ],
			'condition' => [ 'category_icon_type' => 'image' ],
		] );
		$cat_repeater->add_control( 'category_icon_svg_code', [
			'label'       => __( 'SVG Code', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::TEXTAREA,
			'default'     => '',
			'placeholder' => __( 'Paste your SVG code here (e.g. <svg>...</svg>)', 'unlock-addons-for-elementor' ),
			'condition'   => [ 'category_icon_type' => 'svg_code' ],
			'rows'        => 4,
		] );

		$item_repeater = new Repeater();
		$item_repeater->add_control( 'title', [
			'label'       => __( 'Title', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::TEXT,
			'default'     => '',
			'label_block' => true,
		] );
		$item_repeater->add_control( 'description', [
			'label'   => __( 'Description', 'unlock-addons-for-elementor' ),
			'type'    => Controls_Manager::TEXTAREA,
			'default' => '',
		] );
		$item_repeater->add_control( 'link', [
			'label'         => __( 'Link', 'unlock-addons-for-elementor' ),
			'type'          => Controls_Manager::URL,
			'placeholder'   => 'https://',
			'show_external' => true,
			'default'       => [ 'url' => '', 'is_external' => false, 'nofollow' => false ],
		] );
		$item_repeater->add_control( 'category_id', [
			'label'       => __( 'Category', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::TEXT,
			'default'     => '',
			'description' => __( 'Must match a Category ID from the Categories list above. Filter tabs show items by this category. Change per submenu item as needed.', 'unlock-addons-for-elementor' ),
		] );
		$item_repeater->add_control( 'media_type', [
			'label'   => __( 'Left Icon', 'unlock-addons-for-elementor' ),
			'type'    => Controls_Manager::SELECT,
			'default' => 'icon',
			'options' => [
				'none'     => __( 'None', 'unlock-addons-for-elementor' ),
				'icon'     => __( 'Icon', 'unlock-addons-for-elementor' ),
				'image'    => __( 'Image', 'unlock-addons-for-elementor' ),
				'svg_code' => __( 'SVG Code', 'unlock-addons-for-elementor' ),
			],
		] );
		$item_repeater->add_control( 'selected_icon', [
			'label'       => __( 'Icon (left side)', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::ICONS,
			'default'     => [ 'value' => 'fas fa-th', 'library' => 'fa-solid' ],
			'description' => __( 'Shown beside this item in the menu list. Independent of Image below (Image is for featured content).', 'unlock-addons-for-elementor' ),
			'condition'   => [ 'media_type' => 'icon' ],
		] );
		$item_repeater->add_control( 'item_svg_code', [
			'label'       => __( 'SVG Icon Code', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::TEXTAREA,
			'default'     => '',
			'placeholder' => __( 'Paste your SVG code here (e.g. <svg>...</svg>)', 'unlock-addons-for-elementor' ),
			'condition'   => [ 'media_type' => 'svg_code' ],
			'description' => __( 'Paste raw SVG code. It will be shown beside this item in the menu list.', 'unlock-addons-for-elementor' ),
			'rows'        => 4,
		] );
		$item_repeater->add_control( 'featured_media_type', [
			'label'       => __( 'Featured Media Type', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::SELECT,
			'default'     => 'image',
			'options'     => [
				'image'   => __( 'Image', 'unlock-addons-for-elementor' ),
				'gallery' => __( 'Gallery Slider', 'unlock-addons-for-elementor' ),
				'video'   => __( 'Video URL', 'unlock-addons-for-elementor' ),
			],
			'description' => __( 'Controls what is shown in the right preview card (Layout 1 Blocks).', 'unlock-addons-for-elementor' ),
		] );
		$item_repeater->add_control( 'image', [
			'label'       => __( 'Feature Image', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::MEDIA,
			'default'     => [ 'url' => '' ],
			'description' => __( 'Main featured image used in the preview/featured card.', 'unlock-addons-for-elementor' ),
			'condition'   => [
				'featured_media_type' => [ 'image', 'gallery', 'video' ],
			],
		] );
		$item_repeater->add_control( 'image_gallery', [
			'label'       => __( 'Additional Feature Images (Layout 1 slider)', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::GALLERY,
			'default'     => [],
			'description' => __( 'Optional: add multiple feature images. Layout 1 will display them as an auto-playing slider.', 'unlock-addons-for-elementor' ),
			'condition'   => [
				'featured_media_type' => 'gallery',
			],
		] );
		$item_repeater->add_control( 'featured_video_url', [
			'label'         => __( 'Featured Video URL', 'unlock-addons-for-elementor' ),
			'type'          => Controls_Manager::URL,
			'placeholder'   => 'https://',
			'show_external' => false,
			'default'       => [
				'url' => '',
			],
			'condition'     => [
				'featured_media_type' => 'video',
			],
			'description'   => __( 'Video will autoplay (muted) in the right preview card.', 'unlock-addons-for-elementor' ),
		] );
		$item_repeater->add_control( 'badge', [
			'label'   => __( 'Badge', 'unlock-addons-for-elementor' ),
			'type'    => Controls_Manager::SELECT,
			'default' => 'none',
			'options' => [
				'none' => __( 'None', 'unlock-addons-for-elementor' ),
				'new'  => __( 'NEW', 'unlock-addons-for-elementor' ),
			],
		] );
		$item_repeater->add_control( 'is_featured', [
			'label'        => __( 'Featured (Blocks layout: show in right panel)', 'unlock-addons-for-elementor' ),
			'type'         => Controls_Manager::SWITCHER,
			'return_value' => 'yes',
			'default'      => '',
		] );
		$item_repeater->add_control( 'tag_text', [
			'label'       => __( 'Tag (Layout 3: Solutions card badge)', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::TEXT,
			'default'     => '',
			'placeholder' => __( 'e.g. 99.9% Uptime', 'unlock-addons-for-elementor' ),
			'label_block' => true,
		] );

		$menu_repeater = new Repeater();
		$menu_repeater->add_control( 'menu_type', [
			'label'   => __( 'Menu Type', 'unlock-addons-for-elementor' ),
			'type'    => Controls_Manager::SELECT,
			'default' => 'simple',
			'options' => [
				'simple'   => __( 'Simple Menu (link only)', 'unlock-addons-for-elementor' ),
				'dropdown' => __( 'Dropdown Menu (simple sub-items)', 'unlock-addons-for-elementor' ),
				'mega'     => __( 'Mega Menu (Blocks / Features / Solutions)', 'unlock-addons-for-elementor' ),
			],
		] );
		$menu_repeater->add_control( 'menu_label', [
			'label'       => __( 'Menu Label', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::TEXT,
			'default'     => __( 'Menu Item', 'unlock-addons-for-elementor' ),
			'label_block' => true,
		] );
		$menu_repeater->add_control( 'menu_link', [
			'label'         => __( 'Menu Link', 'unlock-addons-for-elementor' ),
			'type'          => Controls_Manager::URL,
			'placeholder'   => 'https://',
			'show_external' => true,
			'default'       => [ 'url' => '#', 'is_external' => false, 'nofollow' => false ],
		] );
		$menu_repeater->add_control( 'show_toggle_arrow', [
			'label'        => __( 'Show Dropdown/Mega Arrow', 'unlock-addons-for-elementor' ),
			'type'         => Controls_Manager::SWITCHER,
			'return_value' => 'yes',
			'default'      => 'yes',
			'condition'    => [ 'menu_type' => [ 'dropdown', 'mega' ] ],
		] );
		$menu_repeater->add_control( 'menu_item_style_enable', [
			'label'        => __( 'Enable Item Style', 'unlock-addons-for-elementor' ),
			'type'         => Controls_Manager::SWITCHER,
			'return_value' => 'yes',
			'default'      => '',
		] );
		$menu_repeater->add_control( 'menu_item_style_heading', [
			'label'     => __( 'Menu Item Style', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::HEADING,
			'separator' => 'before',
			'condition' => [ 'menu_item_style_enable' => 'yes' ],
		] );
		$menu_repeater->add_group_control( Group_Control_Border::get_type(), [
			'name'      => 'menu_item_border',
			'selector'  => '{{WRAPPER}} {{CURRENT_ITEM}} .unlockafe-amm-nav__link',
			'condition' => [ 'menu_item_style_enable' => 'yes' ],
		] );
		$menu_repeater->add_control( 'menu_item_margin', [
			'label'      => __( 'Item Margin', 'unlock-addons-for-elementor' ),
			'type'       => Controls_Manager::DIMENSIONS,
			'size_units' => [ 'px', 'em', '%' ],
			'selectors'  => [
				'{{WRAPPER}} {{CURRENT_ITEM}}.unlockafe-amm-nav__item' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			],
			'condition'  => [ 'menu_item_style_enable' => 'yes' ],
		] );
		$menu_repeater->add_control( 'menu_item_padding', [
			'label'      => __( 'Item Padding', 'unlock-addons-for-elementor' ),
			'type'       => Controls_Manager::DIMENSIONS,
			'size_units' => [ 'px', 'em', '%' ],
			'selectors'  => [
				'{{WRAPPER}} {{CURRENT_ITEM}} .unlockafe-amm-nav__link' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			],
			'condition'  => [ 'menu_item_style_enable' => 'yes' ],
		] );
		$menu_repeater->add_control( 'menu_item_border_radius', [
			'label'      => __( 'Border Radius', 'unlock-addons-for-elementor' ),
			'type'       => Controls_Manager::DIMENSIONS,
			'size_units' => [ 'px', '%' ],
			'selectors'  => [
				'{{WRAPPER}} {{CURRENT_ITEM}} .unlockafe-amm-nav__link' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			],
			'condition'  => [ 'menu_item_style_enable' => 'yes' ],
		] );
		$menu_repeater->add_control( 'menu_item_underline_height', [
			'label'      => __( 'Underline Height', 'unlock-addons-for-elementor' ),
			'type'       => Controls_Manager::SLIDER,
			'size_units' => [ 'px' ],
			'range'      => [
				'px' => [
					'min' => 0,
					'max' => 10,
				],
			],
			'selectors'  => [
				'{{WRAPPER}} {{CURRENT_ITEM}} .unlockafe-amm-nav__link::after' => 'height: {{SIZE}}{{UNIT}};',
			],
			'condition'  => [ 'menu_item_style_enable' => 'yes' ],
		] );
		$menu_repeater->add_control( 'menu_item_underline_color', [
			'label'     => __( 'Underline Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [
				'{{WRAPPER}} {{CURRENT_ITEM}} .unlockafe-amm-nav__link::after' => 'background: {{VALUE}};',
			],
			'condition' => [ 'menu_item_style_enable' => 'yes' ],
		] );
		$menu_repeater->add_control( 'menu_item_hover_style_heading', [
			'label'     => __( 'Hover / Active Style', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::HEADING,
			'separator' => 'before',
			'condition' => [ 'menu_item_style_enable' => 'yes' ],
		] );
		$menu_repeater->add_group_control( Group_Control_Border::get_type(), [
			'name'      => 'menu_item_border_hover',
			'selector'  => '{{WRAPPER}} {{CURRENT_ITEM}}.unlockafe-amm-nav__item:hover .unlockafe-amm-nav__link, {{WRAPPER}} {{CURRENT_ITEM}}.unlockafe-amm-nav__item.is-open .unlockafe-amm-nav__link, {{WRAPPER}} {{CURRENT_ITEM}} .unlockafe-amm-nav__link.is-active',
			'condition' => [ 'menu_item_style_enable' => 'yes' ],
		] );
		$menu_repeater->add_control( 'menu_item_underline_color_hover', [
			'label'     => __( 'Underline Color (Hover / Active)', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [
				'{{WRAPPER}} {{CURRENT_ITEM}}.unlockafe-amm-nav__item:hover .unlockafe-amm-nav__link::after, {{WRAPPER}} {{CURRENT_ITEM}}.unlockafe-amm-nav__item.is-open .unlockafe-amm-nav__link::after, {{WRAPPER}} {{CURRENT_ITEM}} .unlockafe-amm-nav__link.is-active::after' => 'background: {{VALUE}};',
			],
			'condition' => [ 'menu_item_style_enable' => 'yes' ],
		] );
		$menu_repeater->add_control( 'dropdown_items', [
			'label'       => __( 'Dropdown Sub-items', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::REPEATER,
			'fields'      => $dropdown_item_repeater->get_controls(),
			'default'     => [
				[ 'label' => __( 'Free', 'unlock-addons-for-elementor' ), 'link' => [ 'url' => '#' ] ],
				[ 'label' => __( 'Pro', 'unlock-addons-for-elementor' ), 'link' => [ 'url' => '#' ] ],
				[ 'label' => __( 'Enterprise', 'unlock-addons-for-elementor' ), 'link' => [ 'url' => '#' ] ],
			],
			'title_field' => '{{{ label }}}',
			'condition'   => [ 'menu_type' => 'dropdown' ],
		] );
		$menu_repeater->add_control( 'mega_heading', [
			'label'     => __( 'Mega Menu Settings', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::HEADING,
			'separator' => 'before',
			'condition' => [ 'menu_type' => 'mega' ],
		] );
		$menu_repeater->add_control( 'menu_layout', [
			'label'     => __( 'Layout', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::SELECT,
			'default'   => 'blocks',
			'options'   => [
				'blocks'    => __( 'Layout 1: Blocks & Widgets (list + detail panel)', 'unlock-addons-for-elementor' ),
				'features'  => __( 'Layout 2: Features (hero + grid)', 'unlock-addons-for-elementor' ),
				'solutions' => __( 'Layout 3: Solutions (category pills + cards)', 'unlock-addons-for-elementor' ),
				'posts'     => __( 'Layout 4: Posts (all posts as card grid)', 'unlock-addons-for-elementor' ),
				'pages'     => __( 'Layout 5: Pages (all pages as card grid)', 'unlock-addons-for-elementor' ),
				'custom'    => __( 'Custom: Elementor page (your own layout)', 'unlock-addons-for-elementor' ),
			],
			'condition' => [ 'menu_type' => 'mega' ],
		] );
		$pages_for_select = [ 0 => __( '— Select page —', 'unlock-addons-for-elementor' ) ];
		$all_pages = get_posts( [ 'post_type' => 'page', 'post_status' => 'publish', 'posts_per_page' => 200, 'orderby' => 'title', 'order' => 'ASC' ] );
		foreach ( $all_pages as $p ) {
			$pages_for_select[ $p->ID ] = $p->post_title;
		}
		$menu_repeater->add_control( 'custom_layout_page_id', [
			'label'     => __( 'Elementor page for mega content', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::SELECT,
			'default'   => 0,
			'options'   => $pages_for_select,
			'condition' => [ 'menu_type' => 'mega', 'menu_layout' => 'custom' ],
			'description' => __( 'Build the layout in Elementor on that page; it will show as this item\'s mega menu panel.', 'unlock-addons-for-elementor' ),
		] );
		$menu_repeater->add_control( 'panel_title', [
			'label'       => __( 'Panel Title', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::TEXT,
			'default'     => '',
			'label_block' => true,
			'description' => __( 'e.g. Browse by Category, Discover Our Features, Enterprise Solutions', 'unlock-addons-for-elementor' ),
			'condition'   => [ 'menu_type' => 'mega' ],
		] );
		$menu_repeater->add_control( 'posts_limit', [
			'label'     => __( 'Number of Posts', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::NUMBER,
			'default'   => 9,
			'min'       => 1,
			'max'       => 50,
			'condition' => [ 'menu_type' => 'mega', 'menu_layout' => 'posts' ],
		] );
		$menu_repeater->add_control( 'posts_per_row', [
			'label'     => __( 'Posts Per Row', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::NUMBER,
			'default'   => 3,
			'min'       => 2,
			'max'       => 6,
			'condition' => [ 'menu_type' => 'mega', 'menu_layout' => 'posts' ],
		] );
		$menu_repeater->add_control( 'pages_limit', [
			'label'     => __( 'Number of Pages', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::NUMBER,
			'default'   => 9,
			'min'       => 1,
			'max'       => 50,
			'condition' => [ 'menu_type' => 'mega', 'menu_layout' => 'pages' ],
		] );
		$menu_repeater->add_control( 'pages_per_row', [
			'label'     => __( 'Pages Per Row', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::NUMBER,
			'default'   => 3,
			'min'       => 2,
			'max'       => 6,
			'condition' => [ 'menu_type' => 'mega', 'menu_layout' => 'pages' ],
		] );
		$menu_repeater->add_control( 'categories', [
			'label'       => __( 'Categories', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::REPEATER,
			'fields'      => $cat_repeater->get_controls(),
			'default'     => [
				[ 'category_id' => 'all', 'category_label' => __( 'All Blocks', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-th', 'library' => 'fa-solid' ] ],
				[ 'category_id' => 'ai', 'category_label' => __( 'AI Features', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-robot', 'library' => 'fa-solid' ] ],
				[ 'category_id' => 'layout', 'category_label' => __( 'Layout', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-layer-group', 'library' => 'fa-solid' ] ],
				[ 'category_id' => 'woocommerce', 'category_label' => __( 'WooCommerce', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-shopping-cart', 'library' => 'fa-solid' ] ],
			],
			'title_field' => '{{{ category_label }}} ({{{ category_id }}})',
			'condition'   => [ 'menu_type' => 'mega', 'menu_layout!' => [ 'posts', 'pages', 'custom' ] ],
			'description' => __( 'Use the button below to add a category only (filter tab). Each submenu item can be assigned to a category via Category ID.', 'unlock-addons-for-elementor' ),
			'button_text' => __( 'Add Category', 'unlock-addons-for-elementor' ),
		] );
		$menu_repeater->add_control( 'submenu_items', [
			'label'       => __( 'Submenu Items', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::REPEATER,
			'fields'      => $item_repeater->get_controls(),
			'default'     => [
				[ 'title' => __( 'AI Text Generator', 'unlock-addons-for-elementor' ), 'description' => __( 'Generate high-quality content with advanced AI technology.', 'unlock-addons-for-elementor' ), 'category_id' => 'ai', 'media_type' => 'icon', 'selected_icon' => [ 'value' => 'fas fa-th', 'library' => 'fa-solid' ], 'badge' => 'new', 'is_featured' => 'yes' ],
				[ 'title' => __( 'AI Chatbot', 'unlock-addons-for-elementor' ), 'description' => __( 'Add conversational AI to your site.', 'unlock-addons-for-elementor' ), 'category_id' => 'ai', 'media_type' => 'icon', 'selected_icon' => [ 'value' => 'fas fa-th', 'library' => 'fa-solid' ], 'badge' => 'new' ],
				[ 'title' => __( 'Popup Anywhere', 'unlock-addons-for-elementor' ), 'description' => __( 'Create targeted popups anywhere on your site.', 'unlock-addons-for-elementor' ), 'category_id' => 'layout', 'media_type' => 'icon', 'selected_icon' => [ 'value' => 'fas fa-th', 'library' => 'fa-solid' ], 'badge' => 'new' ],
				[ 'title' => __( 'Header Sidebar', 'unlock-addons-for-elementor' ), 'description' => __( 'Add sidebar content in header.', 'unlock-addons-for-elementor' ), 'category_id' => 'layout', 'media_type' => 'icon', 'selected_icon' => [ 'value' => 'fas fa-th', 'library' => 'fa-solid' ] ],
				[ 'title' => __( 'Products Grid', 'unlock-addons-for-elementor' ), 'description' => __( 'Display products in clean grids.', 'unlock-addons-for-elementor' ), 'category_id' => 'woocommerce', 'media_type' => 'icon', 'selected_icon' => [ 'value' => 'fas fa-th', 'library' => 'fa-solid' ] ],
			],
			'title_field' => '{{{ title }}} ({{{ category_id }}})',
			'condition'   => [ 'menu_type' => 'mega', 'menu_layout!' => [ 'posts', 'pages', 'custom' ] ],
			'description' => __( 'Use the button below to add a submenu item only (block/card). Image/Video upload works in all layouts.', 'unlock-addons-for-elementor' ),
			'button_text' => __( 'Add Submenu', 'unlock-addons-for-elementor' ),
		] );
		$menu_repeater->add_control( 'view_all_text', [
			'label'     => __( 'View All Button Text', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::TEXT,
			'default'   => __( 'View All Blocks', 'unlock-addons-for-elementor' ),
			'condition' => [ 'menu_type' => 'mega' ],
		] );
		$menu_repeater->add_control( 'view_all_link', [
			'label'         => __( 'View All Link', 'unlock-addons-for-elementor' ),
			'type'          => Controls_Manager::URL,
			'placeholder'   => 'https://',
			'show_external' => true,
			'default'       => [ 'url' => '#', 'is_external' => false, 'nofollow' => false ],
			'condition'     => [ 'menu_type' => 'mega' ],
		] );
		$menu_repeater->add_control( 'cta_show', [
			'label'        => __( 'Show Bottom of Mega Menu', 'unlock-addons-for-elementor' ),
			'type'         => Controls_Manager::SWITCHER,
			'return_value' => 'yes',
			'default'      => 'yes',
			'condition'    => [ 'menu_type' => 'mega' ],
		] );
		$menu_repeater->add_control( 'cta_text', [
			'label'     => __( 'Title', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::TEXT,
			'default'   => __( 'Unlock All Premium Features', 'unlock-addons-for-elementor' ),
			'condition' => [ 'menu_type' => 'mega', 'cta_show' => 'yes' ],
		] );
		$menu_repeater->add_control( 'cta_subtext', [
			'label'     => __( 'Subtext', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::TEXT,
			'default'   => __( 'Get access to 41+ blocks', 'unlock-addons-for-elementor' ),
			'condition' => [ 'menu_type' => 'mega', 'cta_show' => 'yes' ],
		] );
		$menu_repeater->add_control( 'cta_button_text', [
			'label'     => __( 'Button Text', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::TEXT,
			'default'   => __( 'Get Plugin', 'unlock-addons-for-elementor' ),
			'condition' => [ 'menu_type' => 'mega', 'cta_show' => 'yes' ],
		] );
		$menu_repeater->add_control( 'cta_link', [
			'label'         => __( 'Link', 'unlock-addons-for-elementor' ),
			'type'          => Controls_Manager::URL,
			'placeholder'   => 'https://',
			'show_external' => true,
			'default'       => [ 'url' => '#', 'is_external' => false, 'nofollow' => false ],
			'condition'     => [ 'menu_type' => 'mega', 'cta_show' => 'yes' ],
		] );

		$this->start_controls_section( 'section_menus', [
			'label'     => __( 'Menu Items', 'unlock-addons-for-elementor' ),
			'tab'       => Controls_Manager::TAB_CONTENT,
			'condition' => [ 'menu_source' => 'repeater' ],
		] );
		$this->add_control( 'load_dummy_data_btn', [
			'label'       => __( 'Load dummy data', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::BUTTON,
			'text'        => __( 'Load dummy data into menu', 'unlock-addons-for-elementor' ),
			'event'       => 'unlockafe:load_dummy_menu',
			'description' => __( 'Fills the Menus list below with 6 demo menus (Home, Widgets, Features, Solutions, Pricing, Contact), categories, and 49+ submenus with title-related images. You can then edit everything in Menu Items.', 'unlock-addons-for-elementor' ),
		] );
		$this->add_control( 'menu_items', [
			'label'       => __( 'Menus', 'unlock-addons-for-elementor' ),
			'type'        => Controls_Manager::REPEATER,
			'fields'      => $menu_repeater->get_controls(),
			'default'     => $this->get_default_menu_items(),
			'title_field' => '{{{ menu_label }}} ({{{ menu_type }}})',
			'separator'   => 'before',
			'description' => __( 'Use "Add Menu" to add a top-level menu. Expand a Mega Menu item to add categories and submenu items. Or use "Load dummy data" above to pre-fill demo data.', 'unlock-addons-for-elementor' ),
			'button_text' => __( 'Add Menu', 'unlock-addons-for-elementor' ),
		] );
		$this->end_controls_section();
	}

	private function get_default_menu_items() {
		$base_url = 'https://unlockaddons.com/';
		$categories_blocks = [
			[ 'category_id' => 'all', 'category_label' => __( 'All Blocks', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-th', 'library' => 'fa-solid' ] ],
			[ 'category_id' => 'ai', 'category_label' => __( 'AI Features', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-robot', 'library' => 'fa-solid' ] ],
			[ 'category_id' => 'layout', 'category_label' => __( 'Layout', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-layer-group', 'library' => 'fa-solid' ] ],
			[ 'category_id' => 'woocommerce', 'category_label' => __( 'WooCommerce', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-shopping-cart', 'library' => 'fa-solid' ] ],
		];
		$default_submenus = $this->get_default_submenu_items_for_editor( $base_url );
		return [
			[
				'menu_type'  => 'simple',
				'menu_label' => __( 'Home', 'unlock-addons-for-elementor' ),
				'menu_link'  => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
			],
			[
				'menu_type'       => 'mega',
				'menu_label'      => __( 'Widgets', 'unlock-addons-for-elementor' ),
				'menu_link'       => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
				'show_toggle_arrow' => 'yes',
				'menu_layout'     => 'blocks',
				'panel_title'     => __( 'Browse by Category', 'unlock-addons-for-elementor' ),
				'categories'      => $categories_blocks,
				'submenu_items'   => $default_submenus,
				'view_all_text'   => __( 'View All 41 Blocks', 'unlock-addons-for-elementor' ),
				'view_all_link'   => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
				'cta_show'        => 'yes',
				'cta_text'        => __( 'Unlock All Premium Features', 'unlock-addons-for-elementor' ),
				'cta_subtext'     => __( 'Get access to 41+ blocks', 'unlock-addons-for-elementor' ),
				'cta_button_text' => __( 'Get Plugin', 'unlock-addons-for-elementor' ),
				'cta_link'        => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
			],
			[
				'menu_type'       => 'mega',
				'menu_label'      => __( 'Features', 'unlock-addons-for-elementor' ),
				'menu_link'       => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
				'show_toggle_arrow' => 'yes',
				'menu_layout'     => 'features',
				'panel_title'     => __( 'Discover Our Features', 'unlock-addons-for-elementor' ),
				'categories'      => $categories_blocks,
				'submenu_items'   => $default_submenus,
				'view_all_text'   => __( 'View All 41 Blocks', 'unlock-addons-for-elementor' ),
				'view_all_link'   => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
				'cta_show'        => 'yes',
				'cta_text'        => __( 'Ready to supercharge your website?', 'unlock-addons-for-elementor' ),
				'cta_subtext'     => __( 'Unlock all 41+ premium features today', 'unlock-addons-for-elementor' ),
				'cta_button_text' => __( 'Get Started Free', 'unlock-addons-for-elementor' ),
				'cta_link'        => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
			],
			[
				'menu_type'       => 'mega',
				'menu_label'      => __( 'Solutions', 'unlock-addons-for-elementor' ),
				'menu_link'       => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
				'show_toggle_arrow' => 'yes',
				'menu_layout'     => 'solutions',
				'panel_title'     => __( 'Enterprise Solutions', 'unlock-addons-for-elementor' ),
				'categories'      => [
					[ 'category_id' => 'all', 'category_label' => __( 'All Solutions', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-folder', 'library' => 'fa-solid' ] ],
					[ 'category_id' => 'infrastructure', 'category_label' => __( 'Infrastructure', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-server', 'library' => 'fa-solid' ] ],
					[ 'category_id' => 'security', 'category_label' => __( 'Security', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-shield-alt', 'library' => 'fa-solid' ] ],
				],
				'submenu_items'   => $this->get_default_solution_submenu_items_for_editor( $base_url ),
				'view_all_text'   => __( 'View All Solutions', 'unlock-addons-for-elementor' ),
				'view_all_link'   => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
				'cta_show'        => 'yes',
				'cta_text'        => __( 'Ready to Scale Your Business?', 'unlock-addons-for-elementor' ),
				'cta_subtext'     => __( 'Join 10,000+ companies using our enterprise solutions', 'unlock-addons-for-elementor' ),
				'cta_button_text' => __( 'Book a Demo', 'unlock-addons-for-elementor' ),
				'cta_link'        => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
			],
			[
				'menu_type' => 'dropdown',
				'menu_label' => __( 'Pricing', 'unlock-addons-for-elementor' ),
				'menu_link' => [ 'url' => $base_url . '#pricing', 'is_external' => true, 'nofollow' => false ],
				'show_toggle_arrow' => 'yes',
				'dropdown_items' => [
					[ 'label' => __( 'Free', 'unlock-addons-for-elementor' ), 'link' => [ 'url' => $base_url ] ],
					[ 'label' => __( 'Pro', 'unlock-addons-for-elementor' ), 'link' => [ 'url' => $base_url ] ],
					[ 'label' => __( 'Enterprise', 'unlock-addons-for-elementor' ), 'link' => [ 'url' => $base_url ] ],
				],
			],
			[
				'menu_type' => 'simple',
				'menu_label' => __( 'Contact', 'unlock-addons-for-elementor' ),
				'menu_link' => [ 'url' => $base_url . 'contact', 'is_external' => true, 'nofollow' => false ],
			],
		];
	}

	/** Default submenu items for editor (fewer items for faster panel). Uses thematic image URL; frontend falls back to placeholder on error. */
	private function get_default_submenu_items_for_editor( $base_url ) {
		$items = [
			[ 'title' => 'AI Text Generator', 'desc' => 'Generate high-quality content with advanced AI technology.', 'cat' => 'ai', 'badge' => 'new', 'featured' => true ],
			[ 'title' => 'AI Chatbot', 'desc' => 'Add conversational AI to your site.', 'cat' => 'ai', 'badge' => 'new' ],
			[ 'title' => 'Popup Anywhere', 'desc' => 'Create targeted popups anywhere on your site.', 'cat' => 'layout', 'badge' => 'new' ],
			[ 'title' => 'Header Sidebar', 'desc' => 'Add sidebar content in header.', 'cat' => 'layout' ],
			[ 'title' => 'Products Grid', 'desc' => 'Display products in clean grids.', 'cat' => 'woocommerce' ],
		];
		$out = [];
		foreach ( $items as $row ) {
			$out[] = [
				'title'         => $row['title'],
				'description'   => $row['desc'],
				'link'          => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
				'category_id'   => $row['cat'],
				'media_type'    => 'icon',
				'selected_icon' => [ 'value' => 'fas fa-th', 'library' => 'fa-solid' ],
				'badge'         => isset( $row['badge'] ) ? $row['badge'] : 'none',
				'is_featured'   => ! empty( $row['featured'] ) ? 'yes' : '',
			];
		}
		return $out;
	}

	/** Default solution submenu items for editor. */
	private function get_default_solution_submenu_items_for_editor( $base_url ) {
		$items = [
			[ 'title' => 'Enterprise Scale', 'desc' => 'Scale your infrastructure with confidence.', 'cat' => 'infrastructure', 'tag' => 'Popular' ],
			[ 'title' => 'Security First', 'desc' => 'Enterprise-grade security for your data.', 'cat' => 'security', 'tag' => 'NEW' ],
			[ 'title' => 'AI Powered', 'desc' => 'Leverage AI to automate and optimize.', 'cat' => 'infrastructure', 'tag' => 'NEW' ],
		];
		$out = [];
		foreach ( $items as $row ) {
			$out[] = [
				'title'         => $row['title'],
				'description'   => $row['desc'],
				'link'          => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
				'category_id'   => $row['cat'],
				'media_type'    => 'icon',
				'selected_icon' => [ 'value' => 'fas fa-th', 'library' => 'fa-solid' ],
				'badge'         => 'none',
				'tag_text'      => isset( $row['tag'] ) ? $row['tag'] : '',
			];
		}
		return $out;
	}

	/**
	 * Thematic image URL for submenu: real image when possible (Unsplash with keywords).
	 * Frontend uses data-fallback (placeholder) if image fails to load.
	 */
	private function get_thematic_image_url( $title ) {
		$keywords = preg_replace( '/[^a-z0-9\s]/', '', strtolower( $title ) );
		$keywords = preg_replace( '/\s+/', ',', trim( $keywords ) );
		if ( strlen( $keywords ) > 50 ) {
			$keywords = implode( ',', array_slice( explode( ',', $keywords ), 0, 4 ) );
		}
		return 'https://source.unsplash.com/600x600/?' . ( $keywords ?: 'featured' );
	}

	/** Placeholder image URL when thematic image is unavailable (used in data-fallback). */
	private function get_placeholder_image_url( $title ) {
		return 'https://placehold.co/600x600/e2e8f0/64748b?text=' . rawurlencode( $title );
	}

	/**
	 * Allowed SVG tags and attributes for wp_kses when outputting user SVG code.
	 *
	 * @return array<string, array<string>>
	 */
	private function allowed_svg_kses(): array {
		$allowed_attr = [
			'class'   => true,
			'id'      => true,
			'width'   => true,
			'height'  => true,
			'viewbox' => true,
			'viewBox' => true,
			'xmlns'   => true,
			'fill'    => true,
			'stroke'  => true,
			'stroke-width' => true,
			'stroke-linecap' => true,
			'stroke-linejoin' => true,
			'd'       => true,
			'transform' => true,
			'x'       => true,
			'y'       => true,
			'cx'      => true,
			'cy'      => true,
			'r'       => true,
			'rx'      => true,
			'ry'      => true,
			'points'  => true,
			'x1'      => true,
			'y1'      => true,
			'x2'      => true,
			'y2'      => true,
		];
		return [
			'svg'   => $allowed_attr,
			'path'  => $allowed_attr,
			'circle' => $allowed_attr,
			'rect'  => $allowed_attr,
			'line'  => $allowed_attr,
			'polyline' => $allowed_attr,
			'polygon' => $allowed_attr,
			'g'     => $allowed_attr,
			'defs'  => $allowed_attr,
			'use'   => array_merge( $allowed_attr, [ 'href' => true, 'xlink:href' => true ] ),
		];
	}

	/**
	 * Collect all image URLs for a submenu item: primary image + optional gallery images.
	 *
	 * @param array $item Submenu item data.
	 *
	 * @return array<string> Unique list of image URLs.
	 */
	private function get_item_image_urls( array $item ): array {
		$urls = [];

		if ( ! empty( $item['image']['url'] ) ) {
			$urls[] = (string) $item['image']['url'];
		}

		if ( ! empty( $item['image_gallery'] ) && is_array( $item['image_gallery'] ) ) {
			foreach ( $item['image_gallery'] as $img ) {
				if ( empty( $img['url'] ) ) {
					continue;
				}
				$url = (string) $img['url'];
				if ( ! in_array( $url, $urls, true ) ) {
					$urls[] = $url;
				}
			}
		}

		return $urls;
	}

	/**
	 * Determine featured media configuration (type + video URL + images) for a submenu item.
	 *
	 * This is used by:
	 * - Left list rows (data-* attributes for JS)
	 * - Right featured card (initial render)
	 *
	 * It is backward compatible with older data where:
	 * - media_type could be 'video' and video.url was used
	 * - no featured_media_type setting existed.
	 *
	 * @param array $item
	 * @return array{
	 *   type: string,
	 *   video: string,
	 *   images: array<int, string>
	 * }
	 */
	private function get_featured_media_config( array $item ): array {
		$images = $this->get_item_image_urls( $item );
		$legacy_media_type = isset( $item['media_type'] ) ? (string) $item['media_type'] : 'icon';
		$legacy_video_url  = isset( $item['video']['url'] ) ? (string) $item['video']['url'] : '';

		$featured_type = isset( $item['featured_media_type'] ) ? (string) $item['featured_media_type'] : '';
		if ( $featured_type === '' ) {
			if ( $legacy_media_type === 'video' && $legacy_video_url !== '' ) {
				$featured_type = 'video';
			} elseif ( count( $images ) > 1 ) {
				$featured_type = 'gallery';
			} else {
				$featured_type = 'image';
			}
		}

		$video_url = '';
		if ( $featured_type === 'video' ) {
			if ( ! empty( $item['featured_video_url']['url'] ) ) {
				$video_url = (string) $item['featured_video_url']['url'];
			} elseif ( $legacy_video_url !== '' ) {
				$video_url = $legacy_video_url;
			}
		}

		return [
			'type'   => $featured_type,
			'video'  => $video_url,
			'images' => $images,
		];
	}

	/**
	 * Dummy menu data when "Import dummy data" is ON: 6 menus (Home, Widgets, Features, Solutions, Pricing, Contact)
	 * with 49 addon submenus for Layout 1 & 2 and solution submenus for Layout 3.
	 */
	public function get_dummy_menu_items() {
		$base_url = 'https://unlockaddons.com/';
		$img_base = 'https://picsum.photos/seed/';
		$categories_blocks = [
			[ 'category_id' => 'all', 'category_label' => __( 'All Blocks', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-th', 'library' => 'fa-solid' ] ],
			[ 'category_id' => 'ai', 'category_label' => __( 'AI Features', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-robot', 'library' => 'fa-solid' ] ],
			[ 'category_id' => 'layout', 'category_label' => __( 'Layout', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-layer-group', 'library' => 'fa-solid' ] ],
			[ 'category_id' => 'woocommerce', 'category_label' => __( 'WooCommerce', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-shopping-cart', 'library' => 'fa-solid' ] ],
		];
		$addons = $this->get_dummy_addon_submenu_items( $base_url, $img_base );
		$solutions = $this->get_dummy_solution_submenu_items( $base_url, $img_base );
		$categories_solutions = [
			[ 'category_id' => 'all', 'category_label' => __( 'All Solutions', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-folder', 'library' => 'fa-solid' ] ],
			[ 'category_id' => 'infrastructure', 'category_label' => __( 'Infrastructure', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-server', 'library' => 'fa-solid' ] ],
			[ 'category_id' => 'security', 'category_label' => __( 'Security', 'unlock-addons-for-elementor' ), 'category_icon_type' => 'icon', 'category_icon' => [ 'value' => 'fas fa-shield-alt', 'library' => 'fa-solid' ] ],
		];
		return [
			[
				'menu_type'  => 'simple',
				'menu_label' => __( 'Home', 'unlock-addons-for-elementor' ),
				'menu_link'  => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
			],
			[
				'menu_type'       => 'mega',
				'menu_label'      => __( 'Widgets', 'unlock-addons-for-elementor' ),
				'menu_link'       => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
				'show_toggle_arrow' => 'yes',
				'menu_layout'     => 'blocks',
				'panel_title'     => __( 'Browse by Category', 'unlock-addons-for-elementor' ),
				'categories'      => $categories_blocks,
				'submenu_items'   => $addons,
				'view_all_text'   => __( 'View All 41 Blocks', 'unlock-addons-for-elementor' ),
				'view_all_link'   => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
				'cta_show'        => 'yes',
				'cta_text'        => __( 'Unlock All Premium Features', 'unlock-addons-for-elementor' ),
				'cta_subtext'     => __( 'Get access to 41+ blocks', 'unlock-addons-for-elementor' ),
				'cta_button_text' => __( 'Get Plugin', 'unlock-addons-for-elementor' ),
				'cta_link'        => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
			],
			[
				'menu_type'       => 'mega',
				'menu_label'      => __( 'Features', 'unlock-addons-for-elementor' ),
				'menu_link'       => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
				'show_toggle_arrow' => 'yes',
				'menu_layout'     => 'features',
				'panel_title'     => __( 'Discover Our Features', 'unlock-addons-for-elementor' ),
				'categories'      => $categories_blocks,
				'submenu_items'   => $addons,
				'view_all_text'   => __( 'View All 41 Blocks', 'unlock-addons-for-elementor' ),
				'view_all_link'   => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
				'cta_show'        => 'yes',
				'cta_text'        => __( 'Ready to supercharge your website?', 'unlock-addons-for-elementor' ),
				'cta_subtext'     => __( 'Unlock all 41+ premium features today', 'unlock-addons-for-elementor' ),
				'cta_button_text' => __( 'Get Started Free', 'unlock-addons-for-elementor' ),
				'cta_link'        => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
			],
			[
				'menu_type'       => 'mega',
				'menu_label'      => __( 'Solutions', 'unlock-addons-for-elementor' ),
				'menu_link'       => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
				'show_toggle_arrow' => 'yes',
				'menu_layout'     => 'solutions',
				'panel_title'     => __( 'Enterprise Solutions', 'unlock-addons-for-elementor' ),
				'categories'      => $categories_solutions,
				'submenu_items'   => $solutions,
				'view_all_text'   => __( 'View All Solutions', 'unlock-addons-for-elementor' ),
				'view_all_link'   => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
				'cta_show'        => 'yes',
				'cta_text'        => __( 'Ready to Scale Your Business?', 'unlock-addons-for-elementor' ),
				'cta_subtext'     => __( 'Join 10,000+ companies using our enterprise solutions', 'unlock-addons-for-elementor' ),
				'cta_button_text' => __( 'Book a Demo', 'unlock-addons-for-elementor' ),
				'cta_link'        => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
			],
			[
				'menu_type'       => 'dropdown',
				'menu_label'      => __( 'Pricing', 'unlock-addons-for-elementor' ),
				'menu_link'       => [ 'url' => $base_url . '#pricing', 'is_external' => true, 'nofollow' => false ],
				'show_toggle_arrow' => 'yes',
				'dropdown_items'  => [
					[ 'label' => __( 'Free', 'unlock-addons-for-elementor' ), 'link' => [ 'url' => $base_url ] ],
					[ 'label' => __( 'Pro', 'unlock-addons-for-elementor' ), 'link' => [ 'url' => $base_url ] ],
					[ 'label' => __( 'Enterprise', 'unlock-addons-for-elementor' ), 'link' => [ 'url' => $base_url ] ],
				],
			],
			[
				'menu_type'  => 'simple',
				'menu_label' => __( 'Contact', 'unlock-addons-for-elementor' ),
				'menu_link'  => [ 'url' => $base_url . 'contact', 'is_external' => true, 'nofollow' => false ],
			],
		];
	}

	/** Dummy submenu items: 49 addons for Layout 1 & 2. Images: thematic (Unsplash); fallback to placeholder if unavailable. */
	private function get_dummy_addon_submenu_items( $base_url, $img_base ) {
		$items = [
			[ 'title' => 'AI Text Generator', 'desc' => 'Generate high-quality content with advanced AI technology.', 'cat' => 'ai', 'badge' => 'new', 'featured' => true ],
			[ 'title' => 'AI Chatbot', 'desc' => 'Add conversational AI to your site.', 'cat' => 'ai', 'badge' => 'new' ],
			[ 'title' => 'Email Subscription', 'desc' => 'Collect emails with beautiful forms.', 'cat' => 'ai' ],
			[ 'title' => 'Popup Anywhere', 'desc' => 'Create targeted popups anywhere on your site.', 'cat' => 'layout', 'badge' => 'new' ],
			[ 'title' => 'Shopping Cart', 'desc' => 'Sync and display data from spreadsheets.', 'cat' => 'woocommerce', 'badge' => 'new' ],
			[ 'title' => 'Checkout', 'desc' => 'Showcase media with galleries and sliders.', 'cat' => 'woocommerce' ],
			[ 'title' => 'Advanced Google Map', 'desc' => 'Display interactive maps on your pages.', 'cat' => 'layout' ],
			[ 'title' => 'Animated Text', 'desc' => 'Add eye-catching text animations.', 'cat' => 'layout' ],
			[ 'title' => 'Breadcrumbs', 'desc' => 'Show navigation breadcrumbs for better UX.', 'cat' => 'layout' ],
			[ 'title' => 'Blog Grid', 'desc' => 'Display posts in a clean grid layout.', 'cat' => 'layout' ],
			[ 'title' => 'Business Hour', 'desc' => 'Show your business hours clearly.', 'cat' => 'layout' ],
			[ 'title' => 'Cookie Consent', 'desc' => 'Easy and compliant cookie consent banners.', 'cat' => 'layout' ],
			[ 'title' => 'Customer Review', 'desc' => 'Showcase customer testimonials.', 'cat' => 'layout' ],
			[ 'title' => 'Counter', 'desc' => 'Animated number counters for stats.', 'cat' => 'layout' ],
			[ 'title' => 'Circle Menu', 'desc' => 'Create circular navigation menus.', 'cat' => 'layout' ],
			[ 'title' => 'Dynamic Chart', 'desc' => 'Display charts and data visualizations.', 'cat' => 'layout' ],
			[ 'title' => 'Event Timeline', 'desc' => 'Present events in a clear visual timeline.', 'cat' => 'layout' ],
			[ 'title' => 'Footer Links', 'desc' => 'Build custom footer link sections.', 'cat' => 'layout' ],
			[ 'title' => 'Footer Social', 'desc' => 'Add social icons to your footer.', 'cat' => 'layout' ],
			[ 'title' => 'Google Sheet', 'desc' => 'Sync and display data from spreadsheets.', 'cat' => 'layout' ],
			[ 'title' => 'Header Logo', 'desc' => 'Place and style your header logo.', 'cat' => 'layout' ],
			[ 'title' => 'Header Menu', 'desc' => 'Build custom header navigation.', 'cat' => 'layout' ],
			[ 'title' => 'Header Sidebar', 'desc' => 'Add sidebar content in header.', 'cat' => 'layout' ],
			[ 'title' => 'Header Search', 'desc' => 'Add search to your header.', 'cat' => 'layout' ],
			[ 'title' => 'Header Shop Cart', 'desc' => 'WooCommerce cart in header.', 'cat' => 'woocommerce' ],
			[ 'title' => 'Image Hotspot', 'desc' => 'Add interactive hotspots to images.', 'cat' => 'layout' ],
			[ 'title' => 'Interactive Promo', 'desc' => 'Create engaging promo blocks.', 'cat' => 'layout' ],
			[ 'title' => 'Onepage Navigation', 'desc' => 'Smooth one-page site navigation.', 'cat' => 'layout' ],
			[ 'title' => 'Promo Box', 'desc' => 'Highlight offers with promo boxes.', 'cat' => 'layout' ],
			[ 'title' => 'Products Grid', 'desc' => 'Display products in clean grids.', 'cat' => 'woocommerce' ],
			[ 'title' => 'Products Carousel', 'desc' => 'Highlight products with carousels.', 'cat' => 'woocommerce' ],
			[ 'title' => '360° Photo Viewer', 'desc' => 'Showcase 360-degree product images.', 'cat' => 'layout' ],
			[ 'title' => 'Progress Bar', 'desc' => 'Animated progress indicators.', 'cat' => 'layout' ],
			[ 'title' => 'Price Menu', 'desc' => 'Display pricing tables and menus.', 'cat' => 'woocommerce' ],
			[ 'title' => 'Showcase Slider', 'desc' => 'Create showcase content sliders.', 'cat' => 'layout' ],
			[ 'title' => 'Sync Advantage', 'desc' => 'Sync products and inventory effortlessly.', 'cat' => 'woocommerce' ],
			[ 'title' => 'Team', 'desc' => 'Display team member profiles.', 'cat' => 'layout' ],
			[ 'title' => 'Table of Contents', 'desc' => 'Auto-generated table of contents.', 'cat' => 'layout' ],
			[ 'title' => 'Video Gallery', 'desc' => 'Showcase videos in a gallery.', 'cat' => 'layout' ],
			[ 'title' => 'Image Scroller', 'desc' => 'Horizontal image scroll sections.', 'cat' => 'layout' ],
			[ 'title' => 'Form Builder', 'desc' => 'Build advanced forms with drag-and-drop.', 'cat' => 'layout' ],
			[ 'title' => 'Testimonial Slider', 'desc' => 'Display testimonials in sliders.', 'cat' => 'layout' ],
			[ 'title' => 'Countdown Timer', 'desc' => 'Add countdown timers for events.', 'cat' => 'layout' ],
			[ 'title' => 'Tabs & Accordion', 'desc' => 'Tabs and accordion content.', 'cat' => 'layout' ],
			[ 'title' => 'Before After', 'desc' => 'Before/after image comparison.', 'cat' => 'layout' ],
			[ 'title' => 'Icon Box', 'desc' => 'Icon with title and description boxes.', 'cat' => 'layout' ],
			[ 'title' => 'Pricing Table', 'desc' => 'Compare plans with pricing tables.', 'cat' => 'layout' ],
			[ 'title' => 'FAQ', 'desc' => 'Frequently asked questions block.', 'cat' => 'layout' ],
			[ 'title' => 'Mega Menu', 'desc' => 'Build attractive mega menus with categories.', 'cat' => 'layout' ],
			[ 'title' => 'Image Gallery', 'desc' => 'Display images in galleries and lightboxes.', 'cat' => 'layout' ],
			[ 'title' => 'Call to Action', 'desc' => 'Create compelling CTA sections.', 'cat' => 'layout' ],
			[ 'title' => 'Dual Heading', 'desc' => 'Styled dual-line headings.', 'cat' => 'layout' ],
			[ 'title' => 'Flip Box', 'desc' => 'Animated flip box content.', 'cat' => 'layout' ],
			[ 'title' => 'Info Box', 'desc' => 'Info cards with icon and text.', 'cat' => 'layout' ],
			[ 'title' => 'Post Grid', 'desc' => 'Display posts in customizable grids.', 'cat' => 'layout' ],
			[ 'title' => 'Slider Revolution', 'desc' => 'Create stunning sliders and carousels.', 'cat' => 'layout' ],
		];
		$out = [];
		foreach ( $items as $i => $row ) {
			$out[] = [
				'title'         => $row['title'],
				'description'   => $row['desc'],
				'link'          => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
				'category_id'   => $row['cat'],
				'media_type'    => 'icon',
				'selected_icon' => [ 'value' => 'fas fa-th', 'library' => 'fa-solid' ],
				'badge'         => isset( $row['badge'] ) ? $row['badge'] : 'none',
				'is_featured'   => ! empty( $row['featured'] ) ? 'yes' : '',
			];
		}
		return $out;
	}

	/** Dummy submenu items for Layout 3 (Solutions). Thematic image by submenu name. */
	private function get_dummy_solution_submenu_items( $base_url, $img_base ) {
		$items = [
			[ 'title' => 'Enterprise Scale', 'desc' => 'Scale your infrastructure with confidence.', 'cat' => 'infrastructure', 'tag' => 'Popular' ],
			[ 'title' => 'Security First', 'desc' => 'Enterprise-grade security for your data.', 'cat' => 'security', 'tag' => 'NEW' ],
			[ 'title' => 'Advance Analytics', 'desc' => 'Make data-driven decisions with powerful analytics.', 'cat' => 'infrastructure' ],
			[ 'title' => 'AI Powered', 'desc' => 'Leverage AI to automate and optimize.', 'cat' => 'infrastructure', 'tag' => 'NEW' ],
			[ 'title' => 'Global CDN', 'desc' => 'Deliver content fast across the globe.', 'cat' => 'infrastructure' ],
			[ 'title' => 'Scalable Architecture', 'desc' => 'Build for growth from day one.', 'cat' => 'infrastructure' ],
			[ 'title' => 'Cloud Infrastructure', 'desc' => 'Reliable cloud hosting and services.', 'cat' => 'infrastructure' ],
			[ 'title' => 'Data Insights', 'desc' => 'Turn data into actionable insights.', 'cat' => 'infrastructure' ],
			[ 'title' => 'Compliance Ready', 'desc' => 'Meet regulatory requirements easily.', 'cat' => 'security' ],
			[ 'title' => '24/7 Monitoring', 'desc' => 'Round-the-clock monitoring and support.', 'cat' => 'infrastructure' ],
		];
		$out = [];
		foreach ( $items as $i => $row ) {
			$out[] = [
				'title'         => $row['title'],
				'description'   => $row['desc'],
				'link'          => [ 'url' => $base_url, 'is_external' => true, 'nofollow' => false ],
				'category_id'   => $row['cat'],
				'media_type'    => 'icon',
				'selected_icon' => [ 'value' => 'fas fa-th', 'library' => 'fa-solid' ],
				'badge'         => 'none',
				'tag_text'      => isset( $row['tag'] ) ? $row['tag'] : '',
			];
		}
		return $out;
	}

	/**
	 * Add Elementor repeater _id to each item (and nested repeater items) for editor.
	 *
	 * @param array $items
	 * @return array
	 */
	private function add_repeater_ids_deep( array $items ) {
		$out = [];
		foreach ( $items as $key => $value ) {
			if ( is_array( $value ) && isset( $value[0] ) && is_array( $value[0] ) ) {
				$out[ $key ] = [];
				foreach ( $value as $i => $sub ) {
					$sub = is_array( $sub ) ? $this->add_repeater_ids_deep( $sub ) : $sub;
					if ( is_array( $sub ) && ! isset( $sub['_id'] ) ) {
						$sub['_id'] = substr( wp_generate_uuid4(), 0, 7 );
					}
					$out[ $key ][] = $sub;
				}
			} elseif ( is_array( $value ) ) {
				$out[ $key ] = $this->add_repeater_ids_deep( $value );
			} else {
				$out[ $key ] = $value;
			}
		}
		if ( isset( $out[0] ) && is_array( $out[0] ) && ! isset( $items['_id'] ) ) {
			foreach ( $out as $i => $sub ) {
				if ( is_array( $sub ) && ! isset( $sub['_id'] ) ) {
					$out[ $i ]['_id'] = substr( wp_generate_uuid4(), 0, 7 );
				}
			}
		}
		return $out;
	}

	/**
	 * Return dummy menu items with _id for Elementor editor (Load dummy data button).
	 *
	 * @return array
	 */
	public static function get_dummy_menu_items_for_editor() {
		$widget = new self();
		return $widget->add_repeater_ids_deep( $widget->get_dummy_menu_items() );
	}

	/**
	 * AJAX handler: return dummy menu items for editor (Load dummy data button).
	 */
	public static function ajax_get_dummy_mega_menu() {
		if ( ! current_user_can( 'edit_posts' ) ) {
			wp_send_json_error( [ 'message' => 'Unauthorized' ] );
		}
		$nonce = isset( $_REQUEST['nonce'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['nonce'] ) ) : '';
		if ( ! wp_verify_nonce( $nonce, 'unlockafe_dummy_mega_menu' ) ) {
			wp_send_json_error( [ 'message' => 'Invalid nonce' ] );
		}
		wp_send_json_success( self::get_dummy_menu_items_for_editor() );
	}

	/**
	 * Get Advance Mega Menu widget settings from the document by widget element ID.
	 * Used by Advance Mega Sidebar to read menu data (menu_items or wp_menu_id) from the same page.
	 *
	 * @param int    $post_id   Post/template ID.
	 * @param string $widget_id Elementor widget element ID (e.g. the id shown in panel when widget is selected).
	 * @return array|null Settings array (menu_source, menu_items, wp_menu_id, ...) or null if not found.
	 */
	public static function get_settings_by_widget_id( $post_id, $widget_id ) {
		$post_id   = (int) $post_id;
		$widget_id = sanitize_text_field( (string) $widget_id );
		if ( ! $post_id ) {
			return null;
		}
		if ( ! class_exists( '\Elementor\Plugin' ) ) {
			return null;
		}
		$documents = \Elementor\Plugin::instance()->documents;
		$document  = is_preview() ? $documents->get_doc_or_auto_save( $post_id, get_current_user_id() ) : $documents->get( $post_id );
		if ( ! $document ) {
			return null;
		}
		$elements_data = $document->get_elements_data();
		if ( ! is_array( $elements_data ) ) {
			return null;
		}
		$found = self::find_widget_settings_in_elements( $elements_data, $widget_id );
		return $found;
	}

	/**
	 * Recursively find Advance Mega Menu widget by id and return its settings.
	 *
	 * @param array  $elements_data Elements tree from get_elements_data().
	 * @param string $widget_id     Element ID to find.
	 * @return array|null Settings or null.
	 */
	private static function find_widget_settings_in_elements( array $elements_data, $widget_id ) {
		foreach ( $elements_data as $element ) {
			if ( ! is_array( $element ) ) {
				continue;
			}
			$id          = isset( $element['id'] ) ? $element['id'] : '';
			$widget_type = isset( $element['widgetType'] ) ? $element['widgetType'] : '';
			if ( $widget_type === 'unlockafe-advance-mega-menu' && ( $widget_id === '' || (string) $id === (string) $widget_id ) ) {
				return isset( $element['settings'] ) && is_array( $element['settings'] ) ? $element['settings'] : null;
			}
			if ( ! empty( $element['elements'] ) && is_array( $element['elements'] ) ) {
				$found = self::find_widget_settings_in_elements( $element['elements'], $widget_id );
				if ( $found !== null ) {
					return $found;
				}
			}
		}
		return null;
	}

	protected function register_style_controls() {
		$this->start_controls_section( 'section_style_nav', [
			'label' => __( 'Main Menu', 'unlock-addons-for-elementor' ),
			'tab'   => Controls_Manager::TAB_STYLE,
		] );
		$this->add_group_control( Group_Control_Typography::get_type(), [
			'name'     => 'nav_typography',
			'selector' => '{{WRAPPER}} .unlockafe-amm-nav__link',
		] );
		$this->add_control( 'nav_color', [
			'label'     => __( 'Text Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-nav__link' => 'color: {{VALUE}};' ],
		] );
		$this->add_control( 'nav_color_active', [
			'label'     => __( 'Active / Hover Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-nav__link:hover, {{WRAPPER}} .unlockafe-amm-nav__item.is-open .unlockafe-amm-nav__link, {{WRAPPER}} .unlockafe-amm-nav__link.is-active' => 'color: {{VALUE}};' ],
		] );
		$this->add_control( 'nav_underline_color', [
			'label'     => __( 'Active Underline Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [
				'{{WRAPPER}} .unlockafe-amm-nav__link:hover::after, {{WRAPPER}} .unlockafe-amm-nav__item.is-open .unlockafe-amm-nav__link::after, {{WRAPPER}} .unlockafe-amm-nav__link.is-active::after' => 'background: {{VALUE}};',
			],
		] );
		$this->add_control( 'nav_arrow_heading', [
			'label'     => __( 'Arrow (beside menu)', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::HEADING,
			'separator' => 'before',
		] );
		$this->add_control( 'nav_arrow_color', [
			'label'     => __( 'Arrow Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-nav__arrow' => 'border-color: {{VALUE}};' ],
		] );
		$this->add_control( 'nav_arrow_color_open', [
			'label'     => __( 'Arrow Color (open/hover)', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-nav__item.is-open .unlockafe-amm-nav__arrow, {{WRAPPER}} .unlockafe-amm-nav__item:hover .unlockafe-amm-nav__arrow' => 'border-color: {{VALUE}};' ],
		] );
		$this->add_control( 'nav_arrow_size', [
			'label'     => __( 'Arrow Size', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::SLIDER,
			'size_units' => [ 'px' ],
			'range'     => [ 'px' => [ 'min' => 4, 'max' => 24 ] ],
			'default'   => [ 'size' => 9 ],
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-nav__arrow' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};' ],
		] );
		$this->end_controls_section();

		// ——— Mobile & Small Device (responsive: tablet + mobile) ———
		$this->start_controls_section( 'section_style_mobile', [
			'label' => __( 'Mobile & Small Device', 'unlock-addons-for-elementor' ),
			'tab'   => Controls_Manager::TAB_STYLE,
		] );
		$this->add_responsive_control( 'mobile_nav_font_size', [
			'label'      => __( 'Menu Link Size', 'unlock-addons-for-elementor' ),
			'type'       => Controls_Manager::SLIDER,
			'size_units' => [ 'px', 'em', 'rem' ],
			'range'      => [
				'px'  => [ 'min' => 10, 'max' => 32 ],
				'em'  => [ 'min' => 0.6, 'max' => 2, 'step' => 0.1 ],
				'rem' => [ 'min' => 0.6, 'max' => 2, 'step' => 0.1 ],
			],
			'selectors'  => [
				'{{WRAPPER}} .unlockafe-amm-nav__link' => 'font-size: {{SIZE}}{{UNIT}};',
			],
		] );
		$this->add_responsive_control( 'mobile_nav_padding', [
			'label'      => __( 'Menu Link Padding', 'unlock-addons-for-elementor' ),
			'type'       => Controls_Manager::DIMENSIONS,
			'size_units' => [ 'px', 'em', '%' ],
			'selectors'  => [
				'{{WRAPPER}} .unlockafe-amm-nav__link' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			],
		] );
		$this->add_responsive_control( 'mobile_nav_margin', [
			'label'      => __( 'Menu Item Margin', 'unlock-addons-for-elementor' ),
			'type'       => Controls_Manager::DIMENSIONS,
			'size_units' => [ 'px', 'em', '%' ],
			'selectors'  => [
				'{{WRAPPER}} .unlockafe-amm-nav__item' => 'margin: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			],
		] );
		$this->end_controls_section();

		// Style: Logo – hidden; uncomment to restore.
		/*
		$this->start_controls_section( 'section_style_logo', [
			'label' => __( 'Logo', 'unlock-addons-for-elementor' ),
			'tab'   => Controls_Manager::TAB_STYLE,
		] );
		$this->add_control( 'logo_size', [
			'label'      => __( 'Height (px)', 'unlock-addons-for-elementor' ),
			'type'       => Controls_Manager::SLIDER,
			'size_units' => [ 'px' ],
			'range'      => [ 'px' => [ 'min' => 16, 'max' => 80 ] ],
			'default'    => [ 'unit' => 'px', 'size' => 40 ],
			'selectors'  => [ '{{WRAPPER}} .unlockafe-amm-logo__img' => 'max-height: {{SIZE}}{{UNIT}};' ],
		] );
		$this->end_controls_section();
		*/

		// Style: Header Button – hidden; uncomment to restore.
		/*
		$this->start_controls_section( 'section_style_header_button', [
			'label' => __( 'Header Button', 'unlock-addons-for-elementor' ),
			'tab'   => Controls_Manager::TAB_STYLE,
		] );
		$this->add_group_control( Group_Control_Typography::get_type(), [
			'name'     => 'header_button_typography',
			'selector' => '{{WRAPPER}} .unlockafe-amm-btn',
		] );
		$this->add_group_control( Group_Control_Background::get_type(), [
			'name'     => 'header_button_bg',
			'types'    => [ 'classic', 'gradient' ],
			'selector' => '{{WRAPPER}} .unlockafe-amm-btn',
		] );
		$this->add_control( 'header_button_color', [
			'label'     => __( 'Text Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'default'   => '#ffffff',
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-btn' => 'color: {{VALUE}} !important;' ],
		] );
		$this->add_control( 'header_button_hover_color', [
			'label'     => __( 'Hover Text Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-btn:hover' => 'color: {{VALUE}} !important;' ],
		] );
		$this->end_controls_section();
		*/

		$this->start_controls_section( 'section_style_mega_panel', [
			'label' => __( 'Mega Menu Panel', 'unlock-addons-for-elementor' ),
			'tab'   => Controls_Manager::TAB_STYLE,
		] );
		$this->add_control( 'mega_panel_bg', [
			'label'     => __( 'Background Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'default'   => '#ffffff',
			'selectors' => [
				'{{WRAPPER}} .unlockafe-amm-panel' => 'background-color: {{VALUE}};',
				'{{WRAPPER}} .unlockafe-amm-dropdown' => 'background-color: {{VALUE}};',
			],
		] );
		$this->add_control( 'mega_animation_heading', [
			'label' => __( 'Animation', 'unlock-addons-for-elementor' ),
			'type'  => Controls_Manager::HEADING,
			'separator' => 'before',
		] );
		$this->add_control( 'mega_animation_type', [
			'label'   => __( 'Animation Type', 'unlock-addons-for-elementor' ),
			'type'    => Controls_Manager::SELECT,
			'default' => 'slide-down',
			'options' => [
				'none'         => __( 'None', 'unlock-addons-for-elementor' ),
				'fade'         => __( 'Fade', 'unlock-addons-for-elementor' ),
				'slide-down'   => __( 'Slide Down', 'unlock-addons-for-elementor' ),
				'slide-up'     => __( 'Slide Up', 'unlock-addons-for-elementor' ),
				'zoom-in'      => __( 'Zoom In', 'unlock-addons-for-elementor' ),
			],
		] );
		$this->add_control( 'mega_animation_duration', [
			'label'   => __( 'Duration (ms)', 'unlock-addons-for-elementor' ),
			'type'    => Controls_Manager::SLIDER,
			'range'   => [
				'px' => [ 'min' => 100, 'max' => 600, 'step' => 10 ],
			],
			'default' => [ 'size' => 220 ],
		] );
		$this->end_controls_section();

		// Panel header (title + count) – all layouts
		$this->start_controls_section( 'section_style_panel_header', [
			'label' => __( 'Panel Header', 'unlock-addons-for-elementor' ),
			'tab'   => Controls_Manager::TAB_STYLE,
		] );
		$this->add_control( 'panel_title_heading', [
			'label' => __( 'Title', 'unlock-addons-for-elementor' ),
			'type'  => Controls_Manager::HEADING,
		] );
		$this->add_group_control( Group_Control_Typography::get_type(), [
			'name'     => 'panel_title_typography',
			'selector' => '{{WRAPPER}} .unlockafe-amm-panel__title',
		] );
		$this->add_control( 'panel_title_color', [
			'label'     => __( 'Title Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-panel__title' => 'color: {{VALUE}};' ],
		] );
		$this->add_control( 'panel_count_heading', [
			'label'     => __( 'Count / Subtitle', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::HEADING,
			'separator' => 'before',
		] );
		$this->add_group_control( Group_Control_Typography::get_type(), [
			'name'     => 'panel_count_typography',
			'selector' => '{{WRAPPER}} .unlockafe-amm-panel__count',
		] );
		$this->add_control( 'panel_count_color', [
			'label'     => __( 'Count Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-panel__count' => 'color: {{VALUE}};' ],
		] );
		$this->end_controls_section();

		// Category tabs / pills – all layouts
		$this->start_controls_section( 'section_style_tabs', [
			'label' => __( 'Category Tabs', 'unlock-addons-for-elementor' ),
			'tab'   => Controls_Manager::TAB_STYLE,
		] );
		$this->add_group_control( Group_Control_Typography::get_type(), [
			'name'     => 'tabs_typography',
			'selector' => '{{WRAPPER}} .unlockafe-amm-tab',
		] );
		$this->add_control( 'tabs_color', [
			'label'     => __( 'Text Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-tab' => 'color: {{VALUE}};' ],
		] );
		$this->add_control( 'tabs_bg', [
			'label'     => __( 'Background Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-tab' => 'background-color: {{VALUE}};' ],
		] );
		$this->add_control( 'tabs_active_color', [
			'label'     => __( 'Active Text Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-tab.is-active' => 'color: {{VALUE}};' ],
		] );
		$this->add_control( 'tabs_active_bg', [
			'label'     => __( 'Active Background', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-tab.is-active' => 'background-color: {{VALUE}}; border-color: {{VALUE}};' ],
		] );
		$this->end_controls_section();

		// Mega Menu's Submenu – cards/items + View Details button design
		$this->start_controls_section( 'section_style_cards', [
			'label' => __( "Mega Menu's Submenu", 'unlock-addons-for-elementor' ),
			'tab'   => Controls_Manager::TAB_STYLE,
		] );
		$this->add_control( 'card_title_heading', [
			'label' => __( 'Card / Item Title', 'unlock-addons-for-elementor' ),
			'type'  => Controls_Manager::HEADING,
		] );
		$this->add_group_control( Group_Control_Typography::get_type(), [
			'name'     => 'card_title_typography',
			'selector' => '{{WRAPPER}} .unlockafe-amm-card__title, {{WRAPPER}} .unlockafe-amm-sol-card__title, {{WRAPPER}} .unlockafe-amm-item__title',
		] );
		$this->add_control( 'card_title_color', [
			'label'     => __( 'Title Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [
				'{{WRAPPER}} .unlockafe-amm-card__title' => 'color: {{VALUE}};',
				'{{WRAPPER}} .unlockafe-amm-sol-card__title' => 'color: {{VALUE}};',
				'{{WRAPPER}} .unlockafe-amm-item__title' => 'color: {{VALUE}};',
			],
		] );
		$this->add_control( 'card_desc_heading', [
			'label'     => __( 'Description', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::HEADING,
			'separator' => 'before',
		] );
		$this->add_group_control( Group_Control_Typography::get_type(), [
			'name'     => 'card_desc_typography',
			'selector' => '{{WRAPPER}} .unlockafe-amm-card__desc, {{WRAPPER}} .unlockafe-amm-sol-card__desc, {{WRAPPER}} .unlockafe-amm-item__desc',
		] );
		$this->add_control( 'card_desc_color', [
			'label'     => __( 'Description Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [
				'{{WRAPPER}} .unlockafe-amm-card__desc' => 'color: {{VALUE}};',
				'{{WRAPPER}} .unlockafe-amm-sol-card__desc' => 'color: {{VALUE}};',
				'{{WRAPPER}} .unlockafe-amm-item__desc' => 'color: {{VALUE}};',
			],
		] );
		$this->add_control( 'solutions_tag_heading', [
			'label'     => __( 'Solutions Tag (Popular / NEW badge)', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::HEADING,
			'separator' => 'before',
		] );
		$this->add_control( 'solutions_tag_bg', [
			'label'     => __( 'Tag Background Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'default'   => '',
			'selectors' => [
				'{{WRAPPER}} .unlockafe-amm-sol-card__tag' => 'background: {{VALUE}};',
			],
		] );
		$this->add_control( 'solutions_tag_color', [
			'label'     => __( 'Tag Text Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'default'   => '',
			'selectors' => [
				'{{WRAPPER}} .unlockafe-amm-sol-card__tag' => 'color: {{VALUE}};',
			],
		] );
		$this->add_control( 'submenu_btn_heading', [
			'label'     => __( 'Button (View Details)', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::HEADING,
			'separator' => 'before',
		] );
		$this->add_control( 'submenu_btn_color', [
			'label'     => __( 'Button Text Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-featured__btn' => 'color: {{VALUE}} !important;' ],
		] );
		$this->add_control( 'submenu_btn_bg', [
			'label'     => __( 'Button Background Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-featured__btn' => 'background-color: {{VALUE}};' ],
		] );
		$this->add_control( 'submenu_btn_radius', [
			'label'      => __( 'Button Border Radius', 'unlock-addons-for-elementor' ),
			'type'       => Controls_Manager::DIMENSIONS,
			'size_units' => [ 'px', '%', 'em' ],
			'selectors'  => [ '{{WRAPPER}} .unlockafe-amm-featured__btn' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};' ],
		] );
		$this->end_controls_section();

		// Bottom of Mega Menu (CTA bar) – all layouts
		$this->start_controls_section( 'section_style_cta', [
			'label' => __( 'Bottom of Mega Menu', 'unlock-addons-for-elementor' ),
			'tab'   => Controls_Manager::TAB_STYLE,
		] );
		$this->add_group_control( Group_Control_Background::get_type(), [
			'name'     => 'cta_bg',
			'types'    => [ 'classic', 'gradient' ],
			'selector' => '{{WRAPPER}} .unlockafe-amm-cta',
		] );
		$this->add_control( 'cta_text_heading', [
			'label'     => __( 'Title Text', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::HEADING,
			'separator' => 'before',
		] );
		$this->add_group_control( Group_Control_Typography::get_type(), [
			'name'     => 'cta_text_typography',
			'selector' => '{{WRAPPER}} .unlockafe-amm-cta__text',
		] );
		$this->add_control( 'cta_text_color', [
			'label'     => __( 'Title Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-cta__text' => 'color: {{VALUE}};' ],
		] );
		$this->add_control( 'cta_subtext_heading', [
			'label'     => __( 'Subtext', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::HEADING,
			'separator' => 'before',
		] );
		$this->add_group_control( Group_Control_Typography::get_type(), [
			'name'     => 'cta_subtext_typography',
			'selector' => '{{WRAPPER}} .unlockafe-amm-cta__subtext',
		] );
		$this->add_control( 'cta_subtext_color', [
			'label'     => __( 'Subtext Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-cta__subtext' => 'color: {{VALUE}};' ],
		] );
		$this->add_control( 'cta_btn_heading', [
			'label'     => __( 'Button', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::HEADING,
			'separator' => 'before',
		] );
		$this->add_group_control( Group_Control_Typography::get_type(), [
			'name'     => 'cta_btn_typography',
			'selector' => '{{WRAPPER}} .unlockafe-amm-cta__btn',
		] );
		$this->add_control( 'cta_btn_bg', [
			'label'     => __( 'Button Background', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-cta__btn' => 'background-color: {{VALUE}};' ],
		] );
		$this->add_control( 'cta_btn_color', [
			'label'     => __( 'Button Text Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-cta__btn' => 'color: {{VALUE}} !important;' ],
		] );
		$this->add_control( 'cta_btn_hover_bg', [
			'label'     => __( 'Button Hover Background', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-cta__btn:hover' => 'background-color: {{VALUE}};' ],
		] );
		$this->add_control( 'cta_btn_hover_color', [
			'label'     => __( 'Button Hover Text Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-cta__btn:hover' => 'color: {{VALUE}} !important;' ],
		] );
		$this->end_controls_section();

		// Dropdown's Sub menu (simple dropdown menu links)
		$this->start_controls_section( 'section_style_dropdown', [
			'label' => __( "Dropdown's Sub menu", 'unlock-addons-for-elementor' ),
			'tab'   => Controls_Manager::TAB_STYLE,
		] );
		$this->add_group_control( Group_Control_Typography::get_type(), [
			'name'     => 'dropdown_typography',
			'selector' => '{{WRAPPER}} .unlockafe-amm-dropdown__link',
		] );
		$this->add_control( 'dropdown_color', [
			'label'     => __( 'Text Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-dropdown__link' => 'color: {{VALUE}};' ],
		] );
		$this->add_control( 'dropdown_hover_color', [
			'label'     => __( 'Hover Color', 'unlock-addons-for-elementor' ),
			'type'      => Controls_Manager::COLOR,
			'selectors' => [ '{{WRAPPER}} .unlockafe-amm-dropdown__link:hover' => 'color: {{VALUE}};' ],
		] );
		$this->add_control( 'dropdown_animation_heading', [
			'label' => __( 'Animation', 'unlock-addons-for-elementor' ),
			'type'  => Controls_Manager::HEADING,
			'separator' => 'before',
		] );
		$this->add_control( 'dropdown_animation_type', [
			'label'   => __( 'Animation Type', 'unlock-addons-for-elementor' ),
			'type'    => Controls_Manager::SELECT,
			'default' => 'slide-down',
			'options' => [
				'none'         => __( 'None', 'unlock-addons-for-elementor' ),
				'fade'         => __( 'Fade', 'unlock-addons-for-elementor' ),
				'slide-down'   => __( 'Slide Down', 'unlock-addons-for-elementor' ),
				'slide-up'     => __( 'Slide Up', 'unlock-addons-for-elementor' ),
				'zoom-in'      => __( 'Zoom In', 'unlock-addons-for-elementor' ),
			],
		] );
		$this->add_control( 'dropdown_animation_duration', [
			'label'   => __( 'Duration (ms)', 'unlock-addons-for-elementor' ),
			'type'    => Controls_Manager::SLIDER,
			'range'   => [
				'px' => [ 'min' => 100, 'max' => 600, 'step' => 10 ],
			],
			'default' => [ 'size' => 220 ],
		] );
		$this->add_control( 'dropdown_width', [
			'label'      => __( 'Panel Min Width', 'unlock-addons-for-elementor' ),
			'type'       => Controls_Manager::SLIDER,
			'size_units' => [ 'px' ],
			'range'      => [
				'px' => [
					'min' => 120,
					'max' => 600,
				],
			],
			'selectors'  => [
				'{{WRAPPER}} .unlockafe-amm-dropdown' => 'min-width: {{SIZE}}{{UNIT}};',
			],
		] );
		$this->add_control( 'dropdown_min_height', [
			'label'      => __( 'Panel Min Height', 'unlock-addons-for-elementor' ),
			'type'       => Controls_Manager::SLIDER,
			'size_units' => [ 'px' ],
			'range'      => [
				'px' => [
					'min' => 0,
					'max' => 500,
				],
			],
			'selectors'  => [
				'{{WRAPPER}} .unlockafe-amm-dropdown' => 'min-height: {{SIZE}}{{UNIT}};',
			],
		] );
		$this->add_control( 'dropdown_panel_padding', [
			'label'      => __( 'Panel Padding', 'unlock-addons-for-elementor' ),
			'type'       => Controls_Manager::DIMENSIONS,
			'size_units' => [ 'px', 'em', '%' ],
			'selectors'  => [
				'{{WRAPPER}} .unlockafe-amm-dropdown' => 'padding: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			],
		] );
		$this->add_group_control( Group_Control_Border::get_type(), [
			'name'     => 'dropdown_panel_border',
			'selector' => '{{WRAPPER}} .unlockafe-amm-dropdown',
		] );
		$this->add_control( 'dropdown_panel_radius', [
			'label'      => __( 'Panel Border Radius', 'unlock-addons-for-elementor' ),
			'type'       => Controls_Manager::DIMENSIONS,
			'size_units' => [ 'px', '%' ],
			'selectors'  => [
				'{{WRAPPER}} .unlockafe-amm-dropdown' => 'border-radius: {{TOP}}{{UNIT}} {{RIGHT}}{{UNIT}} {{BOTTOM}}{{UNIT}} {{LEFT}}{{UNIT}};',
			],
		] );
		$this->end_controls_section();
	}

	/**
	 * Get animation duration in milliseconds from Elementor slider/number value.
	 *
	 * @param mixed $value Control value (array with 'size' or numeric).
	 * @return int Duration in ms (100–600), default 220.
	 */
	private function get_animation_duration_ms( $value ) {
		if ( is_numeric( $value ) ) {
			return max( 100, min( 600, (int) $value ) );
		}
		if ( is_array( $value ) && isset( $value['size'] ) && is_numeric( $value['size'] ) ) {
			return max( 100, min( 600, (int) $value['size'] ) );
		}
		return 220;
	}

	protected function render() {
		$settings = $this->get_settings_for_display();
		// When Z-Index is empty, apply default 15 so mega menu stays above theme header.
		$z_index = $settings['_z_index'] ?? '';
		if ( $z_index === '' || $z_index === null ) {
			$this->add_render_attribute( '_wrapper', 'style', 'z-index: 15' );
		}
		$menu_source = $settings['menu_source'] ?? 'repeater';
		$trigger  = $settings['trigger'] ?? 'hover';
		$widget_id = $this->get_id();
		// Logo & Get Started button hidden – uncomment next 2 lines to restore from settings.
		// $show_logo  = ! empty( $settings['show_logo'] ) && $settings['show_logo'] === 'yes';
		// $show_button = ! empty( $settings['show_button'] ) && $settings['show_button'] === 'yes';
		$show_logo  = false;
		$show_button = false;
		$gap_logo_menu   = isset( $settings['gap_logo_menu'] ) ? (int) $settings['gap_logo_menu'] : 24;
		$gap_menu_items  = isset( $settings['gap_menu_items'] ) ? (int) $settings['gap_menu_items'] : 24;
		$gap_menu_button = isset( $settings['gap_menu_button'] ) ? (int) $settings['gap_menu_button'] : 24;

		if ( $menu_source === 'wp_menu' ) {
			$this->render_wp_menu( $settings, $trigger, $widget_id, $show_logo, $show_button, $gap_logo_menu, $gap_menu_items, $gap_menu_button );
			return;
		}

		// Always use Menu Items repeater only – so any change (menu, submenu, category) in the panel is reflected in the menu.
		$menus = ( $menu_source === 'repeater' ) ? ( $settings['menu_items'] ?? [] ) : [];
		$dropdown_anim = $settings['dropdown_animation_type'] ?? 'slide-down';
		$mega_anim     = $settings['mega_animation_type'] ?? 'slide-down';
		$dropdown_dur  = $this->get_animation_duration_ms( $settings['dropdown_animation_duration'] ?? null );
		$mega_dur      = $this->get_animation_duration_ms( $settings['mega_animation_duration'] ?? null );
		$nav_style     = '--amm-dropdown-duration: ' . $dropdown_dur . 'ms; --amm-mega-duration: ' . $mega_dur . 'ms;';
		?>
		<nav class="unlockafe-amm unlockafe-amm--trigger-<?php echo esc_attr( $trigger ); ?>" data-widget-id="<?php echo esc_attr( $widget_id ); ?>" data-dropdown-animation="<?php echo esc_attr( $dropdown_anim ); ?>" data-mega-animation="<?php echo esc_attr( $mega_anim ); ?>" style="<?php echo esc_attr( $nav_style ); ?>">
			<div class="unlockafe-amm__inner">
				<?php
				// Logo hidden – uncomment to restore.
				// if ( $show_logo ) {
				// 	$this->render_logo( $settings, $gap_logo_menu );
				// }
				?>
				<button class="unlockafe-amm-toggle" type="button" aria-label="<?php esc_attr_e( 'Toggle menu', 'unlock-addons-for-elementor' ); ?>" aria-expanded="false">
					<span class="unlockafe-amm-toggle__bar"></span>
					<span class="unlockafe-amm-toggle__bar"></span>
					<span class="unlockafe-amm-toggle__bar"></span>
				</button>
				<ul class="unlockafe-amm-nav" style="gap: <?php echo (int) $gap_menu_items; ?>px;">
					<?php
					foreach ( $menus as $index => $menu ) {
						$this->render_menu_item( $menu, $index, $widget_id );
					}
					?>
				</ul>
				<?php
				// Get Started button hidden – uncomment to restore.
				// if ( $show_button ) {
				// 	$this->render_header_button( $settings, $gap_menu_button );
				// }
				?>
			</div>
		</nav>
		<?php
	}

	/**
	 * Render when menu source is WordPress menu (with optional mega template from Appearance → Menus).
	 */
	private function render_wp_menu( array $settings, $trigger, $widget_id, $show_logo, $show_button, $gap_logo_menu, $gap_menu_items, $gap_menu_button ) {
		$wp_menu_id = (int) ( $settings['wp_menu_id'] ?? 0 );
		if ( ! $wp_menu_id ) {
			echo '<p class="unlockafe-amm-empty">' . esc_html__( 'Select a WordPress menu in the widget settings.', 'unlock-addons-for-elementor' ) . '</p>';
			return;
		}
		$menu_items = wp_get_nav_menu_items( $wp_menu_id );
		if ( ! $menu_items || ! is_array( $menu_items ) ) {
			echo '<p class="unlockafe-amm-empty">' . esc_html__( 'No menu items found.', 'unlock-addons-for-elementor' ) . '</p>';
			return;
		}
		$mega = Mega_Menu_Nav_Settings::get_settings_for_menu( $wp_menu_id );
		$top_level = array_filter( $menu_items, function ( $item ) { return (int) $item->menu_item_parent === 0; } );
		$top_level = array_values( $top_level );
		$dropdown_anim = $settings['dropdown_animation_type'] ?? 'slide-down';
		$mega_anim     = $settings['mega_animation_type'] ?? 'slide-down';
		$dropdown_dur  = $this->get_animation_duration_ms( $settings['dropdown_animation_duration'] ?? null );
		$mega_dur      = $this->get_animation_duration_ms( $settings['mega_animation_duration'] ?? null );
		$nav_style     = '--amm-dropdown-duration: ' . $dropdown_dur . 'ms; --amm-mega-duration: ' . $mega_dur . 'ms;';
		?>
		<nav class="unlockafe-amm unlockafe-amm--trigger-<?php echo esc_attr( $trigger ); ?>" data-widget-id="<?php echo esc_attr( $widget_id ); ?>" data-dropdown-animation="<?php echo esc_attr( $dropdown_anim ); ?>" data-mega-animation="<?php echo esc_attr( $mega_anim ); ?>" style="<?php echo esc_attr( $nav_style ); ?>">
			<div class="unlockafe-amm__inner">
				<?php
				// Logo hidden – uncomment to restore.
				// if ( $show_logo ) {
				// 	$this->render_logo( $settings, $gap_logo_menu );
				// }
				?>
				<button class="unlockafe-amm-toggle" type="button" aria-label="<?php esc_attr_e( 'Toggle menu', 'unlock-addons-for-elementor' ); ?>" aria-expanded="false">
					<span class="unlockafe-amm-toggle__bar"></span>
					<span class="unlockafe-amm-toggle__bar"></span>
					<span class="unlockafe-amm-toggle__bar"></span>
				</button>
				<ul class="unlockafe-amm-nav" style="gap: <?php echo (int) $gap_menu_items; ?>px;">
					<?php
					foreach ( $top_level as $index => $item ) {
						$item_tpl = Mega_Menu_Nav_Settings::get_item_template( $item->ID );
						// Only show mega panel when this item has a template chosen (per-item, not menu-level).
						$use_tpl = ( $mega['enabled'] && $item_tpl !== null && $item_tpl['template'] !== '' ) ? $item_tpl : null;
						$item_class = 'unlockafe-amm-nav__item unlockafe-amm-nav__item--simple';
						if ( $use_tpl !== null ) {
							$item_class = 'unlockafe-amm-nav__item unlockafe-amm-nav__item--mega';
							$layout = ( $use_tpl['template'] === 'custom' ) ? 'custom' : Mega_Menu_Nav_Settings::get_layout_for_template( $use_tpl['template'] );
							$item_class .= ' unlockafe-amm-nav__item--' . esc_attr( $layout );
						}
						$item_class .= ' unlockafe-amm-nav__item--wp-id-' . (int) $item->ID;
						$target = ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) . '"' : '';
						$rel    = ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) . '"' : '';
						?>
						<li class="<?php echo esc_attr( $item_class ); ?>" data-menu-type="<?php echo ( $use_tpl !== null ) ? 'mega' : 'simple'; ?>">
							<a href="<?php echo esc_url( $item->url ); ?>" class="unlockafe-amm-nav__link"<?php echo $target . $rel; ?>><?php echo esc_html( $item->title ); ?></a>
							<?php
							if ( $use_tpl !== null ) {
								$panel_id = 'unlockafe-amm-panel-' . $widget_id . '-wp-' . $index;
								echo '<div id="' . esc_attr( $panel_id ) . '" class="unlockafe-amm-panel unlockafe-amm-panel--' . esc_attr( $layout ) . '">';
								$this->render_panel_for_template( $use_tpl['template'], (int) ( $use_tpl['custom_page_id'] ?? 0 ), $panel_id );
								echo '</div>';
							}
							?>
						</li>
					<?php } ?>
				</ul>
				<?php
				// Get Started button hidden – uncomment to restore.
				// if ( $show_button ) {
				// 	$this->render_header_button( $settings, $gap_menu_button );
				// }
				?>
			</div>
		</nav>
		<?php
	}

	/**
	 * Output panel content for a template key (template_1 … template_5) or custom page.
	 */
	private function render_panel_for_template( $template, $custom_page_id, $panel_id ) {
		$template = is_string( $template ) ? trim( $template ) : '';
		if ( $template === 'custom' && $custom_page_id > 0 && class_exists( '\Elementor\Plugin' ) ) {
			$content = \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $custom_page_id );
			echo '<div class="unlockafe-amm-panel__inner unlockafe-amm-panel--custom-inner">';
			echo $content;
			echo '</div>';
			return;
		}
		// Ensure template_1 … template_5 are valid so layout (blocks/features/etc.) always runs
		$valid = [ 'template_1', 'template_2', 'template_3', 'template_4', 'template_5' ];
		if ( ! in_array( $template, $valid, true ) ) {
			$template = 'template_1';
		}
		$layout = Mega_Menu_Nav_Settings::get_layout_for_template( $template );
		$menu   = $this->get_default_menu_for_layout( $layout );
		$this->render_panel( $menu, $layout, $panel_id );
	}

	/**
	 * Minimal menu array for a layout when using WP menu + template (no repeater data).
	 */
	private function get_default_menu_for_layout( $layout ) {
		$defaults = [
			'panel_title' => '',
			'categories'  => [],
			'submenu_items' => [],
			'cta_show'    => 'no',
			'posts_limit' => 9,
			'pages_limit' => 9,
		];
		if ( $layout === 'blocks' ) {
			$defaults['panel_title'] = __( 'Browse by Category', 'unlock-addons-for-elementor' );
		} elseif ( $layout === 'features' ) {
			$defaults['panel_title'] = __( 'Discover Our Features', 'unlock-addons-for-elementor' );
		} elseif ( $layout === 'solutions' ) {
			$defaults['panel_title'] = __( 'Enterprise Solutions', 'unlock-addons-for-elementor' );
		}
		return $defaults;
	}

	private function render_logo( array $settings, int $margin_right ) {
		$img_url = $settings['logo_image']['url'] ?? '';
		if ( $img_url === '' ) {
			return;
		}
		$logo_url = $settings['logo_link']['url'] ?? '';
		$logo_ext = ! empty( $settings['logo_link']['is_external'] );
		$logo_nof = ! empty( $settings['logo_link']['nofollow'] );
		$logo_rel = array_filter( [ $logo_ext ? 'noopener noreferrer' : '', $logo_nof ? 'nofollow' : '' ] );
		$style    = 'margin-right: ' . (int) $margin_right . 'px;';
		$content  = '<img src="' . esc_url( $img_url ) . '" alt="' . esc_attr( get_bloginfo( 'name' ) ) . '" class="unlockafe-amm-logo__img" />';
		echo '<div class="unlockafe-amm-logo" style="' . esc_attr( $style ) . '">';
		if ( $logo_url !== '' ) {
			echo '<a href="' . esc_url( $logo_url ) . '" class="unlockafe-amm-logo__link"' . ( $logo_ext ? ' target="_blank"' : '' ) . ( ! empty( $logo_rel ) ? ' rel="' . esc_attr( implode( ' ', $logo_rel ) ) . '"' : '' ) . '>' . $content . '</a>';
		} else {
			echo '<span class="unlockafe-amm-logo__wrap">' . $content . '</span>';
		}
		echo '</div>';
	}

	private function render_header_button( array $settings, int $margin_left ) {
		$text = $settings['button_text'] ?? __( 'Get Started', 'unlock-addons-for-elementor' );
		$url  = $settings['button_link']['url'] ?? '#';
		$ext  = ! empty( $settings['button_link']['is_external'] );
		$nof  = ! empty( $settings['button_link']['nofollow'] );
		$rel  = array_filter( [ $ext ? 'noopener noreferrer' : '', $nof ? 'nofollow' : '' ] );
		echo '<a href="' . esc_url( $url ) . '" class="unlockafe-amm-btn"' . ( $ext ? ' target="_blank"' : '' ) . ( ! empty( $rel ) ? ' rel="' . esc_attr( implode( ' ', $rel ) ) . '"' : '' ) . ' style="margin-left: ' . (int) $margin_left . 'px;">' . esc_html( $text ) . '</a>';
	}

	private function render_menu_item( array $menu, int $index, string $widget_id ) {
		$menu_type = $menu['menu_type'] ?? 'simple';
		$label     = $menu['menu_label'] ?? __( 'Menu', 'unlock-addons-for-elementor' );
		$link      = isset( $menu['menu_link']['url'] ) ? trim( (string) $menu['menu_link']['url'] ) : '';
		$external  = ! empty( $menu['menu_link']['is_external'] );
		$nofollow  = ! empty( $menu['menu_link']['nofollow'] );
		$rel       = array_filter( [ $external ? 'noopener noreferrer' : '', $nofollow ? 'nofollow' : '' ] );
		$rel_attr  = ! empty( $rel ) ? ' rel="' . esc_attr( implode( ' ', $rel ) ) . '"' : '';

		// Build href so that:
		// - No target="_blank" is ever forced for top-level menu items
		// - If link is empty, clicking should not navigate anywhere
		if ( $link !== '' ) {
			$href_attr = ' href="' . esc_url( $link ) . '"';
		} else {
			// No URL: keep it focusable for accessibility but prevent navigation
			$href_attr = ' href="#" onclick="return false;"';
		}

		$item_class = 'unlockafe-amm-nav__item unlockafe-amm-nav__item--' . esc_attr( $menu_type );
		if ( $menu_type === 'mega' ) {
			$layout = $menu['menu_layout'] ?? 'blocks';
			$item_class .= ' unlockafe-amm-nav__item--' . esc_attr( $layout );
		}
		if ( ! empty( $menu['_id'] ) ) {
			$item_class .= ' elementor-repeater-item-' . $menu['_id'];
		}
		$show_arrow = ( $menu_type === 'dropdown' || $menu_type === 'mega' ) && ( $menu['show_toggle_arrow'] ?? '' ) === 'yes';
		?>
		<li class="<?php echo esc_attr( $item_class ); ?>" data-menu-type="<?php echo esc_attr( $menu_type ); ?>">
			<a<?php echo $href_attr; ?> class="unlockafe-amm-nav__link"<?php echo $rel_attr; ?>>
				<span class="unlockafe-amm-nav__text"><?php echo esc_html( $label ); ?></span>
				<?php if ( $show_arrow ) : ?>
					<span class="unlockafe-amm-nav__arrow" aria-hidden="true"></span>
				<?php endif; ?>
			</a>
			<?php
			if ( $menu_type === 'dropdown' ) {
				$this->render_dropdown( $menu );
			} elseif ( $menu_type === 'mega' ) {
				$layout   = $menu['menu_layout'] ?? 'blocks';
				$panel_id = 'unlockafe-amm-panel-' . $widget_id . '-' . $index;
				$panel_style = ( $layout === 'blocks' ) ? ' style="height:auto; max-height:none; overflow:visible;"' : '';
				echo '<div id="' . esc_attr( $panel_id ) . '" class="unlockafe-amm-panel unlockafe-amm-panel--' . esc_attr( $layout ) . '"' . $panel_style . '>';
				$this->render_panel( $menu, $layout, $panel_id );
				echo '</div>';
			}
			?>
		</li>
		<?php
	}

	private function render_dropdown( array $menu ) {
		$items = $menu['dropdown_items'] ?? [];
		if ( empty( $items ) ) {
			return;
		}
		echo '<ul class="unlockafe-amm-dropdown">';
		foreach ( $items as $sub ) {
			$sub_label = $sub['label'] ?? '';
			if ( $sub_label === '' ) {
				continue;
			}
			$sub_url     = $sub['link']['url'] ?? '#';
			$sub_external = ! empty( $sub['link']['is_external'] );
			$sub_nofollow = ! empty( $sub['link']['nofollow'] );
			$sub_rel     = array_filter( [ $sub_external ? 'noopener noreferrer' : '', $sub_nofollow ? 'nofollow' : '' ] );
			$li_classes = [];
			if ( ! empty( $sub['_id'] ) ) {
				$li_classes[] = 'elementor-repeater-item-' . $sub['_id'];
			}

			$li_styles   = [];
			$link_styles = [];

			if ( isset( $sub['dropdown_item_style_enable'] ) && $sub['dropdown_item_style_enable'] === 'yes' ) {
				// Typography (font family, size, weight).
				$font_family = $sub['dropdown_item_typography_font_family'] ?? '';
				if ( $font_family !== '' ) {
					$link_styles[] = 'font-family:' . $font_family . ';';
				}
				$font_size = $sub['dropdown_item_typography_font_size'] ?? [];
				if ( is_array( $font_size ) && ! empty( $font_size['size'] ) ) {
					$size = $font_size['size'];
					$unit = isset( $font_size['unit'] ) ? $font_size['unit'] : 'px';
					$link_styles[] = 'font-size:' . $size . $unit . ';';
				}
				$font_weight = $sub['dropdown_item_typography_font_weight'] ?? '';
				if ( $font_weight !== '' && $font_weight !== 'default' ) {
					$link_styles[] = 'font-weight:' . $font_weight . ';';
				}

				// Text color.
				if ( ! empty( $sub['dropdown_item_color'] ) ) {
					$link_styles[] = 'color:' . $sub['dropdown_item_color'] . ';';
				}

				// Padding.
				if ( ! empty( $sub['dropdown_item_padding'] ) && is_array( $sub['dropdown_item_padding'] ) ) {
					$p = $sub['dropdown_item_padding'];
					$unit = isset( $p['unit'] ) ? $p['unit'] : 'px';
					$top = isset( $p['top'] ) ? $p['top'] : '';
					$right = isset( $p['right'] ) ? $p['right'] : '';
					$bottom = isset( $p['bottom'] ) ? $p['bottom'] : '';
					$left = isset( $p['left'] ) ? $p['left'] : '';
					$link_styles[] = 'padding:' . $top . $unit . ' ' . $right . $unit . ' ' . $bottom . $unit . ' ' . $left . $unit . ';';
				}

				// Margin.
				if ( ! empty( $sub['dropdown_item_margin'] ) && is_array( $sub['dropdown_item_margin'] ) ) {
					$m = $sub['dropdown_item_margin'];
					$unit = isset( $m['unit'] ) ? $m['unit'] : 'px';
					$top = isset( $m['top'] ) ? $m['top'] : '';
					$right = isset( $m['right'] ) ? $m['right'] : '';
					$bottom = isset( $m['bottom'] ) ? $m['bottom'] : '';
					$left = isset( $m['left'] ) ? $m['left'] : '';
					$li_styles[] = 'margin:' . $top . $unit . ' ' . $right . $unit . ' ' . $bottom . $unit . ' ' . $left . $unit . ';';
				}

				// Border (from Group_Control_Border).
				$border_type = $sub['dropdown_item_border_border'] ?? '';
				if ( $border_type && 'none' !== $border_type ) {
					$widths = $sub['dropdown_item_border_width'] ?? [];
					$top_w    = isset( $widths['top'] ) ? (int) $widths['top'] : 0;
					$right_w  = isset( $widths['right'] ) ? (int) $widths['right'] : $top_w;
					$bottom_w = isset( $widths['bottom'] ) ? (int) $widths['bottom'] : $top_w;
					$left_w   = isset( $widths['left'] ) ? (int) $widths['left'] : $right_w;
					$color    = $sub['dropdown_item_border_color'] ?? '';
					$link_styles[] = 'border-style:' . $border_type . ';';
					$link_styles[] = 'border-width:' . $top_w . 'px ' . $right_w . 'px ' . $bottom_w . 'px ' . $left_w . 'px;';
					if ( $color !== '' ) {
						$link_styles[] = 'border-color:' . $color . ';';
					}
				}

				// Border radius.
				if ( ! empty( $sub['dropdown_item_border_radius'] ) && is_array( $sub['dropdown_item_border_radius'] ) ) {
					$r = $sub['dropdown_item_border_radius'];
					$unit = isset( $r['unit'] ) ? $r['unit'] : 'px';
					$top = isset( $r['top'] ) ? $r['top'] : '';
					$right = isset( $r['right'] ) ? $r['right'] : '';
					$bottom = isset( $r['bottom'] ) ? $r['bottom'] : '';
					$left = isset( $r['left'] ) ? $r['left'] : '';
					$link_styles[] = 'border-radius:' . $top . $unit . ' ' . $right . $unit . ' ' . $bottom . $unit . ' ' . $left . $unit . ';';
				}
			}

			$li_attr_class = ! empty( $li_classes ) ? ' class="' . esc_attr( implode( ' ', $li_classes ) ) . '"' : '';
			$li_attr_style = ! empty( $li_styles ) ? ' style="' . esc_attr( implode( '', $li_styles ) ) . '"' : '';
			$link_attr_style = ! empty( $link_styles ) ? ' style="' . esc_attr( implode( '', $link_styles ) ) . '"' : '';

			echo '<li' . $li_attr_class . $li_attr_style . '>';
			echo '<a href="' . esc_url( $sub_url ) . '" class="unlockafe-amm-dropdown__link"' . ( $sub_external ? ' target="_blank"' : '' ) . ( ! empty( $sub_rel ) ? ' rel="' . esc_attr( implode( ' ', $sub_rel ) ) . '"' : '' ) . $link_attr_style . '>' . esc_html( $sub_label ) . '</a>';
			echo '</li>';
		}
		echo '</ul>';
	}

	private function render_panel( array $menu, string $layout, string $panel_id ) {
		if ( $layout === 'custom' ) {
			$page_id = isset( $menu['custom_layout_page_id'] ) ? (int) $menu['custom_layout_page_id'] : 0;
			if ( $page_id > 0 && class_exists( '\Elementor\Plugin' ) ) {
				$content = \Elementor\Plugin::instance()->frontend->get_builder_content_for_display( $page_id );
				echo '<div class="unlockafe-amm-panel__inner unlockafe-amm-panel--custom-inner">';
				echo $content;
				echo '</div>';
			} else {
				echo '<div class="unlockafe-amm-panel__inner"><p class="unlockafe-amm-empty">' . esc_html__( 'Select an Elementor page in Mega Menu Settings → Layout → Custom.', 'unlock-addons-for-elementor' ) . '</p></div>';
			}
			return;
		}
		$categories  = $menu['categories'] ?? [];
		$items       = $menu['submenu_items'] ?? [];
		$panel_title = $menu['panel_title'] ?? '';
		$total       = 0;
		$count_label = __( 'items', 'unlock-addons-for-elementor' );
		$posts_list  = [];
		$pages_list  = [];

		if ( $layout === 'posts' ) {
			$limit       = isset( $menu['posts_limit'] ) ? max( 1, (int) $menu['posts_limit'] ) : 9;
			$posts_list  = get_posts( [
				'post_type'      => 'post',
				'post_status'    => 'publish',
				'numberposts'    => $limit,
				'orderby'        => 'date',
				'order'          => 'DESC',
				'suppress_filters' => false,
			] );
			$total       = count( $posts_list );
			$count_label = _n( 'post', 'posts', $total, 'unlock-addons-for-elementor' );
		} elseif ( $layout === 'pages' ) {
			$limit       = isset( $menu['pages_limit'] ) ? max( 1, (int) $menu['pages_limit'] ) : 9;
			$pages_list  = get_pages( [
				'number'     => $limit,
				'sort_column' => 'post_date',
				'sort_order'  => 'DESC',
				'post_status' => 'publish',
			] );
			$total       = count( $pages_list );
			$count_label = _n( 'page', 'pages', $total, 'unlock-addons-for-elementor' );
		} else {
			$enabled_items = array_filter( $items, function ( $i ) { return ! isset( $i['_id'] ) || true; } );
			$total         = count( $enabled_items );
			$count_label   = $layout === 'solutions' ? __( 'solutions', 'unlock-addons-for-elementor' ) : ( $layout === 'blocks' ? __( 'blocks', 'unlock-addons-for-elementor' ) : __( 'features', 'unlock-addons-for-elementor' ) );
		}

		$show_count = $total > 0;

		// Layout 4 & 5: display title "Browse by Post" / "Browse by Page"
		$display_title = $panel_title;
		if ( $layout === 'posts' ) {
			$display_title = __( 'Browse by Post', 'unlock-addons-for-elementor' );
		} elseif ( $layout === 'pages' ) {
			$display_title = __( 'Browse by Page', 'unlock-addons-for-elementor' );
		}

		echo '<div class="unlockafe-amm-panel__inner">';

		// Header: title + count + category tabs (for Features & Solutions only; Blocks has these inside left column)
		if ( $layout !== 'blocks' && ( $display_title !== '' || $show_count || ( count( $categories ) > 0 && in_array( $layout, [ 'features', 'solutions' ], true ) ) ) ) {
			echo '<div class="unlockafe-amm-panel__header">';
			if ( $display_title !== '' ) {
				echo '<h3 class="unlockafe-amm-panel__title">' . esc_html( $display_title ) . '</h3>';
			}
			if ( $show_count ) {
				echo '<p class="unlockafe-amm-panel__count">' . esc_html( sprintf( __( 'Showing %1$d of %2$d %3$s', 'unlock-addons-for-elementor' ), $total, $total, $count_label ) ) . '</p>';
			}
			if ( count( $categories ) > 0 && in_array( $layout, [ 'features', 'solutions' ], true ) ) {
				$this->render_category_tabs( $layout, $categories );
			}
			echo '</div>';
		}

		// Body by layout (Layout 4 & 5: wrap with scroll buttons)
		$use_scroll_buttons = ( $layout === 'posts' || $layout === 'pages' );
		if ( $use_scroll_buttons ) {
			echo '<div class="unlockafe-amm-panel__body-outer">';
			echo '<button type="button" class="unlockafe-amm-scroll-btn unlockafe-amm-scroll-btn--up" aria-label="' . esc_attr__( 'Scroll up', 'unlock-addons-for-elementor' ) . '" title="' . esc_attr__( 'Scroll up', 'unlock-addons-for-elementor' ) . '">&uarr;</button>';
		}
		echo '<div class="unlockafe-amm-panel__body">';
		if ( $layout === 'blocks' ) {
			$this->render_layout_blocks( $menu, $items, $categories );
		} elseif ( $layout === 'features' ) {
			$this->render_layout_features( $items, $categories );
		} elseif ( $layout === 'posts' ) {
			$this->render_layout_posts( $menu, $posts_list );
		} elseif ( $layout === 'pages' ) {
			$this->render_layout_pages( $menu, $pages_list );
		} else {
			$this->render_layout_solutions( $items, $categories );
		}
		echo '</div>';
		if ( $use_scroll_buttons ) {
			echo '<button type="button" class="unlockafe-amm-scroll-btn unlockafe-amm-scroll-btn--down" aria-label="' . esc_attr__( 'Scroll down', 'unlock-addons-for-elementor' ) . '" title="' . esc_attr__( 'Scroll down', 'unlock-addons-for-elementor' ) . '">&darr;</button>';
			echo '</div>';
		}

		// CTA (Layout 1 blocks: rendered inside right column; other layouts: here)
		if ( $layout !== 'blocks' && ! empty( $menu['cta_show'] ) && $menu['cta_show'] === 'yes' ) {
			$cta_text   = $menu['cta_text'] ?? '';
			$cta_sub    = $menu['cta_subtext'] ?? '';
			$cta_btn    = isset( $menu['cta_button_text'] ) ? trim( (string) $menu['cta_button_text'] ) : '';
			$cta_url    = $menu['cta_link']['url'] ?? '#';
			if ( $layout === 'posts' && $cta_btn === '' ) {
				$cta_btn = __( 'View All Posts', 'unlock-addons-for-elementor' );
			} elseif ( $layout === 'pages' && $cta_btn === '' ) {
				$cta_btn = __( 'View All Pages', 'unlock-addons-for-elementor' );
			}
			if ( $cta_url === '' ) {
				$cta_url = '#';
			}
			$cta_ext    = ! empty( $menu['cta_link']['is_external'] );
			$cta_nof    = ! empty( $menu['cta_link']['nofollow'] );
			$cta_rel    = array_filter( [ $cta_ext ? 'noopener noreferrer' : '', $cta_nof ? 'nofollow' : '' ] );
			if ( $cta_text !== '' || $cta_sub !== '' || $cta_btn !== '' ) {
				echo '<div class="unlockafe-amm-cta">';
				echo '<div class="unlockafe-amm-cta__inner">';
				if ( $cta_text !== '' || $cta_sub !== '' ) {
					echo '<div class="unlockafe-amm-cta__copy">';
					echo '<div class="unlockafe-amm-cta__copy-inner">';
					if ( $cta_text !== '' ) {
						echo '<p class="unlockafe-amm-cta__text">' . esc_html( $cta_text ) . '</p>';
					}
					if ( $cta_sub !== '' ) {
						echo '<p class="unlockafe-amm-cta__subtext">' . esc_html( $cta_sub ) . '</p>';
					}
					echo '</div>';
					echo '</div>';
				}
				if ( $cta_btn !== '' ) {
					echo '<a href="' . esc_url( $cta_url ) . '" class="unlockafe-amm-cta__btn"' . ( $cta_ext ? ' target="_blank"' : '' ) . ( ! empty( $cta_rel ) ? ' rel="' . esc_attr( implode( ' ', $cta_rel ) ) . '"' : '' ) . '>' . esc_html( $cta_btn ) . ' →</a>';
				}
				echo '</div></div>';
			}
		}

		echo '</div>';
	}

	private function render_category_tabs( string $layout, array $categories ) {
		$tabs_class = 'unlockafe-amm-tabs';
		if ( $layout === 'blocks' ) {
			$tabs_class .= ' unlockafe-amm-tabs--blocks';
		}
		echo '<div class="' . esc_attr( $tabs_class ) . '" role="tablist" aria-label="' . esc_attr__( 'Categories', 'unlock-addons-for-elementor' ) . '">';
		$all_label = $layout === 'solutions' ? __( 'All Solutions', 'unlock-addons-for-elementor' ) : __( 'All Blocks', 'unlock-addons-for-elementor' );
		if ( $layout === 'blocks' || $layout === 'features' ) {
			echo '<button type="button" class="unlockafe-amm-tab is-active" role="tab" data-category="">';
			echo '<span class="unlockafe-amm-tab__icon unlockafe-amm-tab__icon--all-blocks" aria-hidden="true"></span>';
			echo esc_html( $all_label ) . '</button>';
		} elseif ( $layout === 'solutions' ) {
			echo '<button type="button" class="unlockafe-amm-tab is-active" role="tab" data-category="">';
			echo '<span class="unlockafe-amm-tab__icon unlockafe-amm-tab__icon--all-solutions" aria-hidden="true"></span>';
			echo esc_html( $all_label ) . '</button>';
		} else {
			echo '<button type="button" class="unlockafe-amm-tab is-active" role="tab" data-category="">' . esc_html( $all_label ) . '</button>';
		}
		foreach ( $categories as $cat ) {
			$cid = isset( $cat['category_id'] ) ? trim( (string) $cat['category_id'] ) : '';
			if ( $cid === '' || $cid === 'all' ) {
				continue;
			}
			$lab = isset( $cat['category_label'] ) ? $cat['category_label'] : $cid;
			$icon_type = isset( $cat['category_icon_type'] ) ? $cat['category_icon_type'] : 'icon';
			echo '<button type="button" class="unlockafe-amm-tab" role="tab" data-category="' . esc_attr( $cid ) . '">';
			if ( $icon_type === 'icon' && ! empty( $cat['category_icon']['value'] ) ) {
				echo '<span class="unlockafe-amm-tab__icon">';
				Icons_Manager::render_icon( $cat['category_icon'], [ 'aria-hidden' => 'true' ] );
				echo '</span>';
			} elseif ( $icon_type === 'image' && ! empty( $cat['category_icon_image']['url'] ) ) {
				echo '<span class="unlockafe-amm-tab__icon unlockafe-amm-tab__icon--img"><img src="' . esc_url( $cat['category_icon_image']['url'] ) . '" alt="" loading="lazy"></span>';
			} elseif ( $icon_type === 'svg_code' ) {
				$svg_code = isset( $cat['category_icon_svg_code'] ) ? trim( (string) $cat['category_icon_svg_code'] ) : '';
				if ( $svg_code !== '' ) {
					$sanitized = wp_kses( $svg_code, $this->allowed_svg_kses() );
					if ( $sanitized !== '' ) {
						echo '<span class="unlockafe-amm-tab__icon unlockafe-amm-tab__icon--svg">' . $sanitized . '</span>';
					}
				}
			}
			echo esc_html( $lab ) . '</button>';
		}
		echo '</div>';
	}

	private function render_layout_blocks( array $menu, array $items, array $categories ) {
		$featured = null;
		foreach ( $items as $item ) {
			if ( ! empty( $item['is_featured'] ) && $item['is_featured'] === 'yes' ) {
				$featured = $item;
				break;
			}
		}
		if ( $featured === null && ! empty( $items[0] ) ) {
			$featured = $items[0];
		}

		$panel_title = $menu['panel_title'] ?? '';
		$blocks_heading = $panel_title !== '' ? $panel_title : __( 'Browse by Category', 'unlock-addons-for-elementor' );
		$total = count( array_filter( $items, function ( $i ) { return ! isset( $i['_id'] ) || true; } ) );
		$view_all_default = $total > 0 ? sprintf( __( 'View All %d Blocks', 'unlock-addons-for-elementor' ), $total ) : __( 'View All Blocks', 'unlock-addons-for-elementor' );

		echo '<div class="unlockafe-amm-blocks">';
		echo '<div class="unlockafe-amm-blocks__left">';
		echo '<h3 class="unlockafe-amm-blocks__heading">' . esc_html( $blocks_heading ) . '</h3>';
		if ( count( $categories ) > 0 ) {
			$this->render_category_tabs( 'blocks', $categories );
		}
		echo '<div class="unlockafe-amm-list" role="list">';
		if ( empty( $items ) ) {
			echo '<p class="unlockafe-amm-empty" style="padding: 12px 0;">' . esc_html__( 'Template 1 (Blocks). Add items in the widget repeater or choose Custom Elementor page for this menu item.', 'unlock-addons-for-elementor' ) . '</p>';
		}
		foreach ( $items as $item ) {
			$this->render_item_block_row( $item, $featured );
		}
		echo '</div>';
		$view_all = $menu['view_all_text'] ?? $view_all_default;
		$view_url = $menu['view_all_link']['url'] ?? '#';
		$view_ext = ! empty( $menu['view_all_link']['is_external'] );
		echo '<a href="' . esc_url( $view_url ) . '" class="unlockafe-amm-view-all"' . ( $view_ext ? ' target="_blank" rel="noopener noreferrer"' : '' ) . '>' . esc_html( $view_all ) . '</a>';
		echo '</div>';
		echo '<div class="unlockafe-amm-blocks__right">';
		if ( $featured ) {
			$this->render_featured_detail( $featured );
		}
		// Layout 1: CTA only in right column (same as MegaMenu.tsx – positioned right side)
		if ( ! empty( $menu['cta_show'] ) && $menu['cta_show'] === 'yes' ) {
			$cta_text = $menu['cta_text'] ?? '';
			$cta_sub  = $menu['cta_subtext'] ?? '';
			$cta_btn  = isset( $menu['cta_button_text'] ) ? trim( (string) $menu['cta_button_text'] ) : '';
			$cta_url  = $menu['cta_link']['url'] ?? '#';
			if ( $cta_url === '' ) {
				$cta_url = '#';
			}
			$cta_ext = ! empty( $menu['cta_link']['is_external'] );
			$cta_nof = ! empty( $menu['cta_link']['nofollow'] );
			$cta_rel = array_filter( [ $cta_ext ? 'noopener noreferrer' : '', $cta_nof ? 'nofollow' : '' ] );
			if ( $cta_text !== '' || $cta_sub !== '' || $cta_btn !== '' ) {
				echo '<div class="unlockafe-amm-cta">';
				echo '<div class="unlockafe-amm-cta__inner">';
				if ( $cta_text !== '' || $cta_sub !== '' ) {
					echo '<div class="unlockafe-amm-cta__copy">';
					echo '<div class="unlockafe-amm-cta__copy-inner">';
					if ( $cta_text !== '' ) {
						echo '<p class="unlockafe-amm-cta__text">' . esc_html( $cta_text ) . '</p>';
					}
					if ( $cta_sub !== '' ) {
						echo '<p class="unlockafe-amm-cta__subtext">' . esc_html( $cta_sub ) . '</p>';
					}
					echo '</div></div>';
				}
				if ( $cta_btn !== '' ) {
					echo '<a href="' . esc_url( $cta_url ) . '" class="unlockafe-amm-cta__btn"' . ( $cta_ext ? ' target="_blank"' : '' ) . ( ! empty( $cta_rel ) ? ' rel="' . esc_attr( implode( ' ', $cta_rel ) ) . '"' : '' ) . '>' . esc_html( $cta_btn ) . ' →</a>';
				}
				echo '</div></div>';
			}
		}
		echo '</div>';
		echo '</div>';
	}

	private function render_item_block_row( array $item, $featured_item ) {
		$title = $item['title'] ?? '';
		$desc  = $item['description'] ?? '';
		$link  = $item['link']['url'] ?? '#';
		$ext   = ! empty( $item['link']['is_external'] );
		$nof   = ! empty( $item['link']['nofollow'] );
		$cat   = isset( $item['category_id'] ) ? trim( (string) $item['category_id'] ) : '';
		$badge = $item['badge'] ?? 'none';
		$is_active = $featured_item && ( $item['title'] ?? '' ) === ( $featured_item['title'] ?? '' );
		$rel = array_filter( [ $ext ? 'noopener noreferrer' : '', $nof ? 'nofollow' : '' ] );

		$featured_cfg = $this->get_featured_media_config( $item );
		$images       = $featured_cfg['images'];

		$classes = [ 'unlockafe-amm-item', 'unlockafe-amm-item--row' ];
		if ( $badge === 'new' ) {
			$classes[] = 'unlockafe-amm-item--has-badge';
		}
		if ( $is_active ) {
			$classes[] = 'is-active';
		}
		$img   = $images[0] ?? '';
		$fallback_img = $this->get_placeholder_image_url( $title );
		$gallery_attr = '';
		if ( count( $images ) > 1 ) {
			$gallery_attr = ' data-gallery="' . esc_attr( implode( '|', $images ) ) . '"';
		}
		echo '<div class="' . esc_attr( implode( ' ', $classes ) ) . '"'
			. ( $cat !== '' ? ' data-category="' . esc_attr( $cat ) . '"' : '' )
			. ' data-title="' . esc_attr( $title ) . '"'
			. ' data-desc="' . esc_attr( $desc ) . '"'
			. ' data-link="' . esc_url( $link ) . '"'
			. ( $img !== '' ? ' data-img="' . esc_url( $img ) . '"' : '' )
			. ' data-fallback="' . esc_attr( $fallback_img ) . '"'
			. $gallery_attr
			. ' data-featured-type="' . esc_attr( $featured_cfg['type'] ) . '"'
			. ( $featured_cfg['video'] !== '' ? ' data-featured-video="' . esc_url( $featured_cfg['video'] ) . '"' : '' )
			. '>';
		echo '<a class="unlockafe-amm-item__link" href="' . esc_url( $link ) . '"' . ( $ext ? ' target="_blank"' : '' ) . ( ! empty( $rel ) ? ' rel="' . esc_attr( implode( ' ', $rel ) ) . '"' : '' ) . '>';
		$media_type = $item['media_type'] ?? 'icon';
		$video_url  = $item['video']['url'] ?? '';
		$has_icon   = ! empty( $item['selected_icon']['value'] );
		$svg_code   = isset( $item['item_svg_code'] ) ? trim( (string) $item['item_svg_code'] ) : '';
		// Left side: show icon / image / SVG code / video (legacy) / placeholder
		if ( $has_icon && $media_type === 'icon' ) {
			echo '<span class="unlockafe-amm-item__icon">';
			Icons_Manager::render_icon( $item['selected_icon'], [ 'aria-hidden' => 'true' ] );
			echo '</span>';
		} elseif ( $media_type === 'image' && ! empty( $item['image']['url'] ) ) {
			$fallback = $this->get_placeholder_image_url( $title );
			echo '<span class="unlockafe-amm-item__thumb"><img src="' . esc_url( $item['image']['url'] ) . '" alt="" loading="lazy" data-fallback="' . esc_attr( $fallback ) . '"></span>';
		} elseif ( $media_type === 'svg_code' && $svg_code !== '' ) {
			$sanitized = wp_kses( $svg_code, $this->allowed_svg_kses() );
			if ( $sanitized !== '' ) {
				echo '<span class="unlockafe-amm-item__icon unlockafe-amm-item__icon--svg">' . $sanitized . '</span>';
			} else {
				echo '<span class="unlockafe-amm-item__icon">';
				Icons_Manager::render_icon( [ 'value' => 'fas fa-th', 'library' => 'fa-solid' ], [ 'aria-hidden' => 'true' ] );
				echo '</span>';
			}
		} elseif ( $media_type === 'video' && $video_url !== '' ) {
			$poster = $item['image']['url'] ?? '';
			echo '<span class="unlockafe-amm-item__thumb unlockafe-amm-item__thumb--video">';
			echo '<video src="' . esc_url( $video_url ) . '" muted playsinline preload="metadata"' . ( $poster !== '' ? ' poster="' . esc_url( $poster ) . '"' : '' ) . '></video>';
			echo '<span class="unlockafe-amm-item__play" aria-hidden="true"></span>';
			echo '</span>';
		} else {
			echo '<span class="unlockafe-amm-item__icon">';
			Icons_Manager::render_icon( [ 'value' => 'fas fa-th', 'library' => 'fa-solid' ], [ 'aria-hidden' => 'true' ] );
			echo '</span>';
		}
		echo '<span class="unlockafe-amm-item__content">';
		echo '<span class="unlockafe-amm-item__title-row">';
		echo '<span class="unlockafe-amm-item__title">' . esc_html( $title ) . '</span>';
		if ( $badge === 'new' ) {
			echo '<span class="unlockafe-amm-item__badge">' . esc_html__( 'NEW', 'unlock-addons-for-elementor' ) . '</span>';
		}
		echo '</span>';
		if ( $desc !== '' ) {
			echo '<span class="unlockafe-amm-item__desc">' . esc_html( $desc ) . '</span>';
		}
		echo '</span>';
		echo '</a></div>';
	}

	private function render_featured_detail( array $item ) {
		$title = $item['title'] ?? '';
		$desc  = $item['description'] ?? '';
		$link  = $item['link']['url'] ?? '#';
		$cfg   = $this->get_featured_media_config( $item );
		$images = $cfg['images'];
		$img    = $images[0] ?? '';
		$video_url = $cfg['video'];
		$featured_type = $cfg['type'];
		$ext   = ! empty( $item['link']['is_external'] );
		$nof   = ! empty( $item['link']['nofollow'] );
		$rel   = array_filter( [ $ext ? 'noopener noreferrer' : '', $nof ? 'nofollow' : '' ] );
		echo '<div class="unlockafe-amm-featured">';
		echo '<h4 class="unlockafe-amm-featured__title">' . esc_html( $title ) . '</h4>';
		if ( $desc !== '' ) {
			echo '<p class="unlockafe-amm-featured__subtitle">' . esc_html( $desc ) . '</p>';
		}
		echo '<div class="unlockafe-amm-featured__card">';
		if ( $featured_type === 'video' && $video_url !== '' ) {
			echo '<div class="unlockafe-amm-featured__media unlockafe-amm-featured__media--video">';
			echo '<video src="' . esc_url( $video_url ) . '" autoplay muted playsinline loop preload="metadata"' . ( $img !== '' ? ' poster="' . esc_url( $img ) . '"' : '' ) . '></video>';
			echo '</div>';
		} elseif ( $featured_type === 'gallery' && count( $images ) > 1 ) {
			$fallback = $this->get_placeholder_image_url( $title );
			echo '<div class="unlockafe-amm-featured__media unlockafe-amm-featured__media--slider">';
			foreach ( $images as $index => $url ) {
				if ( $url === '' ) {
					continue;
				}
				$classes = 'unlockafe-amm-featured__img';
				if ( $index === 0 ) {
					$classes .= ' is-active';
				}
				echo '<div class="' . esc_attr( $classes ) . '" style="background-image:url(' . esc_url( $url ) . ')" data-src="' . esc_attr( $url ) . '" data-fallback="' . esc_attr( $fallback ) . '"></div>';
			}
			echo '</div>';
		} elseif ( $img !== '' ) {
			$fallback = $this->get_placeholder_image_url( $title );
			echo '<div class="unlockafe-amm-featured__img" style="background-image:url(' . esc_url( $img ) . ')" data-src="' . esc_attr( $img ) . '" data-fallback="' . esc_attr( $fallback ) . '"></div>';
		} else {
			echo '<div class="unlockafe-amm-featured__img unlockafe-amm-featured__img--placeholder"></div>';
		}
		echo '<div class="unlockafe-amm-featured__overlay">';
		echo '<h4 class="unlockafe-amm-featured__overlay-title">' . esc_html( $title ) . '</h4>';
		echo '<p class="unlockafe-amm-featured__overlay-desc">' . esc_html( $desc ) . '</p>';
		echo '<a href="' . esc_url( $link ) . '" class="unlockafe-amm-featured__btn"' . ( $ext ? ' target="_blank"' : '' ) . ( ! empty( $rel ) ? ' rel="' . esc_attr( implode( ' ', $rel ) ) . '"' : '' ) . '>' . esc_html__( 'View Details', 'unlock-addons-for-elementor' ) . ' →</a>';
		echo '</div></div></div>';
	}

	private function render_layout_features( array $items, array $categories ) {
		// Masonry-style grid: all submenu items as cards; category filter still works via data-category.
		echo '<div class="unlockafe-amm-features unlockafe-amm-features--masonry">';
		echo '<div class="unlockafe-amm-features__grid">';
		foreach ( $items as $item ) {
			$this->render_card( $item, 'small' );
		}
		echo '</div>';
		echo '</div>';
	}

	private function render_layout_solutions( array $items, array $categories ) {
		echo '<div class="unlockafe-amm-solutions">';
		echo '<div class="unlockafe-amm-solutions__grid">';
		foreach ( $items as $item ) {
			$this->render_solution_card( $item );
		}
		echo '</div>';
		echo '</div>';
	}

	private function render_layout_posts( array $menu, array $posts_list ) {
		$per_row = isset( $menu['posts_per_row'] ) ? max( 2, min( 6, (int) $menu['posts_per_row'] ) ) : 3;
		echo '<div class="unlockafe-amm-posts">';
		echo '<div class="unlockafe-amm-posts__grid" style="grid-template-columns:repeat(' . (int) $per_row . ',1fr)">';
		foreach ( $posts_list as $post ) {
			$this->render_post_page_card( $post, 'post' );
		}
		echo '</div>';
		echo '</div>';
	}

	private function render_layout_pages( array $menu, array $pages_list ) {
		$per_row = isset( $menu['pages_per_row'] ) ? max( 2, min( 6, (int) $menu['pages_per_row'] ) ) : 3;
		echo '<div class="unlockafe-amm-pages">';
		echo '<div class="unlockafe-amm-pages__grid" style="grid-template-columns:repeat(' . (int) $per_row . ',1fr)">';
		foreach ( $pages_list as $page ) {
			$this->render_post_page_card( $page, 'page' );
		}
		echo '</div>';
		echo '</div>';
	}

	/**
	 * Single card for a WP post or page (title, description, image) – same structure as solution card.
	 *
	 * @param \WP_Post $post_or_page Post or page object.
	 * @param string   $type        'post' or 'page'.
	 */
	private function render_post_page_card( $post_or_page, string $type ) {
		$url   = get_permalink( $post_or_page );
		$title = get_the_title( $post_or_page );
		$img   = get_the_post_thumbnail_url( $post_or_page, 'medium' );
		if ( $type === 'post' ) {
			$desc = has_excerpt( $post_or_page ) ? get_the_excerpt( $post_or_page ) : wp_trim_words( wp_strip_all_tags( $post_or_page->post_content ), 20 );
		} else {
			$desc = has_excerpt( $post_or_page ) ? get_the_excerpt( $post_or_page ) : wp_trim_words( wp_strip_all_tags( $post_or_page->post_content ), 20 );
		}
		echo '<div class="unlockafe-amm-sol-card">';
		echo '<a href="' . esc_url( $url ) . '" class="unlockafe-amm-sol-card__link">';
		echo '<div class="unlockafe-amm-sol-card__img-wrap">';
		if ( $img ) {
			echo '<div class="unlockafe-amm-sol-card__img" style="background-image:url(' . esc_url( $img ) . ')"></div>';
		} else {
			echo '<div class="unlockafe-amm-sol-card__img unlockafe-amm-sol-card__img--placeholder"></div>';
		}
		echo '</div>';
		echo '<div class="unlockafe-amm-sol-card__content">';
		echo '<h4 class="unlockafe-amm-sol-card__title">' . esc_html( $title ) . '</h4>';
		if ( $desc !== '' ) {
			echo '<p class="unlockafe-amm-sol-card__desc">' . esc_html( $desc ) . '</p>';
		}
		echo '</div></a></div>';
	}

	private function render_card( array $item, string $size ) {
		$title = $item['title'] ?? '';
		$desc  = $item['description'] ?? '';
		$link  = $item['link']['url'] ?? '#';
		$ext   = ! empty( $item['link']['is_external'] );
		$nof   = ! empty( $item['link']['nofollow'] );
		$cat   = isset( $item['category_id'] ) ? trim( (string) $item['category_id'] ) : '';
		$badge = $item['badge'] ?? 'none';
		$rel   = array_filter( [ $ext ? 'noopener noreferrer' : '', $nof ? 'nofollow' : '' ] );
		$img   = $item['image']['url'] ?? '';
		$video_url = $item['video']['url'] ?? '';
		$media_type = $item['media_type'] ?? 'icon';
		$has_icon   = ! empty( $item['selected_icon']['value'] );
		$svg_code   = isset( $item['item_svg_code'] ) ? trim( (string) $item['item_svg_code'] ) : '';
		$classes = [ 'unlockafe-amm-card', 'unlockafe-amm-card--' . $size ];
		if ( $badge === 'new' ) {
			$classes[] = 'unlockafe-amm-card--has-badge';
		}
		if ( $img !== '' || ( $media_type === 'video' && $video_url !== '' ) ) {
			$classes[] = 'unlockafe-amm-card--has-img';
		} else {
			$classes[] = 'unlockafe-amm-card--no-img';
		}
		if ( $media_type === 'video' && $video_url !== '' ) {
			$classes[] = 'unlockafe-amm-card--has-video';
		}
		echo '<div class="' . esc_attr( implode( ' ', $classes ) ) . '"' . ( $cat !== '' ? ' data-category="' . esc_attr( $cat ) . '"' : '' ) . '>';
		echo '<a href="' . esc_url( $link ) . '" class="unlockafe-amm-card__link"' . ( $ext ? ' target="_blank"' : '' ) . ( ! empty( $rel ) ? ' rel="' . esc_attr( implode( ' ', $rel ) ) . '"' : '' ) . '>';
		// Media wrap: image/video + NEW badge only here so badge never overlaps title/description
		echo '<span class="unlockafe-amm-card__media-wrap">';
		if ( $badge === 'new' ) {
			echo '<span class="unlockafe-amm-card__badge">' . esc_html__( 'NEW', 'unlock-addons-for-elementor' ) . '</span>';
		}
		if ( $media_type === 'video' && $video_url !== '' ) {
			echo '<span class="unlockafe-amm-card__media unlockafe-amm-card__media--video">';
			echo '<video src="' . esc_url( $video_url ) . '" muted playsinline loop preload="metadata"' . ( $img !== '' ? ' poster="' . esc_url( $img ) . '"' : '' ) . '></video>';
			echo '<span class="unlockafe-amm-card__play" aria-hidden="true"></span>';
			echo '</span>';
		} elseif ( $img !== '' ) {
			$fallback = $this->get_placeholder_image_url( $title );
			if ( $size === 'small' ) {
				echo '<img class="unlockafe-amm-card__img" src="' . esc_url( $img ) . '" alt="' . esc_attr( $title ) . '" loading="lazy" data-src="' . esc_attr( $img ) . '" data-fallback="' . esc_attr( $fallback ) . '"/>';
			} else {
				echo '<span class="unlockafe-amm-card__img" style="background-image:url(' . esc_url( $img ) . ')" data-src="' . esc_attr( $img ) . '" data-fallback="' . esc_attr( $fallback ) . '"></span>';
			}
		} else {
			echo '<span class="unlockafe-amm-card__img unlockafe-amm-card__img--placeholder"></span>';
		}
		// Optional left/icon overlay for Features/Blocks layouts: icon or SVG code.
		if ( $media_type === 'svg_code' && $svg_code !== '' ) {
			$sanitized = wp_kses( $svg_code, $this->allowed_svg_kses() );
			if ( $sanitized !== '' ) {
				echo '<span class="unlockafe-amm-card__icon-overlay unlockafe-amm-card__icon-overlay--svg">' . $sanitized . '</span>';
			}
		} elseif ( $has_icon && $media_type === 'icon' ) {
			echo '<span class="unlockafe-amm-card__icon-overlay">';
			Icons_Manager::render_icon( $item['selected_icon'], [ 'aria-hidden' => 'true' ] );
			echo '</span>';
		}
		echo '</span>';
		echo '<span class="unlockafe-amm-card__content">';
		echo '<span class="unlockafe-amm-card__title">' . esc_html( $title ) . '</span>';
		if ( $desc !== '' ) {
			echo '<span class="unlockafe-amm-card__desc">' . esc_html( $desc ) . '</span>';
		}
		echo '</span>';
		echo '</a></div>';
	}

	private function render_solution_card( array $item ) {
		$title = $item['title'] ?? '';
		$desc  = $item['description'] ?? '';
		$link  = $item['link']['url'] ?? '#';
		$ext   = ! empty( $item['link']['is_external'] );
		$nof   = ! empty( $item['link']['nofollow'] );
		$cat   = isset( $item['category_id'] ) ? trim( (string) $item['category_id'] ) : '';
		$badge = $item['badge'] ?? 'none';
		$rel   = array_filter( [ $ext ? 'noopener noreferrer' : '', $nof ? 'nofollow' : '' ] );
		$img   = $item['image']['url'] ?? '';
		$video_url = $item['video']['url'] ?? '';
		$media_type = $item['media_type'] ?? 'icon';
		$svg_code   = isset( $item['item_svg_code'] ) ? trim( (string) $item['item_svg_code'] ) : '';
		$tag_text = isset( $item['tag_text'] ) ? trim( (string) $item['tag_text'] ) : '';
		$tag   = $tag_text !== '' ? $tag_text : ( $badge === 'new' ? __( 'NEW', 'unlock-addons-for-elementor' ) : '' );
		$has_icon = ! empty( $item['selected_icon']['value'] );
		$classes = [ 'unlockafe-amm-sol-card' ];
		if ( $tag !== '' ) {
			$classes[] = 'unlockafe-amm-sol-card--has-tag';
		}
		if ( $media_type === 'video' && $video_url !== '' ) {
			$classes[] = 'unlockafe-amm-sol-card--has-video';
		}
		echo '<div class="' . esc_attr( implode( ' ', $classes ) ) . '"' . ( $cat !== '' ? ' data-category="' . esc_attr( $cat ) . '"' : '' ) . '>';
		echo '<a href="' . esc_url( $link ) . '" class="unlockafe-amm-sol-card__link"' . ( $ext ? ' target="_blank"' : '' ) . ( ! empty( $rel ) ? ' rel="' . esc_attr( implode( ' ', $rel ) ) . '"' : '' ) . '>';
		echo '<div class="unlockafe-amm-sol-card__img-wrap">';
		if ( $media_type === 'video' && $video_url !== '' ) {
			echo '<div class="unlockafe-amm-sol-card__media unlockafe-amm-sol-card__media--video">';
			echo '<video src="' . esc_url( $video_url ) . '" muted playsinline loop preload="metadata"' . ( $img !== '' ? ' poster="' . esc_url( $img ) . '"' : '' ) . '></video>';
			echo '<span class="unlockafe-amm-sol-card__play" aria-hidden="true"></span>';
			echo '</div>';
		} elseif ( $img !== '' ) {
			$fallback = $this->get_placeholder_image_url( $title );
			echo '<div class="unlockafe-amm-sol-card__img" style="background-image:url(' . esc_url( $img ) . ')" data-src="' . esc_attr( $img ) . '" data-fallback="' . esc_attr( $fallback ) . '"></div>';
		} else {
			echo '<div class="unlockafe-amm-sol-card__img unlockafe-amm-sol-card__img--placeholder"></div>';
		}
		if ( $media_type === 'svg_code' && $svg_code !== '' ) {
			$sanitized = wp_kses( $svg_code, $this->allowed_svg_kses() );
			if ( $sanitized !== '' ) {
				echo '<span class="unlockafe-amm-sol-card__icon-overlay unlockafe-amm-sol-card__icon-overlay--svg">' . $sanitized . '</span>';
			}
		} elseif ( $has_icon && $media_type === 'icon' ) {
			echo '<span class="unlockafe-amm-sol-card__icon-overlay">';
			Icons_Manager::render_icon( $item['selected_icon'], [ 'aria-hidden' => 'true' ] );
			echo '</span>';
		}
		echo '</div>';
		echo '<div class="unlockafe-amm-sol-card__content">';
		echo '<h4 class="unlockafe-amm-sol-card__title">' . esc_html( $title ) . '</h4>';
		if ( $desc !== '' ) {
			echo '<p class="unlockafe-amm-sol-card__desc">' . esc_html( $desc ) . '</p>';
		}
		if ( $tag !== '' ) {
			echo '<span class="unlockafe-amm-sol-card__tag"><span class="unlockafe-amm-sol-card__tag-dot"></span>' . esc_html( $tag ) . '</span>';
		}
		echo '</div></a></div>';
	}
}
