<x-filament-widgets::widget>
    <div wire:poll.5s>
        <style>
            .cf-container {
                font-family: inherit;
            }
            .cf-header {
                display: flex;
                justify-content: space-between;
                align-items: center;
                border-bottom: 1px solid rgba(0, 0, 0, 0.05);
                padding-bottom: 16px;
                margin-bottom: 24px;
            }
            .dark .cf-header {
                border-bottom: 1px solid rgba(255, 255, 255, 0.08);
            }
            .cf-title {
                font-size: 1.125rem;
                font-weight: 700;
                color: #111827;
            }
            .dark .cf-title {
                color: #f3f4f6;
            }
            .cf-desc {
                font-size: 0.875rem;
                color: #6b7280;
                margin-top: 2px;
            }
            .dark .cf-desc {
                color: #9ca3af;
            }
            .cf-badge {
                font-size: 0.75rem;
                font-weight: 600;
                padding: 4px 12px;
                border-radius: 9999px;
                background-color: rgba(99, 102, 241, 0.1);
                color: #4f46e5;
            }
            .dark .cf-badge {
                background-color: rgba(99, 102, 241, 0.2);
                color: #818cf8;
            }

            /* Timeline Flow Layout */
            .timeline-wrapper {
                position: relative;
                margin-top: 24px;
            }
            .timeline-line {
                position: absolute;
                left: 10px;
                top: 12px;
                bottom: 12px;
                width: 2px;
                background-color: #e5e7eb;
                z-index: 1;
            }
            .dark .timeline-line {
                background-color: #374151;
            }
            .timeline-steps {
                display: flex;
                flex-direction: column;
                gap: 24px;
                position: relative;
                z-index: 2;
            }
            .timeline-step {
                display: flex;
                align-items: flex-start;
                gap: 16px;
            }
            .timeline-node {
                width: 22px;
                height: 22px;
                border-radius: 50%;
                background-color: #ffffff;
                border: 3px solid #3b82f6;
                display: flex;
                align-items: center;
                justify-content: center;
                flex-shrink: 0;
                box-sizing: border-box;
            }
            .dark .timeline-node {
                background-color: #111827;
            }
            
            .node-sessions { border-color: #3b82f6; box-shadow: 0 0 8px rgba(59, 130, 246, 0.3); }
            .node-views { border-color: #6366f1; box-shadow: 0 0 8px rgba(99, 102, 241, 0.3); }
            .node-cart { border-color: #a855f7; box-shadow: 0 0 8px rgba(168, 85, 247, 0.3); }
            .node-checkout { border-color: #ec4899; box-shadow: 0 0 8px rgba(236, 72, 153, 0.3); }
            .node-purchase { border-color: #10b981; box-shadow: 0 0 8px rgba(16, 185, 129, 0.3); }

            .timeline-node-inner {
                width: 6px;
                height: 6px;
                border-radius: 50%;
            }
            .node-inner-sessions { background-color: #3b82f6; }
            .node-inner-views { background-color: #6366f1; }
            .node-inner-cart { background-color: #a855f7; }
            .node-inner-checkout { background-color: #ec4899; }
            .node-inner-purchase { background-color: #10b981; }

            .timeline-content {
                flex: 1;
                display: flex;
                justify-content: space-between;
                align-items: center;
                gap: 24px;
                padding-top: 2px;
            }
            @media (max-width: 640px) {
                .timeline-content {
                    flex-direction: column;
                    align-items: flex-start;
                    gap: 8px;
                }
            }

            .step-info {
                flex: 1;
            }
            .step-title {
                font-size: 0.9rem;
                font-weight: 700;
                color: #1f2937;
            }
            .dark .step-title {
                color: #f3f4f6;
            }
            .step-desc {
                font-size: 0.75rem;
                color: #6b7280;
                margin-top: 2px;
            }
            .dark .step-desc {
                color: #9ca3af;
            }

            .step-stats {
                display: flex;
                flex-direction: column;
                align-items: flex-end;
                min-width: 160px;
            }
            @media (max-width: 640px) {
                .step-stats {
                    align-items: flex-start;
                    min-width: 0;
                    width: 100%;
                }
            }
            .step-metrics {
                display: flex;
                align-items: baseline;
                gap: 6px;
            }
            .step-count {
                font-size: 1rem;
                font-weight: 800;
                color: #111827;
            }
            .dark .step-count {
                color: #f9fafb;
            }
            .step-pct {
                font-size: 0.75rem;
                font-weight: 700;
            }
            .cf-pct-primary { color: #3b82f6; }
            .cf-pct-indigo { color: #6366f1; }
            .cf-pct-purple { color: #a855f7; }
            .cf-pct-pink { color: #ec4899; }
            .cf-pct-emerald { color: #10b981; }

            .progress-track {
                width: 120px;
                height: 5px;
                background-color: #e5e7eb;
                border-radius: 9999px;
                overflow: hidden;
                margin-top: 4px;
            }
            .dark .progress-track {
                background-color: #374151;
            }
            .progress-fill {
                height: 100%;
                border-radius: 9999px;
                transition: width 0.8s ease-in-out;
            }
            .fill-sessions { background: linear-gradient(90deg, #3b82f6, #60a5fa); }
            .fill-views { background: linear-gradient(90deg, #6366f1, #818cf8); }
            .fill-cart { background: linear-gradient(90deg, #a855f7, #c084fc); }
            .fill-checkout { background: linear-gradient(90deg, #ec4899, #f472b6); }
            .fill-purchase { background: linear-gradient(90deg, #10b981, #34d399); }

            .drop-badge {
                font-size: 0.7rem;
                font-weight: 600;
                margin-top: 4px;
            }
            .drop-down { color: #ef4444; }
            .drop-ok { color: #10b981; }

            .cf-footer {
                margin-top: 28px;
                display: grid;
                grid-template-cols: repeat(2, minmax(0, 1fr));
                gap: 16px;
                border-top: 1px solid rgba(0, 0, 0, 0.05);
                padding-top: 20px;
            }
            .dark .cf-footer {
                border-top-color: rgba(255, 255, 255, 0.08);
            }
            .cf-footer-card {
                background-color: #f9fafb;
                padding: 16px;
                border-radius: 12px;
                text-align: center;
                border: 1px solid rgba(0, 0, 0, 0.02);
            }
            .dark .cf-footer-card {
                background-color: rgba(31, 41, 55, 0.3);
                border-color: rgba(255, 255, 255, 0.03);
            }
            .cf-footer-card-label {
                font-size: 0.75rem;
                font-weight: 500;
                color: #6b7280;
                display: block;
                margin-bottom: 6px;
                text-transform: uppercase;
                letter-spacing: 0.05em;
            }
            .dark .cf-footer-card-label {
                color: #9ca3af;
            }
            .cf-footer-card-val {
                font-size: 1.25rem;
                font-weight: 800;
                color: #1f2937;
            }
            .dark .cf-footer-card-val {
                color: #f9fafb;
            }
        </style>

        <div class="cf-container">
            <div class="cf-header">
                <div>
                    <h3 class="cf-title">Storefront Conversion Funnel</h3>
                    <p class="cf-desc">Customer lifecycle journey (Last 30 Days)</p>
                </div>
                <div class="cf-badge">
                    Live Session Logs
                </div>
            </div>

            <!-- Timeline Flow -->
            <div class="timeline-wrapper">
                <div class="timeline-line"></div>
                <div class="timeline-steps">
                    <!-- Step 1: Sessions -->
                    <div class="timeline-step">
                        <div class="timeline-node node-sessions">
                            <div class="timeline-node-inner node-inner-sessions"></div>
                        </div>
                        <div class="timeline-content">
                            <div class="step-info">
                                <span class="step-title">Sessions / Visits</span>
                                <p class="step-desc">Total unique visitor storefront entries</p>
                            </div>
                            <div class="step-stats">
                                <div class="step-metrics">
                                    <span class="step-count">{{ number_format($totalSessions) }}</span>
                                    <span class="step-pct cf-pct-primary">(100%)</span>
                                </div>
                                <div class="progress-track">
                                    <div class="progress-fill fill-sessions" style="width: 100%"></div>
                                </div>
                                <span class="drop-badge drop-ok">Baseline</span>
                            </div>
                        </div>
                    </div>

                    <!-- Step 2: Product Views -->
                    <div class="timeline-step">
                        <div class="timeline-node node-views">
                            <div class="timeline-node-inner node-inner-views"></div>
                        </div>
                        <div class="timeline-content">
                            <div class="step-info">
                                <span class="step-title">Product Views (ViewContent)</span>
                                <p class="step-desc">Visitors who browsed at least one product page</p>
                            </div>
                            <div class="step-stats">
                                <div class="step-metrics">
                                    <span class="step-count">{{ number_format($productViews) }}</span>
                                    <span class="step-pct cf-pct-indigo">({{ $viewRate }}%)</span>
                                </div>
                                <div class="progress-track">
                                    <div class="progress-fill fill-views" style="width: {{ $viewRate }}%"></div>
                                </div>
                                @php
                                    $viewsDrop = $totalSessions > 0 ? (1 - ($productViews / $totalSessions)) * 100 : 0;
                                @endphp
                                <span class="drop-badge {{ $viewsDrop > 0 ? 'drop-down' : 'drop-ok' }}">
                                    {{ $viewsDrop > 0 ? '↓ ' . number_format($viewsDrop, 0) . '% drop-off' : '0% drop-off' }}
                                </span>
                            </div>
                        </div>
                    </div>

                    <!-- Step 3: Add to Cart -->
                    <div class="timeline-step">
                        <div class="timeline-node node-cart">
                            <div class="timeline-node-inner node-inner-cart"></div>
                        </div>
                        <div class="timeline-content">
                            <div class="step-info">
                                <span class="step-title">Added to Cart (AddToCart)</span>
                                <p class="step-desc">Visitors who added items to their shopping cart</p>
                            </div>
                            <div class="step-stats">
                                <div class="step-metrics">
                                    <span class="step-count">{{ number_format($addToCarts) }}</span>
                                    <span class="step-pct cf-pct-purple">({{ $cartRate }}%)</span>
                                </div>
                                <div class="progress-track">
                                    <div class="progress-fill fill-cart" style="width: {{ $cartRate }}%"></div>
                                </div>
                                @php
                                    $cartDrop = $productViews > 0 ? (1 - ($addToCarts / $productViews)) * 100 : 0;
                                @endphp
                                <span class="drop-badge {{ $cartDrop > 0 ? 'drop-down' : 'drop-ok' }}">
                                    {{ $cartDrop > 0 ? '↓ ' . number_format($cartDrop, 0) . '% drop-off' : '0% drop-off' }}
                                </span>
                            </div>
                        </div>
                    </div>

                    <!-- Step 4: Checkout -->
                    <div class="timeline-step">
                        <div class="timeline-node node-checkout">
                            <div class="timeline-node-inner node-inner-checkout"></div>
                        </div>
                        <div class="timeline-content">
                            <div class="step-info">
                                <span class="step-title">Initiated Checkout (BeginCheckout)</span>
                                <p class="step-desc">Visitors who entered details on the checkout page</p>
                            </div>
                            <div class="step-stats">
                                <div class="step-metrics">
                                    <span class="step-count">{{ number_format($checkouts) }}</span>
                                    <span class="step-pct cf-pct-pink">({{ $checkoutRate }}%)</span>
                                </div>
                                <div class="progress-track">
                                    <div class="progress-fill fill-checkout" style="width: {{ $checkoutRate }}%"></div>
                                </div>
                                @php
                                    $checkoutDrop = $addToCarts > 0 ? (1 - ($checkouts / $addToCarts)) * 100 : 0;
                                @endphp
                                <span class="drop-badge {{ $checkoutDrop > 0 ? 'drop-down' : 'drop-ok' }}">
                                    {{ $checkoutDrop > 0 ? '↓ ' . number_format($checkoutDrop, 0) . '% drop-off' : '0% drop-off' }}
                                </span>
                            </div>
                        </div>
                    </div>

                    <!-- Step 5: Purchases -->
                    <div class="timeline-step">
                        <div class="timeline-node node-purchase">
                            <div class="timeline-node-inner node-inner-purchase"></div>
                        </div>
                        <div class="timeline-content">
                            <div class="step-info">
                                <span class="step-title">Completed Purchases (Purchase)</span>
                                <p class="step-desc">Successful transactions and order conversions</p>
                            </div>
                            <div class="step-stats">
                                <div class="step-metrics">
                                    <span class="step-count">{{ number_format($purchases) }}</span>
                                    <span class="step-pct cf-pct-emerald">({{ $purchaseRate }}%)</span>
                                </div>
                                <div class="progress-track">
                                    <div class="progress-fill fill-purchase" style="width: {{ $purchaseRate }}%"></div>
                                </div>
                                @php
                                    $purchaseDrop = $checkouts > 0 ? (1 - ($purchases / $checkouts)) * 100 : 0;
                                @endphp
                                <span class="drop-badge {{ $purchaseDrop > 0 ? 'drop-down' : 'drop-ok' }}">
                                    {{ $purchaseDrop > 0 ? '↓ ' . number_format($purchaseDrop, 0) . '% drop-off' : '0% drop-off' }}
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>

            <div class="cf-footer">
                <div class="cf-footer-card">
                    <span class="cf-footer-card-label">Cart-to-Detail Rate</span>
                    <span class="cf-footer-card-val">
                        {{ $productViews > 0 ? number_format(($addToCarts / $productViews) * 100, 1) : 0 }}%
                    </span>
                </div>
                <div class="cf-footer-card">
                    <span class="cf-footer-card-label">Purchase-to-Cart Rate</span>
                    <span class="cf-footer-card-val">
                        {{ $addToCarts > 0 ? number_format(($purchases / $addToCarts) * 100, 1) : 0 }}%
                    </span>
                </div>
            </div>
        </div>
    </div>
</x-filament-widgets::widget>
