<?php
$unread = getUnreadCount($conn, $_SESSION['user_id']);
$notif_link = isAdmin() ? BASE_URL . '/admin/notifications.php' : BASE_URL . '/user/notifications.php';
$settings_link = isAdmin() ? BASE_URL . '/admin/settings.php' : BASE_URL . '/user/settings.php';
$topbar_user = $conn->query("SELECT avatar FROM users WHERE id={$_SESSION['user_id']}")->fetch_assoc();
?>
<div class="sidebar-overlay" onclick="closeSidebar()"></div>
<div class="topbar">
    <div class="topbar-left">
        <button onclick="toggleSidebar()" class="menu-btn" aria-label="Toggle menu">☰</button>
        <span class="topbar-title"><?= $page_title ?? 'Dashboard' ?></span>
    </div>
    <div class="topbar-actions">
        <button onclick="toggleDarkMode()" class="theme-toggle-btn" id="themeToggleBtn" title="Toggle Dark/Light Mode">🌙</button>
        <a href="<?= $notif_link ?>" class="notif-btn" aria-label="Notifications">
            🔔
            <?php if($unread > 0): ?><span class="notif-badge"><?= $unread ?></span><?php endif; ?>
        </a>
        <a href="<?= $settings_link ?>" class="topbar-userlink">
            <?php if(!empty($topbar_user['avatar'])): ?>
            <img src="<?= BASE_URL ?>/<?= htmlspecialchars($topbar_user['avatar']) ?>" class="topbar-avatar-img">
            <?php else: ?>
            <div class="topbar-avatar-fallback">
                <?= strtoupper(substr($_SESSION['name'] ?? 'U', 0, 1)) ?>
            </div>
            <?php endif; ?>
            <span class="topbar-user">Hi, <strong><?= htmlspecialchars($_SESSION['name'] ?? 'User') ?></strong></span>
        </a>
    </div>
</div>
