@php
    $url = $item->url;
    if (str_starts_with($url, '#')) {
        $href = route('home') . $url;
    } elseif (str_starts_with($url, 'http')) {
        $href = $url;
    } elseif (\Illuminate\Support\Facades\Route::has($url)) {
        $href = route($url);
    } else {
        $href = url($url);
    }
    $hasChildren = isset($item->children) && $item->children->isNotEmpty();
    $btnStyle = '';
    if ($item->color) {
        $dark = adjust_hex($item->color, -22);
        $btnStyle = '--btn-bg:' . e($item->color) . ';--btn-dark:' . $dark . ';background:var(--btn-bg);border-color:var(--btn-bg);';
    }
@endphp
@if($hasChildren)
    <li class="nav-item dropdown{{ $depth > 0 ? ' dropdown-submenu' : ' mega-dropdown' }}" id="navDrop-{{ $item->id }}">
        <a class="nav-link dropdown-toggle{{ $item->type === 'button' ? ' btn btn-brand' : '' }}" href="#" role="button" data-bs-toggle="dropdown" data-bs-display="static" aria-expanded="false" @if($item->type === 'button' && $btnStyle) style="{{ $btnStyle }}" @endif>
            <i class="bi {{ $item->icon ?: 'bi-link-45deg' }} me-1"></i> {{ $item->label }}
        </a>
        <div class="dropdown-menu mega-menu">
            <div class="mega-grid">
                @foreach($item->children as $child)
                    @php
                        $cUrl = $child->url;
                        if (str_starts_with($cUrl, '#')) { $cHref = route('home') . $cUrl; }
                        elseif (str_starts_with($cUrl, 'http')) { $cHref = $cUrl; }
                        elseif (\Illuminate\Support\Facades\Route::has($cUrl)) { $cHref = route($cUrl); }
                        else { $cHref = url($cUrl); }
                    @endphp
                    <a href="{{ $cHref }}" class="mega-card">
                        <span class="mega-card-icon bg-primary bg-opacity-10 text-primary">
                            <i class="bi {{ $child->icon ?: 'bi-link-45deg' }}"></i>
                        </span>
                        <span class="mega-card-body">
                            <span class="mega-card-title">{{ $child->label }}</span>
                        </span>
                        <i class="bi bi-chevron-right mega-card-arrow"></i>
                    </a>
                @endforeach
            </div>
        </div>
    </li>
@else
    <li class="nav-item {{ $item->type === 'button' && $depth === 0 ? 'ms-lg-1 mt-2 mt-lg-0' : '' }}">
        @if($depth > 0)
            <a class="dropdown-item" href="{{ $href }}">
                @if($item->icon)<i class="bi {{ $item->icon }} me-1"></i>@endif {{ $item->label }}
            </a>
        @elseif($item->type === 'button')
            <a class="btn btn-brand" href="{{ $href }}" @if($btnStyle) style="{{ $btnStyle }}" @endif><i class="bi {{ $item->icon ?: 'bi-link-45deg' }} me-1"></i> {{ $item->label }}</a>
        @else
            <a class="nav-link" href="{{ $href }}">
                @if($item->icon)<i class="bi {{ $item->icon }} me-1"></i>@endif {{ $item->label }}
            </a>
        @endif
    </li>
@endif