<?php
$current_path = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH);
$current_path = rtrim($current_path, '/');
if ($current_path === '') $current_path = '/';

function nav_active($href, $current) {
    // Exact match
    if ($href === $current) return ' active';
    // Section match (e.g. /tools/ matches /tools/cost-calculator)
    $href_trimmed = rtrim($href, '/');
    if ($href_trimmed !== '' && $href_trimmed !== '/' && strpos($current, $href_trimmed) === 0) return ' active';
    return '';
}
?>
<!-- Navigation -->
<nav class="navbar" id="navbar" aria-label="Main navigation">
    <div class="nav-container">
        <div class="nav-logo">
            <a href="/">
                <picture>
                    <source srcset="/assets/images/ukds-main-logo.webp" type="image/webp">
                    <img src="/assets/images/ukds-main-logo.png" alt="UK Data Services" class="logo">
                </picture>
            </a>
        </div>
        <div class="nav-menu" id="nav-menu">
            <a href="/" class="nav-link<?php echo $current_path === '/' ? ' active' : ''; ?>">Home</a>
            <a href="/#services" class="nav-link<?php echo (strpos($current_path, '/services') === 0) ? ' active' : ''; ?>">Services</a>
            <a href="/project-types" class="nav-link<?php echo nav_active('/project-types', $current_path); ?>">Project Types</a>
            <a href="/about" class="nav-link<?php echo nav_active('/about', $current_path); ?>">About</a>
            <a href="/tools/" class="nav-link<?php echo nav_active('/tools', $current_path); ?>">Free Tools</a>
            <a href="/blog/" class="nav-link<?php echo nav_active('/blog', $current_path); ?>">Blog</a>
            <a href="/#contact" class="nav-link">Contact</a>
            <a href="/quote" class="nav-link cta-button<?php echo nav_active('/quote', $current_path); ?>">Request Consultation</a>
        </div>
        <button class="nav-toggle" id="nav-toggle" aria-expanded="false" aria-controls="nav-menu" aria-label="Toggle navigation menu">
            <span class="bar"></span>
            <span class="bar"></span>
            <span class="bar"></span>
        </button>
    </div>
</nav>
