<header class="h-16 bg-white border-b border-slate-200 flex items-center justify-between px-4 md:px-6 flex-shrink-0 z-20"
        x-data="{
            open: false,
            initial: '{{ .user.NombreUsuario }}'.charAt(0).toUpperCase() || 'U',
            userName: '{{ .user.NombreUsuario }}',
            roleName: '{{ .user.Role.Name }}',
            bellOpen: false,
            notifs: [],
            unread: 0,
            async loadNotifs() {
                try {
                    const r = await axios.get('/app/mis-notifs?no_leidas=0');
                    this.notifs = r.data.items || [];
                    this.unread = r.data.unread || 0;
                } catch {}
            },
            async markRead(id) {
                await axios.put('/app/mis-notifs/' + id + '/leida');
                const n = this.notifs.find(x => x.ID === id);
                if (n) { n.leida = true; this.unread = Math.max(0, this.unread - 1); }
            },
            async markAll() {
                await axios.post('/app/mis-notifs/marcar-todas');
                this.notifs.forEach(n => n.leida = true);
                this.unread = 0;
            }
        }"
        x-init="loadNotifs(); setInterval(() => loadNotifs(), 30000)">

    <!-- Left: page context -->
    <div class="flex items-center gap-3">
        <div class="md:hidden w-8"></div>
        <div class="hidden sm:flex items-center gap-2 text-sm text-slate-500">
            <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 text-slate-400" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                <path stroke-linecap="round" stroke-linejoin="round" d="M2.25 12l8.954-8.955c.44-.439 1.152-.439 1.591 0L21.75 12M4.5 9.75v10.125c0 .621.504 1.125 1.125 1.125H9.75v-4.875c0-.621.504-1.125 1.125-1.125h2.25c.621 0 1.125.504 1.125 1.125V21h4.125c.621 0 1.125-.504 1.125-1.125V9.75M8.25 21h8.25"/>
            </svg>
            <span class="text-slate-400">Panel de control</span>
        </div>
    </div>

    <!-- Right: actions + user menu -->
    <div class="flex items-center gap-3">

        <!-- 🔔 Bell de notificaciones -->
        <div class="relative">
            <button @click="bellOpen = !bellOpen; if(bellOpen) loadNotifs()"
                class="relative p-2 rounded-xl hover:bg-slate-100 transition-colors focus:outline-none">
                <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-slate-500" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M14.857 17.082a23.848 23.848 0 005.454-1.31A8.967 8.967 0 0118 9.75v-.7V9A6 6 0 006 9v.75a8.967 8.967 0 01-2.312 6.022c1.733.64 3.56 1.085 5.455 1.31m5.714 0a24.255 24.255 0 01-5.714 0m5.714 0a3 3 0 11-5.714 0"/>
                </svg>
                <!-- Badge -->
                <span x-show="unread > 0"
                    class="absolute -top-0.5 -right-0.5 h-4 w-4 flex items-center justify-center rounded-full text-white text-[10px] font-bold"
                    style="background:#8eb02f"
                    x-text="unread > 9 ? '9+' : unread">
                </span>
            </button>

            <!-- Dropdown notificaciones -->
            <div x-show="bellOpen"
                @click.outside="bellOpen = false"
                x-transition:enter="transition ease-out duration-150"
                x-transition:enter-start="opacity-0 scale-95 -translate-y-1"
                x-transition:enter-end="opacity-100 scale-100 translate-y-0"
                x-transition:leave="transition ease-in duration-100"
                x-transition:leave-start="opacity-100 scale-100 translate-y-0"
                x-transition:leave-end="opacity-0 scale-95 -translate-y-1"
                class="absolute right-0 top-12 w-80 bg-white rounded-xl shadow-lg border border-slate-200 overflow-hidden z-50">
                <!-- Header -->
                <div class="px-4 py-3 border-b border-slate-100 flex items-center justify-between">
                    <span class="text-sm font-semibold text-slate-800">Notificaciones</span>
                    <button x-show="unread > 0" @click="markAll()"
                        class="text-xs text-slate-400 hover:text-slate-600">Marcar todas leídas</button>
                </div>
                <!-- Lista -->
                <div class="max-h-72 overflow-y-auto divide-y divide-slate-100">
                    <template x-if="notifs.length === 0">
                        <div class="px-4 py-6 text-center text-slate-400 text-sm">Sin notificaciones</div>
                    </template>
                    <template x-for="n in notifs" :key="n.ID">
                        <a :href="n.url || '#'"
                            @click="if(!n.leida) markRead(n.ID)"
                            class="flex items-start gap-3 px-4 py-3 hover:bg-slate-50 transition-colors"
                            :class="!n.leida ? 'bg-emerald-50/50' : ''">
                            <span class="text-lg leading-none mt-0.5" x-text="n.icono || '🔔'"></span>
                            <div class="flex-1 min-w-0">
                                <p class="text-sm font-medium text-slate-800 truncate" x-text="n.titulo"></p>
                                <p class="text-xs text-slate-500 truncate mt-0.5" x-text="n.cuerpo"></p>
                            </div>
                            <div x-show="!n.leida" class="w-2 h-2 rounded-full flex-shrink-0 mt-1.5" style="background:#8eb02f"></div>
                        </a>
                    </template>
                </div>
                <!-- Footer -->
                <div class="border-t border-slate-100 px-4 py-2">
                    <a href="/app/tickets" class="text-xs text-slate-500 hover:text-slate-700">Ver todos los tickets →</a>
                </div>
            </div>
        </div>

        <!-- User info (desktop) -->
        <div class="hidden md:flex flex-col items-end mr-1">
            <span class="text-xs font-semibold text-slate-700 leading-tight" x-text="userName"></span>
            <span class="text-xs text-slate-400 leading-tight" x-text="roleName"></span>
        </div>

        <!-- Avatar dropdown -->
        <div class="relative">
            <button @click="open = !open"
                    class="flex items-center gap-2 p-1 rounded-xl hover:bg-slate-100 transition-colors focus:outline-none focus-visible:ring-2 focus-visible:ring-offset-1"
                    style="--tw-ring-color:#8eb02f">
                <div class="ui-avatar ui-avatar-lg" x-text="initial"></div>
                <svg xmlns="http://www.w3.org/2000/svg"
                     class="h-3.5 w-3.5 text-slate-400 transition-transform duration-150"
                     :class="open ? 'rotate-180' : ''"
                     fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M19.5 8.25l-7.5 7.5-7.5-7.5"/>
                </svg>
            </button>

            <!-- Dropdown -->
            <div x-show="open"
                 @click.outside="open = false"
                 x-transition:enter="transition ease-out duration-150"
                 x-transition:enter-start="opacity-0 scale-95 -translate-y-1"
                 x-transition:enter-end="opacity-100 scale-100 translate-y-0"
                 x-transition:leave="transition ease-in duration-100"
                 x-transition:leave-start="opacity-100 scale-100 translate-y-0"
                 x-transition:leave-end="opacity-0 scale-95 -translate-y-1"
                 class="absolute right-0 top-12 w-52 bg-white rounded-xl shadow-lg border border-slate-200 overflow-hidden z-50">
                <div class="px-4 py-3 border-b border-slate-100">
                    <p class="text-sm font-semibold text-slate-800 truncate" x-text="userName"></p>
                    <p class="text-xs text-slate-500 truncate" x-text="roleName"></p>
                </div>
                <div class="py-1">
                    <a href="/app/profile"
                       class="flex items-center gap-3 px-4 py-2.5 text-sm text-slate-700 hover:bg-slate-50 transition-colors">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4 text-slate-400" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M17.982 18.725A7.488 7.488 0 0012 15.75a7.488 7.488 0 00-5.982 2.975m11.963 0a9 9 0 10-11.963 0m11.963 0A8.966 8.966 0 0112 21a8.966 8.966 0 01-5.982-2.275M15 9.75a3 3 0 11-6 0 3 3 0 016 0z"/>
                        </svg>
                        Mi cuenta
                    </a>
                    <div class="border-t border-slate-100 my-1"></div>
                    <a href="/do/logout"
                       class="flex items-center gap-3 px-4 py-2.5 text-sm text-red-600 hover:bg-red-50 transition-colors"
                       onclick="event.preventDefault(); document.getElementById('header-logout').submit();">
                        <svg xmlns="http://www.w3.org/2000/svg" class="h-4 w-4" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M15.75 9V5.25A2.25 2.25 0 0013.5 3h-6a2.25 2.25 0 00-2.25 2.25v13.5A2.25 2.25 0 007.5 21h6a2.25 2.25 0 002.25-2.25V15M12 9l-3 3m0 0l3 3m-3-3h12.75"/>
                        </svg>
                        Cerrar sesión
                    </a>
                    <form id="header-logout" action="/do/logout" method="POST" class="hidden"></form>
                </div>
            </div>
        </div>
    </div>
</header>
