@php
  /** @var \App\Models\GeneralSetting $settings */
  $gs = $settings ?? new \App\Models\GeneralSetting();

  // Theme colors from settings (with safe fallbacks)
  $g1   = $gs->icon_color       ?? '#0B5D1E'; // primary/brand
  $g2   = $gs->hover_color      ?? '#0F7A2E'; // secondary/hover
  $text = '#ffffff';                          // header text on gradient should be white for contrast
  $ring = $gs->text_color       ?? '#0f172a'; // used for input ring etc.

  // Logo url (fallback to /logo.png)
  $logo = !empty($gs->logo)
    ? asset(ltrim($gs->logo, '/')) . '?v=' . ($gs->updated_at?->timestamp ?? time())
    : asset('logo.png');

  $adminUser = auth('admin')->user();
  $isSuperAdmin = $adminUser?->isSuperAdmin() ?? true;
  $canSearchUsers = $isSuperAdmin || ($adminUser && $adminUser->hasAnyProjectPermission(['users', 'users_all']));

  $adminName = $adminUser?->name ?? 'Admin';
  $adminPhotoRaw = (string) ($adminUser?->profile_photo ?? '');
  $hasAdminPasswordRoute = \Illuminate\Support\Facades\Route::has('admin.password.form');
  if ($adminPhotoRaw !== '') {
    $adminPhotoUrl = preg_match('/^https?:\/\//i', $adminPhotoRaw)
      ? $adminPhotoRaw
      : asset(ltrim($adminPhotoRaw, '/'));
  } else {
    $adminPhotoUrl = !empty($gs->logo) ? asset(ltrim($gs->logo, '/')) : asset('images/default-avatar.png');
  }
@endphp

<header
  class="sticky top-0 z-50 px-4 py-3 flex justify-between items-center shadow-md relative"
  style="background: linear-gradient(90deg, {{ $g1 }}, {{ $g2 }}); color: {{ $text }};"
>
  <style>
    /* small, soft icon background pills */
    .icon-pill{width:28px;height:28px;border-radius:9999px;display:flex;align-items:center;justify-content:center}
  </style>

  <div class="flex items-center gap-2">
    <!-- ☰ Sidebar toggle -->
    <button id="toggleSidebar" class="text-2xl leading-none" aria-label="Toggle sidebar">☰</button>

    <!-- Brand Logo -->
    <img
      src="{{ $logo }}"
      alt="Logo"
      class="h-8 w-auto object-contain ml-2 select-none"
      draggable="false"
    />
  </div>

  <div class="flex items-center gap-3 relative">
    <!-- Search -->
    @if($canSearchUsers)
      <form method="GET" action="{{ route('admin.users') }}" class="hidden xs:block">
        <input
          type="text"
          name="query"
          value="{{ request('query') }}"
          placeholder="Search Users"
          class="w-28 sm:w-36 md:w-48 lg:w-64 px-2 py-1 rounded-md text-black focus:outline-none focus:ring-2"
          style="--tw-ring-color: {{ $g1 }};"
          autocomplete="off"
        />
      </form>
    @endif

    <div class="relative">
      <button id="profileToggle"
              type="button"
              class="flex items-center gap-2 px-2 py-1 rounded-full"
              style="background:#ffffff1f;color:{{ $text }};">
        <img src="{{ $adminPhotoUrl }}" alt="Admin"
             class="h-8 w-8 rounded-full object-cover border border-white/30">
        <span class="hidden md:inline text-sm font-semibold truncate max-w-[120px]">{{ $adminName }}</span>
        <i class="fa-solid fa-chevron-down text-xs opacity-90"></i>
      </button>

      <div id="profileMenu"
           class="hidden absolute right-0 mt-2 w-48 bg-white text-slate-800 rounded-xl shadow-lg overflow-hidden z-50">
        <a href="{{ route('admin.profile') }}" class="block px-4 py-2 text-sm hover:bg-slate-50">
          <i class="fa-solid fa-user-gear me-2"></i> প্রোফাইল
        </a>
        @if($hasAdminPasswordRoute)
          <a href="{{ route('admin.password.form') }}" class="block px-4 py-2 text-sm hover:bg-slate-50">
            <i class="fa-solid fa-key me-2"></i> পাসওয়ার্ড পরিবর্তন
          </a>
        @endif
        <form method="POST" action="{{ route('admin.logout') }}">
          @csrf
          <button type="submit" class="w-full text-left px-4 py-2 text-sm hover:bg-slate-50">
            <i class="fa-solid fa-right-from-bracket me-2"></i> লগ আউট
          </button>
        </form>
      </div>
    </div>

  </div>
</header>
