<?php
if(!isset($pdo) && file_exists(__DIR__ . '/includes/config.php')) {
    require_once __DIR__ . '/includes/config.php';
}

// Merr të gjitha settings nga database
$site_name = getSetting('site_name', $pdo, 'ESC Rental');
$site_phone = getSetting('site_phone', $pdo, '+355 69 123 4567');
$site_email = getSetting('site_email', $pdo, 'info@carrental.com');
$site_address = getSetting('site_address', $pdo, 'Tirana, Albania');
$working_hours_start = getSetting('working_hours_start', $pdo, '08:00');
$working_hours_end = getSetting('working_hours_end', $pdo, '20:00');
$facebook_url = getSetting('facebook_url', $pdo, '');
$instagram_url = getSetting('instagram_url', $pdo, '');
$whatsapp_number = getSetting('whatsapp_number', $pdo, '');

// Logo settings
$logo_url = getSetting('logo_url', $pdo, '');
$logo_width = getSetting('logo_width', $pdo, '180');
$logo_height = getSetting('logo_height', $pdo, '50');
$logo_padding_top = getSetting('logo_padding_top', $pdo, '0');
$logo_padding_bottom = getSetting('logo_padding_bottom', $pdo, '0');
$logo_padding_left = getSetting('logo_padding_left', $pdo, '0');
$logo_padding_right = getSetting('logo_padding_right', $pdo, '0');

$current_page = basename($_SERVER['PHP_SELF']);
?>
<!DOCTYPE html>
<html lang="<?php echo getCurrentLanguage(); ?>">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, viewport-fit=cover, user-scalable=yes">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
    <meta name="format-detection" content="telephone=yes">
    <meta name="description" content="Premium car rental services in Albania">
    <title><?php echo isset($page_title) ? sanitize($page_title) . ' - ' . sanitize($site_name) : sanitize($site_name); ?></title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:opsz,wght@14..32,300;14..32,400;14..32,500;14..32,600;14..32,700;14..32,800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.css"/>
    <style>
        :root {
            --primary: #0066ff;
            --primary-dark: #0052cc;
            --secondary: #1a1f36;
            --text-light: #6b7280;
            --bg-light: #f3f3f3;
            --white: #ffffff;
            --border: #e5e7eb;
            --success: #10b981;
            --warning: #f59e0b;
            --danger: #ef4444;
        }

        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            -webkit-tap-highlight-color: transparent;
        }

        body {
            font-family: -apple-system, BlinkMacSystemFont, 'Inter', 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
            background: #f3f3f3;
            color: #000000;
            line-height: 1.5;
            overflow-x: hidden;
            -webkit-font-smoothing: antialiased;
            -moz-osx-font-smoothing: grayscale;
        }

        .main-header {
            background: #f3f3f3;
            position: sticky;
            top: 0;
            z-index: 1000;
            border-bottom: 1px solid #e5e7eb;
        }

        .top-bar {
            background: #e8e8e8;
            padding: 0.4rem 5%;
            font-size: 0.65rem;
            display: flex;
            justify-content: space-between;
            flex-wrap: wrap;
            gap: 0.5rem;
            color: #333333;
            border-bottom: 1px solid #ddd;
        }

        .top-bar-left span, .top-bar-right span {
            margin-right: 1rem;
        }

        .top-bar-left i, .top-bar-right i {
            margin-right: 0.3rem;
            color: var(--primary);
        }

        .top-bar-right a {
            color: #333333;
            text-decoration: none;
            transition: color 0.3s;
        }

        .top-bar-right a:hover {
            color: var(--primary);
        }

        /* ============================================
           LANGUAGE DROPDOWN - Desktop
           ============================================ */
        .language-dropdown {
            position: relative;
            display: inline-block;
            margin-left: 10px;
            cursor: pointer;
        }
        .language-dropdown .selected-lang {
            display: flex;
            align-items: center;
            gap: 4px;
            padding: 2px 6px 2px 6px;
            border-radius: 6px;
            background: rgba(255,255,255,0.5);
            border: 1px solid transparent;
            transition: background 0.2s, border-color 0.2s;
            font-size: 12px;
            color: #333333;
            line-height: 1;
            width: fit-content;
            min-width: 0;
            max-width: fit-content;
        }
        .language-dropdown .selected-lang:hover {
            background: rgba(255,255,255,0.9);
            border-color: #ddd;
        }
        .language-dropdown .selected-lang img {
            width: 18px;
            height: 12px;
            object-fit: cover;
            border-radius: 2px;
            display: block;
            flex-shrink: 0;
        }
        .language-dropdown .selected-lang .arrow {
            font-size: 8px;
            color: #666;
            transition: transform 0.2s;
            margin: 0;
            line-height: 1;
            display: inline-block;
        }
        .language-dropdown .selected-lang .arrow.open {
            transform: rotate(180deg);
        }
        
        .language-dropdown .dropdown-menu {
            display: none;
            position: absolute;
            top: 100%;
            right: 0;
            background: white;
            min-width: 130px;
            border-radius: 10px;
            box-shadow: 0 8px 30px rgba(0,0,0,0.15);
            border: 1px solid #e5e7eb;
            padding: 6px 0;
            margin-top: 6px;
            z-index: 1001;
            animation: dropdownFadeIn 0.2s ease;
        }
        .language-dropdown .dropdown-menu.show {
            display: block;
        }
        
        @keyframes dropdownFadeIn {
            from { opacity: 0; transform: translateY(-8px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .language-dropdown .dropdown-menu .lang-item {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 8px 14px;
            text-decoration: none;
            color: #333333;
            font-size: 13px;
            transition: background 0.15s;
            cursor: pointer;
            border: none;
            background: none;
            width: 100%;
            text-align: left;
        }
        .language-dropdown .dropdown-menu .lang-item:hover {
            background: #f0f4ff;
        }
        .language-dropdown .dropdown-menu .lang-item img {
            width: 24px;
            height: 16px;
            object-fit: cover;
            border-radius: 2px;
            display: block;
            flex-shrink: 0;
        }
        .language-dropdown .dropdown-menu .lang-item .lang-name {
            font-weight: 500;
        }
        .language-dropdown .dropdown-menu .lang-item.active {
            background: #e8edf5;
            font-weight: 600;
        }
        .language-dropdown .dropdown-menu .lang-item.active .lang-name {
            color: var(--primary);
        }

        /* ============================================
           LANGUAGE DROPDOWN - Mobile
           ============================================ */
        .language-dropdown-mobile {
            display: none;
            position: relative;
            margin-left: auto;
            margin-right: 8px;
        }
        .language-dropdown-mobile .selected-lang-mobile {
            display: flex;
            align-items: center;
            padding: 2px;
            cursor: pointer;
            border-radius: 4px;
            transition: background 0.2s;
        }
        .language-dropdown-mobile .selected-lang-mobile:hover {
            background: rgba(0,0,0,0.05);
        }
        .language-dropdown-mobile .selected-lang-mobile img {
            width: 26px;
            height: 17px;
            object-fit: cover;
            border-radius: 3px;
            display: block;
            flex-shrink: 0;
        }
        .language-dropdown-mobile .dropdown-menu-mobile {
            display: none;
            position: absolute;
            top: 100%;
            right: 0;
            background: white;
            min-width: 130px;
            border-radius: 10px;
            box-shadow: 0 8px 30px rgba(0,0,0,0.15);
            border: 1px solid #e5e7eb;
            padding: 6px 0;
            margin-top: 6px;
            z-index: 1001;
            animation: dropdownFadeIn 0.2s ease;
        }
        .language-dropdown-mobile .dropdown-menu-mobile.show {
            display: block;
        }
        .language-dropdown-mobile .dropdown-menu-mobile .lang-item-mobile {
            display: flex;
            align-items: center;
            gap: 10px;
            padding: 8px 14px;
            text-decoration: none;
            color: #333333;
            font-size: 13px;
            transition: background 0.15s;
            cursor: pointer;
            border: none;
            background: none;
            width: 100%;
            text-align: left;
        }
        .language-dropdown-mobile .dropdown-menu-mobile .lang-item-mobile:hover {
            background: #f0f4ff;
        }
        .language-dropdown-mobile .dropdown-menu-mobile .lang-item-mobile img {
            width: 24px;
            height: 16px;
            object-fit: cover;
            border-radius: 2px;
            display: block;
            flex-shrink: 0;
        }
        .language-dropdown-mobile .dropdown-menu-mobile .lang-item-mobile.active {
            background: #e8edf5;
            font-weight: 600;
        }

        .header-container {
            display: flex;
            justify-content: space-between;
            align-items: center;
            flex-wrap: wrap;
            gap: 1rem;
            padding: 1rem 5%;
            max-width: 1400px;
            margin: 0 auto;
            position: relative;
        }

        .logo {
            display: flex;
            align-items: center;
            flex-shrink: 0;
        }
        
        .logo a {
            text-decoration: none;
            display: flex;
            align-items: center;
            -webkit-tap-highlight-color: transparent;
        }
        
        .logo-img {
            width: <?php echo (int)$logo_width; ?>px !important;
            height: auto !important;
            max-width: none !important;
            max-height: none !important;
            object-fit: contain;
            padding-top: <?php echo (int)$logo_padding_top; ?>px;
            padding-bottom: <?php echo (int)$logo_padding_bottom; ?>px;
            padding-left: <?php echo (int)$logo_padding_left; ?>px;
            padding-right: <?php echo (int)$logo_padding_right; ?>px;
        }
        
        .logo-text {
            font-size: 1.5rem;
            font-weight: 800;
            letter-spacing: -0.5px;
            color: #000000;
        }
        
        .logo-text span {
            color: var(--primary);
        }
        
        .logo-text small {
            font-size: 0.55rem;
            font-weight: normal;
            color: #666666;
            display: block;
            letter-spacing: 0;
        }

        .nav-menu {
            display: flex;
            gap: 2rem;
            align-items: center;
            flex-wrap: wrap;
        }

        .nav-menu a {
            text-decoration: none;
            color: #333333;
            font-weight: 500;
            transition: color 0.3s;
            font-size: 0.9rem;
            -webkit-tap-highlight-color: transparent;
        }

        .nav-menu a:hover {
            color: var(--primary);
        }

        .nav-menu a.active {
            color: var(--primary);
        }

        .mobile-menu-btn {
            display: none;
            font-size: 1.5rem;
            cursor: pointer;
            color: #000000;
            background: none;
            border: none;
            padding: 8px;
            -webkit-tap-highlight-color: transparent;
            transition: transform 0.2s ease;
        }
        
        .mobile-menu-btn:active {
            transform: scale(0.95);
        }

        .menu-overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: rgba(0,0,0,0.4);
            z-index: 998;
            opacity: 0;
            visibility: hidden;
            transition: opacity 0.3s ease, visibility 0.3s ease;
        }
        .menu-overlay.show {
            opacity: 1;
            visibility: visible;
        }

        /* ============================================
           MOBILE RESPONSIVE
           ============================================ */
        @media (max-width: 768px) {
            .top-bar-right {
                display: none;
            }
            
            .top-bar {
                padding: 0.6rem 4%;
                font-size: 0.75rem;
                gap: 0.5rem;
                justify-content: center;
            }
            
            .top-bar-left {
                display: flex;
                flex-wrap: nowrap;
                gap: 0.8rem;
                justify-content: center;
                width: 100%;
            }
            
            .top-bar-left span {
                margin: 0;
                font-size: 0.7rem;
                white-space: nowrap;
                display: inline-flex;
                align-items: center;
            }
            
            .top-bar-left i {
                font-size: 0.75rem;
                margin-right: 0.25rem;
            }
            
            .language-dropdown-mobile {
                display: block;
            }
            
            .mobile-menu-btn {
                display: block;
            }
            
            .nav-menu {
                position: absolute;
                top: 100%;
                left: 0;
                right: 0;
                width: 100%;
                background: #f3f3f3;
                display: flex;
                flex-direction: column;
                gap: 0;
                padding: 0;
                max-height: 0;
                opacity: 0;
                visibility: hidden;
                overflow: hidden;
                box-shadow: 0 4px 12px rgba(0,0,0,0.1);
                border-top: 1px solid #e5e7eb;
                border-bottom: 1px solid #e5e7eb;
                z-index: 999;
                transition: max-height 0.3s cubic-bezier(0.4, 0, 0.2, 1), 
                            opacity 0.25s ease, 
                            visibility 0.25s ease;
            }
            
            .nav-menu.show {
                max-height: 300px;
                opacity: 1;
                visibility: visible;
            }
            
            .nav-menu a {
                width: 100%;
                text-align: center;
                padding: 14px 5%;
                opacity: 0;
                transform: translateY(-8px);
                transition: opacity 0.25s ease, transform 0.25s ease, background 0.2s;
                border-radius: 0;
                font-size: 1rem;
                border-bottom: 1px solid #f0f0f0;
            }
            
            .nav-menu a:last-child {
                border-bottom: none;
            }
            
            .nav-menu.show a {
                opacity: 1;
                transform: translateY(0);
            }
            
            .nav-menu.show a:nth-child(1) { transition-delay: 0.02s; }
            .nav-menu.show a:nth-child(2) { transition-delay: 0.05s; }
            .nav-menu.show a:nth-child(3) { transition-delay: 0.08s; }
            
            .nav-menu a:active {
                background: rgba(0,102,255,0.08);
            }
        }
        
        @media (max-width: 480px) {
            .top-bar {
                padding: 0.5rem 3%;
                gap: 0.3rem;
            }
            
            .top-bar-left {
                gap: 0.5rem;
            }
            
            .top-bar-left span {
                font-size: 0.6rem;
            }
            
            .top-bar-left i {
                font-size: 0.65rem;
                margin-right: 0.2rem;
            }
            
            .header-container {
                padding: 0.6rem 4%;
                gap: 0.5rem;
            }
        }

        .fade-up {
            opacity: 0;
            transform: translateY(30px);
            transition: all 0.6s ease;
        }
        
        .fade-up.visible {
            opacity: 1;
            transform: translateY(0);
        }
    </style>
    <?php
    $ga_id = getSetting('google_analytics_id', $pdo, '');
    if($ga_id): ?>
    <script async src="https://www.googletagmanager.com/gtag/js?id=<?php echo $ga_id; ?>"></script>
    <script>
        window.dataLayer = window.dataLayer || [];
        function gtag(){dataLayer.push(arguments);}
        gtag('js', new Date());
        gtag('config', '<?php echo $ga_id; ?>');
    </script>
    <?php endif; ?>
</head>
<body>
    <header class="main-header">
        <div class="top-bar">
            <div class="top-bar-left">
                <span><i class="fas fa-phone-alt"></i> <?php echo sanitize($site_phone); ?></span>
                <span><i class="fas fa-envelope"></i> <?php echo sanitize($site_email); ?></span>
                <span><i class="fas fa-map-marker-alt"></i> <?php echo sanitize($site_address); ?></span>
            </div>
            <div class="top-bar-right">
                <span><i class="far fa-clock"></i> <?php echo sanitize($working_hours_start); ?> - <?php echo sanitize($working_hours_end); ?></span>
                
                <!-- Language Dropdown - Desktop -->
                <div class="language-dropdown" id="langDropdown">
                    <div class="selected-lang" onclick="toggleDropdown()">
                        <img src="/lang/<?php echo getCurrentLanguage(); ?>.png" alt="<?php echo getCurrentLanguage(); ?>" width="18" height="12">
                        <span class="arrow" id="dropdownArrow">▼</span>
                    </div>
                    <div class="dropdown-menu" id="dropdownMenu">
                        <?php foreach($available_languages as $code => $lang): ?>
                            <button class="lang-item <?php echo getCurrentLanguage() == $code ? 'active' : ''; ?>" 
                                    onclick="changeLanguage('<?php echo $code; ?>')">
                                <img src="/lang/<?php echo $code; ?>.png" alt="<?php echo $code; ?>" width="24" height="16">
                                <span class="lang-name"><?php echo $lang['name']; ?></span>
                            </button>
                        <?php endforeach; ?>
                    </div>
                </div>
            </div>
        </div>

        <div class="header-container">
            <div class="logo">
                <a href="index.php">
                    <?php if(!empty($logo_url) && file_exists($logo_url)): ?>
                        <img src="<?php echo $logo_url; ?>" alt="<?php echo sanitize($site_name); ?>" class="logo-img">
                    <?php else: ?>
                        <div class="logo-text">
                            <span>ESC</span>RENTAL<small>.al</small>
                        </div>
                    <?php endif; ?>
                </a>
            </div>
            
            <!-- Language Dropdown - Mobile -->
            <div class="language-dropdown-mobile" id="langDropdownMobile">
                <div class="selected-lang-mobile" onclick="toggleDropdownMobile()">
                    <img src="/lang/<?php echo getCurrentLanguage(); ?>.png" alt="<?php echo getCurrentLanguage(); ?>" id="mobileFlagImg" width="26" height="17">
                </div>
                <div class="dropdown-menu-mobile" id="dropdownMenuMobile">
                    <?php foreach($available_languages as $code => $lang): ?>
                        <button class="lang-item-mobile <?php echo getCurrentLanguage() == $code ? 'active' : ''; ?>" 
                                onclick="changeLanguage('<?php echo $code; ?>')">
                            <img src="/lang/<?php echo $code; ?>.png" alt="<?php echo $code; ?>" width="24" height="16">
                            <span class="lang-name"><?php echo $lang['name']; ?></span>
                        </button>
                    <?php endforeach; ?>
                </div>
            </div>
            
            <button class="mobile-menu-btn" id="mobileMenuBtn" aria-label="Menu">
                <i class="fas fa-bars"></i>
            </button>
            <div class="nav-menu" id="navMenu">
                <a href="index.php" class="<?php echo $current_page == 'index.php' ? 'active' : ''; ?>"><?php echo t('nav_home'); ?></a>
                <a href="all-cars.php" class="<?php echo $current_page == 'all-cars.php' ? 'active' : ''; ?>"><?php echo t('nav_all_cars'); ?></a>
                <a href="faq.php" class="<?php echo $current_page == 'faq.php' ? 'active' : ''; ?>"><?php echo t('nav_faq'); ?></a>
            </div>
        </div>
    </header>

    <div id="menuOverlay" class="menu-overlay"></div>

    <script src="https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.js"></script>
    <script>
        (function() {
            const menuBtn = document.getElementById('mobileMenuBtn');
            const navMenu = document.getElementById('navMenu');
            const menuOverlay = document.getElementById('menuOverlay');
            
            if(!menuBtn || !navMenu) return;
            
            let isAnimating = false;
            
            function closeMenu() {
                if(isAnimating) return;
                isAnimating = true;
                
                navMenu.classList.remove('show');
                if(menuOverlay) menuOverlay.classList.remove('show');
                
                const icon = menuBtn.querySelector('i');
                if(icon) icon.className = 'fas fa-bars';
                
                setTimeout(() => {
                    isAnimating = false;
                }, 300);
            }
            
            function openMenu() {
                if(isAnimating) return;
                isAnimating = true;
                
                navMenu.classList.add('show');
                if(menuOverlay) menuOverlay.classList.add('show');
                
                const icon = menuBtn.querySelector('i');
                if(icon) icon.className = 'fas fa-times';
                
                document.body.style.overflow = 'hidden';
                
                setTimeout(() => {
                    isAnimating = false;
                }, 300);
            }
            
            function toggleMenu(e) {
                if(e) {
                    e.preventDefault();
                    e.stopPropagation();
                }
                
                if(navMenu.classList.contains('show')) {
                    closeMenu();
                    document.body.style.overflow = '';
                } else {
                    openMenu();
                }
            }
            
            menuBtn.addEventListener('click', toggleMenu);
            
            const menuLinks = navMenu.querySelectorAll('a');
            menuLinks.forEach(link => {
                link.addEventListener('click', function(e) {
                    if(navMenu.classList.contains('show')) {
                        e.preventDefault();
                        const href = this.getAttribute('href');
                        
                        closeMenu();
                        document.body.style.overflow = '';
                        
                        setTimeout(() => {
                            if(href && href !== '#') {
                                window.location.href = href;
                            }
                        }, 200);
                    }
                });
            });
            
            if(menuOverlay) {
                menuOverlay.addEventListener('click', function(e) {
                    closeMenu();
                    document.body.style.overflow = '';
                });
            }
            
            menuBtn.addEventListener('click', function(e) {
                e.stopPropagation();
            });
            
            document.addEventListener('keydown', function(e) {
                if(e.key === 'Escape' && navMenu.classList.contains('show')) {
                    closeMenu();
                    document.body.style.overflow = '';
                }
            });
            
            let resizeTimer;
            window.addEventListener('resize', function() {
                clearTimeout(resizeTimer);
                resizeTimer = setTimeout(function() {
                    if(window.innerWidth > 768 && navMenu.classList.contains('show')) {
                        closeMenu();
                        document.body.style.overflow = '';
                    }
                }, 250);
            });
        })();

        // ============================================
        // LANGUAGE DROPDOWN FUNCTIONS
        // ============================================
        
        function changeLanguage(lang) {
            const url = new URL(window.location.href);
            url.searchParams.set('lang', lang);
            window.location.href = url.toString();
        }
        
        function toggleDropdown() {
            const menu = document.getElementById('dropdownMenu');
            const arrow = document.getElementById('dropdownArrow');
            if (!menu || !arrow) return;
            menu.classList.toggle('show');
            arrow.classList.toggle('open');
        }
        
        function toggleDropdownMobile() {
            const menu = document.getElementById('dropdownMenuMobile');
            if (!menu) return;
            menu.classList.toggle('show');
        }
        
        // Close dropdowns when clicking outside
        document.addEventListener('click', function(e) {
            const desktopDropdown = document.getElementById('langDropdown');
            const mobileDropdown = document.getElementById('langDropdownMobile');
            
            if(desktopDropdown && !desktopDropdown.contains(e.target)) {
                const menu = document.getElementById('dropdownMenu');
                const arrow = document.getElementById('dropdownArrow');
                if (menu) menu.classList.remove('show');
                if (arrow) arrow.classList.remove('open');
            }
            
            if(mobileDropdown && !mobileDropdown.contains(e.target)) {
                const menu = document.getElementById('dropdownMenuMobile');
                if (menu) menu.classList.remove('show');
            }
        });
        
        // Scroll animations
        document.addEventListener('DOMContentLoaded', function() {
            const observerOptions = { threshold: 0.1, rootMargin: '0px 0px -50px 0px' };
            const observer = new IntersectionObserver((entries) => {
                entries.forEach(entry => {
                    if(entry.isIntersecting) {
                        entry.target.classList.add('visible');
                    }
                });
            }, observerOptions);
            document.querySelectorAll('.fade-up').forEach(el => observer.observe(el));
        });
    </script>
</body>
</html>