<?php
$errors = session()->getFlashdata('errors') ?? [];
$old = session()->getFlashdata('_ci_old_input') ?? [];
$oldPost = $old['post'] ?? [];
?>

<div class="space-y-6 max-w-7xl">
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
        <div class="flex items-center gap-3">
            <a href="<?= site_url('/admin/leads') ?>" class="p-2 rounded-xl bg-white border border-slate-200 text-slate-500 hover:text-slate-800 hover:bg-slate-50 transition">
                <svg xmlns="http://www.w3.org/2000/svg" class="w-4.5 h-4.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M15 19l-7-7 7-7" />
                </svg>
            </a>
            <div>
                <h3 class="text-lg font-bold text-slate-800"><?= esc($lead['name']) ?></h3>
                <p class="text-sm text-slate-500"><?= esc($lead['lead_number']) ?></p>
            </div>
        </div>
        <div class="flex items-center gap-2">
            <a href="<?= esc($agentBookUrl ?? site_url('/admin/leads/book/' . (int)($lead['id'] ?? 0))) ?>" class="inline-flex items-center gap-2 px-4 py-2.5 text-sm font-semibold text-white bg-gradient-to-r from-purple-600 to-indigo-600 rounded-xl shadow-md shadow-purple-500/20 hover:shadow-lg hover:-translate-y-0.5 transition-all">
                <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M6.75 3v2.25M17.25 3v2.25M3 18.75V7.5a2.25 2.25 0 012.25-2.25h13.5A2.25 2.25 0 0121 7.5v11.25m-18 0A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75m-18 0v-7.5A2.25 2.25 0 015.25 9h13.5A2.25 2.25 0 0121 11.25v7.5m-9-6h.008v.008H12v-.008zM12 15h.008v.008H12V15zm0 2.25h.008v.008H12v-.008zM9.75 15h.008v.008H9.75V15zm0 2.25h.008v.008H9.75v-.008zM7.5 15h.008v.008H7.5V15zm0 2.25h.008v.008H7.5v-.008zm6.75-4.5h.008v.008h-.008v-.008zm0 2.25h.008v.008h-.008V15zm0 2.25h.008v.008h-.008v-.008zm2.25-4.5h.008v.008H16.5v-.008zm0 2.25h.008v.008H16.5V15z" />
                </svg>
                Book Assessment
            </a>
            <button type="button" id="profile-call-btn"
                    class="inline-flex items-center gap-2 px-4 py-2.5 text-sm font-semibold text-white bg-gradient-to-r from-emerald-500 to-teal-600 rounded-xl shadow-md shadow-emerald-500/20 hover:shadow-lg hover:-translate-y-0.5 transition-all <?= empty($cloudtalkConfigured) ? 'opacity-60 cursor-not-allowed' : '' ?>"
                    data-lead-id="<?= (int) $lead['id'] ?>"
                    data-lead-phone="<?= esc($lead['phone']) ?>"
                    data-lead-name="<?= esc($lead['name']) ?>"
                    data-lead-email="<?= esc($lead['email'] ?? '') ?>"
                    data-lead-company="<?= esc($lead['company_name'] ?? '') ?>"
                    <?= empty($lead['phone']) ? 'disabled' : '' ?>>
                <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z" />
                </svg>
                Call Lead
            </button>
            <a href="<?= site_url('/admin/leads') ?>" class="inline-flex items-center gap-2 px-4 py-2.5 text-sm font-semibold text-slate-600 bg-white border border-slate-200 rounded-xl hover:bg-slate-50 transition">
                <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M4 6h16M4 10h16M4 14h16M4 18h16" />
                </svg>
                Back to List
            </a>
            <a href="<?= site_url('/admin/leads/edit/' . $lead['id']) ?>" class="inline-flex items-center gap-2 px-4 py-2.5 text-sm font-semibold text-white bg-gradient-to-r from-blue-600 to-indigo-600 rounded-xl shadow-md hover:shadow-lg hover:-translate-y-0.5 transition-all">
                <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
                </svg>
                Edit Lead
            </a>
        </div>
    </div>

    <?php if (!empty($errors)): ?>
        <div class="glass rounded-2xl border border-red-200/70 bg-red-50/50 p-5">
            <ul class="list-disc list-inside text-sm text-red-700 space-y-0.5">
                <?php foreach ($errors as $e): ?>
                    <li><?= esc($e) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <div class="lg:col-span-2 space-y-6">
            <div class="glass rounded-2xl p-6 shadow-sm border border-slate-200/60">
                <div class="flex flex-col sm:flex-row sm:items-start gap-5 pb-6 border-b border-slate-100">
                    <div class="w-20 h-20 rounded-2xl bg-gradient-to-br from-blue-500 via-indigo-500 to-purple-600 flex items-center justify-center text-white font-bold text-2xl shadow-lg flex-shrink-0">
                        <?= strtoupper(substr(esc($lead['name']), 0, 1)) ?>
                    </div>
                    <div class="flex-1 min-w-0">
                        <div class="flex flex-wrap items-center gap-3 mb-2">
                            <h3 class="text-xl font-bold text-slate-800"><?= esc($lead['name']) ?></h3>
                            <?php if (!empty($lead['status_name'])): ?>
                                <span class="inline-flex items-center gap-1.5 px-3 py-1 rounded-full text-xs font-semibold" style="background: <?= esc($lead['status_color'] ?? '#3b82f6') ?>20; color: <?= esc($lead['status_color'] ?? '#3b82f6') ?>;">
                                    <span class="w-1.5 h-1.5 rounded-full" style="background: <?= esc($lead['status_color'] ?? '#3b82f6') ?>;"></span>
                                    <?= esc($lead['status_name']) ?>
                                </span>
                            <?php else: ?>
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-semibold bg-slate-100 text-slate-500">
                                    No Status
                                </span>
                            <?php endif; ?>
                        </div>
                        <p class="text-sm font-mono text-blue-600 mb-3"><?= esc($lead['lead_number']) ?></p>
                        <?php if (!empty($lead['company_name'])): ?>
                            <p class="text-sm text-slate-600 flex items-center gap-2">
                                <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4 text-slate-400" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M19 21V5a2 2 0 00-2-2H7a2 2 0 00-2 2v16m14 0h2m-2 0h-5m-9 0H3m2 0h5M9 7h1m-1 4h1m4-4h1m-1 4h1m-5 10v-5a1 1 0 011-1h2a1 1 0 011 1v5m-4 0h4" />
                                </svg>
                                <?= esc($lead['company_name']) ?>
                            </p>
                        <?php endif; ?>
                    </div>
                </div>

                <div class="grid grid-cols-1 sm:grid-cols-2 gap-4 pt-6">
                    <div class="flex items-start gap-3 p-3 rounded-xl bg-slate-50/50 hover:bg-slate-50 transition">
                        <div class="w-9 h-9 rounded-lg bg-blue-100 flex items-center justify-center flex-shrink-0">
                            <svg xmlns="http://www.w3.org/2000/svg" class="w-4.5 h-4.5 text-blue-600" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M3 5a2 2 0 012-2h3.28a1 1 0 01.948.684l1.498 4.493a1 1 0 01-.502 1.21l-2.257 1.13a11.042 11.042 0 005.516 5.516l1.13-2.257a1 1 0 011.21-.502l4.493 1.498a1 1 0 01.684.949V19a2 2 0 01-2 2h-1C9.716 21 3 14.284 3 6V5z" />
                            </svg>
                        </div>
                        <div class="min-w-0">
                            <p class="text-[11px] font-semibold text-slate-400 uppercase tracking-wider">Phone</p>
                            <p class="text-sm font-medium text-slate-700 mt-0.5"><?= esc($lead['phone']) ?></p>
                        </div>
                    </div>

                    <div class="flex items-start gap-3 p-3 rounded-xl bg-slate-50/50 hover:bg-slate-50 transition">
                        <div class="w-9 h-9 rounded-lg bg-emerald-100 flex items-center justify-center flex-shrink-0">
                            <svg xmlns="http://www.w3.org/2000/svg" class="w-4.5 h-4.5 text-emerald-600" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" />
                            </svg>
                        </div>
                        <div class="min-w-0">
                            <p class="text-[11px] font-semibold text-slate-400 uppercase tracking-wider">Email</p>
                            <p class="text-sm font-medium text-slate-700 mt-0.5 break-all">
                                <?= !empty($lead['email']) ? esc($lead['email']) : '<span class="text-slate-300">—</span>' ?>
                            </p>
                        </div>
                    </div>

                    <div class="flex items-start gap-3 p-3 rounded-xl bg-slate-50/50 hover:bg-slate-50 transition">
                        <div class="w-9 h-9 rounded-lg bg-purple-100 flex items-center justify-center flex-shrink-0">
                            <svg xmlns="http://www.w3.org/2000/svg" class="w-4.5 h-4.5 text-purple-600" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M10 6H5a2 2 0 00-2 2v9a2 2 0 002 2h14a2 2 0 002-2V8a2 2 0 00-2-2h-5m-4 0V5a2 2 0 114 0v1m-4 0a2 2 0 104 0m-5 8a2 2 0 100-4 2 2 0 000 4zm0 0c1.306 0 2.417.835 2.83 2M9 14a3.001 3.001 0 00-2.83 2M15 11h3m-3 4h2" />
                            </svg>
                        </div>
                        <div class="min-w-0">
                            <p class="text-[11px] font-semibold text-slate-400 uppercase tracking-wider">License #</p>
                            <p class="text-sm font-medium text-slate-700 mt-0.5">
                                <?= !empty($lead['license_number']) ? esc($lead['license_number']) : '<span class="text-slate-300">—</span>' ?>
                            </p>
                        </div>
                    </div>

                    <div class="flex items-start gap-3 p-3 rounded-xl bg-slate-50/50 hover:bg-slate-50 transition">
                        <div class="w-9 h-9 rounded-lg bg-amber-100 flex items-center justify-center flex-shrink-0">
                            <svg xmlns="http://www.w3.org/2000/svg" class="w-4.5 h-4.5 text-amber-600" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z" />
                            </svg>
                        </div>
                        <div class="min-w-0">
                            <p class="text-[11px] font-semibold text-slate-400 uppercase tracking-wider">Assigned To</p>
                            <p class="text-sm font-medium text-slate-700 mt-0.5">
                                <?php if (!empty($lead['assigned_name'])): ?>
                                    <?= esc($lead['assigned_name']) ?>
                                    <span class="text-xs text-slate-400">(<?= esc($lead['assigned_username']) ?>)</span>
                                <?php else: ?>
                                    <span class="text-slate-300">Unassigned</span>
                                <?php endif; ?>
                            </p>
                        </div>
                    </div>
                </div>

                <div class="mt-6 pt-6 border-t border-slate-100 flex flex-wrap gap-6 text-xs text-slate-500">
                    <div class="flex items-center gap-2">
                        <svg xmlns="http://www.w3.org/2000/svg" class="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z" />
                        </svg>
                        Created: <?= !empty($lead['created_at']) ? date('F j, Y g:i A', strtotime($lead['created_at'])) : '—' ?>
                    </div>
                    <div class="flex items-center gap-2">
                        <svg xmlns="http://www.w3.org/2000/svg" class="w-3.5 h-3.5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15" />
                        </svg>
                        Updated: <?= !empty($lead['updated_at']) ? date('F j, Y g:i A', strtotime($lead['updated_at'])) : '—' ?>
                    </div>
                </div>
            </div>

            <?= view('_partials/lead_booking_block', [
                'lead'      => $lead,
                'bookings'  => $bookings    ?? [],
                'bookUrl'   => $agentBookUrl ?? site_url('/admin/leads/book/' . (int)($lead['id'] ?? 0)),
                'cancelUrl' => site_url('/admin/bookings/cancel'),
                'viewUrl'   => site_url('/admin/bookings/view'),
                'tzDefault' => 'America/Los_Angeles',
            ]) ?>

            <div class="glass rounded-2xl p-6 shadow-sm border border-slate-200/60">
                <div class="flex items-center justify-between mb-5">
                    <div class="flex items-center gap-3">
                        <div class="w-9 h-9 rounded-xl bg-slate-800 flex items-center justify-center">
                            <svg xmlns="http://www.w3.org/2000/svg" class="w-4.5 h-4.5 text-white" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M7 8h10M7 12h4m1 8l-4-4H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-3l-4 4z" />
                            </svg>
                        </div>
                        <div>
                            <h4 class="text-sm font-bold text-slate-800">Internal Notes</h4>
                            <p class="text-xs text-slate-500"><?= count($notes) ?> note<?= count($notes) !== 1 ? 's' : '' ?></p>
                        </div>
                    </div>
                </div>

                <?= form_open('/admin/leads/note/add/' . $lead['id'], ['class' => 'mb-6']) ?>
                <?= csrf_field() ?>
                <div class="space-y-3">
                    <label class="block text-xs font-semibold text-slate-500 uppercase tracking-wider">Add Note</label>
                    <textarea name="note_text" rows="3" class="w-full px-4 py-3 rounded-xl bg-white border border-slate-200 text-sm text-slate-700 placeholder:text-slate-400 focus:outline-none focus:ring-2 focus:ring-blue-500/20 focus:border-blue-500 transition resize-y" placeholder="Write an internal note about this lead..."><?= esc($oldPost['note_text'] ?? '') ?></textarea>
                    <?php if (isset($errors['note_text'])): ?>
                        <p class="text-xs text-red-600"><?= esc($errors['note_text']) ?></p>
                    <?php endif; ?>
                    <div class="flex justify-end">
                        <button type="submit" class="inline-flex items-center gap-2 px-5 py-2.5 bg-slate-800 text-white text-sm font-semibold rounded-xl hover:bg-slate-900 transition">
                            <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M12 4v16m8-8H4" />
                            </svg>
                            Add Note
                        </button>
                    </div>
                </div>
                </form>

                <div class="space-y-3">
                    <?php if (empty($notes)): ?>
                        <div class="py-12 text-center">
                            <div class="w-14 h-14 rounded-2xl bg-slate-100 flex items-center justify-center mx-auto mb-3">
                                <svg xmlns="http://www.w3.org/2000/svg" class="w-7 h-7 text-slate-400" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8">
                                    <path stroke-linecap="round" stroke-linejoin="round" d="M7 8h10M7 12h4m1 8l-4-4H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-3l-4 4z" />
                                </svg>
                            </div>
                            <p class="text-sm font-medium text-slate-600">No notes yet</p>
                            <p class="text-xs text-slate-400 mt-0.5">Add the first note above</p>
                        </div>
                    <?php else: ?>
                        <?php foreach ($notes as $note): ?>
                            <div class="p-4 rounded-2xl bg-slate-50/70 border border-slate-100 hover:bg-slate-50 transition">
                                <div class="flex items-start gap-3">
                                    <div class="w-9 h-9 rounded-full bg-gradient-to-br from-slate-600 to-slate-800 flex items-center justify-center text-white text-xs font-semibold flex-shrink-0">
                                        <?= strtoupper(substr(esc($note['user_name'] ?? $note['user_username'] ?? 'U'), 0, 1)) ?>
                                    </div>
                                    <div class="flex-1 min-w-0">
                                        <div class="flex flex-wrap items-center gap-2 mb-1.5">
                                            <span class="text-sm font-semibold text-slate-700"><?= esc($note['user_name'] ?? $note['user_username'] ?? 'Unknown') ?></span>
                                            <span class="text-xs text-slate-400">•</span>
                                            <span class="text-xs text-slate-400">
                                                <?= !empty($note['created_at']) ? date('M j, Y g:i A', strtotime($note['created_at'])) : '' ?>
                                            </span>
                                        </div>
                                        <p class="text-sm text-slate-600 leading-relaxed whitespace-pre-wrap"><?= esc($note['note_text']) ?></p>
                                    </div>
                                </div>
                            </div>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </div>
            </div>

            <?= view('_partials/call_history_block', [
                'calls'                => $calls ?? [],
                'cloudtalkConfigured'  => $cloudtalkConfigured ?? true,
            ]) ?>
        </div>

        <div class="space-y-6">
            <div class="glass rounded-2xl p-6 shadow-sm border border-slate-200/60">
                <div class="flex items-center gap-3 mb-5">
                    <div class="w-9 h-9 rounded-xl bg-purple-100 flex items-center justify-center">
                        <svg xmlns="http://www.w3.org/2000/svg" class="w-4.5 h-4.5 text-purple-600" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z" />
                        </svg>
                    </div>
                    <div>
                        <h4 class="text-sm font-bold text-slate-800">Custom Fields</h4>
                        <p class="text-xs text-slate-500"><?= count($customFields) ?> field<?= count($customFields) !== 1 ? 's' : '' ?></p>
                    </div>
                </div>

                <?php if (empty($customFields)): ?>
                    <div class="py-8 text-center">
                        <div class="w-12 h-12 rounded-xl bg-slate-100 flex items-center justify-center mx-auto mb-2">
                            <svg xmlns="http://www.w3.org/2000/svg" class="w-6 h-6 text-slate-400" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8">
                                <path stroke-linecap="round" stroke-linejoin="round" d="M4 6h16M4 10h16M4 14h16M4 18h16" />
                            </svg>
                        </div>
                        <p class="text-sm text-slate-500">No custom fields defined</p>
                    </div>
                <?php else: ?>
                    <div class="space-y-3">
                        <?php foreach ($customFields as $cf): ?>
                            <?php
                            $cv = $customValues[$cf['id']] ?? null;
                            $val = $cv['field_value'] ?? null;
                            ?>
                            <div class="p-3 rounded-xl bg-white/50 border border-slate-100 hover:bg-white transition">
                                <p class="text-[11px] font-semibold text-slate-400 uppercase tracking-wider mb-1 flex items-center gap-1.5">
                                    <?= esc($cf['field_label']) ?>
                                    <?php if (!empty($cf['required'])): ?>
                                        <span class="inline-flex items-center justify-center w-4 h-4 rounded-full bg-red-100 text-red-600 text-[9px] font-bold">!</span>
                                    <?php endif; ?>
                                </p>
                                <?php if ($val !== null && $val !== ''): ?>
                                    <p class="text-sm text-slate-700 font-medium whitespace-pre-wrap break-words"><?= esc($val) ?></p>
                                <?php else: ?>
                                    <p class="text-sm text-slate-300 italic">— Not set —</p>
                                <?php endif; ?>
                            </div>
                        <?php endforeach; ?>
                    </div>
                <?php endif; ?>
            </div>

            <div class="glass rounded-2xl p-6 shadow-sm border border-slate-200/60">
                <div class="flex items-center gap-3 mb-4">
                    <div class="w-9 h-9 rounded-xl bg-rose-100 flex items-center justify-center">
                        <svg xmlns="http://www.w3.org/2000/svg" class="w-4.5 h-4.5 text-rose-600" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-3L13.732 4c-.77-1.333-2.694-1.333-3.464 0L3.34 16c-.77 1.333.192 3 1.732 3z" />
                        </svg>
                    </div>
                    <div>
                        <h4 class="text-sm font-bold text-slate-800">Danger Zone</h4>
                        <p class="text-xs text-slate-500">Permanent deletion</p>
                    </div>
                </div>
                <form method="get" action="<?= site_url('/admin/leads/delete/' . $lead['id']) ?>" onsubmit="return confirm('This will permanently delete the lead, all custom field values, and notes. This cannot be undone. Are you sure?');">
                    <button type="submit" class="w-full inline-flex items-center justify-center gap-2 px-4 py-2.5 border border-red-200 bg-red-50 text-red-600 text-sm font-semibold rounded-xl hover:bg-red-100 transition">
                        <svg xmlns="http://www.w3.org/2000/svg" class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16" />
                        </svg>
                        Delete Lead
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>
