<?php
if (!isset($pageTitle)) $pageTitle = getSetting('site_name', 'CRIMSON');
$siteName = getSetting('site_name', 'CRIMSON');
$tagline = getSetting('tagline', 'Manufacturer of Corrugated Boxes');
$menuItems = getMenuItems();
$contactPhone = getSetting('contact_phone', '+91 98765 43210');
$contactEmail = getSetting('contact_email', 'info@crimson.com');
$contactAddress = getSetting('contact_address', 'Industrial Estate, Sector 12, Mumbai – 400 001');
$facebook = getSetting('social_facebook', 'https://facebook.com/');
$twitter = getSetting('social_twitter', 'https://twitter.com/');
$linkedin = getSetting('social_linkedin', 'https://linkedin.com/');
$instagram = getSetting('social_instagram', 'https://instagram.com/');
// Single base for assets (avoid broken CSS when BASE_URL is wrong)
$assetBase = (BASE_URL && BASE_URL !== '/') ? rtrim(BASE_URL, '/') . '/' : '';
// Logo URL for header and favicon (browser tab icon)
$siteLogoFile = getSetting('site_logo', '');
$logoSrc = '';
if ($siteLogoFile && is_file(BASE_PATH . '/uploads/site/' . $siteLogoFile)) {
    $logoSrc = $assetBase . 'uploads/site/' . rawurlencode($siteLogoFile);
}
if ($logoSrc === '' && is_file(BASE_PATH . '/assets/images/logo.png')) {
    $logoSrc = $assetBase . 'assets/images/logo.png';
}
if ($logoSrc === '') {
    $logoSrc = $assetBase . 'assets/images/logo-dummy.svg';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo htmlspecialchars($pageTitle); ?> - <?php echo htmlspecialchars($siteName); ?></title>
    <link rel="icon" href="<?php echo htmlspecialchars($logoSrc); ?>" type="<?php echo (pathinfo($logoSrc, PATHINFO_EXTENSION) === 'svg') ? 'image/svg+xml' : 'image/png'; ?>">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Plus+Jakarta+Sans:wght@400;500;600;700;800&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="<?php echo htmlspecialchars($assetBase); ?>assets/css/style.css?v=2">
</head>
<body class="theme-crimson">
<div class="top-bar">
    <div class="top-bar-inner">
        <div class="top-bar-contact">
            <?php if ($contactPhone): ?>
            <span class="top-item">
                <span class="top-pill"><svg class="top-icon" width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><path d="M6.62 10.79c1.44 2.83 3.76 5.14 6.59 6.59l2.2-2.2c.27-.27.67-.36 1.02-.24 1.12.37 2.33.57 3.57.57.55 0 1 .45 1 1V20c0 .55-.45 1-1 1-9.39 0-17-7.61-17-17 0-.55.45-1 1-1h3.5c.55 0 1 .45 1 1 0 1.25.2 2.45.57 3.57.11.35.03.74-.25 1.02l-2.2 2.2z"/></svg>
                <a href="tel:<?php echo preg_replace('/\s+/', '', $contactPhone); ?>"><?php echo htmlspecialchars($contactPhone); ?></a></span>
            </span>
            <?php endif; ?>
            <?php if ($contactEmail): ?>
            <span class="top-item">
                <span class="top-pill"><svg class="top-icon" width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><path d="M20 4H4c-1.1 0-1.99.9-1.99 2L2 18c0 1.1.9 2 2 2h16c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 4l-8 5-8-5V6l8 5 8-5v2z"/></svg>
                <a href="mailto:<?php echo htmlspecialchars($contactEmail); ?>"><?php echo htmlspecialchars($contactEmail); ?></a></span>
            </span>
            <?php endif; ?>
            <?php if ($contactAddress): ?>
            <span class="top-item top-address"><svg class="top-icon" width="14" height="14" viewBox="0 0 24 24" fill="currentColor"><path d="M12 2C8.13 2 5 5.13 5 9c0 5.25 7 13 7 13s7-7.75 7-13c0-3.87-3.13-7-7-7zm0 9.5c-1.38 0-2.5-1.12-2.5-2.5s1.12-2.5 2.5-2.5 2.5 1.12 2.5 2.5-1.12 2.5-2.5 2.5z"/></svg>
                <span><?php echo htmlspecialchars($contactAddress); ?></span></span>
            <?php endif; ?>
        </div>
        <?php /* Follow us – uncomment when social links are set in Admin → Settings
        <div class="top-bar-social">
            <span class="social-label">Follow us</span>
            <a href="<?php echo htmlspecialchars($facebook); ?>" target="_blank" rel="noopener" class="social-btn" aria-label="Facebook">f</a>
            <a href="<?php echo htmlspecialchars($twitter); ?>" target="_blank" rel="noopener" class="social-btn" aria-label="X">𝕏</a>
            <a href="<?php echo htmlspecialchars($linkedin); ?>" target="_blank" rel="noopener" class="social-btn" aria-label="LinkedIn">in</a>
            <a href="<?php echo htmlspecialchars($instagram); ?>" target="_blank" rel="noopener" class="social-btn" aria-label="Instagram">@</a>
        </div>
        */ ?>
    </div>
</div>

<header class="site-header">
    <div class="header-inner">
        <a href="<?php echo htmlspecialchars($assetBase); ?>index.php" class="logo-link logo-link--only" title="<?php echo htmlspecialchars($siteName); ?>">
            <div class="logo-graphic logo-graphic--solo">
                <img src="<?php echo htmlspecialchars($logoSrc); ?>" alt="<?php echo htmlspecialchars($siteName); ?>" width="220" height="56" class="logo-img">
            </div>
        </a>
        <button type="button" class="nav-toggle" aria-label="Menu">☰</button>
        <nav class="main-nav">
            <ul class="nav-list">
                <?php
                $showTechDropdown = false;
                foreach ($menuItems as $item) {
                    $url = $item['url'] ?? '';
                    $baseUrl = preg_replace('/\?.*$/', '', trim($url));
                    $script = basename($baseUrl);
                    $isDropdownParent = ($script === 'technology-quality.php') || ($script === 'quality-policy.php');
                    $isPlantOnly = ($script === 'plant-machinery.php');
                    if ($isPlantOnly && $showTechDropdown) continue;
                    if ($isDropdownParent && !$showTechDropdown) {
                        $showTechDropdown = true;
                        $isActive = currentPage('quality-policy.php') || currentPage('plant-machinery.php');
                ?>
                <li class="nav-item-has-dropdown">
                    <a href="<?php echo htmlspecialchars($assetBase . 'quality-policy.php'); ?>" class="<?php echo $isActive ? 'active' : ''; ?>" aria-haspopup="true" aria-expanded="false">Technology &amp; Quality <span class="nav-dropdown-arrow" aria-hidden="true">▾</span></a>
                    <ul class="nav-dropdown">
                        <li><a href="<?php echo htmlspecialchars($assetBase . 'quality-policy.php'); ?>" class="<?php echo currentPage('quality-policy.php') ? 'active' : ''; ?>">Quality policy page</a></li>
                        <li><a href="<?php echo htmlspecialchars($assetBase . 'plant-machinery.php'); ?>" class="<?php echo currentPage('plant-machinery.php') ? 'active' : ''; ?>">Plant &amp; Machinery</a></li>
                    </ul>
                </li>
                <?php
                    } elseif ($isPlantOnly) {
                        continue;
                    } else {
                ?>
                <li>
                    <a href="<?php echo htmlspecialchars($assetBase . ltrim($url, '/')); ?>" class="<?php echo currentPage($url) ? 'active' : ''; ?>"><?php echo htmlspecialchars($item['label']); ?></a>
                </li>
                <?php
                    }
                }
                ?>
            </ul>
        </nav>
    </div>
</header>

<main class="main-content">
