{{-- TemplatePicker — modal triggered by the parent composer via
     $dispatch('open-template-picker'). Emits `template-inserted`
     with the rendered HTML when a template is picked. --}}
<div wire:key="template-picker">
    @if($isOpen)
        <div class="fixed inset-0 z-50 flex items-center justify-center p-4 bg-black/60 backdrop-blur-sm"
             wire:click="close">
            <div class="bg-surface-2 rounded-2xl max-w-3xl w-full max-h-[85vh] overflow-hidden shadow-2xl border border-border flex flex-col"
                 wire:click.stop>

                {{-- Header --}}
                <div class="px-5 py-3 border-b border-border flex items-center justify-between gap-3 shrink-0">
                    <p class="text-sm font-semibold text-ink">{{ __('Insert template') }}</p>
                    <button type="button" wire:click="close" class="text-muted hover:text-ink">
                        <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/></svg>
                    </button>
                </div>

                {{-- Filters --}}
                <div class="px-5 py-3 border-b border-border flex flex-col sm:flex-row gap-2 shrink-0">
                    <input type="text" wire:model.live.debounce.300ms="search"
                           placeholder="{{ __('Search templates…') }}"
                           class="flex-1 px-3 py-2 text-sm bg-surface border border-border rounded-xl focus:outline-none focus:ring-2 focus:ring-primary-500">
                    <select wire:model.live="category"
                            class="px-3 py-2 text-sm bg-surface border border-border rounded-xl focus:outline-none focus:ring-2 focus:ring-primary-500">
                        @foreach($this->categories as $key => $label)
                            <option value="{{ $key }}">{{ $label }}</option>
                        @endforeach
                    </select>
                </div>

                {{-- List --}}
                <div class="flex-1 overflow-auto p-5">
                    @if($this->templates->isEmpty())
                        <div class="text-center py-10 text-sm text-muted">
                            {{ __('No templates match. Try a different search or category.') }}
                        </div>
                    @else
                        <div class="grid grid-cols-1 sm:grid-cols-2 gap-3">
                            @foreach($this->templates as $template)
                                <button type="button" wire:click="pick({{ $template->id }})"
                                        class="text-left p-3 bg-surface rounded-xl border border-border hover:border-primary-500 hover:bg-primary-600/5 transition">
                                    <div class="flex items-start justify-between gap-2">
                                        <p class="text-sm font-semibold text-ink truncate">{{ $template->name }}</p>
                                        @if($template->workspace_id === null)
                                            <span class="px-1.5 py-0.5 rounded text-[10px] font-bold uppercase tracking-wider bg-primary-600/10 text-primary-600 shrink-0">{{ __('Platform') }}</span>
                                        @endif
                                    </div>
                                    <p class="text-[11px] text-muted mt-0.5">
                                        {{ $this->categories[$template->category] ?? $template->category }}
                                        @if($template->usage_count > 0)
                                            · {{ $template->usage_count }} {{ __('uses') }}
                                        @endif
                                    </p>
                                </button>
                            @endforeach
                        </div>
                    @endif
                </div>

                {{-- Footer --}}
                <div class="px-5 py-3 border-t border-border flex items-center justify-between shrink-0">
                    <a href="{{ url('/templates') }}" wire:navigate
                       class="text-xs text-primary-600 hover:underline">
                        {{ __('Manage templates →') }}
                    </a>
                    <button type="button" wire:click="close"
                            class="px-3 py-1.5 text-xs rounded-lg border border-border text-muted hover:text-ink">
                        {{ __('Cancel') }}
                    </button>
                </div>
            </div>
        </div>
    @endif
</div>
