<x-filament-panels::page>
    @php
        $cols = $this->getColumns();
        $counts = $this->getCounts();
    @endphp

    <style>
        /* Mobile-first 390px touch targets */
        @media (max-width: 600px) {
            .mb-stats { gap: 8px !important; }
            .mb-stat { min-width: 130px !important; padding: 10px 14px !important; }
            .mb-stat .val { font-size: 22px !important; }
            .mb-grid { grid-template-columns: 1fr !important; gap: 10px !important; }
            .mb-col { padding: 10px !important; min-height: 120px !important; }
            .mb-card { padding: 14px !important; }
            /* Larger touch targets — 44px min for accessibility */
            .mb-card button {
                min-height: 36px !important;
                padding: 8px 12px !important;
                font-size: 13px !important;
            }
            .mb-card .num { font-size: 15px !important; }
            .mb-card .plate { font-size: 14px !important; }
            /* Block reason modal: full-screen on mobile */
            .mb-modal-content {
                max-width: none !important;
                width: 95% !important;
                padding: 16px !important;
            }
        }

        .mb-stats { display: flex; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
        .mb-stat {
            background: #fff; border: 1px solid #e5e7eb; border-radius: 10px;
            padding: 12px 18px; min-width: 140px;
        }
        .dark .mb-stat { background: #1f2937; border-color: #374151; }
        .mb-stat .lbl { font-size: 11px; text-transform: uppercase; color: #6b7280; letter-spacing: .03em; }
        .mb-stat .val { font-size: 28px; font-weight: 700; color: #111827; }
        .dark .mb-stat .val { color: #f9fafb; }

        .mb-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(260px, 1fr)); gap: 14px; }
        .mb-col {
            background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 12px;
            padding: 12px; min-height: 200px;
        }
        .dark .mb-col { background: #1f2937; border-color: #374151; }
        .mb-col-head {
            font-size: 12px; font-weight: 600; text-transform: uppercase;
            letter-spacing: .04em; padding-bottom: 10px; margin-bottom: 10px;
            border-bottom: 2px solid; display: flex; justify-content: space-between;
        }
        .mb-card {
            background: #fff; border-radius: 10px; padding: 12px 14px;
            margin-bottom: 8px; cursor: pointer; transition: transform .1s, box-shadow .1s;
            border: 1px solid #e5e7eb; text-decoration: none; color: inherit; display: block;
        }
        .dark .mb-card { background: #111827; border-color: #374151; }
        .mb-card:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(0,0,0,.06); }
        .mb-card .num { font-size: 13px; font-weight: 700; color: #3b82f6; }
        .mb-card .plate { font-size: 13px; color: #4b5563; }
        .dark .mb-card .plate { color: #d1d5db; }
        .mb-card .client { font-size: 12px; color: #6b7280; margin-top: 2px; }
        .mb-card .complaint {
            font-size: 12px; color: #374151; margin-top: 8px;
            display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
        }
        .dark .mb-card .complaint { color: #e5e7eb; }
        .mb-empty { color: #9ca3af; text-align: center; padding: 20px 8px; font-size: 12px; }
    </style>

    <div class="mb-stats">
        <div class="mb-stat">
            <div class="lbl">Active acum</div>
            <div class="val">{{ $counts['active'] }}</div>
        </div>
        <div class="mb-stat">
            <div class="lbl">Închise azi</div>
            <div class="val">{{ $counts['closed_today'] }}</div>
        </div>
    </div>

    <div class="mb-grid">
        @foreach ($cols as $col)
            <div class="mb-col">
                <div class="mb-col-head" style="border-color: {{ $col['color'] }}; color: {{ $col['color'] }};">
                    <span>{{ $col['label'] }}</span>
                    <span>{{ $col['items']->count() }}</span>
                </div>
                @forelse ($col['items'] as $wo)
                    <div class="mb-card" style="cursor:default;">
                        <div style="display:flex;justify-content:space-between;align-items:center;">
                            <a href="{{ route('filament.tenant.resources.work-orders.edit', $wo) }}" class="num" style="text-decoration:none;color:#3b82f6;">#{{ $wo->number }}</a>
                            <span class="plate">{{ $wo->vehicle?->plate ?? '—' }}</span>
                        </div>
                        <div class="client">{{ $wo->client?->name ?? 'fără client' }} · {{ $wo->vehicle?->make }} {{ $wo->vehicle?->model }}</div>
                        @if ($wo->complaint)
                            <div class="complaint">{{ $wo->complaint }}</div>
                        @endif
                        @php $works = $this->getWorksFor($wo->id) @endphp
                        @if (! empty($works))
                            <div style="margin-top:8px; border-top:1px solid #f3f4f6; padding-top:8px;">
                                @foreach ($works as $w)
                                    <div style="font-size:12px; margin-bottom:6px; padding:6px 8px; background:#f9fafb; border-radius:6px;">
                                        @php
                                            $ms = $w->mechanic_status ?: 'pending';
                                            $stColor = ['pending' => '#94a3b8', 'in_progress' => '#3b82f6', 'paused' => '#f59e0b', 'done' => '#10b981', 'blocked' => '#dc2626'][$ms];
                                            $eff = $w->efficiencyClass();
                                            $effColor = ['green' => '#10b981', 'amber' => '#f59e0b', 'red' => '#dc2626'][$eff ?? ''] ?? null;
                                        @endphp
                                        <div style="display:flex;justify-content:space-between;align-items:flex-start;gap:6px;">
                                            <div style="flex:1;">
                                                <span style="font-weight:600;">{{ $w->name }}</span>
                                                <div style="margin-top:3px; display:flex; gap:4px; align-items:center; flex-wrap:wrap;">
                                                    <span style="background:{{ $stColor }}; color:white; padding:1px 6px; border-radius:4px; font-size:10px; font-weight:600;">{{ \App\Models\Tenant\WorkOrderWork::MECHANIC_STATUSES[$ms] }}</span>
                                                    @if ($w->hours > 0)
                                                        <span style="color:#6b7280; font-size:10px;">{{ rtrim(rtrim(number_format($w->hours, 2), '0'), '.') }}h norm</span>
                                                    @endif
                                                    @if ($w->actual_hours > 0 && $effColor)
                                                        <span style="color:{{ $effColor }}; font-weight:600; font-size:10px;">{{ rtrim(rtrim(number_format($w->actual_hours, 2), '0'), '.') }}h real ({{ $w->efficiencyPct() }}%)</span>
                                                    @endif
                                                    @if ($w->block_reason)
                                                        <span style="color:#dc2626; font-size:10px;">⚠ {{ \App\Models\Tenant\WorkOrderWork::BLOCK_REASONS[$w->block_reason] ?? $w->block_reason }}</span>
                                                    @endif
                                                </div>
                                            </div>
                                        </div>
                                        <div style="display:flex; gap:4px; margin-top:6px; flex-wrap:wrap;">
                                            @if (in_array($ms, ['pending', 'blocked']))
                                                <button wire:click="startWork({{ $w->id }})" style="background:#3b82f6;color:white;border:none;padding:4px 8px;border-radius:4px;font-size:11px;cursor:pointer;">▶ Start</button>
                                            @endif
                                            @if ($ms === 'in_progress')
                                                <button wire:click="pauseWork({{ $w->id }})" style="background:#f59e0b;color:white;border:none;padding:4px 8px;border-radius:4px;font-size:11px;cursor:pointer;">⏸ Pauză</button>
                                                <button wire:click="doneWork({{ $w->id }})" style="background:#10b981;color:white;border:none;padding:4px 8px;border-radius:4px;font-size:11px;cursor:pointer;">✓ Done</button>
                                                <button wire:click="openBlockModal({{ $w->id }})" style="background:#fff;color:#dc2626;border:1px solid #dc2626;padding:4px 8px;border-radius:4px;font-size:11px;cursor:pointer;">🔴 Blochez</button>
                                            @endif
                                            @if ($ms === 'paused')
                                                <button wire:click="resumeWork({{ $w->id }})" style="background:#3b82f6;color:white;border:none;padding:4px 8px;border-radius:4px;font-size:11px;cursor:pointer;">▶ Reia</button>
                                                <button wire:click="doneWork({{ $w->id }})" style="background:#10b981;color:white;border:none;padding:4px 8px;border-radius:4px;font-size:11px;cursor:pointer;">✓ Done</button>
                                            @endif
                                        </div>
                                    </div>
                                @endforeach
                            </div>
                        @endif
                    </div>
                @empty
                    <div class="mb-empty">—</div>
                @endforelse
            </div>
        @endforeach
    </div>

    {{-- BLOCK REASON MODAL --}}
    @if ($blockingWorkId)
        <div style="position:fixed; inset:0; background:rgba(0,0,0,0.5); z-index:9999; display:flex; align-items:center; justify-content:center;" wire:click="$set('blockingWorkId', null)">
            <div class="mb-modal-content" style="background:white; border-radius:12px; padding:20px; max-width:400px; width:90%;" wire:click.stop>
                <h2 style="font-size:16px; font-weight:600; margin-bottom:12px; color:#dc2626;">🔴 Blochez lucrarea</h2>
                <p style="font-size:13px; color:#4b5563; margin-bottom:14px;">Selectează motivul pentru care lucrarea nu poate continua. Va fi vizibil managerului.</p>
                <label style="font-size:11px; color:#6b7280; text-transform:uppercase; font-weight:600; display:block; margin-bottom:4px;">Motiv *</label>
                <select wire:model="blockReason" style="width:100%; padding:8px 10px; border:1px solid #cbd5e1; border-radius:6px; font-size:14px; margin-bottom:12px;">
                    @foreach (\App\Models\Tenant\WorkOrderWork::BLOCK_REASONS as $k => $v)
                        <option value="{{ $k }}">{{ $v }}</option>
                    @endforeach
                </select>
                <label style="font-size:11px; color:#6b7280; text-transform:uppercase; font-weight:600; display:block; margin-bottom:4px;">Detalii (opțional)</label>
                <textarea wire:model="blockNote" rows="2" style="width:100%; padding:8px 10px; border:1px solid #cbd5e1; border-radius:6px; font-size:14px; margin-bottom:14px;" placeholder="Ex: Lipsește filtrul Mann W712/83"></textarea>
                <div style="display:flex; gap:8px;">
                    <button wire:click="$set('blockingWorkId', null)" style="flex:1; padding:8px; background:white; color:#4b5563; border:1px solid #cbd5e1; border-radius:6px; cursor:pointer;">Anulează</button>
                    <button wire:click="confirmBlock" style="flex:1; padding:8px; background:#dc2626; color:white; border:none; border-radius:6px; cursor:pointer; font-weight:600;">Blochez</button>
                </div>
            </div>
        </div>
    @endif
</x-filament-panels::page>
