<div>
    <div class="mb-4 sm:mb-6">
        <h1 class="text-xl sm:text-2xl font-bold text-gray-800">{{ __('لوحة التحكم') }}</h1>
        <p class="text-gray-500 text-sm mt-1">{{ __('مرحباً') }} {{ auth()->user()->name_ar ?? auth()->user()->name }}</p>
    </div>

    <!-- Row 1: Key Stats — 2-col on mobile, 4-col on desktop -->
    <div class="grid grid-cols-2 lg:grid-cols-4 gap-3 sm:gap-4 mb-4 sm:mb-6">
        <!-- Active Participants -->
        <div class="bg-white rounded-xl shadow-sm border border-green-200 p-3 sm:p-5">
            <div class="flex items-center gap-2 sm:gap-3">
                <div class="shrink-0 w-9 h-9 sm:w-12 sm:h-12 bg-green-50 rounded-lg flex items-center justify-center">
                    <svg class="w-5 h-5 sm:w-6 sm:h-6 text-green-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M17 20h5v-2a3 3 0 00-5.356-1.857M17 20H7m10 0v-2c0-.656-.126-1.283-.356-1.857M7 20H2v-2a3 3 0 015.356-1.857M7 20v-2c0-.656.126-1.283.356-1.857m0 0a5.002 5.002 0 019.288 0M15 7a3 3 0 11-6 0 3 3 0 016 0zm6 3a2 2 0 11-4 0 2 2 0 014 0zM7 10a2 2 0 11-4 0 2 2 0 014 0z"/>
                    </svg>
                </div>
                <div class="min-w-0">
                    <p class="text-xl sm:text-3xl font-bold text-green-600" dir="ltr">{{ number_format($stats['active_participants']) }}</p>
                    <p class="text-xs sm:text-sm text-gray-500 truncate">{{ __('المشتركون النشطون') }}</p>
                </div>
            </div>
        </div>

        <!-- Active Enrollments -->
        <div class="bg-white rounded-xl shadow-sm border border-blue-200 p-3 sm:p-5">
            <div class="flex items-center gap-2 sm:gap-3">
                <div class="shrink-0 w-9 h-9 sm:w-12 sm:h-12 bg-blue-50 rounded-lg flex items-center justify-center">
                    <svg class="w-5 h-5 sm:w-6 sm:h-6 text-blue-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-6 9l2 2 4-4"/>
                    </svg>
                </div>
                <div class="min-w-0">
                    <p class="text-xl sm:text-3xl font-bold text-blue-600" dir="ltr">{{ number_format($stats['active_enrollments']) }}</p>
                    <p class="text-xs sm:text-sm text-gray-500 truncate">{{ __('التسجيلات النشطة') }}</p>
                </div>
            </div>
        </div>

        <!-- Active Groups -->
        <div class="bg-white rounded-xl shadow-sm border border-purple-200 p-3 sm:p-5">
            <div class="flex items-center gap-2 sm:gap-3">
                <div class="shrink-0 w-9 h-9 sm:w-12 sm:h-12 bg-purple-50 rounded-lg flex items-center justify-center">
                    <svg class="w-5 h-5 sm:w-6 sm:h-6 text-purple-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 11H5m14 0a2 2 0 012 2v6a2 2 0 01-2 2H5a2 2 0 01-2-2v-6a2 2 0 012-2m14 0V9a2 2 0 00-2-2M5 11V9a2 2 0 012-2m0 0V5a2 2 0 012-2h6a2 2 0 012 2v2M7 7h10"/>
                    </svg>
                </div>
                <div class="min-w-0">
                    <p class="text-xl sm:text-3xl font-bold text-purple-600" dir="ltr">{{ number_format($stats['active_groups']) }}</p>
                    <p class="text-xs sm:text-sm text-gray-500 truncate">{{ __('المجموعات النشطة') }}</p>
                </div>
            </div>
        </div>

        <!-- Today Sessions -->
        <div class="bg-white rounded-xl shadow-sm border border-amber-200 p-3 sm:p-5">
            <div class="flex items-center gap-2 sm:gap-3">
                <div class="shrink-0 w-9 h-9 sm:w-12 sm:h-12 bg-amber-50 rounded-lg flex items-center justify-center">
                    <svg class="w-5 h-5 sm:w-6 sm:h-6 text-amber-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M8 7V3m8 4V3m-9 8h10M5 21h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v12a2 2 0 002 2z"/>
                    </svg>
                </div>
                <div class="min-w-0">
                    <p class="text-xl sm:text-3xl font-bold text-amber-600" dir="ltr">{{ number_format($stats['today_sessions']) }}</p>
                    <p class="text-xs sm:text-sm text-gray-500 truncate">{{ __('جلسات اليوم') }}</p>
                </div>
            </div>
        </div>
    </div>

    <!-- Row 2: Financial Summary -->
    <div class="grid grid-cols-1 sm:grid-cols-2 gap-3 sm:gap-4 mb-4 sm:mb-6">
        <!-- Revenue This Month -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
            <div class="flex items-center gap-3 mb-2 sm:mb-3">
                <div class="shrink-0 w-9 h-9 sm:w-10 sm:h-10 bg-emerald-50 rounded-lg flex items-center justify-center">
                    <svg class="w-4 h-4 sm:w-5 sm:h-5 text-emerald-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8c-1.657 0-3 .895-3 2s1.343 2 3 2 3 .895 3 2-1.343 2-3 2m0-8c1.11 0 2.08.402 2.599 1M12 8V7m0 1v8m0 0v1m0-1c-1.11 0-2.08-.402-2.599-1M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
                    </svg>
                </div>
                <h3 class="text-xs sm:text-sm font-medium text-gray-500">{{ __('إيرادات الشهر') }}</h3>
            </div>
            <p class="text-lg sm:text-2xl font-bold text-gray-800" dir="ltr">{{ number_format($financial['revenue_this_month'] / 100, 2) }} {{ __('ج.م') }}</p>
            <p class="text-xs sm:text-sm text-gray-500 mt-1 sm:mt-2">
                {{ __('مبيعات اليوم:') }}
                <span dir="ltr" class="font-medium">{{ number_format($financial['pos_today_count']) }}</span>
                {{ __('عملية بقيمة') }}
                <span dir="ltr" class="font-medium">{{ number_format($financial['pos_today'] / 100, 2) }} {{ __('ج.م') }}</span>
            </p>
        </div>

        <!-- Outstanding Invoices -->
        <div class="bg-white rounded-xl shadow-sm border {{ $financial['outstanding_invoices'] > 0 ? 'border-red-200' : 'border-gray-200' }} p-4 sm:p-6">
            <div class="flex items-center gap-3 mb-2 sm:mb-3">
                <div class="shrink-0 w-9 h-9 sm:w-10 sm:h-10 {{ $financial['outstanding_invoices'] > 0 ? 'bg-red-50' : 'bg-gray-50' }} rounded-lg flex items-center justify-center">
                    <svg class="w-4 h-4 sm:w-5 sm:h-5 {{ $financial['outstanding_invoices'] > 0 ? 'text-red-600' : 'text-gray-600' }}" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"/>
                    </svg>
                </div>
                <h3 class="text-xs sm:text-sm font-medium text-gray-500">{{ __('الفواتير المعلقة') }}</h3>
            </div>
            <p class="text-lg sm:text-2xl font-bold {{ $financial['outstanding_invoices'] > 0 ? 'text-red-600' : 'text-gray-800' }}" dir="ltr">{{ number_format($financial['outstanding_invoices'] / 100, 2) }} {{ __('ج.م') }}</p>
        </div>
    </div>

    <!-- Row 3: Attendance Summary -->
    <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6 mb-4 sm:mb-6">
        <div class="flex items-center justify-between mb-3 sm:mb-4">
            <h3 class="text-base sm:text-lg font-semibold text-gray-800">{{ __('حضور اليوم') }}</h3>
            <span class="text-xl sm:text-2xl font-bold {{ $attendance['rate'] >= 90 ? 'text-green-600' : ($attendance['rate'] >= 75 ? 'text-amber-600' : 'text-red-600') }}" dir="ltr">{{ $attendance['rate'] }}%</span>
        </div>

        <!-- Progress bar -->
        <div class="w-full bg-gray-200 rounded-full h-3 sm:h-4 mb-3 sm:mb-4">
            <div class="h-3 sm:h-4 rounded-full transition-all duration-300 {{ $attendance['rate'] >= 90 ? 'bg-green-500' : ($attendance['rate'] >= 75 ? 'bg-amber-500' : 'bg-red-500') }}"
                 style="width: {{ $attendance['rate'] }}%"></div>
        </div>

        <div class="flex flex-wrap items-center gap-3 sm:gap-6 text-xs sm:text-sm text-gray-600">
            <span>{{ __('الحاضرون:') }} <span dir="ltr" class="font-medium">{{ number_format($attendance['present']) }}</span> {{ __('من') }} <span dir="ltr" class="font-medium">{{ number_format($attendance['expected']) }}</span></span>
            <span class="text-gray-300 hidden sm:inline">|</span>
            <span>{{ __('الغائبون:') }} <span dir="ltr" class="font-medium text-red-600">{{ number_format($attendance['absent']) }}</span></span>
        </div>
    </div>

    <!-- Row 4: Alert Badges -->
    @if($lowStockCount > 0 || $nearFullGroups > 0 || $recentEnrollments > 0)
    <div class="flex flex-wrap gap-2 sm:gap-4 mb-4 sm:mb-6">
        @if($lowStockCount > 0)
        <div class="inline-flex items-center gap-2 bg-red-50 border border-red-200 text-red-700 px-3 py-2 rounded-lg text-xs sm:text-sm font-medium">
            <svg class="w-4 h-4 shrink-0" 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>
            <span dir="ltr" class="font-bold">{{ $lowStockCount }}</span> {{ __('منتج تحت الحد الأدنى') }}
        </div>
        @endif

        @if($nearFullGroups > 0)
        <div class="inline-flex items-center gap-2 bg-amber-50 border border-amber-200 text-amber-700 px-3 py-2 rounded-lg text-xs sm:text-sm font-medium">
            <svg class="w-4 h-4 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"/>
            </svg>
            <span dir="ltr" class="font-bold">{{ $nearFullGroups }}</span> {{ __('مجموعة شبه ممتلئة') }}
        </div>
        @endif

        @if($recentEnrollments > 0)
        <div class="inline-flex items-center gap-2 bg-green-50 border border-green-200 text-green-700 px-3 py-2 rounded-lg text-xs sm:text-sm font-medium">
            <svg class="w-4 h-4 shrink-0" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M18 9v3m0 0v3m0-3h3m-3 0h-3m-2-5a4 4 0 11-8 0 4 4 0 018 0zM3 20a6 6 0 0112 0v1H3v-1z"/>
            </svg>
            <span dir="ltr" class="font-bold">{{ $recentEnrollments }}</span> {{ __('تسجيل جديد (٧ أيام)') }}
        </div>
        @endif
    </div>
    @endif

    <!-- Row 5: Today's Schedule + Overdue Invoices -->
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-3 sm:gap-4 mb-4 sm:mb-6">
        <!-- Today's Schedule -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
            <h3 class="text-base sm:text-lg font-semibold text-gray-800 mb-3 sm:mb-4">{{ __('جدول اليوم') }}</h3>
            @if($todaySchedule->isEmpty())
                <p class="text-gray-400 text-sm">{{ __('لا توجد جلسات مجدولة اليوم') }}</p>
            @else
                <div class="space-y-2 sm:space-y-3 max-h-64 overflow-y-auto">
                    @foreach($todaySchedule as $session)
                    <div class="flex items-center gap-3 p-2.5 sm:p-3 bg-gray-50 rounded-lg">
                        <div class="text-center min-w-[44px]">
                            <span class="text-xs sm:text-sm font-bold text-blue-600" dir="ltr">{{ substr($session->start_time, 0, 5) }}</span>
                        </div>
                        <div class="flex-1 min-w-0">
                            <p class="text-xs sm:text-sm font-medium text-gray-800 truncate">{{ $session->group?->name_ar ?? '' }}</p>
                            <p class="text-xs text-gray-500 truncate">{{ $session->group?->program?->name_ar ?? '' }}</p>
                        </div>
                        <span class="text-xs px-2 py-1 rounded-full whitespace-nowrap
                            {{ $session->status === 'completed' ? 'bg-green-100 text-green-700' :
                               ($session->status === 'in_progress' ? 'bg-blue-100 text-blue-700' : 'bg-gray-100 text-gray-600') }}">
                            {{ $session->status === 'completed' ? __('مكتملة') :
                               ($session->status === 'in_progress' ? __('جارية') : __('مجدولة')) }}
                        </span>
                    </div>
                    @endforeach
                </div>
            @endif
        </div>

        <!-- Overdue Invoices -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
            <h3 class="text-base sm:text-lg font-semibold text-gray-800 mb-3 sm:mb-4">{{ __('فواتير متأخرة') }}</h3>
            @if($overdueInvoices->isEmpty())
                <p class="text-gray-400 text-sm">{{ __('لا توجد فواتير متأخرة') }}</p>
            @else
                <div class="space-y-2 sm:space-y-3 max-h-64 overflow-y-auto">
                    @foreach($overdueInvoices as $inv)
                    <a href="{{ route('invoices.show', $inv) }}" class="flex items-center justify-between p-2.5 sm:p-3 bg-red-50 rounded-lg hover:bg-red-100 transition-colors">
                        <div class="min-w-0 flex-1 me-2">
                            <p class="text-xs sm:text-sm font-medium text-gray-800 truncate">{{ $inv->contact_name ?? $inv->number }}</p>
                            <p class="text-xs text-red-600">{{ __('استحقاق:') }} {{ $inv->due_date?->format('Y-m-d') }}</p>
                        </div>
                        <span class="text-xs sm:text-sm font-bold text-red-700 whitespace-nowrap" dir="ltr">{{ number_format(($inv->total_amount - $inv->paid_amount) / 100, 2) }} {{ __('ج.م') }}</span>
                    </a>
                    @endforeach
                </div>
            @endif
        </div>
    </div>

    <!-- Row 6: Recent Payments + Upcoming Birthdays -->
    <div class="grid grid-cols-1 lg:grid-cols-2 gap-3 sm:gap-4">
        <!-- Recent Payments -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
            <h3 class="text-base sm:text-lg font-semibold text-gray-800 mb-3 sm:mb-4">{{ __('آخر المدفوعات') }}</h3>
            @if($recentPayments->isEmpty())
                <p class="text-gray-400 text-sm">{{ __('لا توجد مدفوعات حديثة') }}</p>
            @else
                <div class="space-y-2 sm:space-y-3 max-h-64 overflow-y-auto">
                    @foreach($recentPayments as $payment)
                    <div class="flex items-center justify-between p-2.5 sm:p-3 bg-gray-50 rounded-lg">
                        <div class="min-w-0 flex-1 me-2">
                            <p class="text-xs sm:text-sm font-medium text-gray-800 truncate">{{ $payment->reference ?? '-' }}</p>
                            <p class="text-xs text-gray-500 truncate">{{ $payment->created_at?->diffForHumans() }} — {{ $payment->creator?->name ?? '' }}</p>
                        </div>
                        <span class="text-xs sm:text-sm font-bold text-emerald-600 whitespace-nowrap" dir="ltr">{{ number_format($payment->amount / 100, 2) }} {{ __('ج.م') }}</span>
                    </div>
                    @endforeach
                </div>
            @endif
        </div>

        <!-- Upcoming Birthdays -->
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4 sm:p-6">
            <h3 class="text-base sm:text-lg font-semibold text-gray-800 mb-3 sm:mb-4">{{ __('أعياد ميلاد قادمة') }}</h3>
            @if($upcomingBirthdays->isEmpty())
                <p class="text-gray-400 text-sm">{{ __('لا توجد أعياد ميلاد قريبة') }}</p>
            @else
                <div class="space-y-2 sm:space-y-3 max-h-64 overflow-y-auto">
                    @foreach($upcomingBirthdays as $person)
                    <div class="flex items-center gap-3 p-2.5 sm:p-3 bg-pink-50 rounded-lg">
                        <div class="shrink-0 w-8 h-8 bg-pink-200 rounded-full flex items-center justify-center">
                            <svg class="w-4 h-4 text-pink-600" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 15.546c-.523 0-1.046.151-1.5.454a2.704 2.704 0 01-3 0 2.704 2.704 0 00-3 0 2.704 2.704 0 01-3 0 2.704 2.704 0 00-3 0 2.704 2.704 0 01-3 0A1.75 1.75 0 003 15.546V12a9 9 0 0118 0v3.546z"/>
                            </svg>
                        </div>
                        <div class="flex-1 min-w-0">
                            <p class="text-xs sm:text-sm font-medium text-gray-800 truncate">{{ $person->name_ar ?? $person->name }}</p>
                            <p class="text-xs text-gray-500">{{ $person->date_of_birth?->format('d/m') }}</p>
                        </div>
                    </div>
                    @endforeach
                </div>
            @endif
        </div>
    </div>
</div>
