<?php
/**
 * Enhanced Admin Page Template
 *
 * Modern admin interface for Schemafy by SCHEMAFY
 * Features: Dashboard, Smart Builder, AI Paste Mode, URL Mapping
 *
 * @package Schemafy
 * @since 1.2.0
 * @author SCHEMAFY
 */

// Prevent direct access
if (!defined('ABSPATH')) {
    exit;
}

// Get the active tab from URL parameter (read-only navigation, whitelisted below; no state change).
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only tab switch, value validated against $valid_tabs below
$active_tab = isset($_GET['tab']) ? sanitize_text_field(wp_unslash($_GET['tab'])) : 'dashboard';

// Valid tabs list
$valid_tabs = array('dashboard', 'builder', 'manual', 'ai-paste', 'auto-generator', 'bulk', 'sitemap', 'metadata');

// Validate the tab
if (!in_array($active_tab, $valid_tabs)) {
    $active_tab = 'dashboard';
}

// Premium tabs are never redirected away: when the user's plan (or the free
// build) doesn't include them, schemafy_render_premium_tab() shows an upgrade
// teaser in place of the tool. No payment-based redirect happens here.

// Check if onboarding should be shown - PER USER
$current_user_id = get_current_user_id();
$onboarding_dismissed = get_user_meta($current_user_id, 'schemafy_onboarding_dismissed', true);
$show_onboarding = empty($onboarding_dismissed);

// Helper function to check if a tab is active
function schemafy_is_active_tab($tab, $active_tab) {
    return $tab === $active_tab ? ' active' : '';
}
?>

<!-- Admin notices banner: JS moves all WP notices here, above the plugin layout -->
<div id="schemafy-notices-banner"></div>

<div class="wrap schemafy-admin-wrap-v2 schemafy-sidebar-layout">
    <!-- Sidebar Navigation -->
    <aside class="schemafy-sidebar">
        <!-- Logo -->
        <div class="schemafy-sidebar-header">
            <div class="schemafy-logo-compact">
                <span class="dashicons dashicons-editor-code"></span>
                <span class="schemafy-logo-text">Schemafy<span class="schemafy-logo-dot">.</span></span>
            </div>
        </div>

        <!-- Navigation Menu -->
        <nav class="schemafy-sidebar-nav">

            <!-- ── PRIMARY GROUP: 5 main schema tabs ── -->
            <div class="schemafy-nav-section">

                <button class="schemafy-tab-btn-v2 schemafy-tab-primary<?php echo esc_attr( schemafy_is_active_tab('dashboard', $active_tab) ); ?>" data-tab="dashboard">
                    <span class="schemafy-nav-icon-box"><span class="dashicons dashicons-dashboard"></span></span>
                    <span class="schemafy-nav-text"><?php esc_html_e('Dashboard', 'schemafy'); ?></span>
                </button>

                <button class="schemafy-tab-btn-v2 schemafy-tab-primary<?php echo esc_attr( schemafy_is_active_tab('builder', $active_tab) ); ?>" data-tab="builder">
                    <span class="schemafy-nav-icon-box"><span class="dashicons dashicons-hammer"></span></span>
                    <span class="schemafy-nav-text"><?php esc_html_e('Smart Builder', 'schemafy'); ?></span>
                </button>

                <button class="schemafy-tab-btn-v2 schemafy-tab-primary<?php echo esc_attr( schemafy_is_active_tab('manual', $active_tab) ); ?>" data-tab="manual">
                    <span class="schemafy-nav-icon-box"><span class="dashicons dashicons-editor-code"></span></span>
                    <span class="schemafy-nav-text"><?php esc_html_e('Manual JSON', 'schemafy'); ?></span>
                </button>

                <button class="schemafy-tab-btn-v2 schemafy-tab-primary<?php echo esc_attr( schemafy_is_active_tab('ai-paste', $active_tab) ); ?>" data-tab="ai-paste">
                    <span class="schemafy-nav-icon-box"><span class="dashicons dashicons-superhero-alt"></span></span>
                    <span class="schemafy-nav-text"><?php esc_html_e('AI Generator', 'schemafy'); ?></span>
                    <?php if ( ! schemafy_premium_tab_available('ai-paste') ): ?><span class="schemafy-tier-badge schemafy-tier-full">Pro</span><?php endif; ?>
                </button>

                <button class="schemafy-tab-btn-v2 schemafy-tab-primary<?php echo esc_attr( schemafy_is_active_tab('auto-generator', $active_tab) ); ?>" data-tab="auto-generator">
                    <span class="schemafy-nav-icon-box"><span class="dashicons dashicons-visibility"></span></span>
                    <span class="schemafy-nav-text"><?php esc_html_e('AutoGenerator', 'schemafy'); ?></span>
                    <?php if ( ! schemafy_premium_tab_available('auto-generator') ): ?><span class="schemafy-tier-badge schemafy-tier-mid">Pro</span><?php endif; ?>
                </button>

                <button class="schemafy-tab-btn-v2 schemafy-tab-primary<?php echo esc_attr( schemafy_is_active_tab('bulk', $active_tab) ); ?>" data-tab="bulk">
                    <span class="schemafy-nav-icon-box"><span class="dashicons dashicons-database"></span></span>
                    <span class="schemafy-nav-text"><?php esc_html_e('Rich Snippets', 'schemafy'); ?></span>
                </button>

            </div>

            <!-- ── DIVIDER ── -->
            <div class="schemafy-nav-divider"></div>

            <!-- ── SECONDARY GROUP: tools & settings ── -->
            <div class="schemafy-nav-section">
                <span class="schemafy-nav-label"><?php esc_html_e('Tools', 'schemafy'); ?></span>

                <button class="schemafy-tab-btn-v2<?php echo esc_attr( schemafy_is_active_tab('metadata', $active_tab) ); ?>" data-tab="metadata">
                    <span class="dashicons dashicons-tag"></span>
                    <span class="schemafy-nav-text"><?php esc_html_e('Meta Tags', 'schemafy'); ?></span>
                </button>

                <button class="schemafy-tab-btn-v2<?php echo esc_attr( schemafy_is_active_tab('sitemap', $active_tab) ); ?>" data-tab="sitemap">
                    <span class="dashicons dashicons-networking"></span>
                    <span class="schemafy-nav-text"><?php esc_html_e('Sitemap', 'schemafy'); ?></span>
                </button>

            </div>
        </nav>

        <!-- Sidebar Footer -->
        <div class="schemafy-sidebar-footer">
            <a href="#" id="schemafy-restart-tutorial" class="schemafy-sidebar-link" title="<?php esc_html_e('Start the guided tour again', 'schemafy'); ?>">
                <span class="dashicons dashicons-welcome-learn-more"></span>
                <span><?php esc_html_e('Tutorial', 'schemafy'); ?></span>
            </a>
            <a href="https://schemafy.net/docs/introduction" target="_blank" class="schemafy-sidebar-link">
                <span class="dashicons dashicons-book"></span>
                <span><?php esc_html_e('Documentation', 'schemafy') ?></span>
            </a>
            <a href="https://schemafy.net/" target="_blank" class="schemafy-sidebar-link" title="<?php esc_html_e('Developed By Schemafy & WebTek', 'schemafy'); ?>">
                <span class="dashicons dashicons-sos"></span>
                <span><?php esc_html_e('Support', 'schemafy'); ?></span>
            </a>
            <div class="schemafy-version-badge">v<?php echo esc_html( SCHEMAFY_VERSION ); ?></div>
        </div>
    </aside>

    <!-- Sidebar Toggle Tab (placed after sidebar for CSS sibling selector) -->
    <button class="schemafy-sidebar-toggle-tab" id="schemafy-toggle-sidebar" title="<?php esc_html_e('Toggle Sidebar', 'schemafy'); ?>">
        <span class="dashicons dashicons-arrow-right-alt2"></span>
    </button>

    <!-- Main Content Area -->
    <main class="schemafy-main-content">
        <!-- Top Bar -->
        <header class="schemafy-topbar">
            <h1 class="schemafy-page-title" id="schemafy-current-page-title"><?php esc_html_e('Dashboard', 'schemafy'); ?></h1>
            <div class="schemafy-topbar-actions">
                <button class="schemafy-btn-icon" id="schemafy-refresh-stats" title="<?php esc_html_e('Refresh Stats', 'schemafy'); ?>">
                    <span class="dashicons dashicons-image-rotate"></span>
                </button>
            </div>
        </header>

        <!-- Content Container -->
        <div class="schemafy-content-area">

    <!-- URL Detail View (Hidden by default, overlays dashboard) -->
    <div id="schemafy-url-detail-view" class="schemafy-url-detail-overlay" style="display: none;">
        <div class="schemafy-card-v2">
            <div id="schemafy-url-detail-container">
                <!-- Content will be loaded dynamically -->
            </div>
        </div>
    </div>

    <!-- Tab: Dashboard -->
    <div id="schemafy-tab-dashboard" class="schemafy-tab-content-v2<?php echo esc_attr( schemafy_is_active_tab('dashboard', $active_tab) ); ?>">

        <!-- KPI Row -->
        <div class="schemafy-kpi-row">
            <div class="schemafy-kpi-card">
                <div class="schemafy-kpi-value" id="schemafy-stat-urls">0</div>
                <div class="schemafy-kpi-label"><?php esc_html_e('URLs with Schemas', 'schemafy'); ?></div>
            </div>
            <div class="schemafy-kpi-card">
                <div class="schemafy-kpi-value" id="schemafy-stat-total">0</div>
                <div class="schemafy-kpi-label"><?php esc_html_e('Total Schemas', 'schemafy'); ?></div>
            </div>
            <div class="schemafy-kpi-card">
                <div class="schemafy-kpi-value" id="schemafy-stat-active">0</div>
                <div class="schemafy-kpi-label"><?php esc_html_e('Active', 'schemafy'); ?></div>
            </div>
            <div class="schemafy-kpi-card">
                <div class="schemafy-kpi-value schemafy-kpi-muted" id="schemafy-stat-inactive">0</div>
                <div class="schemafy-kpi-label"><?php esc_html_e('Inactive', 'schemafy'); ?></div>
            </div>
        </div>

        <!-- Coverage Overview Chart -->
        <div class="schemafy-card-v2" id="schemafy-coverage-card">
            <div class="schemafy-card-header-v2">
                <h2><?php esc_html_e('Coverage Overview', 'schemafy'); ?></h2>
            </div>
            <div class="schemafy-card-body-v2"><div class="schemafy-coverage-layout">
                    <div class="schemafy-coverage-donut-wrap">
                        <svg viewBox="0 0 120 120" class="schemafy-donut-svg" aria-hidden="true">
                            <circle cx="60" cy="60" r="50" fill="none" stroke="#e4e4e7" stroke-width="14"/>
                            <circle id="schemafy-donut-arc-covered" cx="60" cy="60" r="50" fill="none"
                                stroke="#4f6ef5" stroke-width="14" stroke-linecap="round"
                                stroke-dasharray="0 314.16"
                                transform="rotate(-90 60 60)"
                                style="transition:stroke-dasharray 1s cubic-bezier(.4,0,.2,1),stroke .4s"/>
                        </svg>
                        <div class="schemafy-donut-center">
                            <span class="schemafy-donut-pct" id="schemafy-coverage-pct">—</span>
                            <span class="schemafy-donut-lbl">Coverage</span>
                        </div>
                    </div>
                    <div class="schemafy-coverage-metrics">
                        <div class="schemafy-coverage-headline" id="schemafy-coverage-headline"><?php esc_html_e('Loading coverage data…', 'schemafy'); ?></div>
                        <div class="schemafy-cov-stats-grid">
                            <div class="schemafy-cov-stat">
                                <span class="schemafy-cov-stat-val" id="schemafy-cov-covered-val">—</span>
                                <span class="schemafy-cov-stat-lbl"><span class="schemafy-cov-dot" style="background:#4f6ef5"></span><?php esc_html_e('URLs covered', 'schemafy'); ?></span>
                            </div>
                            <div class="schemafy-cov-stat">
                                <span class="schemafy-cov-stat-val" id="schemafy-cov-missing-val">—</span>
                                <span class="schemafy-cov-stat-lbl"><span class="schemafy-cov-dot" style="background:#e4e4e7"></span><?php esc_html_e('Pages without schema', 'schemafy'); ?></span>
                            </div>
                            <div class="schemafy-cov-stat">
                                <span class="schemafy-cov-stat-val" id="schemafy-cov-schemas-val">—</span>
                                <span class="schemafy-cov-stat-lbl"><span class="schemafy-cov-dot" style="background:#8b5cf6"></span><?php esc_html_e('Total schemas', 'schemafy'); ?></span>
                            </div>
                            <div class="schemafy-cov-stat">
                                <span class="schemafy-cov-stat-val" id="schemafy-cov-active-val">—</span>
                                <span class="schemafy-cov-stat-lbl"><span class="schemafy-cov-dot" style="background:#22c55e"></span><?php esc_html_e('Active rate', 'schemafy'); ?></span>
                            </div>
                        </div>
                        <div id="schemafy-coverage-tip" class="schemafy-coverage-tip" style="display:none"></div>
                    </div>
                </div>
            </div>
        </div>

        <!-- URL Map Section -->
        <div class="schemafy-card-v2">
            <div class="schemafy-card-header-v2">
                <h2><?php esc_html_e('Schema Map', 'schemafy'); ?></h2>
                <div class="schemafy-card-actions">
                    <input
                        type="search"
                        id="schemafy-url-map-search"
                        class="schemafy-search-input-v2 schemafy-search-compact"
                        placeholder="<?php esc_html_e('Search URLs...', 'schemafy'); ?>"
                    >
                    <button class="schemafy-btn-secondary schemafy-btn-icon" id="schemafy-refresh-dashboard" title="<?php esc_html_e('Refresh', 'schemafy'); ?>">
                        <span class="dashicons dashicons-image-rotate"></span>
                    </button>
                </div>
            </div>
            <div class="schemafy-card-body-v2">
                <div id="schemafy-url-map-container">
                    <div class="schemafy-loading-v2">
                        <div class="schemafy-spinner-v2"></div>
                        <p><?php esc_html_e('Loading URL map...', 'schemafy'); ?></p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Schema Types Distribution -->
        <div class="schemafy-card-v2">
            <div class="schemafy-card-header-v2">
                <h2><?php esc_html_e('Schema Types', 'schemafy'); ?></h2>
            </div>
            <div class="schemafy-card-body-v2">
                <div id="schemafy-types-chart" class="schemafy-chart-container">
                    <!-- Chart will be rendered here -->
                </div>
            </div>
        </div>

        <!-- Plugin Compatibility Settings -->
        <div class="schemafy-card-v2">
            <div class="schemafy-card-header-v2">
                <h2><?php esc_html_e('Plugin Compatibility', 'schemafy'); ?></h2>
            </div>
            <div class="schemafy-card-body-v2">
                <p class="schemafy-card-description">
                    <?php esc_html_e('When enabled, Schemafy will manage schema markup and suppress duplicate output from the selected plugins.', 'schemafy'); ?>
                </p>
                <?php
                $compat = get_option('schemafy_compatibility', array());
                $plugins_detected = array();

                if (SCHEMAFY_Meta_Store::is_active('yoast')) {
                    $plugins_detected['disable_yoast_schema'] = array(
                        'name' => 'Yoast SEO',
                        'desc' => __('Disable Yoast SEO schema graph output (Article, WebPage, Organization, BreadcrumbList, etc.)', 'schemafy'),
                    );
                }
                if (SCHEMAFY_Meta_Store::is_active('rank-math')) {
                    $plugins_detected['disable_rankmath_schema'] = array(
                        'name' => 'Rank Math',
                        'desc' => __('Disable Rank Math JSON-LD schema output', 'schemafy'),
                    );
                }
                if (SCHEMAFY_Meta_Store::is_active('aioseo')) {
                    $plugins_detected['disable_aioseo_schema'] = array(
                        'name' => 'All in One SEO',
                        'desc' => __('Disable AIOSEO schema markup output', 'schemafy'),
                    );
                }
                if (SCHEMAFY_Meta_Store::is_active('seopress')) {
                    $plugins_detected['disable_seopress_schema'] = array(
                        'name' => 'SEOPress',
                        'desc' => __('Disable SEOPress JSON-LD schema output', 'schemafy'),
                    );
                }
                if (SCHEMAFY_Meta_Store::is_active('the-seo-framework')) {
                    $plugins_detected['disable_tsf_schema'] = array(
                        'name' => 'The SEO Framework',
                        'desc' => __('Disable The SEO Framework schema output', 'schemafy'),
                    );
                }
                if (class_exists('WooCommerce')) {
                    $plugins_detected['disable_woocommerce_schema'] = array(
                        'name' => 'WooCommerce',
                        'desc' => __('Disable WooCommerce built-in product structured data (recommended when Schemafy generates Product schema)', 'schemafy'),
                    );
                }

                if (SCHEMAFY_Meta_Store::is_active('yoast') || SCHEMAFY_Meta_Store::is_active('rank-math')) {
                    $plugins_detected['disable_other_sitemaps'] = array(
                        'name' => __('Other XML sitemaps', 'schemafy'),
                        'desc' => __('Turn off Yoast SEO / Rank Math sitemaps and point them at Schemafy\'s, so search engines see a single sitemap index (only applies while Schemafy\'s sitemap is enabled)', 'schemafy'),
                    );
                }

                // Not tied to a specific plugin: this one strips JSON-LD that any
                // theme or block renders into post content.
                $plugins_detected['strip_embedded_schemas'] = array(
                    'name' => __('Schemas embedded in content', 'schemafy'),
                    'desc' => __('Remove JSON-LD that themes or blocks output inside the post content, so only Schemafy\'s schemas remain', 'schemafy'),
                );

                if (empty($plugins_detected)) : ?>
                    <p class="schemafy-empty-text">
                        <?php esc_html_e('No supported plugins detected. Install Yoast SEO, Rank Math, AIOSEO, or WooCommerce to see compatibility options.', 'schemafy'); ?>
                    </p>
                <?php else :
                    foreach ($plugins_detected as $key => $plugin) : ?>
                        <div class="schemafy-compat-row">
                            <div>
                                <strong><?php echo esc_html($plugin['name']); ?></strong>
                                <p><?php echo esc_html($plugin['desc']); ?></p>
                            </div>
                            <label class="schemafy-toggle-switch">
                                <input type="checkbox" class="schemafy-compat-toggle" data-key="<?php echo esc_attr($key); ?>"
                                       <?php checked(!empty($compat[$key])); ?> />
                                <span class="schemafy-toggle-slider"></span>
                            </label>
                        </div>
                    <?php endforeach; ?>
                    <div class="schemafy-action-row">
                        <button id="schemafy-save-compatibility" class="schemafy-btn-primary">
                            <span class="dashicons dashicons-saved"></span>
                            <?php esc_html_e('Save Compatibility Settings', 'schemafy'); ?>
                        </button>
                    </div>
                <?php endif; ?>
            </div>
        </div>
    </div>

    <!-- Tab: Smart Builder -->
    <div id="schemafy-tab-builder" class="schemafy-tab-content-v2<?php echo esc_attr( schemafy_is_active_tab('builder', $active_tab) ); ?>">

    <!-- Inlined styles: guaranteed last-in-cascade regardless of CSS enqueue order --><div class="schemafy-builder-container">

            <!-- Sidebar: Category Filter + Schema Type Selection -->
            <div class="schemafy-builder-sidebar">
                <div class="schemafy-builder-sidebar-header">
                    <span class="schemafy-builder-sidebar-title"><?php esc_html_e('What are you adding?', 'schemafy'); ?></span>
                    <span class="schemafy-builder-sidebar-sub"><?php esc_html_e('Filter by category, then pick a type', 'schemafy'); ?></span>
                </div>

                <!-- Category filter pills -->
                <div class="schemafy-category-nav" id="schemafy-category-nav" role="group" aria-label="<?php esc_html_e('Filter by category', 'schemafy'); ?>">
                    <button class="schemafy-category-btn active" data-category="all" type="button">
                        <span class="schemafy-category-icon" aria-hidden="true">🔍</span>
                        <span class="schemafy-category-label"><?php esc_html_e('All', 'schemafy'); ?></span>
                    </button>
                    <button class="schemafy-category-btn" data-category="content" type="button">
                        <span class="schemafy-category-icon" aria-hidden="true">📝</span>
                        <span class="schemafy-category-label"><?php esc_html_e('Content', 'schemafy'); ?></span>
                    </button>
                    <button class="schemafy-category-btn" data-category="business" type="button">
                        <span class="schemafy-category-icon" aria-hidden="true">🏢</span>
                        <span class="schemafy-category-label"><?php esc_html_e('Business', 'schemafy'); ?></span>
                    </button>
                    <button class="schemafy-category-btn" data-category="commerce" type="button">
                        <span class="schemafy-category-icon" aria-hidden="true">🛍️</span>
                        <span class="schemafy-category-label"><?php esc_html_e('Commerce', 'schemafy'); ?></span>
                    </button>
                    <button class="schemafy-category-btn" data-category="people" type="button">
                        <span class="schemafy-category-icon" aria-hidden="true">👥</span>
                        <span class="schemafy-category-label"><?php esc_html_e('People', 'schemafy'); ?></span>
                    </button>
                    <button class="schemafy-category-btn" data-category="media" type="button">
                        <span class="schemafy-category-icon" aria-hidden="true">🎬</span>
                        <span class="schemafy-category-label"><?php esc_html_e('Media', 'schemafy'); ?></span>
                    </button>
                </div>

                <div class="schemafy-schema-types-grid" role="group" aria-label="<?php esc_html_e('Schema Type Selection', 'schemafy'); ?>">
                    <?php schemafy_render_builder_cards(); ?>
                </div>
            </div>

            <!-- Main: Step Indicator + Dynamic Form Builder -->
            <div class="schemafy-builder-main">

                <!-- Step progress indicator -->
                <div class="schemafy-step-indicator" id="schemafy-step-indicator" aria-label="<?php esc_html_e('Progress steps', 'schemafy'); ?>">
                    <div class="schemafy-step-item active" data-step="1">
                        <div class="schemafy-step-bubble" aria-hidden="true">1</div>
                        <span class="schemafy-step-label"><?php esc_html_e('Choose type', 'schemafy'); ?></span>
                    </div>
                    <div class="schemafy-step-connector" aria-hidden="true"></div>
                    <div class="schemafy-step-item" data-step="2">
                        <div class="schemafy-step-bubble" aria-hidden="true">2</div>
                        <span class="schemafy-step-label"><?php esc_html_e('Fill details', 'schemafy'); ?></span>
                    </div>
                    <div class="schemafy-step-connector" aria-hidden="true"></div>
                    <div class="schemafy-step-item" data-step="3">
                        <div class="schemafy-step-bubble" aria-hidden="true">3</div>
                        <span class="schemafy-step-label"><?php esc_html_e('Apply to page', 'schemafy'); ?></span>
                    </div>
                </div>

                <div id="schemafy-builder-form-container">
                    <div class="schemafy-builder-placeholder">
                        <span class="schemafy-builder-placeholder-emoji">✨</span>
                        <h3><?php esc_html_e('Your Google Rich Results start here', 'schemafy'); ?></h3>
                        <p><?php esc_html_e('Pick a content type on the left — the right fields appear automatically. No coding needed.', 'schemafy'); ?></p>
                        <div class="schemafy-placeholder-tips">
                            <div class="schemafy-placeholder-tip">
                                <span class="schemafy-placeholder-tip-icon" aria-hidden="true">📰</span>
                                <span><?php echo wp_kses_post( __('<strong>Article</strong> — great for blogs & news', 'schemafy') ); ?></span>
                            </div>
                            <div class="schemafy-placeholder-tip">
                                <span class="schemafy-placeholder-tip-icon" aria-hidden="true">🛍️</span>
                                <span><?php echo wp_kses_post( __('<strong>Product</strong> — shows price & rating on Google', 'schemafy') ); ?></span>
                            </div>
                            <div class="schemafy-placeholder-tip">
                                <span class="schemafy-placeholder-tip-icon" aria-hidden="true">❓</span>
                                <span><?php echo wp_kses_post( __('<strong>FAQ</strong> — answers expand in search results', 'schemafy') ); ?></span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Preview Sidebar -->
            <div class="schemafy-builder-preview">
                <div class="schemafy-preview-header">
                    <h3><?php esc_html_e('Live Preview', 'schemafy'); ?></h3>
                    <button class="schemafy-btn-icon" id="schemafy-copy-json" type="button" title="<?php esc_html_e('Copy code', 'schemafy'); ?>" aria-label="<?php esc_html_e('Copy schema code to clipboard', 'schemafy'); ?>">
                        <span class="dashicons dashicons-clipboard" aria-hidden="true"></span>
                    </button>
                </div>

                <!-- Friendly status card (always visible) -->
                <div class="schemafy-preview-friendly" id="schemafy-preview-friendly">
                    <div class="schemafy-preview-friendly-icon" aria-hidden="true">📋</div>
                    <p class="schemafy-preview-friendly-hint"><?php esc_html_e('Fill in the form to see your schema ready here.', 'schemafy'); ?></p>
                    <div class="schemafy-preview-friendly-rows" id="schemafy-friendly-rows" style="display:none">
                        <div class="schemafy-friendly-row">
                            <span class="schemafy-friendly-label"><?php esc_html_e('Type', 'schemafy'); ?></span>
                            <span class="schemafy-friendly-value" id="schemafy-friendly-type">–</span>
                        </div>
                        <div class="schemafy-friendly-row">
                            <span class="schemafy-friendly-label"><?php esc_html_e('Page', 'schemafy'); ?></span>
                            <span class="schemafy-friendly-value schemafy-friendly-value-url" id="schemafy-friendly-url">–</span>
                        </div>
                        <div class="schemafy-friendly-row">
                            <span class="schemafy-friendly-label"><?php esc_html_e('Fields', 'schemafy'); ?></span>
                            <span class="schemafy-friendly-value" id="schemafy-friendly-fields">–</span>
                        </div>
                    </div>
                    <button class="schemafy-toggle-json-btn" id="schemafy-toggle-json-preview" type="button" aria-expanded="false">
                        <span class="dashicons dashicons-editor-code" aria-hidden="true"></span>
                        <?php esc_html_e('View technical code', 'schemafy'); ?>
                        <span class="schemafy-toggle-arrow" aria-hidden="true">▼</span>
                    </button>
                </div>

                <!-- Technical JSON preview (collapsible) -->
                <div class="schemafy-preview-body schemafy-preview-body--collapsed" id="schemafy-preview-body" style="display:none">
                    <pre id="schemafy-json-preview" role="region" aria-label="<?php esc_html_e('Schema JSON Preview', 'schemafy'); ?>"><code>{
  "@context": "https://schema.org",
  "@type": "...",
  "name": "..."
}</code></pre>
                </div>

                <div class="schemafy-preview-footer">
                    <button class="schemafy-btn-primary schemafy-btn-block" id="schemafy-save-builder-schema" type="button" aria-label="<?php esc_html_e('Save schema to database', 'schemafy'); ?>">
                        <span class="dashicons dashicons-yes-alt" aria-hidden="true"></span>
                        <?php esc_html_e('Apply to My Page', 'schemafy'); ?>
                    </button>
                </div>
            </div>

        </div>
    </div>

    <!-- Tab: AI Generator -->
    <?php schemafy_render_premium_tab( 'ai-paste', $active_tab ); ?>

    <!-- Tab: Manual JSON -->
    <?php require SCHEMAFY_PLUGIN_PATH . 'templates/tab-manual.php'; ?>

    <!-- Tab: Bulk Manager -->
    <div id="schemafy-tab-bulk" class="schemafy-tab-content-v2<?php echo esc_attr( schemafy_is_active_tab('bulk', $active_tab) ); ?>"><div id="schemafy-bulk-manager-main-card" class="schemafy-card-v2">
            <!-- Header: title + Export / Import -->
            <div class="schemafy-card-header-v2">
                <h2>
                    <span class="dashicons dashicons-database"></span>
                    <?php esc_html_e('Rich Snippets Manager', 'schemafy'); ?>
                </h2>
                <div class="schemafy-card-actions">
                    <button id="schemafy-bulk-export-csv" class="schemafy-btn-secondary">
                        <span class="dashicons dashicons-download"></span>
                        <?php esc_html_e('Export', 'schemafy'); ?>
                    </button>
                    <button id="schemafy-bulk-import-csv" class="schemafy-btn-secondary">
                        <span class="dashicons dashicons-upload"></span>
                        <?php esc_html_e('Import', 'schemafy'); ?>
                    </button>
                </div>
            </div>

            <!-- Stats Bar: counts + active rate -->
            <div class="schemafy-stats-bar">
                <div class="schemafy-stats-bar-group">
                    <div class="schemafy-stat-inline">
                        <span id="stat-total" class="schemafy-stat-inline-value">0</span>
                        <span class="schemafy-stat-inline-label"><?php esc_html_e('Total', 'schemafy'); ?></span>
                    </div>
                    <div class="schemafy-stat-inline">
                        <span id="stat-active" class="schemafy-stat-inline-value value-success">0</span>
                        <span class="schemafy-stat-inline-label"><?php esc_html_e('Active', 'schemafy'); ?></span>
                    </div>
                    <div class="schemafy-stat-inline">
                        <span id="stat-inactive" class="schemafy-stat-inline-value value-warning">0</span>
                        <span class="schemafy-stat-inline-label"><?php esc_html_e('Inactive', 'schemafy'); ?></span>
                    </div>
                    <div class="schemafy-stat-inline">
                        <span id="stat-types" class="schemafy-stat-inline-value value-primary">0</span>
                        <span class="schemafy-stat-inline-label"><?php esc_html_e('Types', 'schemafy'); ?></span>
                    </div>
                    <div class="schemafy-stat-sep"></div>
                    <div class="schemafy-stat-inline stat-active-rate-wrap">
                        <span id="stat-active-rate" class="schemafy-stat-inline-value" style="font-size:20px;color:#4f6ef5">—</span>
                        <span class="schemafy-stat-inline-label"><?php esc_html_e('Active Rate', 'schemafy'); ?></span>
                        <div class="schemafy-rsm-rate-bar"><div id="stat-active-rate-bar" class="schemafy-rsm-rate-bar-fill" style="width:0%"></div></div>
                    </div>
                </div>
                <!-- Hidden compat elements -->
                <div id="stat-active-percent"  style="display:none;"></div>
                <div id="stat-inactive-percent" style="display:none;"></div>
                <div id="stat-types-list"       style="display:none;"></div>
                <!-- Tools Toggle -->
                <button id="schemafy-toggle-tools" class="schemafy-btn-secondary">
                    <span class="dashicons dashicons-admin-tools"></span>
                    <?php esc_html_e('Tools', 'schemafy'); ?>
                    <span class="dashicons dashicons-arrow-down-alt2"></span>
                </button>
            </div>

            <!-- Health Strip: always-visible schema health metrics -->
            <div class="schemafy-rsm-health-strip">
                <div class="schemafy-rsm-health-item">
                    <span id="bulk-health-coverage" class="schemafy-rsm-health-val">…</span>
                    <span class="schemafy-rsm-health-lbl"><?php esc_html_e('URL Coverage', 'schemafy'); ?></span>
                </div>
                <div class="schemafy-rsm-health-item">
                    <span id="bulk-health-valid" class="schemafy-rsm-health-val good">…</span>
                    <span class="schemafy-rsm-health-lbl"><?php esc_html_e('Valid Schemas', 'schemafy'); ?></span>
                </div>
                <div class="schemafy-rsm-health-item">
                    <span id="bulk-health-errors" class="schemafy-rsm-health-val bad">…</span>
                    <span class="schemafy-rsm-health-lbl"><?php esc_html_e('Errors', 'schemafy'); ?></span>
                </div>
                <div class="schemafy-rsm-health-item">
                    <span id="bulk-health-orphans" class="schemafy-rsm-health-val warn">…</span>
                    <span class="schemafy-rsm-health-lbl"><?php esc_html_e('Orphan Schemas', 'schemafy'); ?></span>
                </div>
            </div>

            <!-- Collapsible Tools Panel (bulk actions only) -->
            <div id="schemafy-tools-panel" class="schemafy-tools-panel">
                <!-- Row 1: tools included in this build -->
                <div class="schemafy-tools-grid">
                    <button id="schemafy-bulk-validate-all" class="schemafy-tool-btn">
                        <div class="schemafy-tool-btn-icon icon-primary">
                            <span class="dashicons dashicons-yes-alt"></span>
                        </div>
                        <div>
                            <div class="schemafy-tool-btn-title"><?php esc_html_e('Validate All', 'schemafy'); ?></div>
                            <div class="schemafy-tool-btn-desc"><?php esc_html_e('Check schemas for errors', 'schemafy'); ?></div>
                        </div>
                    </button>
                    <button id="schemafy-bulk-refresh-health" class="schemafy-tool-btn">
                        <div class="schemafy-tool-btn-icon icon-info">
                            <span class="dashicons dashicons-image-rotate"></span>
                        </div>
                        <div>
                            <div class="schemafy-tool-btn-title"><?php esc_html_e('Refresh Stats', 'schemafy'); ?></div>
                            <div class="schemafy-tool-btn-desc"><?php esc_html_e('Update health statistics', 'schemafy'); ?></div>
                        </div>
                    </button>
                    <button id="schemafy-csv-help" class="schemafy-tool-btn">
                        <div class="schemafy-tool-btn-icon icon-gray">
                            <span class="dashicons dashicons-editor-help"></span>
                        </div>
                        <div>
                            <div class="schemafy-tool-btn-title"><?php esc_html_e('Import/Export Guide', 'schemafy'); ?></div>
                            <div class="schemafy-tool-btn-desc"><?php esc_html_e('CSV format instructions', 'schemafy'); ?></div>
                        </div>
                    </button>
                </div>

                <?php
                // Row 2 — bulk maintenance tools. The paid build hooks this and
                // renders them; the free package has no such code, so it shows a
                // static upgrade card instead of disabled buttons.
                if ( has_action( 'schemafy_bulk_maintenance_tools' ) ) {
                    do_action( 'schemafy_bulk_maintenance_tools' );
                } else {
                    schemafy_render_upgrade_card(
                        __( 'Bulk maintenance tools', 'schemafy' ),
                        array(
                            __( 'Fix Errors — repair invalid schemas in one pass', 'schemafy' ),
                            __( 'Clean Orphans — drop schemas whose page is gone', 'schemafy' ),
                            __( 'Remove Duplicates — keep one schema per URL', 'schemafy' ),
                            __( 'Clean Embedded Schemas — strip JSON-LD out of post content', 'schemafy' ),
                        )
                    );
                }
                ?>
            </div>

            <!-- Simplified Filters -->
            <div class="schemafy-filters-bar">
                    <!-- Search -->
                    <input
                        type="search"
                        id="schemafy-bulk-search"
                        class="schemafy-filter-input schemafy-filter-input-search"
                        placeholder="<?php esc_html_e('Search schemas...', 'schemafy'); ?>"
                    >

                    <!-- Type Filter - Dynamically populated from database -->
                    <select id="schemafy-bulk-filter-type" class="schemafy-filter-select">
                        <option value=""><?php esc_html_e('All Types', 'schemafy'); ?></option>
                        <?php
                        // Get unique schema types from database ($schemafy_database is passed from admin_page method)
                        if (isset($schemafy_database) && method_exists($schemafy_database, 'get_all_schema_types')) {
                            $schema_types = $schemafy_database->get_all_schema_types();
                            if (!empty($schema_types)) {
                                foreach ($schema_types as $type) {
                                    if (!empty($type)) {
                                        echo '<option value="' . esc_attr($type) . '">' . esc_html($type) . '</option>';
                                    }
                                }
                            }
                        }
                        ?>
                    </select>

                    <!-- Status Filter -->
                    <select id="schemafy-bulk-filter-status" class="schemafy-filter-select select-sm">
                        <option value=""><?php esc_html_e('All Status', 'schemafy'); ?></option>
                        <option value="1"><?php esc_html_e('Active', 'schemafy'); ?></option>
                        <option value="0"><?php esc_html_e('Inactive', 'schemafy'); ?></option>
                    </select>

                    <!-- More Filters Toggle -->
                    <button id="schemafy-toggle-more-filters" class="schemafy-filter-input" style="cursor: pointer; display: flex; align-items: center; gap: 6px; color: var(--schemafy-gray-600);">
                        <span class="dashicons dashicons-filter"></span>
                        <?php esc_html_e('More', 'schemafy'); ?>
                    </button>

                    <!-- Per Page (compact) -->
                    <select id="schemafy-bulk-per-page" class="schemafy-filter-select select-sm">
                        <option value="10">10</option>
                        <option value="25" selected>25</option>
                        <option value="50">50</option>
                        <option value="100">100</option>
                    </select>

                <!-- Extended Filters (Hidden by default) -->
                <div id="schemafy-extended-filters" style="display: none;" class="schemafy-filters-extended">
                    <!-- Date Filter -->
                    <select id="schemafy-bulk-filter-date" class="schemafy-filter-select">
                        <option value=""><?php esc_html_e('Any Date', 'schemafy'); ?></option>
                        <option value="today"><?php esc_html_e('Today', 'schemafy'); ?></option>
                        <option value="week"><?php esc_html_e('Last 7 days', 'schemafy'); ?></option>
                        <option value="month"><?php esc_html_e('Last 30 days', 'schemafy'); ?></option>
                        <option value="year"><?php esc_html_e('Last year', 'schemafy'); ?></option>
                    </select>

                    <!-- Sort -->
                    <select id="schemafy-bulk-sort" class="schemafy-filter-select">
                        <option value="date_desc"><?php esc_html_e('Newest first', 'schemafy'); ?></option>
                        <option value="date_asc"><?php esc_html_e('Oldest first', 'schemafy'); ?></option>
                        <option value="name_asc"><?php esc_html_e('Name A-Z', 'schemafy'); ?></option>
                        <option value="name_desc"><?php esc_html_e('Name Z-A', 'schemafy'); ?></option>
                    </select>

                    <!-- Clear Filters -->
                    <button id="schemafy-bulk-clear-filters" class="schemafy-btn-secondary">
                        <?php esc_html_e('Clear filters', 'schemafy'); ?>
                    </button>
                </div>

                <!-- Active Filters (hidden element for compatibility) -->
                <div id="schemafy-active-filters" style="display: none;">
                    <div id="schemafy-active-filters-tags"></div>
                </div>
            </div>

            <div class="schemafy-card-body-v2" style="padding: 0;">
                <!-- Hidden stat cards for compatibility -->
                <div id="schemafy-bulk-stats" style="display: none;"></div>

                <!-- Type Distribution Chart -->
                <div id="schemafy-type-distribution" class="schemafy-type-distribution" style="display: none;">
                    <h4 class="schemafy-type-distribution-title">
                        <span class="dashicons dashicons-chart-bar"></span>
                        Schema Type Distribution
                    </h4>
                    <div id="schemafy-type-bars" class="schemafy-type-bars"></div>
                </div>

                <!-- Results Summary -->
                <div id="schemafy-bulk-results-summary" class="schemafy-results-summary">
                    <span id="schemafy-results-count"></span>
                    <span id="schemafy-results-range"></span>
                </div>

                <div id="schemafy-bulk-table-container">
                    <div class="schemafy-loading-v2">
                        <div class="schemafy-spinner-v2"></div>
                        <p><?php esc_html_e('Loading schemas...', 'schemafy'); ?></p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Tab: Sitemap -->
    <div id="schemafy-tab-sitemap" class="schemafy-tab-content-v2<?php echo esc_attr( schemafy_is_active_tab('sitemap', $active_tab) ); ?>">
        <?php
        $schemafy_sitemap_data = class_exists('SCHEMAFY_Sitemap') ? SCHEMAFY_Sitemap::get_admin_data() : array();
        $schemafy_sitemap_settings = isset($schemafy_sitemap_data['settings']) ? $schemafy_sitemap_data['settings'] : array();
        $schemafy_sitemap_enabled = !empty($schemafy_sitemap_data['enabled']);
        $schemafy_sitemap_post_types = isset($schemafy_sitemap_data['post_types']) ? $schemafy_sitemap_data['post_types'] : array();
        $schemafy_sitemap_taxonomies = isset($schemafy_sitemap_data['taxonomies']) ? $schemafy_sitemap_data['taxonomies'] : array();
        $schemafy_sitemap_totals = isset($schemafy_sitemap_data['totals']) ? $schemafy_sitemap_data['totals'] : array('urls' => 0, 'sitemaps' => 0, 'sources' => 0);
        $schemafy_sitemap_conflicts = isset($schemafy_sitemap_data['conflicts']) ? $schemafy_sitemap_data['conflicts'] : array();
        $schemafy_sitemap_filesystem = isset($schemafy_sitemap_data['filesystem']) ? $schemafy_sitemap_data['filesystem'] : array();
        $schemafy_sitemap_selected_post_types = isset($schemafy_sitemap_settings['post_types']) ? (array) $schemafy_sitemap_settings['post_types'] : array();
        $schemafy_sitemap_selected_taxonomies = isset($schemafy_sitemap_settings['taxonomies']) ? (array) $schemafy_sitemap_settings['taxonomies'] : array();
        ?><div class="schemafy-sitemap-shell" data-sitemap-enabled="<?php echo esc_attr( $schemafy_sitemap_enabled ? '1' : '0' ); ?>">
            <div class="schemafy-sitemap-hero">
                <div class="schemafy-sitemap-hero-main">
                    <span class="schemafy-sitemap-eyebrow">
                        <span class="dashicons dashicons-networking"></span>
                        <?php esc_html_e('XML Sitemap', 'schemafy'); ?>
                    </span>
                    <div class="schemafy-sitemap-title-row">
                        <div>
                            <h2 class="schemafy-sitemap-title"><?php esc_html_e('Schemafy Sitemap', 'schemafy'); ?></h2>
                            <p class="schemafy-sitemap-subtitle">
                                <?php esc_html_e('A clean sitemap index generated from your public WordPress content, with duplicate sitemap endpoints consolidated into Schemafy.', 'schemafy'); ?>
                            </p>
                        </div>
                    </div>
                    <div class="schemafy-sitemap-urlbar">
                        <span class="dashicons dashicons-admin-links"></span>
                        <span class="schemafy-sitemap-url" id="schemafy-sitemap-index-url"><?php echo esc_html(isset($schemafy_sitemap_data['index_url']) ? $schemafy_sitemap_data['index_url'] : home_url('/sitemap-index.xml')); ?></span>
                        <button type="button" class="schemafy-sitemap-icon-btn" id="schemafy-copy-sitemap-url" title="<?php esc_attr_e('Copy URL', 'schemafy'); ?>">
                            <span class="dashicons dashicons-clipboard"></span>
                        </button>
                        <a class="schemafy-sitemap-icon-btn" id="schemafy-open-sitemap-url" href="<?php echo esc_url(isset($schemafy_sitemap_data['index_url']) ? $schemafy_sitemap_data['index_url'] : home_url('/sitemap-index.xml')); ?>" target="_blank" rel="noopener noreferrer" title="<?php esc_attr_e('Open sitemap', 'schemafy'); ?>">
                            <span class="dashicons dashicons-external"></span>
                        </a>
                    </div>
                    <div class="schemafy-sitemap-stats">
                        <div class="schemafy-sitemap-stat">
                            <span class="schemafy-sitemap-stat-value" id="schemafy-sitemap-total-urls"><?php echo esc_html(number_format_i18n((int) $schemafy_sitemap_totals['urls'])); ?></span>
                            <span class="schemafy-sitemap-stat-label"><?php esc_html_e('URLs', 'schemafy'); ?></span>
                        </div>
                        <div class="schemafy-sitemap-stat">
                            <span class="schemafy-sitemap-stat-value" id="schemafy-sitemap-total-files"><?php echo esc_html(number_format_i18n((int) $schemafy_sitemap_totals['sitemaps'])); ?></span>
                            <span class="schemafy-sitemap-stat-label"><?php esc_html_e('Sitemaps', 'schemafy'); ?></span>
                        </div>
                        <div class="schemafy-sitemap-stat">
                            <span class="schemafy-sitemap-stat-value" id="schemafy-sitemap-total-sources"><?php echo esc_html(number_format_i18n((int) $schemafy_sitemap_totals['sources'])); ?></span>
                            <span class="schemafy-sitemap-stat-label"><?php esc_html_e('Sources', 'schemafy'); ?></span>
                        </div>
                    </div>
                </div>
                <div class="schemafy-sitemap-side">
                    <div class="schemafy-sitemap-status-card">
                        <div class="schemafy-sitemap-switch-head">
                            <div>
                                <div class="schemafy-sitemap-switch-title"><?php esc_html_e('Sitemap status', 'schemafy'); ?></div>
                                <span class="schemafy-sitemap-state <?php echo esc_attr( $schemafy_sitemap_enabled ? 'is-on' : 'is-off' ); ?>" id="schemafy-sitemap-state">
                                    <span class="schemafy-sitemap-state-dot"></span>
                                    <span id="schemafy-sitemap-state-text"><?php echo $schemafy_sitemap_enabled ? esc_html__('Active', 'schemafy') : esc_html__('Inactive', 'schemafy'); ?></span>
                                </span>
                            </div>
                        </div>
                        <button
                            type="button"
                            id="schemafy-sitemap-power"
                            class="schemafy-sitemap-power <?php echo esc_attr( $schemafy_sitemap_enabled ? 'is-on' : 'is-off' ); ?>"
                            aria-pressed="<?php echo esc_attr( $schemafy_sitemap_enabled ? 'true' : 'false' ); ?>"
                            aria-busy="false"
                            data-enabled="<?php echo esc_attr( $schemafy_sitemap_enabled ? '1' : '0' ); ?>"
                        >
                            <span class="schemafy-sitemap-power-icon">
                                <span class="dashicons <?php echo esc_attr( $schemafy_sitemap_enabled ? 'dashicons-controls-pause' : 'dashicons-controls-play' ); ?>"></span>
                            </span>
                            <span class="schemafy-sitemap-power-copy">
                                <span id="schemafy-sitemap-power-label"><?php echo $schemafy_sitemap_enabled ? esc_html__('Deactivate sitemap', 'schemafy') : esc_html__('Activate sitemap', 'schemafy'); ?></span>
                                <span id="schemafy-sitemap-power-meta"><?php esc_html_e('Autosaves immediately', 'schemafy'); ?></span>
                            </span>
                            <span class="schemafy-sitemap-power-track" aria-hidden="true"><span></span></span>
                        </button>
                        <div class="schemafy-sitemap-save-indicator is-saved" id="schemafy-sitemap-save-indicator" aria-live="polite">
                            <span class="dashicons dashicons-saved"></span>
                            <span id="schemafy-sitemap-save-text"><?php esc_html_e('Ready', 'schemafy'); ?></span>
                        </div>
                    </div>
                </div>
            </div>

            <?php if (empty($schemafy_sitemap_data['search_visibility'])) : ?>
                <div class="schemafy-sitemap-alert warn">
                    <span class="dashicons dashicons-warning"></span>
                    <span><?php esc_html_e('WordPress search engine visibility is disabled. Enable site indexing in Reading Settings before submitting the sitemap.', 'schemafy'); ?></span>
                </div>
            <?php endif; ?>

            <?php if (!empty($schemafy_sitemap_filesystem)) : ?>
                <?php foreach ($schemafy_sitemap_filesystem as $warning) : ?>
                    <div class="schemafy-sitemap-alert warn">
                        <span class="dashicons dashicons-warning"></span>
                        <span><?php echo esc_html($warning); ?></span>
                    </div>
                <?php endforeach; ?>
            <?php endif; ?>

            <div class="schemafy-sitemap-grid">
                <div class="schemafy-sitemap-panel">
                    <div class="schemafy-sitemap-panel-head">
                        <h3 class="schemafy-sitemap-panel-title">
                            <span class="dashicons dashicons-list-view"></span>
                            <?php esc_html_e('Content Sources', 'schemafy'); ?>
                        </h3>
                    </div>
                    <div class="schemafy-sitemap-panel-body">
                        <p class="schemafy-sitemap-section-label"><?php esc_html_e('Post Types', 'schemafy'); ?></p>
                        <div class="schemafy-sitemap-source-list">
                            <?php foreach ($schemafy_sitemap_post_types as $slug => $item) : ?>
                                <?php $schemafy_source_selected = in_array($slug, $schemafy_sitemap_selected_post_types, true); ?>
                                <label class="schemafy-sitemap-source-row <?php echo esc_attr( $schemafy_source_selected ? 'is-selected' : '' ); ?>">
                                    <span class="schemafy-sitemap-source-toggle">
                                        <input type="checkbox" class="schemafy-sitemap-check schemafy-sitemap-post-type" value="<?php echo esc_attr($slug); ?>" <?php checked($schemafy_source_selected); ?>>
                                        <span class="schemafy-sitemap-source-toggle-ui" aria-hidden="true"><span class="dashicons dashicons-saved"></span></span>
                                    </span>
                                    <span>
                                        <span class="schemafy-sitemap-source-name"><?php echo esc_html($item['label']); ?></span>
                                        <span class="schemafy-sitemap-source-slug"><?php echo esc_html($slug); ?>-sitemap.xml</span>
                                    </span>
                                    <span class="schemafy-sitemap-source-count"><?php echo esc_html(number_format_i18n((int) $item['count'])); ?></span>
                                    <span class="schemafy-sitemap-source-kind"><?php esc_html_e('Type', 'schemafy'); ?></span>
                                </label>
                            <?php endforeach; ?>
                        </div>

                        <?php if (!empty($schemafy_sitemap_taxonomies)) : ?>
                            <p class="schemafy-sitemap-section-label"><?php esc_html_e('Taxonomies', 'schemafy'); ?></p>
                            <div class="schemafy-sitemap-source-list">
                                <?php foreach ($schemafy_sitemap_taxonomies as $slug => $item) : ?>
                                    <?php $schemafy_source_selected = in_array($slug, $schemafy_sitemap_selected_taxonomies, true); ?>
                                    <label class="schemafy-sitemap-source-row <?php echo esc_attr( $schemafy_source_selected ? 'is-selected' : '' ); ?>">
                                        <span class="schemafy-sitemap-source-toggle">
                                            <input type="checkbox" class="schemafy-sitemap-check schemafy-sitemap-taxonomy" value="<?php echo esc_attr($slug); ?>" <?php checked($schemafy_source_selected); ?>>
                                            <span class="schemafy-sitemap-source-toggle-ui" aria-hidden="true"><span class="dashicons dashicons-saved"></span></span>
                                        </span>
                                        <span>
                                            <span class="schemafy-sitemap-source-name"><?php echo esc_html($item['label']); ?></span>
                                            <span class="schemafy-sitemap-source-slug"><?php echo esc_html($slug); ?>-sitemap.xml</span>
                                        </span>
                                        <span class="schemafy-sitemap-source-count"><?php echo esc_html(number_format_i18n((int) $item['count'])); ?></span>
                                        <span class="schemafy-sitemap-source-kind"><?php esc_html_e('Tax', 'schemafy'); ?></span>
                                    </label>
                                <?php endforeach; ?>
                            </div>
                        <?php endif; ?>

                        <div class="schemafy-sitemap-advanced">
                            <div class="schemafy-sitemap-field">
                                <label for="schemafy-sitemap-per-page"><?php esc_html_e('URLs per sitemap', 'schemafy'); ?></label>
                                <input type="number" id="schemafy-sitemap-per-page" min="100" max="50000" step="100" value="<?php echo esc_attr(isset($schemafy_sitemap_settings['per_page']) ? (int) $schemafy_sitemap_settings['per_page'] : 1000); ?>">
                            </div>
                            <div class="schemafy-sitemap-inline-toggle">
                                <span><?php esc_html_e('Last modified dates', 'schemafy'); ?></span>
                                <label class="schemafy-toggle-switch">
                                    <input type="checkbox" id="schemafy-sitemap-lastmod" <?php checked(!empty($schemafy_sitemap_settings['include_lastmod'])); ?>>
                                    <span class="schemafy-toggle-slider"></span>
                                </label>
                            </div>
                            <div class="schemafy-sitemap-inline-toggle">
                                <span><?php esc_html_e('Consolidate duplicate sitemap URLs', 'schemafy'); ?></span>
                                <label class="schemafy-toggle-switch">
                                    <input type="checkbox" id="schemafy-sitemap-redirect-legacy" <?php checked(!empty($schemafy_sitemap_settings['redirect_legacy'])); ?>>
                                    <span class="schemafy-toggle-slider"></span>
                                </label>
                            </div>
                        </div>
                    </div>
                </div>

                <div class="schemafy-sitemap-panel">
                    <div class="schemafy-sitemap-panel-head">
                        <h3 class="schemafy-sitemap-panel-title">
                            <span class="dashicons dashicons-shield-alt"></span>
                            <?php esc_html_e('Conflict Control', 'schemafy'); ?>
                        </h3>
                        <button type="button" class="schemafy-sitemap-icon-btn" id="schemafy-refresh-sitemap-status" title="<?php esc_attr_e('Refresh status', 'schemafy'); ?>">
                            <span class="dashicons dashicons-image-rotate"></span>
                        </button>
                    </div>
                    <div class="schemafy-sitemap-panel-body">
                        <div class="schemafy-sitemap-conflict-list" id="schemafy-sitemap-conflicts">
                            <?php
                            $schemafy_active_conflicts = array_filter($schemafy_sitemap_conflicts, function($plugin) {
                                return !empty($plugin['active']);
                            });
                            ?>
                            <?php if (empty($schemafy_active_conflicts)) : ?>
                                <div class="schemafy-sitemap-conflict">
                                    <div>
                                        <div class="schemafy-sitemap-conflict-name"><?php esc_html_e('No duplicate sitemap generators detected', 'schemafy'); ?></div>
                                        <div class="schemafy-sitemap-conflict-mode"><?php esc_html_e('Schemafy will still protect the sitemap index URL when active.', 'schemafy'); ?></div>
                                    </div>
                                    <span class="schemafy-sitemap-pill clear"><?php esc_html_e('Clean', 'schemafy'); ?></span>
                                </div>
                            <?php else : ?>
                                <?php foreach ($schemafy_active_conflicts as $plugin) : ?>
                                    <div class="schemafy-sitemap-conflict">
                                        <div>
                                            <div class="schemafy-sitemap-conflict-name"><?php echo esc_html($plugin['name']); ?></div>
                                            <div class="schemafy-sitemap-conflict-mode"><?php echo esc_html($plugin['mode']); ?></div>
                                        </div>
                                        <span class="schemafy-sitemap-pill detected"><?php esc_html_e('Detected', 'schemafy'); ?></span>
                                    </div>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Tab: Meta Tags Manager -->
    <div id="schemafy-tab-metadata" class="schemafy-tab-content-v2<?php echo esc_attr( schemafy_is_active_tab('metadata', $active_tab) ); ?>"><div class="schemafy-card-v2" style="overflow:hidden;">
            <!-- Dark header -->
            <div class="schemafy-card-header-v2">
                <h2>
                    <span class="dashicons dashicons-tag"></span>
                    <?php esc_html_e('Meta Tags Manager', 'schemafy'); ?>
                </h2>
                <div style="display:flex;align-items:center;gap:12px;">
                    <button id="schemafy-mtm-bulk-toggle" class="schemafy-btn-secondary" style="flex-shrink:0;white-space:nowrap;">
                        <span class="dashicons dashicons-upload"></span>
                        <?php esc_html_e('Bulk Import CSV', 'schemafy'); ?>
                    </button>
                </div>
            </div>

            <?php $schemafy_meta_owner = SCHEMAFY_Meta_Store::owner_name(); ?>
            <?php if ( '' !== $schemafy_meta_owner ) : ?>
                <div class="schemafy-mtm-owner-note" style="display:flex;align-items:flex-start;gap:8px;padding:12px 20px;background:#f0f6fc;border-bottom:1px solid #d0e3f5;font-size:13px;color:#1e3a5f;">
                    <span class="dashicons dashicons-info-outline" aria-hidden="true" style="flex-shrink:0;"></span>
                    <span>
                        <?php
                        printf(
                            /* translators: %s is the name of the active SEO plugin, e.g. "Rank Math". */
                            esc_html__( 'These fields are shared with %s. Editing them here updates the very same values that plugin stores, so whichever screen you use, both stay in sync.', 'schemafy' ),
                            '<strong>' . esc_html( $schemafy_meta_owner ) . '</strong>'
                        );
                        ?>
                    </span>
                </div>
            <?php endif; ?>

            <!-- Bulk Import Panel (hidden by default) -->
            <div id="schemafy-mtm-bulk-panel" style="display:none;border-bottom:1px solid #e4e4e7;"><div class="schemafy-mtm-bulk">
                    <!-- Header info + template download -->
                    <div class="schemafy-mtm-bulk-top">
                        <div class="schemafy-mtm-bulk-info">
                            <h4><span class="dashicons dashicons-media-spreadsheet"></span> <?php esc_html_e('CSV Bulk Import', 'schemafy'); ?></h4>
                            <p><?php esc_html_e('Upload a CSV file to set meta tags for multiple pages at once. Required columns:', 'schemafy'); ?><br>
                            <code>url</code> &nbsp;·&nbsp; <code>meta_title</code> &nbsp;·&nbsp; <code>meta_description</code><br>
                            <?php esc_html_e('Recommended: title 50-60 chars · description 120-160 chars · hard limit: 200/500 chars · delimiter: comma or semicolon · encoding: UTF-8', 'schemafy'); ?></p>
                        </div>
                        <div class="schemafy-mtm-bulk-actions">
                            <button type="button" id="schemafy-mtm-download-template" class="schemafy-btn-secondary">
                                <span class="dashicons dashicons-download"></span>
                                <?php esc_html_e('Download Template', 'schemafy'); ?>
                            </button>
                        </div>
                    </div>

                    <!-- Drop zone -->
                    <div class="schemafy-mtm-dropzone" id="schemafy-mtm-dropzone">
                        <input type="file" id="schemafy-mtm-csv-file" accept=".csv,text/csv">
                        <div class="schemafy-mtm-dropzone-icon">📂</div>
                        <p><?php esc_html_e('Drop your CSV file here, or click to browse', 'schemafy'); ?></p>
                        <small><?php esc_html_e('Only .csv files · Max 5 MB · UTF-8 encoding recommended', 'schemafy'); ?></small>
                    </div>

                    <!-- Validation results (hidden until file loaded) -->
                    <div id="schemafy-mtm-val-results" style="display:none;">
                        <div id="schemafy-mtm-val-summary" class="schemafy-mtm-val-summary"></div>
                        <div class="schemafy-mtm-val-wrap">
                            <table class="schemafy-mtm-val-table">
                                <thead>
                                    <tr>
                                        <th>#</th>
                                        <th><?php esc_html_e('URL', 'schemafy'); ?></th>
                                        <th><?php esc_html_e('Meta Title', 'schemafy'); ?></th>
                                        <th><?php esc_html_e('Meta Description', 'schemafy'); ?></th>
                                        <th><?php esc_html_e('Issues', 'schemafy'); ?></th>
                                        <th><?php esc_html_e('Status', 'schemafy'); ?></th>
                                    </tr>
                                </thead>
                                <tbody id="schemafy-mtm-val-tbody"></tbody>
                            </table>
                        </div>
                        <div class="schemafy-mtm-import-footer">
                            <span class="schemafy-mtm-import-note" id="schemafy-mtm-import-note"></span>
                            <div style="display:flex;gap:8px;">
                                <button type="button" id="schemafy-mtm-clear-csv" class="schemafy-btn-secondary"><?php esc_html_e('Clear', 'schemafy'); ?></button>
                                <button type="button" id="schemafy-mtm-import-btn" class="schemafy-btn-primary" disabled>
                                    <span class="dashicons dashicons-yes-alt"></span>
                                    <span id="schemafy-mtm-import-btn-label"><?php esc_html_e('Import', 'schemafy'); ?></span>
                                </button>
                            </div>
                        </div>
                        <div id="schemafy-mtm-import-result" class="schemafy-mtm-import-result"></div>
                    </div>
                </div>
            </div>

            <!-- Split layout -->
            <div class="schemafy-mtm-layout">

                <!-- ── LEFT: URL list panel ── -->
                <div class="schemafy-mtm-left">
                    <div class="schemafy-mtm-left-hd">
                        <div class="schemafy-mtm-left-label"><?php esc_html_e('Pages & Posts', 'schemafy'); ?></div>
                        <div class="schemafy-mtm-search-wrap">
                            <input type="text" id="schemafy-meta-page-search" placeholder="<?php esc_html_e('Search pages…', 'schemafy'); ?>" autocomplete="off">
                        </div>
                    </div>
                    <!-- Native select hidden — JS still uses it internally -->
                    <select id="schemafy-meta-page-select" style="display:none;" size="8"></select>
                    <!-- Custom rendered list populated by JS -->
                    <div id="schemafy-mtm-url-list" class="schemafy-mtm-url-list">
                        <div style="padding:24px 16px;text-align:center;">
                            <div class="schemafy-spinner-v2" style="margin:0 auto 10px;"></div>
                            <p style="color:#a1a1aa;font-size:12px;margin:0;"><?php esc_html_e('Loading pages…', 'schemafy'); ?></p>
                        </div>
                    </div>
                </div>

                <!-- ── RIGHT: empty state / editor ── -->
                <div class="schemafy-mtm-right">

                    <!-- Empty state (shown until a page is clicked) -->
                    <div id="schemafy-meta-empty-state" class="schemafy-mtm-empty">
                        <div class="schemafy-mtm-empty-icon">🔍</div>
                        <h3><?php esc_html_e('Pick a page to get started', 'schemafy'); ?></h3>
                        <p><?php esc_html_e('Select any page or post from the list on the left to customise how it appears in Google search results.', 'schemafy'); ?></p>
                        <div class="schemafy-mtm-tips">
                            <div class="schemafy-mtm-tip">
                                <span class="schemafy-mtm-tip-icon">📋</span>
                                <span><?php esc_html_e('Meta tags tell Google what your page is about', 'schemafy'); ?></span>
                            </div>
                            <div class="schemafy-mtm-tip">
                                <span class="schemafy-mtm-tip-icon">✏️</span>
                                <span><?php esc_html_e('Override your SEO plugin values per page', 'schemafy'); ?></span>
                            </div>
                            <div class="schemafy-mtm-tip">
                                <span class="schemafy-mtm-tip-icon">👀</span>
                                <span><?php esc_html_e('See a live Google preview before saving', 'schemafy'); ?></span>
                            </div>
                        </div>
                    </div>

                    <!-- Editor (hidden until page selected) -->
                    <div id="schemafy-meta-editor" style="display:none;">

                        <!-- URL info bar -->
                        <div class="schemafy-mtm-url-bar">
                            <span class="dashicons dashicons-admin-links"></span>
                            <span id="schemafy-meta-sidebar-url" class="schemafy-mtm-url-text">—</span>
                            <a id="schemafy-meta-sidebar-visit" href="#" target="_blank" rel="noopener noreferrer" class="schemafy-mtm-visit-btn">
                                <?php esc_html_e('Visit', 'schemafy'); ?> <span class="dashicons dashicons-external"></span>
                            </a>
                        </div>

                        <!-- Unified meta tags editor -->
                        <form id="schemafy-meta-form">
                            <input type="hidden" id="schemafy-meta-page-url"     name="page_url">
                            <input type="hidden" id="schemafy-meta-visit-page"   name="visit_page_url">
                            <input type="hidden" id="schemafy-meta-current-url"  name="current_url">

                            <div class="schemafy-mtm-section">
                                <div class="schemafy-mtm-section-hd">
                                    <span class="schemafy-mtm-section-title">
                                        <span class="dashicons dashicons-edit-large"></span>
                                        <?php esc_html_e('Meta Tags', 'schemafy'); ?>
                                    </span>
                                    <div class="schemafy-seo-plugin-badge-v2" id="schemafy-seo-plugin-detected">
                                        <span class="dashicons dashicons-admin-plugins"></span>
                                        <span id="schemafy-seo-plugin-name"><?php esc_html_e('Detecting…', 'schemafy'); ?></span>
                                    </div>
                                </div>

                                <!-- Title -->
                                <div class="schemafy-mtm-field">
                                    <div class="schemafy-mtm-field-row">
                                        <label for="schemafy-meta-custom-title"><?php esc_html_e('Meta Title', 'schemafy'); ?></label>
                                        <span class="schemafy-char-counter-v2"><span id="schemafy-meta-title-count">0</span>/200</span>
                                    </div>
                                    <input type="text" id="schemafy-meta-custom-title" name="custom_meta_title" class="schemafy-input-v2" maxlength="200"
                                        placeholder="<?php esc_html_e('e.g. Best WordPress SEO Plugin | Your Site', 'schemafy'); ?>">
                                    <div class="schemafy-mtm-active-val">
                                        <span class="schemafy-mtm-active-label"><?php esc_html_e('Active', 'schemafy'); ?></span>
                                        <span id="schemafy-current-meta-title"><span style="color:#a1a1aa;"><?php esc_html_e('Loading…', 'schemafy'); ?></span></span>
                                    </div>
                                    <div class="schemafy-mtm-field-foot">
                                        <div class="schemafy-seo-indicator-v2" id="schemafy-meta-title-indicator">
                                            <span class="schemafy-indicator-icon"><span class="dashicons dashicons-info"></span></span>
                                            <span class="schemafy-seo-status"><?php esc_html_e('Empty — using SEO plugin value', 'schemafy'); ?></span>
                                        </div>
                                        <span class="schemafy-mtm-field-tip"><?php esc_html_e('Ideal: 50–60 chars', 'schemafy'); ?></span>
                                    </div>
                                </div>

                                <!-- Description -->
                                <div class="schemafy-mtm-field">
                                    <div class="schemafy-mtm-field-row">
                                        <label for="schemafy-meta-custom-desc"><?php esc_html_e('Meta Description', 'schemafy'); ?></label>
                                        <span class="schemafy-char-counter-v2"><span id="schemafy-meta-desc-count">0</span>/500</span>
                                    </div>
                                    <textarea id="schemafy-meta-custom-desc" name="custom_meta_description" class="schemafy-textarea-v2" rows="3" maxlength="500"
                                        placeholder="<?php esc_html_e('e.g. Improve your WordPress SEO with structured data. Easy schema markup for better rankings.', 'schemafy'); ?>"></textarea>
                                    <div class="schemafy-mtm-active-val">
                                        <span class="schemafy-mtm-active-label"><?php esc_html_e('Active', 'schemafy'); ?></span>
                                        <span id="schemafy-current-meta-desc"><span style="color:#a1a1aa;"><?php esc_html_e('Loading…', 'schemafy'); ?></span></span>
                                    </div>
                                    <div class="schemafy-mtm-field-foot">
                                        <div class="schemafy-seo-indicator-v2" id="schemafy-meta-desc-indicator">
                                            <span class="schemafy-indicator-icon"><span class="dashicons dashicons-info"></span></span>
                                            <span class="schemafy-seo-status"><?php esc_html_e('Empty — using SEO plugin value', 'schemafy'); ?></span>
                                        </div>
                                        <span class="schemafy-mtm-field-tip"><?php esc_html_e('Ideal: 120–160 chars', 'schemafy'); ?></span>
                                    </div>
                                </div>
                            </div>

                            <!-- Google SERP Preview -->
                            <div class="schemafy-mtm-section">
                                <div class="schemafy-mtm-preview-hd">
                                    <div class="schemafy-mtm-preview-hd-left">
                                        <?php esc_html_e('Google Preview', 'schemafy'); ?>
                                        <span class="schemafy-mtm-live-badge"><?php esc_html_e('Live', 'schemafy'); ?></span>
                                    </div>
                                    <div class="schemafy-mtm-preview-hd-right">
                                        <div class="schemafy-device-toggle">
                                            <button class="schemafy-device-btn active" data-device="desktop" type="button">
                                                <svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M20 3H4c-1.1 0-2 .9-2 2v11c0 1.1.9 2 2 2h6l-2 3v1h8v-1l-2-3h6c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zm0 13H4V5h16v11z"/></svg>
                                                Desktop
                                            </button>
                                            <button class="schemafy-device-btn" data-device="mobile" type="button">
                                                <svg width="12" height="12" viewBox="0 0 24 24" fill="currentColor" aria-hidden="true"><path d="M17 1.01L7 1c-1.1 0-2 .9-2 2v18c0 1.1.9 2 2 2h10c1.1 0 2-.9 2-1.99V3c0-1.1-.9-1.99-2-1.99zM17 19H7V5h10v14z"/></svg>
                                                Mobile
                                            </button>
                                        </div>
                                    </div>
                                </div>

                                <!-- Desktop SERP -->
                                <div class="schemafy-serp-wrap schemafy-google-preview-container schemafy-device-desktop active">
                                    <!-- Search bar -->
                                    <div class="schemafy-serp-searchbar">
                                        <span class="schemafy-serp-glogo" aria-hidden="true"><span style="color:#4285F4">G</span><span style="color:#DB4437">o</span><span style="color:#F4B400">o</span><span style="color:#4285F4">g</span><span style="color:#0F9D58">l</span><span style="color:#DB4437">e</span></span>
                                        <div class="schemafy-serp-input-field">
                                            <span class="schemafy-serp-query" id="schemafy-serp-query-desktop"></span>
                                            <svg class="schemafy-serp-search-svg" xmlns="http://www.w3.org/2000/svg" width="18" height="18" viewBox="0 0 24 24"><path fill="#4285F4" d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/></svg>
                                        </div>
                                    </div>
                                    <!-- Nav tabs -->
                                    <div class="schemafy-serp-navtabs">
                                        <span class="schemafy-serp-navtab active">All</span>
                                        <span class="schemafy-serp-navtab">Images</span>
                                        <span class="schemafy-serp-navtab">News</span>
                                        <span class="schemafy-serp-navtab">Videos</span>
                                        <span class="schemafy-serp-navtab">Shopping</span>
                                    </div>
                                    <!-- Stats -->
                                    <div class="schemafy-serp-statsbar">About 1,240,000,000 results <span style="color:#70757a">(0.42 seconds)</span></div>
                                    <!-- Results area -->
                                    <div class="schemafy-serp-results-area">
                                        <!-- Your result -->
                                        <div class="schemafy-serp-result">
                                            <div class="schemafy-serp-result-inner">
                                                <div class="schemafy-serp-row-site">
                                                    <div class="schemafy-serp-fav-group">
                                                        <div class="schemafy-serp-favicon-circle" id="schemafy-serp-fav-circle-desktop">
                                                            <img class="schemafy-serp-fav-img" id="schemafy-serp-favicon-desktop" src="" alt="" width="18" height="18">
                                                            <svg class="schemafy-serp-fav-globe" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="18" height="18" fill="#5f6368"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-1 17.93c-3.95-.49-7-3.85-7-7.93 0-.62.08-1.21.21-1.79L9 15v1c0 1.1.9 2 2 2v1.93zm6.9-2.54c-.26-.81-1-1.39-1.9-1.39h-1v-3c0-.55-.45-1-1-1H8v-2h2c.55 0 1-.45 1-1V7h2c1.1 0 2-.9 2-2v-.41c2.93 1.19 5 4.06 5 7.41 0 2.08-.8 3.97-2.1 5.39z"/></svg>
                                                        </div>
                                                        <div class="schemafy-serp-site-info">
                                                            <span class="schemafy-serp-sitename" id="schemafy-serp-sitename-desktop">example.com</span>
                                                            <span class="schemafy-serp-breadcrumb" id="schemafy-google-preview-url-desktop">example.com › page</span>
                                                        </div>
                                                    </div>
                                                    <button class="schemafy-serp-more-btn" type="button" aria-label="More options" tabindex="-1">
                                                        <svg width="18" height="18" viewBox="0 0 24 24" fill="#5f6368"><path d="M12 8c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm0 2c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm0 6c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2z"/></svg>
                                                    </button>
                                                </div>
                                                <div class="schemafy-serp-title" id="schemafy-google-preview-title-desktop"><?php esc_html_e('Your Page Title Will Appear Here', 'schemafy'); ?></div>
                                                <div class="schemafy-serp-desc" id="schemafy-google-preview-desc-desktop"><?php esc_html_e('Your meta description will appear here. Make it compelling to improve click-through rates from search results.', 'schemafy'); ?></div>
                                            </div>
                                            <span class="schemafy-serp-your-badge"><?php esc_html_e('Your result', 'schemafy'); ?></span>
                                        </div>
                                        <!-- Ghost result 1 -->
                                        <div class="schemafy-serp-ghost" aria-hidden="true">
                                            <div class="schemafy-serp-ghost-fav"></div>
                                            <div class="schemafy-serp-ghost-title"></div>
                                            <div class="schemafy-serp-ghost-desc"></div>
                                            <div class="schemafy-serp-ghost-desc short"></div>
                                        </div>
                                        <!-- Ghost result 2 -->
                                        <div class="schemafy-serp-ghost" aria-hidden="true">
                                            <div class="schemafy-serp-ghost-fav"></div>
                                            <div class="schemafy-serp-ghost-title" style="width:55%"></div>
                                            <div class="schemafy-serp-ghost-desc"></div>
                                        </div>
                                    </div>
                                </div>

                                <!-- Mobile SERP -->
                                <div class="schemafy-serp-mobile-outer schemafy-google-preview-container schemafy-device-mobile">
                                    <div class="schemafy-serp-phone-frame">
                                        <!-- Mobile search bar -->
                                        <div class="schemafy-serp-mobile-bar">
                                            <span class="schemafy-serp-glogo-sm" aria-hidden="true"><span style="color:#4285F4">G</span><span style="color:#DB4437">o</span><span style="color:#F4B400">o</span><span style="color:#4285F4">g</span><span style="color:#0F9D58">l</span><span style="color:#DB4437">e</span></span>
                                            <div class="schemafy-serp-mobile-input">
                                                <span class="schemafy-serp-query" id="schemafy-serp-query-mobile"></span>
                                                <svg xmlns="http://www.w3.org/2000/svg" width="14" height="14" viewBox="0 0 24 24"><path fill="#4285F4" d="M15.5 14h-.79l-.28-.27C15.41 12.59 16 11.11 16 9.5 16 5.91 13.09 3 9.5 3S3 5.91 3 9.5 5.91 16 9.5 16c1.61 0 3.09-.59 4.23-1.57l.27.28v.79l5 4.99L20.49 19l-4.99-5zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"/></svg>
                                            </div>
                                        </div>
                                        <!-- Mobile tabs -->
                                        <div class="schemafy-serp-navtabs schemafy-serp-mobile-tabs">
                                            <span class="schemafy-serp-navtab active">All</span>
                                            <span class="schemafy-serp-navtab">Images</span>
                                            <span class="schemafy-serp-navtab">News</span>
                                        </div>
                                        <!-- Mobile result -->
                                        <div class="schemafy-serp-results-area">
                                            <div class="schemafy-serp-result">
                                                <div class="schemafy-serp-result-inner">
                                                    <div class="schemafy-serp-row-site">
                                                        <div class="schemafy-serp-fav-group">
                                                            <div class="schemafy-serp-favicon-circle" id="schemafy-serp-fav-circle-mobile">
                                                                <img class="schemafy-serp-fav-img" id="schemafy-serp-favicon-mobile" src="" alt="" width="18" height="18">
                                                                <svg class="schemafy-serp-fav-globe" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" width="18" height="18" fill="#5f6368"><path d="M12 2C6.48 2 2 6.48 2 12s4.48 10 10 10 10-4.48 10-10S17.52 2 12 2zm-1 17.93c-3.95-.49-7-3.85-7-7.93 0-.62.08-1.21.21-1.79L9 15v1c0 1.1.9 2 2 2v1.93zm6.9-2.54c-.26-.81-1-1.39-1.9-1.39h-1v-3c0-.55-.45-1-1-1H8v-2h2c.55 0 1-.45 1-1V7h2c1.1 0 2-.9 2-2v-.41c2.93 1.19 5 4.06 5 7.41 0 2.08-.8 3.97-2.1 5.39z"/></svg>
                                                            </div>
                                                            <div class="schemafy-serp-site-info">
                                                                <span class="schemafy-serp-sitename" id="schemafy-serp-sitename-mobile">example.com</span>
                                                                <span class="schemafy-serp-breadcrumb" id="schemafy-google-preview-url-mobile">example.com</span>
                                                            </div>
                                                        </div>
                                                        <button class="schemafy-serp-more-btn" type="button" aria-label="More options" tabindex="-1">
                                                            <svg width="16" height="16" viewBox="0 0 24 24" fill="#5f6368"><path d="M12 8c1.1 0 2-.9 2-2s-.9-2-2-2-2 .9-2 2 .9 2 2 2zm0 2c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2zm0 6c-1.1 0-2 .9-2 2s.9 2 2 2 2-.9 2-2-.9-2-2-2z"/></svg>
                                                        </button>
                                                    </div>
                                                    <div class="schemafy-serp-title" id="schemafy-google-preview-title-mobile"><?php esc_html_e('Your Page Title Will Appear Here', 'schemafy'); ?></div>
                                                    <div class="schemafy-serp-desc" id="schemafy-google-preview-desc-mobile"><?php esc_html_e('Your meta description will appear here. Make it compelling to improve click-through rates from search results.', 'schemafy'); ?></div>
                                                </div>
                                                <span class="schemafy-serp-your-badge"><?php esc_html_e('Your result', 'schemafy'); ?></span>
                                            </div>
                                            <!-- Ghost result -->
                                            <div class="schemafy-serp-ghost" aria-hidden="true">
                                                <div class="schemafy-serp-ghost-fav"></div>
                                                <div class="schemafy-serp-ghost-title"></div>
                                                <div class="schemafy-serp-ghost-desc"></div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>

                            <!-- Buttons -->
                            <div class="schemafy-mtm-actions">
                                <button type="button" id="schemafy-meta-clear" class="schemafy-btn-secondary schemafy-btn-lg">
                                    <span class="dashicons dashicons-dismiss"></span>
                                    <?php esc_html_e('Reset to Default', 'schemafy'); ?>
                                </button>
                                <button type="submit" class="schemafy-btn-primary schemafy-btn-lg">
                                    <span class="dashicons dashicons-saved"></span>
                                    <?php esc_html_e('Save Meta Tags', 'schemafy'); ?>
                                </button>
                            </div>
                        </form>
                    </div><!-- /#schemafy-meta-editor -->

                </div><!-- /.schemafy-mtm-right -->
            </div><!-- /.schemafy-mtm-layout -->
        </div><!-- /.schemafy-card-v2 -->

        <!-- Hidden divider kept for JS compat -->
        <div id="schemafy-meta-divider" style="display:none;"></div>
    </div>
    <!-- ==================== AUTO GENERATOR TAB ==================== -->
    <?php schemafy_render_premium_tab( 'auto-generator', $active_tab ); ?>
    <!-- End AutoGenerator Tab -->




        </div><!-- /.schemafy-content-area -->

        <!-- Footer -->
        <footer class="schemafy-footer-v2">
            <p>
                <strong>Schemafy</strong> - <?php esc_html_e('Made with', 'schemafy'); ?> ❤️ <?php esc_html_e('by', 'schemafy'); ?>
                <strong>Schemafy</strong> & <a href="https://agenciawebtek.com" target="_blank" rel="noopener noreferrer"><strong>WebTek</strong></a>
            </p>
        </footer>
    </main><!-- /.schemafy-main-content -->
</div><!-- /.schemafy-admin-wrap-v2 -->

<?php if ($show_onboarding) : ?>
<!-- Interactive Tour Overlay -->
<div id="schemafy-tour-overlay" class="schemafy-tour-overlay"></div>

<!-- Tour Tooltip -->
<div id="schemafy-tour-tooltip" class="schemafy-tour-tooltip">
    <div class="schemafy-tour-tooltip-arrow"></div>
    <div class="schemafy-tour-tooltip-header">
        <span class="schemafy-tour-step-badge">1/7</span>
        <button type="button" class="schemafy-tour-close" title="<?php esc_html_e('Close tour', 'schemafy'); ?>">
            <span class="dashicons dashicons-no-alt"></span>
        </button>
    </div>
    <div class="schemafy-tour-tooltip-content">
        <h4></h4>
        <p></p>
    </div>
    <div class="schemafy-tour-tooltip-footer">
        <label class="schemafy-tour-dont-show">
            <input type="checkbox" id="schemafy-tour-dont-show">
            <?php esc_html_e('Don\'t show again', 'schemafy'); ?>
        </label>
        <div class="schemafy-tour-buttons">
            <button type="button" class="schemafy-btn-secondary schemafy-tour-skip">
                <?php esc_html_e('Skip', 'schemafy'); ?>
            </button>
            <button type="button" class="schemafy-btn-secondary schemafy-tour-prev">
                <span class="dashicons dashicons-arrow-left-alt2"></span>
            </button>
            <button type="button" class="schemafy-btn-primary schemafy-tour-next">
                <?php esc_html_e('Next', 'schemafy'); ?>
                <span class="dashicons dashicons-arrow-right-alt2"></span>
            </button>
        </div>
    </div>
</div>

<!-- Welcome Modal (shown first) -->
<div id="schemafy-welcome-modal" class="schemafy-onboarding-overlay">
    <div class="schemafy-onboarding-modal" style="max-width: 520px;">
        <div class="schemafy-onboarding-header">
            <div class="schemafy-onboarding-logo">
                <span class="dashicons dashicons-editor-code"></span>
            </div>
            <h2><?php esc_html_e('Welcome to Schemafy!', 'schemafy'); ?></h2>
            <p style="font-size: 14px; line-height: 1.6; opacity: 0.9; margin-top: 12px;">
                <?php esc_html_e('Schemafy helps you add Schema Markup (structured data) to your website. This helps Google understand your content better and can show rich results in search (like star ratings, prices, and more).', 'schemafy'); ?>
            </p>
            <p style="font-size: 15px; margin-top: 16px;">
                <?php esc_html_e('Would you like a quick tour to learn how it works?', 'schemafy'); ?>
            </p>
        </div>
        <div class="schemafy-onboarding-nav">
            <div class="schemafy-onboarding-buttons schemafy-onboarding-buttons-center">
                <button type="button" class="schemafy-btn-secondary" id="schemafy-welcome-skip">
                    <?php esc_html_e('Skip for now', 'schemafy'); ?>
                </button>
                <button type="button" class="schemafy-btn-primary" id="schemafy-welcome-start">
                    <span class="dashicons dashicons-controls-play"></span>
                    <?php esc_html_e('Yes, show me!', 'schemafy'); ?>
                </button>
            </div>
        </div>
        <label class="schemafy-onboarding-dismiss">
            <input type="checkbox" id="schemafy-welcome-dont-show">
            <?php esc_html_e('Don\'t show this again', 'schemafy'); ?>
        </label>
    </div>
</div>
<?php endif; ?>
