<div class="space-y-5">
    <div class="flex items-start justify-between gap-4 animate-rise">
        <div>
            <h1 class="text-2xl font-bold tracking-tight text-ink">{{ __('hosts.title') }}</h1>
            <p class="mt-1 text-sm text-muted">{{ __('hosts.subtitle') }}</p>
        </div>
        <a href="{{ route('admin.hosts.create') }}" wire:navigate>
            <x-ui.button variant="primary" size="sm"><x-ui.icon name="plus" class="size-4" />{{ __('hosts.add') }}</x-ui.button>
        </a>
    </div>

    {{-- Filter bar — scales to many hosts (search + datacenter + status). --}}
    <div class="flex flex-wrap items-center gap-2 animate-rise [animation-delay:40ms]">
        <div class="relative min-w-48 flex-1">
            <input type="search" wire:model.live.debounce.300ms="search" placeholder="{{ __('hosts.search_placeholder') }}"
                   class="w-full rounded-md border border-line-strong bg-surface px-3 py-2 text-sm text-ink placeholder:text-muted" />
        </div>
        <select wire:model.live="datacenter" class="rounded-md border border-line-strong bg-surface px-3 py-2 text-sm text-body">
            <option value="">{{ __('hosts.all_datacenters') }}</option>
            @foreach ($datacenters as $dc)
                <option value="{{ $dc->code }}">{{ $dc->name }} ({{ $dc->code }})</option>
            @endforeach
        </select>
        <select wire:model.live="status" class="rounded-md border border-line-strong bg-surface px-3 py-2 text-sm text-body">
            <option value="">{{ __('hosts.all_statuses') }}</option>
            @foreach ($statuses as $s)
                <option value="{{ $s }}">{{ __('hosts.status.'.$s) }}</option>
            @endforeach
        </select>
        @if ($search !== '' || $datacenter !== '' || $status !== '')
            <button type="button" wire:click="clearFilters" class="rounded-md border border-line px-3 py-2 text-sm text-muted hover:bg-surface-hover">{{ __('hosts.clear') }}</button>
        @endif
        <span class="ml-auto text-xs text-muted">{{ __('hosts.count', ['shown' => $hosts->count(), 'total' => $total]) }}</span>
    </div>

    @if ($hosts->isEmpty())
        <div class="rounded-lg border border-dashed border-line-strong bg-surface p-10 text-center animate-rise">
            <span class="mx-auto grid size-12 place-items-center rounded-lg bg-surface-2 text-muted"><x-ui.icon name="server" class="size-6" /></span>
            <p class="mt-3 text-sm text-muted">{{ $total === 0 ? __('hosts.empty') : __('hosts.no_matches') }}</p>
        </div>
    @else
        <div class="overflow-hidden rounded-lg border border-line bg-surface shadow-xs animate-rise [animation-delay:80ms]">
            <div class="overflow-x-auto">
                <table class="w-full text-sm">
                    <thead>
                        <tr class="border-b border-line bg-surface-2 text-left text-xs font-semibold text-muted">
                            <th class="px-4 py-3 font-semibold">{{ __('hosts.col.host') }}</th>
                            <th class="px-4 py-3 font-semibold">{{ __('hosts.col.datacenter') }}</th>
                            <th class="px-4 py-3 font-semibold">{{ __('hosts.col.health') }}</th>
                            <th class="px-4 py-3 font-semibold">{{ __('hosts.col.vpn') }}</th>
                            <th class="px-4 py-3 font-semibold">{{ __('hosts.col.instances') }}</th>
                            <th class="px-4 py-3 font-semibold">{{ __('hosts.col.capacity') }}</th>
                            <th class="px-4 py-3 font-semibold">{{ __('hosts.col.status') }}</th>
                        </tr>
                    </thead>
                    <tbody>
                        @foreach ($hosts as $host)
                            @php
                                $badge = ['pending' => 'pending', 'onboarding' => 'provisioning', 'active' => 'active', 'error' => 'failed', 'disabled' => 'suspended'][$host->status] ?? 'info';
                                $health = $host->healthState();
                                $hdot = ['online' => 'bg-success-bright', 'stale' => 'bg-warning', 'offline' => 'bg-danger'][$health];
                                $usedPct = $host->usedPct();

                                $vpnPeer = $host->vpnPeer;
                                $vpnState = $host->vpnState($vpnPeer);
                                $vpnIcon = ['not_configured' => 'unlock', 'connected' => 'lock', 'silent' => 'alert-triangle', 'unknown' => 'life-buoy'][$vpnState];
                                $vpnTone = ['not_configured' => 'text-muted', 'connected' => 'text-success', 'silent' => 'text-warning', 'unknown' => 'text-muted'][$vpnState];
                            @endphp
                            <tr wire:key="host-{{ $host->uuid }}" class="border-b border-line last:border-0 hover:bg-surface-hover">
                                <td class="px-4 py-3">
                                    <a href="{{ route('admin.hosts.show', $host) }}" wire:navigate class="flex items-center gap-3">
                                        <span class="grid size-9 shrink-0 place-items-center rounded-lg bg-surface-2 text-accent-text"><x-ui.icon name="server" class="size-4" /></span>
                                        <span class="min-w-0">
                                            <span class="block font-mono font-semibold text-ink">{{ $host->name }}</span>
                                            <span class="block font-mono text-xs text-muted">{{ $host->public_ip }}</span>
                                        </span>
                                    </a>
                                </td>
                                <td class="px-4 py-3 font-mono text-xs text-body">{{ $host->datacenter }}</td>
                                <td class="px-4 py-3">
                                    <span class="inline-flex items-center gap-1.5 text-xs text-body">
                                        <span class="size-2 rounded-pill {{ $hdot }}" aria-hidden="true"></span>{{ __('hosts.health.'.$health) }}
                                    </span>
                                </td>
                                <td class="px-4 py-3">
                                    <span class="inline-flex items-center gap-1.5 text-xs {{ $vpnTone }}">
                                        <x-ui.icon name="{{ $vpnIcon }}" class="size-4" />
                                        @if ($vpnState === 'silent' && $vpnPeer?->last_handshake_at)
                                            {{ __('hosts.vpn.silent_since', ['time' => $vpnPeer->last_handshake_at->diffForHumans()]) }}
                                        @else
                                            {{ __('hosts.vpn.'.$vpnState) }}
                                        @endif
                                    </span>
                                </td>
                                <td class="px-4 py-3 font-mono text-body">{{ $host->instances_count }}</td>
                                <td class="px-4 py-3">
                                    @if ($host->total_gb)
                                        <div class="flex items-center gap-2">
                                            <div class="h-1.5 w-24 overflow-hidden rounded-pill bg-surface-2">
                                                <div class="h-full rounded-pill {{ $usedPct >= 80 ? 'bg-danger' : 'bg-accent' }}" style="width: {{ $usedPct }}%"></div>
                                            </div>
                                            <span class="font-mono text-xs text-muted">{{ $host->availableGb() }} / {{ $host->freeGb() }} GB</span>
                                        </div>
                                    @else
                                        <span class="text-xs text-muted">{{ __('hosts.unknown') }}</span>
                                    @endif
                                </td>
                                <td class="px-4 py-3"><x-ui.badge :status="$badge">{{ __('hosts.status.'.$host->status) }}</x-ui.badge></td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    @endif
</div>
