<?php
if (!defined('ABSPATH')) die('Restricted Access');
if (!current_user_can('manage_options')) {
    wp_die(esc_html__('You do not have sufficient permissions to access this page.', 'ai-flash-tune'));
}
$aiflt_page_title = __('Dashboard Overview', 'ai-flash-tune');
$aiflt_is_wc_active = aiflt_is_woocommerce_active();

// Check if PRO addons are active
$aiflt_is_ai_analysis_addon_active = is_plugin_active('ai-flash-tune-ai-analysis/ai-flash-tune-ai-analysis.php');
$aiflt_is_smart_suggestions_addon_active = is_plugin_active('ai-flash-tune-smart-suggestions/ai-flash-tune-smart-suggestions.php');
// Add the check for the Checkout Optimizer addon
$aiflt_is_checkout_optimizer_addon_active = is_plugin_active('ai-flash-tune-checkout-optimizer/ai-flash-tune-checkout-optimizer.php');
$ai_provider = get_option('aiflt_ai_provider', 'local');
$aiflt_growth_modules = [
    'aiftac' => class_exists('AIFTAC_Logic'),
    'aiftpo' => class_exists('AIFTPO_Logic'),
    'aiftas' => class_exists('AIFTAS_Logic'),
    'aiftal' => class_exists('AIFTAL_Logic'),
    'aiftrp' => class_exists('AIFTRP_Logic'),
    'aiftcr' => class_exists('AIFTCR_Logic'),
    'aiftco' => class_exists('AIFTCO_Logic'),
];
$active_growth_count = count(array_filter($aiflt_growth_modules));
// Conditionally load data and set defaults if WooCommerce is inactive.
if ($aiflt_is_wc_active) {
    // Load all necessary data from logic classes
    $aiflt_dashboard_metrics = AIFLT_Funnel_Analyzer_Logic::get_dashboard_metrics();
    $aiflt_top_drop_off_metrics = AIFLT_Funnel_Analyzer_Logic::get_top_drop_off_metrics();
    
    // --- REVISED: Load the correct report based on addon status ---
    if ($aiflt_is_ai_analysis_addon_active) {
        $aiflt_ai_report = get_transient('aiftaa_latest_full_report');
    } else {
        $aiflt_ai_report = [
            'report' => __('Full AI analysis is available as an advanced feature. The free dashboard now provides local priority checks without requiring an API key.', 'ai-flash-tune'),
            'next_available' => 0,
        ];
    }

    $aiflt_low_performing_products = AIFLT_Products_Logic::get_low_performing_products_for_dashboard();
    $aiflt_has_products = AIFLT_Products_Logic::has_any_products(); // --->  Check if products exist.
    $aiflt_checkout_detector_data = AIFLT_Funnel_Analyzer_Logic::get_checkout_field_data();
    $aiflt_funnel_data = AIFLT_Funnel_Analyzer_Logic::get_funnel_data(); // Fetch initial funnel data
    $aiflt_page_speed_data = AIFLT_Funnel_Analyzer_Logic::get_page_speed_alerts();
} else {
    // If WooCommerce is inactive, provide safe, default values to prevent warnings.
    $aiflt_dashboard_metrics = [
        'total_sessions' => 'N/A',
        'conversion_rate' => 'N/A',
        'abandoned_carts' => 'N/A',
        'page_speed' => 'N/A',
        'last_updated' => __('WooCommerce is inactive', 'ai-flash-tune'),
    ];
    $aiflt_top_drop_off_metrics = [
        'page_name' => 'N/A',
        'drop_off_rate' => '0',
        'ai_insights' => __('Activate WooCommerce to get AI insights.', 'ai-flash-tune'),
        'rage_clicks' => '0',
        'scroll_stops' => '0',
        'quick_bounces' => '0',
        'basic_suggestion' => __('Suggestions require live data from WooCommerce. Please activate the plugin to get started.', 'ai-flash-tune'), // ---> This line was already added, ensure it's present.
    ];
    $aiflt_ai_report = [
        'report' => __('AI report requires live data from WooCommerce.', 'ai-flash-tune'),
        'next_available' => '0',
    ];
    $aiflt_low_performing_products = [];
    $aiflt_has_products = false; // ---> NEW: Set default for has_products.
    $aiflt_checkout_detector_data = [
        'css_class' => 'ai-flash-tune-text-gray-500',
        'icon_class' => 'fas fa-question-circle',
        'message' => __('Requires WooCommerce', 'ai-flash-tune'),
        'status' => 'N/A',
    ];
    $aiflt_funnel_data = ['is_empty' => true];
    $aiflt_page_speed_data = ['status' => 'no_data', 'alerts' => []]; // 'no_data' status prevents the warning box from showing.
}

// Free-version insight layer: local guidance without requiring any external AI service.
$aiflt_free_store_health = class_exists('AIFLT_Free_Insights_Logic')
    ? AIFLT_Free_Insights_Logic::get_store_health_score($aiflt_dashboard_metrics, $aiflt_funnel_data, $aiflt_page_speed_data, $aiflt_checkout_detector_data, $aiflt_low_performing_products)
    : ['score' => 0, 'label' => __('Unavailable', 'ai-flash-tune'), 'tone' => 'notice', 'summary' => '', 'factors' => []];

$aiflt_priority_checks = class_exists('AIFLT_Free_Insights_Logic')
    ? AIFLT_Free_Insights_Logic::get_priority_checks($aiflt_dashboard_metrics, $aiflt_funnel_data, $aiflt_page_speed_data, $aiflt_checkout_detector_data, $aiflt_low_performing_products)
    : [];

$aiflt_tracking_health = class_exists('AIFLT_Free_Insights_Logic')
    ? AIFLT_Free_Insights_Logic::get_tracking_health()
    : ['items' => [], 'ready_count' => 0, 'total_count' => 0];

$aiflt_top_abandoned_products = ($aiflt_is_wc_active && class_exists('AIFLT_Free_Insights_Logic'))
    ? AIFLT_Free_Insights_Logic::get_top_abandoned_products(5, 30)
    : [];

$aiflt_data_confidence = class_exists('AIFLT_Free_Insights_Logic')
    ? AIFLT_Free_Insights_Logic::get_data_confidence($aiflt_dashboard_metrics, $aiflt_funnel_data)
    : ['level' => __('Unavailable', 'ai-flash-tune'), 'tone' => 'notice', 'summary' => '', 'is_ready' => false, 'sessions' => 0, 'min_sessions' => 20, 'progress' => 0];

$aiflt_local_insight_snapshot = class_exists('AIFLT_Free_Insights_Logic')
    ? AIFLT_Free_Insights_Logic::get_local_insight_snapshot($aiflt_priority_checks, $aiflt_dashboard_metrics, $aiflt_funnel_data)
    : __('AI Flash Tune is collecting local conversion signals.', 'ai-flash-tune');

$aiflt_weekly_snapshot = class_exists('AIFLT_Free_Insights_Logic')
    ? AIFLT_Free_Insights_Logic::get_weekly_snapshot($aiflt_dashboard_metrics, $aiflt_funnel_data, $aiflt_page_speed_data)
    : ['title' => __('Weekly Store Snapshot', 'ai-flash-tune'), 'text' => '', 'tone' => 'notice'];

$aiflt_tracking_test_steps = class_exists('AIFLT_Free_Insights_Logic')
    ? AIFLT_Free_Insights_Logic::get_tracking_test_steps()
    : [];

$aiflt_top_priority = !empty($aiflt_priority_checks[0]) ? $aiflt_priority_checks[0] : [
    'title' => __('Run a clean funnel tracking test', 'ai-flash-tune'),
    'description' => __('Visit product, cart, and checkout once so AI Flash Tune can confirm tracking quality before suggesting changes.', 'ai-flash-tune'),
    'action_url' => admin_url('admin.php?page=funnelanalysis'),
    'action_label' => __('Open Funnel Analysis', 'ai-flash-tune'),
    'tone' => 'notice',
    'icon' => 'fas fa-route',
];

$aiflt_command_actions = [
    [
        'label' => __('Review funnel leaks', 'ai-flash-tune'),
        'url' => admin_url('admin.php?page=funnelanalysis'),
        'icon' => 'fas fa-route',
    ],
    [
        'label' => __('Optimize products', 'ai-flash-tune'),
        'url' => admin_url('admin.php?page=products'),
        'icon' => 'fas fa-box-open',
    ],
    [
        'label' => __('Recover carts', 'ai-flash-tune'),
        'url' => admin_url('admin.php?page=abandonedcarts'),
        'icon' => 'fas fa-shopping-cart',
    ],
];

?>
<div class="ai-flash-tune-admin-wrapper">
    <?php include_once('ai-flash-tune-admin-sidebar.php'); ?>
    <div class="ai-flash-tune-main-content">
        <?php if (!$aiflt_is_wc_active): ?>
            <?php
            $aiflt_install_url = admin_url('plugin-install.php?s=woocommerce&tab=search&type=term');
            ?>
            <div class="ai-flash-tune-notice-bar ai-flash-tune-mb-8">
                <div class="ai-flash-tune-notice-content">
                    <i class="fas fa-plug" aria-hidden="true"></i>
                    <span><strong><?php echo esc_html(__('Action Required:', 'ai-flash-tune')); ?></strong> <?php echo esc_html(__('AI Flash Tune requires WooCommerce. Please install or activate it to begin tracking live data.', 'ai-flash-tune')); ?></span>
                </div>
                <a href="<?php echo esc_url($aiflt_install_url); ?>" class="ai-flash-tune-notice-button">
                    <?php echo esc_html(__('Install WooCommerce', 'ai-flash-tune')); ?>
                </a>
            </div>
        <?php endif; ?>
        <!-- Add a class to the content wrapper when WooCommerce is inactive -->
        <div class="ai-flash-tune-page-content-wrapper <?php echo !$aiflt_is_wc_active ? 'ai-flash-tune-is-disabled' : ''; ?>">
            <div class="ai-flash-tune-page-header ai-flash-tune-dashboard-header">
                <div class="ai-flash-tune-dashboard-title-group">
                    
                    <div>
                        <h1 class="ai-flash-tune-main-title"><?php echo esc_html(__('Dashboard Overview', 'ai-flash-tune')); ?></h1>
                        <p><?php echo esc_html__('Store intelligence, tracking health, and prioritized actions in one place.', 'ai-flash-tune'); ?></p>
                    </div>
                </div>
                <div class="ai-flash-tune-header-actions">
                    <span id="ai-flash-tune-last-updated-text" class="ai-flash-tune-last-updated"><?php echo esc_html($aiflt_dashboard_metrics['last_updated']); ?></span>
                    <a href="<?php echo esc_url(admin_url('admin.php?page=ai_flash_tune')); ?>" class="ai-flash-tune-refresh-button">
                        <i class="fas fa-sync-alt" aria-hidden="true"></i> <?php echo esc_html(__('Refresh Data', 'ai-flash-tune')); ?>
                    </a>
                </div>
            </div>

            <section class="aiflt-command-hero ai-flash-tune-mb-8" aria-label="<?php echo esc_attr__('AI Flash Tune command center', 'ai-flash-tune'); ?>">
                <div class="aiflt-command-hero-bg" aria-hidden="true"></div>
                <div class="aiflt-command-hero-main">
                    <span class="aiflt-command-kicker"><i class="fas fa-bolt" aria-hidden="true"></i><?php echo esc_html__('Store Intelligence Command Center', 'ai-flash-tune'); ?></span>
                    <h1><?php echo esc_html__('Know what to fix next', 'ai-flash-tune'); ?></h1>
                    <p><?php echo esc_html($aiflt_local_insight_snapshot); ?></p>
                    <div class="aiflt-command-chips" aria-label="<?php echo esc_attr__('Store status summary', 'ai-flash-tune'); ?>">
                        <span><i class="fas fa-signal" aria-hidden="true"></i><?php echo esc_html__('Data confidence:', 'ai-flash-tune'); ?> <strong><?php echo esc_html($aiflt_data_confidence['level']); ?></strong></span>
                        <span><i class="fas fa-robot" aria-hidden="true"></i><?php echo esc_html__('AI mode:', 'ai-flash-tune'); ?> <strong><?php echo esc_html($ai_provider === 'local' ? __('Local insights', 'ai-flash-tune') : ucfirst($ai_provider)); ?></strong></span>
                        <span><i class="fas fa-puzzle-piece" aria-hidden="true"></i><strong><?php echo esc_html((string) $active_growth_count); ?></strong> <?php echo esc_html__('growth modules active', 'ai-flash-tune'); ?></span>
                    </div>
                    <div class="aiflt-command-action-row">
                        <?php foreach ($aiflt_command_actions as $command_action) : ?>
                            <a href="<?php echo esc_url($command_action['url']); ?>" class="aiflt-command-action-pill">
                                <i class="<?php echo esc_attr($command_action['icon']); ?>" aria-hidden="true"></i>
                                <span><?php echo esc_html($command_action['label']); ?></span>
                            </a>
                        <?php endforeach; ?>
                    </div>
                </div>
                <aside class="aiflt-command-next-card aiflt-command-next-<?php echo esc_attr($aiflt_top_priority['tone']); ?>">
                    <span class="aiflt-command-next-label"><?php echo esc_html__('Next best move', 'ai-flash-tune'); ?></span>
                    <div class="aiflt-command-next-icon"><i class="<?php echo esc_attr($aiflt_top_priority['icon']); ?>" aria-hidden="true"></i></div>
                    <strong><?php echo esc_html($aiflt_top_priority['title']); ?></strong>
                    <p><?php echo esc_html($aiflt_top_priority['description']); ?></p>
                    <a href="<?php echo esc_url($aiflt_top_priority['action_url']); ?>">
                        <?php echo esc_html($aiflt_top_priority['action_label']); ?>
                        <i class="fas fa-arrow-right" aria-hidden="true"></i>
                    </a>
                </aside>
            </section>

            <?php
            // --- NEW: Data Collection Notice ---
            // Check if the plugin has been active for less than, say, 3 days
            // Or check if there's very little data (e.g., less than 100 sessions)
            // For simplicity, let's use a time-based check for now.
            $aiflt_install_time = get_option('aiflt_activation_time', time()); // You'd need to set this option on activation
            $aiflt_days_active = (time() - $aiflt_install_time) / DAY_IN_SECONDS;

            // Show the notice for the first 3 days
            if ($aiflt_days_active < 3) :
            ?>
                <div class="ai-flash-tune-notice-bar ai-flash-tune-notice-info ai-flash-tune-mb-8">
                    <div class="ai-flash-tune-notice-content">
                        <i class="fas fa-info-circle" aria-hidden="true"></i>
                        <span><strong><?php echo esc_html(__('Getting Started:', 'ai-flash-tune')); ?></strong> <?php echo esc_html(__('AI Flash Tune is now collecting data about your visitors. Initial insights might be limited. Suggestions will become more accurate as more data is gathered over the next few days.', 'ai-flash-tune')); ?></span>
                    </div>
                </div>
            <?php
            endif;
            // --- END: Data Collection Notice ---
            ?><div id="ai-flash-tune-dashboard-insights-container" class="ai-flash-tune-grid ai-flash-tune-gap-6 ai-flash-tune-grid-cols-1 ai-flash-tune-grid-cols-md-2 ai-flash-tune-grid-cols-lg-4 ai-flash-tune-mb-8 ai-flash-tune-key-insights">
                <div class="ai-flash-tune-card ai-flash-tune-flex ai-flash-tune-flex-center ai-flash-tune-justify-between ai-flash-tune-insight-card">
                    <div>
                        <span class="ai-flash-tune-text-sm ai-flash-tune-text-gray-500 ai-flash-tune-insight-label"><?php echo esc_html(__('Total Sessions (Last 7 Days)', 'ai-flash-tune')); ?></span>
                        <span class="ai-flash-tune-text-3xl ai-flash-tune-font-bold ai-flash-tune-text-gray-900 ai-flash-tune-data-value"><?php echo esc_html($aiflt_dashboard_metrics['total_sessions']); ?></span>
                    </div>
                    <i class="fas fa-users ai-flash-tune-text-green-500 ai-flash-tune-text-4xl ai-flash-tune-insight-icon" aria-hidden="true"></i>
                </div>
                <div class="ai-flash-tune-card ai-flash-tune-flex ai-flash-tune-flex-center ai-flash-tune-justify-between ai-flash-tune-insight-card">
                    <div>
                        <span class="ai-flash-tune-text-sm ai-flash-tune-text-gray-500 ai-flash-tune-insight-label"><?php echo esc_html(__('Conversion Rate', 'ai-flash-tune')); ?></span>
                        <span class="ai-flash-tune-text-3xl ai-flash-tune-font-bold ai-flash-tune-text-gray-900 ai-flash-tune-data-value"><?php echo esc_html($aiflt_dashboard_metrics['conversion_rate']); ?>%</span>
                    </div>
                    <i class="fas fa-percent ai-flash-tune-text-blue-500 ai-flash-tune-text-4xl ai-flash-tune-insight-icon" aria-hidden="true"></i>
                </div>
                <div class="ai-flash-tune-card ai-flash-tune-flex ai-flash-tune-flex-center ai-flash-tune-justify-between ai-flash-tune-insight-card">
                    <div>
                        <span class="ai-flash-tune-text-sm ai-flash-tune-text-gray-500 ai-flash-tune-insight-label"><?php echo esc_html(__('Abandoned Carts', 'ai-flash-tune')); ?></span>
                        <span class="ai-flash-tune-text-3xl ai-flash-tune-font-bold ai-flash-tune-text-gray-900 ai-flash-tune-data-value"><?php echo esc_html($aiflt_dashboard_metrics['abandoned_carts']); ?></span>
                    </div>
                    <i class="fas fa-shopping-basket ai-flash-tune-text-red-500 ai-flash-tune-text-4xl ai-flash-tune-insight-icon" aria-hidden="true"></i>
                </div>
                <div class="ai-flash-tune-card ai-flash-tune-flex ai-flash-tune-flex-center ai-flash-tune-justify-between ai-flash-tune-insight-card">
                    <div>
                        <span class="ai-flash-tune-text-sm ai-flash-tune-text-gray-500 ai-flash-tune-insight-label"><?php echo esc_html(__('Avg. Page Speed (LCP)', 'ai-flash-tune')); ?></span>
                        <span class="ai-flash-tune-text-3xl ai-flash-tune-font-bold ai-flash-tune-text-gray-900 ai-flash-tune-data-value"><?php echo esc_html($aiflt_dashboard_metrics['page_speed']); ?>s</span>
                    </div>
                    <i class="fas fa-tachometer-alt ai-flash-tune-text-yellow-500 ai-flash-tune-text-4xl ai-flash-tune-insight-icon" aria-hidden="true"></i>
                </div>
            </div>
            <div class="ai-flash-tune-grid ai-flash-tune-gap-6 ai-flash-tune-grid-cols-1 ai-flash-tune-grid-cols-lg-3 ai-flash-tune-mb-8 ai-flash-tune-free-insights-row">
                <div class="ai-flash-tune-card ai-flash-tune-health-score-card ai-flash-tune-health-<?php echo esc_attr($aiflt_free_store_health['tone']); ?>">
                    <div class="ai-flash-tune-free-card-header">
                        <span class="ai-flash-tune-text-xl ai-flash-tune-font-semibold ai-flash-tune-text-gray-800 ai-flash-tune-card-title">
                            <i class="fas fa-heartbeat ai-flash-tune-text-indigo-600 ai-flash-tune-mr-2"></i><?php echo esc_html__('Store Health Score', 'ai-flash-tune'); ?>
                        </span>
                        <span class="ai-flash-tune-free-badge ai-flash-tune-free-badge-<?php echo esc_attr($aiflt_free_store_health['tone']); ?>"><?php echo esc_html($aiflt_free_store_health['label']); ?></span>
                    </div>
                    <div class="ai-flash-tune-health-score-wrap">
                        <?php if (!empty($aiflt_free_store_health['is_ready'])) : ?>
                            <div class="ai-flash-tune-health-score-number"><?php echo esc_html((int) $aiflt_free_store_health['score']); ?><span>/100</span></div>
                            <div class="ai-flash-tune-health-score-bar" aria-hidden="true">
                                <span style="width: <?php echo esc_attr((int) $aiflt_free_store_health['score']); ?>%;"></span>
                            </div>
                        <?php else : ?>
                            <div class="ai-flash-tune-health-waiting-number"><?php echo esc_html__('Waiting for data', 'ai-flash-tune'); ?></div>
                            <div class="ai-flash-tune-data-confidence-bar" aria-hidden="true">
                                <span style="width: <?php echo esc_attr((int) $aiflt_data_confidence['progress']); ?>%;"></span>
                            </div>
                        <?php endif; ?>
                    </div>
                    <div class="ai-flash-tune-data-confidence-note ai-flash-tune-confidence-<?php echo esc_attr($aiflt_data_confidence['tone']); ?>">
                        <strong><?php echo esc_html__('Data Confidence:', 'ai-flash-tune'); ?> <?php echo esc_html($aiflt_data_confidence['level']); ?></strong>
                        <span><?php echo esc_html($aiflt_data_confidence['summary']); ?></span>
                    </div>
                    <p class="ai-flash-tune-text-gray-700 ai-flash-tune-mt-4 ai-flash-tune-section-description"><?php echo esc_html($aiflt_free_store_health['summary']); ?></p>
                    <div class="ai-flash-tune-health-factor-list">
                        <?php foreach (array_slice($aiflt_free_store_health['factors'], 0, 4) as $factor) : ?>
                            <div class="ai-flash-tune-health-factor ai-flash-tune-factor-<?php echo esc_attr($factor['tone']); ?>">
                                <span><?php echo esc_html($factor['label']); ?></span>
                                <strong><?php echo esc_html($factor['impact']); ?></strong>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>

                <div class="ai-flash-tune-card ai-flash-tune-lg-col-span-2 ai-flash-tune-priority-card">
                    <div class="ai-flash-tune-free-card-header">
                        <span class="ai-flash-tune-text-xl ai-flash-tune-font-semibold ai-flash-tune-text-gray-800 ai-flash-tune-card-title">
                            <i class="fas fa-clipboard-check ai-flash-tune-text-indigo-600 ai-flash-tune-mr-2"></i><?php echo esc_html__('What to Check First', 'ai-flash-tune'); ?>
                        </span>
                        <span class="ai-flash-tune-free-subtle-label"><?php echo esc_html__('Free local insight', 'ai-flash-tune'); ?></span>
                    </div>
                    <p class="ai-flash-tune-text-gray-700 ai-flash-tune-section-description ai-flash-tune-mb-4"><?php echo esc_html__('AI Flash Tune turns your free tracking data into a short action list, so store owners know where to look first.', 'ai-flash-tune'); ?></p>
                    <div class="ai-flash-tune-priority-list">
                        <?php foreach ($aiflt_priority_checks as $index => $check) : ?>
                            <div class="ai-flash-tune-priority-item ai-flash-tune-priority-<?php echo esc_attr($check['tone']); ?>">
                                <div class="ai-flash-tune-priority-rank"><?php echo esc_html($index + 1); ?></div>
                                <div class="ai-flash-tune-priority-icon"><i class="<?php echo esc_attr($check['icon']); ?>"></i></div>
                                <div class="ai-flash-tune-priority-body">
                                    <strong><?php echo esc_html($check['title']); ?></strong>
                                    <p><?php echo esc_html($check['description']); ?></p>
                                </div>
                                <a class="ai-flash-tune-priority-action" href="<?php echo esc_url($check['action_url']); ?>"><?php echo esc_html($check['action_label']); ?></a>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>

            <div class="ai-flash-tune-grid ai-flash-tune-gap-6 ai-flash-tune-grid-cols-1 ai-flash-tune-grid-cols-lg-2 ai-flash-tune-mb-8 ai-flash-tune-free-ops-row">
                <div class="ai-flash-tune-card ai-flash-tune-tracking-health-card">
                    <div class="ai-flash-tune-free-card-header">
                        <span class="ai-flash-tune-text-xl ai-flash-tune-font-semibold ai-flash-tune-text-gray-800 ai-flash-tune-card-title">
                            <i class="fas fa-signal ai-flash-tune-text-indigo-600 ai-flash-tune-mr-2"></i><?php echo esc_html__('Tracking Health', 'ai-flash-tune'); ?>
                        </span>
                        <span class="ai-flash-tune-free-subtle-label"><?php echo esc_html($aiflt_tracking_health['ready_count']); ?>/<?php echo esc_html($aiflt_tracking_health['total_count']); ?> <?php echo esc_html__('ready', 'ai-flash-tune'); ?></span>
                    </div>
                    <div class="ai-flash-tune-tracking-health-grid">
                        <?php foreach ($aiflt_tracking_health['items'] as $item) : ?>
                            <div class="ai-flash-tune-tracking-health-item ai-flash-tune-tracking-<?php echo esc_attr($item['tone']); ?>">
                                <i class="fas <?php echo $item['ok'] ? 'fa-check-circle' : 'fa-exclamation-circle'; ?>"></i>
                                <div>
                                    <strong><?php echo esc_html($item['label']); ?></strong>
                                    <span><?php echo esc_html($item['message']); ?></span>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                </div>

                <div class="ai-flash-tune-card ai-flash-tune-local-insight-card">
                    <div class="ai-flash-tune-free-card-header">
                        <span class="ai-flash-tune-text-xl ai-flash-tune-font-semibold ai-flash-tune-text-gray-800 ai-flash-tune-card-title">
                            <i class="fas fa-lightbulb ai-flash-tune-text-indigo-600 ai-flash-tune-mr-2"></i><?php echo esc_html__('Free Insight Snapshot', 'ai-flash-tune'); ?>
                        </span>
                        <span class="ai-flash-tune-free-subtle-label"><?php echo esc_html__('No external AI used', 'ai-flash-tune'); ?></span>
                    </div>
                    <div class="ai-flash-tune-local-insight-box">
                        <?php echo esc_html($aiflt_local_insight_snapshot); ?>
                    </div>
                    <div class="ai-flash-tune-weekly-snapshot-box ai-flash-tune-snapshot-<?php echo esc_attr($aiflt_weekly_snapshot['tone']); ?>">
                        <strong><?php echo esc_html($aiflt_weekly_snapshot['title']); ?></strong>
                        <span><?php echo esc_html($aiflt_weekly_snapshot['text']); ?></span>
                    </div>
                    <?php if (!empty($aiflt_tracking_test_steps)) : ?>
                        <details class="ai-flash-tune-tracking-test-box">
                            <summary><?php echo esc_html__('Run a quick tracking test', 'ai-flash-tune'); ?></summary>
                            <ol>
                                <?php foreach ($aiflt_tracking_test_steps as $step) : ?>
                                    <li><?php echo esc_html($step); ?></li>
                                <?php endforeach; ?>
                            </ol>
                        </details>
                    <?php endif; ?>
                </div>
            </div>
            <div class="ai-flash-tune-card ai-flash-tune-mb-8">

                <div class="aift-performance-alerts-header">
                    <div>
                        <span class="ai-flash-tune-text-xl ai-flash-tune-font-semibold ai-flash-tune-text-gray-800 ai-flash-tune-card-title"><?php echo esc_html(__('Performance Alerts', 'ai-flash-tune')); ?></span>
                        <p><?php echo esc_html__('Local checks for speed-related conversion risk. Checkout and cart behavior are reviewed in the priority sections below.', 'ai-flash-tune'); ?></p>
                    </div>
                    <span class="ai-flash-tune-free-subtle-label"><?php echo esc_html__('Local check', 'ai-flash-tune'); ?></span>
                </div>
                <?php switch ($aiflt_page_speed_data['status']) :
                    case 'warning': ?>
                        <div class="ai-flash-tune-alert-message ai-flash-tune-alert-warning">
                            <i class="fas fa-exclamation-triangle ai-flash-tune-mr-2" aria-hidden="true"></i>
                            <span><?php echo esc_html(__('The following pages are slower than the recommended 2.5s threshold, which may be impacting your conversion rates:', 'ai-flash-tune')); ?></span>
                        </div>
                        <ul class="ai-flash-tune-list-disc ai-flash-tune-list-inside ai-flash-tune-mt-4">
                            <?php foreach ($aiflt_page_speed_data['alerts'] as $aiflt_alert): ?>
                                <li>
                                    <strong><?php echo esc_html($aiflt_alert['name']); ?>:</strong>
                                    <?php 
                                    printf(
                                        /* translators: %s: The average page speed in seconds. */
                                        esc_html__('Average speed is %s seconds.', 'ai-flash-tune'),
                                        '<strong>' . esc_html($aiflt_alert['speed']) . 's</strong>'
                                    );
                                    ?>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                        <?php break; ?>

                    <?php case 'good': ?>
                        <div class="ai-flash-tune-alert-message ai-flash-tune-alert-success">
                            <i class="fas fa-check-circle ai-flash-tune-mr-2" aria-hidden="true"></i>
                            <span><?php echo esc_html(__('Page speed looks healthy. No speed-related conversion risk is visible right now. Continue reviewing checkout and cart behavior below.', 'ai-flash-tune')); ?></span>
                        </div>
                        <?php break; ?>

                    <?php case 'no_data': ?>
                    <?php default: ?>
                        <div class="ai-flash-tune-empty-state-message" style="text-align: left; padding: 1.5rem; margin-top: 1rem; border-style: solid;">
                            <div class="ai-flash-tune-flex" style="align-items: flex-start; gap: 1rem;">
                                <i class="fas fa-tachometer-alt ai-flash-tune-text-indigo-500" style="font-size: 1.75rem; margin-top: 5px;"></i>
                                <div>
                                    <p class="ai-flash-tune-empty-state-title" style="margin-bottom: 0.25rem;"><?php echo esc_html(__('Collecting Page Speed Data', 'ai-flash-tune')); ?></p>
                                    <p class="ai-flash-tune-empty-state-text"><?php echo esc_html(__('Once your store has visitor traffic, this report will alert you to any key pages that are loading slower than the recommended threshold.', 'ai-flash-tune')); ?></p>
                                </div>
                            </div>
                        </div>
                        <?php break; ?>
                <?php endswitch; ?>
            </div>
            <div class="ai-flash-tune-grid ai-flash-tune-gap-6 ai-flash-tune-grid-cols-1 ai-flash-tune-grid-cols-lg-3 ai-flash-tune-mb-8">
            <div class="ai-flash-tune-card ai-flash-tune-mb-8 ai-flash-tune-top-abandoned-products-card">
                <div class="ai-flash-tune-free-card-header">
                    <span class="ai-flash-tune-text-xl ai-flash-tune-font-semibold ai-flash-tune-text-gray-800 ai-flash-tune-card-title">
                        <i class="fas fa-cart-arrow-down ai-flash-tune-text-indigo-600 ai-flash-tune-mr-2"></i><?php echo esc_html__('Top Abandoned Products', 'ai-flash-tune'); ?>
                    </span>
                    <a class="ai-flash-tune-free-subtle-link" href="<?php echo esc_url(admin_url('admin.php?page=abandonedcarts')); ?>"><?php echo esc_html__('View carts', 'ai-flash-tune'); ?></a>
                </div>
                <?php if (!empty($aiflt_top_abandoned_products)) : ?>
                    <p class="ai-flash-tune-text-gray-700 ai-flash-tune-section-description ai-flash-tune-mb-4"><?php echo esc_html__('These products appear most often in abandoned carts. Start here when reviewing pricing, offer clarity, trust, or checkout friction.', 'ai-flash-tune'); ?></p>
                    <div class="ai-flash-tune-top-abandoned-list">
                        <?php foreach ($aiflt_top_abandoned_products as $product) : ?>
                            <div class="ai-flash-tune-top-abandoned-item">
                                <div>
                                    <strong><?php echo esc_html($product['name']); ?></strong>
                                    <span><?php echo esc_html(sprintf(
                                        /* translators: 1: cart count, 2: quantity */
                                        _n('%1$d abandoned cart · %2$d item', '%1$d abandoned carts · %2$d items', (int) $product['cart_count'], 'ai-flash-tune'),
                                        (int) $product['cart_count'],
                                        (int) $product['quantity']
                                    )); ?></span>
                                </div>
                                <div class="ai-flash-tune-top-abandoned-value">
                                    <?php echo function_exists('wc_price') ? wp_kses_post(wc_price($product['estimated_value'])) : esc_html(number_format_i18n($product['estimated_value'], 2)); ?>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php else : ?>
                    <div class="ai-flash-tune-empty-state-message" style="text-align:left; padding:1rem;">
                        <div class="ai-flash-tune-flex" style="align-items:flex-start; gap:1rem;">
                            <i class="fas fa-check-circle ai-flash-tune-text-green-500" style="font-size:1.5rem; margin-top:3px;"></i>
                            <div>
                                <p class="ai-flash-tune-empty-state-title" style="margin-bottom:0.25rem;"><?php echo esc_html__('No abandoned-product pattern yet', 'ai-flash-tune'); ?></p>
                                <p class="ai-flash-tune-empty-state-text"><?php echo esc_html__('Once shoppers add products to cart and leave without purchasing, the most repeated abandoned products will appear here.', 'ai-flash-tune'); ?></p>
                            </div>
                        </div>
                    </div>
                <?php endif; ?>
            </div>

                <div class="ai-flash-tune-lg-col-span-2 ai-flash-tune-card ai-flash-tune-drop-off-analysis">
                    <?php
                    // Check if the drop-off rate is greater than 0
                    if ( isset($aiflt_top_drop_off_metrics['drop_off_rate']) && (float) $aiflt_top_drop_off_metrics['drop_off_rate'] > 0 ) :
                    ?>

                        <span class="ai-flash-tune-flex ai-flash-tune-text-xl ai-flash-tune-font-semibold ai-flash-tune-text-gray-800 ai-flash-tune-mb-4 ai-flash-tune-card-title">
                            <i class="fas fa-exclamation-triangle ai-flash-tune-text-red-500 ai-flash-tune-mr-2"></i><?php echo esc_html(__('Priority Fix: Top Drop-Off Point', 'ai-flash-tune')); ?>
                        </span>
                        <div class="ai-flash-tune-flex ai-flash-tune-space-x-4 ai-flash-tune-mb-4">
                            <span class="ai-flash-tune-text-red-600 ai-flash-tune-text-2xl ai-flash-tune-font-bold ai-flash-tune-drop-off-page">
                                <?php echo esc_html($aiflt_top_drop_off_metrics['page_name']); ?>
                            </span>
                            <span class="ai-flash-tune-text-gray-600 ai-flash-tune-text-lg ai-flash-tune-drop-off-rate">
                                (<?php echo esc_html($aiflt_top_drop_off_metrics['drop_off_rate']); ?>% <?php echo esc_html(__('Drop-Off Rate', 'ai-flash-tune')); ?>)
                            </span>
                        </div>
                        <p class="ai-flash-tune-text-gray-700 ai-flash-tune-mb-4 ai-flash-tune-drop-off-description"><?php echo wp_kses_post($aiflt_top_drop_off_metrics['ai_insights']); ?></p>
                        <div class="ai-flash-tune-grid ai-flash-tune-grid-cols-1 ai-flash-tune-grid-cols-sm-3 ai-flash-tune-gap-4 ai-flash-tune-text-center ai-flash-tune-mb-6">
                            <div class="ai-flash-tune-p-3 ai-flash-tune-bg-red-100 ai-flash-tune-rounded-lg ai-flash-tune-behavior-item">
                                <i class="fas fa-mouse-pointer ai-flash-tune-text-red-500 ai-flash-tune-text-2xl ai-flash-tune-mb-2"></i>
                                <span class="ai-flash-tune-text-lg ai-flash-tune-font-semibold">
                                    <?php echo esc_html(__('Rage Clicks:', 'ai-flash-tune')); ?>
                                    <i class="fas fa-question-circle ai-flash-tune-tooltip-icon" data-tooltip="<?php echo esc_attr(__('Users repeatedly clicking the same area in a short time, indicating frustration.', 'ai-flash-tune')); ?>"></i>
                                    <?php echo esc_html($aiflt_top_drop_off_metrics['rage_clicks']); ?>
                                </span>
                                <p class="ai-flash-tune-text-sm ai-flash-tune-text-gray-600"><?php echo esc_html(__('Users repeatedly clicking', 'ai-flash-tune')); ?></p>
                            </div>
                            <div class="ai-flash-tune-p-3 ai-flash-tune-bg-yellow-100 ai-flash-tune-rounded-lg ai-flash-tune-behavior-item">
                                <i class="fas fa-stopwatch ai-flash-tune-text-yellow-500 ai-flash-tune-text-2xl ai-flash-tune-mb-2"></i>
                                <span class="ai-flash-tune-text-lg ai-flash-tune-font-semibold">
                                    <?php echo esc_html(__('Scroll Stop Events:', 'ai-flash-tune')); ?>
                                    <i class="fas fa-question-circle ai-flash-tune-tooltip-icon" data-tooltip="<?php echo esc_attr(__('Users scrolling to a specific point and stopping for several seconds, indicating hesitation or confusion.', 'ai-flash-tune')); ?>"></i>
                                    <?php echo esc_html($aiflt_top_drop_off_metrics['scroll_stops']); ?>
                                </span>
                                <p class="ai-flash-tune-text-sm ai-flash-tune-text-gray-600"><?php echo esc_html(__('Users pausing on form fields', 'ai-flash-tune')); ?></p>
                            </div>
                            <div class="ai-flash-tune-p-3 ai-flash-tune-bg-purple-100 ai-flash-tune-rounded-lg ai-flash-tune-behavior-item">
                                <i class="fas fa-undo ai-flash-tune-text-purple-500 ai-flash-tune-text-2xl ai-flash-tune-mb-2"></i>
                                <span class="ai-flash-tune-text-lg ai-flash-tune-font-semibold">
                                    <?php echo esc_html(__('Quick Bounce Events:', 'ai-flash-tune')); ?>
                                    <i class="fas fa-question-circle ai-flash-tune-tooltip-icon" data-tooltip="<?php echo esc_attr(__('Users leaving the page very quickly after arriving, indicating the content was not what they expected.', 'ai-flash-tune')); ?>"></i>
                                    <?php echo esc_html($aiflt_top_drop_off_metrics['quick_bounces']); ?>
                                </span>
                                <p class="ai-flash-tune-text-sm ai-flash-tune-text-gray-600"><?php echo esc_html(__('Users leaving immediately', 'ai-flash-tune')); ?></p>
                            </div>
                        </div>
                        <a id="aift-view-suggestion-btn" class="ai-flash-tune-bg-red-600 hover:ai-flash-tune-bg-red-700 ai-flash-tune-text-white ai-flash-tune-font-semibold ai-flash-tune-py-2 ai-flash-tune-px-4 ai-flash-tune-rounded-lg ai-flash-tune-shadow-md ai-flash-tune-cta-button" href="#">
                            <i class="fas fa-lightbulb ai-flash-tune-mr-2"></i> <?php echo esc_html(__('View Basic Suggestion', 'ai-flash-tune')); ?>
                        </a>
                    <?php
                    // Else: If the drop-off rate is 0 or less
                    else :
                    ?>
                        <span class="ai-flash-tune-flex ai-flash-tune-text-xl ai-flash-tune-font-semibold ai-flash-tune-text-gray-800 ai-flash-tune-mb-4 ai-flash-tune-card-title">
                            <i class="fas fa-check-circle ai-flash-tune-text-green-500 ai-flash-tune-mr-2"></i><?php echo esc_html(__('Funnel Readiness', 'ai-flash-tune')); ?>
                        </span>
                        <div class="ai-flash-tune-empty-state-message" style="text-align: left; padding: 1.5rem; margin-top: 1rem; border-style: solid; border-color: #d1fae5; background-color: #f0fdf4;">
                             <div class="ai-flash-tune-flex" style="align-items: flex-start; gap: 1rem;">
                                <i class="fas fa-thumbs-up ai-flash-tune-text-green-500" style="font-size: 1.75rem; margin-top: 5px;"></i>
                                <div>
                                    <p class="ai-flash-tune-empty-state-title" style="margin-bottom: 0.25rem;"><?php echo esc_html__('Waiting for stronger funnel signals', 'ai-flash-tune'); ?></p>
                                    <p class="ai-flash-tune-empty-state-text"><?php echo esc_html__('No critical funnel leak is visible yet. Keep collecting product, cart, checkout, and purchase activity before making final conversion decisions.', 'ai-flash-tune'); ?></p>
                                </div>
                            </div>
                        </div>

                    <?php
                    endif;
                    ?>
                </div>
            </div><!-- /.ai-flash-tune-grid: abandoned products + funnel readiness -->
            <div id="aift-suggestion-modal-overlay" class="ai-flash-tune-modal-overlay"></div>
            <div id="aift-suggestion-modal" class="ai-flash-tune-modal aift-basic-suggestion-modal">
                <div class="ai-flash-tune-modal-header">
                    <div>
                        <span class="aift-modal-kicker"><?php echo esc_html__('Local recommendation', 'ai-flash-tune'); ?></span>
                        <h3 class="ai-flash-tune-modal-title"><?php echo esc_html__('Suggested next check', 'ai-flash-tune'); ?></h3>
                    </div>
                    <button id="aift-suggestion-modal-close" class="ai-flash-tune-modal-close" aria-label="<?php echo esc_attr__('Close suggestion', 'ai-flash-tune'); ?>">&times;</button>
                </div>
                <div class="ai-flash-tune-modal-content aift-basic-suggestion-content">
                    <div class="aift-suggestion-summary">
                        <i class="fas fa-lightbulb" aria-hidden="true"></i>
                        <p><?php echo esc_html($aiflt_top_drop_off_metrics['basic_suggestion']); ?></p>
                    </div>
                    <div class="aift-suggestion-actions">
                        <strong><?php echo esc_html__('Good first checks', 'ai-flash-tune'); ?></strong>
                        <ul>
                            <li><?php echo esc_html__('Make one or two safe changes first, not many changes at once.', 'ai-flash-tune'); ?></li>
                            <li><?php echo esc_html__('Review the affected page as a guest shopper on desktop and mobile.', 'ai-flash-tune'); ?></li>
                            <li><?php echo esc_html__('Collect fresh data for several days before judging the result.', 'ai-flash-tune'); ?></li>
                        </ul>
                    </div>
                </div>
            </div>
            <div id="ai-flash-tune-dashboard-funnel" class="ai-flash-tune-card ai-flash-tune-mb-8 ai-flash-tune-funnel-section">
                <div class="ai-flash-tune-flex ai-flash-tune-justify-between ai-flash-tune-flex-center ai-flash-tune-mb-4">
                    <span class="ai-flash-tune-text-xl ai-flash-tune-font-semibold ai-flash-tune-text-gray-800 ai-flash-tune-mb-4 ai-flash-tune-card-title"><?php echo esc_html(__('Funnel Flow Overview', 'ai-flash-tune')); ?></span>
                    <?php if ($aiflt_is_wc_active && !$aiflt_funnel_data['is_empty']) : ?>
                        <div class="ai-flash-tune-flex ai-flash-tune-justify-end ai-flash-tune-mb-4 ai-flash-tune-funnel-toggles">
                            <div class="ai-flash-tune-inline-flex ai-flash-tune-rounded-md ai-flash-tune-shadow-sm" role="group">
                                <a href="#" data-device="desktop" class="ai-flash-tune-px-4 ai-flash-tune-py-2 ai-flash-tune-text-sm ai-flash-tune-font-medium ai-flash-tune-text-blue-700 ai-flash-tune-bg-white ai-flash-tune-border ai-flash-tune-border-gray-200 ai-flash-tune-rounded-l-lg ai-flash-tune-funnel-toggle-button active">
                                    <?php echo esc_html(__('Desktop', 'ai-flash-tune')); ?>
                                </a>
                                <a href="#" data-device="mobile" class="ai-flash-tune-px-4 ai-flash-tune-py-2 ai-flash-tune-text-sm ai-flash-tune-font-medium ai-flash-tune-text-gray-900 ai-flash-tune-bg-white ai-flash-tune-border ai-flash-tune-rounded-r-lg ai-flash-tune-border-gray-200 ai-flash-tune-funnel-toggle-button">
                                    <?php echo esc_html(__('Mobile', 'ai-flash-tune')); ?>
                                </a>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
                <?php if (!$aiflt_is_wc_active || $aiflt_funnel_data['is_empty']) : ?>
                    <?php // If WC is inactive OR there's no data, show the empty message and NO TABS. ?>
                    <div class="ai-flash-tune-empty-state-message" style="width: 100%;">
                        <i class="fas fa-chart-line ai-flash-tune-empty-state-icon"></i>
                        <p class="ai-flash-tune-empty-state-title"><?php echo esc_html(__('Collecting Funnel Data', 'ai-flash-tune')); ?></p>
                        <p class="ai-flash-tune-empty-state-text"><?php echo esc_html(__('Your funnel visualization will appear here once your store has some visitor traffic.', 'ai-flash-tune')); ?></p>
                    </div>
                <?php else : ?>
                    <?php // If we have data, show the TABS and the initial DESKTOP VIEW. ?>
                    <div id="ai-flash-tune-funnel-flow-dashboard" class="ai-flash-tune-flex ai-flash-tune-flex-wrap ai-flash-tune-flex-center ai-flash-tune-funnel-flow">
                        <div class="ai-flash-tune-funnel-step ai-flash-tune-funnel-step-item">
                            <i class="fas fa-home ai-flash-tune-text-indigo-600 ai-flash-tune-text-3xl ai-flash-tune-mb-2"></i>
                            <p class="ai-flash-tune-font-medium ai-flash-tune-step-label"><?php echo esc_html(__('Landing Page', 'ai-flash-tune')); ?></p>
                            <p class="ai-flash-tune-text-sm ai-flash-tune-text-gray-600 ai-flash-tune-step-value"><?php echo number_format(esc_html($aiflt_funnel_data['landing_page_visits'])); ?> <?php echo esc_html(__('Visitors', 'ai-flash-tune')); ?></p>
                            <br>
                        </div>
                        <div class="ai-flash-tune-funnel-arrow"></div>
                        <div class="ai-flash-tune-funnel-step ai-flash-tune-funnel-step-item">
                            <i class="fas fa-box-open ai-flash-tune-text-indigo-600 ai-flash-tune-text-3xl ai-flash-tune-mb-2"></i>
                            <p class="ai-flash-tune-font-medium ai-flash-tune-step-label"><?php echo esc_html(__('Product Page', 'ai-flash-tune')); ?></p>
                            <p class="ai-flash-tune-text-sm ai-flash-tune-text-gray-600 ai-flash-tune-step-value">
                                <?php echo esc_html( number_format_i18n( (int) $aiflt_funnel_data['product_page_views'] ) ); ?>
                                <?php echo esc_html__( 'Views', 'ai-flash-tune' ); ?>
                            </p>
                            <p class="ai-flash-tune-text-xs ai-flash-tune-text-green-600 ai-flash-tune-step-value">
                                <?php
                                echo esc_html( round($aiflt_funnel_data['conversion_product'], 2))."% ".esc_html__( 'Conversion', 'ai-flash-tune' );
                                ?>
                            </p>
                        </div>
                        <div class="ai-flash-tune-funnel-arrow"></div>
                        <div class="ai-flash-tune-funnel-step ai-flash-tune-funnel-step-item">
                            <i class="fas fa-shopping-cart ai-flash-tune-text-indigo-600 ai-flash-tune-text-3xl ai-flash-tune-mb-2"></i>
                            <p class="ai-flash-tune-font-medium ai-flash-tune-step-label"><?php echo esc_html(__('Added to Cart', 'ai-flash-tune')); ?></p>
                            <p class="ai-flash-tune-text-sm ai-flash-tune-text-gray-600 ai-flash-tune-step-value">
                                <?php echo esc_html( number_format_i18n( (int) $aiflt_funnel_data['cart_entries'] ) ); ?>
                                <?php echo esc_html__( 'Cart Events', 'ai-flash-tune' );
                                ?>
                            </p>
                            <p class="ai-flash-tune-text-xs ai-flash-tune-text-green-600 ai-flash-tune-step-value">
                                <?php
                                echo esc_html( round($aiflt_funnel_data['conversion_cart'], 2))."% ".esc_html__( 'Conversion', 'ai-flash-tune' );
                                ?>
                            </p>
                        </div>
                        <div class="ai-flash-tune-funnel-arrow"></div>
                        <div class="ai-flash-tune-funnel-step ai-flash-tune-funnel-step-item">
                            <i class="fas fa-credit-card ai-flash-tune-text-indigo-600 ai-flash-tune-text-3xl ai-flash-tune-mb-2"></i>
                            <p class="ai-flash-tune-font-medium ai-flash-tune-step-label"><?php echo esc_html(__('Checkout Page', 'ai-flash-tune')); ?></p>
                            <p class="ai-flash-tune-text-sm ai-flash-tune-text-gray-600 ai-flash-tune-step-value">
                                <?php echo esc_html(number_format_i18n( (int) $aiflt_funnel_data['checkout_starts'] )); ?>
                                <?php echo esc_html__( 'Checkouts', 'ai-flash-tune' );
                                ?>
                            </p>
                            <p class="ai-flash-tune-text-xs ai-flash-tune-text-green-600 ai-flash-tune-step-value">
                                <?php
                                echo esc_html( round($aiflt_funnel_data['conversion_checkout'], 2))."% ".esc_html__( 'Conversion', 'ai-flash-tune' );
                                ?>
                            </p>
                        </div>
                    </div>
                <?php endif; ?>
            </div>
            <div class="ai-flash-tune-grid ai-flash-tune-gap-6 ai-flash-tune-grid-cols-1 ai-flash-tune-grid-cols-lg-2 ai-flash-tune-mb-8 ai-flash-tune-dashboard-lower-grid">
                <div class="ai-flash-tune-card ai-flash-tune-low-performers">
                    <span class="ai-flash-tune-text-xl ai-flash-tune-font-semibold ai-flash-tune-text-gray-800 ai-flash-tune-mb-4 ai-flash-tune-card-title">
                        <i class="fas fa-search-dollar ai-flash-tune-text-indigo-600 ai-flash-tune-mr-2"></i><?php echo esc_html(__('Low-Performing Product Finder', 'ai-flash-tune')); ?>
                    </span>
                    <?php if (!empty($aiflt_low_performing_products)) : ?>
                        <p class="ai-flash-tune-text-gray-700 ai-flash-tune-mb-4 ai-flash-tune-section-description"><?php echo esc_html(__('Flags high-traffic products that aren\'t selling, giving you a clear focus for optimization.', 'ai-flash-tune')); ?></p>
                        <ul class="ai-flash-tune-list-disc ai-flash-tune-list-inside ai-flash-tune-text-gray-700 ai-flash-tune-space-y-2 ai-flash-tune-low-performer-list">
                            <?php foreach ($aiflt_low_performing_products as $aiflt_product) : ?>
                                <li>
                                    <a href="<?php echo esc_url( admin_url( 'admin.php?page=product-detail&product_id=' . $aiflt_product['product_id'] ) ); ?>" class="ai-flash-tune-text-indigo-600 hover:ai-flash-tune-text-indigo-900">
                                        "<?php echo esc_html( $aiflt_product['product_name'] ); ?>"
                                    </a> - 
                                    <?php
                                    echo esc_html(
                                        sprintf(
                                            /* translators: 1: Number of views, 2: Number of sales */
                                            __( '%1$d views, %2$d sales', 'ai-flash-tune' ),
                                            $aiflt_product['views'],
                                            $aiflt_product['sales']
                                        )
                                    );
                                    ?>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                        <a class="ai-flash-tune-btn ai-flash-tune-btn-blue" href="<?php echo esc_url(admin_url('admin.php?page=products&status_filter=Low-Performing')); ?>">
                            <?php echo esc_html(__('View All Low-Performers', 'ai-flash-tune')); ?>
                        </a>
                    <?php else : ?>
                        <?php if ($aiflt_has_products) : ?>
                            <?php // Case 1: Products exist, but none are low-performing. ?>
                            <div class="ai-flash-tune-empty-state-message" style="text-align: left; padding: 0.7rem; margin-top: 1rem; border-style: solid;">
                                 <div class="ai-flash-tune-flex" style="align-items: flex-start; gap: 1rem;">
                                    <i class="fas fa-check-circle ai-flash-tune-text-green-500" style="font-size: 1.75rem; margin-top: 5px;"></i>
                                    <div>
                                        <p class="ai-flash-tune-empty-state-title" style="margin-bottom: 0.25rem;"><?php echo esc_html__('No Critical Issues Found', 'ai-flash-tune'); ?></p>
                                        <p class="ai-flash-tune-empty-state-text"><?php echo esc_html__('Our scan for products with high traffic but critically low sales found no issues. This is a great sign!', 'ai-flash-tune'); ?></p>
                                    </div>
                                </div>
                            </div>
                            <a class="ai-flash-tune-btn ai-flash-tune-btn-blue ai-flash-tune-mt-4" href="<?php echo esc_url(admin_url('admin.php?page=products')); ?>">
                                <?php echo esc_html(__('Review All Product Performance', 'ai-flash-tune')); ?>
                            </a>
                        <?php else : ?>
                            <?php // Case 2: No products exist in the store at all. ?>
                            <div class="ai-flash-tune-empty-state-message" style="text-align: left; padding: 0.7rem; margin-top: 1rem; border-style: solid;">
                                 <div class="ai-flash-tune-flex" style="align-items: flex-start; gap: 1rem;">
                                    <i class="fas fa-box-open ai-flash-tune-text-indigo-500" style="font-size: 1.75rem; margin-top: 5px;"></i>
                                    <div>
                                        <p class="ai-flash-tune-empty-state-title" style="margin-bottom: 0.25rem;"><?php echo esc_html__('No Products to Analyze', 'ai-flash-tune'); ?></p>
                                        <?php if (!$aiflt_is_wc_active) { ?>
                                            <p class="ai-flash-tune-empty-state-text"><?php echo esc_html__('This report will automatically find opportunities once you add products to your store.', 'ai-flash-tune'); ?></p>
                                        <?php } ?>
                                    </div>
                                </div>
                            </div>
                            <?php if ($aiflt_is_wc_active) { ?>
                                <a target="_blank" class="ai-flash-tune-btn ai-flash-tune-btn-blue ai-flash-tune-mt-4" href="<?php echo esc_url(admin_url('post-new.php?post_type=product')); ?>">
                                    <i class="fas fa-plus ai-flash-tune-mr-2"></i> <?php echo esc_html(__('Add Your First Product', 'ai-flash-tune')); ?>
                                </a>
                            <?php } ?>
                        <?php endif; ?>
                    <?php endif; ?>
                </div>
                <div class="ai-flash-tune-card ai-flash-tune-checkout-detector">
                    <span class="ai-flash-tune-text-xl ai-flash-tune-font-semibold ai-flash-tune-text-gray-800 ai-flash-tune-mb-4 ai-flash-tune-card-title">
                        <i class="fas fa-clipboard-list ai-flash-tune-text-indigo-600 ai-flash-tune-mr-2"></i><?php echo esc_html(__('Checkout Field Overload Detector', 'ai-flash-tune')); ?>
                    </span>
                    <p class="ai-flash-tune-text-gray-700 ai-flash-tune-mb-4 ai-flash-tune-section-description"><?php echo esc_html(__('Warns if checkout form has too many fields or friction.', 'ai-flash-tune')); ?></p>
                    <div class="ai-flash-tune-flex <?php echo esc_attr($aiflt_checkout_detector_data['css_class']); ?> ai-flash-tune-font-semibold ai-flash-tune-text-lg ai-flash-tune-detector-status ai-flash-tune-card-title">
                        <i class="<?php echo esc_attr($aiflt_checkout_detector_data['icon_class']); ?> ai-flash-tune-mr-2"></i> <span class="ai-flash-tune-detector-text"><?php echo esc_html($aiflt_checkout_detector_data['message']); ?></span>
                    </div>
                    <?php if ($aiflt_checkout_detector_data['status'] === 'High Friction') : ?>
                        <p class="ai-flash-tune-text-sm ai-flash-tune-text-gray-600 ai-flash-tune-mt-2"><?php echo esc_html(__('Your checkout form has more fields than recommended. This can lead to higher abandonment rates.', 'ai-flash-tune')); ?></p>
                    <?php endif; ?>
                </div>
            </div>
            <div class="ai-flash-tune-card ai-flash-tune-mb-8 ai-flash-tune-growth-cta-card">
                <div class="ai-flash-tune-free-card-header">
                    <span class="ai-flash-tune-text-xl ai-flash-tune-font-semibold ai-flash-tune-text-gray-800 ai-flash-tune-card-title">
                        <i class="fas fa-rocket ai-flash-tune-text-indigo-600 ai-flash-tune-mr-2"></i><?php echo esc_html__('Ready for deeper AI guidance?', 'ai-flash-tune'); ?>
                    </span>
                    <span class="ai-flash-tune-free-badge"><?php echo esc_html__('Optional', 'ai-flash-tune'); ?></span>
                </div>
                <p class="ai-flash-tune-text-gray-700 ai-flash-tune-section-description">
                    <?php echo esc_html__('The free dashboard gives local tracking, health checks, and priority hints. Pro modules add AI action plans, product optimization, cart recovery automation, checkout fixes, alerts, segmentation, and reports.', 'ai-flash-tune'); ?>
                </p>
                <div class="ai-flash-tune-growth-module-grid">
                    <div class="ai-flash-tune-growth-module"><i class="fas fa-brain"></i><span><?php echo esc_html__('AI Action Center', 'ai-flash-tune'); ?></span></div>
                    <div class="ai-flash-tune-growth-module"><i class="fas fa-stethoscope"></i><span><?php echo esc_html__('Product Optimizer', 'ai-flash-tune'); ?></span></div>
                    <div class="ai-flash-tune-growth-module"><i class="fas fa-envelope"></i><span><?php echo esc_html__('Cart Recovery', 'ai-flash-tune'); ?></span></div>
                    <div class="ai-flash-tune-growth-module"><i class="fas fa-credit-card"></i><span><?php echo esc_html__('Checkout Optimizer', 'ai-flash-tune'); ?></span></div>
                </div>
                <div class="ai-flash-tune-mt-4">
                    <a class="ai-flash-tune-btn ai-flash-tune-btn-blue" href="<?php echo esc_url(admin_url('admin.php?page=addon-manager')); ?>">
                        <?php echo esc_html__('Review available modules', 'ai-flash-tune'); ?>
                    </a>
                </div>
            </div>
        </div>
    </div>
</div>
