<section class="cards four">
    <div class="card stat"><h3>App Version</h3><p>V<?= e($appVersion) ?></p><span>Code package</span></div>
    <div class="card stat"><h3>Schema Version</h3><p><?= e($schemaVersion) ?></p><span><?= $schemaMatches ? 'Aligned' : 'Mismatch' ?></span></div>
    <div class="card stat"><h3>Diagnostics</h3><p><a class="btn secondary" href="index.php?route=settings/diagnostics">Open Panel</a></p><span>Logs, queue, dead letters</span></div>
    <div class="card stat"><h3>Safe Deploy &amp; Rollback</h3><p><a class="btn secondary" href="install.php#rollback-center">Open Center</a></p><span>Preview packages, deploy through health gates, review snapshots, and roll back code safely</span></div>
</section>

<section class="card" style="margin-top:18px">
    <div class="toolbar"><h2>Safe Deploy &amp; Rollback Center</h2><span class="badge">In-app deploys</span></div>
    <p class="muted">Preview packages before apply, deploy through Safe Deploy health gates, review automatic safety snapshots, and restore a prior code version if a release behaves badly.</p>
    <p><a class="btn secondary" href="install.php#update-packages">Open Safe Deploy</a> <a class="btn secondary" href="install.php#rollback-center">Open Rollback Center</a></p>
</section>

<section class="grid two">
    <div class="card">
        <h2>Application Settings</h2>
        <p class="muted">These values support branding and message defaults.</p>
        <form method="post" action="index.php?route=settings/save">
            <?= csrf_input() ?>
            <div class="form-grid">
                <input name="company_name" placeholder="Company Name" value="<?= e($settings['company_name'] ?? '') ?>">
                <input name="brand_color" placeholder="Brand Color" value="<?= e($settings['brand_color'] ?? '#12395b') ?>">
                <input name="default_sms_footer" placeholder="Default SMS Footer" value="<?= e($settings['default_sms_footer'] ?? '') ?>">
                <input name="default_email_footer" placeholder="Default Email Footer" value="<?= e($settings['default_email_footer'] ?? '') ?>">
                <input name="reply_to_email" placeholder="Reply-To Email" value="<?= e($settings['reply_to_email'] ?? '') ?>">
                <select name="preferred_channel">
                    <option value="email" <?= ($settings['preferred_channel'] ?? 'email') === 'email' ? 'selected' : '' ?>>Default to Email</option>
                    <option value="sms" <?= ($settings['preferred_channel'] ?? '') === 'sms' ? 'selected' : '' ?>>Default to SMS</option>
                </select>
            </div>
            <button class="btn primary">Save Settings</button>
        </form>
    </div>
    <div class="card">
        <h2>Create User</h2>
        <form method="post" action="index.php?route=settings/createUser">
            <?= csrf_input() ?>
            <div class="form-grid">
                <input name="name" placeholder="Full Name" required>
                <input name="email" type="email" placeholder="Email Address" required>
                <input name="password" type="password" placeholder="Temporary Password" required>
                <select name="role">
                    <option value="admin">Admin</option>
                    <option value="manager">Manager</option>
                    <option value="agent">Agent</option>
                </select>
            </div>
            <button class="btn secondary">Create User</button>
        </form>
    </div>
</section>

<section class="card">
    <div class="toolbar"><h2>Integrations</h2><span class="badge">Global Defaults</span></div>
    <p class="muted">These values are editable in the admin panel and act as the system fallback for all users. Personal user profiles can override them.</p>
    <form method="post" action="index.php?route=settings/saveIntegrations">
        <?= csrf_input() ?>
        <div class="grid two">
            <div>
                <h3>Twilio</h3>
                <div class="form-grid">
                    <input name="twilio_sid" placeholder="Twilio SID" value="<?= e($settings['twilio_sid'] ?? '') ?>">
                    <input name="twilio_token" placeholder="Twilio Auth Token" value="<?= e($settings['twilio_token'] ?? '') ?>">
                    <input name="twilio_from_number" placeholder="Twilio From Number" value="<?= e($settings['twilio_from_number'] ?? '') ?>">
                    <select name="twilio_validate_signature">
                        <option value="1" <?= ($settings['twilio_validate_signature'] ?? '1') === '1' ? 'selected' : '' ?>>Validate Webhook Signatures</option>
                        <option value="0" <?= ($settings['twilio_validate_signature'] ?? '') === '0' ? 'selected' : '' ?>>Disable Validation</option>
                    </select>
                </div>
                <h3>Email Delivery</h3>
                <div class="form-grid">
                    <input name="mail_from_name" placeholder="From Name" value="<?= e($settings['mail_from_name'] ?? '') ?>">
                    <input name="mail_from_email" placeholder="From Email" value="<?= e($settings['mail_from_email'] ?? '') ?>">
                    <select name="mail_transport">
                        <?php $mailTransport = $settings['mail_transport'] ?? (($settings['mail_smtp_enabled'] ?? '1') === '1' ? 'smtp' : 'php_mail'); ?>
                        <option value="sendgrid_api" <?= $mailTransport === 'sendgrid_api' ? 'selected' : '' ?>>SendGrid API</option>
                        <option value="smtp" <?= $mailTransport === 'smtp' ? 'selected' : '' ?>>SMTP</option>
                        <option value="php_mail" <?= $mailTransport === 'php_mail' ? 'selected' : '' ?>>PHP mail()</option>
                    </select>
                    <input name="mail_sendgrid_api_key" placeholder="SendGrid API Key" value="<?= e($settings['mail_sendgrid_api_key'] ?? '') ?>">
                    <input name="mail_sendgrid_api_base_url" placeholder="SendGrid API Base URL" value="<?= e($settings['mail_sendgrid_api_base_url'] ?? 'https://api.sendgrid.com') ?>">
                    <input name="mail_sendgrid_webhook_token" placeholder="SendGrid Webhook Token" value="<?= e($settings['mail_sendgrid_webhook_token'] ?? '') ?>">
                    <input name="mail_smtp_host" placeholder="SMTP Host" value="<?= e($settings['mail_smtp_host'] ?? '') ?>">
                    <input name="mail_smtp_port" placeholder="SMTP Port" value="<?= e($settings['mail_smtp_port'] ?? '587') ?>">
                    <input name="mail_smtp_username" placeholder="SMTP Username" value="<?= e($settings['mail_smtp_username'] ?? '') ?>">
                    <input name="mail_smtp_password" placeholder="SMTP Password" value="<?= e($settings['mail_smtp_password'] ?? '') ?>">
                    <select name="mail_smtp_secure">
                        <option value="tls" <?= ($settings['mail_smtp_secure'] ?? 'tls') === 'tls' ? 'selected' : '' ?>>TLS</option>
                        <option value="ssl" <?= ($settings['mail_smtp_secure'] ?? '') === 'ssl' ? 'selected' : '' ?>>SSL</option>
                        <option value="none" <?= ($settings['mail_smtp_secure'] ?? '') === 'none' ? 'selected' : '' ?>>None</option>
                    </select>
                    <input name="reply_to_email" placeholder="Reply-To Email" value="<?= e($settings['reply_to_email'] ?? '') ?>">
                </div>
                <p class="muted small">Use <strong>SendGrid API</strong> to bypass host-level SMTP rewriting and certificate mismatches. SMTP fields are only used when the SMTP transport is selected.</p>
                <p class="muted small">If you enable the SendGrid Event Webhook, point it to <code><?= e(sendgrid_webhook_url(['sendgrid_webhook_token' => $settings['mail_sendgrid_webhook_token'] ?? ''])) ?></code>. Add a webhook token here to require the same token on inbound event posts.</p>
            </div>
            <div>
                <h3>IMAP</h3>
                <div class="form-grid">
                    <input name="imap_host" placeholder="IMAP Host" value="<?= e($settings['imap_host'] ?? '') ?>">
                    <input name="imap_port" placeholder="IMAP Port" value="<?= e($settings['imap_port'] ?? '993') ?>">
                    <input name="imap_flags" placeholder="IMAP Flags" value="<?= e($settings['imap_flags'] ?? '/imap/ssl') ?>">
                    <input name="imap_username" placeholder="IMAP Username" value="<?= e($settings['imap_username'] ?? '') ?>">
                    <input name="imap_password" placeholder="IMAP Password" value="<?= e($settings['imap_password'] ?? '') ?>">
                    <input name="imap_mailbox" placeholder="Mailbox" value="<?= e($settings['imap_mailbox'] ?? 'INBOX') ?>">
                </div>
                <p class="muted small">Use this section to change live mail and Twilio integration values without editing PHP config files.</p>
                <p><a class="btn secondary small" href="index.php?route=settings/profile">Open My Integration Profile</a></p>
            </div>
        </div>
        <button class="btn primary">Save Integrations</button>
    </form>
</section>

<section class="card">
    <div class="toolbar"><h2>User Access Overrides</h2><span class="badge">V8</span></div>
    <table>
        <thead><tr><th>User</th><th>Role</th><th>Access Override</th></tr></thead>
        <tbody>
        <?php foreach ($users as $user): ?>
            <tr>
                <td>
                    <strong><?= e($user['name']) ?></strong><br>
                    <span class="muted small"><?= e($user['email']) ?></span>
                </td>
                <td><?= e($user['role']) ?></td>
                <td>
                    <form method="post" action="index.php?route=settings/savePermissions">
                        <?= csrf_input() ?>
                        <input type="hidden" name="user_id" value="<?= (int)$user['id'] ?>">
                        <div class="permissions-grid">
                            <?php foreach ($allPermissions as $permission): ?>
                                <label class="permission-pill"><input type="checkbox" name="permissions[]" value="<?= e($permission) ?>" <?= !empty($user['permission_overrides'][$permission]) ? 'checked' : '' ?>> <span><?= e($permission) ?></span></label>
                            <?php endforeach; ?>
                        </div>
                        <button class="btn secondary small">Save Access</button>
                    </form>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</section>

<section class="grid two">
    <div class="card">
        <h2>Add Suppression</h2>
        <form method="post" action="index.php?route=settings/addSuppression">
            <?= csrf_input() ?>
            <div class="form-grid">
                <select name="channel"><option value="email">Email</option><option value="sms">SMS</option></select>
                <input name="address" placeholder="Address or phone" required>
                <input name="reason" placeholder="Reason (bounce, complaint, manual)">
            </div>
            <button class="btn primary">Add Suppression</button>
        </form>
    </div>
    <div class="card">
        <h2>Existing Users</h2>
        <table>
            <thead><tr><th>Name</th><th>Email</th><th>Role</th><th>2FA</th><th>Active</th><th>Created</th></tr></thead>
            <tbody>
            <?php foreach ($users as $user): ?>
                <tr>
                    <td><?= e($user['name']) ?></td>
                    <td><?= e($user['email']) ?></td>
                    <td><?= e($user['role']) ?></td>
                    <td><?= !empty($user['two_factor_enabled']) ? 'Enabled' : 'Off' ?></td>
                    <td><?= (int)$user['is_active'] ?></td>
                    <td><?= e($user['created_at']) ?></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</section>

<section class="card">
    <h2>Suppression List</h2>
    <table>
        <thead><tr><th>Channel</th><th>Address</th><th>Reason</th><th>Added</th><th></th></tr></thead>
        <tbody>
        <?php foreach ($suppressions as $row): ?>
            <tr>
                <td><?= e(strtoupper($row['channel'])) ?></td>
                <td><?= e($row['address']) ?></td>
                <td><?= e($row['reason']) ?></td>
                <td><?= e($row['created_at']) ?></td>
                <td><a class="btn danger small" href="index.php?route=settings/deleteSuppression&id=<?= (int)$row['id'] ?>" onclick="return confirm('Remove suppression?')">Delete</a></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
</section>


<section class="grid two">
    <div class="card">
        <h2>Contact Custom Fields</h2>
        <table>
            <thead><tr><th>Name</th><th>Key</th><th>Type</th><th></th></tr></thead>
            <tbody>
            <?php foreach ($customFields as $field): ?>
                <tr>
                    <td><?= e($field['name']) ?></td>
                    <td><code><?= e($field['field_key']) ?></code></td>
                    <td><?= e($field['field_type']) ?></td>
                    <td><a class="btn danger small" href="index.php?route=settings/deleteCustomField&id=<?= (int)$field['id'] ?>" onclick="return confirm('Delete custom field?')">Delete</a></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <div class="card">
        <h2>Add Custom Field</h2>
        <form method="post" action="index.php?route=settings/saveCustomField">
            <?= csrf_input() ?>
            <div class="form-grid">
                <input name="name" placeholder="Display name" required>
                <input name="field_key" placeholder="field_key" required>
                <select name="field_type">
                    <option value="text">Text</option>
                    <option value="textarea">Textarea</option>
                    <option value="number">Number</option>
                    <option value="date">Date</option>
                </select>
                <input name="sort_order" type="number" placeholder="Sort order" value="0">
            </div>
            <button class="btn primary">Save Custom Field</button>
        </form>
    </div>
</section>

<section class="grid two">
    <div class="card">
        <div class="toolbar"><h2>Workflow Automations</h2><span class="badge"><?= (int)($workflowStats['active_count'] ?? 0) ?> active</span></div>
        <table>
            <thead><tr><th>Name</th><th>Trigger</th><th>Channel</th><th>Delay</th><th>Status</th><th></th></tr></thead>
            <tbody>
            <?php foreach ($workflows as $workflow): ?>
                <tr>
                    <td><?= e($workflow['name']) ?></td>
                    <td><?= e($workflow['trigger_event']) ?></td>
                    <td><?= e(strtoupper($workflow['channel'])) ?></td>
                    <td><?= (int)$workflow['delay_minutes'] ?> min</td>
                    <td><?= !empty($workflow['is_active']) ? 'Active' : 'Paused' ?></td>
                    <td><a class="btn secondary small" href="index.php?route=settings/toggleWorkflow&id=<?= (int)$workflow['id'] ?>">Toggle</a></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <div class="card">
        <h2>Create Workflow</h2>
        <form method="post" action="index.php?route=settings/createWorkflow">
            <?= csrf_input() ?>
            <div class="form-grid">
                <input name="name" placeholder="Workflow name" required>
                <select name="trigger_event"><option value="contact_created">Contact created</option><option value="tag_added">Tag added</option></select>
                <select name="channel"><option value="email">Email</option><option value="sms">SMS</option></select>
                <select name="template_id"><option value="">Template</option><?php foreach ($templates as $template): ?><option value="<?= (int)$template['id'] ?>"><?= e($template['name']) ?></option><?php endforeach; ?></select>
                <input name="delay_minutes" type="number" value="0" min="0" placeholder="Delay minutes">
                <select name="target_segment_id"><option value="">Any audience</option><?php foreach ($segments as $segment): ?><option value="<?= (int)$segment['id'] ?>"><?= e($segment['name']) ?></option><?php endforeach; ?></select>
                <input name="tag_filter" placeholder="Optional tag filter">
                <input name="company_filter" placeholder="Optional company filter">
                <select name="lifecycle_stage"><option value="">Any stage</option><option value="lead">Lead</option><option value="qualified">Qualified</option><option value="proposal">Proposal</option><option value="customer">Customer</option></select>
                <input name="minimum_score" type="number" value="0" min="0" placeholder="Minimum score">
            </div>
            <button class="btn primary">Save Workflow</button>
        </form>
    </div>
</section>

<section class="grid two">
    <div class="card">
        <h2>Canned Replies Library</h2>
        <table>
            <thead><tr><th>Title</th><th>Channel</th><th>Subject</th><th></th></tr></thead>
            <tbody>
            <?php foreach ($cannedReplies as $reply): ?>
                <tr>
                    <td><?= e($reply['title']) ?></td>
                    <td><?= e(strtoupper($reply['channel'])) ?></td>
                    <td><?= e($reply['subject'] ?: '—') ?></td>
                    <td><a class="btn danger small" href="index.php?route=settings/deleteCannedReply&id=<?= (int)$reply['id'] ?>">Delete</a></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <div class="card">
        <h2>Add Canned Reply</h2>
        <form method="post" action="index.php?route=settings/saveCannedReply">
            <?= csrf_input() ?>
            <div class="form-grid">
                <input name="title" placeholder="Reply title" required>
                <select name="channel"><option value="email">Email</option><option value="sms">SMS</option></select>
                <input name="subject" placeholder="Subject (email only)">
                <textarea name="body" rows="6" placeholder="Reply text" required></textarea>
            </div>
            <button class="btn primary">Save Canned Reply</button>
        </form>
    </div>
</section>

<section class="grid two">
    <div class="card">
        <div class="toolbar"><h2>Operations Pack</h2><span class="badge">Addon</span></div>
        <div class="grid two">
            <div><div class="small muted">Worker heartbeat</div><strong><?= e(($heartbeat['status_label'] ?? 'Unknown')) ?></strong></div>
            <div><div class="small muted">Provider health</div><strong><?= ($providerStatus['twilio']['configured'] ? 'Twilio OK' : 'Twilio Missing') ?> · <?= ($providerStatus['smtp']['configured'] ? 'Mail OK' : 'Mail Missing') ?></strong></div>
        </div>
        <p class="small muted">Queue monitor, worker health, dead-letter replay, diagnostics logs, and provider status are available from the diagnostics page.</p>
        <p><a class="btn secondary small" href="index.php?route=settings/diagnostics">Open Diagnostics</a> <a class="btn secondary small" href="index.php?route=settings/providerStatus">Provider Status Details</a></p>
    </div>
    <div class="card">
        <h2>Provider Status</h2>
        <table><tbody>
            <tr><th>Twilio</th><td><?= $providerStatus['twilio']['configured'] ? 'Configured' : 'Missing' ?></td><td><?= e($providerStatus['twilio']['from']) ?></td></tr>
            <tr><th>Mail Delivery</th><td><?= $providerStatus['smtp']['configured'] ? 'Configured' : 'Missing' ?></td><td><?= e(($providerStatus['smtp']['label'] ?? 'Mail') . ' · ' . ($providerStatus['smtp']['from'] ?? '')) ?></td></tr>
            <tr><th>IMAP</th><td><?= $providerStatus['imap']['configured'] ? 'Configured' : 'Missing' ?></td><td><?= e($providerStatus['imap']['mailbox']) ?></td></tr>
        </tbody></table>
    </div>
</section>

<section class="grid two">
    <div class="card">
        <h2>Sales/CRM Pack · Accounts</h2>
        <table><thead><tr><th>Name</th><th>Owner</th><th>Contacts</th></tr></thead><tbody>
        <?php foreach ($accounts as $account): ?><tr><td><?= e($account['name']) ?></td><td><?= e($account['owner_name'] ?: '—') ?></td><td><?= (int)($account['contact_count'] ?? 0) ?></td></tr><?php endforeach; ?>
        </tbody></table>
        <form method="post" action="index.php?route=settings/createAccount">
            <?= csrf_input() ?>
            <div class="form-grid">
                <input name="name" placeholder="Account name" required>
                <input name="industry" placeholder="Industry">
                <input name="website" placeholder="Website">
                <select name="owner_user_id"><option value="">Owner</option><?php foreach ($users as $user): ?><option value="<?= (int)$user['id'] ?>"><?= e($user['name']) ?></option><?php endforeach; ?></select>
            </div>
            <button class="btn primary">Add Account</button>
        </form>
    </div>
    <div class="card">
        <h2>Pipelines + Reminders</h2>
        <table><thead><tr><th>Pipeline</th><th>Contacts</th></tr></thead><tbody>
        <?php foreach ($pipelines as $pipeline): ?><tr><td><?= e($pipeline['name']) ?></td><td><?= (int)($pipeline['contact_count'] ?? 0) ?></td></tr><?php endforeach; ?>
        </tbody></table>
        <form method="post" action="index.php?route=settings/createPipeline">
            <?= csrf_input() ?>
            <div class="form-grid">
                <input name="name" placeholder="Pipeline name" required>
                <input name="stage_order_csv" placeholder="lead,qualified,proposal,customer">
                <input name="sort_order" type="number" value="0">
                <label><input type="checkbox" name="is_active" value="1" checked> Active</label>
            </div>
            <button class="btn primary">Add Pipeline</button>
        </form>
        <hr>
        <div class="small muted">Upcoming reminders</div>
        <?php foreach ($reminders as $reminder): ?><div class="small"><?= e(($reminder['first_name'] ?? '') . ' ' . ($reminder['last_name'] ?? '')) ?> · <?= e($reminder['title']) ?> · <?= e($reminder['remind_at']) ?></div><?php endforeach; ?>
    </div>
</section>

<section class="grid two">
    <div class="card">
        <h2>Compliance Pack</h2>
        <form method="post" action="index.php?route=settings/saveComplianceSettings">
            <?= csrf_input() ?>
            <div class="form-grid">
                <label><input type="checkbox" name="quiet_hours_enabled" value="1" <?= !empty($quietHours['enabled']) ? 'checked' : '' ?>> Quiet hours enabled</label>
                <input name="quiet_hours_start" type="time" value="<?= e($quietHours['start']) ?>">
                <input name="quiet_hours_end" type="time" value="<?= e($quietHours['end']) ?>">
                <input name="quiet_hours_timezone" value="<?= e($quietHours['timezone']) ?>" placeholder="Timezone">
                <label><input type="checkbox" name="suppression_automation_enabled" value="1" <?= !empty($quietHours['suppression_automation_enabled']) ? 'checked' : '' ?>> Auto-suppress bounce/complaint events</label>
            </div>
            <button class="btn primary">Save Compliance Settings</button>
        </form>
        <p><a class="btn secondary small" href="index.php?route=settings/exportAuditCsv">Export Audit CSV</a></p>
    </div>
    <div class="card">
        <h2>Channel-Level DNC Controls</h2>
        <p class="small muted">Use the contact profile and suppression list together for channel-level do-not-contact controls. SMS and email opt-out status are stored separately.</p>
        <table><thead><tr><th>Channel</th><th>What it controls</th></tr></thead><tbody>
            <tr><td>SMS</td><td>Quiet hours, STOP opt-outs, suppression list, contact unsubscribed_sms flag</td></tr>
            <tr><td>Email</td><td>Unsubscribe link, bounce/complaint suppression, contact unsubscribed_email flag</td></tr>
        </tbody></table>
    </div>
</section>


<section class="grid two">
    <div class="card">
        <div class="toolbar"><h2>Connector Marketplace</h2><span class="badge">V16.4</span></div>
        <form method="post" action="index.php?route=settings/saveConnector">
            <?= csrf_input() ?>
            <div class="form-grid">
                <select name="connector_key">
                    <?php foreach (($connectorCatalog ?? []) as $connector): ?>
                        <option value="<?= e($connector['key']) ?>"><?= e($connector['name']) ?> — <?= e($connector['scope']) ?></option>
                    <?php endforeach; ?>
                </select>
                <input name="name" placeholder="Display name" required>
                <input name="base_url" placeholder="Base URL / tenant URL">
                <input name="auth_type" placeholder="Auth type" value="oauth2">
                <input name="access_token" placeholder="Access token / secret">
                <input name="refresh_token" placeholder="Refresh token (optional)">
                <input name="config_json" placeholder='JSON config or scope notes'>
                <label class="permission-pill"><input type="checkbox" name="is_active" value="1" checked> <span>Active</span></label>
            </div>
            <button class="btn primary">Save Connector</button>
        </form>
        <table class="mt-12">
            <thead><tr><th>Name</th><th>Key</th><th>Auth</th><th>Status</th><th></th></tr></thead>
            <tbody>
            <?php foreach (($connectors ?? []) as $connector): ?>
                <tr>
                    <td><?= e($connector['name']) ?></td>
                    <td><?= e($connector['connector_key']) ?></td>
                    <td><?= e($connector['auth_type']) ?></td>
                    <td><?= !empty($connector['is_active']) ? 'Active' : 'Paused' ?></td>
                    <td><a class="btn danger small" href="index.php?route=settings/deleteConnector&id=<?= (int)$connector['id'] ?>" onclick="return confirm('Delete connector?')">Delete</a></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($connectors)): ?><tr><td colspan="5" class="muted">No marketplace connectors configured yet.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
    <div class="card">
        <div class="toolbar"><h2>Sender Profiles</h2><span class="badge">Aliases</span></div>
        <form method="post" action="index.php?route=settings/saveSenderProfile">
            <?= csrf_input() ?>
            <div class="form-grid">
                <input name="display_name" placeholder="Display name" required>
                <input name="alias_email" placeholder="Alias email" required>
                <input name="reply_to_email" placeholder="Reply-to email">
                <input name="signature_html" placeholder="Signature HTML or text">
                <label class="permission-pill"><input type="checkbox" name="is_active" value="1" checked> <span>Active</span></label>
            </div>
            <button class="btn primary">Save Sender Profile</button>
        </form>
        <table class="mt-12">
            <thead><tr><th>Display name</th><th>Alias</th><th>Reply-To</th><th>Status</th><th></th></tr></thead>
            <tbody>
            <?php foreach (($senderProfiles ?? []) as $profile): ?>
                <tr>
                    <td><?= e($profile['display_name']) ?></td>
                    <td><?= e($profile['alias_email']) ?></td>
                    <td><?= e($profile['reply_to_email'] ?? '—') ?></td>
                    <td><?= !empty($profile['is_active']) ? 'Active' : 'Paused' ?></td>
                    <td><a class="btn danger small" href="index.php?route=settings/deleteSenderProfile&id=<?= (int)$profile['id'] ?>" onclick="return confirm('Delete sender profile?')">Delete</a></td>
                </tr>
            <?php endforeach; ?>
            <?php if (empty($senderProfiles)): ?><tr><td colspan="5" class="muted">No sender profiles configured yet.</td></tr><?php endif; ?>
            </tbody>
        </table>
    </div>
</section>
