<div class="max-w-4xl mx-auto">
    <div class="flex items-center justify-between mb-6">
        <div class="flex items-center gap-3">
            <a href="?page=leads" class="text-gray-400 hover:text-gray-600"><svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 19l-7-7 7-7"/></svg></a>
            <div>
                <h1 class="text-2xl font-bold text-gray-900"><?= htmlspecialchars($lead['first_name'].' '.$lead['last_name']) ?></h1>
                <p class="text-sm text-gray-500">Lead #<?= $lead['id'] ?> · <?= ucfirst($lead['source'] ?? 'Unknown source') ?></p>
            </div>
            <?= statusBadge($lead['status'] ?? 'new') ?>
        </div>
        <div class="flex gap-2">
            <a href="?page=leads&action=edit&id=<?= $lead['id'] ?>" class="px-4 py-2 border border-gray-300 text-gray-700 rounded-lg hover:bg-gray-50 text-sm font-medium">Edit</a>
            <a href="?page=communications&action=create&recipient_type=tenant" class="px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 text-sm font-medium">Send Message</a>
        </div>
    </div>

    <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <p class="text-xs font-medium text-gray-500 uppercase">Status</p>
            <div class="mt-2"><?= statusBadge($lead['status'] ?? 'new') ?></div>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <p class="text-xs font-medium text-gray-500 uppercase">Desired Property</p>
            <p class="text-lg font-bold text-gray-900 mt-1"><?= htmlspecialchars($lead['desired_property_name'] ?? 'Not specified') ?></p>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-5">
            <p class="text-xs font-medium text-gray-500 uppercase">Move-in Date</p>
            <p class="text-lg font-bold text-gray-900 mt-1"><?= $lead['move_in_date'] ? formatDate($lead['move_in_date']) : 'Not set' ?></p>
        </div>
    </div>

    <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-6">
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Contact Information</h3>
            <dl class="space-y-3 text-sm">
                <div class="flex justify-between"><dt class="text-gray-500">Email</dt><dd class="font-medium"><?php if ($lead['email']): ?><a href="mailto:<?= htmlspecialchars($lead['email']) ?>" class="text-indigo-600 hover:underline"><?= htmlspecialchars($lead['email']) ?></a><?php else: ?>-<?php endif; ?></dd></div>
                <div class="flex justify-between"><dt class="text-gray-500">Phone</dt><dd class="font-medium"><?= htmlspecialchars($lead['phone'] ?? '-') ?></dd></div>
                <div class="flex justify-between"><dt class="text-gray-500">Source</dt><dd class="font-medium"><?= ucwords(str_replace('_',' ',$lead['source'] ?? '-')) ?></dd></div>
                <div class="flex justify-between"><dt class="text-gray-500">Agent</dt><dd class="font-medium"><?= htmlspecialchars(($lead['agent_first'] ?? '').($lead['agent_first'] ? ' '.$lead['agent_last'] : 'Unassigned')) ?></dd></div>
                <div class="flex justify-between"><dt class="text-gray-500">Created</dt><dd class="font-medium"><?= formatDate($lead['created_at']) ?></dd></div>
            </dl>
        </div>

        <?php if ($lead['notes']): ?>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
            <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Notes</h3>
            <p class="text-sm text-gray-700 whitespace-pre-line"><?= htmlspecialchars($lead['notes']) ?></p>
        </div>
        <?php endif; ?>
    </div>

    <!-- Pipeline Quick Actions -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6">
        <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Pipeline Actions</h3>
        <div class="flex flex-wrap gap-2">
            <?php
            $actions = ['new'=>'Mark New','contacted'=>'Mark Contacted','tour_scheduled'=>'Schedule Tour','tour_completed'=>'Tour Completed','applied'=>'Application Received','approved'=>'Approve','leased'=>'Mark Leased','lost'=>'Mark Lost'];
            foreach ($actions as $status => $label):
                $isCurrent = ($lead['status'] ?? 'new') === $status;
                $color = $isCurrent ? 'bg-indigo-600 text-white' : 'bg-gray-100 text-gray-600 hover:bg-gray-200';
            ?>
                <form method="POST" action="?page=leads&action=update&id=<?= $lead['id'] ?>" class="inline">
                    <?= CSRF::field() ?>
                    <input type="hidden" name="first_name" value="<?= htmlspecialchars($lead['first_name']) ?>">
                    <input type="hidden" name="last_name" value="<?= htmlspecialchars($lead['last_name']) ?>">
                    <input type="hidden" name="status" value="<?= $status ?>">
                    <button type="submit" class="px-3 py-1.5 rounded-lg text-xs font-medium <?= $color ?> transition"><?= $label ?></button>
                </form>
            <?php endforeach; ?>
        </div>
    </div>
</div>
