<!-- RIGHT PANEL: Main Content -->
<section class="lg:col-span-8 xl:col-span-9 flex flex-col min-h-0 relative">
    <!-- Main Card Container -->
    <div
        class="flex-1 bg-terminal-bg rounded-3xl shadow-2xl flex flex-col overflow-hidden border border-white/5 relative">

        <!-- Loading Overlay -->
        <div x-show="loading" x-transition.opacity
            class="absolute inset-0 bg-terminal-bg/50 backdrop-blur-sm z-50 flex items-center justify-center">
            <div class="flex flex-col items-center gap-3">
                <div class="w-6 h-6 border-2 border-accent-blue border-t-transparent rounded-full animate-spin">
                </div>
                <span class="text-xs font-medium text-accent-blue">Loading details...</span>
            </div>
        </div>

        <!-- Empty State (No Selection) -->
        <div x-show="!currentExecution"
            class="flex-1 flex flex-col items-center justify-center text-center p-8 opacity-50">
            <div
                class="w-24 h-24 bg-terminal-bg-light rounded-full flex items-center justify-center mb-6 shadow-inner">
                <svg class="w-10 h-10 text-terminal-text-dim" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="1.5"
                        d="M9 5H7a2 2 0 00-2 2v12a2 2 0 002 2h10a2 2 0 002-2V7a2 2 0 00-2-2h-2M9 5a2 2 0 002 2h2a2 2 0 002-2M9 5a2 2 0 012-2h2a2 2 0 012 2m-3 7h3m-3 4h3m-6-4h.01M9 16h.01">
                    </path>
                </svg>
            </div>
            <h3 class="text-lg font-medium text-terminal-text mb-1">Select a Run</h3>
            <p class="text-sm text-terminal-text-muted">Choose a workflow run from the sidebar to view its timeline
                and data.</p>
        </div>

        <!-- Content (Selection Active) -->
        <div x-show="currentExecution" x-cloak class="flex flex-col h-full">

            <!-- Header (Sticky) -->
            <div class="flex-shrink-0 px-8 py-6 border-b border-white/5 bg-terminal-bg/80 backdrop-blur z-10">
                <div class="flex items-start justify-between gap-4">
                    <div>
                        <div class="flex items-center gap-3 mb-2">
                            <h2 class="text-2xl font-bold text-terminal-text-bright"
                                x-text="currentExecution?.workflow_name.replace(/_/g, ' ').replace(/\b\w/g, l => l.toUpperCase())">
                            </h2>
                            <span class="px-3 py-1 rounded-full text-xs font-bold tracking-wide uppercase" :class="{
                                    'bg-status-success/10 text-status-success ring-1 ring-status-success/20': currentExecution?.status === 'completed',
                                    'bg-status-running/10 text-status-running ring-1 ring-status-running/20': currentExecution?.status === 'running',
                                    'bg-status-error/10 text-status-error ring-1 ring-status-error/20': currentExecution?.status === 'failed',
                                    'bg-status-pending/10 text-status-pending ring-1 ring-status-pending/20': currentExecution?.status === 'pending'
                                }" x-text="currentExecution?.status"></span>
                        </div>
                        <div class="flex items-center gap-4 text-xs text-terminal-text-muted font-mono">
                            <span class="flex items-center gap-1.5">
                                <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                        d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                                </svg>
                                <span
                                    x-text="currentExecution?.started_at ? currentExecution.started_at.split('T')[1].split('.')[0] : ''"></span>
                            </span>
                            <span class="flex items-center gap-1.5 px-2 py-0.5 rounded bg-terminal-bg-dark/50">
                                <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                        d="M7 20l4-16m2 16l4-16M6 9h14M4 15h14"></path>
                                </svg>
                                <span x-text="currentExecution?.execution_id"></span>
                            </span>
                            
                            <!-- Total Tokens Badge -->
                            <span x-show="totalRunTokens > 0" 
                                  class="flex items-center gap-1.5 px-2 py-0.5 rounded text-[10px] font-mono text-terminal-text-muted bg-terminal-bg-dark/50 border border-white/5" 
                                  title="Total Tokens Used">
                                <span x-text="totalRunTokens + ' total tokens'"></span>
                            </span>
                        </div>
                    </div>
                    <!-- Actions -->
                    <div class="flex items-center gap-2">
                        <!-- Run Again button -->
                        <button
                            x-show="['completed', 'failed'].includes(currentExecution?.status)"
                            @click="rerunWorkflow(currentExecution.workflow_name, currentExecution.inputs)"
                            class="flex items-center gap-2 px-3 py-1.5 rounded-lg bg-accent-blue/10 text-accent-blue text-xs font-bold hover:bg-accent-blue hover:text-white transition-all"
                            title="Run this workflow again with the same inputs">
                            <svg class="w-3.5 h-3.5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 4v5h.582m15.356 2A8.001 8.001 0 004.582 9m0 0H9m11 11v-5h-.581m0 0a8.003 8.003 0 01-15.357-2m15.357 2H15"></path>
                            </svg>
                            <span>Run Again</span>
                        </button>
                    </div>
                </div>
            </div>

            <!-- Scrollable Details -->
            <div class="flex-1 overflow-y-auto p-8 custom-scrollbar space-y-8">

                <!-- Inputs & Outputs Grid -->
                <div class="grid grid-cols-1 lg:grid-cols-2 gap-6">
                    <!-- Inputs -->
                    <div class="space-y-3">
                        <div class="flex justify-between items-end">
                            <h3 class="text-xs font-bold text-terminal-text-muted uppercase tracking-wider ml-1">
                                Inputs</h3>
                            <div x-data="{ copied: false }">
                                <button
                                    @click="navigator.clipboard.writeText(JSON.stringify(currentExecution?.inputs, null, 2)); copied = true; setTimeout(() => copied = false, 2000)"
                                    class="text-xs transition-colors"
                                    :class="copied ? 'text-status-success' : 'text-terminal-text-muted hover:text-accent-blue'"
                                    :title="copied ? 'Copied!' : 'Copy JSON'">
                                    <svg x-show="!copied" class="w-4 h-4" fill="none" stroke="currentColor"
                                        viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                            d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z">
                                        </path>
                                    </svg>
                                    <svg x-show="copied" x-cloak class="w-4 h-4" fill="none" stroke="currentColor"
                                        viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                            d="M5 13l4 4L19 7"></path>
                                    </svg>
                                </button>
                            </div>
                        </div>
                        <div
                            class="bg-terminal-bg-dark/50 rounded-xl p-4 border border-white/5 hover:border-white/10 transition-colors group">
                            <pre class="font-mono text-xs text-terminal-text-bright overflow-x-auto custom-scrollbar max-h-40 group-hover:text-white transition-colors"
                                x-text="JSON.stringify(currentExecution?.inputs, null, 2)"></pre>
                        </div>
                    </div>

                    <!-- Outputs -->
                    <div class="space-y-3">
                        <div class="flex justify-between items-end">
                            <h3 class="text-xs font-bold text-terminal-text-muted uppercase tracking-wider ml-1">
                                Outputs</h3>
                            <div x-show="currentExecution?.outputs" x-data="{ copied: false }">
                                <button
                                    @click="navigator.clipboard.writeText(JSON.stringify(currentExecution?.outputs, null, 2)); copied = true; setTimeout(() => copied = false, 2000)"
                                    class="text-xs transition-colors"
                                    :class="copied ? 'text-status-success' : 'text-terminal-text-muted hover:text-accent-blue'"
                                    :title="copied ? 'Copied!' : 'Copy JSON'">
                                    <svg x-show="!copied" class="w-4 h-4" fill="none" stroke="currentColor"
                                        viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                            d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z">
                                        </path>
                                    </svg>
                                    <svg x-show="copied" x-cloak class="w-4 h-4" fill="none" stroke="currentColor"
                                        viewBox="0 0 24 24">
                                        <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                            d="M5 13l4 4L19 7"></path>
                                    </svg>
                                </button>
                            </div>
                        </div>
                        <div class="rounded-xl overflow-hidden">
                            <div x-show="currentExecution?.error && !currentExecution?.outputs"
                                class="bg-status-error/10 p-4 border border-status-error/20">
                                <div class="flex gap-3">
                                    <div class="text-status-error mt-0.5">
                                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                                                d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path>
                                        </svg>
                                    </div>
                                    <div class="min-w-0">
                                        <h4 class="text-xs font-bold text-status-error mb-1">Execution Failed</h4>
                                        <pre class="text-xs font-mono text-status-error/80 whitespace-pre-wrap break-words"
                                            x-text="currentExecution?.error"></pre>
                                    </div>
                                </div>
                            </div>
                            <div x-show="currentExecution?.outputs"
                                class="bg-terminal-bg-dark/50 p-4 border border-white/5 hover:border-white/10 transition-colors">
                                <pre class="font-mono text-xs text-status-success/90 overflow-x-auto custom-scrollbar max-h-40"
                                    x-text="JSON.stringify(currentExecution?.outputs, null, 2)"></pre>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Timeline / Steps -->
                <div class="space-y-4">
                    <h3 class="text-xs font-bold text-terminal-text-muted uppercase tracking-wider ml-1">Execution
                        Timeline</h3>

                    <div class="relative space-y-4">
                        <template x-for="(step, index) in flattenedSteps" :key="step.id || index">
                            <div class="relative group transition-all duration-300"
                                 :style="'margin-left: ' + (step.depth * 24) + 'px'">
                                
                                <!-- Connector Line for nested items -->
                                <div x-show="step.depth > 0" 
                                     class="absolute -left-4 top-6 w-4 h-[2px] bg-white/10"></div>
                                <div x-show="step.depth > 0"
                                     class="absolute -left-4 -top-4 bottom-6 w-[2px] bg-white/10"></div>

                                <!-- Card -->
                                <div class="bg-terminal-bg-light rounded-xl p-5 border border-transparent hover:border-white/5 transition-all shadow-sm"
                                    :class="step.error ? 'ring-1 ring-status-error/30 bg-status-error/5' : ''">

                                    <!-- Step Header -->
                                    <div class="flex items-center justify-between mb-4">
                                        <div class="flex items-center gap-3">
                                            <!-- Toggle Button (Chevron) -->
                                            <button x-show="step.hasChildren"
                                                    @click="toggle(step.id)"
                                                    class="p-1 rounded hover:bg-white/10 transition-colors focus:outline-none text-terminal-text-muted hover:text-terminal-text-bright">
                                                <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                                                    <!-- Dynamic Path: Up if Expanded, Down if Collapsed -->
                                                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" 
                                                          :d="step.isExpanded ? 'M5 15l7-7 7 7' : 'M19 9l-7 7-7-7'"></path>
                                                </svg>
                                            </button>
                                            <!-- Placeholder removed to align titles left when no children -->

                                            <!-- Step Name & Icon -->
                                            <div class="flex items-center gap-2">
                                                <h4 class="text-sm font-bold"
                                                    :class="step.step_name !== 'call_llm' ? 'text-terminal-text-bright' : ''"
                                                    :style="step.step_name === 'call_llm' ? 'background: linear-gradient(135deg, #7aa2f7 0%, #bb9af7 100%); -webkit-background-clip: text; background-clip: text; color: transparent; filter: drop-shadow(0 2px 6px rgba(122, 162, 247, 0.4));' : ''"
                                                    x-text="step.step_name"></h4>
                                            </div>

                                            <!-- AI Badges -->
                                            <template x-if="step.step_name === 'call_llm' && !step.error">
                                                <div class="flex gap-2 items-center ml-2">
                                                    <!-- Model Badge -->
                                                    <span x-show="step.inputs?.model" 
                                                          class="px-2 py-0.5 rounded text-[10px] font-mono text-terminal-text-muted bg-terminal-bg-dark/50 border border-white/5 truncate max-w-[150px]"
                                                          x-text="step.inputs.model.replace('gemini/', '').replace('gpt-', 'GPT-')">
                                                    </span>
                                                    <!-- Token Badge (Simple) -->
                                                    <span x-show="step.outputs?.usage?.total_tokens" 
                                                          class="px-2 py-0.5 rounded text-[10px] font-mono text-terminal-text-muted bg-terminal-bg-dark/50 border border-white/5"
                                                          x-text="step.outputs.usage.total_tokens + ' tokens'">
                                                    </span>
                                                </div>
                                            </template>

                                            <span x-show="step.error"
                                                class="px-2 py-0.5 bg-status-error/20 text-status-error text-[10px] rounded font-bold uppercase">Error</span>
                                        </div>
                                        <div class="flex items-center gap-3">
                                            <span class="text-xs font-mono text-terminal-text-dim"
                                                x-text="new Date(step.timestamp).toLocaleTimeString('en-US', {hour: '2-digit', minute: '2-digit', second: '2-digit', hour12: false})"></span>
                                            <button
                                                @click="timeTravel(currentExecution.workflow_name, step.cache_key || step.step_name, step.inputs)"
                                                class="px-3 py-1.5 bg-accent-blue/10 text-accent-blue text-xs font-semibold rounded-lg hover:bg-accent-blue hover:text-white transition-all"
                                                :title="step.error ? 'Retry workflow from this failed step' : 'Rerun workflow from this step'"
                                                x-text="step.error ? '↺ Retry' : '↺ Resume'">
                                            </button>
                                        </div>
                                    </div>

                                    <!-- Step Data -->
                                    <div class="grid grid-cols-1 md:grid-cols-2 gap-4 text-xs">
                                        <div class="space-y-1.5">
                                            <div class="flex justify-between items-end">
                                                <span class="text-terminal-text-muted font-medium">Input</span>
                                                <div x-data="{ copied: false }">
                                                    <button
                                                        @click="navigator.clipboard.writeText(JSON.stringify(step.inputs, null, 2)); copied = true; setTimeout(() => copied = false, 2000)"
                                                        class="text-xs transition-colors"
                                                        :class="copied ? 'text-status-success' : 'text-terminal-text-muted hover:text-accent-blue'"
                                                        :title="copied ? 'Copied!' : 'Copy JSON'">
                                                        <svg x-show="!copied" class="w-3.5 h-3.5" fill="none"
                                                            stroke="currentColor" viewBox="0 0 24 24">
                                                            <path stroke-linecap="round" stroke-linejoin="round"
                                                                stroke-width="2"
                                                                d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z">
                                                            </path>
                                                        </svg>
                                                        <svg x-show="copied" x-cloak class="w-3.5 h-3.5" fill="none"
                                                            stroke="currentColor" viewBox="0 0 24 24">
                                                            <path stroke-linecap="round" stroke-linejoin="round"
                                                                stroke-width="2" d="M5 13l4 4L19 7"></path>
                                                        </svg>
                                                    </button>
                                                </div>
                                            </div>
                                            <div
                                                class="bg-terminal-bg-dark/50 rounded-lg p-3 font-mono text-terminal-text overflow-x-auto custom-scrollbar">
                                                <pre x-text="JSON.stringify(step.inputs, null, 2)"></pre>
                                            </div>
                                        </div>
                                        <div class="space-y-1.5">
                                            <div class="flex justify-between items-end">
                                                <span class="text-terminal-text-muted font-medium">Result</span>
                                                <!-- Copy Result Button (only if no error) -->
                                                <div x-show="!step.error" x-data="{ copied: false }">
                                                    <button
                                                        @click="navigator.clipboard.writeText(JSON.stringify(step.outputs, null, 2)); copied = true; setTimeout(() => copied = false, 2000)"
                                                        class="text-xs transition-colors"
                                                        :class="copied ? 'text-status-success' : 'text-terminal-text-muted hover:text-accent-blue'"
                                                        :title="copied ? 'Copied!' : 'Copy JSON'">
                                                        <svg x-show="!copied" class="w-3.5 h-3.5" fill="none"
                                                            stroke="currentColor" viewBox="0 0 24 24">
                                                            <path stroke-linecap="round" stroke-linejoin="round"
                                                                stroke-width="2"
                                                                d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z">
                                                            </path>
                                                        </svg>
                                                        <svg x-show="copied" x-cloak class="w-3.5 h-3.5" fill="none"
                                                            stroke="currentColor" viewBox="0 0 24 24">
                                                            <path stroke-linecap="round" stroke-linejoin="round"
                                                                stroke-width="2" d="M5 13l4 4L19 7"></path>
                                                        </svg>
                                                    </button>
                                                </div>
                                                <!-- Copy Error Button (only if error) -->
                                                <div x-show="step.error" x-data="{ copied: false }">
                                                    <button
                                                        @click="navigator.clipboard.writeText(step.error); copied = true; setTimeout(() => copied = false, 2000)"
                                                        class="text-xs transition-colors"
                                                        :class="copied ? 'text-status-success' : 'text-status-error hover:text-status-error/80'"
                                                        :title="copied ? 'Copied!' : 'Copy Error'">
                                                        <svg x-show="!copied" class="w-3.5 h-3.5" fill="none"
                                                            stroke="currentColor" viewBox="0 0 24 24">
                                                            <path stroke-linecap="round" stroke-linejoin="round"
                                                                stroke-width="2"
                                                                d="M8 16H6a2 2 0 01-2-2V6a2 2 0 012-2h8a2 2 0 012 2v2m-6 12h8a2 2 0 002-2v-8a2 2 0 00-2-2h-8a2 2 0 00-2 2v8a2 2 0 002 2z">
                                                            </path>
                                                        </svg>
                                                        <svg x-show="copied" x-cloak class="w-3.5 h-3.5" fill="none"
                                                            stroke="currentColor" viewBox="0 0 24 24">
                                                            <path stroke-linecap="round" stroke-linejoin="round"
                                                                stroke-width="2" d="M5 13l4 4L19 7"></path>
                                                        </svg>
                                                    </button>
                                                </div>
                                            </div>

                                            <!-- Output / Error Display -->
                                            <div class="rounded-lg overflow-hidden"
                                                :class="step.error ? 'bg-status-error/10 border border-status-error/20' : 'bg-terminal-bg-dark/50 border border-transparent'">

                                                <!-- Successful Output -->
                                                <pre x-show="!step.error"
                                                    class="p-3 font-mono text-terminal-text-bright overflow-x-auto custom-scrollbar"
                                                    x-text="JSON.stringify(step.outputs, null, 2)"></pre>

                                                <!-- Error Message -->
                                                <div x-show="step.error" class="p-3">
                                                    <div class="flex items-start gap-2 text-status-error">
                                                        <pre class="font-mono text-xs whitespace-pre-wrap break-words"
                                                            x-text="step.error"></pre>
                                                    </div>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </template>
                    </div>

                    <!-- No steps state -->
                    <div x-show="!currentExecution?.step_executions?.length" class="text-center py-8 opacity-50">
                        <span class="text-sm text-terminal-text-muted">No steps recorded yet.</span>
                    </div>
                </div>
            </div>
        </div>
    </div>
</section>
