/*
 * 小聆 · 样式表
 * 单文件、零依赖：不引任何外部字体 / CSS / 图片。
 * 移动端优先；深色模式随系统自动切换。
 */

/* ---------------- 主题变量 ---------------- */
:root {
  color-scheme: light dark;

  --bg: #f2f3f5;
  --surface: #ffffff;
  --surface-2: #f7f8fa;
  --text: #1d2129;
  --text-soft: #4e5969;
  --muted: #86909c;
  --border: #e5e6eb;
  --accent: #2b6de8;
  --accent-ink: #ffffff;
  --user-bg: #2b6de8;
  --user-ink: #ffffff;
  --assistant-bg: #ffffff;
  --danger: #c8341a;
  --ok: #1a9e5f;
  --warn: #c98a06;
  --ring: rgba(43, 109, 232, 0.2);
  --shadow: 0 1px 2px rgba(15, 23, 42, 0.07);
  --radius: 16px;

  /* 由 app.js 的 visualViewport 监听覆盖；无 JS 时退回 100dvh */
  --app-height: 100dvh;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #191d24;
    --surface-2: #21262f;
    --text: #e6e8eb;
    --text-soft: #b7bdc6;
    --muted: #8a919c;
    --border: #2b303a;
    --accent: #4d8bf5;
    --user-bg: #2f6fe0;
    --assistant-bg: #191d24;
    --danger: #ff7a5c;
    --ok: #3ecf8e;
    --warn: #e5a021;
    --ring: rgba(77, 139, 245, 0.28);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.45);
  }
}

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

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  font-size: 15px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
  overflow: hidden; /* 页面本身不滚，只有消息区滚 */
  overscroll-behavior: none;
}

button {
  font: inherit;
  color: inherit;
}

a {
  color: var(--accent);
}

/* ---------------- 布局骨架 ---------------- */
.app {
  height: 100vh; /* 老浏览器兜底 */
  height: var(--app-height, 100dvh);
  max-width: 860px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding-left: env(safe-area-inset-left, 0px);
  padding-right: env(safe-area-inset-right, 0px);
  background: var(--bg);
}

/* ---------------- 顶栏 ---------------- */
.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.brand-mark {
  width: 28px;
  height: 28px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-ink);
  display: grid;
  place-items: center;
  font-size: 15px;
  font-weight: 700;
  flex: 0 0 auto;
}

.brand-name {
  margin: 0;
  font-size: 17px;
  font-weight: 650;
  letter-spacing: 0.5px;
  white-space: nowrap;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 12px;
  flex: 0 0 auto;
}

.topbar-link {
  color: var(--muted);
  font-size: 13px;
  text-decoration: none;
  padding: 4px 2px;
  -webkit-tap-highlight-color: transparent;
}

.topbar-link:hover {
  color: var(--accent);
}

.conn {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}

.conn-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  flex: 0 0 auto;
  transition: background-color 0.2s ease, box-shadow 0.2s ease;
}

.conn[data-state="online"] .conn-dot {
  background: var(--ok);
  box-shadow: 0 0 0 3px rgba(26, 158, 95, 0.16);
}

.conn[data-state="offline"] .conn-dot {
  background: var(--danger);
  box-shadow: 0 0 0 3px rgba(200, 52, 26, 0.14);
}

/* ---------------- 消息区 ---------------- */
.messages {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 12px 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.empty {
  margin: auto;
  padding: 20px 16px;
  max-width: 32ch;
  text-align: center;
  color: var(--muted);
  font-size: 13.5px;
}

.empty-title {
  margin: 0 0 6px;
  color: var(--text-soft);
  font-size: 16px;
  font-weight: 600;
}

.empty-sub {
  margin: 0;
  line-height: 1.65;
}

.msg {
  display: flex;
  width: 100%;
}

.msg-user {
  justify-content: flex-end;
}

.msg-assistant {
  justify-content: flex-start;
}

.msg-body {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  max-width: min(86%, 560px);
  min-width: 0;
}

.bubble {
  max-width: 100%;
  padding: 9px 13px;
  border-radius: var(--radius);
  font-size: 15px;
  line-height: 1.6;
  overflow-wrap: anywhere;
  word-break: break-word;
  white-space: pre-wrap;
  box-shadow: var(--shadow);
}

.bubble-user {
  max-width: 86%;
  background: var(--user-bg);
  color: var(--user-ink);
  border-bottom-right-radius: 6px;
}

.bubble-assistant {
  position: relative;
  background: var(--assistant-bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
}

/* 有工具处于 start 时，气泡顶部轻微脉动；prefers-reduced-motion 由文件末尾统一关闭 */
.bubble.is-busy::before {
  content: "";
  position: absolute;
  top: 3px;
  left: 14px;
  right: 14px;
  height: 2px;
  border-radius: 2px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  animation: busy-pulse 1.3s ease-in-out infinite;
  pointer-events: none;
}

@keyframes busy-pulse {
  0%,
  100% {
    opacity: 0.18;
  }
  50% {
    opacity: 0.75;
  }
}

.bubble-assistant.is-stopped {
  border-style: dashed;
}

.bubble-text {
  min-width: 0;
  overflow-wrap: anywhere;
}

.bubble-text.is-placeholder {
  color: var(--muted);
  font-size: 13.5px;
}

/* 等待首个 delta 时的输入动画 */
.bubble.is-typing .bubble-text::after {
  content: "· · ·";
  color: var(--muted);
  font-size: 13px;
  letter-spacing: 2px;
  animation: blink 1.1s ease-in-out infinite;
}

@keyframes blink {
  0%,
  100% {
    opacity: 0.25;
  }
  50% {
    opacity: 1;
  }
}

/* 助手气泡下方的附加信息（引用列表 / 停止标注 / 用量） */
.msg-extras {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
  margin-top: 5px;
  padding: 0 3px;
  max-width: 100%;
}

/* 工具状态行：气泡内、正文上方；小号灰字，按首次出现顺序排列 */
.tool-list {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  margin: 0 0 5px;
  max-width: 100%;
}

.tool-list:empty {
  display: none;
}

.tool-item {
  max-width: 100%;
  font-size: 12px;
  line-height: 1.5;
  color: var(--muted);
  overflow-wrap: anywhere;
  word-break: break-word;
}

.tool-item[data-status="start"] {
  color: var(--text-soft);
}

.tool-item[data-status="done"] {
  color: var(--muted);
}

.tool-item[data-status="error"] {
  color: var(--danger);
}

/* 引用列表：正文结束后渲染的有序列表；紧凑、字号小于正文 */
.citation-list {
  margin: 1px 0 0;
  padding-left: 1.25em;
  font-size: 12.5px;
  line-height: 1.55;
  color: var(--muted);
  max-width: 100%;
}

.citation-item {
  max-width: 100%;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.citation-item::marker {
  color: var(--muted);
}

.citation-link {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid transparent;
  overflow-wrap: anywhere;
  word-break: break-word;
  -webkit-tap-highlight-color: transparent;
}

.citation-link:hover {
  border-bottom-color: currentColor;
}

.citation-site {
  color: var(--text-soft);
}

.citation-site + .citation-title::before {
  content: " · ";
  color: var(--muted);
}

.citation-title {
  color: var(--accent);
}

.meta-row {
  font-size: 11.5px;
  color: var(--muted);
  opacity: 0.85;
}

.stopped-note {
  font-size: 12px;
  color: var(--muted);
}

.stopped-note::before {
  content: "■";
  font-size: 8px;
  margin-right: 4px;
  vertical-align: 1px;
}

/* 系统条（错误、截断提示等） */
.sys-row {
  align-self: center;
  max-width: 92%;
  padding: 5px 12px;
  border-radius: 999px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 12.5px;
  line-height: 1.5;
  text-align: center;
  overflow-wrap: anywhere;
  word-break: break-word;
}

/* ---------------- 输入区 ---------------- */
.composer {
  flex: 0 0 auto;
  padding: 8px 12px calc(6px + env(safe-area-inset-bottom, 0px));
  background: var(--surface);
  border-top: 1px solid var(--border);
}

.composer-inner {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.input {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 40px;
  max-height: 140px;
  margin: 0;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* >=16px，避免 iOS 聚焦自动放大 */
  line-height: 1.45;
  resize: none;
  overflow-y: auto;
}

.input::placeholder {
  color: var(--muted);
}

.input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--ring);
}

.input:disabled {
  opacity: 0.6;
}

.composer-buttons {
  display: flex;
  align-items: center;
  gap: 6px;
  flex: 0 0 auto;
}

.btn {
  min-height: 40px;
  min-width: 62px;
  padding: 0 14px;
  border: 1px solid transparent;
  border-radius: 14px;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transition: opacity 0.15s ease, background-color 0.15s ease;
}

.btn:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

.btn-send {
  background: var(--accent);
  color: var(--accent-ink);
}

.btn-send:not(:disabled):active {
  opacity: 0.85;
}

.btn-stop {
  background: var(--surface-2);
  color: var(--danger);
  border-color: var(--border);
}

.btn-stop:not(:disabled):active {
  opacity: 0.85;
}

.composer-hint {
  margin: 5px 2px 0;
  color: var(--muted);
  font-size: 11.5px;
  text-align: center;
  user-select: none;
}

/* ---------------- 语音回归占位页 ---------------- */
.back-link {
  color: var(--accent);
  font-size: 13.5px;
  text-decoration: none;
  padding: 4px 0;
  -webkit-tap-highlight-color: transparent;
}

.page-title {
  margin: 0;
  font-size: 16px;
  font-weight: 650;
}

.simple-main {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 14px 14px calc(20px + env(safe-area-inset-bottom, 0px));
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 14px;
  box-shadow: var(--shadow);
}

.card-title {
  margin: 0 0 8px;
  font-size: 15px;
  font-weight: 650;
  line-height: 1.45;
}

.card-text {
  margin: 0;
  color: var(--text-soft);
  font-size: 14px;
  line-height: 1.65;
}

.card-list {
  margin: 0;
  padding-left: 1.2em;
  color: var(--text-soft);
  font-size: 13.5px;
  line-height: 1.7;
}

.card-actions {
  margin-top: 12px;
}

.mic-result {
  margin-top: 12px;
  padding: 9px 11px;
  border-radius: 10px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-soft);
  font-size: 13px;
  line-height: 1.6;
  white-space: pre-line;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.mic-result[data-state="busy"] {
  color: var(--muted);
}

.mic-result[data-state="ok"] {
  color: var(--ok);
  border-color: var(--ok);
}

.mic-result[data-state="error"] {
  color: var(--danger);
  border-color: var(--danger);
}

.mic-result[data-state="warn"] {
  color: var(--warn);
  border-color: var(--warn);
}

.simple-foot {
  margin: 2px 0 0;
  text-align: center;
}

.noscript {
  margin: 0;
  padding: 16px;
  text-align: center;
  color: var(--danger);
  font-size: 14px;
}

/* ---------------- 小屏与动效偏好 ---------------- */
@media (max-width: 380px) {
  .bubble {
    font-size: 14.5px;
  }

  .brand-name {
    font-size: 16px;
  }

  .btn {
    min-width: 54px;
    padding: 0 11px;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation: none !important;
    transition: none !important;
  }
}
