{{-- In-app notification bell + dropdown (scoped to this app). --}}
<div class="relative hidden lg:block"
     x-data="notificationDropdown({
         unreadUrl: {{ \Illuminate\Support\Js::from(route('notifications.unread')) }},
         markReadUrl: {{ \Illuminate\Support\Js::from(route('notifications.mark-read', ['id' => '__ID__'])) }},
         markAllReadUrl: {{ \Illuminate\Support\Js::from(route('notifications.mark-all-read')) }},
         indexUrl: {{ \Illuminate\Support\Js::from(route('notifications.index')) }},
         csrfToken: {{ \Illuminate\Support\Js::from(csrf_token()) }},
     })"
     @click.outside="open = false">
    <button type="button"
            @click="toggle()"
            class="relative inline-flex items-center justify-center rounded-full border border-slate-200 p-2.5 text-slate-600 transition hover:bg-slate-50"
            aria-label="Notifications">
        <svg class="h-5 w-5" fill="none" stroke="currentColor" stroke-width="1.7" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" d="M14.857 17.082a23.848 23.848 0 0 0 5.454-1.31A8.967 8.967 0 0 1 18 9.75V9a6 6 0 1 0-12 0v.75a8.967 8.967 0 0 1-2.312 6.022c1.733.64 3.56 1.08 5.455 1.31m5.714 0a24.255 24.255 0 0 1-5.714 0m5.714 0a3 3 0 1 1-5.714 0"/>
        </svg>
        <span x-show="unreadCount > 0"
              x-cloak
              x-text="unreadCount > 9 ? '9+' : unreadCount"
              class="absolute -right-1 -top-1 inline-flex min-w-5 items-center justify-center rounded-full bg-rose-500 px-1.5 py-0.5 text-[10px] font-semibold text-white"></span>
    </button>

    <div x-show="open"
         x-cloak
         x-transition:enter="transition ease-out duration-150"
         x-transition:enter-start="opacity-0 scale-95"
         x-transition:enter-end="opacity-100 scale-100"
         x-transition:leave="transition ease-in duration-100"
         x-transition:leave-start="opacity-100 scale-100"
         x-transition:leave-end="opacity-0 scale-95"
         class="absolute right-0 z-50 mt-2 w-80 origin-top-right rounded-xl border border-slate-200 bg-white shadow-lg">
        <div class="flex items-center justify-between border-b border-slate-100 px-4 py-3">
            <h3 class="text-sm font-semibold text-slate-900">Notifications</h3>
            <button type="button"
                    x-show="unreadCount > 0"
                    @click="markAllRead()"
                    class="text-xs font-medium text-indigo-600 hover:text-indigo-700">
                Mark all read
            </button>
        </div>

        <div class="max-h-80 overflow-y-auto">
            <template x-if="loading">
                <div class="px-4 py-6 text-center">
                    <svg class="mx-auto h-5 w-5 animate-spin text-slate-400" fill="none" viewBox="0 0 24 24">
                        <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
                        <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4z"></path>
                    </svg>
                </div>
            </template>

            <template x-if="!loading && notifications.length === 0">
                <div class="px-4 py-8 text-center">
                    <svg class="mx-auto h-10 w-10 text-slate-300" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" d="M14.857 17.082a23.848 23.848 0 0 0 5.454-1.31A8.967 8.967 0 0 1 18 9.75V9a6 6 0 1 0-12 0v.75a8.967 8.967 0 0 1-2.312 6.022c1.733.64 3.56 1.08 5.455 1.31m5.714 0a24.255 24.255 0 0 1-5.714 0m5.714 0a3 3 0 1 1-5.714 0"/>
                    </svg>
                    <p class="mt-2 text-sm text-slate-500">No notifications yet</p>
                </div>
            </template>

            <template x-if="!loading && notifications.length > 0">
                <div class="divide-y divide-slate-100">
                    <template x-for="notification in notifications" :key="notification.id">
                        <a :href="notification.url || '#'"
                           @click="markRead(notification.id)"
                           class="flex items-start gap-3 px-4 py-3 transition hover:bg-slate-50">
                            <span class="mt-0.5 flex h-8 w-8 shrink-0 items-center justify-center rounded-full"
                                  :class="getIconBg(notification.icon)">
                                <template x-if="notification.icon === 'success'">
                                    <svg class="h-4 w-4" :class="getIconColor(notification.icon)" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75 11.25 15 15 9.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
                                </template>
                                <template x-if="notification.icon === 'task'">
                                    <svg class="h-4 w-4" :class="getIconColor(notification.icon)" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M9 12.75 11.25 15 15 9.75M21 12a9 9 0 1 1-18 0 9 9 0 0 1 18 0Z"/></svg>
                                </template>
                                <template x-if="!['success','task'].includes(notification.icon)">
                                    <svg class="h-4 w-4" :class="getIconColor(notification.icon)" fill="none" stroke="currentColor" stroke-width="1.5" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" d="M14.857 17.082a23.848 23.848 0 0 0 5.454-1.31A8.967 8.967 0 0 1 18 9.75V9a6 6 0 1 0-12 0v.75a8.967 8.967 0 0 1-2.312 6.022c1.733.64 3.56 1.08 5.455 1.31m5.714 0a24.255 24.255 0 0 1-5.714 0m5.714 0a3 3 0 1 1-5.714 0"/></svg>
                                </template>
                            </span>
                            <div class="min-w-0 flex-1">
                                <p class="text-sm font-medium text-slate-900 truncate" x-text="notification.title"></p>
                                <p class="text-xs text-slate-500 line-clamp-2" x-text="notification.message"></p>
                                <p class="mt-1 text-[11px] text-slate-400" x-text="notification.created_at"></p>
                            </div>
                        </a>
                    </template>
                </div>
            </template>
        </div>

        <div class="border-t border-slate-100 px-4 py-2.5">
            <a :href="indexUrl" class="block text-center text-xs font-medium text-slate-600 hover:text-slate-900">
                View all notifications
            </a>
        </div>
    </div>
</div>
