<div x-data="{
    activeTab: 'products',
    showParticipantResults: false,
    mobileCartOpen: false,
}" class="min-h-[calc(100vh-4rem)]">

    {{-- No Open Cash Session Guard --}}
    @if(!$hasOpenSession)
        <div class="flex flex-col items-center justify-center min-h-[60vh] p-4 sm:p-8">
            <div class="bg-amber-50 border border-amber-200 rounded-xl p-6 sm:p-8 text-center max-w-md w-full">
                <svg class="w-12 h-12 sm:w-16 sm:h-16 text-amber-500 mx-auto mb-3 sm:mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L4.082 16.5c-.77.833.192 2.5 1.732 2.5z"/>
                </svg>
                <h3 class="text-base sm:text-lg font-bold text-amber-800 mb-2">{{ __('يجب فتح وردية أولاً') }}</h3>
                <p class="text-amber-700 text-xs sm:text-sm mb-4">{{ __('لا يمكن إجراء عمليات بيع بدون وردية مفتوحة') }}</p>
                <a href="{{ route('cash-sessions.manage') }}"
                   class="inline-flex items-center gap-2 px-4 py-2.5 min-h-[44px] bg-amber-600 text-white rounded-lg hover:bg-amber-700 text-xs sm:text-sm font-medium transition-colors">
                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 4v16m8-8H4"/>
                    </svg>
                    {{ __('فتح وردية جديدة') }}
                </a>
            </div>
        </div>
    @else

    {{-- Main POS Layout --}}
    <div class="flex flex-col lg:flex-row gap-3 sm:gap-4 p-3 sm:p-4 h-full pb-20 lg:pb-4">

        {{-- LEFT PANEL: Catalog & Items (60%) --}}
        <div class="w-full lg:w-3/5 space-y-3 sm:space-y-4">

            {{-- Participant Search --}}
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-3 sm:p-4">
                <div class="flex items-center gap-2 sm:gap-3">
                    <svg class="w-5 h-5 text-gray-400 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 7a4 4 0 11-8 0 4 4 0 018 0zM12 14a7 7 0 00-7 7h14a7 7 0 00-7-7z"/>
                    </svg>

                    @if($participantId)
                        <div class="flex items-center gap-2 flex-1 min-w-0">
                            <span class="px-3 py-1.5 min-h-[44px] flex items-center bg-blue-50 text-blue-700 rounded-lg text-xs sm:text-sm font-medium truncate">
                                {{ $participantName }}
                            </span>
                            @if($participantIsFree) <x-ui.free-player-badge :small="true" /> @endif
                            {{-- Which price list the grid below is showing, so a member rate is
                                 something the cashier can see rather than something they hope for. --}}
                            <span class="shrink-0 px-2 py-1 rounded-lg text-[11px] font-medium {{ $buyerTier === 'member' ? 'bg-emerald-50 text-emerald-700 border border-emerald-200' : 'bg-gray-100 text-gray-600 border border-gray-200' }}">
                                {{ $buyerTier === 'member' ? __('أسعار الأعضاء') : __('أسعار غير الأعضاء') }}
                            </span>
                            <button wire:click="clearParticipant" class="min-w-[44px] min-h-[44px] flex items-center justify-center text-gray-400 hover:text-red-500 transition-colors">
                                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                                </svg>
                            </button>
                        </div>
                    @else
                        <span class="shrink-0 px-2 py-1 rounded-lg text-[11px] font-medium bg-gray-100 text-gray-600 border border-gray-200">
                            {{ __('أسعار غير الأعضاء') }}
                        </span>
                        <div class="relative flex-1">
                            <input type="text"
                                   wire:model.live.debounce.300ms="participantSearch"
                                   @focus="showParticipantResults = true"
                                   @click.away="showParticipantResults = false"
                                   placeholder="{{ __('بحث عن مشترك بالاسم أو رقم الهاتف...') }}"
                                   class="w-full px-3 sm:px-4 py-2.5 min-h-[44px] border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-xs sm:text-sm">

                            {{-- Search Results Dropdown --}}
                            @if(count($searchResults) > 0)
                            <div x-show="showParticipantResults"
                                 x-transition
                                 class="absolute z-50 top-full mt-1 w-full bg-white border border-gray-200 rounded-lg shadow-lg max-h-60 overflow-y-auto">
                                @foreach($searchResults as $result)
                                    <button wire:click="selectParticipant({{ $result['id'] }})"
                                            @click="showParticipantResults = false"
                                            class="w-full text-start px-3 sm:px-4 py-2.5 min-h-[44px] hover:bg-gray-50 active:bg-blue-50 flex items-center justify-between border-b border-gray-100 last:border-b-0 transition-colors">
                                        <span class="text-xs sm:text-sm font-medium text-gray-700">{{ $result['name'] }}</span>
                                        @if($result['phone'])
                                            <span class="text-xs text-gray-400" dir="ltr">{{ $result['phone'] }}</span>
                                        @endif
                                    </button>
                                @endforeach
                            </div>
                            @endif
                        </div>
                    @endif
                </div>
            </div>

            {{-- Tabs --}}
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
                <div class="flex border-b border-gray-200">
                    <button @click="activeTab = 'products'"
                            :class="activeTab === 'products' ? 'border-b-2 border-blue-600 text-blue-700 bg-blue-50/50' : 'text-gray-500 hover:text-gray-700'"
                            class="flex-1 px-3 sm:px-4 py-3 min-h-[44px] text-xs sm:text-sm font-medium transition-colors">
                        {{ __('المنتجات') }}
                    </button>
                    <button @click="activeTab = 'manual'"
                            :class="activeTab === 'manual' ? 'border-b-2 border-blue-600 text-blue-700 bg-blue-50/50' : 'text-gray-500 hover:text-gray-700'"
                            class="flex-1 px-3 sm:px-4 py-3 min-h-[44px] text-xs sm:text-sm font-medium transition-colors">
                        {{ __('عنصر يدوي') }}
                    </button>
                </div>

                {{-- Products Tab --}}
                <div x-show="activeTab === 'products'" class="p-3 sm:p-4">
                    @if($products->isEmpty())
                        <div class="text-center py-6 sm:py-8">
                            <svg class="w-10 h-10 sm:w-12 sm:h-12 text-gray-300 mx-auto mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"/>
                            </svg>
                            <p class="text-gray-500 text-xs sm:text-sm">{{ __('لا توجد منتجات متاحة') }}</p>
                        </div>
                    @else
                        <div class="grid grid-cols-2 sm:grid-cols-2 lg:grid-cols-3 gap-2 sm:gap-3" wire:loading.class="opacity-50 pointer-events-none" wire:target="addProduct">
                            @foreach($products as $product)
                                <button wire:click="addProduct({{ $product->id }})"
                                        class="flex flex-col items-start p-2.5 sm:p-3 min-h-[44px] border border-gray-200 rounded-lg hover:border-blue-300 hover:bg-blue-50/50 active:bg-blue-100 active:scale-[0.97] transition-all text-start group relative">
                                    @if(!empty($productHasPlans[$product->id]))
                                        <span class="absolute top-1 end-1 px-1.5 py-0.5 text-[10px] font-bold bg-indigo-100 text-indigo-700 rounded">{{ __('تقسيط') }}</span>
                                    @elseif($product->allows_partial_payment)
                                        <span class="absolute top-1 end-1 px-1.5 py-0.5 text-[10px] font-bold bg-blue-100 text-blue-700 rounded">{{ __('عربون') }}</span>
                                    @endif
                                    <div class="flex items-center gap-2 w-full">
                                        <div class="w-8 h-8 sm:w-9 sm:h-9 rounded-lg bg-emerald-100 text-emerald-600 flex items-center justify-center shrink-0 group-hover:bg-emerald-200 transition-colors">
                                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M20 7l-8-4-8 4m16 0l-8 4m8-4v10l-8 4m0-10L4 7m8 4v10M4 7v10l8 4"/>
                                            </svg>
                                        </div>
                                        <div class="flex-1 min-w-0">
                                            <p class="text-xs sm:text-sm font-medium text-gray-800 truncate">{{ $product->name_ar }}</p>
                                            @php
                                                $priced = $productPrices[$product->id] ?? ['base' => $product->selling_price, 'unit' => $product->selling_price];
                                            @endphp
                                            <p class="text-xs text-blue-600 font-bold" dir="ltr">
                                                {{ number_format($priced['unit'] / 100, 2) }} {{ __('ج.م') }}
                                                @if($priced['base'] > $priced['unit'])
                                                    {{-- The list price, so the cashier can see the rate applied rather than guess at it. --}}
                                                    <span class="font-normal text-gray-400 line-through">{{ number_format($priced['base'] / 100, 2) }}</span>
                                                @endif
                                            </p>
                                        </div>
                                    </div>
                                </button>
                            @endforeach
                        </div>
                    @endif
                </div>

                {{-- Manual Item Tab --}}
                <div x-show="activeTab === 'manual'" class="p-3 sm:p-4">
                    <div class="space-y-3">
                        <div>
                            <label class="block text-xs sm:text-sm font-medium text-gray-700 mb-1">{{ __('اسم العنصر') }}</label>
                            <input type="text"
                                   wire:model="manualItemName"
                                   placeholder="{{ __('مثال: رسوم إضافية') }}"
                                   class="w-full px-3 sm:px-4 py-2.5 min-h-[44px] border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-xs sm:text-sm">
                        </div>
                        <div class="grid grid-cols-2 gap-2 sm:gap-3">
                            <div>
                                <label class="block text-xs sm:text-sm font-medium text-gray-700 mb-1">{{ __('السعر (ج.م)') }}</label>
                                <input type="number"
                                       wire:model="manualItemPrice"
                                       min="0"
                                       step="0.01"
                                       dir="ltr"
                                       class="w-full px-3 sm:px-4 py-2.5 min-h-[44px] border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-xs sm:text-sm">
                            </div>
                            <div>
                                <label class="block text-xs sm:text-sm font-medium text-gray-700 mb-1">{{ __('الكمية') }}</label>
                                <input type="number"
                                       wire:model="manualItemQty"
                                       min="1"
                                       dir="ltr"
                                       class="w-full px-3 sm:px-4 py-2.5 min-h-[44px] border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-xs sm:text-sm">
                            </div>
                        </div>
                        <button wire:click="addManualItem"
                                wire:loading.attr="disabled"
                                wire:target="addManualItem"
                                class="w-full px-4 py-2.5 min-h-[44px] bg-blue-600 text-white rounded-lg hover:bg-blue-700 active:bg-blue-800 active:scale-[0.97] text-xs sm:text-sm font-medium transition-colors disabled:opacity-50">
                            <span wire:loading.remove wire:target="addManualItem">{{ __('إضافة للسلة') }}</span>
                            <span wire:loading wire:target="addManualItem">{{ __('جارٍ الإضافة...') }}</span>
                        </button>
                    </div>
                </div>
            </div>

            {{-- Coupon --}}
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-3 sm:p-4">
                <label class="block text-xs sm:text-sm font-medium text-gray-700 mb-2">{{ __('كود الخصم') }}</label>
                <div class="flex gap-2">
                    <input type="text"
                           wire:model="couponCode"
                           placeholder="{{ __('أدخل كود الخصم') }}"
                           class="flex-1 px-3 sm:px-4 py-2.5 min-h-[44px] border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-xs sm:text-sm"
                           dir="ltr">
                    @if($couponCode)
                        <button wire:click="$set('couponCode', '')"
                                class="min-w-[44px] min-h-[44px] flex items-center justify-center text-gray-400 hover:text-red-500 transition-colors">
                            <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                            </svg>
                        </button>
                    @endif
                </div>
            </div>
        </div>

        {{-- RIGHT PANEL: Cart (40%) --}}
        <div id="cart-section" class="w-full lg:w-2/5">
            <div class="bg-white rounded-xl shadow-sm border border-gray-200 flex flex-col lg:sticky lg:top-4 lg:max-h-[calc(100vh-6rem)]"
                 :class="{ 'hidden lg:flex': !mobileCartOpen, 'flex': mobileCartOpen }">

                {{-- Cart Header --}}
                <div class="flex items-center justify-between p-3 sm:p-4 border-b border-gray-200">
                    <h2 class="text-base sm:text-lg font-bold text-gray-800 flex items-center gap-2">
                        <svg class="w-5 h-5 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 3h2l.4 2M7 13h10l4-8H5.4M7 13L5.4 5M7 13l-2.293 2.293c-.63.63-.184 1.707.707 1.707H17m0 0a2 2 0 100 4 2 2 0 000-4zm-8 2a2 2 0 100 4 2 2 0 000-4z"/>
                        </svg>
                        {{ __('السلة') }}
                        @if(count($cart) > 0)
                            <span class="px-2 py-0.5 text-xs bg-blue-100 text-blue-700 rounded-full">{{ count($cart) }}</span>
                        @endif
                    </h2>
                    <div class="flex items-center gap-2">
                        @if(count($cart) > 0)
                            <button wire:click="newTransaction"
                                    wire:loading.attr="disabled"
                                    class="text-xs text-red-500 hover:text-red-700 transition-colors min-h-[44px] flex items-center px-2">
                                {{ __('مسح الكل') }}
                            </button>
                        @endif
                        {{-- Mobile close button --}}
                        <button @click="mobileCartOpen = false"
                                class="lg:hidden min-w-[44px] min-h-[44px] flex items-center justify-center text-gray-400 hover:text-gray-600 transition-colors">
                            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                            </svg>
                        </button>
                    </div>
                </div>

                {{-- Essential Product Warnings --}}
                @if(count($essentialWarnings) > 0)
                    <div class="px-3 sm:px-4 pt-3 space-y-2">
                        @foreach($essentialWarnings as $productId => $warning)
                            <div class="flex items-start gap-2 p-3 bg-amber-50 border border-amber-300 rounded-lg">
                                <svg class="w-5 h-5 text-amber-600 shrink-0 mt-0.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.964-.833-2.732 0L4.082 16.5c-.77.833.192 2.5 1.732 2.5z"/>
                                </svg>
                                <div class="flex-1 min-w-0">
                                    <p class="text-xs font-bold text-amber-800">{{ __('تنبيه: منتج أساسي مكرر') }}</p>
                                    <p class="text-xs text-amber-700 mt-0.5">
                                        {{ $warning['participant_name'] }} {{ __('اشترى') }} "{{ $warning['product_name'] }}" {{ __('بتاريخ') }} <span dir="ltr" class="font-medium">{{ $warning['purchased_at'] }}</span>
                                    </p>
                                </div>
                                <button wire:click="dismissEssentialWarning({{ $productId }})" class="text-amber-500 hover:text-amber-700 shrink-0">
                                    <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/></svg>
                                </button>
                            </div>
                        @endforeach
                    </div>
                @endif

                {{-- Cart Items --}}
                <div class="flex-1 overflow-y-auto p-3 sm:p-4 space-y-2 sm:space-y-3" wire:loading.class="opacity-50 pointer-events-none" wire:target="removeItem,updateQuantity">
                    @forelse($cart as $index => $item)
                        <div class="flex items-start gap-2 sm:gap-3 p-2.5 sm:p-3 bg-gray-50 rounded-lg">
                            <div class="flex-1 min-w-0">
                                <p class="text-xs sm:text-sm font-medium text-gray-800 truncate">{{ $item['item_name_ar'] }}</p>
                                <div class="flex items-center gap-2 mt-1">
                                    <span class="text-xs text-gray-500" dir="ltr">{{ number_format($item['unit_price'] / 100, 2) }} {{ __('ج.م') }}</span>
                                    @if($item['discount_amount'] > 0)
                                        <span class="text-xs text-green-600" dir="ltr">-{{ number_format($item['discount_amount'] / 100, 2) }}</span>
                                    @endif
                                </div>
                            </div>

                            {{-- Quantity Controls --}}
                            <div class="flex items-center gap-1">
                                <button wire:click="updateQuantity({{ $index }}, {{ $item['quantity'] - 1 }})"
                                        class="w-8 h-8 sm:w-7 sm:h-7 flex items-center justify-center rounded bg-gray-200 text-gray-600 hover:bg-gray-300 active:bg-gray-400 active:scale-90 text-xs font-bold transition-all">
                                    -
                                </button>
                                <span class="w-8 text-center text-xs sm:text-sm font-medium" dir="ltr">{{ $item['quantity'] }}</span>
                                <button wire:click="updateQuantity({{ $index }}, {{ $item['quantity'] + 1 }})"
                                        class="w-8 h-8 sm:w-7 sm:h-7 flex items-center justify-center rounded bg-gray-200 text-gray-600 hover:bg-gray-300 active:bg-gray-400 active:scale-90 text-xs font-bold transition-all">
                                    +
                                </button>
                            </div>

                            {{-- Line Total --}}
                            <div class="text-end shrink-0">
                                <p class="text-xs sm:text-sm font-bold text-gray-800" dir="ltr">{{ number_format($item['line_total'] / 100, 2) }}</p>
                            </div>

                            {{-- Remove --}}
                            <button wire:click="removeItem({{ $index }})"
                                    class="min-w-[44px] min-h-[44px] flex items-center justify-center text-gray-400 hover:text-red-500 transition-colors shrink-0 -me-2 -my-2">
                                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                                </svg>
                            </button>
                        </div>

                        {{-- Instalment plans, when this product has any open to this buyer's tier --}}
                        @if(!empty($cartPlans[$index]))
                            @php $activePlanId = $item['installment_plan_id'] ?? null; @endphp
                            <div class="-mt-1 mb-1 ms-2 me-2 p-2.5 rounded-b-lg bg-indigo-50/70 border border-t-0 border-indigo-100 space-y-2">
                                <p class="text-[11px] font-medium text-indigo-800">{{ __('التقسيط') }}</p>
                                <div class="flex flex-wrap gap-1.5">
                                    <button type="button" wire:click="setItemPlan({{ $index }}, null)"
                                            class="px-2 py-1 text-[11px] rounded-lg border transition-colors {{ $activePlanId ? 'bg-white border-gray-200 text-gray-600 hover:border-indigo-300' : 'bg-indigo-600 border-indigo-600 text-white' }}">
                                        {{ __('دفعة واحدة') }}
                                    </button>
                                    @foreach($cartPlans[$index] as $plan)
                                        <button type="button" wire:click="setItemPlan({{ $index }}, {{ $plan['id'] }})"
                                                class="px-2 py-1 text-[11px] rounded-lg border transition-colors {{ (int) $activePlanId === $plan['id'] ? 'bg-indigo-600 border-indigo-600 text-white' : 'bg-white border-gray-200 text-gray-600 hover:border-indigo-300' }}">
                                            {{ $plan['label_ar'] }}
                                            <span class="opacity-70">· <span dir="ltr">{{ $plan['installments'] }}</span> {{ __('أقساط') }} {{ $plan['frequency_label'] }}</span>
                                        </button>
                                    @endforeach
                                </div>

                                @if(!empty($cartSchedules[$index]))
                                    @php $sch = $cartSchedules[$index]; @endphp
                                    <div class="space-y-1.5">
                                        <div class="flex flex-wrap items-center gap-1">
                                            @foreach($sch['schedule'] as $slot => $amount)
                                                <span class="px-1.5 py-0.5 rounded text-[10px] border {{ $slot < $sch['pay'] ? 'bg-emerald-100 border-emerald-200 text-emerald-800' : 'bg-white border-gray-200 text-gray-500' }}"
                                                      dir="ltr">{{ number_format($amount / 100, 2) }}</span>
                                            @endforeach
                                        </div>
                                        <div class="flex items-center gap-2">
                                            <label class="text-[11px] text-indigo-800 shrink-0">{{ __('يُدفع الآن:') }}</label>
                                            <div class="flex items-center gap-1">
                                                <button type="button" wire:click="setPayInstallments({{ $index }}, {{ $sch['pay'] - 1 }})"
                                                        class="w-6 h-6 flex items-center justify-center rounded bg-white border border-indigo-200 text-indigo-700 text-xs font-bold">-</button>
                                                <span class="w-6 text-center text-[11px] font-bold text-indigo-900" dir="ltr">{{ $sch['pay'] }}</span>
                                                <button type="button" wire:click="setPayInstallments({{ $index }}, {{ $sch['pay'] + 1 }})"
                                                        class="w-6 h-6 flex items-center justify-center rounded bg-white border border-indigo-200 text-indigo-700 text-xs font-bold">+</button>
                                            </div>
                                            <span class="text-[11px] text-indigo-700">{{ __('من') }} <span dir="ltr">{{ $sch['installments'] }}</span></span>
                                        </div>
                                    </div>
                                @endif
                            </div>
                        @endif
                    @empty
                        <div class="text-center py-8 sm:py-12">
                            <svg class="w-12 h-12 sm:w-16 sm:h-16 text-gray-200 mx-auto mb-3" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 3h2l.4 2M7 13h10l4-8H5.4M7 13L5.4 5M7 13l-2.293 2.293c-.63.63-.184 1.707.707 1.707H17m0 0a2 2 0 100 4 2 2 0 000-4zm-8 2a2 2 0 100 4 2 2 0 000-4z"/>
                            </svg>
                            <p class="text-gray-400 text-xs sm:text-sm">{{ __('السلة فارغة') }}</p>
                            <p class="text-gray-300 text-xs mt-1">{{ __('أضف عناصر من القائمة') }}</p>
                        </div>
                    @endforelse
                </div>

                {{-- Cart Footer: Totals & Checkout --}}
                @if(count($cart) > 0)
                <div class="border-t border-gray-200 p-3 sm:p-4 space-y-3">
                    {{-- Totals --}}
                    <div class="space-y-1">
                        @if($cartDiscount > 0)
                            <div class="flex justify-between text-xs sm:text-sm">
                                <span class="text-gray-500">{{ __('المجموع الفرعي') }}</span>
                                <span class="text-gray-700" dir="ltr">{{ number_format($cartSubtotal / 100, 2) }} {{ __('ج.م') }}</span>
                            </div>
                            <div class="flex justify-between text-xs sm:text-sm">
                                <span class="text-green-600">{{ __('الخصم') }}</span>
                                <span class="text-green-600" dir="ltr">-{{ number_format($cartDiscount / 100, 2) }} {{ __('ج.م') }}</span>
                            </div>
                        @endif
                        @if($serviceFee > 0)
                            <div class="flex justify-between text-xs sm:text-sm">
                                <span class="text-amber-600">{{ __('مصاريف خدمة') }}</span>
                                <span class="text-amber-600" dir="ltr">{{ number_format($serviceFee / 100, 2) }} {{ __('ج.م') }}</span>
                            </div>
                        @endif
                        <div class="flex justify-between text-sm sm:text-base font-bold pt-1 border-t border-gray-100">
                            <span class="text-gray-800">{{ __('الإجمالي') }}</span>
                            <span class="text-blue-700" dir="ltr">{{ number_format($cartGrandTotal / 100, 2) }} {{ __('ج.م') }}</span>
                        </div>
                        @if($hasInstallmentPlan)
                            <div class="flex justify-between text-sm sm:text-base font-bold">
                                <span class="text-indigo-800">{{ __('المطلوب الآن') }}</span>
                                <span class="text-indigo-700" dir="ltr">{{ number_format($amountDueNow / 100, 2) }} {{ __('ج.م') }}</span>
                            </div>
                            <div class="flex justify-between text-xs">
                                <span class="text-amber-700">{{ __('يُقسَّط على دفعات لاحقة') }}</span>
                                <span class="text-amber-700 font-bold" dir="ltr">{{ number_format(($cartGrandTotal - $amountDueNow) / 100, 2) }} {{ __('ج.م') }}</span>
                            </div>
                        @endif
                    </div>

                    {{-- Payment Method --}}
                    <div>
                        <label class="block text-xs sm:text-sm font-medium text-gray-700 mb-1">{{ __('طريقة الدفع') }}</label>
                        <div class="grid grid-cols-1 sm:grid-cols-3 gap-2">
                            @foreach($paymentMethods as $method)
                                <label class="relative flex items-center justify-center gap-2 p-2.5 min-h-[44px] border rounded-lg cursor-pointer transition-all text-xs sm:text-sm font-medium
                                    {{ $paymentMethod === $method->value ? 'border-blue-500 bg-blue-50 text-blue-700 ring-2 ring-blue-200' : 'border-gray-200 text-gray-600 hover:border-gray-300' }}">
                                    <input type="radio"
                                           wire:model.live="paymentMethod"
                                           value="{{ $method->value }}"
                                           class="sr-only">
                                    @switch($method->value)
                                        @case('cash') {{ __('نقدي') }} @break
                                        @case('card') {{ __('بطاقة') }} @break
                                        @case('wallet') {{ __('محفظة') }} @break
                                        @case('split') {{ __('تقسيم') }} @break
                                    @endswitch
                                </label>
                            @endforeach
                        </div>
                    </div>

                    {{-- Split Payment Builder --}}
                    @if($paymentMethod === 'split')
                        <div class="bg-gray-50 rounded-lg p-3 space-y-2">
                            <p class="text-xs font-medium text-gray-600">{{ __('تقسيم الدفع') }}</p>

                            @foreach($splitPayments as $index => $sp)
                                <div class="flex items-center gap-2 bg-white rounded px-2 py-1.5 min-h-[40px]">
                                    <span class="text-xs text-gray-600 flex-1">
                                        @switch($sp['method'])
                                            @case('cash') {{ __('نقدي') }} @break
                                            @case('card') {{ __('بطاقة') }} @break
                                            @case('wallet') {{ __('محفظة') }} @break
                                        @endswitch
                                    </span>
                                    <span class="text-xs font-bold" dir="ltr">{{ number_format($sp['amount'] / 100, 2) }}</span>
                                    <button wire:click="removeSplitPayment({{ $index }})" class="min-w-[44px] min-h-[44px] flex items-center justify-center text-red-400 hover:text-red-600 -me-2 -my-1">
                                        <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
                                        </svg>
                                    </button>
                                </div>
                            @endforeach

                            <div class="flex gap-2">
                                <select wire:model="splitMethod" class="flex-1 px-2 py-2 min-h-[44px] border border-gray-300 rounded text-xs">
                                    <option value="cash">{{ __('نقدي') }}</option>
                                    <option value="card">{{ __('بطاقة') }}</option>
                                    <option value="wallet">{{ __('محفظة') }}</option>
                                </select>
                                <input type="number"
                                       wire:model="splitAmount"
                                       min="0"
                                       step="0.01"
                                       placeholder="{{ __('المبلغ') }}"
                                       dir="ltr"
                                       class="w-24 px-2 py-2 min-h-[44px] border border-gray-300 rounded text-xs">
                                <button wire:click="addSplitPayment"
                                        class="min-w-[44px] min-h-[44px] px-3 bg-blue-600 text-white rounded text-xs hover:bg-blue-700 active:bg-blue-800 active:scale-90 transition-all font-bold">
                                    +
                                </button>
                            </div>

                            @php $splitTotal = collect($splitPayments)->sum(fn ($sp) => $sp['amount']); @endphp
                            @if(count($splitPayments) > 0)
                                <div class="flex justify-between text-xs pt-1 border-t border-gray-200">
                                    <span class="text-gray-500">{{ __('المدفوع') }}</span>
                                    <span class="{{ $splitTotal >= $amountDueNow ? 'text-green-600' : 'text-red-600' }} font-bold" dir="ltr">
                                        {{ number_format($splitTotal / 100, 2) }} / {{ number_format($amountDueNow / 100, 2) }}
                                    </span>
                                </div>
                            @endif
                        </div>
                    @endif

                    {{-- Partial Payment (Deposit) Option — a plan already says what is due today --}}
                    @if($allowsDeposit && !$hasInstallmentPlan && $paymentMethod !== 'split')
                    <div class="bg-blue-50 border border-blue-200 rounded-lg p-3 space-y-2">
                        <label class="flex items-center gap-2 cursor-pointer">
                            <input type="checkbox" wire:model.live="useDeposit"
                                   class="w-4 h-4 rounded border-blue-400 text-blue-600 focus:ring-blue-500">
                            <span class="text-xs sm:text-sm font-medium text-blue-800">{{ __('دفع مقدم (عربون) — الباقي لاحقاً') }}</span>
                        </label>
                        @if($useDeposit)
                        <div class="flex items-center gap-2 ms-6">
                            <label class="text-xs text-blue-700 shrink-0">{{ __('المبلغ المدفوع الآن (ج.م):') }}</label>
                            <input type="number"
                                   wire:model.live="depositAmount"
                                   min="{{ number_format($minimumDepositAmount / 100, 2, '.', '') }}"
                                   max="{{ number_format(($cartGrandTotal - 1) / 100, 2, '.', '') }}"
                                   step="0.01"
                                   dir="ltr"
                                   class="w-28 px-2 py-1.5 min-h-[40px] border border-blue-300 rounded-lg text-xs focus:ring-2 focus:ring-blue-500">
                        </div>
                        <div class="ms-6 text-xs text-blue-600 space-y-0.5">
                            <p>{{ __('الحد الأدنى:') }} <span class="font-bold" dir="ltr">{{ number_format($minimumDepositAmount / 100, 2) }} {{ __('ج.م') }}</span> ({{ $minimumDepositPercent }}%)</p>
                            <p>{{ __('المتبقي للدفع لاحقاً:') }} <span class="font-bold text-amber-700" dir="ltr">{{ number_format(($cartGrandTotal - (int) round($depositAmount * 100)) / 100, 2) }} {{ __('ج.م') }}</span></p>
                        </div>
                        @endif
                    </div>
                    @endif

                    {{-- Notes --}}
                    <div>
                        <input type="text"
                               wire:model="notes"
                               placeholder="{{ __('ملاحظات (اختياري)') }}"
                               class="w-full px-3 sm:px-4 py-2.5 min-h-[44px] border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 text-xs sm:text-sm">
                    </div>

                    {{-- Checkout Button --}}
                    <button wire:click="checkout"
                            wire:loading.attr="disabled"
                            wire:target="checkout"
                            @if($paymentMethod === 'split' && collect($splitPayments)->sum(fn ($sp) => $sp['amount']) < $amountDueNow) disabled @endif
                            class="w-full px-4 py-3 min-h-[48px] bg-green-600 text-white rounded-lg hover:bg-green-700 active:bg-green-800 active:scale-[0.97] text-xs sm:text-sm font-bold transition-all disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-2">
                        <span wire:loading.remove wire:target="checkout">
                            <svg class="w-5 h-5 inline-block" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 9V7a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2m2 4h10a2 2 0 002-2v-6a2 2 0 00-2-2H9a2 2 0 00-2 2v6a2 2 0 002 2zm7-5a2 2 0 11-4 0 2 2 0 014 0z"/>
                            </svg>
                            {{ __('إتمام البيع') }}
                        </span>
                        <span wire:loading wire:target="checkout">{{ __('جارٍ المعالجة...') }}</span>
                    </button>
                </div>
                @endif
            </div>
        </div>
    </div>

    {{-- Mobile Sticky Bottom Bar (visible only on mobile when cart has items) --}}
    @if(count($cart) > 0)
    <div class="fixed bottom-0 inset-x-0 z-40 lg:hidden bg-white border-t border-gray-200 shadow-[0_-4px_6px_-1px_rgba(0,0,0,0.1)]"
         x-show="!mobileCartOpen"
         x-transition:enter="transition ease-out duration-200"
         x-transition:enter-start="translate-y-full"
         x-transition:enter-end="translate-y-0"
         x-transition:leave="transition ease-in duration-150"
         x-transition:leave-start="translate-y-0"
         x-transition:leave-end="translate-y-full">
        <div class="flex items-center gap-3 p-3 max-w-screen-sm mx-auto">
            {{-- Cart summary --}}
            <button @click="mobileCartOpen = true; $nextTick(() => { document.getElementById('cart-section').scrollIntoView({ behavior: 'smooth' }) })"
                    class="flex-1 flex items-center gap-3 min-h-[48px]">
                <div class="relative">
                    <svg class="w-6 h-6 text-gray-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M3 3h2l.4 2M7 13h10l4-8H5.4M7 13L5.4 5M7 13l-2.293 2.293c-.63.63-.184 1.707.707 1.707H17m0 0a2 2 0 100 4 2 2 0 000-4zm-8 2a2 2 0 100 4 2 2 0 000-4z"/>
                    </svg>
                    <span class="absolute -top-1.5 -end-1.5 w-5 h-5 flex items-center justify-center bg-blue-600 text-white text-[10px] font-bold rounded-full">{{ count($cart) }}</span>
                </div>
                <div class="text-start">
                    <p class="text-xs text-gray-500">{{ __('الإجمالي') }}</p>
                    <p class="text-sm font-bold text-gray-800" dir="ltr">{{ number_format($cartGrandTotal / 100, 2) }} {{ __('ج.م') }}</p>
                </div>
            </button>

            {{-- Pay button --}}
            <button @click="mobileCartOpen = true; $nextTick(() => { document.getElementById('cart-section').scrollIntoView({ behavior: 'smooth' }) })"
                    class="px-5 py-3 min-h-[48px] bg-green-600 text-white rounded-lg text-sm font-bold hover:bg-green-700 active:bg-green-800 active:scale-[0.95] transition-all flex items-center gap-2 shrink-0">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 9V7a2 2 0 00-2-2H5a2 2 0 00-2 2v6a2 2 0 002 2h2m2 4h10a2 2 0 002-2v-6a2 2 0 00-2-2H9a2 2 0 00-2 2v6a2 2 0 002 2zm7-5a2 2 0 11-4 0 2 2 0 014 0z"/>
                </svg>
                {{ __('ادفع') }}
            </button>
        </div>
    </div>
    @endif

    {{-- Receipt Modal --}}
    @if($showReceipt)
    <div class="fixed inset-0 z-50 flex items-center justify-center bg-black/50 p-0 sm:p-4" x-data x-transition>
        <div class="bg-white sm:rounded-xl shadow-2xl w-full h-full sm:h-auto sm:max-w-sm overflow-hidden flex flex-col sm:block relative">
            <button wire:click="closeReceipt" class="absolute top-3 end-3 flex items-center justify-center w-9 h-9 rounded-lg text-gray-400 hover:text-gray-600 hover:bg-gray-100 active:bg-gray-200 transition-colors z-10" aria-label="{{ __('إغلاق') }}">
                <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/></svg>
            </button>
            <div class="p-4 sm:p-6 text-center flex-1 flex flex-col items-center justify-center">
                <div class="w-14 h-14 sm:w-16 sm:h-16 bg-green-100 rounded-full flex items-center justify-center mx-auto mb-3 sm:mb-4">
                    <svg class="w-7 h-7 sm:w-8 sm:h-8 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
                    </svg>
                </div>
                <h3 class="text-base sm:text-lg font-bold text-gray-800 mb-2">{{ __('تمت العملية بنجاح') }}</h3>
                <div class="bg-gray-50 rounded-lg p-3 sm:p-4 my-3 sm:my-4 space-y-2 w-full">
                    <div class="flex justify-between text-xs sm:text-sm">
                        <span class="text-gray-500">{{ __('رقم الإيصال') }}</span>
                        <span class="font-mono font-bold text-gray-800" dir="ltr">{{ $lastReceiptNumber }}</span>
                    </div>
                    <div class="flex justify-between text-xs sm:text-sm">
                        <span class="text-gray-500">{{ __('الإجمالي') }}</span>
                        <span class="font-bold text-gray-800" dir="ltr">{{ number_format($lastTransactionTotal / 100, 2) }} {{ __('ج.م') }}</span>
                    </div>
                    @if($lastTransactionPaid && $lastTransactionPaid < $lastTransactionTotal)
                    <div class="flex justify-between text-xs sm:text-sm">
                        <span class="text-gray-500">{{ __('المدفوع') }}</span>
                        <span class="font-bold text-green-700" dir="ltr">{{ number_format($lastTransactionPaid / 100, 2) }} {{ __('ج.م') }}</span>
                    </div>
                    <div class="flex justify-between text-xs sm:text-sm">
                        <span class="text-gray-500">{{ __('المتبقي') }}</span>
                        <span class="font-bold text-orange-700" dir="ltr">{{ number_format(($lastTransactionTotal - $lastTransactionPaid) / 100, 2) }} {{ __('ج.م') }}</span>
                    </div>
                    @endif
                </div>
            </div>
            <div class="border-t border-gray-200 p-3 sm:p-4 flex gap-3">
                @if($lastTransactionUuid)
                <x-ui.print-receipt-button :route="route('receipts.pos.print', $lastTransactionUuid)" class="flex-1" />
                @endif
                <button wire:click="closeReceipt"
                        class="flex-1 px-4 py-2.5 min-h-[44px] bg-blue-600 text-white rounded-lg text-xs sm:text-sm font-medium hover:bg-blue-700 active:bg-blue-800 active:scale-[0.97] transition-all">
                    {{ __('عملية جديدة') }}
                </button>
            </div>
        </div>
    </div>
    @endif

    @endif
</div>
