/* ==========================================================================
   健康助手聊天界面 - iMessage 风格
   - 移动端优先，响应式设计
   - 使用 CSS 变量方便主题定制
   ========================================================================== */

/* 主题变量：类 iMessage 的浅色主题 */
:root {
  --color-bg: #f7f7f7;
  --color-header-bg: rgba(247, 247, 247, 0.85);
  --color-header-border: #d1d1d6;
  --color-text: #1c1c1e;
  --color-muted: #8e8e93;

  --color-user-bubble: #007aff;           /* iOS 蓝 */
  --color-user-bubble-text: #ffffff;

  --color-ai-bubble: #e9e9eb;             /* 灰色气泡 */
  --color-ai-bubble-text: #1c1c1e;

  --color-input-bg: #ffffff;
  --color-input-border: rgba(60, 60, 67, 0.18);

  --color-send-bg: #007aff;
  --color-send-bg-disabled: #c7c7cc;
  --color-send-text: #ffffff;

  --color-ok: #00a870;     /* 成功色（饮食记录入库） */
  --color-fail: #ff3b30;   /* 失败色 */

  --radius-bubble: 20px;
  --radius-input: 22px;

  --shadow-soft: 0 1px 2px rgba(0, 0, 0, 0.04);

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);

  --font-size-base: 16px;
}

/* 深色模式（跟随系统） */
@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #000000;
    --color-header-bg: rgba(22, 22, 23, 0.85);
    --color-header-border: rgba(84, 84, 88, 0.65);
    --color-text: #ffffff;
    --color-muted: #8e8e93;

    --color-ai-bubble: #2c2c2e;
    --color-ai-bubble-text: #ffffff;

    --color-input-bg: #1c1c1e;
    --color-input-border: rgba(84, 84, 88, 0.65);
  }
}

/* ---- Reset 基础样式 ---- */
*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Helvetica Neue",
    "Microsoft YaHei", "Segoe UI", Roboto, Arial, sans-serif;
  font-size: var(--font-size-base);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden; /* 防止整体滚动，由消息区内部滚动 */
}

button {
  font-family: inherit;
  border: none;
  background: transparent;
  cursor: pointer;
  color: inherit;
}

input {
  font-family: inherit;
}

/* ---- 整体布局：顶栏 + 消息区 + 输入栏 ---- */
body {
  display: flex;
  flex-direction: column;
}

/* ==== 顶部导航栏 ==== */
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--color-header-bg);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--color-header-border);
  padding-top: var(--safe-top);
}

.app-header__inner {
  height: 48px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  padding: 0 12px;
}

.app-header__title {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
}

.app-header__action {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  font-size: 14px;
  color: var(--color-user-bubble);
  padding: 6px 8px;
  border-radius: 8px;
}

/* ⭐ 新增：左侧按钮（🍽 今日饮食） */
.app-header__action--left {
  right: auto;
  left: 12px;
  font-size: 18px;
}

.app-header__action:active {
  background: rgba(0, 122, 255, 0.12);
}

/* ==== 消息列表区 ==== */
.chat-list {
  flex: 1 1 auto;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 12px 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* 单条消息（气泡 + 下方的 meta 标签） */
.msg {
  display: flex;
  flex-direction: column;
  max-width: 100%;
}

.msg--user {
  align-items: flex-end;
}

.msg--ai {
  align-items: flex-start;
}

/* ⭐ 新增：AI 消息气泡下方的工具执行小标签 */
.msg__meta {
  margin-top: 4px;
  font-size: 11px;
  color: var(--color-muted);
  padding: 0 4px;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.msg__meta--ok { color: var(--color-ok); }
.msg__meta--fail { color: var(--color-fail); }

.msg__bubble {
  max-width: 80%;
  padding: 10px 14px;
  border-radius: var(--radius-bubble);
  word-wrap: break-word;
  word-break: break-word;
  box-shadow: var(--shadow-soft);
  position: relative;
  animation: bubble-in 0.2s ease-out;
  font-size: 15px;
  line-height: 1.45;
  /* ⭐ 默认完全禁止文本选择，屏蔽 iOS 系统长按菜单 */
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  /* 关键新增：让长按事件不被浏览器手势抢走 */
  touch-action: manipulation;
  transition: filter 0.15s ease;
}

.msg--user .msg__bubble {
  background: var(--color-user-bubble);
  color: var(--color-user-bubble-text);
  border-bottom-right-radius: 6px;
}

.msg--ai .msg__bubble {
  background: var(--color-ai-bubble);
  color: var(--color-ai-bubble-text);
  border-bottom-left-radius: 6px;
}

/* ⭐ 微信风格：长按后气泡高亮（整条消息复制，不再允许部分选择） */
.msg__bubble.is-selected {
  filter: brightness(0.92) saturate(1.1);
}

/* 气泡出现动画 */
@keyframes bubble-in {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* ⭐ 消息内图片缩略（缩小尺寸 + 点击放大） */
.msg__bubble img.msg-image {
  display: block;
  max-width: 180px;
  max-height: 240px;
  border-radius: 10px;
  margin: 4px 0;
  cursor: zoom-in;
  object-fit: cover;
}

/* ==== Markdown 渲染（AI 消息内） ==== */
.msg__content p {
  margin: 0;
}

.msg__content p + p {
  margin-top: 6px;
}

.msg--ai .msg__content a {
  color: var(--color-user-bubble);
}

.msg--user .msg__content a {
  color: #ffffff;
  text-decoration: underline;
}

.msg__content table {
  border-collapse: collapse;
  margin: 6px 0;
  width: 100%;
  font-size: 13px;
}

.msg__content th,
.msg__content td {
  border: 1px solid rgba(127, 127, 127, 0.3);
  padding: 4px 8px;
  text-align: left;
}

.msg__content th {
  background: rgba(127, 127, 127, 0.15);
  font-weight: 600;
}

.msg__content code {
  background: rgba(127, 127, 127, 0.2);
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 0.9em;
}

.msg__content pre {
  background: rgba(0, 0, 0, 0.05);
  padding: 8px 10px;
  border-radius: 8px;
  overflow-x: auto;
}

.msg__content pre code {
  background: transparent;
  padding: 0;
}

.msg__content ul,
.msg__content ol {
  margin: 4px 0 4px 20px;
  padding: 0;
}

/* ==== 打字动画 ==== */
.typing {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  color: var(--color-muted);
  font-size: 14px;
}

.typing__dots {
  display: inline-flex;
  gap: 3px;
}

.typing__dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--color-muted);
  opacity: 0.5;
  animation: typing-bounce 1.2s infinite;
}

.typing__dots span:nth-child(2) {
  animation-delay: 0.15s;
}

.typing__dots span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes typing-bounce {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-4px);
    opacity: 1;
  }
}

/* ==== 底部输入栏 ==== */
.chat-input-bar {
  background: var(--color-header-bg);
  backdrop-filter: saturate(180%) blur(20px);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  border-top: 1px solid var(--color-header-border);
  padding: 8px 10px calc(8px + var(--safe-bottom));
  position: sticky;
  bottom: 0;
  z-index: 10;
  display: flex;
  flex-direction: column;
}

.chat-input-bar__file {
  display: none; /* 隐藏，通过按钮触发 */
}

/* ⭐ 输入行：图片按钮 + 表单（同一行） */
.chat-input-bar__row {
  display: flex;
  align-items: center;
  gap: 6px;
}

.chat-input-bar__icon {
  padding: 8px;
  color: var(--color-user-bubble);
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.chat-input-bar__icon:active {
  background: rgba(0, 122, 255, 0.12);
}

.chat-input-bar__form {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1 1 auto;
  min-width: 0;
}

.chat-input-bar__text {
  flex: 1 1 auto;
  border: 1px solid var(--color-input-border);
  background: var(--color-input-bg);
  color: var(--color-text);
  border-radius: var(--radius-input);
  padding: 10px 16px;
  font-size: 16px;           /* 避免 iOS 聚焦时自动放大 */
  outline: none;
  min-width: 0;
}

.chat-input-bar__text:focus {
  border-color: var(--color-user-bubble);
  box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.15);
}

.chat-input-bar__send {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--color-send-bg);
  color: var(--color-send-text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.1s ease, background 0.2s ease;
  flex: 0 0 auto;
}

.chat-input-bar__send:disabled {
  background: var(--color-send-bg-disabled);
  cursor: not-allowed;
}

.chat-input-bar__send:not(:disabled):active {
  transform: scale(0.92);
}

/* ==== 图片预览（待发送） ==== */
.image-preview {
  position: relative;
  margin-top: 6px;
  padding-left: 4px;
}

.image-preview img {
  max-height: 90px;
  border-radius: 12px;
  display: block;
}

.image-preview__remove {
  position: absolute;
  top: -6px;
  left: 72px;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  font-size: 16px;
  line-height: 22px;
  padding: 0;
}

/* ==== 响应式：桌面端限制最大宽度 ==== */
@media (min-width: 640px) {
  body {
    background: #e5e5ea;
    align-items: center;
  }

  .app-header,
  .chat-list,
  .chat-input-bar {
    width: 100%;
    max-width: 480px;
  }

  .app-header {
    border-left: 1px solid var(--color-header-border);
    border-right: 1px solid var(--color-header-border);
  }

  .chat-input-bar {
    border-left: 1px solid var(--color-header-border);
    border-right: 1px solid var(--color-header-border);
  }

  .chat-list {
    background: var(--color-bg);
  }
}

/* ============================================================
   环境标识（env-badge）
   ============================================================ */
.env-badge {
  display: inline-block;
  padding: 3px 10px;
  margin: 0 8px;
  font-size: 12px;
  font-weight: 500;
  color: #fff;
  background-color: #00a870; /* 默认生产环境：绿色 */
  border-radius: 10px;
  cursor: pointer;
  user-select: none;
  letter-spacing: 0.5px;
  opacity: 0.9;
  transition: opacity 0.2s ease;
}

.env-badge:hover {
  opacity: 1;
}

.env-badge::before {
  content: '';
  display: inline-block;
  width: 6px;
  height: 6px;
  margin-right: 6px;
  background: #fff;
  border-radius: 50%;
  vertical-align: middle;
}

/* ============================================================
   ⭐ 新增：今日饮食弹层
   ============================================================ */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal[hidden] {
  display: none;
}

.modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(2px);
}

.modal__card {
  position: relative;
  width: 100%;
  max-width: 100%;
  max-height: 80vh;
  overflow: hidden;
  background: var(--color-input-bg);
  border-radius: 16px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.25);
  display: flex;
  flex-direction: column;
}

.modal__header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--color-input-border);
}

.modal__title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
}

.modal__close {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: rgba(127, 127, 127, 0.15);
  font-size: 18px;
  line-height: 28px;
  padding: 0;
}

.modal__body {
  padding: 14px 16px;
  overflow-y: auto;
  font-size: 14px;
}

/* 饮食统计块 */
.food-stat {
  display: flex;
  gap: 12px;
  margin-bottom: 12px;
  padding: 12px;
  border-radius: 10px;
  background: rgba(0, 168, 112, 0.08);
}

.food-stat--over {
  background: rgba(255, 59, 48, 0.08);
}

.food-stat__item {
  flex: 1;
  text-align: center;
}

.food-stat__num {
  font-size: 18px;
  font-weight: 600;
}

.food-stat__label {
  font-size: 11px;
  color: var(--color-muted);
  margin-top: 2px;
}

/* 单条饮食记录 */
.food-log-item {
  padding: 10px 0;
  border-bottom: 1px solid var(--color-input-border);
}

.food-log-item:last-child {
  border-bottom: none;
}

.food-log-item__meal {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 6px;
  background: var(--color-user-bubble);
  color: #fff;
  font-size: 11px;
  margin-right: 8px;
}

.food-log-item__cal {
  float: right;
  font-weight: 600;
  color: var(--color-user-bubble);
}

.food-log-item__list {
  margin-top: 6px;
  padding-left: 0;
  font-size: 13px;
  color: var(--color-muted);
  list-style: none;
}

.food-log-item__list li {
  padding: 2px 0;
}

/* 桌面端弹层宽度限制 */
@media (min-width: 640px) {
  .modal__card {
    max-width: 460px;
  }
}

/* ============================================================
   ⭐ 新增：引用块 + 引用栏 + 长按菜单 + 图片查看器
   ============================================================ */

/* ---- 1. 引用块（消息气泡内，引用回复的原始消息） ---- */
.msg__quote {
  border-left: 3px solid rgba(127, 127, 127, 0.5);
  padding: 6px 8px 6px 10px;
  margin-bottom: 6px;
  font-size: 12px;
  color: var(--color-muted);
  background: rgba(0, 0, 0, 0.08);
  border-radius: 0 6px 6px 0;
}

.msg__quote-role {
  font-weight: 600;
  display: block;
  margin-bottom: 2px;
  opacity: 0.8;
}

.msg__quote-text {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---- 2. 引用栏（输入框上方，显示当前引用的消息） ---- */
.quote-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  margin-bottom: 6px;
  background: var(--color-header-bg);
  border-radius: 10px;
  font-size: 13px;
  animation: quote-in 0.2s ease-out;
  width: 100%;
  box-sizing: border-box;
}

@keyframes quote-in {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: translateY(0); }
}

.quote-bar__body {
  flex: 1;
  min-width: 0;
  border-left: 3px solid var(--color-primary);
  padding-left: 10px;
}

.quote-bar__role {
  font-weight: 600;
  font-size: 12px;
  color: var(--color-primary);
  display: block;
}

.quote-bar__text {
  color: var(--color-text);
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 13px;
  margin-top: 2px;
}

.quote-bar__close {
  flex-shrink: 0;
  background: transparent;
  border: none;
  color: var(--color-muted);
  font-size: 16px;
  padding: 4px 8px;
  cursor: pointer;
  border-radius: 4px;
}

.quote-bar__close:hover { background: rgba(127, 127, 127, 0.15); }

/* ---- 3. 长按消息操作菜单（白色圆角卡片，显示在消息附近） ---- */
.msg-action-menu {
  position: fixed;
  z-index: 10000;
  min-width: 180px;
  background: #ffffff;
  border-radius: 14px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15), 0 2px 12px rgba(0, 0, 0, 0.1);
  padding: 6px;
  animation: menu-pop-in 0.16s cubic-bezier(0.3, 0, 0.3, 1.2);
}

@keyframes menu-pop-in {
  from { opacity: 0; transform: scale(0.85) translateY(-4px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.msg-action-menu__btn {
  display: flex;
  align-items: center;
  width: 100%;
  padding: 12px 14px;
  background: transparent;
  border: none;
  border-radius: 10px;
  font-size: 15px;
  color: #1f1f1f;
  cursor: pointer;
  gap: 12px;
  transition: background 0.15s;
}

.msg-action-menu__btn:hover {
  background: #8e8e93;
  color: #ffffff;
}

.msg-action-menu__btn:active {
  background: #636366;
  color: #ffffff;
}

.msg-action-menu__btn-icon {
  font-size: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 24px;
}



/* ---- 3.2 顶部：一键清除缓存按钮 ---- */
.app-header__action--clear-cache {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 6px 10px;
  background: rgba(255, 59, 48, 0.1);
  color: #ff3b30;
  border: 1px solid rgba(255, 59, 48, 0.3);
  border-radius: 8px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
  margin-left: 8px;
  gap: 4px;
}

.app-header__action--clear-cache:hover {
  background: rgba(255, 59, 48, 0.2);
}

.app-header__action--clear-cache:active {
  background: rgba(255, 59, 48, 0.3);
  transform: scale(0.96);
}

/* ---- 4. 图片放大查看器 ---- */
.image-viewer {
  position: fixed;
  inset: 0;
  z-index: 10001;
  background: rgba(0, 0, 0, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  animation: viewer-in 0.2s ease-out;
  cursor: zoom-out;
}

@keyframes viewer-in {
  from { opacity: 0; }
  to { opacity: 1; }
}

.image-viewer__img {
  max-width: 92vw;
  max-height: 92vh;
  border-radius: 8px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.5);
  cursor: zoom-out;
}

.image-viewer__close {
  position: absolute;
  top: 20px;
  right: 20px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.15);
  color: #fff;
  border: none;
  font-size: 20px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

.image-viewer__close:hover {
  background: rgba(255, 255, 255, 0.25);
}

/* ---- 5. 深色模式下的引用栏/菜单 颜色调整 ---- */
@media (prefers-color-scheme: dark) {
  .msg__quote {
    background: rgba(255, 255, 255, 0.08);
    color: rgba(255, 255, 255, 0.6);
  }
  .msg-action-menu {
    background: #1c1c1e;
  }
  .msg-action-menu__btn {
    color: #fff;
  }
}

/* ---- 6. 复制成功 Toast（微信风格反馈）---- */
.copy-toast {
  position: fixed;
  left: 50%;
  bottom: 30%;
  transform: translateX(-50%);
  z-index: 10001;
  background: rgba(0, 0, 0, 0.75);
  color: #fff;
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 14px;
  pointer-events: none;
  animation: toast-fade 1.6s ease forwards;
  white-space: nowrap;
}

@keyframes toast-fade {
  0%   { opacity: 0; transform: translate(-50%, 8px); }
  15%  { opacity: 1; transform: translate(-50%, 0); }
  80%  { opacity: 1; transform: translate(-50%, 0); }
  100% { opacity: 0; transform: translate(-50%, -8px); }
}
