<?php
/**
 * 247ChatMate — Agency Hub partial.
 * Checkpoint: AGENCYCLIENTREMOTEUXANDFREEMIUMFIX1
 *
 * Display-only split from admin/views/partials/agency-hub.php.
 * Expects parent Agency Hub variables to be prepared before include.
 */

defined('ABSPATH') || exit;

$matewa_agency_client_pdf_records = function_exists('matewa_agency_client_pdf_get_records')
    ? matewa_agency_client_pdf_get_records()
    : array();
$matewa_agency_client_pdf_records = is_array($matewa_agency_client_pdf_records) ? $matewa_agency_client_pdf_records : array();
?>
    <section id="matewa-agency-client-packs" class="matewa-agency-client-board" aria-label="Client setup packs board">
        <div class="matewa-agency-client-board-header">
            <div>
                <span class="matewa-agency-hub-kicker">Client onboarding</span>
                <h4>Client setup packs</h4>
                <p>Create manual setup packs for client sites. Export JSON stays as the fallback, and remote push can now send that same setup payload to an installed client plugin using a one-time setup key.</p>
            </div>
            <div class="matewa-agency-client-board-actions" aria-label="Client setup pack next actions">
                <span class="matewa-agency-soft-pill">Manual entry active</span>
                <span class="matewa-agency-soft-pill">Export JSON ready</span>
                <span class="matewa-agency-soft-pill">Remote push ready</span>
            </div>
        </div>

        <?php if ('' !== $matewa_agency_client_pack_notice_message) : ?>
            <div class="matewa-agency-bundle-notice is-<?php echo esc_attr($matewa_agency_client_pack_notice_type); ?>" role="status" aria-live="polite">
                <?php echo esc_html($matewa_agency_client_pack_notice_message); ?>
            </div>
        <?php endif; ?>

        <?php if (function_exists('matewa_agency_client_setup_key_render_panel')) : ?>
            <?php matewa_agency_client_setup_key_render_panel('agency'); ?>
        <?php endif; ?>

        <?php if (function_exists('matewa_agency_client_pdf_render_panel')) : ?>
            <?php matewa_agency_client_pdf_render_panel($matewa_agency_client_pack_records); ?>
        <?php endif; ?>

        <?php
        $matewa_remote_push_prefill_pack_id = '';
        $matewa_remote_push_prefill_client_url = '';
        $matewa_remote_push_prefill_client_admin_url = '';
        $matewa_remote_push_prefill_live_url = '';
        $matewa_remote_push_has_live_last = !empty($matewa_agency_remote_push_last) && empty($matewa_agency_remote_push_last['test_mode']);
        $matewa_remote_push_has_result = !empty($matewa_agency_remote_push_last);
        $matewa_remote_push_should_open = ('' !== $matewa_agency_remote_push_notice_message) || ($matewa_remote_push_has_live_last && !empty($matewa_agency_remote_push_last['ok']));
        if ($matewa_remote_push_has_live_last) {
            $matewa_remote_push_prefill_pack_id = isset($matewa_agency_remote_push_last['pack_id']) ? sanitize_key((string) $matewa_agency_remote_push_last['pack_id']) : '';
            if ('' === $matewa_remote_push_prefill_pack_id && isset($matewa_agency_remote_push_last['setup_pack_id'])) {
                $matewa_remote_push_prefill_pack_id = sanitize_key((string) $matewa_agency_remote_push_last['setup_pack_id']);
            }

            $matewa_remote_push_prefill_live_url = isset($matewa_agency_remote_push_last['client_site_url']) ? esc_url_raw((string) $matewa_agency_remote_push_last['client_site_url']) : '';
            if (function_exists('matewa_agency_remote_push_clean_client_site_url') && '' !== $matewa_remote_push_prefill_live_url) {
                $matewa_remote_push_prefill_live_url = matewa_agency_remote_push_clean_client_site_url($matewa_remote_push_prefill_live_url);
            }

            if ('' !== $matewa_remote_push_prefill_live_url) {
                $matewa_remote_push_parts = wp_parse_url($matewa_remote_push_prefill_live_url);
                if (is_array($matewa_remote_push_parts) && !empty($matewa_remote_push_parts['host'])) {
                    $matewa_remote_push_prefill_client_url = strtolower((string) $matewa_remote_push_parts['host']);
                    $matewa_remote_push_prefill_path = isset($matewa_remote_push_parts['path']) ? trim((string) $matewa_remote_push_parts['path'], '/') : '';
                    if ('' !== $matewa_remote_push_prefill_path) {
                        $matewa_remote_push_prefill_client_url .= '/' . $matewa_remote_push_prefill_path;
                    }
                    $matewa_remote_push_prefill_client_admin_url = esc_url_raw(trailingslashit($matewa_remote_push_prefill_live_url) . 'wp-admin/admin.php?page=247chatmate&matewa_saved_section=setup#matewa-setup-agency-key');
                }
            }
        }
        ?>

        <div id="matewa-agency-client-pack-editor" class="matewa-agency-client-pack-form" aria-label="Create or edit manual client setup pack">
            <?php if ($matewa_agency_client_is_editing) : ?>
                <input type="hidden" name="matewa_agency_client_pack_id" value="<?php echo esc_attr($matewa_agency_client_edit_pack_id); ?>">
            <?php endif; ?>
            <div class="matewa-agency-client-pack-form-head">
                <div>
                    <strong><?php echo $matewa_agency_client_is_editing ? esc_html('Edit client setup pack') : esc_html('Add client setup pack'); ?></strong>
                    <span><?php echo $matewa_agency_client_is_editing ? esc_html('Update this client pack in the main Agency Hub window. The assigned seat is preserved.') : esc_html('Assign one available bundle seat and capture the details needed for the client-site setup JSON export.'); ?></span>
                </div>
                <em><?php echo esc_html((string) $matewa_agency_available_seats); ?> seats available</em>
            </div>

            <?php if ($matewa_agency_client_is_editing && isset($matewa_agency_client_edit_pack['status']) && 'pdf_uploaded_review' === sanitize_key((string) $matewa_agency_client_edit_pack['status'])) : ?>
                <div class="matewa-agency-bundle-notice is-warning matewa-agency-pdf-review-explainer" role="note">
                    <strong>PDF draft — review before export.</strong>
                    <span>The PDF has started this setup pack and filled any fields it could read. Check the boxes below, choose the correct business type if needed, then click Save setup pack before export or remote push.</span>
                </div>
            <?php endif; ?>

            <div class="matewa-agency-client-pack-section" id="matewa-agency-client-details-section">
                <div class="matewa-agency-client-pack-section-head">
                    <span class="matewa-agency-client-pack-step">1</span>
                    <div>
                        <strong>Client details</strong>
                        <small>These details feed the setup JSON that will be imported onto the end-user WordPress site.</small>
                    </div>
                </div>
                <div class="matewa-agency-client-pack-grid is-details">
                    <label>
                        <span>Client business name</span>
                        <input type="text" name="matewa_agency_client_business" placeholder="e.g. ABC Plumbing" value="<?php echo esc_attr($matewa_agency_client_form_values['client_business']); ?>" autocomplete="organization">
                    </label>
                    <label>
                        <span>Client domain</span>
                        <input type="text" name="matewa_agency_client_domain" placeholder="e.g. abcplumbing.co.uk" value="<?php echo esc_attr($matewa_agency_client_form_values['client_domain']); ?>" autocomplete="url">
                    </label>
                    <label>
                        <span>Client admin email</span>
                        <input type="email" name="matewa_agency_client_admin_email" placeholder="e.g. admin@client.co.uk" value="<?php echo esc_attr($matewa_agency_client_form_values['client_admin_email']); ?>" autocomplete="email">
                    </label>
                    <label>
                        <span>WhatsApp mobile/cell number</span>
                        <input type="text" name="matewa_agency_client_whatsapp" placeholder="e.g. 447900123456" value="<?php echo esc_attr($matewa_agency_client_form_values['whatsapp_number']); ?>" autocomplete="tel">
                    </label>
                    <label>
                        <span>Client leads email</span>
                        <input type="email" name="matewa_agency_client_leads_email" placeholder="e.g. enquiries@client.co.uk" value="<?php echo esc_attr($matewa_agency_client_form_values['leads_email']); ?>" autocomplete="email">
                    </label>
                </div>

                <div class="matewa-agency-remote-sending-scope" aria-label="Remote sending and client widget scope">
                    <div class="matewa-agency-client-pack-section-head is-compact">
                        <span class="matewa-agency-client-pack-step is-soft">RS</span>
                        <div>
                            <strong>Remote sending & widget scope</strong>
                            <small>Keep the sending controls in this client box. The selected pack sends the client's widget scope, visitor buttons and agency-managed status to the installed client plugin.</small>
                        </div>
                        <?php if (!empty($matewa_agency_remote_push_last['ok']) && empty($matewa_agency_remote_push_last['test_mode'])) : ?>
                            <em class="matewa-agency-soft-pill">Client site updated</em>
                        <?php endif; ?>
                    </div>
                    <div class="matewa-agency-client-pack-grid is-details matewa-agency-remote-sending-scope-grid">
                        <label>
                            <span>Client widget scope</span>
                            <select name="matewa_agency_client_page_mode">
                                <option value="all_pages" <?php selected($matewa_agency_client_form_values['page_mode'], 'all_pages'); ?>>All pages</option>
                                <option value="homepage" <?php selected($matewa_agency_client_form_values['page_mode'], 'homepage'); ?>>Homepage only</option>
                                <option value="specific_pages" <?php selected($matewa_agency_client_form_values['page_mode'], 'specific_pages'); ?>>Specific scanned pages</option>
                            </select>
                            <small>This reuses the existing frontend widget scope on the client site only.</small>
                        </label>
                    </div>

                <details id="matewa-agency-remote-push-panel" class="matewa-agency-client-pack-inline-tools matewa-agency-remote-push-panel" aria-label="Remote client setup push" <?php echo !empty($matewa_remote_push_should_open) ? 'open' : ''; ?>>
                    <summary>
                        <span>Push setup to client site</span>
                        <small>Sends the saved pack, widget scope and visitor buttons to the installed client plugin.</small>
                    </summary>
                    <div class="matewa-agency-remote-push-inline-body">
                        <?php if ('' !== $matewa_agency_remote_push_notice_message) : ?>
                            <div class="matewa-agency-bundle-notice is-<?php echo esc_attr($matewa_agency_remote_push_notice_type); ?>" role="status" aria-live="polite">
                                <?php echo esc_html($matewa_agency_remote_push_notice_message); ?>
                            </div>
                        <?php endif; ?>

                        <?php if (!empty($matewa_agency_remote_push_last)) : ?>
                            <?php
                            $matewa_remote_push_time = isset($matewa_agency_remote_push_last['attempted_at']) ? absint($matewa_agency_remote_push_last['attempted_at']) : 0;
                            $matewa_remote_push_time_label = $matewa_remote_push_time > 0 ? wp_date((string) get_option('date_format', 'Y-m-d') . ' ' . (string) get_option('time_format', 'H:i'), $matewa_remote_push_time) : 'Unknown time';
                            $matewa_remote_push_ok = !empty($matewa_agency_remote_push_last['ok']);
                            $matewa_remote_push_client_url = isset($matewa_agency_remote_push_last['client_site_url']) ? esc_url((string) $matewa_agency_remote_push_last['client_site_url']) : '';
                            $matewa_remote_push_message = isset($matewa_agency_remote_push_last['message']) ? sanitize_text_field((string) $matewa_agency_remote_push_last['message']) : '';
                            $matewa_remote_push_pack = isset($matewa_agency_remote_push_last['pack_id']) ? sanitize_key((string) $matewa_agency_remote_push_last['pack_id']) : '';
                            $matewa_remote_push_transport = isset($matewa_agency_remote_push_last['transport']) ? sanitize_key((string) $matewa_agency_remote_push_last['transport']) : 'https_post';
                            $matewa_remote_push_response = isset($matewa_agency_remote_push_last['response_code']) ? absint($matewa_agency_remote_push_last['response_code']) : 0;
                            $matewa_remote_push_test = !empty($matewa_agency_remote_push_last['test_mode']);
                            $matewa_remote_push_hash = isset($matewa_agency_remote_push_last['payload_hash']) ? sanitize_text_field((string) $matewa_agency_remote_push_last['payload_hash']) : '';
                            $matewa_remote_push_temp_key_id = isset($matewa_agency_remote_push_last['temporary_key_id']) ? sanitize_key((string) $matewa_agency_remote_push_last['temporary_key_id']) : '';
                            $matewa_remote_push_temp_consumed = !empty($matewa_agency_remote_push_last['temporary_key_consumed']);
                            $matewa_remote_push_key_restored = !empty($matewa_agency_remote_push_last['setup_key_restored']);
                            $matewa_remote_push_refresh_url = add_query_arg(array(
                                'page'                 => '247chatmate',
                                'matewa_saved_section' => 'agency',
                            ), admin_url('admin.php')) . '#matewa-agency-remote-push-panel';
                            $matewa_remote_push_setup_url = '';
                            if ('' !== $matewa_remote_push_client_url) {
                                $matewa_remote_push_setup_url = esc_url_raw(trailingslashit($matewa_remote_push_client_url) . 'wp-admin/admin.php?page=247chatmate&matewa_saved_section=setup#matewa-setup-agency-key');
                            }
                            ?>
                            <?php if (!$matewa_remote_push_test && $matewa_remote_push_ok && '' !== $matewa_remote_push_client_url) : ?>
                                <div class="matewa-agency-remote-push-success-summary" role="status" aria-live="polite">
                                    <strong>Client site updated.</strong>
                                    <span>The setup pack was imported and the one-time setup key was consumed.</span>
                                    <div class="matewa-agency-remote-push-post-actions" aria-label="Remote setup follow-up actions">
                                        <a class="button button-secondary" href="<?php echo esc_url($matewa_remote_push_refresh_url); ?>">Refresh result</a>
                                        <a class="button button-secondary" href="#matewa-agency-remote-push-panel">Prepare update</a>
                                        <?php if ('' !== $matewa_remote_push_setup_url) : ?>
                                            <a class="button button-secondary" href="<?php echo esc_url($matewa_remote_push_setup_url); ?>" target="_blank" rel="noopener noreferrer">Open client Setup</a>
                                        <?php endif; ?>
                                        <a class="button button-secondary" href="<?php echo esc_url(add_query_arg('matewa_widget_refresh', (string) time(), $matewa_remote_push_client_url)); ?>" target="_blank" rel="noopener noreferrer">Open client website</a>
                                    </div>
                                    <small>To update this client again, generate a fresh one-time setup key on the client site, paste it below, then push setup again.</small>
                                </div>
                            <?php endif; ?>

                            <details class="matewa-agency-remote-push-audit" aria-label="Last remote setup result details">
                                <summary>
                                    <span>Open last push result details</span>
                                    <small><?php echo esc_html($matewa_remote_push_time_label . ' — ' . ($matewa_remote_push_ok ? 'imported' : 'not imported')); ?></small>
                                </summary>
                                <div class="matewa-agency-bundle-note" role="status" aria-live="polite">
                                    <strong>Last remote setup result:</strong>
                                    <?php echo esc_html($matewa_remote_push_time_label); ?> — <strong><?php echo $matewa_remote_push_ok ? esc_html('imported') : esc_html('not imported'); ?></strong>
                                    <?php echo '' !== $matewa_remote_push_client_url ? esc_html(' to ' . $matewa_remote_push_client_url) : ''; ?>
                                    <?php echo '' !== $matewa_remote_push_pack ? esc_html(' · Pack ' . $matewa_remote_push_pack) : ''; ?>
                                    <br><small><?php echo esc_html('Transport: ' . ('local_loopback' === $matewa_remote_push_transport ? 'local loopback test' : 'HTTPS remote push') . ($matewa_remote_push_response > 0 ? ' · Response ' . $matewa_remote_push_response : '') . ($matewa_remote_push_test ? ' · Test facility' : '')); ?></small>
                                    <?php echo '' !== $matewa_remote_push_message ? '<br><small>' . esc_html($matewa_remote_push_message) . '</small>' : ''; ?>
                                    <?php if ($matewa_remote_push_test) : ?>
                                        <br><small><?php echo esc_html('Temporary setup key consumed: ' . ($matewa_remote_push_temp_consumed ? 'yes' : 'not confirmed')); ?></small>
                                        <?php echo '' !== $matewa_remote_push_temp_key_id ? '<br><small>' . esc_html('Temporary setup key ID: ' . $matewa_remote_push_temp_key_id) . '</small>' : ''; ?>
                                        <br><small><?php echo esc_html('Existing setup key preserved: ' . ($matewa_remote_push_key_restored ? 'yes' : 'no previous active key')); ?></small>
                                        <br><small><?php echo esc_html('Plain setup key stored: no'); ?></small>
                                    <?php endif; ?>
                                    <?php echo '' !== $matewa_remote_push_hash ? '<br><small>' . esc_html('Payload hash: ' . substr($matewa_remote_push_hash, 0, 18) . '…') . '</small>' : ''; ?>
                                </div>
                            </details>
                        <?php endif; ?>

                        <div class="matewa-agency-client-pack-grid is-details matewa-agency-remote-push-grid is-client-detail-aligned">
                            <label>
                                <span>Setup pack to send</span>
                                <select name="matewa_agency_remote_push_pack_id" form="matewa-agency-remote-push-form" data-agency-remote-pack-select <?php echo empty($matewa_agency_client_pack_records) ? 'disabled' : ''; ?>>
                                    <?php if (empty($matewa_agency_client_pack_records)) : ?>
                                        <option value="">Create a setup pack first</option>
                                    <?php else : ?>
                                        <option value="">Choose saved setup pack</option>
                                        <?php foreach ($matewa_agency_client_pack_records as $matewa_push_pack) : ?>
                                            <?php
                                            $matewa_push_pack_id = isset($matewa_push_pack['setup_pack_id']) ? sanitize_key((string) $matewa_push_pack['setup_pack_id']) : '';
                                            $matewa_push_client = isset($matewa_push_pack['client_business']) ? sanitize_text_field((string) $matewa_push_pack['client_business']) : 'Client';
                                            $matewa_push_domain = isset($matewa_push_pack['client_domain']) ? sanitize_text_field((string) $matewa_push_pack['client_domain']) : '';
                                            $matewa_push_domain_for_field = preg_replace('#^https?://#i', '', (string) $matewa_push_domain);
                                            $matewa_push_domain_for_field = preg_replace('#/.*$#', '', (string) $matewa_push_domain_for_field);
                                            $matewa_push_domain_for_field = strtolower(trim((string) $matewa_push_domain_for_field));
                                            ?>
                                            <?php if ('' !== $matewa_push_pack_id) : ?>
                                                <option value="<?php echo esc_attr($matewa_push_pack_id); ?>" data-client-domain="<?php echo esc_attr($matewa_push_domain_for_field); ?>" <?php selected($matewa_push_pack_id, $matewa_remote_push_prefill_pack_id); ?>><?php echo esc_html($matewa_push_client . ('' !== $matewa_push_domain ? ' — ' . $matewa_push_domain : '') . ' · ' . $matewa_push_pack_id); ?></option>
                                            <?php endif; ?>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                </select>
                            </label>
                            <label class="matewa-agency-url-input-label">
                                <span>Client domain</span>
                                <span class="matewa-agency-url-prefix-field">
                                    <span class="matewa-agency-url-prefix" aria-hidden="true">https://</span>
                                    <input type="text" name="matewa_agency_remote_push_client_site_url" form="matewa-agency-remote-push-form" placeholder="clientdomain.co.uk" value="<?php echo esc_attr($matewa_remote_push_prefill_client_url); ?>" inputmode="url" autocomplete="url" spellcheck="false" data-agency-remote-url-input>
                                </span>
                                <small>Type the domain only. The HTTPS prefix is added automatically.</small>
                            </label>
                            <label>
                                <span>Client one-time setup key</span>
                                <input type="password" name="matewa_agency_remote_push_setup_key" form="matewa-agency-remote-push-form" placeholder="Paste key from client plugin" autocomplete="off" spellcheck="false">
                            </label>
                        </div>
                        <div class="matewa-agency-client-pack-actions">
                            <button type="submit" class="button button-primary" form="matewa-agency-remote-push-form" <?php echo empty($matewa_agency_client_pack_records) ? 'disabled' : ''; ?>>Push setup</button>
                            <small>No WordPress admin login is used. The client plugin verifies the one-time key, imports locally, stores the audit payload, then consumes the key.</small>
                        </div>
                        <div class="matewa-agency-remote-push-next-step" role="note">
                            <strong>After a successful push:</strong> open the client WordPress admin, go to <strong>247ChatMate → Setup</strong>, confirm the imported fields, visitor buttons, dashboard status and agency-managed button set, then test the live widget and lead email. If the public widget opens unstyled, clear site/cache/minification and reopen the client website using the refresh button above.
                        </div>
                    </div>
                </details>

                <details class="matewa-agency-page-scan-panel matewa-agency-page-scan-panel--collapsed" aria-label="Agency client page scanner">
                    <summary>
                        <span>Open client page scan</span>
                        <small>Use only when sending the widget to selected client pages.</small>
                    </summary>
                    <div class="matewa-agency-page-scan-body">
                    <div class="matewa-agency-page-scan-head">
                        <div>
                            <strong>Client page scan</strong>
                            <small>Scan the saved client domain, then choose which discovered pages should be included in the client setup JSON.</small>
                        </div>
                        <?php if ($matewa_agency_client_is_editing && function_exists('matewa_agency_client_pack_scan_pages_url')) : ?>
                            <a class="button button-secondary" href="<?php echo esc_url(matewa_agency_client_pack_scan_pages_url($matewa_agency_client_edit_pack_id)); ?>">Scan pages</a>
                        <?php else : ?>
                            <span class="matewa-agency-client-row-note">Save the setup pack first, then scan pages.</span>
                        <?php endif; ?>
                    </div>

                    <?php if (!empty($matewa_agency_client_last_page_scan['scanned_at'])) : ?>
                        <p class="matewa-agency-page-scan-meta">Last scan: <?php echo esc_html(wp_date((string) get_option('date_format', 'Y-m-d') . ' ' . (string) get_option('time_format', 'H:i'), absint($matewa_agency_client_last_page_scan['scanned_at']))); ?> · <?php echo esc_html((string) count($matewa_agency_client_detected_pages)); ?> page<?php echo 1 === count($matewa_agency_client_detected_pages) ? '' : 's'; ?> found.</p>
                    <?php endif; ?>

                    <?php if (!empty($matewa_agency_client_detected_pages)) : ?>
                        <div class="matewa-agency-page-scan-list" role="group" aria-label="Detected client pages">
                            <?php foreach ($matewa_agency_client_detected_pages as $matewa_detected_page) : ?>
                                <?php
                                $matewa_detected_page = is_array($matewa_detected_page) ? $matewa_detected_page : array();
                                $matewa_detected_url = isset($matewa_detected_page['url']) ? esc_url_raw((string) $matewa_detected_page['url']) : '';
                                if ('' === $matewa_detected_url) {
                                    continue;
                                }
                                $matewa_detected_title = isset($matewa_detected_page['title']) && '' !== trim((string) $matewa_detected_page['title']) ? sanitize_text_field((string) $matewa_detected_page['title']) : $matewa_detected_url;
                                $matewa_detected_selected = in_array(strtolower($matewa_detected_url), $matewa_agency_client_selected_page_urls, true);
                                ?>
                                <label class="matewa-agency-page-scan-row">
                                    <input type="checkbox" name="matewa_agency_client_specific_pages[]" value="<?php echo esc_attr($matewa_detected_url); ?>" <?php checked($matewa_detected_selected); ?>>
                                    <span>
                                        <a class="matewa-agency-page-scan-title-link" href="<?php echo esc_url($matewa_detected_url); ?>" target="_blank" rel="noopener noreferrer" onclick="event.stopPropagation();">
                                            <strong><?php echo esc_html($matewa_detected_title); ?></strong>
                                        </a>
                                        <a class="matewa-agency-page-scan-url-link" href="<?php echo esc_url($matewa_detected_url); ?>" target="_blank" rel="noopener noreferrer" onclick="event.stopPropagation();">
                                            <small><?php echo esc_html($matewa_detected_url); ?></small>
                                        </a>
                                    </span>
                                </label>
                            <?php endforeach; ?>
                        </div>
                        <p class="matewa-agency-page-scan-meta">Set Page visibility to <strong>Specific scanned pages</strong>, tick the pages, then save the setup pack. Nothing is placed on the client site until the JSON is imported and applied there.</p>
                    <?php else : ?>
                        <p class="matewa-agency-page-scan-meta">No scanned pages are stored yet. This first version checks public sitemaps, WordPress page endpoints and homepage navigation links.</p>
                    <?php endif; ?>
                    </div>
                </details>
                </div>
            </div>

            <div class="matewa-agency-client-pack-section matewa-agency-visitor-buttons-copy" data-matewa-agency-client-selector>
                <div class="matewa-agency-client-pack-section-head">
                    <span class="matewa-agency-client-pack-step">2</span>
                    <div>
                        <strong>Choose business type</strong>
                        <small>Choose in order, then edit the best starter buttons for this client.</small>
                    </div>
                </div>
                <div class="matewa-agency-client-pack-grid is-business-type">
                    <label>
                        <span>Category</span>
                        <select id="matewa_agency_client_business_category_key" name="matewa_agency_client_business_category_key" data-agency-client-category>
                            <?php foreach ($matewa_agency_client_selector_categories as $matewa_client_category_key => $matewa_client_category_data) : ?>
                                <?php
                                $matewa_client_category_key = sanitize_key((string) $matewa_client_category_key);
                                $matewa_client_category_label = isset($matewa_client_category_data['label']) ? sanitize_text_field((string) $matewa_client_category_data['label']) : 'General';
                                ?>
                                <option value="<?php echo esc_attr($matewa_client_category_key); ?>" <?php selected($matewa_agency_client_default_category, $matewa_client_category_key); ?>><?php echo esc_html($matewa_client_category_label); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </label>
                    <label>
                        <span>Business type</span>
                        <select id="matewa_agency_client_business_type_key" name="matewa_agency_client_business_type_key" data-agency-client-business-type>
                            <?php foreach ($matewa_agency_client_default_options as $matewa_client_vertical_key => $matewa_client_vertical_data) : ?>
                                <?php
                                $matewa_client_vertical_key = sanitize_key((string) $matewa_client_vertical_key);
                                $matewa_client_vertical_label = isset($matewa_client_vertical_data['display_label']) ? sanitize_text_field((string) $matewa_client_vertical_data['display_label']) : 'General';
                                ?>
                                <option value="<?php echo esc_attr($matewa_client_vertical_key); ?>" <?php selected($matewa_agency_client_default_vertical, $matewa_client_vertical_key); ?>><?php echo esc_html($matewa_client_vertical_label); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </label>
                </div>

                <div class="matewa-agency-client-helper matewa-agency-client-selector-helper">
                    <strong>Choose business type</strong>
                    <ul>
                        <li>Choose a category.</li>
                        <li>Choose your business type.</li>
                        <li>Edit the best starter buttons for this client.</li>
                    </ul>
                </div>

                <input type="hidden" name="matewa_agency_client_industry" data-agency-client-industry-label value="<?php echo esc_attr($matewa_agency_client_default_category_label); ?>">
                <input type="hidden" name="matewa_agency_client_business_category" data-agency-client-category-label value="<?php echo esc_attr($matewa_agency_client_default_category_label); ?>">
                <input type="hidden" name="matewa_agency_client_business_type" data-agency-client-business-label value="<?php echo esc_attr($matewa_agency_client_default_vertical_label); ?>">

                <div class="matewa-agency-client-chip-editor" aria-label="Editable copied Visitor Buttons for this client setup pack">
                    <div class="matewa-agency-client-chip-editor-head">
                        <strong>Visitor buttons — <span data-agency-client-chip-title><?php echo esc_html($matewa_agency_client_default_vertical_label); ?></span></strong>
                        <small>Edit the 6 quick-reply buttons for this client setup pack.</small>
                    </div>
                    <div class="matewa-agency-client-chip-grid" data-agency-client-chip-grid>
                        <?php foreach (array_slice($matewa_agency_client_default_chips, 0, 6) as $matewa_client_chip_label) : ?>
                            <div class="matewa-chip-editor-item">
                                <div class="matewa-chip-editor-chip">
                                    <textarea name="matewa_agency_client_chip_labels[]" maxlength="34" rows="2" autocomplete="off" aria-label="Visitor button label, maximum 34 characters over two visual rows"><?php echo esc_textarea($matewa_client_chip_label); ?></textarea>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    </div>
                    <div class="matewa-agency-client-helper matewa-agency-client-chip-helper">
                        <strong>Edit buttons</strong>
                        <ul>
                            <li>Edit any labels if needed.</li>
                            <li>Keep labels short — two lines max.</li>
                            <li>Save the setup pack when the buttons are ready.</li>
                            <li>If changes do not appear, clear your cache and try again.</li>
                            <li>Keep the final button for custom requests, such as “Something else”.</li>
                        </ul>
                    </div>
                </div>
            </div>
            <div class="matewa-agency-client-pack-actions">
                <button type="submit" class="button button-primary" name="matewa_agency_client_pack_action" value="<?php echo $matewa_agency_client_is_editing ? esc_attr('save') : esc_attr('create'); ?>"><?php echo $matewa_agency_client_is_editing ? esc_html('Save setup pack') : esc_html('Create setup pack'); ?></button>
                <?php if ($matewa_agency_client_is_editing) : ?>
                    <a class="button button-secondary" href="<?php echo esc_url(add_query_arg(array('page' => '247chatmate', 'matewa_saved_section' => 'agency'), admin_url('admin.php')) . '#matewa-agency-client-packs'); ?>">Cancel edit</a>
                <?php endif; ?>
                <small><?php echo $matewa_agency_client_is_editing ? esc_html('The assigned seat is preserved. The client site will still not receive the agency master LemonSqueezy key.') : esc_html('One available bundle seat will be reserved. The client site will not receive the agency master LemonSqueezy key.'); ?></small>
            </div>
        </div>

        <div class="matewa-agency-client-table-wrap">
            <table class="matewa-agency-client-table">
                <thead>
                    <tr>
                        <th scope="col">Client / domain</th>
                        <th scope="col">Licence</th>
                        <th scope="col">Expires</th>
                        <th scope="col">Pages</th>
                        <th scope="col">Business type</th>
                        <th scope="col">Visitor buttons</th>
                        <th scope="col">Status</th>
                        <th scope="col">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($matewa_agency_client_pack_records)) : ?>
                        <tr>
                            <td data-label="Client / domain">
                                <strong>First client</strong>
                                <span>Waiting for manual client setup pack creation</span>
                            </td>
                            <td data-label="Licence"><span class="matewa-agency-table-pill is-muted">Seat not assigned</span></td>
                            <td data-label="Expires">—</td>
                            <td data-label="Pages">All pages / Homepage / Specific pages</td>
                            <td data-label="Business type">Editable</td>
                            <td data-label="Chips">Defaults or edited</td>
                            <td data-label="Status"><span class="matewa-agency-table-pill is-warning">Create first pack</span></td>
                            <td data-label="Actions">Manual form above</td>
                        </tr>
                    <?php else : ?>
                        <?php foreach ($matewa_agency_client_pack_records as $matewa_client_pack) : ?>
                            <?php
                            $matewa_client_name = isset($matewa_client_pack['client_business']) ? sanitize_text_field((string) $matewa_client_pack['client_business']) : 'Client';
                            $matewa_client_domain = isset($matewa_client_pack['client_domain']) ? sanitize_text_field((string) $matewa_client_pack['client_domain']) : '';
                            $matewa_client_admin_email = isset($matewa_client_pack['client_admin_email']) ? sanitize_email((string) $matewa_client_pack['client_admin_email']) : '';
                            $matewa_client_leads_email = isset($matewa_client_pack['leads_email']) ? sanitize_email((string) $matewa_client_pack['leads_email']) : '';
                            $matewa_client_status = isset($matewa_client_pack['status']) ? sanitize_key((string) $matewa_client_pack['status']) : 'draft';
                            if ('ready' === $matewa_client_status) {
                                $matewa_client_status_label = 'Ready';
                                $matewa_client_status_class = 'is-ready';
                            } elseif ('pdf_uploaded_review' === $matewa_client_status) {
                                $matewa_client_status_label = 'PDF draft — review';
                                $matewa_client_status_class = 'is-warning';
                            } elseif ('needs_attention' === $matewa_client_status) {
                                $matewa_client_status_label = 'Needs attention';
                                $matewa_client_status_class = 'is-warning';
                            } else {
                                $matewa_client_status_label = 'Draft';
                                $matewa_client_status_class = 'is-warning';
                            }
                            $matewa_client_expiry_source = !empty($matewa_client_pack['renews_at']) ? $matewa_client_pack['renews_at'] : ($matewa_client_pack['expires_at'] ?? '');
                            $matewa_client_expiry_label = $matewa_agency_format_date($matewa_client_expiry_source);
                            $matewa_client_page_label = function_exists('matewa_agency_client_pack_page_mode_label') ? matewa_agency_client_pack_page_mode_label($matewa_client_pack['page_mode'] ?? 'all_pages') : 'All pages';
                            $matewa_client_detected_page_count = isset($matewa_client_pack['detected_pages']) && is_array($matewa_client_pack['detected_pages']) ? count($matewa_client_pack['detected_pages']) : 0;
                            $matewa_client_selected_page_count = isset($matewa_client_pack['selected_pages']) && is_array($matewa_client_pack['selected_pages']) ? count($matewa_client_pack['selected_pages']) : 0;
                            $matewa_client_chip_label = function_exists('matewa_agency_client_pack_chip_mode_label') ? matewa_agency_client_pack_chip_mode_label($matewa_client_pack['chip_mode'] ?? 'defaults') : 'Default chips';
                            $matewa_client_visitor_buttons = isset($matewa_client_pack['visitor_buttons']) && is_array($matewa_client_pack['visitor_buttons']) ? array_values($matewa_client_pack['visitor_buttons']) : array();
                            $matewa_client_type = isset($matewa_client_pack['business_type']) ? sanitize_text_field((string) $matewa_client_pack['business_type']) : 'General';
                            $matewa_client_category = isset($matewa_client_pack['business_category']) ? sanitize_text_field((string) $matewa_client_pack['business_category']) : '';
                            $matewa_client_pack_id = isset($matewa_client_pack['setup_pack_id']) ? sanitize_key((string) $matewa_client_pack['setup_pack_id']) : '';
                            $matewa_client_copy_url = trim($matewa_client_domain);
                            if ('' !== $matewa_client_copy_url && !preg_match('#^https?://#i', $matewa_client_copy_url)) {
                                $matewa_client_copy_url = 'https://' . ltrim($matewa_client_copy_url, '/');
                            }
                            $matewa_client_copy_input_id = '' !== $matewa_client_pack_id ? 'matewa-client-copy-url-' . $matewa_client_pack_id : 'matewa-client-copy-url-' . sanitize_key($matewa_client_name);
                            $matewa_client_review_flags = '' !== $matewa_client_pack_id && isset($matewa_agency_client_duplicate_flags[$matewa_client_pack_id]) && is_array($matewa_agency_client_duplicate_flags[$matewa_client_pack_id])
                                ? array_values($matewa_agency_client_duplicate_flags[$matewa_client_pack_id])
                                : array();
                            $matewa_client_setup_route_label = 'pdf_uploaded_review' === $matewa_client_status ? 'PDF uploaded; check fields' : 'Manual setup saved';
                            $matewa_client_export_label = 'pdf_uploaded_review' === $matewa_client_status ? 'Review and save before export/import' : 'Export client-site JSON ready';
                            $matewa_client_pdf_record = '' !== $matewa_client_pack_id && isset($matewa_agency_client_pdf_records[$matewa_client_pack_id]) && is_array($matewa_agency_client_pdf_records[$matewa_client_pack_id])
                                ? $matewa_agency_client_pdf_records[$matewa_client_pack_id]
                                : array();
                            $matewa_client_pdf_file_name = isset($matewa_client_pdf_record['file_name']) ? sanitize_file_name((string) $matewa_client_pdf_record['file_name']) : '';
                            $matewa_client_pdf_label = '' !== $matewa_client_pdf_file_name ? 'PDF uploaded: ' . $matewa_client_pdf_file_name : 'PDF review later';
                            ?>
                            <tr>
                                <td data-label="Client / domain">
                                    <strong><?php echo esc_html($matewa_client_name); ?></strong>
                                    <?php if ('' !== $matewa_client_copy_url) : ?>
                                        <label class="matewa-agency-client-copy-domain" for="<?php echo esc_attr($matewa_client_copy_input_id); ?>">
                                            <span>Copy client URL</span>
                                            <input id="<?php echo esc_attr($matewa_client_copy_input_id); ?>" type="text" readonly value="<?php echo esc_attr($matewa_client_copy_url); ?>" onfocus="this.select();" onclick="this.select();" aria-label="Copy client URL for <?php echo esc_attr($matewa_client_name); ?>">
                                        </label>
                                    <?php else : ?>
                                        <span>Domain needed</span>
                                    <?php endif; ?>
                                    <?php if ('' !== $matewa_client_pack_id) : ?>
                                        <span class="matewa-agency-client-pack-id"><?php echo esc_html('Pack: ' . $matewa_client_pack_id); ?></span>
                                    <?php endif; ?>
                                    <?php if ('' !== $matewa_client_admin_email || '' !== $matewa_client_leads_email) : ?>
                                        <span><?php echo '' !== $matewa_client_admin_email ? esc_html('Admin: ' . $matewa_client_admin_email) : esc_html('Admin email needed'); ?></span>
                                        <span><?php echo '' !== $matewa_client_leads_email ? esc_html('Leads: ' . $matewa_client_leads_email) : esc_html('Leads email needed'); ?></span>
                                    <?php endif; ?>
                                </td>
                                <td data-label="Licence"><span class="matewa-agency-table-pill is-ready">Seat assigned</span></td>
                                <td data-label="Expires"><?php echo esc_html($matewa_client_expiry_label); ?><br><span>Follows bundle renewal</span></td>
                                <td data-label="Pages">
                                    <?php echo esc_html($matewa_client_page_label); ?>
                                    <?php if ($matewa_client_detected_page_count > 0) : ?>
                                        <br><span><?php echo esc_html((string) $matewa_client_detected_page_count); ?> scanned</span>
                                    <?php endif; ?>
                                    <?php if ($matewa_client_selected_page_count > 0) : ?>
                                        <br><span><?php echo esc_html((string) $matewa_client_selected_page_count); ?> selected</span>
                                    <?php endif; ?>
                                </td>
                                <td data-label="Business type"><?php echo esc_html($matewa_client_type); ?><?php echo '' !== $matewa_client_category ? '<br><span>' . esc_html($matewa_client_category) . '</span>' : ''; ?></td>
                                <td data-label="Visitor buttons">
                                    <?php if (!empty($matewa_client_visitor_buttons)) : ?>
                                        <div class="matewa-agency-client-chip-summary" aria-label="Saved visitor buttons for <?php echo esc_attr($matewa_client_name); ?>">
                                            <?php foreach (array_slice($matewa_client_visitor_buttons, 0, 6) as $matewa_saved_chip_label) : ?>
                                                <span><?php echo esc_html((string) $matewa_saved_chip_label); ?></span>
                                            <?php endforeach; ?>
                                        </div>
                                    <?php else : ?>
                                        <?php echo esc_html($matewa_client_chip_label); ?>
                                    <?php endif; ?>
                                </td>
                                <td data-label="Status">
                                    <span class="matewa-agency-table-pill <?php echo esc_attr($matewa_client_status_class); ?>"><?php echo esc_html($matewa_client_status_label); ?></span>
                                    <br><span><?php echo esc_html($matewa_client_setup_route_label); ?></span>
                                    <br><span><?php echo esc_html($matewa_client_export_label); ?></span>
                                    <br><span><?php echo esc_html($matewa_client_pdf_label); ?></span>
                                    <?php if (!empty($matewa_client_review_flags)) : ?>
                                        <?php foreach ($matewa_client_review_flags as $matewa_client_review_flag) : ?>
                                            <br><span class="matewa-agency-client-row-note"><?php echo esc_html((string) $matewa_client_review_flag); ?></span>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                </td>
                                <td data-label="Actions">
                                    <div class="matewa-agency-client-row-actions">
                                        <a class="button button-secondary" href="<?php echo esc_url(add_query_arg(array('page' => '247chatmate', 'matewa_saved_section' => 'agency', 'matewa_agency_edit_pack' => $matewa_client_pack_id), admin_url('admin.php')) . '#matewa-agency-client-pack-editor'); ?>">Edit</a>
                                        <?php if (function_exists('matewa_agency_client_pack_scan_pages_url') && '' !== $matewa_client_pack_id) : ?>
                                            <a class="button button-secondary" href="<?php echo esc_url(matewa_agency_client_pack_scan_pages_url($matewa_client_pack_id)); ?>">Scan Pages</a>
                                        <?php endif; ?>
                                        <?php if (function_exists('matewa_agency_client_pack_export_url') && '' !== $matewa_client_pack_id) : ?>
                                            <a class="button button-secondary" href="<?php echo esc_url(matewa_agency_client_pack_export_url($matewa_client_pack_id)); ?>">Export JSON</a>
                                        <?php endif; ?>
                                        <button type="submit" class="button button-secondary is-danger" name="matewa_agency_client_pack_delete_id" value="<?php echo esc_attr($matewa_client_pack_id); ?>" onclick="return window.confirm('Delete this client setup pack and release its assigned seat?');">Delete</button>
                                    </div>
                                    <span class="matewa-agency-client-row-note">Setup JSON export available</span>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>


        <details id="matewa-agency-remote-push-test-panel" class="matewa-plan-access-section matewa-plan-access-section--support matewa-agency-remote-push-test-panel" aria-label="Local remote push test facility">
            <summary>
                <span>Open local setup push test</span>
                <small>Support/testing only</small>
            </summary>
            <div class="matewa-agency-remote-push-test-body">
                <div class="matewa-agency-client-pack-form-head">
                    <div>
                        <strong>Local setup push test</strong>
                        <span>Test the receive/import/key-consume flow on this WordPress site without installing the plugin on a fresh second site.</span>
                    </div>
                    <em>Loopback test</em>
                </div>
                <div class="matewa-agency-client-pack-grid is-details">
                    <label>
                        <span>Setup pack to test</span>
                        <select name="matewa_agency_remote_push_test_pack_id" form="matewa-agency-remote-push-local-test-form" <?php echo empty($matewa_agency_client_pack_records) ? 'disabled' : ''; ?>>
                            <?php if (empty($matewa_agency_client_pack_records)) : ?>
                                <option value="">Create a setup pack first</option>
                            <?php else : ?>
                                <option value="">Choose saved setup pack</option>
                                <?php foreach ($matewa_agency_client_pack_records as $matewa_test_pack) : ?>
                                    <?php
                                    $matewa_test_pack_id = isset($matewa_test_pack['setup_pack_id']) ? sanitize_key((string) $matewa_test_pack['setup_pack_id']) : '';
                                    $matewa_test_client = isset($matewa_test_pack['client_business']) ? sanitize_text_field((string) $matewa_test_pack['client_business']) : 'Client';
                                    $matewa_test_domain = isset($matewa_test_pack['client_domain']) ? sanitize_text_field((string) $matewa_test_pack['client_domain']) : '';
                                    ?>
                                    <?php if ('' !== $matewa_test_pack_id) : ?>
                                        <option value="<?php echo esc_attr($matewa_test_pack_id); ?>"><?php echo esc_html($matewa_test_client . ('' !== $matewa_test_domain ? ' — ' . $matewa_test_domain : '') . ' · ' . $matewa_test_pack_id); ?></option>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                            <?php endif; ?>
                        </select>
                    </label>
                </div>
                <div class="matewa-agency-client-pack-actions">
                    <button type="submit" class="button button-primary" form="matewa-agency-remote-push-local-test-form" <?php echo empty($matewa_agency_client_pack_records) ? 'disabled' : ''; ?>>Run local test push</button>
                    <small>This creates a temporary local setup key, imports the selected pack into this plugin, consumes the key, and records the audit result. It skips only the external HTTPS transport step.</small>
                </div>
            </div>
        </details>

        <?php if (function_exists('matewa_agency_client_import_render_setup_panel')) : ?>
            <details id="matewa-agency-client-import-fallback" class="matewa-plan-access-section matewa-plan-access-section--support matewa-agency-client-import-fallback" aria-label="Manual client setup JSON fallback" <?php echo !empty($matewa_agency_client_import_open) ? 'open' : ''; ?>>
                <summary>
                    <span>Open manual JSON import fallback</span>
                    <small>Use only when remote push is not available</small>
                </summary>
                <div class="matewa-agency-client-import-fallback-body">
                    <?php matewa_agency_client_import_render_setup_panel(); ?>
                </div>
            </details>
        <?php endif; ?>


        <div class="matewa-agency-client-table-wrap matewa-agency-performance-table-wrap" aria-label="Agency client performance board">
            <table class="matewa-agency-client-table matewa-agency-performance-table">
                <thead>
                    <tr>
                        <th scope="col">Client</th>
                        <th scope="col">Reporting</th>
                        <th scope="col">Leads</th>
                        <th scope="col">WA handoffs</th>
                        <th scope="col">Incomplete</th>
                        <th scope="col">Setup route</th>
                        <th scope="col">Review</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($matewa_agency_client_pack_records)) : ?>
                        <tr>
                            <td data-label="Client"><strong>No client packs yet</strong></td>
                            <td data-label="Reporting">Create a setup pack first</td>
                            <td data-label="Leads">—</td>
                            <td data-label="WA handoffs">—</td>
                            <td data-label="Incomplete">—</td>
                            <td data-label="Setup route">Manual/PDF later</td>
                            <td data-label="Review">Waiting</td>
                        </tr>
                    <?php else : ?>
                        <?php foreach ($matewa_agency_client_pack_records as $matewa_client_pack) : ?>
                            <?php
                            $matewa_perf_client_name = isset($matewa_client_pack['client_business']) ? sanitize_text_field((string) $matewa_client_pack['client_business']) : 'Client';
                            $matewa_perf_pack_id = isset($matewa_client_pack['setup_pack_id']) ? sanitize_key((string) $matewa_client_pack['setup_pack_id']) : '';
                            $matewa_perf_leads_email = isset($matewa_client_pack['leads_email']) ? sanitize_email((string) $matewa_client_pack['leads_email']) : '';
                            $matewa_perf_flags = '' !== $matewa_perf_pack_id && isset($matewa_agency_client_duplicate_flags[$matewa_perf_pack_id]) && is_array($matewa_agency_client_duplicate_flags[$matewa_perf_pack_id])
                                ? array_values($matewa_agency_client_duplicate_flags[$matewa_perf_pack_id])
                                : array();
                            $matewa_perf_pdf_record = '' !== $matewa_perf_pack_id && isset($matewa_agency_client_pdf_records[$matewa_perf_pack_id]) && is_array($matewa_agency_client_pdf_records[$matewa_perf_pack_id])
                                ? $matewa_agency_client_pdf_records[$matewa_perf_pack_id]
                                : array();
                            $matewa_perf_pdf_label = !empty($matewa_perf_pdf_record['file_name']) ? 'PDF uploaded' : 'PDF review later';
                            ?>
                            <tr>
                                <td data-label="Client"><strong><?php echo esc_html($matewa_perf_client_name); ?></strong><?php echo '' !== $matewa_perf_pack_id ? '<br><span>' . esc_html($matewa_perf_pack_id) . '</span>' : ''; ?></td>
                                <td data-label="Reporting"><span class="matewa-agency-table-pill <?php echo is_email($matewa_perf_leads_email) ? esc_attr('is-ready') : esc_attr('is-warning'); ?>"><?php echo is_email($matewa_perf_leads_email) ? esc_html('Client email ready') : esc_html('Needs leads email'); ?></span><br><span>Weekly summary wiring later</span></td>
                                <td data-label="Leads">0<br><span>Client install pending</span></td>
                                <td data-label="WA handoffs">0<br><span>Client install pending</span></td>
                                <td data-label="Incomplete">0<br><span>Client install pending</span></td>
                                <td data-label="Setup route">Manual saved<br><span><?php echo esc_html('Export client-site JSON ready'); ?></span><br><span><?php echo esc_html($matewa_perf_pdf_label); ?></span></td>
                                <td data-label="Review">
                                    <?php if (!empty($matewa_perf_flags)) : ?>
                                        <span class="matewa-agency-table-pill is-warning"><?php echo esc_html((string) count($matewa_perf_flags)); ?> flag<?php echo 1 === count($matewa_perf_flags) ? '' : 's'; ?></span>
                                    <?php else : ?>
                                        <span class="matewa-agency-table-pill is-ready">No local flags</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <p class="matewa-agency-client-board-note">Manual setup packs are saved locally. Export JSON remains the permanent fallback; remote push uses the same payload and only works when the client plugin has generated a valid one-time setup key.</p>
    </section>
