/* ============================================================
   财小青数字人 · 舞台式界面
   配色沿用线上财小青青绿品牌（--teal），舞台用深青墨色，
   透明人物在深色舞台上有轮廓光，字幕与控制台贴在底部。
   ============================================================ */
:root {
  --teal: #18aaa2;
  --teal-bright: #2ec6b8;
  --teal-deep: #0d817c;
  --stage-1: #0b2b2e;
  --stage-2: #10383b;
  --stage-3: #07201f;
  --ink: #0f2a2c;
  --text: #eaf6f5;
  --text-dim: #9db8b7;
  --surface: #ffffff;
  --line: rgba(255, 255, 255, 0.14);
  --danger: #ff6b6b;
  --radius: 16px;
  --safe: env(safe-area-inset-bottom, 0px);
  --font: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: var(--font);
  color: var(--text);
  background: var(--stage-1);
  -webkit-text-size-adjust: none;
  text-size-adjust: none;
  overflow: hidden;
}

button { font: inherit; cursor: pointer; border: 0; }
::selection { background: rgba(46, 198, 184, 0.35); }
:focus-visible { outline: 2px solid var(--teal-bright); outline-offset: 2px; }

.stage {
  position: relative;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background:
    radial-gradient(60% 45% at 50% 18%, rgba(46, 198, 184, 0.20), transparent 70%),
    radial-gradient(45% 35% at 12% 88%, rgba(13, 129, 124, 0.22), transparent 72%),
    linear-gradient(180deg, var(--stage-2) 0%, var(--stage-1) 52%, var(--stage-3) 100%);
}

/* 舞台地面光弧，让人物“站”在地面上而不是浮着 */
.stage::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: 8%;
  width: min(560px, 88vw);
  height: 120px;
  transform: translateX(-50%);
  background: radial-gradient(closest-side, rgba(46, 198, 184, 0.22), transparent 78%);
  pointer-events: none;
}

/* ---------- 顶栏 ---------- */
.stage-top {
  position: relative;
  z-index: 4;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 18px;
  padding-top: calc(14px + env(safe-area-inset-top));
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--teal-bright);
  box-shadow: 0 0 0 4px rgba(46, 198, 184, 0.18);
}
.brand-name { font-size: 16px; font-weight: 700; letter-spacing: 0.01em; }
.brand-sub { font-size: 11px; color: var(--text-dim); margin-top: 1px; }
.stage-actions { display: flex; gap: 8px; }

.ghost-btn {
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 7px 13px;
  font-size: 12px;
  transition: background 0.18s ease, border-color 0.18s ease;
}
.ghost-btn:hover { background: rgba(255, 255, 255, 0.14); border-color: rgba(255, 255, 255, 0.28); }
/* 已静音：压暗并降低强调，让「语音已关」一眼可辨 */
.ghost-btn.is-muted {
  background: rgba(255, 255, 255, 0.04);
  border-color: rgba(255, 255, 255, 0.1);
  color: var(--text-dim);
}
.ghost-btn.is-muted:hover { background: rgba(255, 255, 255, 0.1); border-color: rgba(255, 255, 255, 0.22); }

/* ---------- 人物 ---------- */
.figure-wrap {
  position: relative;
  z-index: 3;
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 4px 18px 0;
}
.halo {
  position: absolute;
  width: min(340px, 74vw);
  aspect-ratio: 1;
  border-radius: 50%;
  background: radial-gradient(closest-side, rgba(46, 198, 184, 0.26), transparent 72%);
  filter: blur(6px);
  transition: opacity 0.4s ease, transform 0.5s ease;
  opacity: 0.55;
}
.figure {
  position: relative;
  height: 100%;
  max-height: 58vh;
  /* 人物视频与静帧叠放在同一格，底部对齐居中，避免绝对定位的居中误差 */
  display: grid;
  grid-template-areas: "stack";
  place-items: end center;
}
.figure > * { grid-area: stack; }
/* 静帧兜底：视频首帧真正绘制前显示静态人物，避免空舞台；
   视频不可用时也保证人物可见（部分安卓/微信内核不合成 VP9 透明视频）。 */
.figure-still {
  height: 100%;
  max-height: 58vh;
  width: auto;
  object-fit: contain;
  object-position: center bottom;
  filter: drop-shadow(0 22px 34px rgba(0, 0, 0, 0.42));
  opacity: 1;
  transition: opacity 0.35s ease;
  pointer-events: none;
  /* 静帧盖在视频之上：这样视频本身始终是「可见」的，
     微信 X5 等内核会拒绝播放自身被隐藏（opacity:0 / display:none）的视频。 */
  z-index: 2;
}
.figure video,
.figure-packed {
  height: 100%;
  max-height: 58vh;
  width: auto;
  object-fit: contain;
  object-position: center bottom;
  filter: drop-shadow(0 22px 34px rgba(0, 0, 0, 0.42));
  opacity: 1;
  transition: transform 0.5s cubic-bezier(0.32, 0.72, 0, 1), opacity 0.35s ease;
  z-index: 1;
}
/* 拆包画布：默认不显示，进入拆包模式后才淡入。
   canvas 是替换元素，按 height 定尺寸 + width:auto 会依据 width/height 属性保持比例，
   和上面视频的布局方式一致。 */
.figure-packed {
  opacity: 0;
  z-index: 2;
  pointer-events: none;
}
/* 拆包模式：视频只作为解码源，画面由画布重绘，所以要把它藏起来，
   否则会把「颜色 + alpha」两条画面直接暴露出来。
   用 opacity 而不是 display/visibility —— 部分内核会拒绝播放不在渲染树里的视频。 */
.figure.use-packed > video { opacity: 0; }
/* 人物动画（原生透明视频或拆包画布）确认出帧后，淡出上层的静帧 */
.figure.use-video .figure-still { opacity: 0; }
.figure.use-packed .figure-packed { opacity: 1; }

/* 真机排查面板：?debug=1 时显示 */
.avatar-debug {
  position: absolute;
  left: 10px;
  top: 10px;
  z-index: 9;
  max-width: min(460px, 92vw);
  max-height: 70vh;
  overflow: auto;
  margin: 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(4, 18, 20, 0.92);
  border: 1px solid rgba(46, 198, 184, 0.4);
  color: #cdeceb;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-all;
}
.avatar-debug[hidden] { display: none; }

/* 状态：聆听时人物微微前倾靠近，播报时轻微起伏 */
.stage[data-state="listening"] .figure video,
.stage[data-state="listening"] .figure-packed { transform: scale(1.03) translateY(2px); }
.stage[data-state="speaking"] .figure video,
.stage[data-state="speaking"] .figure-packed { transform: scale(1.015); }
.stage[data-state="listening"] .halo { opacity: 0.95; transform: scale(1.06); }
.stage[data-state="speaking"] .halo { opacity: 0.8; }

/* ---------- 角色切换 ---------- */
/* 角色按钮由 JS 依据后端角色列表渲染；只有 1 个角色时整块隐藏 */
.persona-switch {
  position: relative;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 2px 18px 6px;
}
.persona-switch[hidden] { display: none; }
.persona-switch:empty { display: none; }
.persona-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--text-dim);
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid var(--line);
  transition: background 0.18s ease, border-color 0.18s ease, color 0.18s ease;
}
.persona-btn:hover { background: rgba(255, 255, 255, 0.12); color: var(--text); }
.persona-btn[aria-selected="true"] {
  color: #04211f;
  background: var(--teal-bright);
  border-color: var(--teal-bright);
}
/* 选中项左侧的小圆点，颜色跟随文字 */
.persona-btn::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: currentColor;
  opacity: 0.55;
}
.persona-btn[aria-selected="true"]::before { opacity: 1; }

/* ---------- 角色切换 ---------- */
/* 顶栏下方左侧的分段控件，按钮由 JS 按后端 /api/digital-man/config 的角色列表渲染。
   用文档流定位而不是绝对定位：顶栏高度在窄屏会变（副标题隐藏），绝对定位会压到人物。 */
.persona-switch {
  position: relative;
  z-index: 5;
  align-self: flex-start;
  display: inline-flex;
  align-items: center;
  gap: 3px;
  margin: 2px 0 0 16px;
  padding: 3px;
  border-radius: 999px;
  background: rgba(7, 26, 27, 0.6);
  border: 1px solid rgba(255, 255, 255, 0.14);
  backdrop-filter: blur(12px);
}
.persona-switch[hidden] { display: none; }
.persona-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--text-dim);
  background: transparent;
  border: 1px solid transparent;
  white-space: nowrap;
  transition: background 0.18s ease, color 0.18s ease;
}
.persona-btn:hover { color: var(--text); background: rgba(255, 255, 255, 0.08); }
.persona-btn[aria-selected="true"] {
  color: #04211f;
  background: var(--teal-bright);
}
/* 形象素材缺失的角色只切换音色（形象沿用默认角色）。
   这是内部进度，不向访客暴露，只在 ?debug=1 面板里说明。 */

/* ---------- 互动引导 ---------- */
/* 首次进入时提示用户点击。点击会唤醒人物动画：
   微信 X5 要求播放由用户手势触发，这条提示同时也是引导。 */
.interact-hint {
  position: absolute;
  top: 4px;
  left: 50%;
  transform: translateX(-50%);
  /* 高于人物视频（1/2），低于字幕（6） */
  z-index: 5;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 12.5px;
  font-weight: 700;
  color: var(--text);
  white-space: nowrap;
  background: rgba(7, 26, 27, 0.66);
  border: 1px solid rgba(46, 198, 184, 0.45);
  backdrop-filter: blur(12px);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.28);
  cursor: pointer;
  animation: hintFloat 2.6s ease-in-out infinite;
  transition: opacity 0.35s ease, background 0.18s ease;
}
.interact-hint:hover { background: rgba(46, 198, 184, 0.24); }
.interact-hint:active { animation: none; transform: translateX(-50%) scale(0.97); }
.interact-hint[hidden] { display: none; }
/* 用户已经互动过（提问）后淡出，避免长期占位 */
.interact-hint.is-gone { opacity: 0; pointer-events: none; }
.interact-hint-dot {
  width: 7px;
  height: 7px;
  flex: none;
  border-radius: 50%;
  background: var(--teal-bright);
  animation: hintPulse 1.8s ease-out infinite;
}
@keyframes hintFloat {
  0%, 100% { transform: translateX(-50%) translateY(0); }
  50% { transform: translateX(-50%) translateY(-4px); }
}
@keyframes hintPulse {
  0% { box-shadow: 0 0 0 0 rgba(46, 198, 184, 0.6); }
  100% { box-shadow: 0 0 0 9px rgba(46, 198, 184, 0); }
}

/* ---------- 状态条 ---------- */
.status-chip {
  position: absolute;
  bottom: 14px;
  left: 50%;
  transform: translateX(-50%);
  /* 高于人物视频，避免被挡住 */
  z-index: 5;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 7px 15px;
  border-radius: 999px;
  background: rgba(7, 26, 27, 0.62);
  border: 1px solid var(--line);
  backdrop-filter: blur(12px);
  font-size: 12.5px;
  color: var(--text-dim);
  white-space: nowrap;
}
.status-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--teal-bright);
  flex: none;
}
.stage[data-state="listening"] .status-dot { animation: blink 1s ease-in-out infinite; }
.stage[data-state="thinking"] .status-dot { background: #ffcc66; animation: blink 1.1s ease-in-out infinite; }
.stage[data-state="speaking"] .status-dot { background: var(--teal-bright); animation: blink 0.7s ease-in-out infinite; }
.stage[data-state="error"] .status-dot { background: var(--danger); }
@keyframes blink { 50% { opacity: 0.25; } }

/* ---------- 声波 ---------- */
.wave {
  position: absolute;
  bottom: 52px;
  left: 50%;
  transform: translateX(-50%);
  /* 高于人物视频，避免被挡住 */
  z-index: 5;
  display: none;
  align-items: flex-end;
  gap: 4px;
  height: 26px;
}
.stage[data-state="listening"] .wave,
.stage[data-state="speaking"] .wave { display: flex; }
.wave i {
  display: block;
  width: 4px;
  height: 6px;
  border-radius: 999px;
  background: var(--teal-bright);
  animation: bounce 0.9s ease-in-out infinite;
}
.wave i:nth-child(2) { animation-delay: 0.1s; }
.wave i:nth-child(3) { animation-delay: 0.2s; }
.wave i:nth-child(4) { animation-delay: 0.3s; }
.wave i:nth-child(5) { animation-delay: 0.2s; }
.wave i:nth-child(6) { animation-delay: 0.1s; }
.wave i:nth-child(7) { animation-delay: 0.05s; }
.stage[data-state="listening"] .wave i { background: var(--teal-bright); }
.stage[data-state="speaking"] .wave i { background: rgba(234, 246, 245, 0.85); }
/* 真实音量驱动：--level 由 JS 实时写入 */
.stage[data-state="listening"] .wave i {
  height: calc(6px + var(--level, 0) * 20px);
  animation: none;
  transition: height 0.08s linear;
}
@keyframes bounce {
  0%, 100% { height: 6px; }
  50% { height: 22px; }
}

/* ---------- 字幕 ---------- */
.caption {
  position: absolute;
  left: 50%;
  bottom: 86px;
  transform: translateX(-50%);
  width: min(660px, 90vw);
  padding: 14px 18px;
  border-radius: var(--radius);
  background: rgba(7, 26, 27, 0.72);
  border: 1px solid rgba(46, 198, 184, 0.28);
  backdrop-filter: blur(14px);
  box-shadow: 0 18px 44px rgba(0, 0, 0, 0.35);
  animation: capIn 0.22s ease-out;
  /* 必须高于人物视频（.figure video 为 z-index: 2），否则会被人物挡住 */
  z-index: 6;
}
@keyframes capIn { from { opacity: 0; transform: translateX(-50%) translateY(8px); } }
.caption-who {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  color: var(--teal-bright);
  letter-spacing: 0.04em;
  margin-bottom: 5px;
}
.caption p {
  margin: 0;
  font-size: 15px;
  line-height: 1.68;
  white-space: pre-wrap;
  max-height: 7.2em;
  overflow: auto;
  scrollbar-width: thin;
}
/* 来源标签与播报控制排在同一行，避免和字幕重叠 */
.caption-foot {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 10px;
}
.caption-foot:empty { display: none; }
.caption-src {
  display: inline-block;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 10.5px;
  font-weight: 700;
  background: rgba(46, 198, 184, 0.16);
  color: var(--teal-bright);
  border: 1px solid rgba(46, 198, 184, 0.32);
}

/* 停止播报 / 重播：作为字幕条内的控件，始终显示在人物之前 */
.speak-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
  padding: 6px 12px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 700;
  color: var(--text);
  background: rgba(255, 255, 255, 0.1);
  border: 1px solid rgba(255, 255, 255, 0.22);
  transition: background 0.18s ease, border-color 0.18s ease;
}
.speak-btn:hover { background: rgba(255, 107, 107, 0.24); border-color: rgba(255, 107, 107, 0.5); }
.speak-btn[hidden] { display: none; }
.speak-icon {
  width: 9px;
  height: 9px;
  flex: none;
  background: currentColor;
}
.speak-icon.stop { border-radius: 2px; }
.speak-icon.replay {
  width: 0;
  height: 0;
  background: none;
  border-style: solid;
  border-width: 5px 0 5px 8px;
  border-color: transparent transparent transparent currentColor;
}

/* ---------- 底部控制台 ---------- */
.dock {
  position: relative;
  z-index: 4;
  padding: 12px 16px calc(16px + var(--safe));
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: linear-gradient(180deg, transparent, rgba(7, 32, 31, 0.75) 34%);
}
.chips { display: flex; gap: 8px; overflow-x: auto; padding-bottom: 2px; scrollbar-width: none; }
.chips::-webkit-scrollbar { display: none; }
.chip {
  flex: none;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--line);
  color: var(--text);
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 12.5px;
  white-space: nowrap;
  transition: background 0.18s ease, border-color 0.18s ease;
}
.chip:hover { background: rgba(46, 198, 184, 0.18); border-color: rgba(46, 198, 184, 0.42); }

.compose {
  display: flex;
  align-items: center;
  gap: 10px;
}
.mic {
  width: 46px;
  height: 46px;
  flex: none;
  border-radius: 50%;
  background: var(--teal);
  color: #04211f;
  display: grid;
  place-items: center;
  transition: background 0.18s ease, transform 0.12s ease;
}
.mic svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
.mic:hover { background: var(--teal-bright); }
.mic:active { transform: scale(0.95); }
.mic:disabled { opacity: 0.45; cursor: default; }
.stage[data-state="listening"] .mic {
  background: var(--danger);
  color: #fff;
  box-shadow: 0 0 0 6px rgba(255, 107, 107, 0.18);
}

#input {
  flex: 1;
  min-width: 0;
  height: 46px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
  padding: 0 18px;
  font-size: 14.5px;
  outline: none;
  transition: border-color 0.18s ease, background 0.18s ease;
}
#input::placeholder { color: rgba(157, 184, 183, 0.85); }
#input:focus { border-color: rgba(46, 198, 184, 0.65); background: rgba(255, 255, 255, 0.12); }
#input:disabled { opacity: 0.55; }

.send {
  height: 46px;
  padding: 0 20px;
  flex: none;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.9);
  color: #04211f;
  font-weight: 700;
  font-size: 14px;
  transition: background 0.18s ease, transform 0.12s ease;
}
.send:hover { background: #fff; }
.send:active { transform: scale(0.97); }
.send:disabled { opacity: 0.4; cursor: default; }

.hint {
  margin: 0;
  text-align: center;
  font-size: 11.5px;
  color: var(--text-dim);
  min-height: 1.2em;
}
.stage[data-state="error"] .hint { color: #ffb3b3; }

/* ---------- 对话记录 ---------- */
.log {
  position: absolute;
  z-index: 6;
  top: 0;
  right: 0;
  height: 100%;
  width: min(380px, 92vw);
  background: rgba(7, 26, 27, 0.96);
  border-left: 1px solid var(--line);
  backdrop-filter: blur(18px);
  transform: translateX(102%);
  transition: transform 0.28s cubic-bezier(0.32, 0.72, 0, 1);
  display: flex;
  flex-direction: column;
}
.log.open { transform: translateX(0); }
.log-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px;
  border-bottom: 1px solid var(--line);
  font-size: 14px;
  font-weight: 700;
}
.log-body {
  flex: 1;
  overflow: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scrollbar-width: thin;
}
.log-empty { color: var(--text-dim); font-size: 13px; text-align: center; padding: 28px 12px; line-height: 1.7; }
.msg { max-width: 88%; padding: 11px 14px; border-radius: 14px; font-size: 13.5px; line-height: 1.65; white-space: pre-wrap; word-break: break-word; }
.msg.user { align-self: flex-end; background: var(--teal); color: #04211f; border-bottom-right-radius: 5px; }
.msg.bot { align-self: flex-start; background: rgba(255, 255, 255, 0.09); border: 1px solid var(--line); border-bottom-left-radius: 5px; }
.msg .src { display: inline-block; margin-top: 7px; font-size: 10.5px; color: var(--teal-bright); font-weight: 700; }
.msg.rec { color: var(--text-dim); font-style: italic; }

/* ---------- 窄屏 ---------- */
@media (max-width: 520px) {
  .figure { max-height: 46vh; }
  .figure video, .figure-still, .figure-packed { max-height: 46vh; }
  .caption { bottom: 78px; padding: 12px 14px; }
  .caption p { font-size: 14px; }
  .brand-sub { display: none; }
  .hint { font-size: 11px; }
}

@media (prefers-reduced-motion: reduce) {
  .wave i { animation: none; height: 14px; }
  .figure video, .figure-packed, .halo, .log { transition: none; }
  .caption { animation: none; }
  .interact-hint, .interact-hint-dot { animation: none; }
}
