function updateTimeAndStatus() {
    const now = new Date();
    const timeString = now.toLocaleTimeString('de-DE', { 
        hour: '2-digit', 
        minute: '2-digit', 
        hour12: false 
    });
    
    // Update time display
    const timeDisplay = document.getElementById('current-time');
    if (timeDisplay) {
        timeDisplay.textContent = timeString;
    }

    fetch('/api/check-open-status')
        .then(response => response.json())
        .then(status => {
            const statusBar = document.getElementById('status-bar');
            const statusText = document.getElementById('status-text');
            const deliveryText = document.querySelector('.delivery-text');
            
            if (statusBar && statusText) {
                statusBar.className = status.isOpen 
                    ? 'bg-status-green transition-colors duration-300'
                    : 'bg-status-red transition-colors duration-300';
                
                const textOpen = statusText.dataset.textOpen;
                const textClosed = statusText.dataset.textClosed;
                statusText.textContent = status.isOpen ? textOpen : textClosed;
            }

            // Debug information
            const debugInfo = document.getElementById('debug-info');
            if (debugInfo && !status.isOpen) {
                const nextOpen = document.getElementById('next-open');
                const currentDay = document.getElementById('current-day');
                
                if (currentDay) {
                    currentDay.textContent = now.toLocaleDateString('de-DE', { weekday: 'long' });
                }
                
                if (nextOpen) {
                    const hoursRows = document.querySelectorAll('.hours-row');
                    let nextOpenTime = null;
                    let foundToday = false;
                    
                    // First check today's remaining times
                    hoursRows.forEach(row => {
                        if (row.dataset.day === status.currentDay) {
                            const openTime = row.dataset.open;
                            if (openTime > timeString) {
                                nextOpenTime = `Today at ${openTime}`;
                                foundToday = true;
                            }
                        }
                    });
                    
                    // If no times found today, find next day's opening
                    if (!foundToday) {
                        hoursRows.forEach(row => {
                            const dayIndex = parseInt(row.dataset.dayIndex || 0);
                            const currentDayIndex = now.getDay();
                            
                            if (dayIndex > currentDayIndex || (dayIndex === 0 && currentDayIndex !== 0)) {
                                const openTime = row.dataset.open;
                                const dayName = row.dataset.dayName;
                                if (!nextOpenTime) {
                                    nextOpenTime = `${dayName} at ${openTime}`;
                                }
                            }
                        });
                    }
                    
                    nextOpen.textContent = nextOpenTime || 'Check opening hours';
                }
            }

            // Update other elements
            if (deliveryText) {
                deliveryText.className = status.isOpen 
                    ? 'delivery-text text-status-green'
                    : 'delivery-text text-status-red';
            }

            document.querySelectorAll('.hours-row').forEach(row => {
                const isCurrentDay = row.dataset.day === status.currentDay;
                row.classList.toggle('border-2', isCurrentDay && status.isOpen);
                row.classList.toggle('border-status-green', isCurrentDay && status.isOpen);
            });
        });
}

// Update immediately and then every minute
updateTimeAndStatus();
setInterval(updateTimeAndStatus, 60000);