<?php

namespace Calio\Widgets;

use Elementor\Controls_Manager;
use Elementor\Group_Control_Border;
use Elementor\Group_Control_Box_Shadow;
use Elementor\Group_Control_Typography;
use Elementor\Repeater;

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

/**
 * Builds a comparison-style pricing table with up to four package columns.
 */
class MulticolumnPricingTableWidget extends BaseWidget {
    public function get_name(): string { return 'calio-multicolumn-pricing-table'; }
    public function get_title(): string { return esc_html__('Multicolumn Pricing Table', 'calio-addon'); }
    public function get_icon(): string { return 'eicon-price-table'; }
    public function get_style_depends(): array { return $this->resolve_widget_style_depends(); }
    public function get_script_depends(): array { return $this->resolve_widget_script_depends(); }

    /**
     * Adds package, feature and style controls.
     */
    protected function register_controls(): void {
        $this->start_controls_section('layout', ['label' => esc_html__('Layout', 'calio-addon')]);
        $this->add_control('plan_count', ['label' => esc_html__('Plans', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => '3', 'options' => ['2' => '2', '3' => '3', '4' => '4']]);
        $this->add_control('skin', ['label' => esc_html__('Skin', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'soft', 'options' => ['soft' => esc_html__('Soft', 'calio-addon'), 'minimal' => esc_html__('Minimal', 'calio-addon'), 'contrast' => esc_html__('Contrast', 'calio-addon')]]);
        $this->add_control('featured_skin', ['label' => esc_html__('Featured Skin', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'spotlight', 'options' => ['spotlight' => esc_html__('Spotlight', 'calio-addon'), 'outline' => esc_html__('Outline', 'calio-addon'), 'ribbon' => esc_html__('Ribbon', 'calio-addon')]]);
        $this->add_control('enable_billing_switch', ['label' => esc_html__('Billing Switcher', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => '']);
        $this->add_control('monthly_label', ['label' => esc_html__('Monthly Label', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('Monthly', 'calio-addon'), 'condition' => ['enable_billing_switch' => 'yes']]);
        $this->add_control('yearly_label', ['label' => esc_html__('Yearly Label', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('Yearly', 'calio-addon'), 'condition' => ['enable_billing_switch' => 'yes']]);
        $this->add_control('yearly_badge', ['label' => esc_html__('Yearly Badge', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('Save 20%', 'calio-addon'), 'condition' => ['enable_billing_switch' => 'yes']]);
        $this->add_control('show_switcher_icons', ['label' => esc_html__('Enable Cycle Icons', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => 'yes', 'condition' => ['enable_billing_switch' => 'yes']]);
        $this->add_control('monthly_icon', [
            'label' => esc_html__('Monthly Icon', 'calio-addon'),
            'type' => Controls_Manager::ICONS,
            'default' => [
                'value' => 'fas fa-calendar-alt',
                'library' => 'fa-solid',
            ],
            'condition' => ['enable_billing_switch' => 'yes'],
        ]);
        $this->add_control('yearly_icon', [
            'label' => esc_html__('Yearly Icon', 'calio-addon'),
            'type' => Controls_Manager::ICONS,
            'default' => [
                'value' => 'fas fa-sync-alt',
                'library' => 'fa-solid',
            ],
            'condition' => ['enable_billing_switch' => 'yes'],
        ]);
        $this->add_control('default_billing_cycle', ['label' => esc_html__('Default Billing', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'monthly', 'options' => ['monthly' => esc_html__('Monthly', 'calio-addon'), 'yearly' => esc_html__('Yearly', 'calio-addon')], 'condition' => ['enable_billing_switch' => 'yes']]);
        $this->add_control('mobile_compare_mode', ['label' => esc_html__('Mobile Compare Mode', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'stacked', 'options' => ['stacked' => esc_html__('Stacked', 'calio-addon'), 'plan-focus' => esc_html__('Plan Focus', 'calio-addon')]]);
        $this->add_control('show_feature_labels', ['label' => esc_html__('Show Feature Labels', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => 'yes']);
        $this->add_control('collapse_rows', ['label' => esc_html__('Collapse Rows', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => '']);
        $this->add_control('collapsed_rows', ['label' => esc_html__('Rows Before Collapse', 'calio-addon'), 'type' => Controls_Manager::NUMBER, 'default' => 5, 'min' => 1, 'max' => 20, 'condition' => ['collapse_rows' => 'yes']]);
        $this->add_control('collapse_more_text', ['label' => esc_html__('Show More Text', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('See More', 'calio-addon'), 'condition' => ['collapse_rows' => 'yes']]);
        $this->add_control('collapse_less_text', ['label' => esc_html__('Show Less Text', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('See Less', 'calio-addon'), 'condition' => ['collapse_rows' => 'yes']]);
        $this->end_controls_section();

        $feature_repeater = new Repeater();
        $feature_repeater->add_control('row_kind', ['label' => esc_html__('Row Type', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'feature', 'options' => ['feature' => esc_html__('Feature Row', 'calio-addon'), 'group' => esc_html__('Group Heading', 'calio-addon')]]);
        $feature_repeater->add_control('label', ['label' => esc_html__('Feature Label', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('Total Features', 'calio-addon')]);
        $feature_repeater->add_control('row_note', ['label' => esc_html__('Group Note', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '', 'condition' => ['row_kind' => 'group']]);
        $feature_repeater->add_control('feature_note', ['label' => esc_html__('Feature Note', 'calio-addon'), 'type' => Controls_Manager::TEXTAREA, 'rows' => 3, 'default' => '', 'condition' => ['row_kind' => 'feature']]);
        $feature_repeater->add_control('value_1_type', ['label' => esc_html__('Plan 1 Type', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'text', 'options' => ['text' => esc_html__('Text', 'calio-addon'), 'check' => esc_html__('Check', 'calio-addon'), 'cross' => esc_html__('Cross', 'calio-addon'), 'star' => esc_html__('Star', 'calio-addon'), 'bolt' => esc_html__('Bolt', 'calio-addon'), 'shield' => esc_html__('Shield', 'calio-addon')], 'condition' => ['row_kind' => 'feature']]);
        $feature_repeater->add_control('value_1', ['label' => esc_html__('Plan 1 Value', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '50', 'condition' => ['row_kind' => 'feature', 'value_1_type' => 'text']]);
        $feature_repeater->add_control('value_2_type', ['label' => esc_html__('Plan 2 Type', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'text', 'options' => ['text' => esc_html__('Text', 'calio-addon'), 'check' => esc_html__('Check', 'calio-addon'), 'cross' => esc_html__('Cross', 'calio-addon'), 'star' => esc_html__('Star', 'calio-addon'), 'bolt' => esc_html__('Bolt', 'calio-addon'), 'shield' => esc_html__('Shield', 'calio-addon')], 'condition' => ['row_kind' => 'feature']]);
        $feature_repeater->add_control('value_2', ['label' => esc_html__('Plan 2 Value', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '100', 'condition' => ['row_kind' => 'feature', 'value_2_type' => 'text']]);
        $feature_repeater->add_control('value_3_type', ['label' => esc_html__('Plan 3 Type', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'text', 'options' => ['text' => esc_html__('Text', 'calio-addon'), 'check' => esc_html__('Check', 'calio-addon'), 'cross' => esc_html__('Cross', 'calio-addon'), 'star' => esc_html__('Star', 'calio-addon'), 'bolt' => esc_html__('Bolt', 'calio-addon'), 'shield' => esc_html__('Shield', 'calio-addon')], 'condition' => ['row_kind' => 'feature']]);
        $feature_repeater->add_control('value_3', ['label' => esc_html__('Plan 3 Value', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => 'Unlimited', 'condition' => ['row_kind' => 'feature', 'value_3_type' => 'text']]);
        $feature_repeater->add_control('value_4_type', ['label' => esc_html__('Plan 4 Type', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'text', 'options' => ['text' => esc_html__('Text', 'calio-addon'), 'check' => esc_html__('Check', 'calio-addon'), 'cross' => esc_html__('Cross', 'calio-addon'), 'star' => esc_html__('Star', 'calio-addon'), 'bolt' => esc_html__('Bolt', 'calio-addon'), 'shield' => esc_html__('Shield', 'calio-addon')], 'condition' => ['row_kind' => 'feature']]);
        $feature_repeater->add_control('value_4', ['label' => esc_html__('Plan 4 Value', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => 'Unlimited', 'condition' => ['row_kind' => 'feature', 'value_4_type' => 'text']]);
        $feature_repeater->add_control('row_emphasis', ['label' => esc_html__('Row Emphasis', 'calio-addon'), 'type' => Controls_Manager::SELECT, 'default' => 'none', 'options' => ['none' => esc_html__('None', 'calio-addon'), 'soft' => esc_html__('Soft', 'calio-addon'), 'spotlight' => esc_html__('Spotlight', 'calio-addon'), 'contrast' => esc_html__('Contrast', 'calio-addon')], 'condition' => ['row_kind' => 'feature']]);
        $feature_repeater->add_control('highlight_row', ['label' => esc_html__('Highlight Row', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => '', 'condition' => ['row_kind' => 'feature']]);

        $this->start_controls_section('features', ['label' => esc_html__('Feature Rows', 'calio-addon')]);
        $this->add_control('feature_rows', [
            'type' => Controls_Manager::REPEATER,
            'fields' => $feature_repeater->get_controls(),
            'default' => [
                ['row_kind' => 'group', 'label' => esc_html__('Platform', 'calio-addon'), 'row_note' => esc_html__('Core scale and limits', 'calio-addon')],
                ['label' => esc_html__('Total Features', 'calio-addon'), 'feature_note' => esc_html__('Maximum enabled modules and usage surfaces.', 'calio-addon'), 'value_1' => '50', 'value_2' => '100', 'value_3' => 'Unlimited', 'value_4' => 'Unlimited', 'row_emphasis' => 'soft'],
                ['label' => esc_html__('Support & Update', 'calio-addon'), 'value_1' => esc_html__('Standard', 'calio-addon'), 'value_2' => esc_html__('Priority', 'calio-addon'), 'value_3' => esc_html__('Priority', 'calio-addon'), 'value_4' => esc_html__('VIP', 'calio-addon')],
                ['label' => esc_html__('Ready Templates', 'calio-addon'), 'value_1' => '20', 'value_2' => '60', 'value_3' => '120', 'value_4' => 'Unlimited'],
                ['row_kind' => 'group', 'label' => esc_html__('Automation', 'calio-addon'), 'row_note' => esc_html__('Workflow and team capabilities', 'calio-addon')],
                ['label' => esc_html__('Advance Search', 'calio-addon'), 'feature_note' => esc_html__('Faster query tooling and advanced lookup presets.', 'calio-addon'), 'value_1_type' => 'cross', 'value_2_type' => 'check', 'value_3_type' => 'bolt', 'value_4_type' => 'bolt', 'row_emphasis' => 'spotlight'],
                ['label' => esc_html__('Internal Documentation', 'calio-addon'), 'value_1_type' => 'cross', 'value_2_type' => 'check', 'value_3_type' => 'shield', 'value_4_type' => 'shield', 'row_emphasis' => 'contrast'],
            ],
            'title_field' => '{{{ label }}}',
        ]);
        $this->end_controls_section();

        $this->register_plan_controls(1, esc_html__('Plan 1', 'calio-addon'), true);
        $this->register_plan_controls(2, esc_html__('Plan 2', 'calio-addon'));
        $this->register_plan_controls(3, esc_html__('Plan 3', 'calio-addon'));
        $this->register_plan_controls(4, esc_html__('Plan 4', 'calio-addon'));

        $this->start_controls_section('style_headers', ['label' => esc_html__('Plan Cards', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'plan_title_typography', 'selector' => '{{WRAPPER}} .calio-mc-pricing-plan-title']);
        $this->add_style_heading('pricing_card_surface_heading', esc_html__('Surface States', 'calio-addon'));
        $this->start_controls_tabs('pricing_card_state_tabs');
        $this->start_controls_tab('pricing_card_state_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_text_paint_controls('pricing_plan_title_normal', esc_html__('Title Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-plan-title');
        $this->add_text_paint_controls('pricing_plan_price_normal', esc_html__('Price Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-price');
        $this->add_background_type_control('pricing_plan_card_bg_normal_group', '{{WRAPPER}} .calio-mc-pricing-plan-card');
        $this->end_controls_tab();
        $this->start_controls_tab('pricing_card_state_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_text_paint_controls('pricing_plan_title_hover', esc_html__('Title Color', 'calio-addon'), ['{{WRAPPER}} .calio-mc-pricing-plan-card:hover .calio-mc-pricing-plan-title', '{{WRAPPER}} .calio-mc-pricing-plan-card:focus-within .calio-mc-pricing-plan-title']);
        $this->add_text_paint_controls('pricing_plan_price_hover', esc_html__('Price Color', 'calio-addon'), ['{{WRAPPER}} .calio-mc-pricing-plan-card:hover .calio-mc-pricing-price', '{{WRAPPER}} .calio-mc-pricing-plan-card:focus-within .calio-mc-pricing-price']);
        $this->add_background_type_control('pricing_plan_card_bg_hover_group', ['{{WRAPPER}} .calio-mc-pricing-plan-card:hover', '{{WRAPPER}} .calio-mc-pricing-plan-card:focus-within']);
        $this->end_controls_tab();
        $this->start_controls_tab('pricing_card_state_featured', ['label' => esc_html__('Featured', 'calio-addon')]);
        $this->add_background_type_control('pricing_plan_card_bg_featured_group', '{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-plan-card');
        $this->add_text_paint_controls('pricing_plan_title_featured', esc_html__('Title Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-plan-title');
        $this->add_text_paint_controls('pricing_plan_price_featured', esc_html__('Price Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-price');
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('pricing_card_outline_heading', esc_html__('Outline & Shadow', 'calio-addon'));
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'plan_card_border', 'selector' => '{{WRAPPER}} .calio-mc-pricing-plan-card']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'plan_card_shadow', 'selector' => '{{WRAPPER}} .calio-mc-pricing-plan-card']);
        $this->add_style_heading('pricing_card_spacing_heading', esc_html__('Shape & Spacing', 'calio-addon'));
        $this->add_dimensions_control('plan_card_padding', esc_html__('Padding', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-plan-card', 'padding');
        $this->add_dimensions_control('plan_card_margin', esc_html__('Margin', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-plan-card', 'margin');
        $this->add_dimensions_control('plan_card_radius', esc_html__('Radius', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-plan-card', 'border-radius', ['size_units' => ['px', '%']]);
        $this->end_controls_section();

        $this->start_controls_section('style_header_meta', ['label' => esc_html__('Plan Header', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'plan_subtitle_typography', 'selector' => '{{WRAPPER}} .calio-mc-pricing-plan-subtitle, {{WRAPPER}} .calio-mc-pricing-period, {{WRAPPER}} .calio-mc-pricing-old-price']);
        $this->add_style_heading('pricing_header_meta_surface_heading', esc_html__('Meta States', 'calio-addon'));
        $this->start_controls_tabs('pricing_header_meta_tabs');
        $this->start_controls_tab('pricing_header_meta_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_text_paint_controls('pricing_plan_meta_normal', esc_html__('Subtitle & Meta', 'calio-addon'), ['{{WRAPPER}} .calio-mc-pricing-plan-subtitle', '{{WRAPPER}} .calio-mc-pricing-period', '{{WRAPPER}} .calio-mc-pricing-old-price']);
        $this->end_controls_tab();
        $this->start_controls_tab('pricing_header_meta_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_text_paint_controls('pricing_plan_meta_hover', esc_html__('Subtitle & Meta', 'calio-addon'), ['{{WRAPPER}} .calio-mc-pricing-plan-card:hover .calio-mc-pricing-plan-subtitle', '{{WRAPPER}} .calio-mc-pricing-plan-card:hover .calio-mc-pricing-period', '{{WRAPPER}} .calio-mc-pricing-plan-card:hover .calio-mc-pricing-old-price', '{{WRAPPER}} .calio-mc-pricing-plan-card:focus-within .calio-mc-pricing-plan-subtitle', '{{WRAPPER}} .calio-mc-pricing-plan-card:focus-within .calio-mc-pricing-period', '{{WRAPPER}} .calio-mc-pricing-plan-card:focus-within .calio-mc-pricing-old-price']);
        $this->end_controls_tab();
        $this->start_controls_tab('pricing_header_meta_featured', ['label' => esc_html__('Featured', 'calio-addon')]);
        $this->add_text_paint_controls('pricing_plan_meta_featured', esc_html__('Subtitle & Meta', 'calio-addon'), ['{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-plan-subtitle', '{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-period', '{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-old-price']);
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('pricing_header_meta_spacing_heading', esc_html__('Spacing', 'calio-addon'));
        $this->add_dimensions_control('plan_header_padding', esc_html__('Header Padding', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-plan-header', 'padding');
        $this->add_dimensions_control('plan_header_margin', esc_html__('Header Margin', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-plan-header', 'margin');
        $this->add_control('plan_price_inline_gap', [
            'label' => esc_html__('Price Label Gap', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range' => [
                'px' => ['min' => 0, 'max' => 40],
            ],
            'default' => ['unit' => 'px', 'size' => 10],
            'selectors' => [
                '{{WRAPPER}} .calio-mc-pricing-price-wrap' => '--calio-mc-price-inline-gap: {{SIZE}}{{UNIT}};',
            ],
        ]);
        $this->add_control('plan_price_stack_gap', [
            'label' => esc_html__('Monthly / Yearly Gap', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range' => [
                'px' => ['min' => 0, 'max' => 28],
            ],
            'default' => ['unit' => 'px', 'size' => 0],
            'selectors' => [
                '{{WRAPPER}} .calio-mc-pricing-price-stack' => '--calio-mc-price-stack-gap: {{SIZE}}{{UNIT}};',
            ],
        ]);
        $this->add_control('plan_price_block_spacing', [
            'label' => esc_html__('Price Block Top Spacing', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range' => [
                'px' => ['min' => 0, 'max' => 40],
            ],
            'default' => ['unit' => 'px', 'size' => 0],
            'selectors' => [
                '{{WRAPPER}} .calio-mc-pricing-price-stack' => 'margin-top: {{SIZE}}{{UNIT}};',
            ],
        ]);
        $this->end_controls_section();

        $this->start_controls_section('style_rows', ['label' => esc_html__('Rows', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'row_typography', 'selector' => '{{WRAPPER}} .calio-mc-pricing-row-label, {{WRAPPER}} .calio-mc-pricing-row-value']);
        $this->add_style_heading('pricing_row_surface_heading', esc_html__('Row States', 'calio-addon'));
        $this->start_controls_tabs('pricing_row_state_tabs');
        $this->start_controls_tab('pricing_row_state_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_text_paint_controls('pricing_row_label', esc_html__('Label Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-row-label');
        $this->add_text_paint_controls('pricing_row_value', esc_html__('Value Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-row-value');
        $this->add_background_type_control('pricing_row_bg_normal_group', '{{WRAPPER}} .calio-mc-pricing-row > *');
        $this->end_controls_tab();
        $this->start_controls_tab('pricing_row_state_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_background_type_control('pricing_row_bg_hover_group', '{{WRAPPER}} .calio-mc-pricing-row:hover > *');
        $this->end_controls_tab();
        $this->start_controls_tab('pricing_row_state_highlight', ['label' => esc_html__('Highlight', 'calio-addon')]);
        $this->add_background_type_control('pricing_row_bg_highlight_group', '{{WRAPPER}} .calio-mc-pricing-row.is-highlight > *');
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('pricing_row_meta_heading', esc_html__('Indicators & Notes', 'calio-addon'));
        $this->add_text_paint_controls('pricing_group_heading_text', esc_html__('Group Heading Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-group-label');
        $this->add_background_type_control('pricing_group_heading_bg_group', ['{{WRAPPER}} .calio-mc-pricing-group-label', '{{WRAPPER}} .calio-mc-pricing-group-note']);
        $this->add_text_paint_controls('pricing_indicator_check', esc_html__('Check Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-indicator.is-check');
        $this->add_text_paint_controls('pricing_indicator_cross', esc_html__('Cross Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-indicator.is-cross');
        $this->add_text_paint_controls('pricing_indicator_star', esc_html__('Star Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-indicator.is-star');
        $this->add_text_paint_controls('pricing_indicator_bolt', esc_html__('Bolt Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-indicator.is-bolt');
        $this->add_text_paint_controls('pricing_indicator_shield', esc_html__('Shield Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-indicator.is-shield');
        $this->add_text_paint_controls('pricing_feature_note_text', esc_html__('Feature Note Color', 'calio-addon'), ['{{WRAPPER}} .calio-mc-pricing-row-note-icon', '{{WRAPPER}} .calio-mc-pricing-row-note-tooltip']);
        $this->add_background_type_control('pricing_feature_note_bg_group', ['{{WRAPPER}} .calio-mc-pricing-row-note-tooltip', '{{WRAPPER}} .calio-mc-pricing-row-note-icon']);
        $this->add_text_paint_controls('pricing_featured_column_text', esc_html__('Featured Column Text', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-row-value.is-featured');
        $this->add_background_type_control('pricing_featured_column_bg_group', '{{WRAPPER}} .calio-mc-pricing-row-value.is-featured');
        $this->add_background_type_control('pricing_featured_column_bg_hover_group', '{{WRAPPER}} .calio-mc-pricing-row:hover .calio-mc-pricing-row-value.is-featured');
        $this->add_background_type_control('pricing_featured_chip_bg_group', '{{WRAPPER}} .calio-mc-pricing-mobile-chip-badge');
        $this->end_controls_section();

        $this->start_controls_section('style_ribbon', ['label' => esc_html__('Badge & Ribbon', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_style_heading('pricing_badge_surface_heading', esc_html__('Surface', 'calio-addon'));
        $this->add_text_paint_controls('pricing_badge_text', esc_html__('Badge Text', 'calio-addon'), ['{{WRAPPER}} .calio-mc-pricing-badge', '{{WRAPPER}} .calio-mc-pricing-featured-ribbon']);
        $this->add_background_type_control('pricing_badge_bg_group', ['{{WRAPPER}} .calio-mc-pricing-badge', '{{WRAPPER}} .calio-mc-pricing-featured-ribbon']);
        $this->add_style_heading('pricing_badge_spacing_heading', esc_html__('Shape & Spacing', 'calio-addon'));
        $this->add_dimensions_control('badge_padding', esc_html__('Badge Padding', 'calio-addon'), ['{{WRAPPER}} .calio-mc-pricing-badge', '{{WRAPPER}} .calio-mc-pricing-featured-ribbon'], 'padding');
        $this->add_dimensions_control('badge_margin', esc_html__('Badge Margin', 'calio-addon'), ['{{WRAPPER}} .calio-mc-pricing-badge', '{{WRAPPER}} .calio-mc-pricing-featured-ribbon'], 'margin');
        $this->add_dimensions_control('badge_radius', esc_html__('Badge Radius', 'calio-addon'), ['{{WRAPPER}} .calio-mc-pricing-badge', '{{WRAPPER}} .calio-mc-pricing-featured-ribbon'], 'border-radius', ['size_units' => ['px', '%']]);
        $this->end_controls_section();

        $this->start_controls_section('style_billing_switcher', ['label' => esc_html__('Billing Switcher', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => ['enable_billing_switch' => 'yes']]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'billing_switcher_typography', 'selector' => '{{WRAPPER}} .calio-mc-pricing-cycle-label, {{WRAPPER}} .calio-mc-pricing-cycle-badge']);
        $this->add_style_heading('pricing_switcher_surface_heading', esc_html__('Surface States', 'calio-addon'));
        $this->start_controls_tabs('billing_switcher_tabs');
        $this->start_controls_tab('billing_switcher_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_text_paint_controls('pricing_billing_text_normal', esc_html__('Text Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-cycle-label');
        $this->add_background_type_control('pricing_billing_bg_normal_group', '{{WRAPPER}} .calio-mc-pricing-billing-switcher');
        $this->end_controls_tab();
        $this->start_controls_tab('billing_switcher_active', ['label' => esc_html__('Active', 'calio-addon')]);
        $this->add_text_paint_controls('pricing_billing_text_active', esc_html__('Active Text', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-cycle-label.is-active');
        $this->add_background_type_control('pricing_billing_knob_bg_group', '{{WRAPPER}} .calio-mc-pricing-billing-knob');
        $this->add_text_paint_controls('pricing_billing_badge_text', esc_html__('Badge Text', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-cycle-badge');
        $this->add_background_type_control('pricing_billing_badge_bg_group', '{{WRAPPER}} .calio-mc-pricing-cycle-badge');
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('pricing_switcher_outline_heading', esc_html__('Border Type & Shadow', 'calio-addon'));
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'pricing_switcher_border', 'selector' => '{{WRAPPER}} .calio-mc-pricing-billing-switcher']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'pricing_switcher_shadow', 'selector' => '{{WRAPPER}} .calio-mc-pricing-billing-switcher']);
        $this->add_style_heading('pricing_switcher_spacing_heading', esc_html__('Shape & Spacing', 'calio-addon'));
        $this->add_dimensions_control('billing_switcher_padding', esc_html__('Padding', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-billing-switcher', 'padding');
        $this->add_dimensions_control('billing_switcher_margin', esc_html__('Margin', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-billing-switcher', 'margin');
        $this->add_dimensions_control('billing_switcher_radius', esc_html__('Radius', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-billing-switcher', 'border-radius', ['size_units' => ['px', '%']]);
        $this->add_control('billing_switcher_label_gap', [
            'label' => esc_html__('Label Gap', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range' => [
                'px' => ['min' => 6, 'max' => 30],
            ],
            'default' => ['unit' => 'px', 'size' => 14],
            'selectors' => [
                '{{WRAPPER}} .calio-mc-pricing-billing-switcher' => '--calio-mc-switcher-label-gap: {{SIZE}}{{UNIT}};',
            ],
        ]);
        $this->add_control('billing_switcher_icon_size', [
            'label' => esc_html__('Icon Size', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range' => [
                'px' => ['min' => 10, 'max' => 28],
            ],
            'default' => ['unit' => 'px', 'size' => 13],
            'selectors' => [
                '{{WRAPPER}} .calio-mc-pricing-cycle-icon i' => 'font-size: {{SIZE}}{{UNIT}};',
                '{{WRAPPER}} .calio-mc-pricing-cycle-icon svg' => 'width: {{SIZE}}{{UNIT}}; height: {{SIZE}}{{UNIT}};',
            ],
        ]);
        $this->add_control('billing_switcher_bottom_gap', [
            'label' => esc_html__('Bottom Spacing', 'calio-addon'),
            'type' => Controls_Manager::SLIDER,
            'size_units' => ['px'],
            'range' => [
                'px' => ['min' => 0, 'max' => 40],
            ],
            'default' => ['unit' => 'px', 'size' => 0],
            'selectors' => [
                '{{WRAPPER}} .calio-mc-pricing-billing-wrap' => 'margin-bottom: {{SIZE}}{{UNIT}};',
            ],
        ]);
        $this->end_controls_section();

        $this->start_controls_section('style_button', ['label' => esc_html__('Buttons', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'button_typography', 'selector' => '{{WRAPPER}} .calio-mc-pricing-button']);
        $this->add_style_heading('pricing_button_surface_heading', esc_html__('Surface States', 'calio-addon'));
        $this->start_controls_tabs('pricing_button_state_tabs');
        $this->start_controls_tab('pricing_button_state_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_text_paint_controls('pricing_button_text_normal', esc_html__('Text Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-button');
        $this->add_background_type_control('pricing_button_bg_normal_group', '{{WRAPPER}} .calio-mc-pricing-button');
        $this->end_controls_tab();
        $this->start_controls_tab('pricing_button_state_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_text_paint_controls('pricing_button_text_hover', esc_html__('Text Color', 'calio-addon'), ['{{WRAPPER}} .calio-mc-pricing-button:hover', '{{WRAPPER}} .calio-mc-pricing-button:focus-visible']);
        $this->add_background_type_control('pricing_button_bg_hover_group', ['{{WRAPPER}} .calio-mc-pricing-button:hover', '{{WRAPPER}} .calio-mc-pricing-button:focus-visible']);
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('pricing_button_outline_heading', esc_html__('Border Type & Shadow', 'calio-addon'));
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'pricing_button_border_group', 'selector' => '{{WRAPPER}} .calio-mc-pricing-button']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'pricing_button_shadow_group', 'selector' => '{{WRAPPER}} .calio-mc-pricing-button']);
        $this->add_style_heading('pricing_button_spacing_heading', esc_html__('Shape & Spacing', 'calio-addon'));
        $this->add_dimensions_control('button_padding', esc_html__('Padding', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-button', 'padding');
        $this->add_dimensions_control('button_margin', esc_html__('Margin', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-button', 'margin');
        $this->add_dimensions_control('button_radius', esc_html__('Radius', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-button', 'border-radius', ['size_units' => ['px', '%']]);
        $this->end_controls_section();

        $this->start_controls_section('style_featured_button', ['label' => esc_html__('Featured Button', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_style_heading('pricing_featured_button_surface_heading', esc_html__('Surface States', 'calio-addon'));
        $this->start_controls_tabs('pricing_featured_button_state_tabs');
        $this->start_controls_tab('pricing_featured_button_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_text_paint_controls('pricing_featured_button_text_normal', esc_html__('Text Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-button');
        $this->add_background_type_control('pricing_featured_button_bg_normal_group', '{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-button');
        $this->add_border_paint_controls('pricing_featured_button_border_normal', esc_html__('Border Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-button');
        $this->end_controls_tab();
        $this->start_controls_tab('pricing_featured_button_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_text_paint_controls('pricing_featured_button_text_hover', esc_html__('Text Color', 'calio-addon'), ['{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-button:hover', '{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-button:focus-visible']);
        $this->add_background_type_control('pricing_featured_button_bg_hover_group', ['{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-button:hover', '{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-button:focus-visible']);
        $this->add_border_paint_controls('pricing_featured_button_border_hover', esc_html__('Border Color', 'calio-addon'), ['{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-button:hover', '{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-button:focus-visible']);
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('pricing_featured_button_outline_heading', esc_html__('Border Type & Shadow', 'calio-addon'));
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'pricing_featured_button_border_group', 'selector' => '{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-button']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'pricing_featured_button_shadow_group', 'selector' => '{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-button']);
        $this->add_style_heading('pricing_featured_button_spacing_heading', esc_html__('Shape & Spacing', 'calio-addon'));
        $this->add_dimensions_control('featured_button_padding', esc_html__('Padding', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-button', 'padding');
        $this->add_dimensions_control('featured_button_margin', esc_html__('Margin', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-button', 'margin');
        $this->add_dimensions_control('featured_button_radius', esc_html__('Radius', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-plan.is-featured .calio-mc-pricing-button', 'border-radius', ['size_units' => ['px', '%']]);
        $this->end_controls_section();

        $this->start_controls_section('style_footer', ['label' => esc_html__('Table Footer', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'footer_button_typography', 'selector' => '{{WRAPPER}} .calio-mc-pricing-toggle']);
        $this->add_style_heading('pricing_footer_surface_heading', esc_html__('Surface States', 'calio-addon'));
        $this->start_controls_tabs('pricing_footer_state_tabs');
        $this->start_controls_tab('pricing_footer_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_text_paint_controls('pricing_footer_toggle_text_normal', esc_html__('Text Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-toggle');
        $this->add_background_type_control('pricing_footer_toggle_bg_normal_group', '{{WRAPPER}} .calio-mc-pricing-toggle');
        $this->end_controls_tab();
        $this->start_controls_tab('pricing_footer_hover', ['label' => esc_html__('Hover', 'calio-addon')]);
        $this->add_text_paint_controls('pricing_footer_toggle_text_hover', esc_html__('Text Color', 'calio-addon'), ['{{WRAPPER}} .calio-mc-pricing-toggle:hover', '{{WRAPPER}} .calio-mc-pricing-toggle:focus-visible']);
        $this->add_background_type_control('pricing_footer_toggle_bg_hover_group', ['{{WRAPPER}} .calio-mc-pricing-toggle:hover', '{{WRAPPER}} .calio-mc-pricing-toggle:focus-visible']);
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('pricing_footer_outline_heading', esc_html__('Border Type & Shadow', 'calio-addon'));
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'pricing_footer_toggle_border', 'selector' => '{{WRAPPER}} .calio-mc-pricing-toggle']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'pricing_footer_toggle_shadow', 'selector' => '{{WRAPPER}} .calio-mc-pricing-toggle']);
        $this->add_style_heading('pricing_footer_spacing_heading', esc_html__('Spacing', 'calio-addon'));
        $this->add_dimensions_control('footer_padding', esc_html__('Padding', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-footer', 'padding');
        $this->add_dimensions_control('footer_margin', esc_html__('Margin', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-footer', 'margin');
        $this->end_controls_section();

        $this->start_controls_section('style_mobile_focus', ['label' => esc_html__('Mobile Plan Focus', 'calio-addon'), 'tab' => Controls_Manager::TAB_STYLE, 'condition' => ['mobile_compare_mode' => 'plan-focus']]);
        $this->add_group_control(Group_Control_Typography::get_type(), ['name' => 'mobile_focus_typography', 'selector' => '{{WRAPPER}} .calio-mc-pricing-mobile-chip']);
        $this->add_style_heading('pricing_mobile_focus_surface_heading', esc_html__('Surface States', 'calio-addon'));
        $this->start_controls_tabs('mobile_focus_state_tabs');
        $this->start_controls_tab('mobile_focus_state_normal', ['label' => esc_html__('Normal', 'calio-addon')]);
        $this->add_text_paint_controls('pricing_mobile_focus_text_normal', esc_html__('Text Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-mobile-chip');
        $this->add_background_type_control('pricing_mobile_focus_bg_normal_group', '{{WRAPPER}} .calio-mc-pricing-mobile-chip');
        $this->end_controls_tab();
        $this->start_controls_tab('mobile_focus_state_active', ['label' => esc_html__('Active', 'calio-addon')]);
        $this->add_text_paint_controls('pricing_mobile_focus_text_active', esc_html__('Text Color', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-mobile-chip.is-active');
        $this->add_background_type_control('pricing_mobile_focus_bg_active_group', '{{WRAPPER}} .calio-mc-pricing-mobile-chip.is-active');
        $this->end_controls_tab();
        $this->end_controls_tabs();
        $this->add_style_heading('pricing_mobile_focus_outline_heading', esc_html__('Border Type & Shadow', 'calio-addon'));
        $this->add_group_control(Group_Control_Border::get_type(), ['name' => 'pricing_mobile_focus_border', 'selector' => '{{WRAPPER}} .calio-mc-pricing-mobile-chip']);
        $this->add_group_control(Group_Control_Box_Shadow::get_type(), ['name' => 'pricing_mobile_focus_shadow', 'selector' => '{{WRAPPER}} .calio-mc-pricing-mobile-chip']);
        $this->add_style_heading('pricing_mobile_focus_spacing_heading', esc_html__('Shape & Spacing', 'calio-addon'));
        $this->add_dimensions_control('mobile_focus_padding', esc_html__('Padding', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-mobile-chip', 'padding');
        $this->add_dimensions_control('mobile_focus_margin', esc_html__('Margin', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-mobile-chip', 'margin');
        $this->add_dimensions_control('mobile_focus_radius', esc_html__('Radius', 'calio-addon'), '{{WRAPPER}} .calio-mc-pricing-mobile-chip', 'border-radius', ['size_units' => ['px', '%']]);
        $this->end_controls_section();
    }

    /**
     * Registers controls for a single plan column.
     */
    private function register_plan_controls(int $index, string $label, bool $required = false): void {
        $condition = $required ? [] : ['plan_count!' => (string) ($index - 1)];
        $prefix = 'plan_' . $index . '_';

        $this->start_controls_section($prefix . 'section', ['label' => $label, 'condition' => $condition]);
        $this->add_control($prefix . 'title', ['label' => esc_html__('Title', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => $this->get_plan_default($index, 'title')]);
        $this->add_control($prefix . 'subtitle', ['label' => esc_html__('Subtitle', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => $this->get_plan_default($index, 'subtitle')]);
        $this->add_control($prefix . 'price', ['label' => esc_html__('Price', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => $this->get_plan_default($index, 'price')]);
        $this->add_control($prefix . 'old_price', ['label' => esc_html__('Old Price', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control($prefix . 'period', ['label' => esc_html__('Period', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('/mo', 'calio-addon')]);
        $this->add_control($prefix . 'yearly_price', ['label' => esc_html__('Yearly Price', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => $this->get_plan_default($index, 'yearly_price')]);
        $this->add_control($prefix . 'yearly_old_price', ['label' => esc_html__('Yearly Old Price', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => '']);
        $this->add_control($prefix . 'yearly_period', ['label' => esc_html__('Yearly Period', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('/yr', 'calio-addon')]);
        $this->add_control($prefix . 'badge', ['label' => esc_html__('Badge', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => $this->get_plan_default($index, 'badge')]);
        $this->add_control($prefix . 'featured', ['label' => esc_html__('Featured Plan', 'calio-addon'), 'type' => Controls_Manager::SWITCHER, 'return_value' => 'yes', 'default' => $index === 2 ? 'yes' : '']);
        $this->add_control($prefix . 'button_text', ['label' => esc_html__('Button Text', 'calio-addon'), 'type' => Controls_Manager::TEXT, 'default' => esc_html__('Get Started', 'calio-addon')]);
        $this->add_control($prefix . 'button_url', ['label' => esc_html__('Button URL', 'calio-addon'), 'type' => Controls_Manager::URL, 'placeholder' => 'https://example.com']);
        $this->end_controls_section();
    }

    /**
     * Returns compact defaults for each pricing plan.
     */
    private function get_plan_default(int $index, string $field): string {
        $defaults = [
            1 => ['title' => 'Solo', 'subtitle' => esc_html__('For individuals', 'calio-addon'), 'price' => '$100', 'yearly_price' => '$960', 'badge' => ''],
            2 => ['title' => 'Team', 'subtitle' => esc_html__('For growing teams', 'calio-addon'), 'price' => '$240', 'yearly_price' => '$2,280', 'badge' => esc_html__('Best Value', 'calio-addon')],
            3 => ['title' => 'Enterprise', 'subtitle' => esc_html__('For large orgs', 'calio-addon'), 'price' => '$350', 'yearly_price' => '$3,360', 'badge' => ''],
            4 => ['title' => 'Platinum', 'subtitle' => esc_html__('For unlimited scale', 'calio-addon'), 'price' => '$590', 'yearly_price' => '$5,640', 'badge' => esc_html__('All Access', 'calio-addon')],
        ];

        return $defaults[$index][$field] ?? '';
    }

    /**
     * Outputs the pricing comparison grid.
     */
    protected function render(): void {
        $settings = $this->get_settings_for_display();
        $plans = $this->get_plans($settings);
        $rows = isset($settings['feature_rows']) && is_array($settings['feature_rows']) ? $settings['feature_rows'] : [];

        if (count($plans) < 2 || empty($rows)) {
            echo '<div class="calio-mc-pricing-empty">' . esc_html__('Add at least two plans and one feature row.', 'calio-addon') . '</div>';
            return;
        }

        $wrapper_classes = [
            'calio-mc-pricing-table',
            'skin-' . sanitize_html_class((string) ($settings['skin'] ?? 'soft')),
            'featured-skin-' . sanitize_html_class((string) ($settings['featured_skin'] ?? 'spotlight')),
            'mobile-mode-' . sanitize_html_class((string) ($settings['mobile_compare_mode'] ?? 'stacked')),
            'cols-' . count($plans),
        ];

        $show_labels = ($settings['show_feature_labels'] ?? 'yes') === 'yes';
        $collapse_rows = ($settings['collapse_rows'] ?? '') === 'yes';
        $collapsed_rows = max(1, min(20, (int) ($settings['collapsed_rows'] ?? 5)));
        $should_collapse = $collapse_rows && count($rows) > $collapsed_rows;
        $has_switcher = ($settings['enable_billing_switch'] ?? '') === 'yes';
        $default_billing = (string) ($settings['default_billing_cycle'] ?? 'monthly');
        if (!in_array($default_billing, ['monthly', 'yearly'], true)) {
            $default_billing = 'monthly';
        }

        echo '<div class="' . esc_attr(implode(' ', $wrapper_classes)) . '"' . ($has_switcher ? ' data-billing-default="' . esc_attr($default_billing) . '"' : '') . '>';

        if ($has_switcher) {
            $monthly_label = trim((string) ($settings['monthly_label'] ?? esc_html__('Monthly', 'calio-addon')));
            $yearly_label = trim((string) ($settings['yearly_label'] ?? esc_html__('Yearly', 'calio-addon')));
            $yearly_badge = trim((string) ($settings['yearly_badge'] ?? ''));
            $is_yearly_default = $default_billing === 'yearly';
            $show_switcher_icons = ($settings['show_switcher_icons'] ?? 'yes') === 'yes';
            $monthly_icon = $show_switcher_icons ? $this->render_switcher_icon($settings['monthly_icon'] ?? []) : '';
            $yearly_icon = $show_switcher_icons ? $this->render_switcher_icon($settings['yearly_icon'] ?? []) : '';
            echo '<div class="calio-mc-pricing-billing-wrap">';
            echo '<button class="calio-mc-pricing-billing-switcher" type="button" aria-pressed="' . esc_attr($is_yearly_default ? 'true' : 'false') . '">';
            echo '<span class="' . esc_attr('calio-mc-pricing-cycle-label' . (!$is_yearly_default ? ' is-active' : '')) . '" data-cycle="' . esc_attr('monthly') . '">';
            if ($monthly_icon !== '') {
                echo '<span class="calio-mc-pricing-cycle-icon" aria-hidden="true">' . wp_kses($monthly_icon, $this->calio_allowed_icon_markup()) . '</span>';
            }
            echo '<span class="calio-mc-pricing-cycle-text">' . esc_html($monthly_label !== '' ? $monthly_label : esc_html__('Monthly', 'calio-addon')) . '</span>';
            echo '</span>';
            echo '<span class="calio-mc-pricing-billing-knob" aria-hidden="true"></span>';
            echo '<span class="' . esc_attr('calio-mc-pricing-cycle-label' . ($is_yearly_default ? ' is-active' : '')) . '" data-cycle="' . esc_attr('yearly') . '">';
            if ($yearly_icon !== '') {
                echo '<span class="calio-mc-pricing-cycle-icon" aria-hidden="true">' . wp_kses($yearly_icon, $this->calio_allowed_icon_markup()) . '</span>';
            }
            echo '<span class="calio-mc-pricing-cycle-text">' . esc_html($yearly_label !== '' ? $yearly_label : esc_html__('Yearly', 'calio-addon')) . '</span>';
            echo '</span>';
            echo '</button>';
            if ($yearly_badge !== '') {
                echo '<span class="calio-mc-pricing-cycle-badge">' . esc_html($yearly_badge) . '</span>';
            }
            echo '</div>';
        }

        if (($settings['mobile_compare_mode'] ?? 'stacked') === 'plan-focus') {
            $featured_index = $this->get_featured_plan_index($plans);
            echo '<div class="calio-mc-pricing-mobile-focus" data-mobile-default="' . esc_attr((string) $featured_index) . '">';
            foreach ($plans as $plan_index => $plan) {
                $is_active = $plan_index === $featured_index;
                echo '<button class="' . esc_attr('calio-mc-pricing-mobile-chip' . ($is_active ? ' is-active' : '')) . '" type="button" data-plan-index="' . esc_attr((string) $plan_index) . '" aria-pressed="' . esc_attr($is_active ? 'true' : 'false') . '">';
                echo esc_html($plan['title']);
                if ($plan['featured']) {
                    echo '<span class="calio-mc-pricing-mobile-chip-badge">' . esc_html__('Featured', 'calio-addon') . '</span>';
                }
                echo '</button>';
            }
            echo '</div>';
        }

        echo '<div class="' . esc_attr('calio-mc-pricing-grid' . ($show_labels ? ' has-label-column' : '')) . '">';

        if ($show_labels) {
            echo '<div class="calio-mc-pricing-corner" aria-hidden="true"></div>';
        }

        foreach ($plans as $plan_index => $plan) {
            echo '<div class="' . esc_attr('calio-mc-pricing-plan' . ($plan['featured'] ? ' is-featured' : '')) . '" data-plan-index="' . esc_attr((string) $plan_index) . '">';
            echo '<div class="calio-mc-pricing-plan-card">';
            echo '<div class="calio-mc-pricing-plan-header">';
            if ($plan['featured'] && ($settings['featured_skin'] ?? 'spotlight') === 'ribbon') {
                echo '<div class="calio-mc-pricing-featured-ribbon">' . esc_html__('Featured', 'calio-addon') . '</div>';
            }
            $badge_text = trim((string) $plan['badge']);
            echo '<div class="' . esc_attr('calio-mc-pricing-badge' . ($badge_text === '' ? ' is-placeholder' : '')) . '"';
            if ($badge_text === '') {
                echo ' aria-hidden="' . esc_attr('true') . '"';
            }
            echo '>';
            if ($badge_text === '') {
                echo '&#160;';
            } else {
                echo esc_html($badge_text);
            }
            echo '</div>';
            echo '<h3 class="calio-mc-pricing-plan-title">' . esc_html($plan['title']) . '</h3>';
            if ($plan['subtitle'] !== '') {
                echo '<div class="calio-mc-pricing-plan-subtitle">' . esc_html($plan['subtitle']) . '</div>';
            }
            echo wp_kses_post($this->render_plan_price($plan, $has_switcher, $default_billing));
            echo '</div>';
            echo '<div class="calio-mc-pricing-plan-footer">';
            echo wp_kses_post($this->render_plan_button($plan['button_text'], $plan['button_url']));
            echo '</div>';
            echo '</div>';
            echo '</div>';
        }

        foreach ($rows as $index => $row) {
            $row_kind = (string) ($row['row_kind'] ?? 'feature');
            $is_highlight = (($row['highlight_row'] ?? '') === 'yes');
            $is_hidden = $should_collapse && $index >= $collapsed_rows;
            if ($row_kind === 'group') {
                $colspan = count($plans) + ($show_labels ? 1 : 0);
                echo '<div class="calio-mc-pricing-group-row"';
                if ($is_hidden) {
                    echo ' hidden data-collapsible="' . esc_attr('yes') . '"';
                }
                echo '>';
                echo '<div class="calio-mc-pricing-group-label" style="grid-column: 1 / span ' . esc_attr((string) $colspan) . ';">' . esc_html((string) ($row['label'] ?? '')) . '</div>';
                if (!empty($row['row_note'])) {
                    echo '<div class="calio-mc-pricing-group-note" style="grid-column: 1 / span ' . esc_attr((string) $colspan) . ';">' . esc_html((string) $row['row_note']) . '</div>';
                }
                echo '</div>';
                continue;
            }

            $row_emphasis = sanitize_html_class((string) ($row['row_emphasis'] ?? 'none'));
            echo '<div class="calio-mc-pricing-row' . ($is_highlight ? ' is-highlight' : '') . ' emphasis-' . esc_attr($row_emphasis) . '"' . ($is_hidden ? ' hidden data-collapsible="yes"' : '') . '>';
            if ($show_labels) {
                echo '<div class="calio-mc-pricing-row-label">';
                echo '<span class="calio-mc-pricing-row-label-text">' . esc_html((string) ($row['label'] ?? '')) . '</span>';
                if (!empty($row['feature_note'])) {
                    echo '<span class="calio-mc-pricing-row-note">';
                    echo '<span class="calio-mc-pricing-row-note-icon" tabindex="0" aria-label="' . esc_attr__('Feature details', 'calio-addon') . '">i</span>';
                    echo '<span class="calio-mc-pricing-row-note-tooltip">' . esc_html((string) $row['feature_note']) . '</span>';
                    echo '</span>';
                }
                echo '</div>';
            }
            foreach ($plans as $plan_index => $plan) {
                $value_key = 'value_' . ($plan_index + 1);
                $row_value_html = $this->render_row_value($row, $plan_index + 1);
                echo '<div class="calio-mc-pricing-row-value' . ($plan['featured'] ? ' is-featured' : '') . '" data-plan-index="' . esc_attr((string) $plan_index) . '" data-plan="' . esc_attr($plan['title']) . '" data-label="' . esc_attr((string) ($row['label'] ?? '')) . '">' . wp_kses($row_value_html, $this->allowed_pricing_markup()) . '</div>';
            }
            echo '</div>';
        }

        echo '</div>';

        if ($should_collapse) {
            $more_text = trim((string) ($settings['collapse_more_text'] ?? esc_html__('See More', 'calio-addon')));
            $less_text = trim((string) ($settings['collapse_less_text'] ?? esc_html__('See Less', 'calio-addon')));
            echo '<div class="calio-mc-pricing-footer">';
            echo '<button class="calio-mc-pricing-toggle" type="button" data-more-text="' . esc_attr($more_text !== '' ? $more_text : esc_html__('See More', 'calio-addon')) . '" data-less-text="' . esc_attr($less_text !== '' ? $less_text : esc_html__('See Less', 'calio-addon')) . '">' . esc_html($more_text !== '' ? $more_text : esc_html__('See More', 'calio-addon')) . '</button>';
            echo '</div>';
        }

        echo '</div>';
    }

    /**
     * Returns enabled plan data in render-friendly format.
     *
     * @param array<string, mixed> $settings Widget settings.
     * @return array<int, array{title: string, subtitle: string, price: string, old_price: string, period: string, yearly_price: string, yearly_old_price: string, yearly_period: string, badge: string, featured: bool, button_text: string, button_url: array<string, mixed>}>
     */
    private function get_plans(array $settings): array {
        $plans = [];
        $count = max(2, min(4, (int) ($settings['plan_count'] ?? 3)));

        for ($index = 1; $index <= $count; $index++) {
            $prefix = 'plan_' . $index . '_';
            $plans[] = [
                'title' => (string) ($settings[$prefix . 'title'] ?? ''),
                'subtitle' => (string) ($settings[$prefix . 'subtitle'] ?? ''),
                'price' => (string) ($settings[$prefix . 'price'] ?? ''),
                'old_price' => (string) ($settings[$prefix . 'old_price'] ?? ''),
                'period' => (string) ($settings[$prefix . 'period'] ?? ''),
                'yearly_price' => (string) ($settings[$prefix . 'yearly_price'] ?? ''),
                'yearly_old_price' => (string) ($settings[$prefix . 'yearly_old_price'] ?? ''),
                'yearly_period' => (string) ($settings[$prefix . 'yearly_period'] ?? ''),
                'badge' => (string) ($settings[$prefix . 'badge'] ?? ''),
                'featured' => (($settings[$prefix . 'featured'] ?? '') === 'yes'),
                'button_text' => (string) ($settings[$prefix . 'button_text'] ?? ''),
                'button_url' => is_array($settings[$prefix . 'button_url'] ?? null) ? $settings[$prefix . 'button_url'] : [],
            ];
        }

        return $plans;
    }

    /**
     * Builds safe button markup from Elementor URL controls.
     *
     * @param array<string, mixed> $url URL settings.
     */
    private function render_plan_button(string $label, array $url): string {
        if ($label === '') {
            return '';
        }

        $href = !empty($url['url']) ? (string) $url['url'] : '#';
        $rel = [];
        if (!empty($url['nofollow'])) {
            $rel[] = 'nofollow';
        }
        if (!empty($url['is_external'])) {
            $rel[] = 'noopener';
            $rel[] = 'noreferrer';
        }

        $output = '<a class="calio-mc-pricing-button" href="' . esc_url($href) . '"';
        if (!empty($url['is_external'])) {
            $output .= ' target="' . esc_attr('_blank') . '"';
        }
        if (!empty($rel)) {
            $output .= ' rel="' . esc_attr(implode(' ', array_unique($rel))) . '"';
        }

        return $output . '><span class="calio-mc-pricing-button-label">' . esc_html($label) . '</span></a>';
    }

    /**
     * Renders the monthly/yearly price stack.
     *
     * @param array<string, mixed> $plan Plan settings.
     */
    private function render_plan_price(array $plan, bool $has_switcher, string $default_billing): string {
        $monthly_hidden = $has_switcher && $default_billing === 'yearly';
        $yearly_hidden = !$has_switcher || $default_billing !== 'yearly';

        $output = '<div class="calio-mc-pricing-price-stack">';
        $output .= '<div class="' . esc_attr('calio-mc-pricing-price-wrap is-monthly' . (!$monthly_hidden ? ' is-active' : '')) . '"';
        if ($monthly_hidden) {
            $output .= ' hidden';
        }
        $output .= ' data-billing="' . esc_attr('monthly') . '" aria-hidden="' . esc_attr($monthly_hidden ? 'true' : 'false') . '">';
        if (!empty($plan['old_price'])) {
            $output .= '<span class="calio-mc-pricing-old-price">' . esc_html((string) $plan['old_price']) . '</span>';
        }
        $output .= '<span class="calio-mc-pricing-price">' . esc_html((string) $plan['price']) . '</span>';
        if (!empty($plan['period'])) {
            $output .= '<span class="calio-mc-pricing-period">' . esc_html((string) $plan['period']) . '</span>';
        }
        $output .= '</div>';

        $output .= '<div class="' . esc_attr('calio-mc-pricing-price-wrap is-yearly' . (!$yearly_hidden ? ' is-active' : '')) . '"';
        if ($yearly_hidden) {
            $output .= ' hidden';
        }
        $output .= ' data-billing="' . esc_attr('yearly') . '" aria-hidden="' . esc_attr($yearly_hidden ? 'true' : 'false') . '">';
        if (!empty($plan['yearly_old_price'])) {
            $output .= '<span class="calio-mc-pricing-old-price">' . esc_html((string) $plan['yearly_old_price']) . '</span>';
        }
        $output .= '<span class="calio-mc-pricing-price">' . esc_html((string) ($plan['yearly_price'] !== '' ? $plan['yearly_price'] : $plan['price'])) . '</span>';
        if (!empty($plan['yearly_period'])) {
            $output .= '<span class="calio-mc-pricing-period">' . esc_html((string) $plan['yearly_period']) . '</span>';
        }
        $output .= '</div>';
        $output .= '</div>';

        return $output;
    }

    /**
     * Finds the first featured plan index or falls back to the first plan.
     *
     * @param array<int, array<string, mixed>> $plans Plan list.
     */
    private function get_featured_plan_index(array $plans): int {
        foreach ($plans as $index => $plan) {
            if (!empty($plan['featured'])) {
                return $index;
            }
        }

        return 0;
    }

    /**
     * Outputs a single compare value with indicator support.
     *
     * @param array<string, mixed> $row Row settings.
     */
    private function render_row_value(array $row, int $plan_index): string {
        $type = (string) ($row['value_' . $plan_index . '_type'] ?? 'text');
        if ($type === 'check') {
            return '<span class="calio-mc-pricing-indicator is-check" aria-label="' . esc_attr__('Included', 'calio-addon') . '">&#10003;</span>';
        }

        if ($type === 'cross') {
            return '<span class="calio-mc-pricing-indicator is-cross" aria-label="' . esc_attr__('Not included', 'calio-addon') . '">&#10005;</span>';
        }

        if ($type === 'star') {
            return '<span class="calio-mc-pricing-indicator is-star" aria-label="' . esc_attr__('Premium feature', 'calio-addon') . '">&#9733;</span>';
        }

        if ($type === 'bolt') {
            return '<span class="calio-mc-pricing-indicator is-bolt" aria-label="' . esc_attr__('Advanced feature', 'calio-addon') . '">&#9889;</span>';
        }

        if ($type === 'shield') {
            return '<span class="calio-mc-pricing-indicator is-shield" aria-label="' . esc_attr__('Protected feature', 'calio-addon') . '">&#128737;</span>';
        }

        return wp_kses_post((string) ($row['value_' . $plan_index] ?? ''));
    }

    /**
     * @param array<string, mixed> $icon Icon control data.
     */
    private function render_switcher_icon(array $icon): string {
        return $this->get_icon_markup($icon);
    }

    /**
     * @return array<string, array<string, bool>>
     */
    private function allowed_pricing_markup(): array {
        return array_merge(
            wp_kses_allowed_html('post'),
            [
                'span' => [
                    'aria-hidden' => true,
                    'aria-label' => true,
                    'class' => true,
                ],
            ]
        );
    }
}
