/* Mental Massage Avatar — 阶段 5 完整样式
 * 双栏布局 + 6 表情 + 流式渲染 + 量表浮窗 + 弹窗
 */

/* ===== Reset ===== */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Helvetica Neue", "PingFang SC",
               "Microsoft YaHei", "Hiragino Sans GB", Arial, sans-serif;
  font-size: 15px;
  line-height: 1.6;
  color: #2a2a2a;
  background: #faf8f5;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea { font: inherit; color: inherit; }

/* ===== 双栏布局 ===== */
/* 整页锁定视口高度：消息区在容器内滚动，输入框始终固定可见，
   避免对话内容把页面撑高、压到屏幕以下 */
.app {
  display: grid;
  grid-template-columns: minmax(280px, 30%) 1fr;
  height: 100vh;
  overflow: hidden;
}
@media (max-width: 1023px) {
  .app { grid-template-columns: 1fr; grid-template-rows: 240px minmax(0, 1fr); }
}

/* 左侧 */
.sidebar {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  padding: 24px 16px;
  background: #faf8f5;
  border-right: 1px solid #ece9e2;
  height: 100vh;
  min-height: 0;
  overflow: hidden;
}
@media (max-width: 1023px) {
  .sidebar { height: auto; min-height: 0; }
}
.avatar-wrap {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 0;
}
.avatar-wrap object,
.avatar-wrap img {
  max-height: 72vh;
  max-width: 96%;
  height: auto;
  width: auto;
  aspect-ratio: 2 / 3;
  transition: opacity 400ms ease;
}

/* ============ SVG 六状态动画立绘（spec 04 §1.3/1.4） ============
   基准姿态（Anchor Pose）：双手腹部前轻握、身体直立、神情平静。
   强制约束：所有 @keyframes 的 0% 与 100% 完全相同（identity），
   保证任意动效叠加/切换/移除时视觉连续、无跳变。 */
.avatar-stage {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  max-height: 72vh;
  aspect-ratio: 3 / 4;
  transition: opacity 300ms ease;
}
.avatar-stage.avatar-fade { opacity: 0; }
.avatar-stage svg {
  height: 100%;
  width: auto;
  max-width: 80%;
}

/* 状态表情组：默认隐藏，当前状态 400ms 淡入（spec §1.4 300~500ms） */
.avatar-stage .state-group {
  opacity: 0;
  transition: opacity 400ms ease;
}
.avatar-stage.state-S1 .state-S1,
.avatar-stage.state-S2 .state-S2,
.avatar-stage.state-S3 .state-S3,
.avatar-stage.state-S4 .state-S4,
.avatar-stage.state-S5 .state-S5,
.avatar-stage.state-S6 .state-S6 { opacity: 1; }

/* 动画分层：每层独立、均以 identity 为基准，可叠加可单独移除 */
.avatar-stage .av-whole,
.avatar-stage .av-body,
.avatar-stage .av-head,
.avatar-stage .av-mouth,
.avatar-stage .av-eyelids {
  transform-box: fill-box;
}
.avatar-stage .av-whole,
.avatar-stage .av-body { transform-origin: 50% 90%; }
.avatar-stage .av-head { transform-origin: 50% 95%; }
.avatar-stage .av-eyelids { transform-origin: 50% 50%; }
.avatar-stage .av-mouth { transform-origin: 50% 50%; }

/* 各状态微动效（仅激活时运行，首尾帧均为 identity） */
.avatar-stage.state-S6 .av-whole { animation: av-breathe 3.5s ease-in-out infinite; }
.avatar-stage.state-S1 .av-head  { animation: av-nod 2.4s ease-in-out infinite; }
.avatar-stage.state-S1 .av-whole { animation: av-breathe 3.5s ease-in-out infinite; }
.avatar-stage.state-S2 .av-whole { animation: av-breathe-slow 4.5s ease-in-out infinite; }
.avatar-stage.state-S2 .av-body  { animation: av-shoulder-dip 4.5s ease-in-out infinite; }
.avatar-stage.state-S3 .av-head  { animation: av-tilt 3s ease-in-out infinite; }
.avatar-stage.state-S4 .av-whole { animation: av-bounce 1.6s ease-in-out infinite; }
.avatar-stage.state-S5 .av-body  { animation: av-lean 5s ease-in-out infinite; }

/* 眨眼：全状态共用独立层，与任意状态动画叠加不冲突；S4 月牙眼不适用 */
.avatar-stage .av-eyelids { animation: av-blink 4.6s linear infinite; }
.avatar-stage.state-S4 .av-eyelids { animation: none; }

@keyframes av-breathe {
  0%   { transform: translate(0, 0); }
  50%  { transform: translate(0, -4px); }
  100% { transform: translate(0, 0); }
}
@keyframes av-breathe-slow {
  0%   { transform: translate(0, 0); }
  50%  { transform: translate(0, -3px); }
  100% { transform: translate(0, 0); }
}
@keyframes av-nod {
  0%   { transform: rotate(0deg); }
  40%  { transform: rotate(2deg); }
  100% { transform: rotate(0deg); }
}
@keyframes av-tilt {
  0%   { transform: rotate(0deg); }
  25%  { transform: rotate(-1.5deg); }
  75%  { transform: rotate(1.5deg); }
  100% { transform: rotate(0deg); }
}
@keyframes av-bounce {
  0%   { transform: translate(0, 0); }
  50%  { transform: translate(0, -7px); }
  100% { transform: translate(0, 0); }
}
@keyframes av-lean {
  0%   { transform: rotate(0deg) translate(0, 0); }
  30%  { transform: rotate(2deg) translate(0, 2px); }
  70%  { transform: rotate(2deg) translate(0, 2px); }
  100% { transform: rotate(0deg) translate(0, 0); }
}
@keyframes av-shoulder-dip {
  0%   { transform: translate(0, 0); }
  50%  { transform: translate(0, 2px); }
  100% { transform: translate(0, 0); }
}
/* 眼睑默认 scaleY(0) 不可见；仅眨眼瞬间压合，首尾帧一致 */
@keyframes av-blink {
  0%   { transform: scaleY(0); }
  90%  { transform: scaleY(0); }
  94%  { transform: scaleY(1); }
  98%  { transform: scaleY(0); }
  100% { transform: scaleY(0); }
}

/* 说话视觉（ENH-3）：语音播报期间说话层开合，覆盖各状态静态嘴型；
   JS 在 speakReply/stopVoice 时增删 #avatar.speaking，暂停时增删 .voice-paused */
.avatar-stage .av-mouth { opacity: 0; transition: opacity 150ms ease; }
.avatar-stage.speaking .av-mouth {
  opacity: 1;
  animation: av-talk 0.55s ease-in-out infinite;
}
.avatar-stage.speaking.voice-paused .av-mouth {
  animation-play-state: paused;
}
/* 首尾帧一致：0% 与 100% 均为同一开度 */
@keyframes av-talk {
  0%   { transform: scaleY(0.45); }
  35%  { transform: scaleY(1); }
  65%  { transform: scaleY(0.6); }
  85%  { transform: scaleY(0.85); }
  100% { transform: scaleY(0.45); }
}

/* 无障碍：停用所有循环动画，静止于基准姿态 */
@media (prefers-reduced-motion: reduce) {
  .avatar-stage .av-whole,
  .avatar-stage .av-body,
  .avatar-stage .av-head,
  .avatar-stage .av-mouth,
  .avatar-stage .av-eyelids,
  .avatar-stage img.av-figure { animation: none !important; }
}

/* ============ ENH-6：resource/avatars PNG 立绘（吉卜力风全身形象） ============
   整图驱动状态微动效（复用上方 keyframes，首尾帧均为 identity）；
   白底以 multiply 融入侧栏背景色，无矩形边界。 */
.avatar-stage img.av-figure {
  height: 100%;
  width: auto;
  max-width: 100%;
  object-fit: contain;
  mix-blend-mode: multiply;
  transform-origin: 50% 90%;
}
.avatar-stage.state-S6 img.av-figure { animation: av-breathe 3.5s ease-in-out infinite; }
.avatar-stage.state-S1 img.av-figure { animation: av-nod 2.4s ease-in-out infinite; }
.avatar-stage.state-S2 img.av-figure { animation: av-breathe-slow 4.5s ease-in-out infinite; }
.avatar-stage.state-S3 img.av-figure { animation: av-tilt 3s ease-in-out infinite; }
.avatar-stage.state-S4 img.av-figure { animation: av-bounce 1.6s ease-in-out infinite; }
.avatar-stage.state-S5 img.av-figure { animation: av-lean 5s ease-in-out infinite; }

/* 说话视觉（ENH-6）：PNG 无口型层，播报期间改为轻快点头节奏；
   优先级高于状态动效（规则后置），暂停时冻结 */
.avatar-stage.speaking img.av-figure {
  animation: av-talk-bob 0.55s ease-in-out infinite;
}
.avatar-stage.speaking.voice-paused img.av-figure {
  animation-play-state: paused;
}
@keyframes av-talk-bob {
  0%   { transform: translate(0, 0); }
  30%  { transform: translate(0, -3px); }
  60%  { transform: translate(0, -1px); }
  100% { transform: translate(0, 0); }
}

.consultant-meta { text-align: center; margin: 16px 0; }
.consultant-name-row {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.consultant-name { font-size: 20px; font-weight: 600; color: #2a2a2a; }

/* 语音播报开关（顾问名右侧）：单色线性图标，四状态由 CSS 类驱动切换 */
.voice-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  padding: 0;
  background: transparent;
  border: none;
  border-radius: 8px;
  color: #9e968a;
  cursor: pointer;
  position: relative;
}
.voice-btn:hover { background: #f0ebe2; color: #5a4d3f; }
.voice-btn svg { display: none; width: 18px; height: 18px; }
.voice-btn.voice-off .ico-voice-off,
.voice-btn.voice-on .ico-voice-on,
.bgm-btn .ico-bgm-off { display: block; }
.bgm-btn.bgm-on .ico-bgm-off { display: none; }
.bgm-btn.bgm-on .ico-bgm-on { display: block; }
.voice-btn.voice-on,
.bgm-btn.bgm-on { color: #c8a86b; }

/* 快捷文字提示：hover 即时显示（无延迟），供图标按钮使用 */
[data-tip] { position: relative; }
[data-tip]::after {
  content: attr(data-tip);
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: #5a4d3f;
  color: #fff;
  font-size: 12px;
  line-height: 1.4;
  padding: 4px 10px;
  border-radius: 8px;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  z-index: 70;
}
[data-tip]:hover::after { opacity: 1; }
.send-btn[data-tip]::after { left: auto; right: 0; transform: none; }
.persona-tag { font-size: 13px; color: #7a736a; margin-top: 6px; }

/* 右侧 */
.chat-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100vh;
  min-height: 0;
  background: #ffffff;
}
@media (max-width: 1023px) {
  .chat-panel { height: auto; }
}
.chat-header { padding: 20px 28px 12px; border-bottom: 1px solid #ece9e2; }
.chat-header h1 { margin: 0; font-size: 22px; font-weight: 600; }
.chat-header .subtitle { margin: 4px 0 0; font-size: 13px; color: #7a736a; }

/* ===== 品牌区：愈*考拉 heal * koala（官网同款水彩考拉 logo） ===== */
.chat-header .brand { display: flex; align-items: center; gap: 12px; }
.brand-logo {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  object-fit: cover;
  object-position: 50% 10%;   /* 聚焦考拉头部 */
  border: 1px solid #ece9e2;
  background: #f9f5f0;
  flex-shrink: 0;
}
.brand-en { margin-left: 10px; font-size: 12px; font-weight: 400; color: #a89f93; letter-spacing: 1px; }
/* 浮层内的 logo：标题上方，与左对齐标题对齐 */
.brand-logo-modal { display: block; width: 68px; height: 68px; margin: 0 0 12px; border-radius: 14px; }

.messages {
  padding: 20px 28px;
  overflow-y: auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.bubble {
  max-width: min(70%, 840px);   /* 行宽 ≤ 60 个中文字符（≈ 840px @14px） */
  padding: 12px 16px;
  border-radius: 14px;
  font-size: 14px;
  /* TC-IX-009：英文单词不折行（整体换行），仅超长单词才允许断行兜底 */
  word-break: normal;
  overflow-wrap: break-word;
  line-break: auto;
}
.bubble-meta { font-size: 11px; color: #9e968a; margin-bottom: 6px; }
.bubble p { margin: 0; white-space: pre-wrap; }
.bubble-assistant {
  align-self: flex-start;
  background: #ffffff;
  color: #2a2a2a;
  border: 1px solid #ece9e2;
}
.bubble-user {
  align-self: flex-end;
  background: #fdf3e1;
  color: #2a2a2a;
  border: 1px solid #f4e5c8;
}
/* 思考中指示：等待回复生成期间的反馈气泡 */
.bubble-thinking { opacity: 0.9; }
.thinking-text { color: #7a736a; font-size: 13px; }
.thinking-dot {
  display: inline-block;
  width: 5px;
  height: 5px;
  margin-left: 4px;
  border-radius: 50%;
  background: #b3a995;
  vertical-align: middle;
  animation: thinking-bounce 1.2s ease-in-out infinite;
}
.thinking-dot:nth-child(2) { animation-delay: 0.15s; }
.thinking-dot:nth-child(3) { animation-delay: 0.3s; }
@keyframes thinking-bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.5; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* 输入区：textarea + 麦克风同在一个圆角容器内（视觉融合），发送按钮等高并列 */
.input-bar {
  display: flex;
  align-items: stretch;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid #ece9e2;
  background: #ffffff;
}
.input-wrap {
  flex: 1;
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 7px 8px 7px 14px;
  border: 1px solid #ece9e2;
  border-radius: 12px;
  background: #faf8f5;
}
.input-wrap:focus-within { border-color: #c8a86b; background: #fff; }
.input-wrap textarea {
  flex: 1;
  resize: none;
  padding: 4px 0;
  border: none;
  background: transparent;
  min-height: 34px;
  max-height: 120px;
  outline: none;
}
/* 图标按钮（麦克风）：单色线性图标，无拟物 */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  flex: none;
  padding: 0;
  background: transparent;
  border: none;
  border-radius: 10px;
  color: #7a736a;
  cursor: pointer;
}
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn:hover { background: #f0ebe2; color: #5a4d3f; }
.icon-btn.recording {
  color: #b0413e;
  animation: mic-pulse 1.2s ease-in-out infinite;
}
@keyframes mic-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.12); }
}
/* 发送按钮：与输入容器等高（align-items: stretch），深色底 + 白色箭头 */
.send-btn {
  width: 48px;
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #5a4d3f;
  color: #fff;
  border: none;
  border-radius: 12px;
  cursor: pointer;
}
.send-btn svg { width: 20px; height: 20px; }
.send-btn:hover { background: #4a3f33; }
.send-btn:disabled { background: #d8d2c5; cursor: not-allowed; }

.toast {
  position: fixed;
  right: 20px;
  bottom: 96px;
  background: #5a4d3f;
  color: #fff;
  padding: 10px 16px;
  border-radius: 10px;
  font-size: 13px;
  z-index: 60;
}

/* ===== 弹窗 ===== */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-backdrop[hidden] { display: none; }
.modal-card {
  background: #fff;
  border-radius: 16px;
  padding: 28px;
  max-width: 480px;
  width: calc(100% - 40px);
  box-shadow: 0 12px 40px rgba(0,0,0,0.15);
}
.modal-card h2 { margin: 0 0 12px; font-size: 22px; }
.modal-card h3 { margin: 0 0 8px; font-size: 18px; }
.modal-card ul { padding-left: 20px; color: #5a4d3f; }
.modal-card p { color: #5a4d3f; }
.modal-actions {
  margin-top: 20px;
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
.btn-primary {
  padding: 10px 20px;
  background: #c8a86b;
  color: #fff;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  font-weight: 500;
}
.btn-secondary {
  padding: 10px 20px;
  background: #f0ebe2;
  color: #5a3a22;
  border: none;
  border-radius: 10px;
  cursor: pointer;
}
.btn-link {
  padding: 10px 14px;
  background: transparent;
  color: #7a736a;
  border: none;
  cursor: pointer;
  text-decoration: underline;
}

/* ===== 量表浮窗（TC-IX-012） ===== */
.scale-modal {
  position: fixed;
  right: 16px;
  bottom: 80px;
  width: min(420px, 80%);
  max-height: 60vh;
  overflow-y: auto;
  background: #fff;
  border: 1px solid #ece9e2;
  border-radius: 14px;
  padding: 20px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.1);
  z-index: 50;
  animation: scale-slide-in 350ms ease;
}
.scale-modal[hidden] { display: none; }
@keyframes scale-slide-in {
  from { transform: translateY(24px); opacity: 0; }
  to   { transform: translateY(0); opacity: 1; }
}
.scale-head { display: flex; align-items: center; justify-content: space-between; }
.scale-head h4 { margin: 0; font-size: 16px; }
.scale-desc { font-size: 12px; color: #7a736a; margin: 6px 0 12px; }
.scale-item { margin-bottom: 12px; }
.scale-item p { margin: 0 0 6px; font-size: 13px; }
.scale-options { display: flex; gap: 6px; }
.scale-opt {
  padding: 5px 14px;
  border: 1px solid #ece9e2;
  background: #faf8f5;
  border-radius: 8px;
  font-size: 12px;
  cursor: pointer;
}
.scale-opt.selected { background: #c8a86b; color: #fff; border-color: #c8a86b; }
.scale-actions { margin-top: 8px; text-align: right; }
.scale-result {
  margin-top: 12px;
  padding: 10px 12px;
  background: #fdf3e1;
  border-radius: 10px;
  font-size: 13px;
  color: #5a3a22;
}

/* ===== 首次顾问选择卡片（TC-CS-001） ===== */
.modal-card-wide { max-width: 760px; }
.pick-cards {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  margin-top: 12px;
}
.pick-card {
  text-align: left;
  padding: 14px;
  border: 1px solid #ece9e2;
  border-radius: 12px;
  background: #faf8f5;
  cursor: pointer;
  transition: border-color 200ms ease, background 200ms ease;
}
.pick-card:hover { border-color: #c8a86b; background: #fff; }
.pick-name { font-size: 16px; font-weight: 600; margin-bottom: 4px; }
.pick-tags { font-size: 11px; color: #c8a86b; margin-bottom: 6px; }
.pick-tagline { font-size: 13px; color: #5a4d3f; margin-bottom: 6px; }
.pick-opener { font-size: 12px; color: #9e968a; font-style: italic; }
@media (max-width: 640px) {
  .pick-cards { grid-template-columns: 1fr; }
}

/* ===== ENH-7：Token 登录/注册浮层 ===== */
.auth-backdrop { z-index: 300; background: rgba(0,0,0,0.55); }
.auth-backdrop .modal-card { max-width: 420px; }
.auth-sub { color: #5a4d3f; font-size: 14px; margin: 8px 0 16px; }
.auth-input {
  display: block;
  width: 100%;
  box-sizing: border-box;
  padding: 12px 14px;
  border: 1px solid #d8d2c5;
  border-radius: 10px;
  font-size: 15px;
  font-family: "Consolas", "Courier New", monospace;
  letter-spacing: 0.5px;
  background: #faf8f5;
  margin-bottom: 12px;
}
.auth-input:focus { outline: none; border-color: #c8a86b; background: #fff; }
.auth-error { color: #b0413e; font-size: 13px; margin: 0 0 12px; }
.auth-link-row { font-size: 14px; color: #5a4d3f; margin-top: 16px; }
.auth-terms {
  list-style: disc;
  padding-left: 20px;
  color: #5a4d3f;
  font-size: 13px;
  line-height: 1.7;
  margin: 12px 0 16px;
}
.auth-agree { display: flex; align-items: center; gap: 8px; font-size: 14px; color: #5a4d3f; margin-bottom: 16px; cursor: pointer; }
.auth-token-display {
  font-family: "Consolas", "Courier New", monospace;
  font-size: 20px;
  letter-spacing: 1px;
  background: #faf8f5;
  border: 1px dashed #c8a86b;
  border-radius: 10px;
  padding: 16px;
  text-align: center;
  margin: 12px 0;
  word-break: break-all;
  user-select: all;
}
.auth-backdrop .btn-primary:disabled { background: #d8d2c5; cursor: not-allowed; }
