<?php $n = $notifications ?? ['items' => [], 'count' => 0]; $u = current_user(); $roleLabel = implode(', ', array_map('ucfirst', auth()->roles())); ?>
<header class="topbar">
  <button class="icon-btn d-lg-none" type="button" data-sidebar-toggle><i class="bi bi-list"></i></button>
  <div class="search-wrap">
    <i class="bi bi-search"></i>
    <input id="globalSearch" type="search" placeholder="Search accounts, contacts, leads, opportunities..." autocomplete="off">
    <div id="searchResults" class="search-results"></div>
  </div>
  <div class="top-actions position-relative">
    <?php if (can_add_new()): ?>
    <button class="btn btn-add" type="button" data-bs-toggle="modal" data-bs-target="#quickAddModal"><i class="bi bi-plus-lg"></i> Add New</button>
    <?php endif; ?>
    <button class="icon-btn" type="button" data-panel="#notifPanel"><i class="bi bi-bell"></i>
      <?php if (($n['count'] ?? 0) > 0): ?><span class="badge-dot"><?= (int) $n['count'] ?></span><?php endif; ?>
    </button>
    <a class="icon-btn" href="<?= e(url('emails')) ?>"><i class="bi bi-envelope"></i></a>
    <div class="user-chip">
      <span class="avatar"><?= e(user_initials()) ?></span>
      <span class="d-none d-md-block">
        <strong><?= e(user_name()) ?></strong><br>
        <small class="text-muted"><?= e($roleLabel) ?></small>
      </span>
    </div>
    <div id="notifPanel" class="dropdown-panel">
      <div class="px-2 py-1 fw-bold">Notifications</div>
      <?php if (empty($n['items'])): ?>
        <div class="p-2 text-muted small">Nothing new.</div>
      <?php else: foreach ($n['items'] as $item): ?>
        <a class="search-item" href="<?= e(url('notifications/' . (int) $item['id'])) ?>">
          <strong><?= e($item['title']) ?></strong>
          <div class="small text-muted"><?= e($item['body']) ?></div>
        </a>
      <?php endforeach; endif; ?>
    </div>
  </div>
</header>
