<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Deye Inverter Monitor</title>
    <style>
        :root {
            --bg-color: #1a1a1a;
            --card-bg: #2d2d2d;
            --text-color: #e0e0e0;
            --accent-color: #00d2ff;
            --secondary-accent: #3a7bd5;
            --font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background-color: var(--bg-color);
            color: var(--text-color);
            font-family: var(--font-family);
            margin: 0;
            padding: 20px;
            display: flex;
            flex-direction: column;
            align-items: center;
            min-height: 100vh;
        }

        h1 {
            margin-bottom: 40px;
            font-weight: 300;
            letter-spacing: 2px;
            text-transform: uppercase;
            background: linear-gradient(45deg, var(--accent-color), var(--secondary-accent));
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
        }

        .dashboard {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            width: 100%;
            max-width: 1000px;
        }

        .card {
            background-color: var(--card-bg);
            border-radius: 15px;
            padding: 25px;
            text-align: center;
            box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
            position: relative;
            overflow: hidden;
        }

        .card:hover {
            transform: translateY(-5px);
            box-shadow: 0 8px 25px rgba(0, 0, 0, 0.4);
        }

        .card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 4px;
            background: linear-gradient(90deg, var(--accent-color), var(--secondary-accent));
        }

        .card h2 {
            margin: 0 0 15px 0;
            font-size: 1.2rem;
            color: #aaa;
            font-weight: 400;
        }

        .value {
            font-size: 2.5rem;
            font-weight: 700;
            color: #fff;
        }

        .unit {
            font-size: 1rem;
            color: #888;
            margin-left: 5px;
        }

        .status-bar {
            margin-top: 40px;
            padding: 10px 20px;
            border-radius: 20px;
            background-color: var(--card-bg);
            font-size: 0.9rem;
            color: #888;
        }

        .status-indicator {
            display: inline-block;
            width: 10px;
            height: 10px;
            border-radius: 50%;
            background-color: #555;
            margin-right: 8px;
        }

        .status-indicator.active {
            background-color: #00ff88;
            box-shadow: 0 0 10px #00ff88;
        }

        .status-indicator.error {
            background-color: #ff4444;
            box-shadow: 0 0 10px #ff4444;
        }

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

        .loading .value {
            animation: pulse 1.5s infinite;
            color: #666;
        }
    </style>
</head>
<body>

    <h1>Deye Inverter Monitor</h1>

    <div class="dashboard">
        <div class="card" id="card-input">
            <h2>Grid Voltage (Input)</h2>
            <div class="value-container loading">
                <span class="value" id="input-voltage">--</span><span class="unit">V</span>
            </div>
        </div>

        <div class="card" id="card-output">
            <h2>Output Voltage</h2>
            <div class="value-container loading">
                <span class="value" id="output-voltage">--</span><span class="unit">V</span>
            </div>
        </div>

        <div class="card" id="card-load">
            <h2>Load Voltage</h2>
            <div class="value-container loading">
                <span class="value" id="load-voltage">--</span><span class="unit">V</span>
            </div>
        </div>
    </div>

    <div class="status-bar">
        <span class="status-indicator" id="status-dot"></span>
        <span id="status-text">Connecting...</span>
    </div>

    <script>
        const inputVoltageEl = document.getElementById('input-voltage');
        const outputVoltageEl = document.getElementById('output-voltage');
        const loadVoltageEl = document.getElementById('load-voltage');
        const statusDot = document.getElementById('status-dot');
        const statusText = document.getElementById('status-text');
        const valueContainers = document.querySelectorAll('.value-container');

        async function fetchData() {
            try {
                const response = await fetch('/api/data');
                const data = await response.json();

                if (data.status === 'success') {
                    inputVoltageEl.textContent = data.input_voltage;
                    outputVoltageEl.textContent = data.output_voltage;
                    loadVoltageEl.textContent = data.load_voltage;
                    
                    statusDot.className = 'status-indicator active';
                    statusText.textContent = 'Connected';
                    
                    valueContainers.forEach(el => el.classList.remove('loading'));
                } else {
                    throw new Error(data.message || 'Unknown error');
                }
            } catch (error) {
                console.error('Error fetching data:', error);
                statusDot.className = 'status-indicator error';
                statusText.textContent = 'Error: ' + error.message;
                // Don't reset values to '--' immediately to avoid flickering if it's just a transient network blip,
                // but maybe indicate stale data if needed. For now, keep last known values.
            }
        }

        // Fetch immediately, then every 5 seconds
        fetchData();
        setInterval(fetchData, 5000);
    </script>
</body>
</html>
