{{-- Account switcher (desktop) — only when the user belongs to more than one account. --}}
@if (isset($accessibleAccounts) && $accessibleAccounts->count() > 1)
    <div x-data="{ accountSwitcherOpen: false }" @click.outside="accountSwitcherOpen = false" class="relative hidden lg:block">
        <button type="button" @click="accountSwitcherOpen = !accountSwitcherOpen"
                class="inline-flex items-center gap-1.5 rounded-full border border-slate-200 px-3 py-2 text-sm text-slate-700 transition hover:bg-slate-50">
            <span class="max-w-[140px] truncate">{{ $actingAccount->name ?? $actingAccount->email }}</span>
            <svg class="h-4 w-4 text-slate-400" 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="accountSwitcherOpen" x-cloak x-transition
             class="absolute right-0 z-30 mt-2 w-60 rounded-xl border border-slate-200 bg-white p-1 shadow-lg">
            <p class="px-3 py-1.5 text-[10px] font-bold uppercase tracking-widest text-slate-400">Switch account</p>
            @foreach ($accessibleAccounts as $acctOption)
                <form method="POST" action="{{ route('account.switch') }}">
                    @csrf
                    <input type="hidden" name="account" value="{{ $acctOption->id }}">
                    <button type="submit" class="flex w-full items-center justify-between rounded-lg px-3 py-2 text-left text-sm hover:bg-slate-50 {{ $acctOption->id === $actingAccount->id ? 'font-semibold text-indigo-700' : 'text-slate-700' }}">
                        <span class="truncate">{{ $acctOption->id === auth()->id() ? 'My account' : ($acctOption->name ?? $acctOption->email) }}</span>
                        @if ($acctOption->id === $actingAccount->id)<span class="text-indigo-600">✓</span>@endif
                    </button>
                </form>
            @endforeach
        </div>
    </div>
@endif
