<div class="max-w-3xl mx-auto">
    <div class="flex items-center justify-between mb-6">
        <div class="flex items-center gap-3"><a href="?page=communications" 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($msg['subject']) ?></h1><p class="text-sm text-gray-500"><?= timeAgo($msg['created_at']) ?></p></div>
            <span class="inline-flex px-2 py-0.5 rounded-full text-xs font-medium <?= $msg['type'] === 'email' ? 'bg-blue-50 text-blue-700' : ($msg['type'] === 'sms' ? 'bg-green-50 text-green-700' : 'bg-gray-100 text-gray-700') ?>"><?= ucfirst($msg['type'] ?? 'email') ?></span>
        </div>
        <form method="POST" action="?page=communications&action=delete&id=<?= $msg['id'] ?>" onsubmit="return confirm('Delete?')"><?= 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 class="bg-white rounded-xl shadow-sm border border-gray-200 p-6 mb-6">
        <dl class="space-y-3 text-sm mb-4 pb-4 border-b border-gray-100">
            <div class="flex justify-between"><dt class="text-gray-500">To</dt><dd class="font-medium"><?= ucfirst($msg['recipient_type'] ?? '') ?> #<?= $msg['recipient_id'] ?></dd></div>
            <div class="flex justify-between"><dt class="text-gray-500">Sent By</dt><dd class="font-medium"><?= htmlspecialchars(($msg['sender_first'] ?? '').' '.($msg['sender_last'] ?? '')) ?></dd></div>
            <div class="flex justify-between"><dt class="text-gray-500">Sent At</dt><dd class="font-medium"><?= $msg['sent_at'] ? formatDate($msg['sent_at']) : '-' ?></dd></div>
            <div class="flex justify-between"><dt class="text-gray-500">Status</dt><dd><?= statusBadge($msg['status'] ?? 'sent') ?></dd></div>
        </dl>
        <div class="prose prose-sm max-w-none text-gray-700 whitespace-pre-line"><?= htmlspecialchars($msg['body']) ?></div>
    </div>
</div>
