<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <meta name="theme-color" content="#1d4ed8">
    <title>@yield('title', 'Portal') — NEXUS</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        * { -webkit-tap-highlight-color: transparent; }
        body { font-family: 'Segoe UI', system-ui, sans-serif; }
        .safe-bottom { padding-bottom: env(safe-area-inset-bottom, 0); }
    </style>
    @livewireStyles
</head>
<body class="bg-gray-50 min-h-screen">

{{-- Flash messages --}}
@if(session('success'))
<div x-data="{ show: true }" x-show="show" x-init="setTimeout(() => show = false, 4000)"
     class="fixed top-4 left-4 right-4 z-50 bg-green-500 text-white px-4 py-3 rounded-xl shadow-lg text-sm font-medium text-center">
    {{ session('success') }}
</div>
@endif
@if(session('error'))
<div x-data="{ show: true }" x-show="show" x-init="setTimeout(() => show = false, 5000)"
     class="fixed top-4 left-4 right-4 z-50 bg-red-500 text-white px-4 py-3 rounded-xl shadow-lg text-sm font-medium text-center">
    {{ session('error') }}
</div>
@endif

{{-- Header --}}
<header class="bg-white border-b border-gray-100 sticky top-0 z-40">
    <div class="max-w-lg mx-auto px-4 py-3 flex items-center justify-between">
        <div class="flex items-center gap-3">
            <img src="/images/logo.png" alt="Logo" class="h-12 object-contain">
        </div>
        <div class="flex items-center gap-3">
            {{-- Campanita --}}
            @php $unread = \App\Models\NexusNotification::forUser(auth()->id())->unread()->count(); @endphp
            <div class="relative" x-data="{ open: false }">
                <button @click="open = !open"
                        class="relative w-9 h-9 flex items-center justify-center rounded-full bg-gray-100 text-gray-500 hover:bg-gray-200 transition">
                    <svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2">
                        <path stroke-linecap="round" stroke-linejoin="round" d="M15 17h5l-1.405-1.405A2.032 2.032 0 0118 14.158V11a6.002 6.002 0 00-4-5.659V5a2 2 0 10-4 0v.341C7.67 6.165 6 8.388 6 11v3.159c0 .538-.214 1.055-.595 1.436L4 17h5m6 0v1a3 3 0 11-6 0v-1m6 0H9"/>
                    </svg>
                    @if($unread > 0)
                    <span class="absolute -top-1 -right-1 w-4 h-4 bg-red-500 text-white text-xs font-bold rounded-full flex items-center justify-center">
                        {{ $unread > 9 ? '9+' : $unread }}
                    </span>
                    @endif
                </button>

                {{-- Dropdown notificaciones --}}
                <div x-show="open"
                     @click.outside="open = false"
                     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 top-11 w-80 bg-white rounded-2xl shadow-xl border border-gray-100 z-50 overflow-hidden">

                    <div class="px-4 py-3 border-b border-gray-50 flex items-center justify-between">
                        <span class="text-sm font-semibold text-gray-800">Notificaciones</span>
                        @if($unread > 0)
                        <form method="POST" action="{{ route('notifications.index') }}">
                            @csrf
                            <a href="{{ route('notifications.index') }}"
                               class="text-xs text-blue-600 hover:underline">Ver todas</a>
                        </form>
                        @endif
                    </div>

                    @php
                        $notifs = \App\Models\NexusNotification::forUser(auth()->id())
                            ->latest()->take(5)->get();
                    @endphp

                    <div class="divide-y divide-gray-50 max-h-72 overflow-y-auto">
                        @forelse($notifs as $notif)
                        <a href="{{ $notif->url ?? route('portal.index') }}"
                           class="flex items-start gap-3 px-4 py-3 hover:bg-gray-50 transition {{ $notif->read_at ? 'opacity-60' : '' }}">
                            <div class="w-2 h-2 rounded-full mt-2 flex-shrink-0
                                {{ $notif->read_at ? 'bg-gray-200' : 'bg-blue-500' }}">
                            </div>
                            <div class="flex-1 min-w-0">
                                <p class="text-xs font-semibold text-gray-800 leading-tight">{{ $notif->title }}</p>
                                <p class="text-xs text-gray-500 mt-0.5 leading-tight">{{ $notif->body }}</p>
                                <p class="text-xs text-gray-400 mt-1">{{ $notif->created_at->diffForHumans() }}</p>
                            </div>
                        </a>
                        @empty
                        <div class="px-4 py-8 text-center text-xs text-gray-400">
                            Sin notificaciones
                        </div>
                        @endforelse
                    </div>

                    @if($notifs->count())
                    <div class="px-4 py-2 border-t border-gray-50">
                        <a href="{{ route('notifications.index') }}"
                           class="block text-center text-xs text-blue-600 hover:underline py-1">
                            Ver todas las notificaciones
                        </a>
                    </div>
                    @endif
                </div>
            </div>

            <div class="text-right hidden sm:block">
                <p class="text-xs font-medium text-gray-800 leading-tight">{{ auth()->user()->name }}</p>
                <p class="text-xs text-gray-400 leading-tight">{{ auth()->user()->getRoleNames()->first() ?? 'Usuario' }}</p>
            </div>
            <div class="w-8 h-8 rounded-full bg-blue-600 flex items-center justify-center text-white text-xs font-bold flex-shrink-0">
                {{ strtoupper(substr(auth()->user()->name, 0, 1)) }}
            </div>
        </div>
    </div>
</header>

{{-- Contenido principal --}}
<main class="max-w-lg lg:max-w-3xl mx-auto px-4 lg:px-8 py-5 pb-24 lg:pb-8">
    @yield('content')
</main>

{{-- Bottom nav --}}
<nav class="fixed bottom-0 left-0 right-0 bg-white border-t border-gray-100 z-40 safe-bottom">
    <div class="max-w-lg lg:max-w-3xl mx-auto flex">
        <a href="{{ route('portal.index') }}"
           class="flex-1 flex flex-col items-center py-2 pt-3 {{ request()->routeIs('portal.index') ? 'text-blue-600' : 'text-gray-400' }}">
            <svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2">
                <path stroke-linecap="round" stroke-linejoin="round" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2"/>
            </svg>
            <span class="text-xs mt-0.5 font-medium">Casos</span>
        </a>
        <a href="{{ route('portal.create') }}"
           class="flex-1 flex flex-col items-center py-2 pt-3 {{ request()->routeIs('portal.create') ? 'text-blue-600' : 'text-gray-400' }}">
            <div class="{{ request()->routeIs('portal.create') ? 'bg-blue-600' : 'bg-gray-200' }} rounded-full p-1 -mt-1">
                <svg class="h-5 w-5 {{ request()->routeIs('portal.create') ? 'text-white' : 'text-gray-500' }}" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2.5">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M12 4v16m8-8H4"/>
                </svg>
            </div>
            <span class="text-xs mt-0.5 font-medium">Nueva</span>
        </a>
        <a href="{{ route('kb.index') }}"
           class="flex-1 flex flex-col items-center py-2 pt-3 {{ request()->routeIs('kb.*') ? 'text-blue-600' : 'text-gray-400' }}">
            <svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2">
                <path stroke-linecap="round" stroke-linejoin="round" d="M12 6.253v13m0-13C10.832 5.477 9.246 5 7.5 5S4.168 5.477 3 6.253v13C4.168 18.477 5.754 18 7.5 18s3.332.477 4.5 1.253m0-13C13.168 5.477 14.754 5 16.5 5c1.746 0 3.332.477 4.5 1.253v13C19.832 18.477 18.246 18 16.5 18c-1.746 0-3.332.477-4.5 1.253"/>
            </svg>
            <span class="text-xs mt-0.5 font-medium">Ayuda</span>
        </a>
        <a href="{{ route('profile') }}"
           class="flex-1 flex flex-col items-center py-2 pt-3 {{ request()->routeIs('profile') ? 'text-blue-600' : 'text-gray-400' }}">
            <svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2">
                <path stroke-linecap="round" stroke-linejoin="round" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"/>
            </svg>
            <span class="text-xs mt-0.5 font-medium">Perfil</span>
        </a>
        <form method="POST" action="{{ route('logout') }}" class="flex-1">
            @csrf
            <button type="submit" class="w-full flex flex-col items-center py-2 pt-3 text-gray-400">
                <svg class="h-5 w-5" fill="none" stroke="currentColor" viewBox="0 0 24 24" stroke-width="2">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M17 16l4-4m0 0l-4-4m4 4H7m6 4v1a3 3 0 01-3 3H6a3 3 0 01-3-3V7a3 3 0 013-3h4a3 3 0 013 3v1"/>
                </svg>
                <span class="text-xs mt-0.5 font-medium">Salir</span>
            </button>
        </form>
    </div>
</nav>

@livewireScripts
</body>
</html>
