:root {
  --brand: #1677ff;
  --brand-dark: #0f5fd6;
  --brand-soft: #e8f2ff;
  --bg: #f2f4f7;
  --surface: #ffffff;
  --text: #1f2329;
  --text-2: #5c6673;
  --text-3: #8a929e;
  --line: #e8ebef;
  --danger: #e5484d;
  --radius-lg: 18px;
  --radius-md: 14px;
  --shadow-sm: 0 1px 2px rgba(20, 30, 50, .06), 0 2px 8px rgba(20, 30, 50, .04);
  --shadow-md: 0 6px 24px rgba(20, 30, 50, .10);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

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

body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", sans-serif;
  background: #e9edf2;
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; }

.app {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 760px;
  margin: 0 auto;
  background: var(--bg);
  overflow: hidden;
}

@media (min-width: 800px) {
  .app { box-shadow: 0 0 40px rgba(20, 30, 50, .10); }
}

/* ---------------- 顶栏 ---------------- */
.topbar {
  position: relative;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top, 0px));
  background: rgba(255, 255, 255, .94);
  backdrop-filter: saturate(180%) blur(12px);
  border-bottom: 1px solid var(--line);
}

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

.logo {
  width: 38px;
  height: 38px;
  flex: none;
  border-radius: 11px;
  display: grid;
  place-items: center;
  color: #fff;
  background: linear-gradient(140deg, #3b9bff, #1677ff);
  box-shadow: 0 4px 10px rgba(22, 119, 255, .28);
}

.logo svg { width: 22px; height: 22px; }

.brand-text { min-width: 0; }
.brand-text h1 {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: .2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.brand-text p {
  margin: 0;
  font-size: 11.5px;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.top-actions { display: flex; align-items: center; gap: 7px; flex: none; }

.header-action {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text-2);
  border-radius: 50%;
  cursor: pointer;
  transition: background .15s ease, color .15s ease, border-color .15s ease;
}
.header-action svg { width: 17px; height: 17px; }
.header-action:hover { color: var(--brand); border-color: #cfe2ff; background: #f7faff; }

.user-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px 5px 5px;
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease;
}
.user-chip:hover { background: #f7f9fc; border-color: #dde3ea; }
.chip-avatar {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--brand-soft);
  color: var(--brand);
  font-size: 12px;
  font-weight: 600;
}
.chip-name { font-size: 12.5px; color: var(--text-2); max-width: 86px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chevron { width: 12px; height: 12px; color: var(--text-3); }

.user-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 12px;
  min-width: 230px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
  padding: 6px;
  z-index: 40;
}
.user-menu[hidden] { display: none; }
.menu-label { padding: 8px 10px 6px; font-size: 11px; color: var(--text-3); letter-spacing: .4px; }
.menu-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border: 0;
  background: transparent;
  border-radius: 10px;
  cursor: pointer;
  text-align: left;
}
.menu-item:hover { background: #f5f8fc; }
.menu-item.active { background: var(--brand-soft); }
.menu-avatar {
  width: 30px; height: 30px; border-radius: 50%;
  display: grid; place-items: center;
  background: #eef1f5; color: var(--text-2); font-size: 12px; font-weight: 600; flex: none;
}
.menu-item.active .menu-avatar { background: #fff; color: var(--brand); }
.menu-text { display: flex; flex-direction: column; min-width: 0; }
.menu-text b { font-size: 13px; font-weight: 500; }
.menu-text small { font-size: 11.5px; color: var(--text-3); }
.menu-divider { height: 1px; background: var(--line); margin: 6px 8px; }

/* ---------------- 聊天区 ---------------- */
.chat {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 18px 16px 24px;
  scroll-behavior: smooth;
}

.welcome {
  max-width: 560px;
  margin: 4vh auto 0;
  text-align: center;
  animation: rise .35s ease both;
}
.welcome[hidden] { display: none; }
.welcome-icon svg { width: 58px; height: 58px; filter: drop-shadow(0 8px 18px rgba(22, 119, 255, .22)); }
.welcome h2 { margin: 14px 0 6px; font-size: 21px; font-weight: 600; letter-spacing: .2px; }
.welcome-sub { margin: 0 auto; max-width: 420px; color: var(--text-3); font-size: 13px; line-height: 1.7; }

.suggestions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 22px 0 16px;
  text-align: left;
}
@media (max-width: 520px) { .suggestions { grid-template-columns: 1fr; } }

.suggestion {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 12px 14px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  cursor: pointer;
  color: var(--text);
  font-size: 13.5px;
  line-height: 1.5;
  box-shadow: var(--shadow-sm);
  transition: transform .15s ease, box-shadow .15s ease, border-color .15s ease;
  text-align: left;
}
.suggestion:hover { transform: translateY(-1px); border-color: #cfe2ff; box-shadow: 0 6px 18px rgba(22, 119, 255, .10); }
.suggestion .tag { font-size: 10.5px; color: var(--brand); letter-spacing: .4px; }

.welcome-foot { display: flex; justify-content: center; }
.ghost-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 12px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text-2);
  border-radius: 999px;
  font-size: 12px;
  cursor: pointer;
}
.ghost-btn:hover { border-color: #cfe2ff; color: var(--brand); }
.ghost-btn svg { width: 15px; height: 15px; }

.messages { display: flex; flex-direction: column; gap: 20px; }

.msg { display: flex; gap: 10px; animation: rise .25s ease both; }
.msg-user { justify-content: flex-end; }

.avatar {
  width: 32px; height: 32px; flex: none;
  border-radius: 10px;
  display: grid; place-items: center;
  font-size: 11.5px; font-weight: 600;
  margin-top: 2px;
}
.avatar.ai { background: linear-gradient(140deg, #3b9bff, #1677ff); color: #fff; box-shadow: 0 3px 8px rgba(22, 119, 255, .22); }
.avatar.user { background: #e3e9f2; color: #4a5563; border-radius: 50%; }

.bubble-wrap { max-width: min(78%, 560px); display: flex; flex-direction: column; gap: 8px; }
.msg-user .bubble {
  max-width: min(78%, 560px);
  background: linear-gradient(140deg, #3b9bff, #1677ff);
  color: #fff;
  border-radius: 16px 16px 4px 16px;
  padding: 10px 14px;
  box-shadow: 0 4px 12px rgba(22, 119, 255, .18);
  white-space: pre-wrap;
  word-break: break-word;
}
.msg-ai .bubble {
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 4px 16px 16px 16px;
  padding: 12px 15px;
  box-shadow: var(--shadow-sm);
  word-break: break-word;
  min-height: 20px;
}
.msg-ai .bubble:empty { display: none; }

.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h3, .bubble h4, .bubble h5 { margin: 10px 0 6px; font-size: 15px; font-weight: 600; }
.bubble h3:first-child, .bubble h4:first-child, .bubble h5:first-child { margin-top: 0; }
.bubble ul, .bubble ol { margin: 4px 0 8px; padding-left: 20px; }
.bubble li { margin: 3px 0; }
.bubble strong { font-weight: 600; }
.bubble code {
  background: #f2f4f7; border-radius: 5px; padding: 1px 5px;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: 12.5px;
}
.bubble pre {
  background: #f6f8fa; border: 1px solid var(--line); border-radius: 10px;
  padding: 10px 12px; overflow-x: auto; font-size: 12.5px;
}
.bubble blockquote {
  margin: 8px 0 0; padding: 8px 12px; border-left: 3px solid #cfe2ff;
  background: #f7faff; color: var(--text-2); border-radius: 0 8px 8px 0; font-size: 13px;
}
.table-wrap { overflow-x: auto; margin: 8px 0; }
.bubble table { border-collapse: collapse; width: 100%; font-size: 13px; }
.bubble th, .bubble td { border: 1px solid var(--line); padding: 6px 9px; text-align: left; white-space: nowrap; }
.bubble th { background: #f6f8fb; font-weight: 600; }

.typing { display: inline-flex; gap: 4px; align-items: center; height: 20px; }
.typing span {
  width: 6px; height: 6px; border-radius: 50%; background: #b9c3cf;
  animation: blink 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: .18s; }
.typing span:nth-child(3) { animation-delay: .36s; }

/* 来源 */
.sources { display: flex; flex-direction: column; gap: 6px; }
.sources-head {
  display: flex; align-items: center; gap: 6px;
  font-size: 11.5px; color: var(--text-3); letter-spacing: .3px;
}
.sources-head .dot { width: 5px; height: 5px; border-radius: 50%; background: #b9c3cf; }
.source-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.source-chip {
  display: inline-flex; align-items: center; gap: 6px;
  max-width: 100%;
  padding: 5px 9px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 8px;
  font-size: 11.5px;
  color: var(--text-2);
  cursor: pointer;
  transition: border-color .15s ease, color .15s ease;
}
.source-chip:hover { border-color: #cfe2ff; color: var(--brand); }
.source-chip .idx {
  width: 16px; height: 16px; border-radius: 5px; flex: none;
  display: grid; place-items: center;
  background: var(--brand-soft); color: var(--brand);
  font-size: 10px; font-weight: 600;
}
.source-chip .sec { color: var(--text-3); }
.source-chip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.no-source {
  font-size: 12px; color: #b06a00; background: #fff8eb;
  border: 1px solid #ffe7bf; border-radius: 8px; padding: 6px 10px;
}

.msg-error .bubble {
  border-color: #ffd7d8;
  background: #fff6f6;
  color: #a8272b;
  font-size: 13px;
}

/* ---------------- 输入区 ---------------- */
.composer {
  position: relative;
  z-index: 15;
  padding: 8px 12px calc(8px + var(--safe-bottom));
  background: rgba(255, 255, 255, .96);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}
.composer-box {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 7px 7px 7px 14px;
  background: #f5f7fa;
  border: 1px solid transparent;
  border-radius: 16px;
  transition: border-color .15s ease, background .15s ease;
}
.composer-box:focus-within { border-color: #bcd8ff; background: #fff; box-shadow: 0 0 0 3px rgba(22, 119, 255, .08); }
#input {
  flex: 1;
  border: 0;
  outline: 0;
  resize: none;
  background: transparent;
  font: inherit;
  font-size: 14.5px;
  line-height: 1.55;
  max-height: 120px;
  padding: 6px 0;
  color: var(--text);
}
#input::placeholder { color: #9aa4b0; }
.send-btn {
  width: 36px; height: 36px; flex: none;
  border: 0; border-radius: 11px;
  background: var(--brand); color: #fff;
  display: grid; place-items: center;
  cursor: pointer;
  transition: background .15s ease, transform .1s ease, opacity .15s ease;
}
.send-btn svg { width: 21px; height: 21px; }
.send-btn:hover { background: var(--brand-dark); }
.send-btn:active { transform: scale(.96); }
.send-btn:disabled { background: #c9d3df; cursor: not-allowed; }
.composer-meta {
  display: flex; justify-content: space-between; gap: 10px;
  padding: 6px 6px 0;
  font-size: 10.5px; color: #9aa4b0;
}

/* ---------------- 启动 / 错误 ---------------- */
.boot {
  position: absolute; inset: 0; z-index: 60;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 12px; background: var(--bg);
}
.boot[hidden] { display: none; }
.boot p { color: var(--text-3); font-size: 13px; }
.spinner {
  width: 30px; height: 30px; border-radius: 50%;
  border: 2.5px solid #d8e2ef; border-top-color: var(--brand);
  animation: spin .8s linear infinite;
}
.boot-error { text-align: center; padding: 32px; }
.error-icon {
  width: 46px; height: 46px; border-radius: 50%;
  display: grid; place-items: center;
  background: #ffecec; color: var(--danger); font-weight: 700; font-size: 20px;
}
.boot-error h2 { margin: 0; font-size: 18px; }
.boot-error p { max-width: 420px; line-height: 1.7; }

/* ---------------- 底部弹层 ---------------- */
.sheet-backdrop {
  position: absolute; inset: 0; z-index: 50;
  background: rgba(15, 23, 32, .42);
  display: flex; align-items: flex-end;
  animation: fade .18s ease;
}
.sheet-backdrop[hidden] { display: none; }
.sheet {
  width: 100%;
  max-height: 78%;
  background: #fff;
  border-radius: 20px 20px 0 0;
  padding: 6px 18px calc(18px + var(--safe-bottom));
  box-shadow: 0 -12px 40px rgba(15, 23, 32, .18);
  display: flex; flex-direction: column;
  animation: slideUp .22s cubic-bezier(.2, .8, .2, 1);
}
.sheet-handle { width: 36px; height: 4px; border-radius: 2px; background: #e1e6ec; margin: 4px auto 10px; }
.sheet-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 12px; padding-bottom: 10px; border-bottom: 1px solid var(--line); }
.sheet-head h3 { margin: 0; font-size: 16px; }
.sheet-head p { margin: 3px 0 0; font-size: 12px; color: var(--text-3); }
.icon-btn {
  width: 30px; height: 30px; flex: none;
  border: 0; border-radius: 50%; background: #f2f4f7; color: var(--text-3);
  cursor: pointer; font-size: 13px;
}
.sheet-body { overflow-y: auto; padding: 14px 0 4px; }
.sheet-body p { margin: 0 0 10px; font-size: 14px; color: var(--text-2); line-height: 1.75; }
.doc-item {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 10px; border-radius: 10px;
}
.doc-item:hover { background: #f7f9fc; }
.doc-icon {
  width: 34px; height: 34px; flex: none; border-radius: 9px;
  display: grid; place-items: center;
  background: var(--brand-soft); color: var(--brand); font-size: 12px; font-weight: 600;
}
.doc-text { min-width: 0; }
.doc-text b { display: block; font-size: 13.5px; font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.doc-text small { color: var(--text-3); font-size: 11.5px; }
.badge {
  display: inline-block; padding: 1px 7px; border-radius: 999px;
  background: #eef7ee; color: #2f8a3d; font-size: 10.5px;
}
.badge.locked { background: #fff3e6; color: #b06a00; }

/* ---------------- Toast ---------------- */
.toast {
  position: absolute; left: 50%; bottom: 96px; z-index: 70;
  transform: translate(-50%, 12px);
  max-width: 86%;
  padding: 9px 14px; border-radius: 10px;
  background: rgba(31, 35, 41, .92); color: #fff;
  font-size: 12.5px; line-height: 1.5;
  opacity: 0; pointer-events: none;
  transition: opacity .2s ease, transform .2s ease;
  text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes blink { 0%, 80%, 100% { opacity: .35; transform: translateY(0); } 40% { opacity: 1; transform: translateY(-2px); } }
@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(24px); } to { transform: translateY(0); } }

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
