<?php if ( ! defined( 'ABSPATH' ) ) exit; ?>
<div class="pbuaiv-setup-wrap" id="pbuaiv-onboarding">

    <!-- Header -->
    <div class="pbuaiv-setup-header">
        <div class="pbuaiv-setup-logo">
            <svg width="32" height="32" viewBox="0 0 32 32" fill="none"><rect width="32" height="32" rx="8" fill="#244b8e"/><path d="M8 10h12M8 14h12M8 18h8" stroke="#f59e0b" stroke-width="2" stroke-linecap="round"/><rect x="17" y="18" width="9" height="8" rx="1.5" fill="#f59e0b"/></svg>
            <span class="pbuaiv-setup-logo__name">UAE VAT Invoice <span style="color:#f59e0b">for WooCommerce</span></span>
        </div>
        <p class="pbuaiv-setup-tagline"><?php esc_html_e( 'FTA-compliant invoicing in 3 minutes', 'pb-invoicing-system' ); ?></p>
    </div>

    <!-- Step progress -->
    <div class="pbuaiv-setup-steps" id="pbuaiv-steps-bar">
        <?php
// phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared, PluginCheck.Security.DirectDB.UnescapedDBParameter, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.SlowDBQuery.slow_db_query_meta_key, WordPress.DB.DirectDatabaseQuery.SchemaChange
        $steps = [
            1 => [ 'icon' => '🏢', 'label' => __( 'Company', 'pb-invoicing-system' ) ],
            2 => [ 'icon' => '🪪', 'label' => __( 'VAT / TRN', 'pb-invoicing-system' ) ],
            3 => [ 'icon' => '🎨', 'label' => __( 'Branding', 'pb-invoicing-system' ) ],
            4 => [ 'icon' => '📋', 'label' => __( 'Documents', 'pb-invoicing-system' ) ],
            5 => [ 'icon' => '✅', 'label' => __( 'Done!', 'pb-invoicing-system' ) ],
        ];
        foreach ( $steps as $n => $step ) :
        ?>
            <div class="pbuaiv-setup-step <?php echo $n === 1 ? 'pbuaiv-setup-step--active' : ''; ?>" data-step="<?php echo (int) $n; ?>">
                <div class="pbuaiv-setup-step__icon"><?php echo esc_html( $step['icon'] ); ?></div>
                <div class="pbuaiv-setup-step__label"><?php echo esc_html( $step['label'] ); ?></div>
            </div>
            <?php if ( $n < 5 ) echo '<div class="pbuaiv-setup-step-divider"></div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static HTML ?>
        <?php endforeach; ?>
    </div>

    <!-- Steps content -->
    <div class="pbuaiv-setup-body">

        <!-- Step 1: Company details -->
        <div class="pbuaiv-setup-panel" id="pbuaiv-step-1">
            <h2><?php esc_html_e( 'Your Company Details', 'pb-invoicing-system' ); ?></h2>
            <p class="pbuaiv-setup-panel__sub"><?php esc_html_e( 'These appear on every document you issue.', 'pb-invoicing-system' ); ?></p>

            <div class="pbuaiv-form-grid">
                <div class="pbuaiv-field pbuaiv-field--full">
                    <label class="pbuaiv-label"><?php esc_html_e( 'Company / Trade Name *', 'pb-invoicing-system' ); ?></label>
                    <input type="text" class="pbuaiv-input" name="pbuaiv_company_name" value="<?php echo esc_attr( get_option('pbuaiv_company_name','') ); ?>" placeholder="<?php esc_attr_e( 'ABC Trading LLC', 'pb-invoicing-system' ); ?>" required />
                </div>
                <div class="pbuaiv-field pbuaiv-field--full">
                    <label class="pbuaiv-label"><?php esc_html_e( 'Address Line', 'pb-invoicing-system' ); ?></label>
                    <input type="text" class="pbuaiv-input" name="pbuaiv_company_address" value="<?php echo esc_attr( get_option('pbuaiv_company_address','') ); ?>" placeholder="<?php esc_attr_e( 'Unit 501, Business Bay Tower', 'pb-invoicing-system' ); ?>" />
                </div>
                <div class="pbuaiv-field">
                    <label class="pbuaiv-label"><?php esc_html_e( 'City', 'pb-invoicing-system' ); ?></label>
                    <input type="text" class="pbuaiv-input" name="pbuaiv_company_city" value="<?php echo esc_attr( get_option('pbuaiv_company_city','') ); ?>" placeholder="Dubai" />
                </div>
                <div class="pbuaiv-field">
                    <label class="pbuaiv-label"><?php esc_html_e( 'Emirate', 'pb-invoicing-system' ); ?></label>
                    <select class="pbuaiv-select" name="pbuaiv_company_emirate">
                        <?php
                        $emirates = [ 'Abu Dhabi', 'Dubai', 'Sharjah', 'Ajman', 'Umm Al Quwain', 'Ras Al Khaimah', 'Fujairah' ];
                        $current  = get_option( 'pbuaiv_company_emirate', 'Dubai' );
                        foreach ( $emirates as $em ) {
                            printf( '<option value="%s" %s>%s</option>', esc_attr($em), selected($current,$em,false), esc_html($em) );
                        }
                        ?>
                    </select>
                </div>
                <div class="pbuaiv-field">
                    <label class="pbuaiv-label"><?php esc_html_e( 'Phone', 'pb-invoicing-system' ); ?></label>
                    <input type="text" class="pbuaiv-input" name="pbuaiv_company_phone" value="<?php echo esc_attr( get_option('pbuaiv_company_phone','') ); ?>" placeholder="+971 4 000 0000" />
                </div>
                <div class="pbuaiv-field">
                    <label class="pbuaiv-label"><?php esc_html_e( 'Email', 'pb-invoicing-system' ); ?></label>
                    <input type="email" class="pbuaiv-input" name="pbuaiv_company_email" value="<?php echo esc_attr( get_option('pbuaiv_company_email','') ); ?>" />
                </div>
                <div class="pbuaiv-field pbuaiv-field--full">
                    <label class="pbuaiv-label"><?php esc_html_e( 'Website', 'pb-invoicing-system' ); ?></label>
                    <input type="url" class="pbuaiv-input" name="pbuaiv_company_website" value="<?php echo esc_attr( get_option('pbuaiv_company_website','') ); ?>" />
                </div>

                <!-- Logo upload -->
                <div class="pbuaiv-field pbuaiv-field--full">
                    <label class="pbuaiv-label"><?php esc_html_e( 'Company Logo', 'pb-invoicing-system' ); ?></label>
                    <div class="pbuaiv-logo-upload" id="pbuaiv-logo-wrap">
                        <?php
                        $logo_id  = (int) get_option( 'pbuaiv_logo_id', 0 );
                        $logo_url = $logo_id ? wp_get_attachment_image_url( $logo_id, 'medium' ) : '';
                        ?>
                        <?php if ( $logo_url ) : ?>
                            <img src="<?php echo esc_url($logo_url); ?>" class="pbuaiv-logo-preview" id="pbuaiv-logo-preview" />
                        <?php else : ?>
                            <div class="pbuaiv-logo-placeholder" id="pbuaiv-logo-placeholder">
                                <span class="dashicons dashicons-format-image"></span>
                                <span><?php esc_html_e( 'Click to upload logo', 'pb-invoicing-system' ); ?></span>
                            </div>
                        <?php endif; ?>
                    </div>
                    <input type="hidden" name="pbuaiv_logo_id" id="pbuaiv-logo-id" value="<?php echo esc_attr($logo_id); ?>" />
                    <button type="button" class="pbuaiv-btn pbuaiv-btn--ghost pbuaiv-btn--sm" id="pbuaiv-upload-logo" style="margin-top:8px;">
                        <?php esc_html_e( 'Upload / Change Logo', 'pb-invoicing-system' ); ?>
                    </button>
                    <?php if ( $logo_id ) : ?>
                        <button type="button" class="pbuaiv-btn pbuaiv-btn--danger pbuaiv-btn--sm" id="pbuaiv-remove-logo" style="margin-left:6px;">
                            <?php esc_html_e( 'Remove', 'pb-invoicing-system' ); ?>
                        </button>
                    <?php endif; ?>
                    <p class="pbuaiv-help"><?php esc_html_e( 'Recommended: PNG or SVG, transparent background, min 200×80px.', 'pb-invoicing-system' ); ?></p>
                </div>
            </div>

            <div class="pbuaiv-setup-nav">
                <span></span>
                <button type="button" class="pbuaiv-btn pbuaiv-btn--primary pbuaiv-next-step" data-next="2">
                    <?php esc_html_e( 'Next: VAT Details', 'pb-invoicing-system' ); ?> →
                </button>
            </div>
        </div>

        <!-- Step 2: VAT & TRN -->
        <div class="pbuaiv-setup-panel" id="pbuaiv-step-2" style="display:none;">
            <h2><?php esc_html_e( 'UAE VAT Registration', 'pb-invoicing-system' ); ?></h2>
            <p class="pbuaiv-setup-panel__sub"><?php esc_html_e( 'Your TRN appears on every tax invoice as required by the FTA.', 'pb-invoicing-system' ); ?></p>

            <div class="pbuaiv-fta-notice">
                <span class="pbuaiv-fta-notice__flag">🇦🇪</span>
                <div>
                    <strong><?php esc_html_e( 'FTA Requirement', 'pb-invoicing-system' ); ?></strong><br>
                    <?php esc_html_e( 'All VAT-registered businesses must display their 15-digit TRN on standard tax invoices.', 'pb-invoicing-system' ); ?>
                </div>
            </div>

            <div class="pbuaiv-form-grid">
                <div class="pbuaiv-field pbuaiv-field--full">
                    <label class="pbuaiv-label"><?php esc_html_e( 'Tax Registration Number (TRN) *', 'pb-invoicing-system' ); ?></label>
                    <input type="text" class="pbuaiv-input pbuaiv-trn-input" name="pbuaiv_trn" id="pbuaiv_trn"
                           value="<?php echo esc_attr( get_option('pbuaiv_trn','') ); ?>"
                           placeholder="100000000000001" maxlength="15" />
                    <div class="pbuaiv-trn-status" id="pbuaiv-trn-status"></div>
                    <p class="pbuaiv-help"><?php esc_html_e( '15 digits. Starts with 1 (individual) or 3 (company). Found on your FTA portal.', 'pb-invoicing-system' ); ?></p>
                </div>

                <div class="pbuaiv-field">
                    <label class="pbuaiv-label"><?php esc_html_e( 'VAT Rate', 'pb-invoicing-system' ); ?></label>
                    <select class="pbuaiv-select" name="pbuaiv_vat_rate">
                        <option value="5" <?php selected( get_option('pbuaiv_vat_rate','5'), '5' ); ?>><?php esc_html_e( '5% (Standard Rate)', 'pb-invoicing-system' ); ?></option>
                        <option value="0" <?php selected( get_option('pbuaiv_vat_rate','5'), '0' ); ?>><?php esc_html_e( '0% (Zero-Rated)', 'pb-invoicing-system' ); ?></option>
                    </select>
                </div>

                <div class="pbuaiv-field">
                    <label class="pbuaiv-label"><?php esc_html_e( 'Currency → AED Rate', 'pb-invoicing-system' ); ?></label>
                    <input type="text" class="pbuaiv-input" name="pbuaiv_currency_aed_rate"
                           value="<?php echo esc_attr( get_option('pbuaiv_currency_aed_rate','1') ); ?>"
                           placeholder="1" />
                    <p class="pbuaiv-help"><?php esc_html_e( 'If your store runs in a currency other than AED, enter the conversion rate. VAT must always be shown in AED (FTA rule).', 'pb-invoicing-system' ); ?></p>
                </div>

                <div class="pbuaiv-field pbuaiv-field--full">
                    <label class="pbuaiv-label"><?php esc_html_e( 'Invoice Language', 'pb-invoicing-system' ); ?></label>
                    <div class="pbuaiv-radio-group">
                        <?php
                        $langs = [
                            'en'   => __( 'English only', 'pb-invoicing-system' ),
                            'ar'   => __( 'Arabic only', 'pb-invoicing-system' ),
                            'both' => __( 'Bilingual (Arabic + English)', 'pb-invoicing-system' ),
                        ];
                        $cur = get_option( 'pbuaiv_doc_language', 'en' );
                        foreach ( $langs as $val => $label ) {
                            printf(
                                '<label class="pbuaiv-radio"><input type="radio" name="pbuaiv_doc_language" value="%s" %s /> %s</label>',
                                esc_attr($val), checked($cur,$val,false), esc_html($label)
                            );
                        }
                        ?>
                    </div>
                </div>
            </div>

            <!-- Bank details -->
            <h3><?php esc_html_e( 'Bank Details (optional)', 'pb-invoicing-system' ); ?></h3>
            <p class="pbuaiv-setup-panel__sub"><?php esc_html_e( 'Displayed on invoices to facilitate bank transfers.', 'pb-invoicing-system' ); ?></p>
            <div class="pbuaiv-form-grid">
                <div class="pbuaiv-field"><label class="pbuaiv-label"><?php esc_html_e( 'Bank Name', 'pb-invoicing-system' ); ?></label><input type="text" class="pbuaiv-input" name="pbuaiv_bank_name" value="<?php echo esc_attr(get_option('pbuaiv_bank_name','')); ?>" placeholder="Emirates NBD" /></div>
                <div class="pbuaiv-field"><label class="pbuaiv-label"><?php esc_html_e( 'IBAN', 'pb-invoicing-system' ); ?></label><input type="text" class="pbuaiv-input" name="pbuaiv_bank_iban" value="<?php echo esc_attr(get_option('pbuaiv_bank_iban','')); ?>" placeholder="AE070331234567890123456" /></div>
                <div class="pbuaiv-field"><label class="pbuaiv-label"><?php esc_html_e( 'SWIFT/BIC', 'pb-invoicing-system' ); ?></label><input type="text" class="pbuaiv-input" name="pbuaiv_bank_swift" value="<?php echo esc_attr(get_option('pbuaiv_bank_swift','')); ?>" placeholder="EBILAEAD" /></div>
                <div class="pbuaiv-field"><label class="pbuaiv-label"><?php esc_html_e( 'Account Number', 'pb-invoicing-system' ); ?></label><input type="text" class="pbuaiv-input" name="pbuaiv_bank_account" value="<?php echo esc_attr(get_option('pbuaiv_bank_account','')); ?>" /></div>
            </div>

            <div class="pbuaiv-setup-nav">
                <button type="button" class="pbuaiv-btn pbuaiv-btn--ghost pbuaiv-prev-step" data-prev="1">← <?php esc_html_e( 'Back', 'pb-invoicing-system' ); ?></button>
                <button type="button" class="pbuaiv-btn pbuaiv-btn--primary pbuaiv-next-step" data-next="3"><?php esc_html_e( 'Next: Branding', 'pb-invoicing-system' ); ?> →</button>
            </div>
        </div>

        <!-- Step 3: Branding -->
        <div class="pbuaiv-setup-panel" id="pbuaiv-step-3" style="display:none;">
            <h2><?php esc_html_e( 'Brand Colors & Template', 'pb-invoicing-system' ); ?></h2>
            <p class="pbuaiv-setup-panel__sub"><?php esc_html_e( 'Choose your invoice look. You can change this any time.', 'pb-invoicing-system' ); ?></p>

            <div class="pbuaiv-form-grid">
                <div class="pbuaiv-field">
                    <label class="pbuaiv-label"><?php esc_html_e( 'Primary Color', 'pb-invoicing-system' ); ?></label>
                    <input type="text" class="pbuaiv-color-picker" name="pbuaiv_brand_primary" value="<?php echo esc_attr(get_option('pbuaiv_brand_primary','#244b8e')); ?>" />
                </div>
                <div class="pbuaiv-field">
                    <label class="pbuaiv-label"><?php esc_html_e( 'Accent Color', 'pb-invoicing-system' ); ?></label>
                    <input type="text" class="pbuaiv-color-picker" name="pbuaiv_brand_secondary" value="<?php echo esc_attr(get_option('pbuaiv_brand_secondary','#f59e0b')); ?>" />
                </div>
            </div>

            <div class="pbuaiv-template-picker">
                <h3><?php esc_html_e( 'Invoice Template', 'pb-invoicing-system' ); ?></h3>
                <?php
                $current_template = get_option( 'pbuaiv_default_template', 'modern' );
                $templates = [
                    'modern'   => [ 'label' => __( 'Modern', 'pb-invoicing-system' ),   'desc' => __( 'Clean header, colored accent band', 'pb-invoicing-system' ) ],
                    'classic'  => [ 'label' => __( 'Classic', 'pb-invoicing-system' ),  'desc' => __( 'Traditional layout, black & white friendly', 'pb-invoicing-system' ) ],
                    'minimal'  => [ 'label' => __( 'Minimal', 'pb-invoicing-system' ),  'desc' => __( 'Ultra-clean, no borders, lots of whitespace', 'pb-invoicing-system' ) ],
                    'corporate'=> [ 'label' => __( 'Corporate', 'pb-invoicing-system' ),'desc' => __( 'Bold header, professional two-column layout', 'pb-invoicing-system' ) ],
                ];
                foreach ( $templates as $slug => $tmpl ) :
                ?>
                    <label class="pbuaiv-template-option <?php echo $current_template === $slug ? 'pbuaiv-template-option--selected' : ''; ?>">
                        <input type="radio" name="pbuaiv_default_template" value="<?php echo esc_attr($slug); ?>" <?php checked($current_template,$slug); ?> />
                        <div class="pbuaiv-template-thumb pbuaiv-template-thumb--<?php echo esc_attr($slug); ?>">
                            <div class="pbuaiv-template-thumb__header"></div>
                            <div class="pbuaiv-template-thumb__lines">
                                <span></span><span></span><span></span>
                            </div>
                            <div class="pbuaiv-template-thumb__footer"></div>
                        </div>
                        <strong><?php echo esc_html($tmpl['label']); ?></strong>
                        <small><?php echo esc_html($tmpl['desc']); ?></small>
                    </label>
                <?php endforeach; ?>
            </div>

            <div class="pbuaiv-setup-nav">
                <button type="button" class="pbuaiv-btn pbuaiv-btn--ghost pbuaiv-prev-step" data-prev="2">← <?php esc_html_e( 'Back', 'pb-invoicing-system' ); ?></button>
                <button type="button" class="pbuaiv-btn pbuaiv-btn--primary pbuaiv-next-step" data-next="4"><?php esc_html_e( 'Next: Documents', 'pb-invoicing-system' ); ?> →</button>
            </div>
        </div>

        <!-- Step 4: Document rules -->
        <div class="pbuaiv-setup-panel" id="pbuaiv-step-4" style="display:none;">
            <h2><?php esc_html_e( 'Document Automation', 'pb-invoicing-system' ); ?></h2>
            <p class="pbuaiv-setup-panel__sub"><?php esc_html_e( 'These rules are identical to the Automation and Email tabs in Settings.', 'pb-invoicing-system' ); ?></p>

            <?php
            $statuses         = wc_get_order_statuses();
            $automation_rules = PBUAIV_Settings_Schema::automation_rules();
            $attachment_rules = PBUAIV_Settings_Schema::attachment_rules();
            $customer_emails  = PBUAIV_Settings_Schema::customer_emails();
            ?>

            <div class="pbuaiv-card">
                <div class="pbuaiv-card__head"><h3><?php esc_html_e( 'Automatic Document Generation', 'pb-invoicing-system' ); ?></h3></div>
                <div class="pbuaiv-card__body">
                    <?php foreach ( $automation_rules as $type => $rule ) :
                        $current = PBUAIV_Settings_Schema::get_array_option( $rule['option'] ); ?>
                        <div style="margin-bottom:20px;">
                            <?php
                            /* translators: %s: document type label, such as Tax Invoice or Payment Receipt. */
                            $auto_issue_label = sprintf( __( 'Auto-issue %s when order status becomes:', 'pb-invoicing-system' ), $rule['label'] );
                            ?>
                            <label class="pbuaiv-label"><?php echo esc_html( $auto_issue_label ); ?></label>
                            <div class="pbuaiv-checkbox-grid">
                                <?php foreach ( $statuses as $slug => $label ) :
                                    $slug = str_replace( 'wc-', '', $slug ); ?>
                                    <label class="pbuaiv-checkbox">
                                        <input type="checkbox" name="<?php echo esc_attr( $rule['option'] ); ?>[]" value="<?php echo esc_attr( $slug ); ?>" <?php checked( in_array( $slug, $current, true ) ); ?> />
                                        <?php echo esc_html( $label ); ?>
                                    </label>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>

            <div class="pbuaiv-card" style="margin-top:20px;">
                <div class="pbuaiv-card__head"><h3><?php esc_html_e( 'PDF Email Attachments', 'pb-invoicing-system' ); ?></h3></div>
                <div class="pbuaiv-card__body">
                    <p class="pbuaiv-setup-panel__sub" style="margin-bottom:14px;"><?php esc_html_e( 'Select the WooCommerce customer emails that should receive each PDF document.', 'pb-invoicing-system' ); ?></p>
                    <?php foreach ( $attachment_rules as $type => $rule ) :
                        $current = PBUAIV_Settings_Schema::get_array_option( $rule['option'] ); ?>
                        <div style="margin-bottom:20px;">
                            <?php
                            /* translators: %s: document type label, such as Tax Invoice or Payment Receipt. */
                            $attachment_label = sprintf( __( 'Attach %s to:', 'pb-invoicing-system' ), $rule['label'] );
                            ?>
                            <label class="pbuaiv-label"><?php echo esc_html( $attachment_label ); ?></label>
                            <div class="pbuaiv-checkbox-grid">
                                <?php foreach ( $customer_emails as $email_id => $email_title ) : ?>
                                    <label class="pbuaiv-checkbox">
                                        <input type="checkbox" name="<?php echo esc_attr( $rule['option'] ); ?>[]" value="<?php echo esc_attr( $email_id ); ?>" <?php checked( in_array( $email_id, $current, true ) ); ?> />
                                        <?php echo esc_html( $email_title ); ?>
                                    </label>
                                <?php endforeach; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                    <p class="pbuaiv-help"><?php esc_html_e( 'Selected PDFs are attached directly to WooCommerce customer emails. WooCommerce sends the email; this plugin only supplies the configured document attachments.', 'pb-invoicing-system' ); ?></p>
                </div>
            </div>

            <div class="pbuaiv-setup-nav">
                <button type="button" class="pbuaiv-btn pbuaiv-btn--ghost pbuaiv-prev-step" data-prev="3">← <?php esc_html_e( 'Back', 'pb-invoicing-system' ); ?></button>
                <button type="button" class="pbuaiv-btn pbuaiv-btn--primary pbuaiv-next-step" data-next="5" id="pbuaiv-save-finish"><?php esc_html_e( 'Save & Finish', 'pb-invoicing-system' ); ?> ✓</button>
            </div>
        </div>

        <!-- Step 5: Done -->
        <div class="pbuaiv-setup-panel pbuaiv-setup-panel--done" id="pbuaiv-step-5" style="display:none;">
            <div class="pbuaiv-done-icon">🇦🇪</div>
            <h2><?php esc_html_e( 'You\'re all set!', 'pb-invoicing-system' ); ?></h2>
            <p><?php esc_html_e( 'UAE VAT Invoice for WooCommerce is configured and ready. Your documents will now be automatically issued and attached to WooCommerce emails based on your settings.', 'pb-invoicing-system' ); ?></p>

            <div class="pbuaiv-done-links">
                <a href="<?php echo esc_url(admin_url('admin.php?page=pbuaiv-settings')); ?>" class="pbuaiv-btn pbuaiv-btn--primary">
                    <?php esc_html_e( 'Go to Settings', 'pb-invoicing-system' ); ?>
                </a>
                <a href="<?php echo esc_url(admin_url('edit.php?post_type=shop_order')); ?>" class="pbuaiv-btn pbuaiv-btn--secondary">
                    <?php esc_html_e( 'View Orders', 'pb-invoicing-system' ); ?>
                </a>
                <a href="<?php echo esc_url(admin_url('admin.php?page=pbuaiv-templates')); ?>" class="pbuaiv-btn pbuaiv-btn--ghost">
                    <?php esc_html_e( 'Customise Templates', 'pb-invoicing-system' ); ?>
                </a>
            </div>

            <div class="pbuaiv-fta-checklist" id="pbuaiv-fta-checklist">
                <h3><?php esc_html_e( 'FTA Compliance Checklist', 'pb-invoicing-system' ); ?></h3>
                <?php
                // Static server-side check — this reflects the saved settings
                // (for a fresh install these will be empty until the wizard saves)
                $trn_val  = get_option( 'pbuaiv_trn', '' );
                $trn_ok   = ! empty( $trn_val ) && PBUAIV_FTA_Compliance::validate_trn( $trn_val );
                $name_ok  = ! empty( get_option( 'pbuaiv_company_name', '' ) );
                $addr_ok  = ! empty( get_option( 'pbuaiv_company_address', '' ) );
                $checks   = [
                    [ 'id' => 'trn',       'pass' => $trn_ok,   'label' => __( 'Valid TRN configured', 'pb-invoicing-system' ) ],
                    [ 'id' => 'name',      'pass' => $name_ok,  'label' => __( 'Company name set', 'pb-invoicing-system' ) ],
                    [ 'id' => 'address',   'pass' => $addr_ok,  'label' => __( 'Company address set', 'pb-invoicing-system' ) ],
                    [ 'id' => 'numbering', 'pass' => true,       'label' => __( 'Sequential invoice numbering enabled', 'pb-invoicing-system' ) ],
                    [ 'id' => 'vat_aed',   'pass' => true,       'label' => __( 'VAT amount displays in AED', 'pb-invoicing-system' ) ],
                    [ 'id' => 'autodetect','pass' => true,       'label' => __( 'Standard vs. simplified auto-detection active', 'pb-invoicing-system' ) ],
                    [ 'id' => 'xml',       'pass' => true,       'label' => __( 'PINT AE XML export available', 'pb-invoicing-system' ) ],
                    [ 'id' => 'qr',        'pass' => $trn_ok,   'label' => __( 'FTA QR code on all invoices', 'pb-invoicing-system' ) ],
                ];
                foreach ( $checks as $check ) {
                    $icon  = $check['pass'] ? '✅' : '⚠️';
                    $class = $check['pass'] ? 'pbuaiv-check--pass' : 'pbuaiv-check--warn';
                    printf(
                        '<div class="pbuaiv-check-item %s" id="pbuaiv-check-%s">%s %s</div>',
                        esc_attr( $class ),
                        esc_attr( $check['id'] ),
                        esc_html( $icon ),
                        esc_html( $check['label'] )
                    );
                }
                ?>
            </div>
        </div>

    </div><!-- .pbuaiv-setup-body -->
</div><!-- .pbuaiv-setup-wrap -->
