@php
    $label = ['online' => __('servers.status_online'), 'warning' => __('servers.status_warning'), 'offline' => __('servers.status_offline'), 'pending' => __('servers.status_pending')];
    // Group colour token -> literal bg utility (see servers/groups.blade.php); R3 tokens only.
    $dot = ['accent' => 'bg-accent', 'cyan' => 'bg-cyan', 'online' => 'bg-online', 'warning' => 'bg-warning', 'offline' => 'bg-offline', 'ink' => 'bg-ink-3'];
@endphp

<div class="space-y-4">
    {{-- Header --}}
    <div class="flex flex-wrap items-end justify-between gap-3">
        <div>
            <p class="font-mono text-[11px] uppercase tracking-[0.2em] text-accent-text">{{ __('servers.fleet_eyebrow') }}</p>
            <h2 class="mt-0.5 font-display text-xl font-semibold text-ink sm:text-2xl">{{ __('servers.heading') }}</h2>
        </div>
        <div class="flex items-center gap-2">
            @can('manage-fleet')
                <a href="{{ route('servers.groups') }}" wire:navigate
                   class="inline-flex min-h-11 items-center gap-1.5 rounded-md border border-line bg-inset px-3 text-xs text-ink-2 transition-colors hover:bg-raised hover:text-ink">
                    <x-icon name="folder" class="h-3.5 w-3.5" /> {{ __('groups.title') }}
                </a>
                <x-modal-trigger variant="accent" component="modals.create-server">
                    <x-icon name="plus" class="h-3.5 w-3.5" />
                    {{ __('servers.add_server') }}
                </x-modal-trigger>
            @endcan
            <x-status-pill status="online">{{ __('servers.online_of_total', ['online' => $online, 'total' => $total]) }}</x-status-pill>
        </div>
    </div>

    {{-- Group filter (only when groups exist) --}}
    @if ($groups->isNotEmpty())
        <div class="flex flex-wrap items-center gap-1.5" role="group" aria-label="{{ __('groups.filter_label') }}">
            <button type="button" wire:click="$set('group', null)"
                    @class([
                        'inline-flex min-h-9 items-center rounded-md border px-3 text-xs transition-colors',
                        'border-accent/25 bg-accent/10 text-ink' => ! $activeGroup,
                        'border-line bg-inset text-ink-2 hover:bg-raised hover:text-ink' => $activeGroup,
                    ])>{{ __('groups.filter_all') }}</button>
            @foreach ($groups as $g)
                <button type="button" wire:click="$set('group', '{{ $g->uuid }}')"
                        @class([
                            'inline-flex min-h-9 items-center gap-1.5 rounded-md border px-3 text-xs transition-colors',
                            'border-accent/25 bg-accent/10 text-ink' => $activeGroup?->id === $g->id,
                            'border-line bg-inset text-ink-2 hover:bg-raised hover:text-ink' => $activeGroup?->id !== $g->id,
                        ])>
                    <span class="h-2 w-2 rounded-full {{ $dot[$g->color] ?? 'bg-accent' }}"></span>
                    {{ $g->name }}
                    <span class="font-mono text-[10px] text-ink-4">{{ $g->servers_count }}</span>
                </button>
            @endforeach
        </div>
    @endif

    {{-- KPI grid: 1 → 2 → 4 --}}
    <div class="grid grid-cols-1 gap-3 sm:grid-cols-2 xl:grid-cols-4">
        <x-kpi :label="__('servers.kpi_total')" :value="$total" icon="server" />
        <x-kpi :label="__('servers.kpi_online')" :value="$online" tone="online" icon="activity" />
        <x-kpi :label="__('servers.kpi_warning')" :value="$warning" tone="warning" />
        <x-kpi :label="__('servers.kpi_offline')" :value="$offline" tone="offline" />
    </div>

    {{-- Fleet --}}
    <x-panel :title="__('servers.fleet_title')" :subtitle="__('servers.in_cluster', ['count' => $total])" :padded="false">
        <x-slot:actions>
            <label class="relative block">
                <span class="sr-only">{{ __('servers.search_label') }}</span>
                <x-icon name="search" class="pointer-events-none absolute left-2.5 top-1/2 h-3.5 w-3.5 -translate-y-1/2 text-ink-4" />
                <input
                    type="search"
                    data-page-search
                    wire:model.live.debounce.300ms="search"
                    placeholder="{{ __('servers.search_placeholder') }}"
                    class="min-h-11 w-full rounded-md border border-line bg-inset py-1.5 pl-8 pr-3 font-mono text-xs text-ink placeholder:text-ink-4 focus:border-accent/40 focus:outline-none sm:w-56"
                />
            </label>
        </x-slot:actions>

        @if ($servers->isEmpty())
            <div class="px-4 py-10 text-center sm:px-5">
                <p class="text-sm text-ink-2">{{ __('servers.empty_title') }}</p>
                <p class="mt-1 font-mono text-[11px] text-ink-4">{{ __('servers.empty_hint') }}</p>
            </div>
        @else
            <div class="divide-y divide-line">
                @foreach ($servers as $server)
                    <a
                        href="{{ route('servers.show', $server) }}"
                        wire:key="server-{{ $server->uuid }}"
                        wire:navigate
                        class="flex min-h-11 items-center gap-3 px-4 py-3 transition-colors hover:bg-raised sm:px-5"
                    >
                        <x-status-dot :status="$server->status" :ping="in_array($server->status, ['online', 'pending'], true)" />

                        <div class="min-w-0 flex-1">
                            <p class="truncate text-sm text-ink">{{ $server->name }}</p>
                            <p class="truncate font-mono text-[11px] text-ink-3">
                                {{ $server->ip }}@if ($server->os) · {{ $server->os }}@endif
                            </p>
                        </div>

                        {{-- CPU / RAM mini bars — hidden on mobile to keep the row legible --}}
                        <div class="hidden shrink-0 items-center gap-4 md:flex">
                            <div class="w-20">
                                <div class="flex items-center justify-between">
                                    <span class="font-mono text-[10px] uppercase text-ink-3">CPU</span>
                                    <span class="tabular font-mono text-[10px] text-ink-3">{{ $server->cpu }}%</span>
                                </div>
                                <div class="mt-1 h-1 w-full overflow-hidden rounded-full bg-line">
                                    <div class="h-full rounded-full bg-accent" style="width: {{ $server->cpu }}%"></div>
                                </div>
                            </div>
                            <div class="w-20">
                                <div class="flex items-center justify-between">
                                    <span class="font-mono text-[10px] uppercase text-ink-3">RAM</span>
                                    <span class="tabular font-mono text-[10px] text-ink-3">{{ $server->mem }}%</span>
                                </div>
                                <div class="mt-1 h-1 w-full overflow-hidden rounded-full bg-line">
                                    <div class="h-full rounded-full bg-cyan" style="width: {{ $server->mem }}%"></div>
                                </div>
                            </div>
                        </div>

                        <x-status-pill :status="$server->status" class="shrink-0">
                            {{ $label[$server->status] ?? ucfirst($server->status) }}
                        </x-status-pill>
                    </a>
                @endforeach
            </div>
        @endif
    </x-panel>
</div>
