/* 移动端优先，聊天式布局 */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* 关键：保证 hidden 属性始终生效（否则 display:flex 会覆盖它） */
[hidden] { display: none !important; }
html, body { margin: 0; padding: 0; height: 100%; }
/* 只允许消息区滚动，禁止整页滚动（否则输入框会被地址栏收展顶得来回移动） */
body { overflow: hidden; overscroll-behavior: none; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  background: #f3f4f6; color: #111827; font-size: 16px;
}

/* ---------- 登录 ---------- */
.login { display: flex; align-items: center; justify-content: center; min-height: 100vh; padding: 20px; }
.login-card { background: #fff; border-radius: 16px; padding: 32px 24px; width: 100%; max-width: 340px; box-shadow: 0 4px 24px rgba(0,0,0,.08); text-align: center; }
.login-card h1 { font-size: 20px; margin: 8px 0 4px; }
.login-hint { color: #6b7280; font-size: 13px; margin: 0 0 16px; }
.login-card input { width: 100%; padding: 12px; border: 1px solid #d1d5db; border-radius: 10px; font-size: 16px; margin-bottom: 12px; }
.err { color: #dc2626; font-size: 13px; margin-top: 10px; min-height: 18px; }

/* ---------- 布局 ---------- */
#app {
  display: flex; flex-direction: column;
  height: 100vh;   /* 兜底：不支持 dvh 的老浏览器 */
  height: 100dvh;  /* 移动端动态视口高度：随地址栏/键盘收展实时调整 */
  max-width: 720px; margin: 0 auto;
}
header {
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  background: #1f2937; color: #fff; flex-shrink: 0;
}
.title { font-weight: 600; font-size: 16px; white-space: nowrap; }
/* 仅主界面头部标题居中；子页面（管理/详情）标题与返回靠左 */
#app header .title { flex: 1; text-align: center; overflow: hidden; }
.manage-head .title { text-align: left; flex: 0 0 auto; }
.kb-count { font-size: 12px; color: #9ca3af; font-weight: 400; margin-left: 6px; }
.actions { display: flex; gap: 6px; flex-shrink: 0; }
.chip {
  background: #374151; color: #fff; border: none; border-radius: 999px;
  padding: 6px 9px; font-size: 12px; cursor: pointer; white-space: nowrap;
}

main {
  flex: 1; overflow-y: auto; padding: 14px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;  /* 滚动到边缘不把整页带走 */
}
.msg { display: flex; flex-direction: column; margin-bottom: 12px; position: relative; }
.msg.user { align-items: flex-end; }
.msg.assistant { align-items: flex-start; }
.bubble {
  max-width: 86%; padding: 10px 14px; border-radius: 14px;
  line-height: 1.55; font-size: 15px; word-break: break-word;
}
.msg.user .bubble { background: #2563eb; color: #fff; border-bottom-right-radius: 4px; }
.msg.assistant .bubble { background: #fff; border: 1px solid #e5e7eb; border-bottom-left-radius: 4px; box-shadow: 0 1px 2px rgba(0,0,0,.04); }

/* 来源区块 */
.src-section { margin-top: 10px; border-top: 1px solid #e5e7eb; padding-top: 8px; }
.src-head { font-size: 12px; color: #6b7280; margin-bottom: 8px; display: flex; justify-content: space-between; }
.src-tip { color: #9ca3af; }
.src-chips { display: flex; flex-wrap: wrap; gap: 6px; }
.src-chip {
  display: inline-flex; align-items: center; gap: 6px;
  background: #fff; border: 1px solid #e5e7eb; border-radius: 999px;
  padding: 5px 11px; font-size: 13px; color: #374151; cursor: pointer;
}
.src-chip.active { border-color: #2563eb; background: #eff6ff; }
.pct { font-weight: 700; }
.pct-hi { color: #059669; } .pct-mid { color: #d97706; } .pct-lo { color: #9ca3af; }
.src-panel { background: #f9fafb; border-radius: 10px; padding: 10px; margin-top: 8px; }
.src-panel-head { display: flex; align-items: center; gap: 8px; font-size: 13px; margin-bottom: 6px; }
.src-kind { color: #9ca3af; font-size: 12px; margin-left: auto; }
.src-bar { height: 4px; background: #e5e7eb; border-radius: 2px; overflow: hidden; margin-bottom: 8px; }
.src-bar i { display: block; height: 100%; background: #2563eb; }
.src-tags { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
.src-content { white-space: pre-wrap; max-height: 180px; overflow-y: auto; font-size: 13px; color: #374151; }
/* 图片网格卡片 */
.src-imgs { display: flex; flex-wrap: wrap; gap: 8px; padding-top: 8px; }
.img-card {
  position: relative; width: calc(33.33% - 6px); aspect-ratio: 1;
  border-radius: 10px; overflow: hidden; background: #e5e7eb; cursor: pointer;
}
.img-card img { width: 100%; height: 100%; object-fit: cover; display: block; }
.img-tag {
  position: absolute; right: 4px; bottom: 4px;
  background: rgba(0,0,0,.6); color: #fff; font-size: 11px;
  padding: 1px 7px; border-radius: 999px;
}

/* markdown 细节 */
.md-h { font-size: 16px; font-weight: 700; margin: 6px 0 2px; }
.md-li { margin: 3px 0; padding-left: 4px; }
.md-quote { border-left: 3px solid #d1d5db; padding-left: 8px; color: #6b7280; margin: 4px 0; }
.bubble code { background: #f3f4f6; padding: 1px 5px; border-radius: 4px; font-size: 13px; }

/* 数据提醒：低调小提示条 */
.data-notice {
  margin-top: 8px; padding: 5px 10px;
  background: #fffbeb; border: 1px solid #fde68a;
  color: #92600a; border-radius: 8px;
  font-size: 12px; line-height: 1.5;
}

/* 思考动画 */
.typing-dots span { animation: blink 1.2s infinite; }
.typing-dots span:nth-child(2) { animation-delay: .2s; }
.typing-dots span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 50% { opacity: 0; } }

footer {
  display: flex; gap: 8px; align-items: flex-end;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: #fff; border-top: 1px solid #e5e7eb; flex-shrink: 0;
}
#input {
  flex: 1; resize: none; border: 1px solid #d1d5db; border-radius: 12px;
  padding: 10px 12px; font-size: 16px; font-family: inherit; max-height: 120px; line-height: 1.4;
}
.primary {
  background: #2563eb; color: #fff; border: none; border-radius: 12px;
  padding: 10px 18px; font-size: 15px; cursor: pointer; flex-shrink: 0;
}
.primary:disabled { opacity: .5; }
.send { border-radius: 12px; padding: 10px 16px; }

/* ---------- 弹层 ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.45);
  display: flex; align-items: flex-end; justify-content: center; z-index: 50;
}
.sheet {
  background: #fff; width: 100%; max-width: 720px; border-radius: 18px 18px 0 0;
  padding: 16px 16px calc(20px + env(safe-area-inset-bottom));
  animation: up .2s ease;
  max-height: 92%; overflow-y: auto; -webkit-overflow-scrolling: touch;
}
@keyframes up { from { transform: translateY(30px); opacity: .6; } to { transform: none; opacity: 1; } }
.sheet-head { font-weight: 600; font-size: 17px; margin-bottom: 12px; display: flex; justify-content: space-between; align-items: center; }
.close { color: #9ca3af; font-size: 18px; cursor: pointer; padding: 4px; }
.sheet input[type="text"], .sheet input:not([type]), .sheet textarea {
  width: 100%; border: 1px solid #d1d5db; border-radius: 10px; padding: 10px 12px;
  font-size: 16px; font-family: inherit; margin-bottom: 10px;
}
.form-sec { font-size: 12px; color: #9ca3af; margin: 12px 0 6px; font-weight: 600; }
.form-submit { width: 100%; padding: 13px; }
.doc-file-btn { display: block; text-align: center; padding: 18px; font-size: 15px; }
.sheet textarea { resize: vertical; }
.file-btn {
  display: inline-block; background: #f3f4f6; border: 1px dashed #9ca3af; color: #374151;
  border-radius: 10px; padding: 10px 14px; font-size: 14px; cursor: pointer; margin-bottom: 10px;
}
.thumbs { display: flex; gap: 10px; flex-wrap: wrap; margin-bottom: 10px; }
.note-thumb { position: relative; width: 76px; height: 76px; }
.note-thumb img { width: 100%; height: 100%; object-fit: cover; border-radius: 8px; display: block; }
.thumb-rm {
  position: absolute; top: -8px; right: -8px;
  background: #ef4444; color: #fff; width: 24px; height: 24px;
  border-radius: 50%; font-size: 13px; line-height: 24px; text-align: center; cursor: pointer;
  box-shadow: 0 1px 3px rgba(0,0,0,.35); border: 2px solid #fff;
}
.hint { font-size: 13px; color: #6b7280; margin-top: 8px; min-height: 16px; }
.hint.ok { color: #059669; }
.hint.bad { color: #dc2626; }
#note-submit, #doc-submit { width: 100%; padding: 12px; }

/* ---------- 管理页 ---------- */
#page-manage {
  position: fixed; inset: 0; background: #f3f4f6; z-index: 40;  /* 低于弹层(50)，保证编辑等弹层不被盖住 */
  display: flex; flex-direction: column;
}
.manage-head {
  display: flex; align-items: center; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  background: #1f2937; color: #fff;
}
.manage-head .title { text-align: left; flex: 0 0 auto; }
.detail-kind { margin-left: auto; }
.mg-filter { flex: 1; padding: 8px 12px; border: none; border-radius: 10px; font-size: 14px; min-width: 0; }
/* 筛选框一键清空 */
.filter-wrap { position: relative; flex: 1; min-width: 0; display: block; }
.filter-wrap .mg-filter { width: 100%; box-sizing: border-box; padding-right: 36px; flex: none; }
.filter-clear {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  width: 24px; height: 24px; border-radius: 50%; border: none;
  background: #e5e7eb; color: #6b7280; font-size: 12px; line-height: 1;
  cursor: pointer; display: flex; align-items: center; justify-content: center;
}
.filter-clear:active { background: #d1d5db; }
.manage-list { flex: 1; overflow-y: auto; padding: 12px; -webkit-overflow-scrolling: touch; }
.mg-tabs {
  display: flex; gap: 6px; align-items: center; padding: 10px 12px 0;
}
.mg-tab {
  flex: 1; padding: 8px 0; border: 1px solid #e5e7eb; background: #fff;
  border-radius: 10px; font-size: 14px; cursor: pointer; color: #374151;
}
.mg-tab.active { background: #2563eb; color: #fff; border-color: #2563eb; }
.mg-tab-add {
  padding: 8px 12px; border: 1px dashed #2563eb; color: #2563eb;
  background: #eff6ff; border-radius: 10px; font-size: 13px; cursor: pointer; flex-shrink: 0;
}
.doc-chunk { border: 1px solid #f3f4f6; border-radius: 10px; padding: 10px; margin-bottom: 10px; background: #fafafa; }
.doc-chunk-head { font-size: 12px; color: #9ca3af; margin-bottom: 6px; }
.doc-chunk-body { white-space: pre-wrap; font-size: 14px; line-height: 1.7; color: #1f2937; }
.mg-sec { font-size: 13px; color: #6b7280; margin: 12px 4px 8px; font-weight: 600; }
.mg-item {
  background: #fff; border: 1px solid #e5e7eb; border-radius: 12px;
  padding: 14px; margin-bottom: 10px;
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  cursor: pointer;
}
.mg-item:active { background: #f3f4f6; }
.mg-info { flex: 1; min-width: 0; }
.mg-title {
  font-size: 15px; font-weight: 600; word-break: break-all;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.mg-meta { font-size: 12px; color: #9ca3af; margin-top: 3px; }
.mg-arrow { color: #cbd5e1; font-size: 22px; flex-shrink: 0; }
.row-tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.row-tag {
  background: #eef2ff; color: #3730a3; border-radius: 999px;
  padding: 1px 8px; font-size: 11px;
}
.mg-empty { text-align: center; color: #9ca3af; padding: 40px 0; }

/* 会话列表页 */
#page-convs {
  position: fixed; inset: 0; background: #f3f4f6; z-index: 40;
  display: flex; flex-direction: column;
}
.conv-del {
  border: none; background: transparent; color: #dc2626;
  font-size: 13px; padding: 6px 8px; cursor: pointer; flex-shrink: 0;
}

/* 标签 */
.tags-editor { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; min-height: 24px; }
.tag-chip {
  display: inline-flex; align-items: center; gap: 4px;
  background: #eef2ff; color: #3730a3; border-radius: 999px;
  padding: 4px 10px; font-size: 12px;
}
.tag-link { cursor: pointer; }
.tag-link:active { background: #2563eb; color: #fff; }
.tag-x {
  cursor: pointer; font-size: 12px; opacity: .75;
  padding: 0 2px; margin-left: 2px; line-height: 1;
}
.tag-x:active { opacity: 1; }
/* 表单内平铺选择：已选 / 可选 两组芯片 */
.te-selected, .te-options { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-bottom: 6px; }
.te-label { font-size: 12px; color: #9ca3af; margin-right: 2px; }
.tag-chip.opt { background: #f3f4f6; color: #374151; border: 1px solid #e5e7eb; cursor: pointer; }
.tag-chip.opt:active { background: #eef2ff; color: #3730a3; border-color: #c7d2fe; }
.te-empty { font-size: 12px; color: #9ca3af; padding: 4px 0; }
.mg-tags {
  display: flex; flex-wrap: wrap; gap: 6px; padding: 10px 12px 0;
}
.chat-tags { padding: 6px 12px 0; background: #fff; border-bottom: 1px solid #f3f4f6; }
.mg-tag-chip {
  background: #fff; color: #374151; border: 1px solid #e5e7eb;
  border-radius: 999px; padding: 4px 10px; font-size: 12px; cursor: pointer;
}
.mg-tag-chip.active { background: #2563eb; color: #fff; border-color: #2563eb; }
.mg-tags-empty { color: #9ca3af; font-size: 12px; }
.detail-tags { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.tag-row {
  display: flex; align-items: center; gap: 8px;
  padding: 9px 4px; border-bottom: 1px solid #f3f4f6;
}
.tag-name { font-size: 14px; font-weight: 600; flex: 1; word-break: break-all; }
.tag-count { color: #9ca3af; font-size: 12px; }
.tag-actions { display: flex; gap: 6px; }
.tag-actions button {
  border: 1px solid #e5e7eb; background: #f3f4f6; color: #374151;
  border-radius: 8px; padding: 5px 10px; font-size: 12px; cursor: pointer;
}
.tag-actions button:last-child { color: #dc2626; border-color: #fecaca; background: #fef2f2; }

/* 标签详情 / 挑选打标 */
#page-tagdetail {
  position: fixed; inset: 0; background: #fff; z-index: 45;
  display: flex; flex-direction: column;
}
.td-btn {
  flex: 1; padding: 13px; font-size: 16px; border: 1px solid #e5e7eb;
  border-radius: 12px; cursor: pointer; background: #fff; color: #374151;
}
.pick-list { max-height: 55vh; }

/* 消息操作：复制图标（气泡正下方，贴着气泡边缘） */
.copy-btn {
  background: none; border: none; color: #cbd5e1;
  font-size: 12px; padding: 2px 6px; margin-top: 2px; cursor: pointer;
}
.copy-btn:active { color: #2563eb; }

/* toast */
.toast {
  position: fixed; left: 50%; bottom: 120px; transform: translateX(-50%);
  background: rgba(17,24,39,.9); color: #fff; font-size: 13px;
  padding: 8px 16px; border-radius: 999px; z-index: 120; pointer-events: none;
}

/* 类型分段与徽标（标签管理挑选/详情） */
.tp-tabs { display: flex; gap: 6px; margin-bottom: 10px; }
.tp-tab {
  flex: 1; padding: 7px 0; border: 1px solid #e5e7eb; background: #fff;
  border-radius: 8px; font-size: 13px; cursor: pointer; color: #374151;
}
.tp-tab.active { background: #2563eb; color: #fff; border-color: #2563eb; }
.type-badge { font-size: 11px; padding: 1px 7px; border-radius: 999px; }
.type-badge.note { background: #eef2ff; color: #3730a3; }
.type-badge.doc { background: #ecfdf5; color: #047857; }

/* ---------- 图片查看器 ---------- */
#viewer {
  position: fixed; inset: 0; background: rgba(0,0,0,.94); z-index: 100;
  display: flex; align-items: center; justify-content: center;
  touch-action: none; user-select: none; -webkit-user-select: none;
}
.viewer-bar {
  position: absolute; top: calc(10px + env(safe-area-inset-top)); left: 0; right: 0;
  display: flex; justify-content: space-between; align-items: center;
  padding: 0 16px; color: #fff; font-size: 14px; z-index: 2;
}
#viewer-close { font-size: 22px; padding: 6px 10px; cursor: pointer; }
.viewer-stage { width: 100%; height: 100%; display: flex; align-items: center; justify-content: center; }
#viewer-img {
  max-width: 100%; max-height: 100%; object-fit: contain;
  transform-origin: center center;
}
.viewer-nav {
  position: absolute; top: 50%; transform: translateY(-50%);
  background: rgba(255,255,255,.15); color: #fff; border: none;
  width: 44px; height: 44px; border-radius: 50%; font-size: 26px;
  cursor: pointer; z-index: 2; display: none;
}
.viewer-nav.prev { left: 10px; }
.viewer-nav.next { right: 10px; }
#viewer.show-nav .viewer-nav { display: block; }

/* 横屏 / 矮屏适配：压缩头部与输入区，避免错乱 */
@media (orientation: landscape) and (max-height: 520px) {
  header { padding: 4px 10px; }
  .chip { padding: 4px 8px; font-size: 11px; }
  .kb-count { display: none; }
  .title { font-size: 14px; }
  main { padding: 10px; }
  footer { padding: 6px 10px calc(6px + env(safe-area-inset-bottom)); }
  .actions { gap: 4px; }
  .msg .bubble { max-width: 76%; }
}

/* 详情页 */
#page-detail {
  position: fixed; inset: 0; background: #fff; z-index: 45;
  display: flex; flex-direction: column;
}
.detail-kind { font-size: 13px; color: #93c5fd; background: rgba(255,255,255,.12); padding: 3px 10px; border-radius: 999px; }
.detail-body { flex: 1; overflow-y: auto; padding: 18px 16px; -webkit-overflow-scrolling: touch; }
.detail-body h2 { margin: 0 0 6px; font-size: 20px; line-height: 1.4; word-break: break-all; }
.detail-meta { font-size: 13px; color: #9ca3af; margin-bottom: 14px; }
.detail-content { white-space: pre-wrap; font-size: 15px; line-height: 1.75; color: #1f2937; }
.detail-actions {
  display: flex; gap: 10px;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom));
  border-top: 1px solid #e5e7eb; background: #fff;
}
.detail-actions .primary, .danger-btn {
  flex: 1; padding: 13px; font-size: 16px; border: none; border-radius: 12px; cursor: pointer;
}
.danger-btn { background: #dc2626; color: #fff; }

/* ---------- 设置面板 ---------- */
.set-row { display: flex; justify-content: space-between; align-items: center; gap: 10px; margin-bottom: 10px; }
.set-row label { flex: 1; font-size: 14px; color: #374151; }
.set-row input, .set-row select {
  width: 120px; padding: 8px 10px; border: 1px solid #d1d5db; border-radius: 10px;
  font-size: 15px; font-family: inherit;
}
