<div class="space-y-6">
    <!-- Header -->
    <div class="flex flex-col sm:flex-row sm:items-start sm:justify-between gap-4">
        <div>
            <div class="flex items-center gap-3">
                <a href="{{ route('projects.tasks', $project) }}" class="text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200">
                    <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="M10 19l-7-7m0 0l7-7m-7 7h18"/>
                    </svg>
                </a>
                <h1 class="text-2xl font-bold text-gray-900 dark:text-white">{{ $task->title }}</h1>
            </div>
            <div class="flex flex-wrap items-center gap-3 mt-2">
                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium"
                      style="background-color: {{ $task->status_color }}20; color: {{ $task->status_color }}">
                    {{ $task->status_label }}
                </span>
                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium"
                      style="background-color: {{ $task->priority_color }}20; color: {{ $task->priority_color }}">
                    {{ $task->priority_label }}
                </span>
                @if($task->phase)
                    <span class="px-3 py-1 text-sm text-gray-700 dark:text-gray-300 bg-gray-100 dark:bg-gray-800 rounded-full">
                        {{ $task->phase->name }}
                    </span>
                @endif
            </div>
        </div>
        <div class="flex flex-wrap gap-2">
            @can('edit tasks', $task)
                <a href="{{ route('projects.tasks.edit', [$project, $task]) }}"
                   class="inline-flex items-center px-4 py-2 bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300 rounded-lg hover:bg-gray-200 dark:hover:bg-gray-700 transition-colors">
                    <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M11 5H6a2 2 0 00-2 2v11a2 2 0 002 2h11a2 2 0 002-2v-5m-1.414-9.414a2 2 0 112.828 2.828L11.828 15H9v-2.828l8.586-8.586z"/>
                    </svg>
                    Editar
                </a>
            @endcan
            <div class="flex items-center gap-2" x-data="{ open: false }">
                <button @click="open = !open" class="inline-flex items-center px-4 py-2 bg-gray-100 dark:bg-gray-800 text-gray-700 dark:text-gray-300 rounded-lg hover:bg-gray-200 dark:hover:bg-gray-700 transition-colors">
                    <svg class="w-5 h-5 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 6v6m0 0v6m0-6h6m-6 0H6"/>
                    </svg>
                    Acciones
                </button>
                <div x-show="open" x-transition:enter="transition ease-out duration-100" x-transition:enter-start="opacity-0 scale-95" x-transition:enter-end="opacity-100 scale-100" x-transition:leave="transition ease-in duration-75" x-transition:leave-start="opacity-100 scale-100" x-transition:leave-end="opacity-0 scale-95" @click.outside="open = false" class="absolute right-0 mt-2 w-48 bg-white dark:bg-gray-800 rounded-lg shadow-lg border border-gray-200 dark:border-gray-700 py-1 z-50">
                    @foreach($statusOptions as $value => $label)
                        <button wire:click="changeStatus('{{ $value }}')"
                                @click="open = false"
                                class="w-full text-left px-4 py-2 text-sm text-gray-700 dark:text-gray-300 hover:bg-gray-100 dark:hover:bg-gray-700 flex items-center gap-2 {{ $task->status === $value ? 'font-medium text-blue-600 dark:text-blue-400' : '' }}">
                            @if($task->status === $value)
                                <svg class="w-4 h-4" fill="currentColor" viewBox="0 0 20 20"><path fill-rule="evenodd" d="M16.707 5.293a1 1 0 010 1.414l-8 8a1 1 0 01-1.414 0l-4-4a1 1 0 011.414-1.414L8 12.586l7.293-7.293a1 1 0 011.414 0z" clip-rule="evenodd"/></svg>
                            @endif
                            {{ $label }}
                        </button>
                    @endforeach
                </div>
            </div>
        </div>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <!-- Main Content -->
        <div class="lg:col-span-2 space-y-6">
            <!-- Description -->
            <div class="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 p-6">
                <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">Descripción</h3>
                @if($task->description)
                    <div class="prose dark:prose-invert max-w-none">{{ $task->description }}</div>
                @else
                    <p class="text-gray-500 dark:text-gray-400 italic">Sin descripción</p>
                @endif
            </div>

            <!-- Subtasks -->
            <div class="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700">
                <div class="p-4 border-b border-gray-200 dark:border-gray-700 flex items-center justify-between">
                    <h3 class="text-lg font-semibold text-gray-900 dark:text-white">
                        Subtareas ({{ $task->subtasks->count() }})
                    </h3>
                    @can('create tasks', $task)
                        <div class="flex gap-2">
                            <input type="text" x-model="newSubtaskTitle" @keydown.enter="addSubtask" placeholder="Añadir subtarea..." class="px-3 py-1.5 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent dark:bg-gray-700 dark:text-white text-sm w-64">
                            <button @click="addSubtask" class="px-3 py-1.5 bg-blue-600 text-white rounded-lg hover:bg-blue-700 text-sm transition-colors">Añadir</button>
                        </div>
                    @endcan
                </div>

                @if($task->subtasks->isEmpty())
                    <div class="p-8 text-center text-gray-500 dark:text-gray-400">
                        No hay subtareas. {{ auth()->user()->can('create tasks', $task) ? 'Crea la primera arriba.' : '' }}
                    </div>
                @else
                    <div class="divide-y divide-gray-200 dark:divide-gray-700">
                        @foreach($task->subtasks as $subtask)
                            <div class="p-4 flex items-center gap-4 hover:bg-gray-50 dark:hover:bg-gray-700/50">
                                <div class="flex items-center gap-3">
                                    <select wire:model="subtaskStatuses.{{ $subtask->id }}" wire:change="updateSubtaskStatus({{ $subtask->id }}, $event.target.value)" class="text-sm border border-gray-300 dark:border-gray-600 rounded-lg px-2 py-1 dark:bg-gray-700 dark:text-white" style="width: 140px;">
                                        @foreach($statusOptions as $value => $label)
                                            <option value="{{ $value }}" {{ $subtask->status === $value ? 'selected' : '' }}>{{ $label }}</option>
                                        @endforeach
                                    </select>
                                    <span class="font-medium text-gray-900 dark:text-white">{{ $subtask->title }}</span>
                                </div>
                                <div class="flex-1"></div>
                                @can('delete tasks', $subtask)
                                    <button wire:click="deleteSubtask({{ $subtask->id }})" wire:confirm="¿Eliminar subtarea \"{{ $subtask->title }}\"?" class="text-red-500 hover:text-red-700 dark:text-red-400 dark:hover:text-red-300 p-1">
                                        <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="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"/></svg>
                                    </button>
                                @endcan
                            </div>
                        @endforeach
                    </div>
                @endif
            </div>

            <!-- Comments -->
            <div class="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700">
                <div class="p-4 border-b border-gray-200 dark:border-gray-700">
                    <h3 class="text-lg font-semibold text-gray-900 dark:text-white">Comentarios</h3>
                </div>

                <div class="p-4 border-b border-gray-200 dark:border-gray-700">
                    <textarea wire:model="commentBody" rows="2" placeholder="Escribe un comentario..." class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent dark:bg-gray-700 dark:text-white"></textarea>
                    <div class="mt-2 flex justify-end">
                        <button wire:click="addComment" class="px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors" :disabled="!commentBody.trim()">
                            Comentar
                        </button>
                    </div>
                </div>

                <div class="divide-y divide-gray-200 dark:divide-gray-700">
                    @forelse($task->comments->where('parent_id', null) as $comment)
                        <div class="p-4" x-data="{ editing: false }">
                            <div class="flex gap-3">
                                <div class="w-8 h-8 rounded-full bg-blue-100 dark:bg-blue-900 flex items-center justify-center text-sm font-medium text-blue-700 dark:text-blue-300 flex-shrink-0">
                                    {{ strtoupper($comment->user->name[0]) }}
                                </div>
                                <div class="flex-1 min-w-0">
                                    <div class="flex items-center gap-2 mb-1">
                                        <span class="font-medium text-gray-900 dark:text-white">{{ $comment->user->name }}</span>
                                        <span class="text-sm text-gray-500 dark:text-gray-400">{{ $comment->created_at->diffForHumans() }}</span>
                                        @if($comment->user_id === auth()->id() || auth()->user()->can('manage all tasks'))
                                            <button @click="editing = !editing" class="text-gray-400 hover:text-gray-600 dark:hover:text-gray-300 text-sm">Editar</button>
                                            <button wire:click="deleteComment({{ $comment->id }})" wire:confirm="¿Eliminar comentario?" class="text-gray-400 hover:text-red-600 dark:hover:text-red-400 text-sm">Eliminar</button>
                                        @endif
                                    </div>
                                    @if($editing)
                                        <textarea wire:model="editingCommentBody" x-model="editingCommentBody" rows="2" class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent dark:bg-gray-700 dark:text-white mb-2"></textarea>
                                        <div class="flex gap-2">
                                            <button wire:click="saveEditComment" class="px-3 py-1.5 bg-blue-600 text-white rounded-lg hover:bg-blue-700 text-sm transition-colors">Guardar</button>
                                            <button wire:click="cancelEditComment" @click="editing = false" class="px-3 py-1.5 bg-gray-100 dark:bg-gray-700 text-gray-700 dark:text-gray-300 rounded-lg hover:bg-gray-200 dark:hover:bg-gray-600 text-sm transition-colors">Cancelar</button>
                                        </div>
                                    @else
                                        <div class="prose prose-sm dark:prose-invert max-w-none">{{ $comment->body }}</div>
                                    @endif

                                    <!-- Replies -->
                                    @if($comment->replies->isNotEmpty())
                                        <div class="ml-8 mt-3 border-l-2 border-gray-200 dark:border-gray-700 pl-4 space-y-3">
                                            @foreach($comment->replies as $reply)
                                                <div class="flex gap-3">
                                                    <div class="w-6 h-6 rounded-full bg-gray-100 dark:bg-gray-700 flex items-center justify-center text-xs font-medium text-gray-700 dark:text-gray-300 flex-shrink-0">
                                                        {{ strtoupper($reply->user->name[0]) }}
                                                    </div>
                                                    <div class="flex-1">
                                                        <div class="flex items-center gap-2 mb-1">
                                                            <span class="font-medium text-sm text-gray-900 dark:text-white">{{ $reply->user->name }}</span>
                                                            <span class="text-xs text-gray-500 dark:text-gray-400">{{ $reply->created_at->diffForHumans() }}</span>
                                                        </div>
                                                        <div class="prose prose-sm dark:prose-invert max-w-none">{{ $reply->body }}</div>
                                                    </div>
                                                </div>
                                            @endforeach
                                        </div>
                                    @endif
                                </div>
                            </div>
                        </div>
                    @empty
                        <div class="p-8 text-center text-gray-500 dark:text-gray-400">No hay comentarios aún</div>
                    @endforelse
                </div>
            </div>
        </div>

        <!-- Sidebar -->
        <div class="lg:col-span-1 space-y-6">
            <!-- Info Card -->
            <div class="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 p-6">
                <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">Información</h3>
                <dl class="space-y-4 text-sm">
                    <div>
                        <dt class="text-gray-500 dark:text-gray-400">Proyecto</dt>
                        <dd class="font-medium text-gray-900 dark:text-white">{{ $task->project->name }}</dd>
                    </div>
                    @if($task->phase)
                    <div>
                        <dt class="text-gray-500 dark:text-gray-400">Fase</dt>
                        <dd class="font-medium text-gray-900 dark:text-white">{{ $task->phase->name }}</dd>
                    </div>
                    @endif
                    @if($task->parent_task_id)
                    <div>
                        <dt class="text-gray-500 dark:text-gray-400">Tarea padre</dt>
                        <dd class="font-medium text-gray-900 dark:text-white">
                            <a href="{{ route('projects.tasks.show', [$task->project, $task->parent]) }}" class="hover:text-blue-600 dark:hover:text-blue-400">
                                {{ $task->parent->title }}
                            </a>
                        </dd>
                    </div>
                    @endif
                    <div>
                        <dt class="text-gray-500 dark:text-gray-400">Creada por</dt>
                        <dd class="font-medium text-gray-900 dark:text-white">{{ $task->creator->name }}</dd>
                    </div>
                    <div>
                        <dt class="text-gray-500 dark:text-gray-400">Fecha creación</dt>
                        <dd class="font-medium text-gray-900 dark:text-white">{{ $task->created_at->format('d/m/Y H:i') }}</dd>
                    </div>
                    @if($task->due_date)
                    <div>
                        <dt class="text-gray-500 dark:text-gray-400">Vence</dt>
                        <dd class="font-medium {{ $task->is_overdue ? 'text-red-600 dark:text-red-400' : ($task->is_due_today ? 'text-amber-600 dark:text-amber-400' : 'text-gray-900 dark:text-white') }}">
                            {{ $task->due_date->format('d/m/Y') }}
                            @if($task->is_overdue) <span class="text-xs ml-1">(Vencida)</span> @endif
                            @if($task->is_due_today) <span class="text-xs ml-1">(Hoy)</span> @endif
                        </dd>
                    </div>
                    @endif
                    @if($task->start_date)
                    <div>
                        <dt class="text-gray-500 dark:text-gray-400">Inicio</dt>
                        <dd class="font-medium text-gray-900 dark:text-white">{{ $task->start_date->format('d/m/Y') }}</dd>
                    </div>
                    @endif
                    @if($task->completed_at)
                    <div>
                        <dt class="text-gray-500 dark:text-gray-400">Completada</dt>
                        <dd class="font-medium text-gray-900 dark:text-white">{{ $task->completed_at->format('d/m/Y H:i') }}</dd>
                    </div>
                    <div>
                        <dt class="text-gray-500 dark:text-gray-400">Completada por</dt>
                        <dd class="font-medium text-gray-900 dark:text-white">{{ $task->completer->name ?? '—' }}</dd>
                    </div>
                    @endif
                    @if($task->estimated_hours || $task->actual_hours)
                    <div>
                        <dt class="text-gray-500 dark:text-gray-400">Horas</dt>
                        <dd class="font-medium text-gray-900 dark:text-white">
                            {{ $task->estimated_hours ? $task->estimated_hours . 'h estimadas' : '' }}
                            @if($task->estimated_hours && $task->actual_hours) / @endif
                            {{ $task->actual_hours ? $task->actual_hours . 'h reales' : '' }}
                        </dd>
                    </div>
                    @endif
                </dl>
            </div>

            <!-- Assignee Card -->
            <div class="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 p-6">
                <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">Asignación</h3>
                @if($task->assignee)
                    <div class="flex items-center gap-3 mb-4">
                        <div class="w-12 h-12 rounded-full bg-blue-100 dark:bg-blue-900 flex items-center justify-center text-lg font-medium text-blue-700 dark:text-blue-300">
                            {{ strtoupper($task->assignee->name[0]) }}
                        </div>
                        <div>
                            <p class="font-medium text-gray-900 dark:text-white">{{ $task->assignee->name }}</p>
                            <p class="text-sm text-gray-500 dark:text-gray-400">{{ $task->assignee->email }}</p>
                        </div>
                    </div>
                @else
                    <p class="text-gray-500 dark:text-gray-400 mb-4">Sin asignar</p>
                @endif

                @can('assign tasks', $task)
                    <form wire:submit.prevent="updateAssignee">
                        <select wire:model="newAssigneeId" class="w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent dark:bg-gray-700 dark:text-white mb-2">
                            <option value="">Sin asignar</option>
                            @foreach($assignees as $assignee)
                                <option value="{{ $assignee->id }}" {{ $task->assigned_to === $assignee->id ? 'selected' : '' }}>{{ $assignee->name }}</option>
                            @endforeach
                        </select>
                        <button type="submit" class="w-full px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors">Asignar</button>
                    </form>
                @endcan
            </div>

            <!-- Progress Card -->
            @if($task->subtasks->count() > 0)
            <div class="bg-white dark:bg-gray-800 rounded-lg shadow-sm border border-gray-200 dark:border-gray-700 p-6">
                <h3 class="text-lg font-semibold text-gray-900 dark:text-white mb-4">Progreso</h3>
                <div class="mb-4">
                    <div class="flex justify-between text-sm mb-1">
                        <span class="text-gray-500 dark:text-gray-400">Completado</span>
                        <span class="font-medium text-gray-900 dark:text-white">{{ $task->progress }}%</span>
                    </div>
                    <div class="h-3 bg-gray-200 dark:bg-gray-700 rounded-full overflow-hidden">
                        <div class="h-full bg-blue-500 rounded-full transition-all duration-300" style="width: {{ $task->progress }}%"></div>
                    </div>
                </div>
                <div class="text-sm text-gray-500 dark:text-gray-400">
                    {{ $task->subtasks->where('status', 'completed')->count() }} de {{ $task->subtasks->count() }} subtareas completadas
                </div>
            </div>
            @endif
        </div>
    </div>
</div>

@push('scripts')
<script>
    document.addEventListener('livewire:load', () => {
        // Initialize subtask statuses
        @js($task->subtasks->pluck('status', 'id')->toArray())
    });
</script>
@endpush