<!DOCTYPE html>
<html lang="de">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>NEXUS OSINT</title>
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link href="https://fonts.googleapis.com/css2?family=Space+Mono:wght@400;700&family=Outfit:wght@200;400;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.1/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        :root {
            --bg-primary: #000000;
            --bg-secondary: #0a0a0a;
            --bg-tertiary: #1a1a1a;
            --text-primary: #ffffff;
            --text-secondary: #b0b0b0;
            --accent: #00d9ff;
            --accent-alt: #ffffff;
            --border: #333333;
            --success: #10b981;
            --error: #ef4444;
        }

        body.light-mode {
            --bg-primary: #ffffff;
            --bg-secondary: #f5f5f5;
            --bg-tertiary: #eeeeee;
            --text-primary: #000000;
            --text-secondary: #333333;
            --accent: #0066cc;
            --border: #d0d0d0;
            --success: #10b981;
            --error: #ef4444;
        }

        html {
            scroll-behavior: smooth;
        }

        body {
            font-family: 'Outfit', sans-serif;
            background: var(--bg-primary);
            color: var(--text-primary);
            overflow-x: hidden;
            position: relative;
            transition: background-color 0.3s ease, color 0.3s ease;
        }

        /* Animated background effect */
        body::before {
            content: '';
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
            background: 
                radial-gradient(circle at 20% 50%, rgba(0, 217, 255, 0.05) 0%, transparent 50%),
                radial-gradient(circle at 80% 80%, rgba(0, 217, 255, 0.05) 0%, transparent 50%);
            pointer-events: none;
            z-index: 0;
            transition: opacity 0.3s ease;
        }

        body.light-mode::before {
            background: 
                radial-gradient(circle at 20% 50%, rgba(0, 102, 204, 0.03) 0%, transparent 50%),
                radial-gradient(circle at 80% 80%, rgba(0, 102, 204, 0.03) 0%, transparent 50%);
        }

        .container {
            position: relative;
            z-index: 1;
            max-width: 900px;
            margin: 0 auto;
            padding: 60px 30px;
        }

        /* Theme Toggle Button */
        .theme-toggle {
            position: fixed;
            top: 30px;
            right: 30px;
            z-index: 100;
        }

        .theme-toggle-btn {
            background: var(--bg-secondary);
            border: 1px solid var(--border);
            color: var(--text-primary);
            width: 50px;
            height: 50px;
            border-radius: 12px;
            font-size: 20px;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            transition: all 0.3s ease;
            outline: none;
        }

        .theme-toggle-btn:hover {
            border-color: var(--accent);
            box-shadow: 0 0 15px rgba(0, 217, 255, 0.2);
            transform: translateY(-2px);
        }

        .theme-toggle-btn:active {
            transform: translateY(0);
        }

        body.light-mode .theme-toggle-btn:hover {
            box-shadow: 0 0 15px rgba(0, 102, 204, 0.2);
        }

        /* Header */
        header {
            margin-bottom: 80px;
            text-align: center;
            animation: fadeInDown 0.8s ease-out;
        }

        .logo {
            font-family: 'Space Mono', monospace;
            font-size: 14px;
            letter-spacing: 4px;
            color: var(--accent);
            text-transform: uppercase;
            margin-bottom: 30px;
            font-weight: 700;
            opacity: 0.9;
        }

        h1 {
            font-size: 4.5rem;
            font-weight: 700;
            letter-spacing: -1px;
            margin-bottom: 15px;
            background: linear-gradient(135deg, var(--accent) 0%, var(--text-primary) 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
            line-height: 1.1;
        }

        .tagline {
            font-size: 1.1rem;
            color: var(--text-secondary);
            font-weight: 300;
            letter-spacing: 0.5px;
            margin-bottom: 10px;
        }

        .status-badge {
            display: inline-flex;
            align-items: center;
            gap: 8px;
            background: rgba(0, 0, 0, 0.1);
            border: 1px solid rgba(20, 20, 20, 0.979);
            color: var(--success);
            padding: 8px 16px;
            border-radius: 20px;
            font-size: 12px;
            font-weight: 600;
            letter-spacing: 0.5px;
            margin-top: 25px;
            animation: fadeIn 1.2s ease-out;
        }

        .status-dot {
            width: 6px;
            height: 6px;
            background: var(--success);
            border-radius: 50%;
            animation: pulse 2s ease-in-out infinite;
        }

        @keyframes pulse {
            0%, 100% { opacity: 1; }
            50% { opacity: 0.4; }
        }

        /* Main Card */
        .main-card {
            background: var(--bg-secondary);
            border: 1px solid var(--border);
            border-radius: 16px;
            padding: 50px;
            animation: fadeInUp 0.8s ease-out 0.2s both;
            position: relative;
            overflow: hidden;
            transition: all 0.3s ease;
        }

        .main-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 1px;
            background: linear-gradient(90deg, transparent, var(--accent), transparent);
        }

        /* Form Styles */
        .form-wrapper {
            display: grid;
            gap: 32px;
        }

        .form-group {
            display: flex;
            flex-direction: column;
            gap: 12px;
        }

        label {
            font-size: 12px;
            font-weight: 600;
            letter-spacing: 1px;
            text-transform: uppercase;
            color: var(--text-secondary);
        }

        select, input[type="text"], input[type="url"] {
            background: var(--bg-tertiary);
            border: 1px solid var(--border);
            color: var(--text-primary);
            padding: 16px 18px;
            border-radius: 8px;
            font-family: 'Outfit', sans-serif;
            font-size: 15px;
            transition: all 0.3s ease;
            outline: none;
            -webkit-appearance: none;
            -moz-appearance: none;
            appearance: none;
            cursor: pointer;
        }

        select {
            background-image: url("data:image/svg+xml;charset=UTF-8,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23b0b0b0' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'%3e%3cpolyline points='6,9 12,15 18,9'%3e%3c/polyline%3e%3c/svg%3e");
            background-repeat: no-repeat;
            background-position: right 12px center;
            background-size: 20px;
            padding-right: 40px;
        }

        select option {
            background: var(--bg-tertiary);
            color: var(--text-primary);
            padding: 12px;
            border: none;
        }

        body.light-mode select option {
            background: var(--bg-tertiary);
            color: var(--text-primary);
        }

        select::placeholder, input::placeholder {
            color: var(--text-secondary);
        }

        select:focus, input:focus {
            border-color: var(--accent);
            background-color: var(--bg-tertiary);
            box-shadow: 0 0 0 1px var(--accent);
        }

        select:hover, input:hover {
            border-color: var(--border);
        }

        /* Button */
        .btn-submit {
            background: linear-gradient(135deg, var(--accent) 0%, #00a8cc 100%);
            color: var(--bg-primary);
            border: none;
            padding: 16px 32px;
            border-radius: 8px;
            font-size: 13px;
            font-weight: 700;
            letter-spacing: 1px;
            text-transform: uppercase;
            cursor: pointer;
            transition: all 0.3s ease;
            position: relative;
            overflow: hidden;
            margin-top: 10px;
        }

        .btn-submit::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: rgba(255, 255, 255, 0.2);
            transition: left 0.4s ease;
        }

        .btn-submit:hover:not(:disabled)::before {
            left: 100%;
        }

        .btn-submit:hover:not(:disabled) {
            transform: translateY(-2px);
            box-shadow: 0 10px 30px rgba(0, 217, 255, 0.3);
        }

        .btn-submit:active:not(:disabled) {
            transform: translateY(0);
        }

        .btn-submit:disabled {
            opacity: 0.6;
            cursor: not-allowed;
        }

        body.light-mode .btn-submit {
            background: linear-gradient(135deg, #0066cc 0%, #004499 100%);
            color: #ffffff;
        }

        body.light-mode .btn-submit:hover:not(:disabled) {
            box-shadow: 0 10px 30px rgba(0, 102, 204, 0.3);
        }

        /* Results Section */
        .results {
            margin-top: 50px;
            animation: fadeInUp 0.6s ease-out;
        }

        .loading-container {
            text-align: center;
            padding: 60px 30px;
        }

        .spinner {
            width: 48px;
            height: 48px;
            margin: 0 auto 25px;
            border: 2px solid var(--border);
            border-top-color: var(--accent);
            border-radius: 50%;
            animation: spin 0.8s linear infinite;
        }

        @keyframes spin {
            to { transform: rotate(360deg); }
        }

        .loading-text {
            color: var(--text-secondary);
            font-size: 14px;
            animation: pulse-text 1.5s ease-in-out infinite;
        }

        @keyframes pulse-text {
            0%, 100% { opacity: 1; }
            50% { opacity: 0.6; }
        }

        /* Result Cards */
        .result-card {
            background: var(--bg-secondary);
            border: 1px solid var(--border);
            border-radius: 16px;
            padding: 40px;
            margin-bottom: 24px;
            animation: slideIn 0.5s ease-out;
            transition: all 0.3s ease;
        }

        @keyframes slideIn {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        .result-card.error {
            border-color: rgba(239, 68, 68, 0.3);
            background: rgba(239, 68, 68, 0.05);
        }

        .result-card.success {
            border-color: rgba(16, 185, 129, 0.3);
            background: rgba(16, 185, 129, 0.05);
        }

        .result-header {
            display: flex;
            align-items: center;
            gap: 12px;
            margin-bottom: 25px;
            flex-wrap: wrap;
        }

        .result-icon {
            font-size: 24px;
        }

        .result-title {
            font-size: 18px;
            font-weight: 700;
            letter-spacing: 0.5px;
            flex: 1;
            min-width: 150px;
        }

        .button-group {
            display: flex;
            gap: 12px;
            flex-shrink: 0;
            margin-left: auto;
        }

        .download-btn, .copy-btn {
            background: var(--accent);
            border: 2px solid var(--accent);
            color: var(--bg-primary);
            padding: 12px 20px;
            border-radius: 8px;
            font-size: 14px;
            font-weight: 600;
            cursor: pointer;
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 8px;
            transition: all 0.3s ease;
            outline: none;
            flex-shrink: 0;
            box-shadow: 0 4px 15px rgba(0, 217, 255, 0.25);
            white-space: nowrap;
        }

        .download-btn:hover, .copy-btn:hover {
            background: transparent;
            color: var(--accent);
            transform: translateY(-2px);
            box-shadow: 0 6px 20px rgba(0, 217, 255, 0.4);
        }

        .download-btn:active, .copy-btn:active {
            transform: translateY(0);
        }

        .copy-btn.copied {
            background: var(--success);
            border-color: var(--success);
            color: var(--bg-primary);
            box-shadow: 0 4px 15px rgba(16, 185, 129, 0.25);
        }

        .copy-btn.copied:hover {
            background: var(--success);
            color: var(--bg-primary);
        }

        body.light-mode .download-btn, 
        body.light-mode .copy-btn {
            background: var(--accent);
            border-color: var(--accent);
            color: white;
            box-shadow: 0 4px 15px rgba(0, 102, 204, 0.25);
        }

        body.light-mode .download-btn:hover, 
        body.light-mode .copy-btn:hover {
            background: transparent;
            color: var(--accent);
            box-shadow: 0 6px 20px rgba(0, 102, 204, 0.4);
        }

        .result-meta {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
            gap: 20px;
            margin-bottom: 30px;
            padding-bottom: 30px;
            border-bottom: 1px solid var(--border);
        }

        .meta-item {
            display: flex;
            flex-direction: column;
            gap: 6px;
        }

        .meta-label {
            font-size: 11px;
            font-weight: 600;
            letter-spacing: 0.5px;
            text-transform: uppercase;
            color: var(--text-secondary);
        }

        .meta-value {
            font-family: 'Space Mono', monospace;
            font-size: 14px;
            color: var(--text-primary);
            word-break: break-all;
        }

        .result-section {
            margin-bottom: 30px;
        }

        .result-section:last-child {
            margin-bottom: 0;
        }

        .section-title {
            font-size: 13px;
            font-weight: 700;
            letter-spacing: 1px;
            text-transform: uppercase;
            color: var(--accent);
            margin-bottom: 15px;
        }

        .data-display {
            background: var(--bg-primary);
            border: 1px solid var(--border);
            border-radius: 8px;
            padding: 20px;
            overflow-x: auto;
            font-family: 'Space Mono', monospace;
            font-size: 12px;
            line-height: 1.6;
            color: var(--text-secondary);
            max-height: 400px;
            overflow-y: auto;
            transition: all 0.3s ease;
        }

        .data-display::-webkit-scrollbar {
            width: 8px;
            height: 8px;
        }

        .data-display::-webkit-scrollbar-track {
            background: var(--bg-primary);
        }

        .data-display::-webkit-scrollbar-thumb {
            background: var(--border);
            border-radius: 4px;
        }

        .data-display::-webkit-scrollbar-thumb:hover {
            background: var(--accent);
        }

        .toast {
            position: fixed;
            bottom: 30px;
            right: 30px;
            background: var(--bg-secondary);
            border: 1px solid var(--border);
            color: var(--text-primary);
            padding: 15px 24px;
            border-radius: 8px;
            font-size: 14px;
            animation: slideInRight 0.3s ease-out;
            z-index: 200;
            box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
        }

        .toast.success {
            border-color: rgba(16, 185, 129, 0.3);
            background: rgba(16, 185, 129, 0.1);
            color: var(--success);
        }

        .toast.error {
            border-color: rgba(239, 68, 68, 0.3);
            background: rgba(239, 68, 68, 0.1);
            color: var(--error);
        }

        @keyframes slideInRight {
            from {
                opacity: 0;
                transform: translateX(100px);
            }
            to {
                opacity: 1;
                transform: translateX(0);
            }
        }

        /* Animations */
        @keyframes fadeInDown {
            from {
                opacity: 0;
                transform: translateY(-20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        @keyframes fadeInUp {
            from {
                opacity: 0;
                transform: translateY(20px);
            }
            to {
                opacity: 1;
                transform: translateY(0);
            }
        }

        @keyframes fadeIn {
            from {
                opacity: 0;
            }
            to {
                opacity: 1;
            }
        }

        /* Responsive */
        @media (max-width: 768px) {
            .container {
                padding: 40px 20px;
                padding-top: 100px;
            }

            h1 {
                font-size: 2.8rem;
            }

            .main-card {
                padding: 30px;
            }

            .result-meta {
                grid-template-columns: 1fr;
            }

            .result-header {
                flex-direction: column;
                align-items: flex-start;
            }

            .button-group {
                width: 100%;
                justify-content: flex-start;
            }

            .theme-toggle {
                top: 20px;
                right: 20px;
            }

            .toast {
                bottom: 20px;
                right: 20px;
                left: 20px;
            }
        }

        @media (max-width: 480px) {
            h1 {
                font-size: 2rem;
            }

            .main-card {
                padding: 20px;
            }

            .form-group {
                gap: 8px;
            }

            .theme-toggle-btn {
                width: 44px;
                height: 44px;
                font-size: 18px;
            }

            .button-group {
                width: 100%;
            }

            .download-btn, .copy-btn {
                flex: 1;
                width: auto;
            }
        }
    </style>
</head>
<body>
    <div class="theme-toggle">
        <button class="theme-toggle-btn" id="themeToggle" title="Toggle Dark/Light Mode">
            <i class="fa-solid fa-moon"></i>
        </button>
    </div>

    <div class="container">
        <header>
            <div class="logo">⚡ NEXUS</div>
            <h1>OSINT</h1>
            <p class="tagline">Intelligence Gathering Platform</p>
            <div class="status-badge">
                <div class="status-dot"></div>
                All Systems Active
            </div>
        </header>

        <div class="main-card">
            <form class="form-wrapper" id="investigationForm">
                <div class="form-group">
                    <label for="investigationType">Investigation Type</label>
                    <select id="investigationType" required>
                        <option value="">Select investigation type...</option>
                        <option value="username">Username Search</option>
                        <option value="email">Email Investigation</option>
                        <option value="domain">Domain Analysis</option>
                        <option value="ip">IP Analysis</option>
                        <option value="scrape">Web Scraping</option>
                    </select>
                </div>

                <div class="form-group">
                    <label for="target">Target</label>
                    <input 
                        type="text" 
                        id="target" 
                        placeholder="username, email, domain, IP, or URL..." 
                        required
                    >
                </div>

                <button type="submit" class="btn-submit" id="investigateBtn">
                    Launch Investigation
                </button>
            </form>

            <div class="results" id="results"></div>
        </div>
    </div>

    <script>
        const form = document.getElementById('investigationForm');
        const resultsDiv = document.getElementById('results');
        const investigateBtn = document.getElementById('investigateBtn');
        const themeToggle = document.getElementById('themeToggle');
        const body = document.body;

        // Store current investigation results globally
        let currentResults = null;

        // Dark Mode Toggle
        function initTheme() {
            const savedTheme = localStorage.getItem('theme') || 'dark';
            if (savedTheme === 'light') {
                body.classList.add('light-mode');
                themeToggle.innerHTML = '<i class="fa-solid fa-sun"></i>';
            } else {
                body.classList.remove('light-mode');
                themeToggle.innerHTML = '<i class="fa-solid fa-moon"></i>';
            }
        }

        themeToggle.addEventListener('click', () => {
            body.classList.toggle('light-mode');
            const isLightMode = body.classList.contains('light-mode');
            localStorage.setItem('theme', isLightMode ? 'light' : 'dark');
            themeToggle.innerHTML = isLightMode ? '<i class="fa-solid fa-sun"></i>' : '<i class="fa-solid fa-moon"></i>';
        });

        // Initialize theme on page load
        initTheme();

        // Toast Notification
        function showToast(message, type = 'success') {
            const toast = document.createElement('div');
            toast.className = `toast ${type}`;
            toast.textContent = message;
            document.body.appendChild(toast);

            setTimeout(() => {
                toast.style.animation = 'slideInRight 0.3s ease-out reverse';
                setTimeout(() => toast.remove(), 300);
            }, 3000);
        }

        // Download Results as JSON
        function downloadResults() {
            if (!currentResults) {
                showToast('No results to download', 'error');
                return;
            }

            const downloadData = {
                investigation: {
                    type: currentResults.type,
                    target: currentResults.target,
                    timestamp: currentResults.timestamp,
                    summary: currentResults.summary
                },
                data: currentResults.data,
                metadata: {
                    tool: 'NEXUS OSINT',
                    version: '2.0.0',
                    downloaded_at: new Date().toISOString()
                }
            };

            const jsonString = JSON.stringify(downloadData, null, 2);
            const blob = new Blob([jsonString], { type: 'application/json' });
            const url = URL.createObjectURL(blob);
            const link = document.createElement('a');
            link.href = url;

            const safeTarget = currentResults.target.replace(/[^a-zA-Z0-9]/g, '_');
            const timestamp = new Date().toISOString().slice(0, 19).replace(/:/g, '-');
            link.download = `nexus_${currentResults.type}_${safeTarget}_${timestamp}.json`;

            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link);
            URL.revokeObjectURL(url);

            showToast('Results downloaded successfully!', 'success');
        }

        // Copy Results to Clipboard
        function copyToClipboard() {
            if (!currentResults) {
                showToast('No results to copy', 'error');
                return;
            }

            const downloadData = {
                investigation: {
                    type: currentResults.type,
                    target: currentResults.target,
                    timestamp: currentResults.timestamp,
                    summary: currentResults.summary
                },
                data: currentResults.data,
                metadata: {
                    tool: 'NEXUS OSINT',
                    version: '2.0.0',
                    copied_at: new Date().toISOString()
                }
            };

            const jsonString = JSON.stringify(downloadData, null, 2);
            
            navigator.clipboard.writeText(jsonString).then(() => {
                const copyBtn = document.querySelector('.copy-btn');
                const icon = copyBtn.innerHTML;
                copyBtn.innerHTML = '<i class="fa-solid fa-check"></i>';
                copyBtn.classList.add('copied');
                
                showToast('Results copied to clipboard!', 'success');
                
                setTimeout(() => {
                    copyBtn.innerHTML = icon;
                    copyBtn.classList.remove('copied');
                }, 2000);
            }).catch(() => {
                showToast('Failed to copy to clipboard', 'error');
            });
        }

        // Suppress browser extension errors
        window.addEventListener('error', function(e) {
            if (e.filename && e.filename.includes('vendor.js') || 
                e.message && e.message.includes('tabs:outgoing.message.ready')) {
                e.preventDefault();
                return false;
            }
        });

        // Check server status on load (optional - only if backend available)
        fetch('/api/status', { signal: AbortSignal.timeout(2000) })
            .then(response => {
                if (!response.ok) throw new Error('Server not available');
                return response.json();
            })
            .then(data => {
                if (data.status === 'running') {
                    console.log('✅ All modules active and ready');
                }
            })
            .catch(error => {
                // Silently fail - backend may not be available yet
                console.log('ℹ️ Backend API not available - frontend demo mode');
            });

        // Form submission
        form.addEventListener('submit', async (e) => {
            e.preventDefault();

            const type = document.getElementById('investigationType').value;
            const target = document.getElementById('target').value;

            if (!type || !target) {
                showToast('Please fill in all fields', 'error');
                return;
            }

            investigateBtn.disabled = true;
            investigateBtn.textContent = 'Investigating...';
            resultsDiv.innerHTML = `
                <div class="loading-container">
                    <div class="spinner"></div>
                    <p class="loading-text">Investigating ${type}...</p>
                </div>
            `;

            try {
                const response = await fetch('/api/investigate', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ type, target }),
                    signal: AbortSignal.timeout(10000)
                });

                // Check if response is valid JSON
                const contentType = response.headers.get('content-type');
                if (!contentType || !contentType.includes('application/json')) {
                    throw new Error('Server returned invalid response. Backend may not be available.');
                }

                const data = await response.json();

                if (data.success) {
                    currentResults = data.results;
                    displayResults(data.results);
                    showToast('Investigation completed!', 'success');
                } else {
                    throw new Error(data.error || 'Investigation failed');
                }
            } catch (error) {
                showError(error.message);
                showToast(error.message, 'error');
            } finally {
                investigateBtn.disabled = false;
                investigateBtn.textContent = 'Launch Investigation';
            }
        });

        function displayResults(results) {
            const html = `
                <div class="result-card success">
                    <div class="result-header">
                        <div class="result-icon">✓</div>
                        <div class="result-title">Investigation Complete</div>
                        <div class="button-group">
                            <button class="copy-btn" onclick="copyToClipboard()" title="Copy JSON to clipboard">
                                <i class="fa-solid fa-copy"></i>
                                <span>Copy</span>
                            </button>
                            <button class="download-btn" onclick="downloadResults()" title="Download as JSON">
                                <i class="fa-solid fa-download"></i>
                                <span>Download</span>
                            </button>
                        </div>
                    </div>

                    <div class="result-meta">
                        <div class="meta-item">
                            <div class="meta-label">Type</div>
                            <div class="meta-value">${escapeHtml(results.type)}</div>
                        </div>
                        <div class="meta-item">
                            <div class="meta-label">Target</div>
                            <div class="meta-value">${escapeHtml(results.target)}</div>
                        </div>
                        <div class="meta-item">
                            <div class="meta-label">Timestamp</div>
                            <div class="meta-value">${new Date(results.timestamp).toLocaleString()}</div>
                        </div>
                    </div>

                    ${results.summary ? `
                        <div class="result-section">
                            <div class="section-title">Summary</div>
                            <div class="data-display"><pre>${escapeHtml(formatJSON(results.summary))}</pre></div>
                        </div>
                    ` : ''}

                    <div class="result-section">
                        <div class="section-title">Detailed Data</div>
                        <div class="data-display"><pre>${escapeHtml(formatJSON(results.data))}</pre></div>
                    </div>
                </div>
            `;

            resultsDiv.innerHTML = html;
        }

        function showError(message) {
            const html = `
                <div class="result-card error">
                    <div class="result-header">
                        <div class="result-icon">✕</div>
                        <div class="result-title">Error</div>
                    </div>
                    <p style="color: var(--text-secondary);">${escapeHtml(message)}</p>
                </div>
            `;
            resultsDiv.innerHTML = html;
        }

        function formatJSON(obj) {
            return JSON.stringify(obj, null, 2);
        }

        function escapeHtml(text) {
            const map = {
                '&': '&amp;',
                '<': '&lt;',
                '>': '&gt;',
                '"': '&quot;',
                "'": '&#039;'
            };
            return text.replace(/[&<>"']/g, m => map[m]);
        }
    </script>
</body>
</html>