<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>婚礼总管 · 大屏展示</title>
    <link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><text y='0.9em' font-size='90'>👔</text></svg>">
    <style>
        @import url('https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;600;700;900&display=swap');

        * { margin: 0; padding: 0; box-sizing: border-box; }

        :root {
            --gold: #d4a853;
            --gold-light: #f0d68a;
            --gold-dark: #b8860b;
            --red: #b64a2a;
            --red-deep: #8b0000;
            --red-bright: #c41e3a;
            --text: #f0e6d3;
            --text-dim: #d4c4a8;
            --card-bg: rgba(60, 20, 20, 0.75);
            --card-border: rgba(212, 168, 83, 0.4);
        }

        body {
            background: #1a0a0a;
            color: var(--text);
            font-family: 'Noto Serif SC', 'SimSun', 'STSong', serif;
            overflow: hidden;
            height: 100vh;
            height: 100dvh;
            width: 100vw;
            cursor: none;
        }

        body.controls-visible { cursor: default; }

        .screen-bg {
            position: fixed;
            inset: 0;
            z-index: 0;
            background: 
                radial-gradient(ellipse at 50% 20%, rgba(255,200,100,0.25) 0%, transparent 50%),
                radial-gradient(ellipse at 50% 80%, rgba(139,0,0,0.5) 0%, rgba(40,10,10,0.95) 70%),
                linear-gradient(180deg, #2a0a0a 0%, #1a0505 50%, #0f0202 100%);
        }

        .bg-pattern {
            position: fixed;
            inset: 0;
            z-index: 1;
            pointer-events: none;
            opacity: 0.08;
            background-image: 
                radial-gradient(circle at 25% 25%, var(--gold) 1px, transparent 1px),
                radial-gradient(circle at 75% 75%, var(--gold) 1px, transparent 1px);
            background-size: 40px 40px;
        }

        .bg-xi {
            position: fixed;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            z-index: 2;
            pointer-events: none;
            opacity: 0.06;
            font-size: 500px;
            font-weight: 900;
            color: var(--gold);
            font-family: 'SimSun', serif;
            user-select: none;
        }

        .lantern-left, .lantern-right {
            position: fixed;
            top: 0;
            width: 120px;
            height: 200px;
            z-index: 3;
            pointer-events: none;
            opacity: 0.6;
        }

        .lantern-left { left: 30px; }
        .lantern-right { right: 30px; }

        .lantern {
            width: 100%;
            height: 100%;
            display: flex;
            flex-direction: column;
            align-items: center;
            animation: lanternSway 4s ease-in-out infinite;
        }

        .lantern-right .lantern { animation-delay: -2s; }

        .lantern-top {
            width: 30px;
            height: 10px;
            background: var(--gold-dark);
            border-radius: 3px;
        }

        .lantern-body {
            width: 100px;
            height: 120px;
            background: radial-gradient(ellipse at 30% 30%, #ff6b6b 0%, var(--red-bright) 40%, var(--red-deep) 100%);
            border-radius: 50%;
            position: relative;
            display: flex;
            align-items: center;
            justify-content: center;
            box-shadow: 0 0 30px rgba(255,100,100,0.5), inset 0 -20px 40px rgba(0,0,0,0.3);
        }

        .lantern-body::before {
            content: '囍';
            font-size: 48px;
            color: var(--gold);
            font-family: 'SimSun', serif;
            font-weight: bold;
            text-shadow: 0 0 10px rgba(212,168,83,0.8);
        }

        .lantern-bottom {
            width: 20px;
            height: 50px;
            background: linear-gradient(180deg, var(--gold) 0%, var(--gold-dark) 100%);
            border-radius: 0 0 5px 5px;
            position: relative;
        }

        .lantern-bottom::after {
            content: '';
            position: absolute;
            bottom: -15px;
            left: 50%;
            transform: translateX(-50%);
            width: 4px;
            height: 20px;
            background: var(--gold);
            border-radius: 2px;
        }

        @keyframes lanternSway {
            0%, 100% { transform: rotate(-3deg); }
            50% { transform: rotate(3deg); }
        }

        .light-rays {
            position: fixed;
            top: 0;
            left: 50%;
            transform: translateX(-50%);
            width: 100%;
            height: 60%;
            z-index: 4;
            pointer-events: none;
            opacity: 0.3;
            background: 
                repeating-conic-gradient(from 180deg at 50% 0%, 
                    transparent 0deg, 
                    rgba(255,215,100,0.15) 2deg, 
                    transparent 4deg, 
                    transparent 20deg);
            animation: rayRotate 60s linear infinite;
        }

        @keyframes rayRotate {
            0% { transform: translateX(-50%) rotate(0deg); }
            100% { transform: translateX(-50%) rotate(360deg); }
        }

        .particles {
            position: fixed;
            inset: 0;
            z-index: 5;
            pointer-events: none;
            overflow: hidden;
        }

        .particle {
            position: absolute;
            width: 3px;
            height: 3px;
            background: var(--gold);
            border-radius: 50%;
            opacity: 0;
            animation: floatUp linear infinite;
            box-shadow: 0 0 6px var(--gold);
        }

        @keyframes floatUp {
            0% { opacity: 0; transform: translateY(100vh) scale(0); }
            10% { opacity: 0.9; }
            90% { opacity: 0.4; }
            100% { opacity: 0; transform: translateY(-10vh) scale(1); }
        }

        .main-layout {
            position: relative;
            z-index: 10;
            display: flex;
            flex-direction: column;
            height: 100vh;
            height: 100dvh;
            padding: 10px 60px 20px;
        }

        .header-section {
            text-align: center;
            padding: 5px 0 8px 0;
            position: relative;
        }

        .header-decoration {
            display: flex;
            align-items: center;
            justify-content: center;
            gap: 20px;
            margin-bottom: 8px;
        }

        .deco-line {
            width: 150px;
            height: 2px;
            background: linear-gradient(90deg, transparent, var(--gold), transparent);
        }

        .deco-diamond {
            width: 12px;
            height: 12px;
            background: var(--gold);
            transform: rotate(45deg);
        }

        .header-title {
            font-size: 38px;
            color: var(--gold);
            letter-spacing: 16px;
            font-weight: 700;
            text-shadow: 0 0 40px rgba(212,168,83,0.5), 0 2px 4px rgba(0,0,0,0.5);
        }

        .header-subtitle {
            font-size: 14px;
            color: var(--text-dim);
            letter-spacing: 6px;
            margin-top: 6px;
        }

        .timeline-section {
            background: var(--card-bg);
            border: 1px solid var(--card-border);
            border-radius: 16px;
            padding: 15px 30px;
            margin-bottom: 10px;
            position: relative;
            backdrop-filter: blur(10px);
        }

        .timeline-section::before, .timeline-section::after {
            content: '';
            position: absolute;
            width: 30px;
            height: 30px;
            border: 2px solid var(--gold);
            opacity: 0.6;
        }

        .timeline-section::before {
            top: 8px;
            left: 8px;
            border-right: none;
            border-bottom: none;
        }

        .timeline-section::after {
            bottom: 8px;
            right: 8px;
            border-left: none;
            border-top: none;
        }

        .timeline-title {
            font-size: 16px;
            color: var(--gold);
            letter-spacing: 4px;
            text-align: center;
            margin-bottom: 15px;
            font-weight: 600;
        }

        .timeline-track-horizontal {
            position: relative;
            height: 80px;
            display: flex;
            align-items: center;
        }

        .timeline-line {
            position: absolute;
            left: 0;
            right: 0;
            top: 50%;
            height: 3px;
            background: linear-gradient(90deg, transparent, var(--gold-dark), var(--gold), var(--gold-dark), transparent);
            transform: translateY(-50%);
        }

        .timeline-progress-bar {
            position: absolute;
            left: 0;
            top: 50%;
            height: 3px;
            background: linear-gradient(90deg, var(--red-bright), var(--gold));
            transform: translateY(-50%);
            width: 0%;
            transition: width 1s ease;
            box-shadow: 0 0 10px var(--gold);
        }

        .timeline-nodes {
            position: relative;
            display: flex;
            justify-content: space-between;
            width: 100%;
            padding: 0 10px;
        }

        .timeline-node {
            display: flex;
            flex-direction: column;
            align-items: center;
            cursor: default;
            position: relative;
            flex: 1;
        }

        .node-dot {
            width: 20px;
            height: 20px;
            border-radius: 50%;
            background: var(--card-bg);
            border: 3px solid var(--gold-dark);
            position: relative;
            z-index: 2;
            transition: all 0.3s ease;
        }

        .timeline-node.active .node-dot {
            background: var(--gold);
            border-color: var(--gold-light);
            box-shadow: 0 0 20px var(--gold), 0 0 40px rgba(212,168,83,0.5);
            transform: scale(1.3);
        }

        .timeline-node.completed .node-dot {
            background: var(--gold-dark);
            border-color: var(--gold);
        }

        .timeline-node.active .node-dot::after {
            content: '';
            position: absolute;
            inset: -8px;
            border-radius: 50%;
            border: 2px solid var(--gold);
            animation: dotPulse 2s infinite;
        }

        @keyframes dotPulse {
            0%, 100% { opacity: 1; transform: scale(1); }
            50% { opacity: 0.3; transform: scale(1.5); }
        }

        .node-time {
            font-size: 11px;
            color: var(--text-dim);
            margin-top: 8px;
            white-space: nowrap;
        }

        .timeline-node.active .node-time {
            color: var(--gold-light);
            font-weight: 600;
        }

        .node-name {
            font-size: 12px;
            color: var(--text);
            margin-top: 4px;
            white-space: nowrap;
            max-width: 80px;
            overflow: hidden;
            text-overflow: ellipsis;
        }

        .timeline-node.active .node-name {
            color: var(--gold-light);
            font-weight: 600;
        }

        .phase-highlight {
            text-align: center;
            padding: 12px 30px;
            background: linear-gradient(135deg, rgba(212,168,83,0.15) 0%, rgba(196,30,58,0.15) 100%);
            border: 1px solid rgba(212,168,83,0.4);
            border-radius: 16px;
            margin-bottom: 10px;
            position: relative;
            backdrop-filter: blur(10px);
        }

        .phase-time {
            font-size: 16px;
            color: var(--gold-light);
            letter-spacing: 6px;
            margin-bottom: 6px;
        }

        .phase-name {
            font-size: 32px;
            color: var(--gold);
            font-weight: 700;
            letter-spacing: 8px;
            text-shadow: 0 0 30px rgba(212,168,83,0.4);
        }

        .phase-desc {
            font-size: 13px;
            color: var(--text-dim);
            margin-top: 8px;
            max-width: 80%;
            margin-left: auto;
            margin-right: auto;
            line-height: 1.6;
        }

        .content-grid {
            flex: 1;
            display: grid;
            grid-template-columns: 1fr 1fr;
            grid-template-rows: 1fr 1fr;
            gap: 12px;
            min-height: 0;
        }

        .info-card {
            background: var(--card-bg);
            border: 1px solid var(--card-border);
            border-radius: 14px;
            padding: 20px 24px;
            position: relative;
            overflow: hidden;
            backdrop-filter: blur(10px);
            display: flex;
            flex-direction: column;
        }

        .info-card::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 2px;
            background: linear-gradient(90deg, transparent, var(--gold), transparent);
            opacity: 0.6;
        }

        .card-header {
            display: flex;
            align-items: center;
            gap: 10px;
            margin-bottom: 12px;
            padding-bottom: 10px;
            border-bottom: 1px solid rgba(212,168,83,0.2);
        }

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

        .card-title {
            font-size: 17px;
            color: var(--gold);
            letter-spacing: 3px;
            font-weight: 600;
        }

        .card-content {
            flex: 1;
            font-size: 13px;
            line-height: 1.9;
            color: var(--text);
            overflow-y: auto;
            padding-right: 5px;
        }

        .card-content::-webkit-scrollbar {
            width: 4px;
        }

        .card-content::-webkit-scrollbar-track {
            background: transparent;
        }

        .card-content::-webkit-scrollbar-thumb {
            background: var(--gold-dark);
            border-radius: 2px;
        }

        .card-content .item {
            padding: 4px 0;
            border-bottom: 1px dashed rgba(212,168,83,0.15);
        }

        .card-content .item:last-child {
            border-bottom: none;
        }

        .card-content .highlight {
            color: var(--gold-light);
            font-weight: 500;
            margin-right: 6px;
        }

        .bottom-bar {
            position: fixed;
            bottom: 0;
            left: 0;
            right: 0;
            z-index: 20;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 10px 30px;
            background: linear-gradient(transparent, rgba(20,5,5,0.95));
            opacity: 0;
            transition: opacity 0.3s;
        }

        body.controls-visible .bottom-bar { opacity: 1; }

        .watermark {
            font-size: 12px;
            color: rgba(212,168,83,0.4);
            letter-spacing: 3px;
        }

        .status-indicator {
            display: flex;
            align-items: center;
            gap: 8px;
            font-size: 12px;
            color: var(--text-dim);
        }

        .status-dot {
            width: 8px;
            height: 8px;
            border-radius: 50%;
            background: #27ae60;
            animation: dotPulse 2s infinite;
        }

        .idle-state {
            position: absolute;
            top: 50%;
            left: 50%;
            transform: translate(-50%, -50%);
            text-align: center;
            opacity: 0.5;
            z-index: 100;
        }

        .idle-icon {
            font-size: 80px;
            margin-bottom: 20px;
        }

        .idle-text {
            font-size: 24px;
            color: var(--text-dim);
            letter-spacing: 8px;
        }

        .debug-indicator {
            position: fixed;
            top: 10px;
            right: 10px;
            z-index: 9999;
            background: rgba(0,0,0,0.7);
            color: #27ae60;
            padding: 4px 10px;
            font-size: 11px;
            border-radius: 4px;
            display: none;
        }

        @media (max-width: 1400px) {
            .header-title { font-size: 28px; letter-spacing: 10px; }
            .phase-name { font-size: 26px; letter-spacing: 6px; }
            .card-title { font-size: 15px; }
            .card-content { font-size: 12px; }
            .node-name { font-size: 10px; max-width: 60px; }
            .node-time { font-size: 10px; }
        }

        /* 大屏提醒弹窗遮罩 */
        .reminder-overlay {
            position: fixed;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            z-index: 9999;
            display: flex;
            align-items: center;
            justify-content: center;
            pointer-events: none;
            opacity: 0;
            transition: opacity 0.4s ease;
        }
        .reminder-overlay.show { opacity: 1; }
        .reminder-overlay-content {
            background: rgba(10,5,5,0.92);
            border: 2px solid var(--gold);
            border-radius: 24px;
            padding: 50px 80px;
            text-align: center;
            transform: scale(0.6);
            transition: transform 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
            box-shadow: 0 0 80px rgba(212,168,83,0.3), inset 0 0 60px rgba(212,168,83,0.05);
            min-width: 500px;
        }
        .reminder-overlay.show .reminder-overlay-content { transform: scale(1); }
        .reminder-overlay-icon { font-size: 72px; margin-bottom: 16px; animation: overlayIconPulse 1.5s ease-in-out infinite; }
        @keyframes overlayIconPulse {
            0%, 100% { transform: scale(1); }
            50% { transform: scale(1.1); }
        }
        .reminder-overlay-title {
            font-size: 36px;
            color: var(--gold);
            font-weight: 700;
            letter-spacing: 6px;
            margin-bottom: 12px;
        }
        .reminder-overlay-desc {
            font-size: 22px;
            color: var(--text);
            letter-spacing: 4px;
            opacity: 0.9;
            margin-bottom: 24px;
        }
        .reminder-overlay-bar {
            width: 300px;
            height: 4px;
            background: rgba(212,168,83,0.2);
            border-radius: 2px;
            margin: 0 auto;
            overflow: hidden;
        }
        .reminder-overlay-progress {
            height: 100%;
            width: 100%;
            background: linear-gradient(90deg, var(--gold), var(--gold-light));
            animation: overlayProgress 4s linear forwards;
            border-radius: 2px;
        }
        @keyframes overlayProgress {
            from { width: 100%; }
            to { width: 0%; }
        }
        .reminder-overlay.overdue .reminder-overlay-content { border-color: #e74c3c; box-shadow: 0 0 80px rgba(231,76,60,0.3); }
        .reminder-overlay.overdue .reminder-overlay-title { color: #ff6b6b; }
        .reminder-overlay.switch .reminder-overlay-content { border-color: #27ae60; box-shadow: 0 0 80px rgba(39,174,96,0.3); }
        .reminder-overlay.switch .reminder-overlay-title { color: #2ecc71; }
    </style>
</head>
<body>

<div class="screen-bg"></div>
<div class="bg-pattern"></div>
<div class="bg-xi">囍</div>
<div class="light-rays"></div>

<div class="lantern-left">
    <div class="lantern">
        <div class="lantern-top"></div>
        <div class="lantern-body"></div>
        <div class="lantern-bottom"></div>
    </div>
</div>

<div class="lantern-right">
    <div class="lantern">
        <div class="lantern-top"></div>
        <div class="lantern-body"></div>
        <div class="lantern-bottom"></div>
    </div>
</div>

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

<div class="bottom-bar">
    <div class="watermark">礼典世家 · 婚礼总管如意管家</div>
    <div class="status-indicator">
        <span class="status-dot"></span>
        <span>实时同步中</span>
    </div>
</div>

<div class="debug-indicator" id="debugIndicator">SYNC OK</div>

<div class="main-layout">
    <div class="header-section">
        <div class="header-decoration">
            <div class="deco-line"></div>
            <div class="deco-diamond"></div>
            <div class="deco-line"></div>
        </div>
        <div class="header-title">👔 如意管家</div>
        <div class="header-subtitle">婚礼统筹 · 全程监控</div>
    </div>

    <div class="timeline-section">
        <div class="timeline-title">统筹时间轴</div>
        <div class="timeline-track-horizontal">
            <div class="timeline-line"></div>
            <div class="timeline-progress-bar" id="timelineProgressBar"></div>
            <div class="timeline-nodes" id="timelineNodes"></div>
        </div>
    </div>

    <div class="phase-highlight" id="phaseHighlight">
        <div class="phase-time" id="phaseTime"></div>
        <div class="phase-name" id="phaseName"></div>
        <div class="phase-desc" id="phaseDesc"></div>
    </div>

    <div class="content-grid">
        <div class="info-card" id="cardTeam">
            <div class="card-header">
                <span class="card-icon">👥</span>
                <span class="card-title">负责人员</span>
            </div>
            <div class="card-content" id="contentTeam">
                <div class="idle-text">等待数据同步...</div>
            </div>
        </div>

        <div class="info-card" id="cardMaterials">
            <div class="card-header">
                <span class="card-icon">📦</span>
                <span class="card-title">必备物资</span>
            </div>
            <div class="card-content" id="contentMaterials">
                <div class="idle-text">等待数据同步...</div>
            </div>
        </div>

        <div class="info-card" id="cardNotes">
            <div class="card-header">
                <span class="card-icon">📝</span>
                <span class="card-title">习俗备注</span>
            </div>
            <div class="card-content" id="contentNotes">
                <div class="idle-text">等待数据同步...</div>
            </div>
        </div>

        <div class="info-card" id="cardInstruction">
            <div class="card-header">
                <span class="card-icon">🎯</span>
                <span class="card-title">统筹指令</span>
            </div>
            <div class="card-content" id="contentInstruction">
                <div class="idle-text">等待数据同步...</div>
            </div>
        </div>
    </div>
</div>

<div class="idle-state" id="idleState">
    <div class="idle-icon">👔</div>
    <div class="idle-text">选择环节开始</div>
</div>

<div class="reminder-overlay" id="reminderOverlay">
    <div class="reminder-overlay-content" id="reminderOverlayContent">
        <div class="reminder-overlay-icon" id="reminderOverlayIcon">⏰</div>
        <div class="reminder-overlay-title" id="reminderOverlayTitle">即将开始</div>
        <div class="reminder-overlay-desc" id="reminderOverlayDesc">环节名称</div>
        <div class="reminder-overlay-bar"><div class="reminder-overlay-progress" id="reminderOverlayProgress"></div></div>
    </div>
</div>

<script>
(function() {
    var syncCheckInterval = null;
    var controlsTimer = null;
    var allPhases = null;

    function init() {
        try {
            createParticles();
            startSyncCheck();
            
            window.addEventListener('storage', onStorageChange);
            window.addEventListener('message', onMessageReceived);
            
            document.addEventListener('mousemove', showControls);
            document.addEventListener('click', showControls);
            
            var existingData = localStorage.getItem('managerPhaseData');
            var timelineData = localStorage.getItem('managerTimelineData');
            
            if (timelineData) {
                var phases = JSON.parse(timelineData);
                renderTimeline(phases);
            }
            
            if (existingData) {
                var data = JSON.parse(existingData);
                updateDisplay(data);
            }
            
            setTimeout(function() {
                var checkData = localStorage.getItem('managerPhaseData');
                var timelineData = localStorage.getItem('managerTimelineData');
                
                if (timelineData && !allPhases) {
                    try {
                        renderTimeline(JSON.parse(timelineData));
                    } catch(err) {}
                }
                
                if (!checkData) {
                    showWelcome();
                } else {
                    try {
                        updateDisplay(JSON.parse(checkData));
                    } catch(err) {}
                }
            }, 500);
        } catch(e) {
            console.error('Screen init error:', e);
        }
    }

    function createParticles() {
        var container = document.getElementById('particles');
        for (var i = 0; i < 25; i++) {
            var p = document.createElement('div');
            p.className = 'particle';
            p.style.left = Math.random() * 100 + '%';
            p.style.animationDuration = (10 + Math.random() * 15) + 's';
            p.style.animationDelay = Math.random() * 10 + 's';
            p.style.width = (2 + Math.random() * 4) + 'px';
            p.style.height = p.style.width;
            container.appendChild(p);
        }
    }

    function showControls() {
        document.body.classList.add('controls-visible');
        clearTimeout(controlsTimer);
        controlsTimer = setTimeout(function() {
            document.body.classList.remove('controls-visible');
        }, 3000);
    }

    function startSyncCheck() {
        syncCheckInterval = setInterval(function() {
            var phaseData = localStorage.getItem('managerPhaseData');
            var timelineData = localStorage.getItem('managerTimelineData');
            
            if (timelineData && !allPhases) {
                try {
                    var phases = JSON.parse(timelineData);
                    renderTimeline(phases);
                } catch(e) {}
            }
            
            if (phaseData) {
                try {
                    var data = JSON.parse(phaseData);
                    updateDisplay(data);
                } catch(e) {
                    console.error('Parse phase data error:', e);
                }
            }
        }, 500);
    }

    function onStorageChange(e) {
        if (e.key === 'managerPhaseData' && e.newValue) {
            try {
                var data = JSON.parse(e.newValue);
                updateDisplay(data);
            } catch(err) {
                console.error('Storage change parse error:', err);
            }
        }
        if (e.key === 'managerTimelineData' && e.newValue) {
            try {
                var phases = JSON.parse(e.newValue);
                renderTimeline(phases);
            } catch(err) {}
        }
    }

    function onMessageReceived(e) {
        try {
            if (e.data && e.data.type === 'managerPhaseUpdate') {
                if (!allPhases) {
                    var timelineData = localStorage.getItem('managerTimelineData');
                    if (timelineData) {
                        try {
                            renderTimeline(JSON.parse(timelineData));
                        } catch(err) {}
                    }
                }
                updateDisplay(e.data.payload);
            }
            if (e.data && e.data.type === 'managerTimelineUpdate') {
                renderTimeline(e.data.payload);
            }
        } catch(err) {
            console.error('postMessage handler error:', err);
        }
    }

    function renderTimeline(phases) {
        if (!phases || !Array.isArray(phases) || phases.length === 0) return;
        
        allPhases = phases;
        
        var container = document.getElementById('timelineNodes');
        if (!container) {
            console.error('timelineNodes container not found');
            return;
        }
        
        var html = '';
        
        phases.forEach(function(phase, index) {
            if (!phase || typeof phase !== 'object') return;
            
            var timePart = '';
            var namePart = phase.name || '未知环节';
            var code = phase.code || 'phase_' + index;
            
            if (typeof phase.name === 'string') {
                var spaceIndex = phase.name.indexOf(' ');
                if (spaceIndex > 0 && spaceIndex < phase.name.length - 1) {
                    var firstPart = phase.name.substring(0, spaceIndex);
                    if (/^[\d:–\-]+/.test(firstPart)) {
                        timePart = firstPart;
                        namePart = phase.name.substring(spaceIndex + 1);
                    }
                }
            }
            
            html += '<div class="timeline-node" data-code="' + escapeHtml(code) + '" data-index="' + index + '">' +
                '<div class="node-dot"></div>' +
                (timePart ? '<div class="node-time">' + escapeHtml(timePart) + '</div>' : '') +
                '<div class="node-name">' + escapeHtml(namePart) + '</div>' +
                '</div>';
        });
        
        container.innerHTML = html;
    }

    function updateDisplay(data) {
        if (!data || typeof data !== 'object') {
            console.error('Invalid phase data:', data);
            return;
        }
        
        var idleState = document.getElementById('idleState');
        if (idleState) idleState.style.display = 'none';

        var timePart = '';
        var namePart = typeof data.name === 'string' ? data.name : '未知环节';
        
        if (typeof data.name === 'string') {
            var spaceIndex = data.name.indexOf(' ');
            if (spaceIndex > 0 && spaceIndex < data.name.length - 1) {
                var firstPart = data.name.substring(0, spaceIndex);
                if (/^[\d:–\-]+/.test(firstPart)) {
                    timePart = firstPart;
                    namePart = data.name.substring(spaceIndex + 1);
                }
            }
        }
        
        var phaseTime = document.getElementById('phaseTime');
        var phaseName = document.getElementById('phaseName');
        var phaseDesc = document.getElementById('phaseDesc');
        
        if (phaseTime) phaseTime.textContent = timePart;
        if (phaseName) phaseName.textContent = namePart;
        if (phaseDesc) phaseDesc.textContent = typeof data.desc === 'string' ? data.desc : '';

        updateCard('cardTeam', 'contentTeam', typeof data.team === 'string' ? data.team : '暂无信息', 'team');
        updateCard('cardMaterials', 'contentMaterials', typeof data.materials === 'string' ? data.materials : '暂无信息', 'materials');
        updateCard('cardNotes', 'contentNotes', typeof data.notes === 'string' ? data.notes : '暂无信息', 'notes');
        updateCard('cardInstruction', 'contentInstruction', typeof data.instruction === 'string' ? data.instruction : '暂无信息', 'instruction');

        var code = typeof data.code === 'string' ? data.code : '';
        var elapsed = typeof data.elapsed === 'number' ? data.elapsed : 0;
        var totalDuration = typeof data.totalDuration === 'number' ? data.totalDuration : 0;
        
        updateTimelineProgress(code, elapsed, totalDuration);

        var debugIndicator = document.getElementById('debugIndicator');
        if (debugIndicator) {
            debugIndicator.style.display = 'block';
            debugIndicator.textContent = '同步: ' + namePart;
        }
    }

    function updateTimelineProgress(currentCode, elapsed, totalDuration) {
        if (!allPhases || !allPhases.length) return;
        
        var nodes = document.querySelectorAll('.timeline-node');
        var calcTotalDuration = 0;
        var calcElapsed = elapsed || 0;
        var currentIndex = -1;
        
        allPhases.forEach(function(p, i) {
            calcTotalDuration += p.duration;
            if (p.code === currentCode) {
                currentIndex = i;
            }
        });
        
        var finalTotal = totalDuration || calcTotalDuration;
        var progress = finalTotal > 0 ? (calcElapsed / finalTotal) * 100 : 0;
        progress = Math.max(0, Math.min(100, progress));
        document.getElementById('timelineProgressBar').style.width = progress + '%';
        
        var timeAccum = 0;
        nodes.forEach(function(node, i) {
            node.classList.remove('active', 'completed');
            
            var phase = allPhases[i];
            phase.startTime = timeAccum;
            phase.endTime = timeAccum + phase.duration;
            timeAccum = phase.endTime;
            
            if (calcElapsed >= phase.endTime) {
                node.classList.add('completed');
            } else if (calcElapsed >= phase.startTime && calcElapsed < phase.endTime) {
                node.classList.add('active');
            }
        });
    }

    function updateCard(cardId, contentId, text, type) {
        var content = document.getElementById(contentId);
        
        if (type === 'team') {
            var items = text.split('；');
            var html = '';
            items.forEach(function(item) {
                if (item.trim()) {
                    html += '<div class="item"><span class="highlight">◆</span> ' + escapeHtml(item.trim()) + '</div>';
                }
            });
            content.innerHTML = html;
        } else if (type === 'materials') {
            var items = text.split('、');
            var html = '<div style="columns:2;column-gap:15px;">';
            items.forEach(function(item) {
                if (item.trim()) {
                    html += '<div style="break-inside:avoid;padding:3px 0;">' + escapeHtml(item.trim()) + '</div>';
                }
            });
            html += '</div>';
            content.innerHTML = html;
        } else if (type === 'notes') {
            var items = text.split('；');
            var html = '';
            items.forEach(function(item) {
                if (item.trim()) {
                    html += '<div class="item" style="color:#f0d68a;"><span class="highlight">⚠</span> ' + escapeHtml(item.trim()) + '</div>';
                }
            });
            content.innerHTML = html;
        } else {
            content.innerHTML = escapeHtml(text);
        }
    }

    function showWelcome() {
        document.getElementById('idleState').style.display = 'none';
        document.getElementById('phaseTime').textContent = '';
        document.getElementById('phaseName').textContent = '如意管家';
        document.getElementById('phaseDesc').textContent = '请在总管控制台选择环节，详细信息将同步显示于此';
        
        updateCard('cardTeam', 'contentTeam', '等待总管控制台选择环节', 'team');
        updateCard('cardMaterials', 'contentMaterials', '等待数据同步...', 'materials');
        updateCard('cardNotes', 'contentNotes', '等待数据同步...', 'notes');
        updateCard('cardInstruction', 'contentInstruction', '点击时间轴环节即可同步显示详细信息', 'instruction');
    }

    function escapeHtml(text) {
        var div = document.createElement('div');
        div.textContent = text;
        return div.innerHTML;
    }

    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', init);
    } else {
        init();
    }
})();
</script>

</body>
</html>