@php
    $u = auth()->user();
    $initials = strtoupper(mb_substr($u->name, 0, 2));
    $twoFactorEnabled = $u->hasTwoFactorEnabled();
    // Profile, security and sessions are self-service (visible to everyone). The
    // login-protection + email tabs need manage-panel; the users tab needs
    // manage-users — the tab bodies re-guard, so hiding the nav entry is just UX.
    $tabs = [
        ['key' => 'profile', 'label' => __('settings.tab_profile'), 'icon' => 'settings'],
        ['key' => 'security', 'label' => __('settings.tab_security'), 'icon' => 'shield'],
    ];
    if (auth()->user()?->can('manage-panel')) {
        $tabs[] = ['key' => 'login-protection', 'label' => __('settings.tab_login_protection'), 'icon' => 'shield'];
    }
    if (auth()->user()?->can('manage-users')) {
        $tabs[] = ['key' => 'users', 'label' => __('settings.tab_users'), 'icon' => 'user-plus'];
    }
    $tabs[] = ['key' => 'sessions', 'label' => __('settings.tab_sessions'), 'icon' => 'logout'];
    if (auth()->user()?->can('manage-panel')) {
        $tabs[] = ['key' => 'email', 'label' => __('settings.tab_email'), 'icon' => 'mail'];
    }
@endphp

<div class="space-y-5">
    @if ($openRecoveryModal)
        <div x-data x-init="$dispatch('openModal', { component: 'modals.recovery-codes' })"></div>
    @endif
    {{-- Identity header --}}
    <div class="flex flex-wrap items-center gap-4 rounded-xl border border-line bg-surface p-5 shadow-panel">
        <span class="grid h-14 w-14 shrink-0 place-items-center rounded-xl border border-accent/25 bg-accent/10 font-display text-xl font-semibold text-accent">{{ $initials }}</span>
        <div class="min-w-0 flex-1">
            <p class="font-mono text-[11px] uppercase tracking-[0.2em] text-accent-text">{{ __('settings.account') }}</p>
            <h2 class="mt-0.5 truncate font-display text-xl font-semibold text-ink">{{ $u->name }}</h2>
            <p class="truncate font-mono text-[11px] text-ink-3">{{ $u->email }}</p>
        </div>
        <div class="flex items-center gap-2">
            <button type="button" x-data @click="window.dispatchEvent(new CustomEvent('onboarding:start'))"
                    class="inline-flex min-h-9 items-center gap-1.5 rounded-md border border-line px-3 font-mono text-[11px] text-ink-2 transition-colors hover:border-accent/40 hover:text-accent-text">
                <x-icon name="help-circle" class="h-3.5 w-3.5" />{{ __('onboarding.relaunch') }}
            </button>
            <x-badge tone="neutral">{{ $u->role?->label() ?? __('settings.role_admin') }}</x-badge>
            <x-two-factor-badge :enabled="$twoFactorEnabled" />
        </div>
    </div>

    <div class="grid grid-cols-1 gap-5 lg:grid-cols-[200px_minmax(0,1fr)]">
        {{-- Section nav --}}
        <nav class="flex min-w-0 gap-1 overflow-x-auto lg:flex-col lg:overflow-visible">
            @foreach ($tabs as $t)
                <button type="button" wire:click="$set('tab', '{{ $t['key'] }}')" @class([
                    'inline-flex min-h-10 shrink-0 items-center gap-2 whitespace-nowrap rounded-md px-3 text-sm transition-colors',
                    'bg-accent/10 text-accent-text shadow-[inset_2px_0_0_var(--color-accent)]' => $tab === $t['key'],
                    'text-ink-2 hover:bg-raised hover:text-ink' => $tab !== $t['key'],
                ])>
                    <x-icon :name="$t['icon']" class="h-4 w-4" />{{ $t['label'] }}
                </button>
            @endforeach
        </nav>

        {{-- Content --}}
        <div class="min-w-0">
            @if ($tab === 'profile')   <livewire:settings.profile />
            @elseif ($tab === 'security') <livewire:settings.security />
            @elseif ($tab === 'login-protection') <livewire:settings.login-protection />
            @elseif ($tab === 'users')    <livewire:settings.users />
            @elseif ($tab === 'sessions') <livewire:settings.sessions />
            @elseif ($tab === 'email')    <livewire:settings.email />
            {{-- Unknown ?tab= falls back to profile instead of rendering nothing. --}}
            @else <livewire:settings.profile />
            @endif
        </div>
    </div>
</div>
