<div>
    {{-- Header --}}
    <div class="mb-6">
        <h1 class="text-2xl font-bold text-gray-900">Dashboard</h1>
        @if($annoCorrente)
            <p class="text-sm text-gray-500">Anno Teocratico {{ $annoCorrente->label }} — {{ $annoCorrente->mesi_trascorsi }} mesi trascorsi</p>
        @endif
    </div>

    {{-- Stats cards --}}
    <div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4">
            <p class="text-xs font-medium text-gray-500 uppercase">Territori Attivi</p>
            <p class="mt-1 text-3xl font-bold text-gray-900">{{ $totTerritoriAttivi }}</p>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4">
            <p class="text-xs font-medium text-gray-500 uppercase">Assegnati</p>
            <p class="mt-1 text-3xl font-bold text-blue-600">{{ $totAssegnati }}</p>
            <p class="text-xs text-gray-500">{{ $totInReparto }} in reparto</p>
        </div>
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4">
            <p class="text-xs font-medium text-gray-500 uppercase">Percorsi (anno)</p>
            <p class="mt-1 text-3xl font-bold text-green-600">{{ $territoriPercorsi }}</p>
            <p class="text-xs text-gray-500">media {{ $mediaPercorrenzaMensile }}/mese</p>
        </div>
        @if($campagnaStats)
        <div class="bg-amber-50 rounded-xl shadow-sm border border-amber-200 p-4">
            <p class="text-xs font-medium text-amber-600 uppercase">Campagna</p>
            <p class="mt-1 text-lg font-bold text-amber-800">{{ $campagnaStats['descrizione'] }}</p>
            <div class="mt-2">
                <div class="flex justify-between text-xs text-amber-700 mb-1">
                    <span>{{ $campagnaStats['percentuale'] }}%</span>
                    <span>scade {{ $campagnaStats['fine'] }}</span>
                </div>
                <div class="w-full bg-amber-200 rounded-full h-2">
                    <div class="bg-amber-500 h-2 rounded-full transition-all" style="width: {{ min($campagnaStats['percentuale'], 100) }}%"></div>
                </div>
            </div>
        </div>
        @else
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 p-4">
            <p class="text-xs font-medium text-gray-500 uppercase">Campagna</p>
            <p class="mt-1 text-sm text-gray-400">Nessuna campagna attiva</p>
        </div>
        @endif
    </div>

    {{-- Quick lists --}}
    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        {{-- Da assegnare --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
            <div class="px-4 py-3 bg-green-50 border-b border-green-100">
                <h3 class="text-sm font-semibold text-green-800">Da Assegnare</h3>
            </div>
            <ul class="divide-y divide-gray-100">
                @forelse($daAssegnare as $t)
                <li class="px-4 py-2.5 flex items-center justify-between hover:bg-gray-50">
                    <div>
                        <a href="{{ route('territori.show', $t) }}" class="text-sm font-semibold text-gray-900 hover:text-indigo-600">N° {{ $t->numero }}</a>
                        <p class="text-xs text-gray-500">{{ $t->zona?->nome }} — {{ $t->tipologia?->nome }}</p>
                    </div>
                    @can('territori.assign')
                    <a href="{{ route('assegnazioni.assegna', ['territorioId' => $t->id]) }}" class="text-xs font-medium text-green-600 hover:text-green-800">Assegna →</a>
                    @endcan
                </li>
                @empty
                <li class="px-4 py-4 text-sm text-gray-400 text-center">Tutti assegnati</li>
                @endforelse
            </ul>
            @if($daAssegnare->count() >= 10)
            <div class="px-4 py-2 bg-gray-50 border-t text-center">
                <a href="{{ route('territori.index') }}?filtroStato=in_reparto" class="text-xs text-indigo-600 hover:text-indigo-800">Vedi tutti →</a>
            </div>
            @endif
        </div>

        {{-- Prioritari --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
            <div class="px-4 py-3 bg-amber-50 border-b border-amber-100">
                <h3 class="text-sm font-semibold text-amber-800">★ Prioritari</h3>
            </div>
            <ul class="divide-y divide-gray-100">
                @forelse($prioritari as $t)
                <li class="px-4 py-2.5 flex items-center justify-between hover:bg-gray-50">
                    <div>
                        <a href="{{ route('territori.show', $t) }}" class="text-sm font-semibold text-gray-900 hover:text-indigo-600">N° {{ $t->numero }}</a>
                        <p class="text-xs text-gray-500">
                            {{ $t->zona?->nome }}
                            @if($t->ultimaAssegnazione?->returned_at)
                            — ultimo rientro {{ $t->ultimaAssegnazione->returned_at->diffForHumans() }}
                            @endif
                        </p>
                    </div>
                    <span class="text-xs font-medium text-amber-600">
                        {{ $t->prioritario ? 'Man' : 'Auto' }}
                    </span>
                </li>
                @empty
                <li class="px-4 py-4 text-sm text-gray-400 text-center">Nessun territorio prioritario</li>
                @endforelse
            </ul>
            @if($prioritari->count() >= 10)
            <div class="px-4 py-2 bg-gray-50 border-t text-center">
                <a href="{{ route('territori.index') }}?filtroStato=prioritari" class="text-xs text-indigo-600 hover:text-indigo-800">Vedi tutti →</a>
            </div>
            @endif
        </div>

        {{-- Da rientrare --}}
        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
            <div class="px-4 py-3 bg-red-50 border-b border-red-100">
                <h3 class="text-sm font-semibold text-red-800">Da Rientrare</h3>
            </div>
            <ul class="divide-y divide-gray-100">
                @forelse($daRientrare as $t)
                @php($assegnazioneCorrente = $t->assegnazioneCorrente)
                <li class="px-4 py-2.5 flex items-center justify-between hover:bg-gray-50">
                    <div>
                        <a href="{{ route('territori.show', $t) }}" class="text-sm font-semibold text-gray-900 hover:text-indigo-600">N° {{ $t->numero }}</a>
                        <p class="text-xs text-gray-500">
                            {{ $assegnazioneCorrente?->proclamatore?->nome_completo }}
                            — {{ $assegnazioneCorrente?->giorni }} giorni
                        </p>
                    </div>
                    @can('territori.return')
                        @if($assegnazioneCorrente)
                    <a href="{{ route('assegnazioni.rientra', ['assegnazione' => $assegnazioneCorrente->id]) }}" class="text-xs font-medium text-red-600 hover:text-red-800">Rientra →</a>
                        @endif
                    @endcan
                </li>
                @empty
                <li class="px-4 py-4 text-sm text-gray-400 text-center">Nessun territorio da rientrare</li>
                @endforelse
            </ul>
            @if($daRientrare->count() >= 10)
            <div class="px-4 py-2 bg-gray-50 border-t text-center">
                <a href="{{ route('territori.index') }}?filtroStato=da_rientrare" class="text-xs text-indigo-600 hover:text-indigo-800">Vedi tutti →</a>
            </div>
            @endif
        </div>
    </div>
</div>
