<!DOCTYPE html>
<html lang="en">
<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', 'Winery - Catalog')</title>
    @vite(['resources/css/app.css', 'resources/js/app.js'])
    <style>
        html {
            font-size: 80%;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
            background-color: #f8f9fa;
            color: #333;
            min-height: 100vh;
            display: flex;
            flex-direction: column;
        }

        /* Top Navigation Bar */
        .top-bar {
            background: linear-gradient(135deg, #89b4d9 0%, #a8cce5 100%);
            padding: 0;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
            position: sticky;
            top: 0;
            z-index: 1000;
        }

        .nav-container {
            max-width: 85%;
            margin: 0 auto;
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 0 2rem;
            height: 60px;
        }

        .nav-brand {
            font-size: 1.5rem;
            font-weight: 600;
            color: #1a365d;
            text-decoration: none;
            display: flex;
            align-items: center;
            gap: 0.5rem;
            flex: 1;
        }

        /* Hamburger Menu Button */
        .hamburger-button {
            display: none;
            background: rgba(255, 255, 255, 0.25);
            border: none;
            border-radius: 8px;
            padding: 0.5rem 1rem;
            cursor: pointer;
            transition: all 0.2s;
            color: #1a365d;
        }

        .hamburger-button:hover {
            background: rgba(255, 255, 255, 0.4);
        }

        .hamburger-icon {
            display: flex;
            flex-direction: column;
            gap: 4px;
            width: 20px;
            height: 20px;
            justify-content: center;
        }

        .hamburger-icon span {
            display: block;
            width: 100%;
            height: 3px;
            background: #1a365d;
            border-radius: 2px;
            transition: all 0.3s;
        }

        .hamburger-button.active .hamburger-icon span:nth-child(1) {
            transform: rotate(45deg) translate(6px, 6px);
        }

        .hamburger-button.active .hamburger-icon span:nth-child(2) {
            opacity: 0;
        }

        .hamburger-button.active .hamburger-icon span:nth-child(3) {
            transform: rotate(-45deg) translate(6px, -6px);
        }

        .nav-menu {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 2rem;
            list-style: none;
            flex: 1;
        }

        .nav-link {
            color: #1a365d;
            text-decoration: none;
            font-weight: 500;
            font-size: 0.95rem;
            padding: 0.5rem 1rem;
            border-radius: 6px;
            transition: all 0.2s;
            white-space: nowrap;
        }

        .nav-link:hover {
            background: rgba(255, 255, 255, 0.3);
        }

        .nav-actions {
            display: flex;
            align-items: center;
            justify-content: flex-end;
            gap: 1.5rem;
            flex: 1;
        }

        .cart-button {
            position: relative;
            display: flex;
            align-items: center;
            gap: 0.5rem;
            padding: 0.5rem 1rem;
            background: rgba(255, 255, 255, 0.25);
            border: none;
            border-radius: 8px;
            color: #1a365d;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s;
            text-decoration: none;
        }

        .cart-button:hover {
            background: rgba(255, 255, 255, 0.4);
        }

        .cart-badge {
            position: absolute;
            top: -6px;
            right: -6px;
            background: #dc3545;
            color: white;
            border-radius: 50%;
            width: 22px;
            height: 22px;
            display: flex;
            align-items: center;
            justify-content: center;
            font-size: 0.75rem;
            font-weight: 600;
        }

        .user-menu {
            position: relative;
        }

        /* Extend hover area downward to prevent dropdown from closing */
        .user-menu::after {
            content: '';
            position: absolute;
            top: 100%;
            left: 0;
            right: 0;
            height: 0.5rem;
        }

        .user-button {
            display: flex;
            align-items: center;
            gap: 0.5rem;
            padding: 0.5rem 1rem;
            background: rgba(255, 255, 255, 0.25);
            border: none;
            border-radius: 8px;
            color: #1a365d;
            font-weight: 500;
            cursor: pointer;
            transition: all 0.2s;
        }

        .user-button:hover {
            background: rgba(255, 255, 255, 0.4);
        }

        .dropdown-menu {
            display: none;
            position: absolute;
            top: calc(100% + 0.5rem);
            right: 0;
            background: white;
            border-radius: 8px;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
            min-width: 180px;
            overflow: hidden;
            z-index: 1000;
        }

        .user-menu:hover .dropdown-menu {
            display: block;
        }

        .dropdown-item {
            display: block;
            padding: 0.75rem 1rem;
            color: #333;
            text-decoration: none;
            transition: background 0.2s;
        }

        .dropdown-item:hover {
            background: #f8f9fa;
        }

        .dropdown-item-logout {
            display: block;
            padding: 0.75rem 1rem;
            color: #dc3545;
            text-decoration: none;
            transition: all 0.2s;
            border-left: 2px solid #dc3545;
            border-right: 2px solid #dc3545;
            border-bottom: 2px solid #dc3545;
            border-top: 2px solid #dc3545;
            border-bottom-left-radius: 8px;
            border-bottom-right-radius: 8px;
            margin: 0;
            width: 100%;
            background: white;
        }

        .dropdown-item-logout:hover {
            background: #f8f9fa !important;
        }

        .dropdown-divider {
            height: 1px;
            background: #e9ecef;
            margin: 0.5rem 0;
        }

        /* Flash Messages */
        .flash-messages {
            max-width: 85%;
            margin: 1rem auto;
            padding: 0 2rem;
        }

        .alert {
            padding: 1rem 1.5rem;
            border-radius: 8px;
            margin-bottom: 1rem;
            display: flex;
            align-items: center;
            gap: 0.75rem;
        }

        .alert-success {
            background: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .alert-error {
            background: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }

        .alert-info {
            background: #d1ecf1;
            color: #0c5460;
            border: 1px solid #bee5eb;
        }

        /* Main Content */
        main {
            flex: 1;
            max-width: 85%;
            margin: 0 auto;
            padding: 2rem;
            width: 100%;
        }

        /* Footer */
        footer {
            background: #2c3e50;
            color: #ecf0f1;
            padding: 2rem;
            margin-top: 4rem;
        }

        .footer-content {
            max-width: 85%;
            margin: 0 auto;
            text-align: center;
        }

        /* Icons */
        .icon {
            width: 20px;
            height: 20px;
            display: inline-block;
        }

        /* Mobile Responsive */
        @media (max-width: 768px) {
            .nav-container {
                padding: 0 1rem;
                flex-wrap: wrap;
            }

            .nav-brand {
                order: 1;
                flex: 1;
            }

            .hamburger-button {
                display: flex;
                order: 2;
                justify-content: center;
                flex: 1;
                margin-left: 0.5rem;
                margin-right: 0.5rem;
            }

            .nav-actions {
                order: 3;
                gap: 0.75rem;
                flex: 1;
                justify-content: flex-end;
            }

            .nav-menu {
                order: 4;
                display: none;
                position: absolute;
                top: 60px;
                left: 0;
                right: 0;
                flex-direction: column;
                background: linear-gradient(135deg, #89b4d9 0%, #a8cce5 100%);
                padding: 1rem;
                gap: 0.5rem;
                box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
                z-index: 999;
            }

            .nav-menu.active {
                display: flex;
            }

            .nav-menu li {
                width: 100%;
            }

            .nav-link {
                display: block;
                width: 100%;
                text-align: center;
                padding: 0.75rem 1rem;
            }

            main {
                padding: 1rem;
            }
        }
    </style>
    @stack('styles')
</head>
<body>
    <!-- Top Navigation Bar -->
    <nav class="top-bar">
        <div class="nav-container">
            <a href="{{ url('/') }}" class="nav-brand">
                <svg class="icon" fill="currentColor" viewBox="0 0 24 24">
                    <path d="M12 2L2 7v10c0 5.55 3.84 10.74 9 12 5.16-1.26 9-6.45 9-12V7l-10-5zm0 2.18l8 3.6v8.55c0 4.45-2.94 8.62-7 9.67-4.06-1.05-7-5.22-7-9.67V7.78l6-2.7v13.48h2V4.18z"/>
                </svg>
                Winery
            </a>

            <button class="hamburger-button" id="mobile-menu-button" aria-label="Toggle navigation">
                <div class="hamburger-icon">
                    <span></span>
                    <span></span>
                    <span></span>
                </div>
            </button>

            <ul class="nav-menu" id="mobile-menu">
                <li><a href="{{ route('catalog.index') }}" class="nav-link">Catalog</a></li>
                <li><a href="{{ url('/') }}" class="nav-link">About Us</a></li>
                <li><a href="{{ route('contact') }}" class="nav-link">Contact</a></li>
            </ul>

            <div class="nav-actions">
                <!-- Cart Button with Badge -->
                <a href="{{ route('cart.index') }}" class="cart-button">
                    <svg class="icon" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 3h2l.4 2M7 13h10l4-8H5.4M7 13L5.4 5M7 13l-2.293 2.293c-.63.63-.184 1.707.707 1.707H17m0 0a2 2 0 100 4 2 2 0 000-4zm-8 2a2 2 0 11-4 0 2 2 0 014 0z"/>
                    </svg>
                    @if(session()->has('cart') && count(session('cart')) > 0)
                        <span class="cart-badge">{{ array_sum(array_column(session('cart'), 'quantity')) }}</span>
                    @endif
                </a>

                <!-- User Menu -->
                @auth
                <div class="user-menu">
                    <button class="user-button">
                        <svg class="icon" fill="currentColor" viewBox="0 0 24 24">
                            <path d="M12 12c2.21 0 4-1.79 4-4s-1.79-4-4-4-4 1.79-4 4 1.79 4 4 4zm0 2c-2.67 0-8 1.34-8 4v2h16v-2c0-2.66-5.33-4-8-4z"/>
                        </svg>
                        {{ auth()->user()->username }}
                        <svg class="icon" style="width: 16px; height: 16px;" fill="currentColor" viewBox="0 0 20 20">
                            <path fill-rule="evenodd" d="M5.293 7.293a1 1 0 011.414 0L10 10.586l3.293-3.293a1 1 0 111.414 1.414l-4 4a1 1 0 01-1.414 0l-4-4a1 1 0 010-1.414z" clip-rule="evenodd"/>
                        </svg>
                    </button>
                    <div class="dropdown-menu">
                        @if(auth()->user()->role === \App\Enums\UserRole::CUSTOMER)
                            <a href="{{ route('purchases.my') }}" class="dropdown-item">My Orders</a>
                            <a href="{{ route('events.my-reservations') }}" class="dropdown-item">Reserved Events</a>
                        @endif
                        @if(auth()->user()->role === \App\Enums\UserRole::EMPLOYEE)
                            <a href="{{ route('purchases.my') }}" class="dropdown-item">My Orders</a>
                            <a href="{{ route('events.my-reservations') }}" class="dropdown-item">Reserved Events</a>
                            <div class="dropdown-divider"></div>
                            <a href="{{ route('employee.tasks.index') }}" class="dropdown-item">My Tasks</a>
                        @endif
                        @if(auth()->user()->isWinemaker())
                            <a href="{{ route('purchases.my') }}" class="dropdown-item">My Orders</a>
                            <div class="dropdown-divider"></div>
                            <a href="{{ route('winemaker.dashboard') }}" class="dropdown-item">Winemaker Dashboard</a>
                        @endif
                        @if(auth()->user()->role === \App\Enums\UserRole::ADMIN)
                            <a href="{{ route('admin.users.index') }}" class="dropdown-item">User Management</a>
                            <a href="{{ route('admin.events.index') }}" class="dropdown-item">Event Management</a>
                        @endif
                        <form action="{{ route('logout') }}" method="POST" style="margin: 0; margin-top: 0.5rem;">
                            @csrf
                            <button type="submit" class="dropdown-item-logout" style="width: 100%; text-align: left; background: none; cursor: pointer; font: inherit;">
                                Logout
                            </button>
                        </form>
                    </div>
                </div>
                @else
                <a href="{{ route('login') }}" class="user-button" style="text-decoration: none;">
                    <svg class="icon" fill="currentColor" viewBox="0 0 24 24">
                        <path d="M11 7L9.6 8.4l2.6 2.6H2v2h10.2l-2.6 2.6L11 17l5-5-5-5zm9 12h-8v2h8c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2h-8v2h8v14z"/>
                    </svg>
                    Login
                </a>
                @endauth
            </div>
        </div>
    </nav>

    <!-- Flash Messages -->
    @if(session('success') || session('error') || session('info'))
    <div class="flash-messages">
        @if(session('success'))
        <div class="alert alert-success">
            <svg class="icon" fill="currentColor" viewBox="0 0 20 20">
                <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd"/>
            </svg>
            {{ session('success') }}
        </div>
        @endif

        @if(session('error'))
        <div class="alert alert-error">
            <svg class="icon" fill="currentColor" viewBox="0 0 20 20">
                <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clip-rule="evenodd"/>
            </svg>
            {{ session('error') }}
        </div>
        @endif

        @if(session('info'))
        <div class="alert alert-info">
            <svg class="icon" fill="currentColor" viewBox="0 0 20 20">
                <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7-4a1 1 0 11-2 0 1 1 0 012 0zM9 9a1 1 0 000 2v3a1 1 0 001 1h1a1 1 0 100-2v-3a1 1 0 00-1-1H9z" clip-rule="evenodd"/>
            </svg>
            {{ session('info') }}
        </div>
        @endif
    </div>
    @endif

    <!-- Main Content -->
    <main>
        @yield('content')
    </main>

    <!-- Footer -->
    <footer>
        <div class="footer-content">
            <p>&copy; {{ date('Y') }} Winery. All rights reserved.</p>
        </div>
    </footer>

    @stack('scripts')

    <script>
        // Mobile menu toggle
        document.addEventListener('DOMContentLoaded', function() {
            const mobileMenuButton = document.getElementById('mobile-menu-button');
            const mobileMenu = document.getElementById('mobile-menu');

            if (mobileMenuButton && mobileMenu) {
                mobileMenuButton.addEventListener('click', function() {
                    mobileMenuButton.classList.toggle('active');
                    mobileMenu.classList.toggle('active');
                });

                // Close menu when clicking on a link
                const menuLinks = mobileMenu.querySelectorAll('.nav-link');
                menuLinks.forEach(link => {
                    link.addEventListener('click', function() {
                        mobileMenuButton.classList.remove('active');
                        mobileMenu.classList.remove('active');
                    });
                });

                // Close menu when clicking outside
                document.addEventListener('click', function(event) {
                    if (!mobileMenu.contains(event.target) && !mobileMenuButton.contains(event.target)) {
                        mobileMenuButton.classList.remove('active');
                        mobileMenu.classList.remove('active');
                    }
                });
            }
        });
    </script>
</body>
</html>
