<div class="p-4 lg:p-6 space-y-6">
    <div class="flex flex-col sm:flex-row sm:items-center sm:justify-between gap-4">
        <div>
            <h1 class="text-2xl font-bold text-gray-900">Properties</h1>
            <p class="text-sm text-gray-500 mt-1"><?= number_format($total) ?> total properties</p>
        </div>
        <a href="?page=properties&action=create" class="inline-flex items-center gap-2 px-4 py-2.5 text-sm font-semibold text-white bg-slate-900 rounded-xl hover:bg-slate-800 transition">
            <svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg>
            Add Property
        </a>
    </div>

    <!-- Filters -->
    <form class="flex flex-wrap gap-3" method="GET">
        <input type="hidden" name="page" value="properties">
        <div class="flex-1 min-w-[200px]">
            <input type="text" name="search" value="<?= e($search) ?>" placeholder="Search properties..." class="w-full px-4 py-2 text-sm border border-gray-200 rounded-xl bg-white focus:outline-none focus:ring-2 focus:ring-blue-500/20 focus:border-blue-400">
        </div>
        <select name="type" class="px-4 py-2 text-sm border border-gray-200 rounded-xl bg-white focus:outline-none focus:ring-2 focus:ring-blue-500/20">
            <option value="">All Types</option>
            <?php foreach (['single-family','duplex','triplex','fourplex','multifamily','apartment','commercial','retail','office','salon-suite','storage','mixed-use','other'] as $t): ?>
                <option value="<?= $t ?>" <?= $type === $t ? 'selected' : '' ?>><?= ucwords(str_replace('-', ' ', $t)) ?></option>
            <?php endforeach; ?>
        </select>
        <button type="submit" class="px-4 py-2 text-sm font-medium text-gray-700 bg-white border border-gray-200 rounded-xl hover:bg-gray-50 transition">Filter</button>
    </form>

    <!-- Table -->
    <div class="bg-white rounded-2xl border border-gray-200/80 overflow-hidden">
        <?php if (empty($properties)): ?>
            <div class="text-center py-16">
                <svg class="w-12 h-12 text-gray-300 mx-auto mb-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1" 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>
                <p class="text-gray-500 text-sm">No properties found</p>
                <a href="?page=properties&action=create" class="inline-flex items-center gap-1 mt-3 text-sm text-blue-600 hover:text-blue-700 font-medium">
                    <svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg>
                    Add your first property
                </a>
            </div>
        <?php else: ?>
            <div class="overflow-x-auto">
                <table class="w-full text-sm">
                    <thead>
                        <tr class="text-left text-xs font-medium text-gray-500 uppercase tracking-wider bg-gray-50/80 border-b border-gray-200/80">
                            <th class="px-5 py-3">Property</th>
                            <th class="px-5 py-3">Type</th>
                            <th class="px-5 py-3">Owner</th>
                            <th class="px-5 py-3">Units</th>
                            <th class="px-5 py-3">Occupancy</th>
                            <th class="px-5 py-3"></th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-100">
                        <?php foreach ($properties as $prop): 
                            $occRate = $prop['unit_count'] > 0 ? round(($prop['occupied_count'] / $prop['unit_count']) * 100) : 0;
                        ?>
                        <tr class="hover:bg-gray-50/50 transition">
                            <td class="px-5 py-4">
                                <a href="?page=properties&action=show&id=<?= $prop['id'] ?>" class="font-medium text-gray-900 hover:text-blue-600"><?= e($prop['name']) ?></a>
                                <div class="text-xs text-gray-500 mt-0.5"><?= e($prop['address']) ?>, <?= e($prop['city']) ?> <?= e($prop['state']) ?></div>
                            </td>
                            <td class="px-5 py-4 text-gray-600"><?= ucwords(str_replace('-', ' ', $prop['type'])) ?></td>
                            <td class="px-5 py-4 text-gray-600"><?= e(($prop['owner_first'] ?? '') . ' ' . ($prop['owner_last'] ?? '')) ?: '—' ?></td>
                            <td class="px-5 py-4 text-gray-600"><?= $prop['unit_count'] ?></td>
                            <td class="px-5 py-4">
                                <div class="flex items-center gap-2">
                                    <div class="flex-1 h-1.5 bg-gray-100 rounded-full max-w-[60px]">
                                        <div class="h-1.5 rounded-full <?= $occRate >= 80 ? 'bg-emerald-500' : ($occRate >= 50 ? 'bg-amber-500' : 'bg-red-400') ?>" style="width: <?= $occRate ?>%"></div>
                                    </div>
                                    <span class="text-xs text-gray-600"><?= $occRate ?>%</span>
                                </div>
                            </td>
                            <td class="px-5 py-4 text-right">
                                <a href="?page=properties&action=edit&id=<?= $prop['id'] ?>" class="text-gray-400 hover:text-blue-600 transition">
                                    <svg class="w-4 h-4 inline" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" 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>
                                </a>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            <?php if ($total > $perPage): ?>
                <div class="px-5 py-3 border-t border-gray-100"><?= paginationHtml($pagination) ?></div>
            <?php endif; ?>
        <?php endif; ?>
    </div>
</div>
