@php
    $seller = $product->seller;
    $plan = $seller->subscription?->plan ?? null; // Robust check
    $subscription = $seller->subscription ?? null; // Define subscription
    $isPremium = $plan && $plan->is_featured;
    $hasVerifiedBadge = $plan && $plan->has_verified_badge;
    $yearsInBusiness = $seller->created_at ? (now()->year - $seller->created_at->year) : 1;
    if($yearsInBusiness < 1) $yearsInBusiness = 1;

    // Image
    $mainImage = $product->main_image;
@endphp

<div class="bg-white rounded-4 p-3 shadow-sm border hover-shadow-lg transition-all mb-4" style="overflow: hidden;">
    <div class="row g-0">
        <!-- Image Section -->
        <div class="col-md-5">
            <div class="position-relative w-100 h-100 rounded-3 overflow-hidden border" style="background: #f9f9f9; min-height: 220px;">
                <img src="{{ $mainImage }}" class="w-100 h-100 object-fit-cover position-absolute top-0 start-0" alt="{{ $product->name }}">
                <div class="position-absolute top-0 end-0 p-2">
                     <img src="{{ asset('images/india-flag.png') }}" class="rounded-circle shadow-sm" width="24" height="24" alt="India" onerror="this.style.display='none'">
                </div>
            </div>
        </div>
        
        <!-- Content Section -->
        <div class="col-md-7 ps-md-4 pt-3 pt-md-0 d-flex flex-column justify-content-between">
            <div>
                <h5 class="fw-bold text-dark mb-1 text-truncate-2" style="font-size: 1.1rem; line-height: 1.4;">
                    <a href="{{ route('public.products.show', $product->slug) }}" class="text-dark text-decoration-none hover:tw-text-orange-600">
                        {{ $product->name }}
                    </a>
                </h5>
                <div class="text-muted small mb-3">Category: <span class="fw-bold text-dark">{{ $product->category->name ?? 'Industrial' }}</span></div>

                <!-- MOQ & Delivery -->
                <div class="mb-3">
                    <!-- Platform MOQ -->
                     <div class="tw-mb-1">
                            <i class="bi bi-box-seam-fill tw-text-orange-600 tw-mt-0.5 lg:tw-mt-0"></i>
                            <span class="tw-text-xs tw-font-medium tw-text-gray-700 tw-tracking-tight" style="font-size: 0.7rem; letter-spacing: -0.3px;">
                                Platform MOQ Range: <span class="text-secondary tw-font-semibold d-block d-lg-inline">{{ \App\Models\Setting::getValue('min_moq', 10) }}-{{ \App\Models\Setting::getValue('max_moq', 150) }}</span>
                            </span>
                        </div>
                    </div>
                    <!-- Product MOQ -->
                    <div class="tw-mb-2">
                        <div class="tw-flex tw-items-start lg:tw-items-center tw-gap-1.5">
                            <i class="bi bi-box-seam-fill tw-text-orange-600 tw-mt-0.5 lg:tw-mt-0"></i>
                            <span class="tw-text-xs tw-font-medium tw-text-gray-700 tw-tracking-tight" style="font-size: 0.7rem; letter-spacing: -0.3px;">
                                Product MOQ: <span class="text-secondary tw-font-semibold d-block d-lg-inline">{{ ($product->moq && $product->moq > 1) ? $product->moq : \App\Models\Setting::getValue('min_moq', 10) }} {{ $product->unit->name ?? 'Pieces' }}</span>
                            </span>
                        </div>
                    </div>
                     <!-- Delivery Notice -->
                    <div class="tw-flex tw-items-center tw-gap-1.5 tw-text-orange-600">
                        <i class="bi bi-truck" style="transform: scaleX(-1); display: inline-block;"></i>
                        <span class="tw-text-xs tw-font-bold">Seller handles delivery</span>
                    </div>
                </div>

                <!-- Price -->
                <div class="d-flex align-items-baseline gap-2 mb-2">
                    <span class="text-secondary fw-bold">Price:</span>
                    <h4 class="text-orange-600 fw-bold mb-0">{{ $product->currency == 'USD' ? '$' : '₹' }}{{ number_format($product->price, 0) }}</h4>
                    <span class="text-muted small">per {{ $product->unit->name ?? 'Piece' }}</span>
                </div>
                <div class="text-muted small mb-3 fst-italic">(Tax & Shipping Extra)</div>
            </div>

            <!-- Action Buttons Grid -->
            <div class="d-flex gap-2 flex-wrap">
                <button type="button" class="btn btn-primary px-3 py-1 fw-bold rounded-1 text-white flex-grow-1" style="font-size: 0.85rem;" data-bs-toggle="modal" data-bs-target="#quoteModal{{ $product->id }}">
                    <i class="bi bi-envelope-fill me-1"></i> Send Inquiry
                </button>
                <a href="{{ route('public.products.show', $product->slug) }}" class="btn btn-warning bg-opacity-25 border-warning text-warning-emphasis px-3 py-1 fw-bold rounded-1 flex-grow-1" style="font-size: 0.85rem; background-color: #fef3c7; border-color: #fcd34d;">
                    <i class="bi bi-eye-fill me-1"></i> View More
                </a>
            </div>
            
            <!-- Seller Badges (Replacements for Right Col) -->
             <div class="d-flex gap-2 flex-wrap mt-2">
                 <small class="text-muted w-100 fw-bold mb-1">
                    {{ $seller->company_name }}
                    @if($seller->isPaid())
                        <i class="bi bi-patch-check-fill text-warning ms-1" title="Premium Seller"></i>
                    @endif
                 </small>
                @if($seller->is_verified || in_array($seller->kyc_status, ['submitted', 'verified']))
                    <button class="btn btn-light border text-primary px-2 py-1 fw-bold rounded-1" style="font-size: 0.7rem; background: #e0f2fe; border-color: #bae6fd; color: #0369a1;">
                        <i class="bi bi-file-earmark-check-fill me-1"></i> KYC Submitted
                    </button>
                @else
                     <button class="btn btn-light border text-secondary px-2 py-1 fw-bold rounded-1 disabled" style="font-size: 0.7rem;">
                        <i class="bi bi-hourglass-split me-1"></i> KYC Pending
                    </button>
                @endif
                 <div class="badge bg-light text-muted border py-1 px-2 d-flex align-items-center" style="font-size: 0.7rem;">{{ $yearsInBusiness }} Yrs</div>
                 @if($seller->business_type)
                    @foreach(is_array($seller->business_type) ? $seller->business_type : [$seller->business_type] as $type)
                        <div class="badge bg-light text-dark border py-1 px-2 d-flex align-items-center" style="font-size: 0.7rem;"><i class="bi bi-building me-1"></i> {{ $type }}</div>
                    @endforeach
                 @endif
            </div>

        </div>
    </div>
</div>

<!-- Embedded Quote Modal for this card -->
<div class="modal fade" id="quoteModal{{ $product->id }}" tabindex="-1" aria-hidden="true" data-bs-backdrop="static"> <!-- Renamed ID to avoid conflict if already exists -->
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header text-white py-2" style="background: #f97316;">
                <h6 class="modal-title">
                    <i class="bi bi-chat-quote me-2"></i>Contact {{ $seller->company_name }}
                    @if($seller->isPaid())
                        <i class="bi bi-patch-check-fill text-warning ms-1" title="Premium Seller"></i>
                    @endif
                </h6>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form action="{{ route('inquiries.store', $seller) }}" method="POST">
                @csrf
                <input type="hidden" name="product_id" value="{{ $product->id }}">
                <input type="hidden" name="seller_id" value="{{ $seller->id }}">
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label fw-bold" style="font-size: 13px;">Your Name *</label>
                        <input type="text" name="name" class="form-control form-control-sm" required value="{{ auth()->user()->name ?? '' }}">
                    </div>
                    <div class="mb-3">
                        <label class="form-label fw-bold" style="font-size: 13px;">Email *</label>
                        <input type="email" name="email" class="form-control form-control-sm" required value="{{ auth()->user()->email ?? '' }}">
                    </div>
                    <div class="mb-3">
                        <label class="form-label fw-bold" style="font-size: 13px;">Phone *</label>
                        <input type="text" name="phone" class="form-control form-control-sm" required value="{{ auth()->user()->phone ?? '' }}">
                    </div>
                    <div class="mb-3">
                         <label class="form-label fw-bold" style="font-size: 13px;">Quantity</label>
                         <input type="number" name="quantity" class="form-control form-control-sm" placeholder="e.g. 100">
                    </div>
                    <div class="mb-3">
                        <label class="form-label fw-bold" style="font-size: 13px;">Message *</label>
                        <textarea name="message" class="form-control form-control-sm" rows="3" required>I am interested in {{ $product->name }}. Please share details.</textarea>
                    </div>
                </div>
                <div class="modal-footer py-2">
                    <button type="button" class="btn btn-outline-secondary btn-sm" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-sm fw-bold text-white" style="background-color: #f97316; border-color: #f97316;"><i class="bi bi-send me-1"></i> Send</button>
                </div>
            </form>
        </div>
    </div>
</div>
