:root {
  --bg: #f4f7f9;
  --panel: #ffffff;
  --ink: #17212b;
  --muted: #65717e;
  --line: #e3e9ee;
  --teal: #0f766e;
  --teal-dark: #0b5f59;
  --teal-soft: #e6f4f1;
  --blue: #2563eb;
  --danger: #dc2626;
  --shadow: 0 8px 28px rgba(23, 42, 58, 0.08);
  --radius: 16px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei",
    "PingFang SC", "Hiragino Sans GB", "Noto Sans CJK SC", sans-serif;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body { overflow: hidden; }

button, textarea { font: inherit; }
button { -webkit-tap-highlight-color: transparent; }

.hidden { display: none !important; }

.app {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 960px;
  height: 100vh;
  height: 100dvh;
  margin: 0 auto;
  background:
    radial-gradient(900px 420px at 50% -180px, rgba(15, 118, 110, 0.10), transparent 70%),
    var(--bg);
  box-shadow: 0 0 0 1px rgba(15, 118, 110, 0.04);
}

/* ---------- 顶栏 ---------- */
.topbar {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 18px;
  padding-top: calc(12px + env(safe-area-inset-top));
  background: rgba(255, 255, 255, 0.86);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid rgba(227, 233, 238, 0.9);
  z-index: 5;
}

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

.brand-mark {
  width: 36px;
  height: 36px;
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  border-radius: 11px;
  background: linear-gradient(145deg, #0f766e, #159b8f);
  color: #fff;
  font-size: 18px;
  font-weight: 700;
  box-shadow: 0 4px 12px rgba(15, 118, 110, 0.28);
}

.brand-text { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.brand-text strong { font-size: 16px; letter-spacing: 0.02em; }
.brand-text span { font-size: 11.5px; color: var(--muted); margin-top: 2px; }

.new-chat {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 7px 11px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  color: #3b4a58;
  font-size: 12.5px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.new-chat:hover { background: var(--teal-soft); border-color: #b9ded8; color: var(--teal-dark); }
.new-chat svg { width: 14px; height: 14px; }

/* ---------- 消息区 ---------- */
.messages {
  flex: 1 1 auto;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 20px 18px 26px;
  scroll-behavior: smooth;
  overscroll-behavior: contain;
}

.msg { display: flex; gap: 10px; margin: 0 0 20px; align-items: flex-start; }
.msg.user { justify-content: flex-end; }

.msg .avatar {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  margin-top: 2px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: linear-gradient(145deg, #0f766e, #159b8f);
  color: #fff;
  font-size: 15px;
  font-weight: 700;
  box-shadow: 0 3px 9px rgba(15, 118, 110, 0.22);
}

.msg .body { flex: 1 1 auto; min-width: 0; max-width: 100%; }

.msg .bubble {
  display: inline-block;
  max-width: 86%;
  padding: 11px 15px;
  border-radius: 16px 16px 5px 16px;
  background: var(--teal);
  color: #fff;
  font-size: 16px;
  line-height: 1.65;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  box-shadow: 0 4px 14px rgba(15, 118, 110, 0.18);
}

.msg.user .bubble img {
  display: block;
  max-width: 240px;
  max-height: 240px;
  margin-top: 8px;
  border-radius: 10px;
  object-fit: contain;
  background: rgba(255, 255, 255, 0.2);
}

.msg.user .bubble .tag {
  display: inline-block;
  margin-top: 6px;
  font-size: 11.5px;
  opacity: 0.75;
}

.md {
  font-size: 16px;
  line-height: 1.74;
  overflow-wrap: anywhere;
  color: var(--ink);
}
.md > :first-child { margin-top: 0; }
.md > :last-child { margin-bottom: 0; }
.md p { margin: 0 0 10px; }
.md h1, .md h2, .md h3, .md h4 { margin: 18px 0 8px; line-height: 1.4; font-weight: 700; }
.md h1 { font-size: 21px; }
.md h2 { font-size: 19px; }
.md h3 { font-size: 17px; }
.md h4 { font-size: 16px; }
.md ul, .md ol { margin: 6px 0 10px; padding-left: 1.35em; }
.md li { margin: 3px 0; }
.md li > p { margin: 3px 0; }
.md strong { font-weight: 700; color: #0d2b33; }
.md a { color: var(--blue); text-decoration: none; border-bottom: 1px solid rgba(37, 99, 235, 0.25); }
.md a:hover { border-bottom-color: var(--blue); }
.md blockquote {
  margin: 10px 0;
  padding: 8px 12px;
  border-left: 3px solid #9dd3cc;
  background: #f1f8f7;
  color: #40525e;
  border-radius: 0 8px 8px 0;
}
.md hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
.md code {
  padding: 2px 5px;
  border-radius: 5px;
  background: #eef2f5;
  color: #b42318;
  font-family: "Cascadia Code", Consolas, "Courier New", monospace;
  font-size: 0.9em;
}
.md pre {
  margin: 10px 0;
  padding: 12px 14px;
  border-radius: 10px;
  background: #0f1720;
  color: #e6edf3;
  overflow-x: auto;
  font-size: 13.5px;
  line-height: 1.55;
}
.md pre code { padding: 0; background: transparent; color: inherit; font-size: inherit; }
.md table {
  display: block;
  width: 100%;
  max-width: 100%;
  margin: 10px 0;
  overflow-x: auto;
  border-collapse: collapse;
  font-size: 14px;
}
.md table th, .md table td {
  padding: 7px 10px;
  border: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}
.md table th { background: #f1f6f6; font-weight: 600; white-space: nowrap; }
.md img { max-width: 100%; height: auto; border-radius: 8px; }
.md .mermaid {
  margin: 12px 0;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
  text-align: center;
}
.md .mermaid img { display: block; max-width: 100%; margin: 0 auto; }
.md .mermaid .mermaid-fallback { font-size: 12.5px; color: var(--muted); }

/* ---------- 选项 ---------- */
.opts, .suggestions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 12px;
}

.opt, .suggestion {
  display: flex;
  align-items: center;
  gap: 9px;
  width: 100%;
  padding: 10px 13px;
  border: 1px solid #d7e2e7;
  border-radius: 11px;
  background: #fff;
  color: #263541;
  font-size: 15px;
  line-height: 1.4;
  text-align: left;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s, transform 0.1s;
}
.opt:hover, .suggestion:hover { border-color: #86c9bf; background: #f3fbf9; }
.opt:active, .suggestion:active { transform: scale(0.995); }
.opt:disabled, .suggestion:disabled { opacity: 0.5; cursor: default; }

.opt-key {
  flex: 0 0 auto;
  width: 24px;
  height: 24px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--teal-soft);
  color: var(--teal);
  font-size: 12.5px;
  font-weight: 700;
}
.opt-text { flex: 1 1 auto; min-width: 0; }

.suggestion::before {
  content: "";
  flex: 0 0 auto;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #8bc7bf;
}

/* ---------- 状态 ---------- */
.status {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  margin-top: 2px;
  padding: 8px 12px;
  border: 1px solid #dbe7e5;
  border-radius: 10px;
  background: #f4fbfa;
  color: #40625f;
  font-size: 13px;
}
.status .dots { display: inline-flex; gap: 3px; }
.status .dots i {
  width: 5px; height: 5px; border-radius: 50%; background: #3ba99b;
  animation: dot 1.05s infinite ease-in-out;
}
.status .dots i:nth-child(2) { animation-delay: 0.16s; }
.status .dots i:nth-child(3) { animation-delay: 0.32s; }
.status .elapsed { color: #7b8b93; font-variant-numeric: tabular-nums; }
@keyframes dot {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.45; }
  30% { transform: translateY(-3px); opacity: 1; }
}

.error-text { color: var(--danger); }

/* ---------- 输入区 ---------- */
.composer-wrap {
  flex: 0 0 auto;
  position: relative;
  z-index: 4;
  padding: 9px 16px calc(9px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, rgba(244, 247, 249, 0.98) 70%, rgba(244, 247, 249, 0.72));
  border-top: 1px solid rgba(227, 233, 238, 0.8);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 7px 7px 7px 13px;
  border: 1px solid #d8e2e8;
  border-radius: 18px;
  background: #fff;
  box-shadow: var(--shadow);
}
.composer:focus-within { border-color: #8fcfc7; box-shadow: 0 8px 28px rgba(15, 118, 110, 0.12); }

#input {
  flex: 1 1 auto;
  width: 100%;
  min-height: 38px;
  max-height: 150px;
  padding: 7px 0;
  border: 0;
  outline: 0;
  resize: none;
  background: transparent;
  color: var(--ink);
  font-size: 16px;
  line-height: 1.55;
}
#input::placeholder { color: #98a5ae; }

.tools { display: flex; align-items: center; gap: 3px; flex: 0 0 auto; }

.tool-btn, .send-btn {
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 11px;
  background: transparent;
  color: #60717e;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, opacity 0.15s;
}
.tool-btn:hover { background: #eef4f5; color: var(--teal); }
.tool-btn svg { width: 22px; height: 22px; }
.tool-btn.busy { opacity: 0.45; pointer-events: none; }

.mic-btn { touch-action: none; }
.mic-btn.recording { color: var(--danger); background: #fdecec; }

.send-btn {
  background: var(--teal);
  color: #fff;
  border-radius: 12px;
  box-shadow: 0 3px 10px rgba(15, 118, 110, 0.22);
}
.send-btn svg { width: 20px; height: 20px; margin-left: -1px; }
.send-btn:hover:not(:disabled) { background: var(--teal-dark); }
.send-btn:disabled { opacity: 0.35; cursor: default; box-shadow: none; }

.hint {
  margin: 6px 4px 0;
  color: #8a98a2;
  font-size: 11.5px;
  line-height: 1.4;
  text-align: center;
}

.preview {
  display: flex;
  align-items: center;
  gap: 8px;
  width: fit-content;
  max-width: 100%;
  margin: 0 0 8px;
  padding: 5px 6px;
  border: 1px solid #d8e2e8;
  border-radius: 12px;
  background: #fff;
  box-shadow: 0 4px 14px rgba(23, 42, 58, 0.06);
}
.preview img { width: 56px; height: 56px; object-fit: cover; border-radius: 8px; background: #eef2f4; }
.preview button {
  width: 26px; height: 26px;
  border: 0; border-radius: 50%;
  background: #eef2f4; color: #53636f;
  font-size: 18px; line-height: 1; cursor: pointer;
}
.preview button:hover { background: #fdecec; color: var(--danger); }

/* ---------- 语音浮层 ---------- */
.voice-overlay {
  position: fixed;
  left: 50%;
  bottom: calc(112px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 30;
  pointer-events: none;
}
.voice-panel {
  min-width: 210px;
  padding: 16px 20px 14px;
  border-radius: 18px;
  background: rgba(20, 33, 43, 0.94);
  color: #fff;
  text-align: center;
  box-shadow: 0 16px 44px rgba(10, 25, 35, 0.32);
  backdrop-filter: blur(10px);
}
.voice-wave { display: flex; align-items: center; justify-content: center; gap: 4px; height: 30px; margin-bottom: 8px; }
.voice-wave i {
  display: block; width: 4px; height: 10px; border-radius: 3px; background: #5eead4;
  animation: wave 0.9s infinite ease-in-out;
}
.voice-wave i:nth-child(2) { animation-delay: 0.12s; }
.voice-wave i:nth-child(3) { animation-delay: 0.24s; }
.voice-wave i:nth-child(4) { animation-delay: 0.36s; }
.voice-wave i:nth-child(5) { animation-delay: 0.48s; }
@keyframes wave {
  0%, 100% { height: 8px; opacity: 0.55; }
  50% { height: 26px; opacity: 1; }
}
.voice-title { font-size: 14px; letter-spacing: 0.02em; }
.voice-time { margin-top: 5px; font-size: 12px; color: #9fd8d0; font-variant-numeric: tabular-nums; }
.voice-overlay.cancel .voice-wave i { background: #fca5a5; }
.voice-overlay.cancel .voice-title { color: #fecaca; }
.voice-overlay.cancel .voice-panel { background: rgba(70, 22, 22, 0.95); }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  top: calc(16px + env(safe-area-inset-top));
  transform: translateX(-50%);
  z-index: 50;
  max-width: min(88vw, 520px);
  padding: 9px 15px;
  border-radius: 10px;
  background: rgba(23, 33, 43, 0.92);
  color: #fff;
  font-size: 13.5px;
  line-height: 1.5;
  text-align: center;
  box-shadow: 0 10px 30px rgba(10, 25, 35, 0.22);
  backdrop-filter: blur(8px);
}
.toast a { color: #7dd3c7; text-decoration: underline; }

/* ---------- 回到底部 ---------- */
.scroll-btn {
  position: absolute;
  right: 20px;
  bottom: calc(128px + env(safe-area-inset-bottom));
  z-index: 6;
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border: 1px solid #d8e2e8;
  border-radius: 50%;
  background: #fff;
  color: #53636f;
  box-shadow: 0 5px 16px rgba(23, 42, 58, 0.12);
  cursor: pointer;
}
.scroll-btn svg { width: 20px; height: 20px; }

/* ---------- 备案信息 ---------- */
.icp {
  margin: 2px 0 0;
  text-align: center;
  font-size: 11px;
  line-height: 1.5;
  color: #94a3ac;
}
.icp a { color: inherit; text-decoration: none; }
.icp a:hover { color: var(--teal); text-decoration: underline; }

/* ---------- 移动端 ---------- */
@media (max-width: 767px) {
  .topbar { padding-left: 12px; padding-right: 12px; }
  .brand-mark { width: 33px; height: 33px; font-size: 17px; border-radius: 10px; }
  .brand-text strong { font-size: 15px; }
  .brand-text span { font-size: 11px; }
  .new-chat { padding: 6px 9px; font-size: 12px; }

  .messages { padding: 14px 11px 20px; }
  .msg { gap: 8px; margin-bottom: 17px; }
  .msg .avatar { width: 31px; height: 31px; font-size: 14px; border-radius: 9px; }
  .msg .bubble { max-width: 90%; font-size: 16.5px; padding: 10px 13px; }
  .md { font-size: 16.5px; line-height: 1.76; }
  .md h1 { font-size: 20px; }
  .md h2 { font-size: 18.5px; }
  .md h3 { font-size: 17.5px; }
  .md table { font-size: 14.5px; }
  .opt, .suggestion { font-size: 15.5px; padding: 11px 13px; }

  .composer-wrap { padding-left: 10px; padding-right: 10px; }
  .composer { border-radius: 16px; padding-left: 11px; }
  .tool-btn, .send-btn { width: 36px; height: 36px; }
  .tool-btn svg { width: 21px; height: 21px; }
  .hint { font-size: 11px; }
  .scroll-btn { right: 13px; bottom: calc(122px + env(safe-area-inset-bottom)); }
}

@media (max-width: 380px) {
  .brand-text span { display: none; }
  .new-chat span { display: none; }
}
