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

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
    background: #1a1a2e;
    color: #fff;
    height: 100vh;
    overflow: hidden;
    user-select: none;
    -webkit-user-select: none;
}

#app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    max-width: 480px;
    margin: 0 auto;
    background: #1a1a2e;
}

/* ============ Digital Human Avatar ============ */
.video-container {
    position: relative;
    flex: 1;
    min-height: 320px;
    background: radial-gradient(circle at 50% 35%, #232347 0%, #0d0d1a 80%);
    overflow: hidden;
    display: flex;
    align-items: center;
    justify-content: center;
}

.dh {
    position: relative;
    width: 260px;
    height: 280px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.3s ease;
}

.dh-halo {
    position: absolute;
    top: 50%; left: 50%;
    width: 210px; height: 210px;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    background: radial-gradient(circle, rgba(120,130,240,0.35) 0%, rgba(120,130,240,0) 70%);
    opacity: 0.6;
    transition: background 0.4s ease, opacity 0.4s ease;
}

.dh-svg {
    position: relative;
    width: 210px;
    height: 250px;
    z-index: 2;
    filter: drop-shadow(0 8px 20px rgba(0,0,0,0.4));
}

/* idle breathing */
.dh.idle .dh-svg { animation: dhBreathe 4.5s ease-in-out infinite; }
@keyframes dhBreathe {
    0%, 100% { transform: translateY(0); }
    50% { transform: translateY(-4px); }
}

.dh-expression {
    position: absolute;
    bottom: 4px;
    z-index: 3;
    font-size: 13px;
    color: #cfd2ff;
    background: rgba(0,0,0,0.35);
    padding: 3px 12px;
    border-radius: 12px;
    white-space: nowrap;
}

/* eyes blink */
.dh-eye {
    transform-box: fill-box;
    transform-origin: center;
    animation: dhBlink 4.5s infinite;
}
@keyframes dhBlink {
    0%, 92%, 100% { transform: scaleY(1); }
    96% { transform: scaleY(0.1); }
}

/* mouth: JS-driven lip-sync when .lipsync is present; CSS fake talk otherwise */
.dh-mouth {
    transform-box: fill-box;
    transform-origin: 50% 50%;
    transition: transform 0.05s linear;
}
.dh-mouth.lipsync { animation: none !important; }
.dh.speaking .dh-mouth:not(.lipsync) { animation: dhTalk 0.16s infinite alternate; }

@keyframes dhTalk {
    0% { transform: scaleY(1); }
    100% { transform: scaleY(2.6); }
}

/* ---- states ---- */
.dh.idle .dh-halo { background: radial-gradient(circle, rgba(150,150,170,0.30) 0%, rgba(150,150,170,0) 70%); }

.dh.listening .dh-halo {
    background: radial-gradient(circle, rgba(76,175,80,0.45) 0%, rgba(76,175,80,0) 70%);
    animation: dhPulse 1.4s infinite;
}
.dh.listening .dh-expression { color: #8fffa0; }

.dh.thinking .dh-halo {
    background: radial-gradient(circle, rgba(255,152,0,0.42) 0%, rgba(255,152,0,0) 70%);
    animation: dhPulse 1s infinite;
}
.dh.thinking .dh-expression { color: #ffce8a; }

.dh.speaking .dh-halo {
    background: radial-gradient(circle, rgba(124,141,240,0.55) 0%, rgba(124,141,240,0) 70%);
}
.dh.speaking .dh-expression { color: #b9c0ff; }

/* ---- video mode (real digital human clip) ---- */
.dh.video-mode .dh-svg,
.dh.video-mode .dh-halo { display: none; }
.dh-video {
    display: none;
    width: 100%;
    height: 100%;
    max-width: 340px;
    max-height: 380px;
    object-fit: contain;
    border-radius: 16px;
    z-index: 2;
    background: #000;
}
.dh-video:not([src]):not([srcobject]) { background: #0e1430; }
.dh.video-mode .dh-video { display: block; }

/* canvas is the captureStream source for <video> - keep it invisible */
.dh-canvas {
    position: absolute;
    top: 0; left: 0;
    width: 100%; height: 100%;
    opacity: 0;
    pointer-events: none;
    z-index: 1;
}
/* fallback when captureStream() is unsupported: show the canvas directly */
.dh.video-mode.show-canvas .dh-video { display: none; }
.dh.video-mode.show-canvas .dh-canvas { opacity: 1; }

/* mood: happy raises brows, serious lowers */
.dh-mood-happy .dh-brow-l { transform: translateY(-3px) rotate(-6deg); }
.dh-mood-happy .dh-brow-r { transform: translateY(-3px) rotate(6deg); }
.dh-mood-serious .dh-brow-l,
.dh-mood-serious .dh-brow-r { transform: translateY(3px); }
.dh-brow { transform-box: fill-box; transform-origin: center; transition: transform 0.3s ease; }

.video-loading {
    position: absolute;
    top: 50%; left: 50%;
    transform: translate(-50%, -50%);
    color: #888;
    font-size: 14px;
    z-index: 5;
}

/* Status Bar */
.status-bar {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    padding: 8px 16px;
    background: rgba(0,0,0,0.3);
    font-size: 13px;
}
.status-text { color: #aaa; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #666; }
.status-dot.idle { background: #666; }
.status-dot.listening { background: #4CAF50; animation: pulse 1.5s infinite; }
.status-dot.replying { background: #FF9800; animation: pulse 1s infinite; }
@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

/* Chat Messages */
.chat-messages {
    flex: 0 0 auto;
    max-height: 200px;
    overflow-y: auto;
    padding: 12px 16px;
    display: flex;
    flex-direction: column;
    gap: 8px;
}
.chat-messages::-webkit-scrollbar { width: 4px; }
.chat-messages::-webkit-scrollbar-thumb { background: #333; border-radius: 2px; }

.msg {
    max-width: 80%;
    padding: 8px 12px;
    border-radius: 12px;
    font-size: 14px;
    line-height: 1.5;
    word-break: break-word;
    animation: fadeIn 0.3s ease;
}
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(10px); }
    to { opacity: 1; transform: translateY(0); }
}
.msg.user { align-self: flex-end; background: #534AB7; color: #fff; }
.msg.ai { align-self: flex-start; background: #2a2a4a; color: #e0e0e0; }

/* Input Area */
.input-area {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 16px;
    background: rgba(0,0,0,0.4);
}
.btn-voice {
    flex: 0 0 auto;
    width: 44px; height: 44px;
    border: none; border-radius: 50%;
    background: #534AB7; color: #fff;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.2s;
}
.btn-voice:active, .btn-voice.recording {
    background: #A32D2D;
    transform: scale(1.1);
    box-shadow: 0 0 0 4px rgba(163,45,45,0.25);
}
.input-text {
    flex: 1; height: 40px;
    border: 1px solid #333; border-radius: 20px;
    background: #2a2a4a; color: #fff;
    padding: 0 16px; font-size: 14px; outline: none;
}
.input-text:focus { border-color: #534AB7; }
.btn-send {
    flex: 0 0 auto;
    width: 44px; height: 44px;
    border: none; border-radius: 50%;
    background: #0F6E56; color: #fff;
    cursor: pointer;
    display: flex; align-items: center; justify-content: center;
    transition: all 0.2s;
}
.btn-send:active { transform: scale(0.95); }
.btn-send:disabled { opacity: 0.5; cursor: not-allowed; }

/* Volume Meter */
.volume-meter {
    display: flex; align-items: center; gap: 12px;
    padding: 8px 16px;
    background: rgba(15, 110, 86, 0.2);
}
.volume-meter.hidden { display: none; }
.volume-bar {
    flex: 1; height: 6px; background: #333; border-radius: 3px; overflow: hidden;
}
.volume-fill {
    height: 100%; width: 0%;
    background: linear-gradient(90deg, #4CAF50, #FF9800, #f44336);
    transition: width 0.1s;
}
.volume-label { font-size: 12px; color: #4CAF50; white-space: nowrap; }

/* TTS toggle */
.tts-toggle {
    display: flex; align-items: center; gap: 8px;
    padding: 6px 16px 14px;
    background: rgba(0,0,0,0.4);
    font-size: 12px; color: #999;
}
.tts-toggle input { vertical-align: middle; }
.btn-clear {
    margin-left: auto;
    border: 1px solid #444;
    background: transparent;
    color: #999;
    border-radius: 12px;
    padding: 3px 10px;
    font-size: 12px;
    cursor: pointer;
    transition: all .15s;
}
.btn-clear:active { background: #2a2a4a; color: #cfd2ff; }
.tts-source { color: #777; }
