{{--
  Top-right header widgets:
  Mobile — Afia + launcher only (profile/notifications live in bottom nav).
  Desktop — Afia → notifications → launcher → divider → avatar dropdown.
--}}
@php
    $topbarUser = $user ?? auth()->user();
    $initials = collect(explode(' ', trim((string) $topbarUser?->name)))
        ->filter()->take(2)->map(fn ($p) => strtoupper(substr($p, 0, 1)))->implode('');
    $avatarUrl = $topbarUser && method_exists($topbarUser, 'avatarUrl')
        ? $topbarUser->avatarUrl()
        : ($topbarUser?->avatar_url ?? null);
    $showUserHeader = $showUser ?? true;
@endphp

@includeIf('partials.afia-button', ['compact' => true])

@includeIf('partials.notification-dropdown')

@include('partials.launcher')

@includeIf('partials.topbar-widgets-mid')

<div class="hidden h-8 w-px bg-slate-200 lg:block"></div>

<div class="relative hidden lg:block" x-data="{ profileOpen: false }" @click.outside="profileOpen = false" @keydown.escape.window="profileOpen = false">
    <button type="button" @click="profileOpen = !profileOpen"
            class="inline-flex items-center gap-2 rounded-full border border-slate-200 px-2 py-1.5 text-slate-700 transition hover:bg-slate-50">
        @if ($avatarUrl)
            <img src="{{ $avatarUrl }}" alt="Avatar" class="h-8 w-8 rounded-full object-cover ring-1 ring-slate-200">
        @else
            <span class="inline-flex h-8 w-8 items-center justify-center rounded-full bg-slate-900 text-xs font-semibold text-white">{{ $initials !== '' ? $initials : 'U' }}</span>
        @endif
        <svg class="h-4 w-4 text-slate-500" fill="none" stroke="currentColor" stroke-width="2" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="m19 9-7 7-7-7"/></svg>
    </button>

    <div x-show="profileOpen" x-cloak x-transition
         class="absolute right-0 z-50 mt-2 w-64 rounded-xl border border-slate-200 bg-white shadow-lg">
        @include('partials.user-profile-menu', [
            'items' => \App\Support\UserProfileMenu::items($topbarUser),
            'user' => $topbarUser,
            'showUser' => $showUserHeader,
        ])
    </div>
</div>
