<div class="flex items-center justify-between mb-6">
    <div><h1 class="text-2xl font-bold text-gray-900">Communications</h1><p class="text-sm text-gray-500 mt-1"><?= $total ?? 0 ?> messages</p></div>
    <a href="?page=communications&action=create" class="inline-flex items-center gap-2 px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition text-sm font-medium"><svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/></svg>New Message</a>
</div>
<div class="bg-white rounded-xl shadow-sm border border-gray-200">
    <div class="p-4 border-b border-gray-100">
        <form method="GET" class="flex flex-wrap gap-3"><input type="hidden" name="page" value="communications">
            <input type="text" name="search" value="<?= htmlspecialchars($_GET['search'] ?? '') ?>" placeholder="Search messages..." class="flex-1 min-w-[200px] px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500">
            <select name="type" class="px-3 py-2 border border-gray-300 rounded-lg text-sm focus:ring-2 focus:ring-indigo-500"><option value="">All Types</option>
                <?php foreach (['email'=>'Email','sms'=>'SMS','phone'=>'Phone','note'=>'Internal Note'] as $v=>$l): ?><option value="<?= $v ?>" <?= ($_GET['type'] ?? '') === $v ? 'selected' : '' ?>><?= $l ?></option><?php endforeach; ?>
            </select>
            <button type="submit" class="px-4 py-2 bg-gray-100 text-gray-700 rounded-lg hover:bg-gray-200 text-sm font-medium">Filter</button>
        </form>
    </div>
    <div class="divide-y divide-gray-100">
        <?php if (empty($messages)): ?><div class="px-4 py-12 text-center text-gray-400">No messages found.</div>
        <?php else: foreach ($messages as $m): ?>
            <a href="?page=communications&action=show&id=<?= $m['id'] ?>" class="block px-4 py-4 hover:bg-gray-50 transition">
                <div class="flex items-start justify-between">
                    <div class="min-w-0 flex-1">
                        <div class="flex items-center gap-2 mb-1">
                            <span class="inline-flex px-2 py-0.5 rounded-full text-xs font-medium <?= $m['type'] === 'email' ? 'bg-blue-50 text-blue-700' : ($m['type'] === 'sms' ? 'bg-green-50 text-green-700' : ($m['type'] === 'phone' ? 'bg-purple-50 text-purple-700' : 'bg-gray-100 text-gray-700')) ?>"><?= ucfirst($m['type'] ?? 'email') ?></span>
                            <span class="font-medium text-sm text-gray-900 truncate"><?= htmlspecialchars($m['subject']) ?></span>
                        </div>
                        <p class="text-xs text-gray-500">To: <?= htmlspecialchars($m['recipient_name'] ?? 'Unknown') ?> (<?= ucfirst($m['recipient_type'] ?? '') ?>)</p>
                    </div>
                    <span class="text-xs text-gray-400 whitespace-nowrap ml-4"><?= timeAgo($m['created_at']) ?></span>
                </div>
                <p class="text-sm text-gray-600 mt-1 truncate"><?= htmlspecialchars(substr($m['body'] ?? '', 0, 120)) ?></p>
            </a>
        <?php endforeach; endif; ?>
    </div>
    <?php if (!empty($pagination)): ?><div class="px-4 py-3 border-t border-gray-100"><?= $pagination ?></div><?php endif; ?>
</div>
