<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=leases" 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">Lease: <?= htmlspecialchars(($lease['first_name'] ?? '').' '.($lease['last_name'] ?? '')) ?></h1>
                <p class="text-sm text-gray-500"><?= htmlspecialchars(($lease['property_name'] ?? '') . ($lease['unit_number'] ? ' / Unit '.$lease['unit_number'] : '')) ?></p>
            </div>
            <?= statusBadge($lease['status'] ?? 'draft') ?>
        </div>
        <div class="flex gap-2">
            <a href="?page=leases&action=edit&id=<?= $lease['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>
            <form method="POST" action="?page=leases&action=delete&id=<?= $lease['id'] ?>" onsubmit="return confirm('Delete this lease?')">
                <?= CSRF::field() ?><button class="px-4 py-2 border border-red-300 text-red-600 rounded-lg hover:bg-red-50 text-sm font-medium">Delete</button>
            </form>
        </div>
    </div>

    <div class="grid grid-cols-1 md:grid-cols-4 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">Rent</p>
            <p class="text-2xl font-bold text-gray-900 mt-1"><?= formatMoney($lease['rent_amount']) ?></p>
            <p class="text-xs text-gray-400"><?= ucfirst($lease['rent_frequency'] ?? 'monthly') ?></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">Security Deposit</p>
            <p class="text-2xl font-bold text-gray-900 mt-1"><?= formatMoney($lease['security_deposit'] ?? 0) ?></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">Start Date</p>
            <p class="text-xl font-bold text-gray-900 mt-1"><?= formatDate($lease['start_date']) ?></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">End Date</p>
            <p class="text-xl font-bold text-gray-900 mt-1"><?= formatDate($lease['end_date']) ?></p>
            <?php
                $daysLeft = (strtotime($lease['end_date']) - time()) / 86400;
                if ($daysLeft > 0 && $daysLeft < 60): ?>
                    <p class="text-xs text-amber-600 mt-1"><?= round($daysLeft) ?> days remaining</p>
            <?php endif; ?>
        </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">Lease Details</h3>
            <dl class="space-y-3 text-sm">
                <div class="flex justify-between"><dt class="text-gray-500">Tenant</dt><dd class="font-medium"><a href="?page=tenants&action=show&id=<?= $lease['tenant_id'] ?>" class="text-indigo-600 hover:underline"><?= htmlspecialchars($lease['first_name'].' '.$lease['last_name']) ?></a></dd></div>
                <div class="flex justify-between"><dt class="text-gray-500">Email</dt><dd class="font-medium"><?= htmlspecialchars($lease['tenant_email'] ?? '-') ?></dd></div>
                <div class="flex justify-between"><dt class="text-gray-500">Late Fee</dt><dd class="font-medium"><?= formatMoney($lease['late_fee_amount'] ?? 0) ?></dd></div>
                <div class="flex justify-between"><dt class="text-gray-500">Grace Period</dt><dd class="font-medium"><?= ($lease['grace_period_days'] ?? 0) ?> days</dd></div>
            </dl>
        </div>

        <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">Quick Actions</h3>
            <div class="grid grid-cols-2 gap-2">
                <a href="?page=payments&action=create&lease_id=<?= $lease['id'] ?>" class="flex items-center gap-2 p-3 rounded-lg border border-gray-200 hover:bg-gray-50 text-sm font-medium text-gray-700 transition">
                    <svg class="w-4 h-4 text-green-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v12m6-6H6"/></svg>Record Payment
                </a>
                <a href="?page=maintenance&action=create&unit_id=<?= $lease['unit_id'] ?>" class="flex items-center gap-2 p-3 rounded-lg border border-gray-200 hover:bg-gray-50 text-sm font-medium text-gray-700 transition">
                    <svg class="w-4 h-4 text-amber-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.066 2.573c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.573 1.066c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.066-2.573c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065z"/><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/></svg>Maintenance
                </a>
                <a href="?page=documents&action=create&lease_id=<?= $lease['id'] ?>" class="flex items-center gap-2 p-3 rounded-lg border border-gray-200 hover:bg-gray-50 text-sm font-medium text-gray-700 transition">
                    <svg class="w-4 h-4 text-blue-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/></svg>Upload Doc
                </a>
                <a href="?page=communications&action=create&tenant_id=<?= $lease['tenant_id'] ?>" class="flex items-center gap-2 p-3 rounded-lg border border-gray-200 hover:bg-gray-50 text-sm font-medium text-gray-700 transition">
                    <svg class="w-4 h-4 text-purple-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 10h.01M12 10h.01M16 10h.01M9 16H5a2 2 0 01-2-2V6a2 2 0 012-2h14a2 2 0 012 2v8a2 2 0 01-2 2h-5l-5 5v-5z"/></svg>Message
                </a>
            </div>
        </div>
    </div>

    <!-- Charges -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6 mb-6">
        <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Charges</h3>
        <?php if (!empty($charges)): ?>
            <table class="w-full text-sm">
                <thead class="text-left"><tr><th class="pb-2 font-medium text-gray-600">Due Date</th><th class="pb-2 font-medium text-gray-600">Description</th><th class="pb-2 font-medium text-gray-600">Amount</th><th class="pb-2 font-medium text-gray-600">Status</th></tr></thead>
                <tbody class="divide-y divide-gray-50">
                    <?php foreach ($charges as $c): ?>
                        <tr><td class="py-2"><?= formatDate($c['due_date']) ?></td><td class="py-2"><?= htmlspecialchars($c['description'] ?? $c['charge_type']) ?></td><td class="py-2 font-medium"><?= formatMoney($c['amount']) ?></td><td class="py-2"><?= statusBadge($c['status']) ?></td></tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php else: ?><p class="text-gray-400 text-sm">No charges recorded</p><?php endif; ?>
    </div>

    <!-- Payments -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-6 mb-6">
        <h3 class="text-sm font-semibold text-gray-500 uppercase tracking-wider mb-4">Payments</h3>
        <?php if (!empty($payments)): ?>
            <table class="w-full text-sm">
                <thead class="text-left"><tr><th class="pb-2 font-medium text-gray-600">Date</th><th class="pb-2 font-medium text-gray-600">Amount</th><th class="pb-2 font-medium text-gray-600">Method</th><th class="pb-2 font-medium text-gray-600">Status</th></tr></thead>
                <tbody class="divide-y divide-gray-50">
                    <?php foreach ($payments as $p): ?>
                        <tr><td class="py-2"><?= formatDate($p['payment_date']) ?></td><td class="py-2 font-medium"><?= formatMoney($p['amount']) ?></td><td class="py-2"><?= ucfirst($p['payment_method'] ?? '-') ?></td><td class="py-2"><?= statusBadge($p['status']) ?></td></tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        <?php else: ?><p class="text-gray-400 text-sm">No payments recorded</p><?php endif; ?>
    </div>

    <?php if ($lease['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-3">Notes</h3>
            <p class="text-sm text-gray-700 whitespace-pre-line"><?= htmlspecialchars($lease['notes']) ?></p>
        </div>
    <?php endif; ?>
</div>
