<!DOCTYPE html>
<html lang="mn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <title>@yield('title', 'Админ') — NSMA</title>
    <link rel="icon" href="{{ asset('images/favicon.ico') }}">
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        'nsma-red':    '#FF3D3D',
                        'nsma-dark':   '#0F1729',
                        'nsma-darker': '#0A1020',
                    },
                    fontFamily: { sans: ['Inter', 'system-ui', 'sans-serif'] },
                },
            },
        };
    </script>
    @stack('head')
</head>
<body class="font-sans bg-gray-50 text-nsma-dark">

@php
    $admin = auth('admin')->user();
    $nav = [
        ['name' => 'Хяналтын самбар', 'route' => 'admin.dashboard', 'icon' => 'M3 12l2-2m0 0l7-7 7 7M5 10v10a1 1 0 001 1h3m10-11l2 2m-2-2v10a1 1 0 01-1 1h-3m-6 0a1 1 0 001-1v-4a1 1 0 011-1h2a1 1 0 011 1v4a1 1 0 001 1m-6 0h6'],
        ['name' => 'Захиалгууд',       'route' => 'admin.orders.index',    'icon' => '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'],
        ['name' => 'Уулзалтууд',       'route' => 'admin.meetings.index',  'icon' => 'M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z'],
        ['name' => 'Харилцагчид',      'route' => 'admin.clients.index',   'icon' => 'M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z'],
        ['name' => 'Зурвасууд',        'route' => 'admin.messages.index',  'icon' => 'M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z'],
        ['name' => 'Lead-ууд',         'route' => 'admin.leads.index',     'icon' => 'M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z'],
        ['divider' => 'Контент'],
        ['name' => 'Үйлчилгээ',        'route' => 'admin.services.index',  'icon' => 'M13 10V3L4 14h7v7l9-11h-7z'],
        ['name' => 'Хийсэн ажлууд',    'route' => 'admin.portfolios.index','icon' => 'M4 6a2 2 0 012-2h12a2 2 0 012 2v2a2 2 0 01-2 2H6a2 2 0 01-2-2V6zM4 14a2 2 0 012-2h6a2 2 0 012 2v6a2 2 0 01-2 2H6a2 2 0 01-2-2v-6zM16 14a2 2 0 012-2h2a2 2 0 012 2v6a2 2 0 01-2 2h-2a2 2 0 01-2-2v-6z'],
        ['name' => 'Хамтрагчид',       'route' => 'admin.partners.index',  'icon' => 'M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z'],
        ['name' => 'Мэдээ',            'route' => 'admin.news.index',      'icon' => 'M19 20H5a2 2 0 01-2-2V6a2 2 0 012-2h10a2 2 0 012 2v1m2 13a2 2 0 01-2-2V7m2 13a2 2 0 002-2V9a2 2 0 00-2-2h-2m-4-3H9M7 16h6M7 8h6v4H7V8z'],
        ['name' => 'Hero баннер',      'route' => 'admin.heroes.index',    'icon' => 'M4 5a1 1 0 011-1h14a1 1 0 011 1v2a1 1 0 01-1 1H5a1 1 0 01-1-1V5zM4 13a1 1 0 011-1h6a1 1 0 011 1v6a1 1 0 01-1 1H5a1 1 0 01-1-1v-6zM16 13a1 1 0 011-1h2a1 1 0 011 1v6a1 1 0 01-1 1h-2a1 1 0 01-1-1v-6z'],
        ['divider' => 'Тохиргоо'],
        ['name' => 'Тохиргоо',         'route' => 'admin.settings.edit',   'icon' => 'M10.325 4.317c.426-1.756 2.924-1.756 3.35 0a1.724 1.724 0 002.573 1.066c1.543-.94 3.31.826 2.37 2.37a1.724 1.724 0 001.065 2.572c1.756.426 1.756 2.924 0 3.35a1.724 1.724 0 00-1.066 2.573c.94 1.543-.826 3.31-2.37 2.37a1.724 1.724 0 00-2.572 1.065c-.426 1.756-2.924 1.756-3.35 0a1.724 1.724 0 00-2.573-1.066c-1.543.94-3.31-.826-2.37-2.37a1.724 1.724 0 00-1.065-2.572c-1.756-.426-1.756-2.924 0-3.35a1.724 1.724 0 001.066-2.573c-.94-1.543.826-3.31 2.37-2.37.996.608 2.296.07 2.572-1.065zM15 12a3 3 0 11-6 0 3 3 0 016 0z'],
    ];
@endphp

<div class="min-h-screen flex">
    {{-- Sidebar --}}
    <aside class="w-64 bg-nsma-dark text-white flex-shrink-0 hidden lg:flex flex-col">
        <div class="p-6 border-b border-white/10">
            <a href="{{ route('admin.dashboard') }}" class="flex items-center gap-2">
                <div class="font-black text-xl">NSMA<span class="text-nsma-red">.</span></div>
                <span class="text-xs text-gray-400 uppercase tracking-wider">админ</span>
            </a>
        </div>

        <nav class="flex-1 p-3 overflow-y-auto">
            @foreach($nav as $item)
                @if(isset($item['divider']))
                    <div class="px-3 pt-5 pb-2 text-xs uppercase tracking-wider text-gray-500 font-semibold">{{ $item['divider'] }}</div>
                @else
                    @php $active = request()->routeIs(str_replace('.index','.*', $item['route'])); @endphp
                    <a href="{{ route($item['route']) }}" class="flex items-center gap-3 px-3 py-2.5 rounded-lg text-sm transition {{ $active ? 'bg-nsma-red text-white' : 'text-gray-300 hover:bg-white/5 hover:text-white' }}">
                        <svg class="w-5 h-5 flex-shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5" d="{{ $item['icon'] }}"/></svg>
                        <span>{{ $item['name'] }}</span>
                    </a>
                @endif
            @endforeach
        </nav>

        <div class="p-4 border-t border-white/10 flex items-center gap-3">
            <img src="{{ $admin->avatarUrl() }}" alt="" class="w-9 h-9 rounded-full">
            <div class="flex-1 min-w-0">
                <div class="text-sm font-semibold truncate">{{ $admin->name }}</div>
                <div class="text-xs text-gray-400 truncate">{{ $admin->email }}</div>
            </div>
            <form action="{{ route('admin.logout') }}" method="POST">
                @csrf
                <button class="text-gray-400 hover:text-nsma-red" title="Гарах">
                    <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" 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>
                </button>
            </form>
        </div>
    </aside>

    {{-- Main content --}}
    <main class="flex-1 min-w-0">
        <header class="bg-white border-b border-gray-200 px-4 lg:px-8 h-16 flex items-center justify-between">
            <h1 class="text-lg font-bold">@yield('page_title', 'Админ')</h1>
            <div class="flex items-center gap-3">
                <a href="{{ url('/') }}" target="_blank" class="text-sm text-gray-500 hover:text-nsma-red">Сайтыг харах →</a>
            </div>
        </header>

        <div class="p-4 lg:p-8">
            @if(session('success'))
                <div class="mb-6 px-4 py-3 bg-green-50 border border-green-200 text-green-800 rounded-lg">{{ session('success') }}</div>
            @endif
            @if(session('error') || $errors->any())
                <div class="mb-6 px-4 py-3 bg-red-50 border border-red-200 text-red-800 rounded-lg">
                    {{ session('error') ?? $errors->first() }}
                </div>
            @endif

            @yield('content')
        </div>
    </main>
</div>

<script>
    window.csrfToken = document.querySelector('meta[name="csrf-token"]').content;
    window.showToast = function(message, type = 'success') {
        const toast = document.createElement('div');
        toast.className = `fixed top-6 right-6 z-50 px-6 py-4 rounded-lg shadow-lg font-medium text-white ${type === 'success' ? 'bg-green-500' : type === 'error' ? 'bg-red-500' : 'bg-nsma-dark'}`;
        toast.textContent = message;
        document.body.appendChild(toast);
        setTimeout(() => toast.remove(), 4000);
    };
    window.ajaxJson = async function(url, options = {}) {
        const res = await fetch(url, {
            method: options.method || 'POST',
            headers: {
                'X-CSRF-TOKEN': window.csrfToken,
                'X-Requested-With': 'XMLHttpRequest',
                'Accept': 'application/json',
                ...(options.body && !(options.body instanceof FormData) ? {'Content-Type': 'application/json'} : {}),
            },
            body: options.body,
        });
        return { ok: res.ok, data: await res.json().catch(() => ({})) };
    };
</script>
@stack('scripts')
</body>
</html>
