/* ==========================================================================
   全站浮动 AI 助手
   --------------------------------------------------------------------------
   只用模板的 token 别名（--base / --accent / --t-* / --st-*）。深色模式是
   属性驱动的（:root[color-scheme=dark] 由 app.min.js 切换并重映射这些别名），
   所以这里一条 [color-scheme] 规则都不用写，主题自动跟随。

   注意 html 的 font-size 是 62.5%，即 1rem = 10px。
   ========================================================================== */

:root {
  /* 高于 header(200)、loader(1000)、光标画布(999)，
     但**故意低于图库灯箱(9999)** —— 灯箱打开时应该压在助手上面。 */
  --hope-chat-z: 9000;
  --hope-chat-radius: 1.6rem;
}

/* --------------------------------------------------------------------------
   悬浮按钮
   -------------------------------------------------------------------------- */

.hope-chat-fab {
  position: fixed;
  right: 2.4rem;
  bottom: 2.4rem;
  z-index: var(--hope-chat-z);
  display: flex;
  align-items: center;
  justify-content: center;
  width: 5.6rem;
  height: 5.6rem;
  padding: 0;
  border: 1px solid var(--st-medium);
  border-radius: 50%;
  background: var(--base-tint);
  color: var(--t-bright);
  cursor: pointer;
  transition:
    transform var(--_animspeed-medium) var(--_animbezier),
    border-color var(--_animspeed-medium) ease,
    background-color var(--_animspeed-medium) ease;
}

.hope-chat-fab:hover {
  transform: translateY(-0.3rem);
  border-color: var(--accent);
  color: var(--accent);
}

.hope-chat-fab:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
}

.hope-chat-fab__icon {
  width: 2.4rem;
  height: 2.4rem;
}

/* 打开状态下按钮变成「关闭」图标 */
.hope-chat-fab .hope-chat-fab__icon.is-close,
.hope-chat-fab[aria-expanded='true'] .hope-chat-fab__icon.is-open {
  display: none;
}

.hope-chat-fab[aria-expanded='true'] .hope-chat-fab__icon.is-close {
  display: block;
}

/* --------------------------------------------------------------------------
   面板
   -------------------------------------------------------------------------- */

.hope-chat {
  position: fixed;
  right: 2.4rem;
  bottom: 9.2rem;
  z-index: var(--hope-chat-z);
  display: flex;
  flex-direction: column;
  width: min(40rem, calc(100vw - 4.8rem));
  height: min(60rem, calc(100dvh - 14rem));
  overflow: hidden;
  border: 1px solid var(--st-medium);
  border-radius: var(--hope-chat-radius);
  background: var(--base);
  box-shadow: 0 2.4rem 6.4rem rgba(0, 0, 0, 0.28);
  font-family: var(--_font-default);
  font-size: 1.4rem;
  line-height: 1.6;
  color: var(--t-bright);
}

/* 这一行是必须的：上面的 display:flex 会盖过 UA 默认的 [hidden]{display:none}。
   漏了它面板就永久可见 —— gallery.css:521 需要的是同一行。 */
.hope-chat[hidden] {
  display: none;
}

/* 入场动画。reduced-motion 由 hope.css 全局兜底。 */
.hope-chat.is-entering {
  animation: hope-chat-in var(--_animspeed-medium) var(--_animbezier);
}

@keyframes hope-chat-in {
  from { opacity: 0; transform: translateY(1.2rem) scale(0.98); }
  to { opacity: 1; transform: none; }
}

/* --- 头部 --- */

.hope-chat__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1.2rem;
  padding: 1.2rem 1.2rem 1.2rem 1.6rem;
  border-bottom: 1px solid var(--st-muted);
  background: var(--base-tint);
}

.hope-chat__title {
  display: flex;
  align-items: center;
  gap: 0.8rem;
  font-family: var(--_font-accent);
  font-size: 1.2rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--t-medium);
}

.hope-chat__dot {
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 50%;
  background: var(--accent);
}

.hope-chat__head-tools {
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.hope-chat__model select {
  max-width: 14rem;
  padding: 0.4rem 0.6rem;
  border: 1px solid var(--st-muted);
  border-radius: 0.6rem;
  background: var(--base);
  font-family: var(--_font-accent);
  font-size: 1.1rem;
  color: var(--t-medium);
  cursor: pointer;
}

.hope-chat__model select:focus-visible,
.hope-chat__icon-btn:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.hope-chat__icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 3.2rem;
  height: 3.2rem;
  padding: 0;
  border: 0;
  border-radius: 0.6rem;
  background: transparent;
  color: var(--t-medium);
  cursor: pointer;
  transition: color var(--_animspeed-fast) ease, background-color var(--_animspeed-fast) ease;
}

.hope-chat__icon-btn:hover {
  background: var(--st-muted);
  color: var(--t-bright);
}

.hope-chat__icon-btn svg {
  width: 1.8rem;
  height: 1.8rem;
}

/* --- 对话区 --- */

.hope-chat__log {
  flex: 1;
  min-height: 0;
  padding: 1.6rem;
  overflow-y: auto;
  /* 滚到头之后别把滚动传给页面 */
  overscroll-behavior: contain;
  scrollbar-width: thin;
}

.hope-chat__log:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -2px;
}

.hope-chat__empty {
  margin: 2.4rem 0;
  text-align: center;
  font-size: 1.3rem;
  color: var(--t-medium);
}

.hope-chat__empty strong {
  display: block;
  margin-bottom: 0.6rem;
  font-size: 1.5rem;
  color: var(--t-bright);
}

.hope-chat__msg {
  margin-bottom: 1.6rem;
  /* 长 URL 不能把面板撑爆 */
  overflow-wrap: anywhere;
}

.hope-chat__msg:last-child {
  margin-bottom: 0;
}

.hope-chat__bubble {
  padding: 1rem 1.2rem;
  border-radius: 1.2rem;
}

/* 用户气泡用半透明 accent 而不是实心：实心 accent 在两个主题下
   文字可读性会翻车（暗色 #5DA6FF 上放白字对比不够）。 */
.hope-chat__msg.is-user {
  display: flex;
  justify-content: flex-end;
}

.hope-chat__msg.is-user .hope-chat__bubble {
  max-width: 85%;
  border: 1px solid color-mix(in srgb, var(--accent) 30%, transparent);
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  color: var(--t-bright);
  white-space: pre-wrap;
}

.hope-chat__msg.is-assistant .hope-chat__bubble {
  padding: 0;
  color: var(--t-bright);
}

.hope-chat__msg.is-error .hope-chat__bubble {
  border: 1px solid color-mix(in srgb, #e5484d 45%, transparent);
  background: color-mix(in srgb, #e5484d 10%, transparent);
  color: var(--t-bright);
}

.hope-chat__retry {
  margin-top: 0.8rem;
  padding: 0.4rem 1rem;
  border: 1px solid var(--st-medium);
  border-radius: 0.6rem;
  background: transparent;
  font-family: var(--_font-accent);
  font-size: 1.2rem;
  color: var(--t-bright);
  cursor: pointer;
}

.hope-chat__retry:hover {
  border-color: var(--accent);
  color: var(--accent);
}

/* 「已截断」标记：流断了但内容留着 */
.hope-chat__truncated {
  margin-top: 0.6rem;
  font-family: var(--_font-accent);
  font-size: 1.1rem;
  color: var(--t-medium);
}

/* 思维链。推理模型正文来之前会先吐一大段，摊开显示比让用户干等好；
   正文一到就收成一行摘要，别喧宾夺主。 */
.hope-chat__think {
  margin: 0 0 0.8rem;
  border-left: 2px solid var(--st-medium);
  padding-left: 1rem;
  font-size: 1.25rem;
  color: var(--t-medium);
}

.hope-chat__think > summary {
  cursor: pointer;
  font-family: var(--_font-accent);
  font-size: 1.15rem;
  letter-spacing: 0.04em;
  color: var(--t-muted);
  list-style: none;
}

.hope-chat__think > summary::-webkit-details-marker { display: none; }

.hope-chat__think > summary::before {
  content: "▸ ";
  display: inline-block;
  transition: transform var(--_animspeed-fast) ease;
}

.hope-chat__think[open] > summary::before { content: "▾ "; }

.hope-chat__think > summary:hover { color: var(--t-medium); }

.hope-chat__think > summary:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.hope-chat__think > div {
  margin-top: 0.6rem;
  max-height: 16rem;
  overflow-y: auto;
  overscroll-behavior: contain;
  white-space: pre-wrap;
  line-height: 1.6;
}

/* 等待首个 token 时的三点 */
.hope-chat__typing {
  display: inline-flex;
  gap: 0.4rem;
  padding: 0.4rem 0;
}

.hope-chat__typing span {
  width: 0.6rem;
  height: 0.6rem;
  border-radius: 50%;
  background: var(--t-muted);
  animation: hope-chat-blink 1.2s infinite ease-in-out;
}

.hope-chat__typing span:nth-child(2) { animation-delay: 0.15s; }
.hope-chat__typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes hope-chat-blink {
  0%, 60%, 100% { opacity: 0.25; }
  30% { opacity: 1; }
}

/* --- Markdown 排版 --- */

.hope-chat__bubble > :first-child { margin-top: 0; }
.hope-chat__bubble > :last-child { margin-bottom: 0; }

/* 模板给全局 p 设了 font-size，会漏进气泡里，导致 p(16px) 和 li(14px) 不一致。
   这里把段落钉回继承值，气泡内的字号只由 .hope-chat 一处决定。 */
.hope-chat__bubble p,
.hope-chat__bubble li,
.hope-chat__bubble blockquote {
  font-size: inherit;
  line-height: 1.65;
}

.hope-chat__bubble p { margin: 0 0 0.8rem; }

/* renderMarkdown 把 # 映射成 h3、## → h4、### → h5、#### → h6
   （刻意从 h3 起步，别跟页面正文的标题层级打架）。
   所以这里必须覆盖 h3–h6 —— 站点全局样式给 h5 的是 26px，
   放进 16px 的聊天气泡里大得离谱。 */
.hope-chat__bubble h3,
.hope-chat__bubble h4,
.hope-chat__bubble h5,
.hope-chat__bubble h6 {
  margin: 1.6rem 0 0.8rem;
  font-family: var(--_font-default);
  font-size: 1.7rem;
  font-weight: 700;
  line-height: 1.4;
  letter-spacing: normal;
  text-transform: none;
  color: var(--t-bright);
}

.hope-chat__bubble h5,
.hope-chat__bubble h6 { font-size: 1.55rem; }

.hope-chat__bubble ul,
.hope-chat__bubble ol {
  margin: 0 0 0.8rem;
  padding-left: 2rem;
}

.hope-chat__bubble li { margin-bottom: 0.4rem; }

.hope-chat__bubble blockquote {
  margin: 0 0 0.8rem;
  padding-left: 1.2rem;
  border-left: 2px solid var(--st-medium);
  color: var(--t-medium);
}

.hope-chat__bubble hr {
  margin: 1.2rem 0;
  border: 0;
  border-top: 1px solid var(--st-muted);
}

.hope-chat__bubble a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 0.2em;
}

.hope-chat__bubble code {
  padding: 0.1rem 0.4rem;
  border-radius: 0.4rem;
  background: var(--st-muted);
  font-family: var(--_font-accent);
  font-size: 0.92em;
}

.hope-chat__code {
  position: relative;
  margin: 0 0 0.8rem;
}

.hope-chat__code pre {
  margin: 0;
  padding: 1.2rem;
  border: 1px solid var(--st-muted);
  border-radius: 0.8rem;
  background: var(--base-tint);
  /* 代码块横向滚动，不折行 —— 折了就不是代码了 */
  overflow-x: auto;
}

.hope-chat__code pre code {
  padding: 0;
  background: none;
  font-size: 1.25rem;
  line-height: 1.55;
  white-space: pre;
  word-break: normal;
  overflow-wrap: normal;
}

.hope-chat__copy {
  position: absolute;
  top: 0.6rem;
  right: 0.6rem;
  padding: 0.3rem 0.7rem;
  border: 1px solid var(--st-medium);
  border-radius: 0.5rem;
  background: var(--base);
  font-family: var(--_font-accent);
  font-size: 1.1rem;
  color: var(--t-medium);
  cursor: pointer;
  opacity: 0;
  transition: opacity var(--_animspeed-fast) ease;
}

.hope-chat__code:hover .hope-chat__copy,
.hope-chat__copy:focus-visible {
  opacity: 1;
}

/* 触屏没有 hover，复制按钮必须常驻 */
@media (hover: none) {
  .hope-chat__copy { opacity: 1; }
}

/* --- 输入区 --- */

.hope-chat__composer {
  display: flex;
  align-items: flex-end;
  gap: 0.8rem;
  padding: 1.2rem;
  border-top: 1px solid var(--st-muted);
  background: var(--base-tint);
}

.hope-chat__input {
  flex: 1;
  max-height: 12rem;
  padding: 0.9rem 1.2rem;
  border: 1px solid var(--st-medium);
  border-radius: 1rem;
  background: var(--base);
  font-family: var(--_font-default);
  font-size: 1.4rem;
  line-height: 1.5;
  color: var(--t-bright);
  resize: none;
  overflow-y: auto;
}

.hope-chat__input::placeholder { color: var(--t-muted); }

.hope-chat__input:focus-visible,
.hope-chat__send:focus-visible,
.hope-chat__stop:focus-visible,
.hope-chat__retry:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.hope-chat__send {
  display: flex;
  align-items: center;
  justify-content: center;
  flex: none;
  width: 4rem;
  height: 4rem;
  padding: 0;
  border: 0;
  border-radius: 1rem;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  transition: opacity var(--_animspeed-fast) ease;
}

.hope-chat__send:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.hope-chat__send svg {
  width: 2rem;
  height: 2rem;
}

.hope-chat__stop {
  flex: none;
  height: 4rem;
  padding: 0 1.4rem;
  border: 1px solid var(--st-medium);
  border-radius: 1rem;
  background: var(--base);
  font-family: var(--_font-accent);
  font-size: 1.2rem;
  color: var(--t-bright);
  cursor: pointer;
}

.hope-chat__stop[hidden] { display: none; }

/* --------------------------------------------------------------------------
   移动端：全屏
   -------------------------------------------------------------------------- */

@media (max-width: 600px) {
  .hope-chat-fab {
    right: 1.6rem;
    bottom: 1.6rem;
    width: 4.8rem;
    height: 4.8rem;
  }

  .hope-chat {
    inset: 0;
    width: 100%;
    /* dvh 不是 vh：100vh 会把输入框顶到 iOS 地址栏底下去 */
    height: 100dvh;
    border: 0;
    border-radius: 0;
  }

  .hope-chat__composer {
    /* 刘海屏的 home indicator */
    padding-bottom: calc(1.2rem + env(safe-area-inset-bottom));
  }

  /* 面板全屏时按钮会压在内容上，藏掉 —— 关闭走头部的 ✕ */
  body.hope-chat-open .hope-chat-fab { display: none; }
}
