<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
    @php
        $appName = config('settings.app_name', 'Innovaxcess Attendance');
        $favicon = config('settings.favicon');
        $logo = config('settings.logo');
    @endphp

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ $pageTitle ?? 'Dashboard' }} - {{ $appName }}</title>

    @if($favicon)
    <link rel="icon" href="{{ Storage::url($favicon) }}" type="image/x-icon">
    @endif

    <!-- PWA Configuration -->
    <link rel="manifest" href="/manifest.json">
    <meta name="theme-color" content="#3b82f6">
    <meta name="msapplication-TileColor" content="#3b82f6">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="apple-mobile-web-app-title" content="Innovaxcess">
    <link rel="apple-touch-icon" href="{{ $favicon ? Storage::url($favicon) : '/images/icon-192x192.png' }}">
    
    <!-- Android icons -->
    <link rel="icon" type="image/png" sizes="192x192" href="{{ $favicon ? Storage::url($favicon) : '/images/icon-192x192.png' }}">
    <link rel="icon" type="image/png" sizes="512x512" href="{{ $favicon ? Storage::url($favicon) : '/images/icon-512x512.png' }}">

    <!-- Tailwind CSS CDN -->
    <script src="https://cdn.tailwindcss.com"></script>
    <script>
        tailwind.config = {
            theme: {
                extend: {
                    colors: {
                        primary: {
                            50: '#eff6ff',
                            100: '#dbeafe',
                            200: '#bfdbfe',
                            300: '#93c5fd',
                            400: '#60a5fa',
                            500: '#3b82f6',
                            600: '#2563eb',
                            700: '#1d4ed8',
                            800: '#1e40af',
                            900: '#1e3a8a',
                        }
                    }
                }
            }
        }
    </script>
    
    <!-- Font Awesome -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    
    <!-- Alpine.js -->
    <script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>

    <!-- Custom Styles -->
    <style>
        .gradient-bg {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        .sidebar-transition {
            transition: all 0.3s ease-in-out;
        }
        .btn-primary {
            @apply bg-primary-600 hover:bg-primary-700 text-white font-medium py-2 px-4 rounded-lg transition duration-200 ease-in-out transform hover:scale-105;
        }
        .btn-secondary {
            @apply bg-gray-600 hover:bg-gray-700 text-white font-medium py-2 px-4 rounded-lg transition duration-200;
        }
        .card {
            @apply bg-white rounded-xl shadow-md hover:shadow-lg transition duration-300 ease-in-out;
        }
        .form-input {
            @apply w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-transparent transition duration-200;
        }
        .form-select {
            @apply w-full px-3 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-500 focus:border-transparent bg-white;
        }
        
        /* PWA Install Button */
        #install-button {
            position: fixed;
            bottom: 20px;
            right: 20px;
            z-index: 1000;
            display: none;
            background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
            color: white;
            padding: 12px 20px;
            border-radius: 50px;
            box-shadow: 0 4px 12px rgba(59, 130, 246, 0.3);
            font-weight: 600;
            cursor: pointer;
            transition: all 0.3s ease;
        }
        
        #install-button:hover {
            transform: translateY(-2px);
            box-shadow: 0 6px 16px rgba(59, 130, 246, 0.4);
        }
        
        #install-button i {
            margin-right: 8px;
        }
    </style>
</head>
<body class="font-sans antialiased bg-gray-50">
    <!-- PWA Install Button -->
    <div id="install-button" onclick="installPWA()">
        <i class="fas fa-download"></i> Install App
    </div>

    <div class="min-h-screen">
        <!-- Navigation -->
        @if (Route::has('login'))
            <nav class="bg-white shadow-sm border-b border-gray-200">
                <div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
                    <div class="flex justify-between h-16">
                        <div class="flex items-center">
                            <div class="flex-shrink-0 flex items-center space-x-3">
                                @if($logo)
                                <div class="w-10 h-10 rounded-lg flex items-center justify-center overflow-hidden">
                                    <img src="{{ Storage::url($logo) }}" 
                                         alt="{{ $appName }}" 
                                         class="max-w-full max-h-full">
                                </div>
                                @else
                                <div class="w-10 h-10 bg-gradient-to-r from-primary-600 to-purple-600 rounded-lg flex items-center justify-center">
                                    <i class="fas fa-stethoscope text-white text-lg"></i>
                                </div>
                                @endif
                                <span class="text-xl font-bold text-gray-900">{{ $appName }}</span>
                            </div>
                        </div>

                        <div class="flex items-center space-x-4">
                            @auth
                                <a href="{{ url('/dashboard') }}" class="text-gray-700 hover:text-primary-600 font-medium transition duration-200">
                                    <i class="fas fa-tachometer-alt mr-1"></i>Dashboard
                                </a>
                            @else
                                <a href="{{ route('login') }}" class="text-gray-700 hover:text-primary-600 font-medium transition duration-200">
                                    <i class="fas fa-sign-in-alt mr-1"></i>Log in
                                </a>

                                @if (Route::has('register'))
                                    <a href="{{ route('register') }}" class="text-gray-700 hover:text-primary-600 font-medium transition duration-200">
                                        <i class="fas fa-user-plus mr-1"></i>Register
                                    </a>
                                @endif
                            @endauth
                        </div>
                    </div>
                </div>
            </nav>
        @endif

        <!-- Page Content -->
        <main class="flex-1">
            {{ $slot }}
        </main>
    </div>

    <!-- PWA Script -->
    <script>
        // Register service worker
        if ('serviceWorker' in navigator) {
            window.addEventListener('load', () => {
                navigator.serviceWorker.register('/sw.js')
                    .then(registration => {
                        console.log('ServiceWorker registered:', registration);
                    })
                    .catch(error => {
                        console.log('ServiceWorker registration failed:', error);
                    });
            });
        }

        // Add to homescreen prompt
        let deferredPrompt;
        window.addEventListener('beforeinstallprompt', (e) => {
            e.preventDefault();
            deferredPrompt = e;
            
            // Show install button
            const installButton = document.getElementById('install-button');
            if (installButton) {
                installButton.style.display = 'block';
            }
            
            // Auto-hide after 10 seconds
            setTimeout(() => {
                if (installButton && installButton.style.display === 'block') {
                    installButton.style.opacity = '0';
                    installButton.style.transition = 'opacity 0.5s ease';
                    setTimeout(() => {
                        installButton.style.display = 'none';
                        installButton.style.opacity = '1';
                    }, 500);
                }
            }, 10000);
        });

        // Install PWA function
        function installPWA() {
            if (deferredPrompt) {
                deferredPrompt.prompt();
                deferredPrompt.userChoice.then((choiceResult) => {
                    if (choiceResult.outcome === 'accepted') {
                        console.log('User accepted install');
                        // Hide the install button
                        const installButton = document.getElementById('install-button');
                        if (installButton) {
                            installButton.style.display = 'none';
                        }
                    }
                    deferredPrompt = null;
                });
            }
        }

        // Check if app is already installed
        window.addEventListener('appinstalled', (evt) => {
            console.log('App installed successfully');
            // Hide install button
            const installButton = document.getElementById('install-button');
            if (installButton) {
                installButton.style.display = 'none';
            }
        });
    </script>

    <!-- Custom JavaScript -->
    <script>
        // Mobile menu toggle
        function toggleMobileMenu() {
            const menu = document.getElementById('mobile-menu');
            menu.classList.toggle('hidden');
        }

        // Dropdown toggle
        function toggleDropdown(id) {
            const dropdown = document.getElementById(id);
            dropdown.classList.toggle('hidden');
        }

        // Close dropdowns when clicking outside
        document.addEventListener('click', function(event) {
            const dropdowns = document.querySelectorAll('.dropdown-content');
            dropdowns.forEach(function(dropdown) {
                if (!dropdown.parentElement.contains(event.target)) {
                    dropdown.classList.add('hidden');
                }
            });
        });

        // Form validation
        function validateForm(formId) {
            const form = document.getElementById(formId);
            const inputs = form.querySelectorAll('input[required], select[required], textarea[required]');
            let isValid = true;

            inputs.forEach(input => {
                if (!input.value.trim()) {
                    input.classList.add('border-red-500');
                    isValid = false;
                } else {
                    input.classList.remove('border-red-500');
                }
            });

            return isValid;
        }

        // Toast notifications
        function showToast(message, type = 'success') {
            const toast = document.createElement('div');
            toast.className = `fixed top-4 right-4 p-4 rounded-lg shadow-lg text-white ${
                type === 'success' ? 'bg-green-500' : 
                type === 'error' ? 'bg-red-500' : 
                'bg-blue-500'
            } transform translate-x-full transition-transform duration-300`;
            toast.innerHTML = `
                <div class="flex items-center space-x-2">
                    <i class="fas fa-${type === 'success' ? 'check' : 'exclamation-triangle'}"></i>
                    <span>${message}</span>
                </div>
            `;
            
            document.body.appendChild(toast);
            
            setTimeout(() => {
                toast.classList.remove('translate-x-full');
            }, 100);
            
            setTimeout(() => {
                toast.classList.add('translate-x-full');
                setTimeout(() => {
                    document.body.removeChild(toast);
                }, 300);
            }, 3000);
        }

        // Auto-hide alerts
        document.addEventListener('DOMContentLoaded', function() {
            const alerts = document.querySelectorAll('.alert-auto-hide');
            alerts.forEach(alert => {
                setTimeout(() => {
                    alert.style.transition = 'all 0.5s ease-in-out';
                    alert.style.opacity = '0';
                    alert.style.height = '0';
                    alert.style.padding = '0';
                    alert.style.margin = '0';
                    setTimeout(() => {
                        alert.remove();
                    }, 500);
                }, 5000);
            });
            
            // Check if app is in standalone mode
            if (window.matchMedia('(display-mode: standalone)').matches) {
                console.log('App is running in standalone mode');
                // You can add specific behavior for standalone mode here
            }
        });
    </script>

    @stack('scripts')
</body>
</html>