<div class="flex flex-col md:flex-row">
  <!-- Filter Panel for Desktop -->
  <div class="hidden md:block w-full md:w-1/4 h-full bg-white shadow-lg p-6 overflow-y-auto z-10">
    <app-search-modal [isModal]="false"></app-search-modal>
  </div>

  <!-- Main Content -->
  <div class="w-full p-4">
    <div class="container mx-auto">
      <!-- Breadcrumbs -->
      <div class="mb-4">
        <app-breadcrumbs [breadcrumbs]="breadcrumbs"></app-breadcrumbs>
      </div>

      <!-- SEO-optimized heading -->
      <div class="mb-6">
        <h1 class="text-3xl md:text-4xl font-bold text-neutral-900 mb-2">Businesses for Sale - Find Your Next Business Opportunity</h1>
        <p class="text-lg text-neutral-600">Discover profitable business opportunities across the State of Texas. Browse
          verified listings from business owners and brokers.</p>
        <div class="mt-4 text-base text-neutral-700 max-w-4xl">
          <p>BizMatch features a curated selection of businesses for sale across diverse industries and price ranges. Browse opportunities in sectors like restaurants, retail, franchises, services, e-commerce, and manufacturing. Each listing includes financial details, years established, location information, and seller contact details. Our marketplace connects business buyers with sellers and brokers nationwide, making it easy to find your next business opportunity.</p>
        </div>
      </div>

      <!-- Loading Skeleton -->
      @if(isLoading) {
      <h2 class="text-2xl font-semibold text-neutral-800 mb-4">Loading Business Listings...</h2>
      <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
        @for (item of [1,2,3,4,5,6]; track item) {
        <div class="bg-white rounded-lg drop-shadow-custom-bg-mobile md:drop-shadow-custom-bg overflow-hidden">
          <div class="p-6 animate-pulse">
            <!-- Category icon and text -->
            <div class="flex items-center mb-4">
              <div class="w-5 h-5 bg-neutral-200 rounded mr-2"></div>
              <div class="h-5 bg-neutral-200 rounded w-32"></div>
            </div>
            <!-- Title -->
            <div class="h-7 bg-neutral-200 rounded w-3/4 mb-4"></div>
            <!-- Badges -->
            <div class="flex justify-between mb-4">
              <div class="h-6 bg-neutral-200 rounded-full w-20"></div>
              <div class="h-6 bg-neutral-200 rounded-full w-16"></div>
            </div>
            <!-- Details -->
            <div class="space-y-2 mb-4">
              <div class="h-4 bg-neutral-200 rounded w-full"></div>
              <div class="h-4 bg-neutral-200 rounded w-5/6"></div>
              <div class="h-4 bg-neutral-200 rounded w-4/6"></div>
              <div class="h-4 bg-neutral-200 rounded w-3/4"></div>
              <div class="h-4 bg-neutral-200 rounded w-2/3"></div>
            </div>
            <!-- Button -->
            <div class="h-12 bg-neutral-200 rounded-full w-full mt-4"></div>
          </div>
        </div>
        }
      </div>
      } @else if(listings?.length > 0) {
      <h2 class="text-2xl font-semibold text-neutral-800 mb-4">Available Business Listings</h2>
      <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-6">
        @for (listing of listings; track listing.id) {
        <div
          class="bg-white rounded-lg drop-shadow-custom-bg-mobile md:drop-shadow-custom-bg overflow-hidden hover:shadow-2xl transition-all duration-300 hover:scale-[1.02] group">
          <div class="p-6 flex flex-col h-full relative z-[0]">
            <!-- Quick Actions Overlay -->
            <div
              class="absolute top-4 right-4 flex gap-2 opacity-0 group-hover:opacity-100 transition-opacity duration-200 z-20">
              @if(user) {
              <button class="bg-white rounded-full p-2 shadow-lg transition-colors"
                [class.bg-red-50]="isFavorite(listing)"
                [attr.aria-label]="isFavorite(listing) ? 'Remove from favorites' : 'Save to favorites'"
                (click)="toggleFavorite($event, listing)">
                <i
                  [class]="isFavorite(listing) ? 'fas fa-heart text-red-500' : 'far fa-heart text-red-500 hover:scale-110 transition-transform'"></i>
              </button>
              }
              <button type="button" class="bg-white rounded-full p-2 shadow-lg hover:bg-blue-50 transition-colors"
                aria-label="Share listing" (click)="shareListing($event, listing)">
                <i class="fas fa-share-alt text-blue-500 hover:scale-110 transition-transform"></i>
              </button>
            </div>

            <div class="flex items-center mb-4">
              <i [class]="selectOptions.getIconAndTextColorType(listing.type)" class="mr-2 text-xl"></i>
              <span [class]="selectOptions.getTextColorType(listing.type)" class="font-bold text-lg">{{
                selectOptions.getBusiness(listing.type) }}</span>
            </div>
            <h2 class="text-xl font-semibold mb-4">
              {{ listing.title }}
              @if(listing.draft) {
              <span
                class="bg-amber-100 text-amber-800 border border-amber-300 text-sm font-medium me-2 ml-2 px-2.5 py-0.5 rounded">Draft</span>
              }
            </h2>
            <div class="flex justify-between">
              <span
                class="w-fit inline-flex items-center justify-center px-2 py-1 mb-4 text-xs font-bold leading-none bg-neutral-200 text-neutral-700 rounded-full">
                {{ selectOptions.getState(listing.location.state) }}
              </span>

              @if (getListingBadge(listing); as badge) {
              <span
                class="mb-4 h-fit inline-flex items-center justify-center px-2 py-1 text-xs font-bold leading-none rounded-full border"
                [ngClass]="{
                  'bg-emerald-100 text-emerald-800 border-emerald-300': badge === 'NEW',
                  'bg-teal-100 text-teal-800 border-teal-300': badge === 'UPDATED'
                }">
                {{ badge }}
              </span>
              }
            </div>

            <p class="text-base font-bold text-neutral-800 mb-2">
              <strong>Asking price:</strong>
              <span class="text-success-600">
                {{ listing?.price != null ? (listing.price | currency : 'USD' : 'symbol' : '1.0-0') : 'undisclosed' }}
              </span>
            </p>
            <p class="text-sm text-neutral-600 mb-2">
              <strong>Sales revenue:</strong>
              {{ listing?.salesRevenue != null ? (listing.salesRevenue | currency : 'USD' : 'symbol' : '1.0-0') :
              'undisclosed' }}
            </p>
            <p class="text-sm text-neutral-600 mb-2">
              <strong>Net profit:</strong>
              {{ listing?.cashFlow != null ? (listing.cashFlow | currency : 'USD' : 'symbol' : '1.0-0') : 'undisclosed'
              }}
            </p>
            <p class="text-sm text-neutral-600 mb-2">
              <strong>Location:</strong> {{ listing.location.name ? listing.location.name : listing.location.county ?
              listing.location.county : this.selectOptions.getState(listing.location.state) }}
            </p>
            <p class="text-sm text-neutral-600 mb-4"><strong>Years established:</strong> {{ listing.established }}</p>
            @if(listing.imageName) {
            <img [appLazyLoad]="env.imageBaseUrl + '/pictures/logo/' + listing.imageName + '.avif?_ts=' + ts"
              [alt]="altText.generateListingCardLogoAlt(listing)"
              class="absolute bottom-[80px] right-[20px] h-[45px] w-auto" width="100" height="45" />
            }
            <div class="flex-grow"></div>
            <button
              class="bg-success-600 text-white px-5 py-3 rounded-full w-full flex items-center justify-center mt-4 transition-all duration-200 hover:bg-success-700 hover:shadow-lg group/btn"
              [routerLink]="['/business', listing.slug || listing.id]">
              <span class="font-semibold">View Opportunity</span>
              <i class="fas fa-arrow-right ml-2 group-hover/btn:translate-x-1 transition-transform duration-200"></i>
            </button>
          </div>
        </div>
        }
      </div>
      } @else if (listings?.length === 0) {
      <div class="w-full flex items-center flex-wrap justify-center gap-10 py-12">
        <div class="grid gap-6 max-w-2xl w-full">
          <svg class="mx-auto" xmlns="http://www.w3.org/2000/svg" width="154" height="161" viewBox="0 0 154 161"
            fill="none">
            <path
              d="M0.0616455 84.4268C0.0616455 42.0213 34.435 7.83765 76.6507 7.83765C118.803 7.83765 153.224 42.0055 153.224 84.4268C153.224 102.42 147.026 118.974 136.622 132.034C122.282 150.138 100.367 161 76.6507 161C52.7759 161 30.9882 150.059 16.6633 132.034C6.25961 118.974 0.0616455 102.42 0.0616455 84.4268Z"
              fill="#EEF2FF" />
            <path
              d="M96.8189 0.632498L96.8189 0.632384L96.8083 0.630954C96.2034 0.549581 95.5931 0.5 94.9787 0.5H29.338C22.7112 0.5 17.3394 5.84455 17.3394 12.4473V142.715C17.3394 149.318 22.7112 154.662 29.338 154.662H123.948C130.591 154.662 135.946 149.317 135.946 142.715V38.9309C135.946 38.0244 135.847 37.1334 135.648 36.2586L135.648 36.2584C135.117 33.9309 133.874 31.7686 132.066 30.1333C132.066 30.1331 132.065 30.1329 132.065 30.1327L103.068 3.65203C103.068 3.6519 103.067 3.65177 103.067 3.65164C101.311 2.03526 99.1396 0.995552 96.8189 0.632498Z"
              fill="white" stroke="#E5E7EB" />
            <ellipse cx="80.0618" cy="81" rx="28.0342" ry="28.0342" fill="#EEF2FF" />
            <path
              d="M99.2393 61.3061L99.2391 61.3058C88.498 50.5808 71.1092 50.5804 60.3835 61.3061C49.6423 72.0316 49.6422 89.4361 60.3832 100.162C71.109 110.903 88.4982 110.903 99.2393 100.162C109.965 89.4363 109.965 72.0317 99.2393 61.3061ZM105.863 54.6832C120.249 69.0695 120.249 92.3985 105.863 106.785C91.4605 121.171 68.1468 121.171 53.7446 106.785C39.3582 92.3987 39.3582 69.0693 53.7446 54.683C68.1468 40.2965 91.4605 40.2966 105.863 54.6832Z"
              stroke="#E5E7EB" />
            <path
              d="M110.782 119.267L102.016 110.492C104.888 108.267 107.476 105.651 109.564 102.955L118.329 111.729L110.782 119.267Z"
              stroke="#E5E7EB" />
            <path
              d="M139.122 125.781L139.122 125.78L123.313 109.988C123.313 109.987 123.313 109.987 123.312 109.986C121.996 108.653 119.849 108.657 118.521 109.985L118.871 110.335L118.521 109.985L109.047 119.459C107.731 120.775 107.735 122.918 109.044 124.247L109.047 124.249L124.858 140.06C128.789 143.992 135.191 143.992 139.122 140.06C143.069 136.113 143.069 129.728 139.122 125.781Z"
              fill="#A5B4FC" stroke="#818CF8" />
            <path
              d="M83.185 87.2285C82.5387 87.2285 82.0027 86.6926 82.0027 86.0305C82.0027 86.0305 82.0027 83.3821 77.9987 83.3821C77.9987 83.3821 77.9987 86.0305 77.9987 86.0305C77.9987 86.6926 77.4627 87.2285 76.8006 87.2285C76.1543 87.2285 75.6183 86.6926 75.6183 86.0305C75.6183 80.2294 84.3831 80.2451 84.3831 86.0305C84.3831 86.6926 83.8471 87.2285 83.185 87.2285Z"
              fill="#4F46E5" />
            <path
              d="M93.3528 77.0926H88.403C87.7409 77.0926 87.2049 76.5567 87.2049 75.8946C87.2049 75.2483 87.7409 74.7123 88.403 74.7123H93.3528C94.0149 74.7123 94.5509 75.2483 94.5509 75.8946C94.5509 76.5567 94.0149 77.0926 93.3528 77.0926Z"
              fill="#4F46E5" />
            <path
              d="M71.5987 77.0925H66.6488C65.9867 77.0925 65.4507 76.5565 65.4507 75.8945C65.4507 75.2481 65.9867 74.7122 66.6488 74.7122H71.5987C72.245 74.7122 72.781 75.2481 72.781 75.8945C72.781 76.5565 72.245 77.0925 71.5987 77.0925Z"
              fill="#4F46E5" />
            <rect x="38.3522" y="21.5128" width="41.0256" height="2.73504" rx="1.36752" fill="#4F46E5" />
            <rect x="38.3522" y="133.65" width="54.7009" height="5.47009" rx="2.73504" fill="#A5B4FC" />
            <rect x="38.3522" y="29.7179" width="13.6752" height="2.73504" rx="1.36752" fill="#4F46E5" />
            <circle cx="56.13" cy="31.0854" r="1.36752" fill="#4F46E5" />
            <circle cx="61.6001" cy="31.0854" r="1.36752" fill="#4F46E5" />
            <circle cx="67.0702" cy="31.0854" r="1.36752" fill="#4F46E5" />
          </svg>
          <div class="text-center">
            <h2 class="text-black text-2xl font-semibold leading-loose pb-2">No listings found</h2>
            <p class="text-neutral-600 text-base font-normal leading-relaxed pb-6">We couldn't find any businesses
              matching your criteria.<br />Try adjusting your filters or explore popular categories below.</p>

            <!-- Action Buttons -->
            <div class="flex flex-col sm:flex-row gap-3 justify-center mb-8">
              <button (click)="clearAllFilters()"
                class="px-6 py-3 rounded-full bg-primary-600 text-white text-sm font-semibold hover:bg-primary-700 transition-colors">
                <i class="fas fa-redo mr-2"></i>Clear All Filters
              </button>
              <button [routerLink]="['/home']"
                class="px-6 py-3 rounded-full border-2 border-neutral-300 text-neutral-700 text-sm font-semibold hover:border-primary-600 hover:text-primary-600 transition-colors">
                <i class="fas fa-home mr-2"></i>Back to Home
              </button>
            </div>

            <!-- Popular Categories Suggestions -->
            <div class="mt-8 p-6 bg-neutral-50 rounded-lg">
              <h3 class="text-lg font-semibold text-neutral-800 mb-4">
                <i class="fas fa-fire text-orange-500 mr-2"></i>Popular Categories
              </h3>
              <div class="grid grid-cols-2 md:grid-cols-3 gap-3">
                <button (click)="filterByCategory('foodAndRestaurant')"
                  class="px-4 py-2 bg-white rounded-lg border border-neutral-200 hover:border-primary-500 hover:bg-primary-50 text-sm text-neutral-700 hover:text-primary-600 transition-all">
                  <i class="fas fa-utensils mr-2"></i>Restaurants
                </button>
                <button (click)="filterByCategory('retail')"
                  class="px-4 py-2 bg-white rounded-lg border border-neutral-200 hover:border-primary-500 hover:bg-primary-50 text-sm text-neutral-700 hover:text-primary-600 transition-all">
                  <i class="fas fa-store mr-2"></i>Retail
                </button>
                <button (click)="filterByCategory('realEstate')"
                  class="px-4 py-2 bg-white rounded-lg border border-neutral-200 hover:border-primary-500 hover:bg-primary-50 text-sm text-neutral-700 hover:text-primary-600 transition-all">
                  <i class="fas fa-building mr-2"></i>Real Estate
                </button>
                <button (click)="filterByCategory('service')"
                  class="px-4 py-2 bg-white rounded-lg border border-neutral-200 hover:border-primary-500 hover:bg-primary-50 text-sm text-neutral-700 hover:text-primary-600 transition-all">
                  <i class="fas fa-cut mr-2"></i>Services
                </button>
                <button (click)="filterByCategory('franchise')"
                  class="px-4 py-2 bg-white rounded-lg border border-neutral-200 hover:border-primary-500 hover:bg-primary-50 text-sm text-neutral-700 hover:text-primary-600 transition-all">
                  <i class="fas fa-handshake mr-2"></i>Franchise
                </button>
                <button (click)="filterByCategory('professional')"
                  class="px-4 py-2 bg-white rounded-lg border border-neutral-200 hover:border-primary-500 hover:bg-primary-50 text-sm text-neutral-700 hover:text-primary-600 transition-all">
                  <i class="fas fa-briefcase mr-2"></i>Professional
                </button>
              </div>
            </div>

            <!-- Helpful Tips -->
            <div class="mt-6 p-4 bg-primary-50 border border-primary-100 rounded-lg text-left">
              <h4 class="font-semibold text-primary-900 mb-2 flex items-center">
                <i class="fas fa-lightbulb mr-2"></i>Search Tips
              </h4>
              <ul class="text-sm text-primary-800 space-y-1">
                <li>• Try expanding your search radius</li>
                <li>• Consider adjusting your price range</li>
                <li>• Browse all categories to discover opportunities</li>
              </ul>
            </div>
          </div>
        </div>
      </div>
      }
    </div>
    @if(pageCount > 1) {
    <app-paginator [page]="page" [pageCount]="pageCount" (pageChange)="onPageChange($event)"></app-paginator>
    }
  </div>

  <!-- Filter Button for Mobile -->
  <button (click)="openFilterModal()"
    class="md:hidden fixed bottom-4 right-4 bg-primary-500 text-white p-3 rounded-full shadow-lg z-20"><i
      class="fas fa-filter"></i> Filter</button>
</div>