<div>
    {{-- Toolbar --}}
    <form class="mb-6 flex flex-wrap items-center gap-4" onsubmit="return false" toolname="search_experiments" tooldescription="Filter experiments by status, track, and search query">
        <div class="relative flex-1">
            <x-form-input wire:model.live.debounce.300ms="search" type="text" placeholder="Search experiments..." class="pl-10" toolparamdescription="Free-text search across experiment titles and theses">
                <x-slot:leadingIcon>
                    <i class="fa-solid fa-magnifying-glass pointer-events-none absolute left-3 top-1/2 -translate-y-1/2 text-base text-gray-400"></i>
                </x-slot:leadingIcon>
            </x-form-input>
        </div>

        <x-form-select wire:model.live="statusFilter" toolparamdescription="Filter by experiment status: draft, scoring, planning, building, executing, completed, killed, paused">
            <option value="">All Statuses</option>
            @foreach($statuses as $status)
                <option value="{{ $status->value }}">{{ str_replace('_', ' ', ucfirst($status->value)) }}</option>
            @endforeach
        </x-form-select>

        <x-form-select wire:model.live="trackFilter" toolparamdescription="Filter by experiment track: exploration, optimization, validation">
            <option value="">All Tracks</option>
            @foreach($tracks as $track)
                <option value="{{ $track->value }}">{{ ucfirst($track->value) }}</option>
            @endforeach
        </x-form-select>

        <button wire:click="$toggle('showCreateForm')"
            class="rounded-lg bg-primary-600 px-4 py-2 text-sm font-medium text-white hover:bg-primary-700">
            New Experiment
        </button>
    </form>

    {{-- Create Form --}}
    @if($showCreateForm)
        <livewire:experiments.create-experiment-form />
    @endif

    {{-- Table --}}
    @if(count($selectedIds) > 0)
        <x-assistant-select-toolbar :count="count($selectedIds)" />
    @endif

    <div class="overflow-hidden rounded-xl border border-gray-200 bg-white">
        <div class="overflow-x-auto">
        <table class="w-full table-fixed divide-y divide-gray-200">
            <thead class="bg-gray-50">
                <tr>
                    @php
                        $sortIcon = fn($field) => $sortField === $field
                            ? ($sortDirection === 'asc' ? '&#9650;' : '&#9660;')
                            : '<span class="text-gray-300">&#9650;</span>';
                    @endphp
                    <th class="w-10 px-3 py-2 md:px-6"></th>
                    <th wire:click="sortBy('title')" class="cursor-pointer px-3 py-2 md:px-6 md:py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500 hover:text-gray-700">
                        Title {!! $sortIcon('title') !!}
                    </th>
                    <th wire:click="sortBy('status')" class="cursor-pointer px-3 py-2 md:px-6 md:py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500 hover:text-gray-700">
                        Status {!! $sortIcon('status') !!}
                    </th>
                    <th class="hidden md:table-cell px-3 py-2 md:px-6 md:py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500">Track</th>
                    <th class="hidden md:table-cell px-3 py-2 md:px-6 md:py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500">Budget</th>
                    <th class="hidden md:table-cell px-3 py-2 md:px-6 md:py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500">Iteration</th>
                    <th wire:click="sortBy('created_at')" class="hidden md:table-cell cursor-pointer px-3 py-2 md:px-6 md:py-3 text-left text-xs font-medium uppercase tracking-wider text-gray-500 hover:text-gray-700">
                        Created {!! $sortIcon('created_at') !!}
                    </th>
                </tr>
            </thead>
            <tbody class="divide-y divide-gray-200">
                @forelse($experiments as $experiment)
                    <tr class="transition hover:bg-gray-50 {{ $this->isSelected($experiment->id) ? 'bg-indigo-50/50' : '' }}">
                        <td class="px-3 py-3 md:px-6 md:py-4" wire:click.stop="toggleSelection('{{ $experiment->id }}')">
                            <input type="checkbox"
                                   @checked($this->isSelected($experiment->id))
                                   wire:click.stop="toggleSelection('{{ $experiment->id }}')"
                                   class="h-4 w-4 cursor-pointer rounded border-gray-300 text-indigo-600 focus:ring-indigo-500">
                        </td>
                        <td class="px-3 py-3 md:px-6 md:py-4">
                            <a href="{{ route('experiments.show', $experiment) }}" class="font-medium text-primary-600 hover:text-primary-800">
                                {{ $experiment->title }}
                            </a>
                            @if($experiment->thesis)
                                <p class="mt-0.5 truncate text-xs text-gray-400">{{ $experiment->thesis }}</p>
                            @endif
                        </td>
                        <td class="px-3 py-3 md:px-6 md:py-4">
                            <x-status-badge :status="$experiment->status->value" />
                        </td>
                        <td class="hidden md:table-cell px-3 py-3 md:px-6 md:py-4 text-sm text-gray-500">{{ ucfirst($experiment->track->value) }}</td>
                        <td class="hidden md:table-cell px-3 py-3 md:px-6 md:py-4">
                            @if($experiment->budget_cap_credits > 0)
                                @php $pct = min(100, round(($experiment->budget_spent_credits / $experiment->budget_cap_credits) * 100)); @endphp
                                <div class="flex items-center gap-2">
                                    <div class="h-2 w-20 rounded-full bg-gray-200">
                                        <div class="h-2 rounded-full {{ $pct > 80 ? 'bg-red-500' : ($pct > 50 ? 'bg-yellow-500' : 'bg-green-500') }}" style="width: {{ $pct }}%"></div>
                                    </div>
                                    <span class="text-xs text-gray-500">{{ $pct }}%</span>
                                </div>
                            @else
                                <span class="text-xs text-gray-400">No cap</span>
                            @endif
                        </td>
                        <td class="hidden md:table-cell px-3 py-3 md:px-6 md:py-4 text-sm text-gray-500">{{ $experiment->current_iteration }}/{{ $experiment->max_iterations }}</td>
                        <td class="hidden md:table-cell px-3 py-3 md:px-6 md:py-4 text-sm text-gray-500">{{ $experiment->created_at->diffForHumans() }}</td>
                    </tr>
                @empty
                    <tr>
                        <td colspan="7" class="px-6 py-12 text-center text-sm text-gray-400">
                            No experiments found. Create your first one!
                        </td>
                    </tr>
                @endforelse
            </tbody>
        </table>
        </div>
    </div>

    <div class="mt-4">
        {{ $experiments->links() }}
    </div>
</div>
