/* ==========================================================================
   LynkLLM CE — 主样式
   设计原则：CSS 变量驱动主题；移动优先 + 媒体查询适配桌面
   ========================================================================== */

/* ---------- 主题变量 ---------- */
:root {
  --bg: #ffffff;
  --bg-soft: #f7f8fa;
  --bg-sidebar: #f4f5f7;
  --bg-elevated: #ffffff;
  --bg-hover: rgba(15, 23, 42, .055);
  --bg-active: rgba(79, 110, 247, .11);
  --bg-input: #ffffff;
  --bg-code: #f6f7f9;
  --bg-overlay: rgba(15, 23, 42, .38);

  --text: #14181f;
  --text-soft: #5b6472;
  --text-muted: #8b94a3;
  --text-invert: #ffffff;

  --border: #e3e6eb;
  --border-strong: #cfd4dc;

  --brand: #4f6ef7;
  --brand-hover: #3f5ce8;
  --brand-soft: rgba(79, 110, 247, .12);
  --brand-text: #3b55d9;

  --danger: #e5484d;
  --danger-soft: rgba(229, 72, 77, .12);
  --success: #16a34a;
  --success-soft: rgba(22, 163, 74, .12);
  --warning: #d97706;
  --warning-soft: rgba(217, 119, 6, .13);
  --info: #0284c7;
  --info-soft: rgba(2, 132, 199, .12);

  --shadow-sm: 0 1px 2px rgba(15, 23, 42, .06), 0 1px 3px rgba(15, 23, 42, .04);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, .08), 0 2px 4px rgba(15, 23, 42, .04);
  --shadow-lg: 0 18px 48px rgba(15, 23, 42, .18), 0 4px 12px rgba(15, 23, 42, .08);

  --radius-sm: 8px;
  --radius: 12px;
  --radius-lg: 16px;
  --radius-xl: 22px;

  --sidebar-w: 33.3333%;
  --sidebar-min: 268px;
  --sidebar-max: 420px;
  --composer-max: 820px;

  --fs-xs: 12px;
  --fs-sm: 13px;
  --fs-base: 14.5px;
  --fs-md: 15.5px;
  --fs-lg: 18px;
  --fs-xl: 22px;

  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei UI", "Microsoft YaHei", Roboto,
    "Helvetica Neue", Arial, "Noto Sans SC", sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas,
    "Liberation Mono", "Courier New", monospace;

  --ease: cubic-bezier(.32, .72, 0, 1);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
}

html[data-theme="dark"] {
  --bg: #0f1115;
  --bg-soft: #14171d;
  --bg-sidebar: #14171d;
  --bg-elevated: #191d24;
  --bg-hover: rgba(255, 255, 255, .06);
  --bg-active: rgba(122, 145, 255, .16);
  --bg-input: #1b1f27;
  --bg-code: #12151a;
  --bg-overlay: rgba(0, 0, 0, .58);

  --text: #e9edf3;
  --text-soft: #a8b1bf;
  --text-muted: #79828f;
  --text-invert: #0f1115;

  --border: #262b34;
  --border-strong: #363d48;

  --brand: #6d88ff;
  --brand-hover: #7f97ff;
  --brand-soft: rgba(109, 136, 255, .16);
  --brand-text: #9db0ff;

  --danger: #f2555a;
  --danger-soft: rgba(242, 85, 90, .16);
  --success: #3fbf6b;
  --success-soft: rgba(63, 191, 107, .16);
  --warning: #e0a33c;
  --warning-soft: rgba(224, 163, 60, .16);
  --info: #38bdf8;
  --info-soft: rgba(56, 189, 248, .16);

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .4);
  --shadow-md: 0 6px 16px rgba(0, 0, 0, .45);
  --shadow-lg: 0 20px 52px rgba(0, 0, 0, .6);
  color-scheme: dark;
}

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

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

body {
  font-family: var(--font);
  font-size: var(--fs-base);
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-size-adjust: 100%;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: none; padding: 0; }
button:disabled { cursor: not-allowed; opacity: .55; }
a { color: var(--brand-text); text-decoration: none; }
a:hover { text-decoration: underline; }
img { max-width: 100%; }
::selection { background: var(--brand-soft); }

:focus-visible {
  outline: 2px solid var(--brand);
  outline-offset: 2px;
  border-radius: 6px;
}

/* 滚动条 */
.scroll-y { scrollbar-width: thin; scrollbar-color: var(--border-strong) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 8px;
  border: 3px solid transparent;
  background-clip: content-box;
}
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); background-clip: content-box; }

[hidden] { display: none !important; }

.mobile-only { display: none !important; }

/* ---------- 启动动画 ---------- */
.boot-loading {
  position: fixed; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 16px;
  background: var(--bg); z-index: 9999;
  transition: opacity .25s ease;
}
.boot-loading.hide { opacity: 0; pointer-events: none; }
.boot-spinner {
  width: 30px; height: 30px; border-radius: 50%;
  border: 2.5px solid var(--border-strong);
  border-top-color: var(--brand);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.boot-text { font-weight: 600; letter-spacing: .06em; color: var(--text-soft); }

/* ==========================================================================
   布局
   ========================================================================== */
.app {
  display: flex;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
}

/* ---------- 侧边栏 ---------- */
.sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-min);
  max-width: var(--sidebar-max);
  display: flex;
  flex-direction: column;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  padding-left: var(--safe-l);
  flex-shrink: 0;
  transition: margin-left .28s var(--ease), transform .28s var(--ease);
}

.sidebar-head {
  display: flex; align-items: center; gap: 8px;
  padding: 14px 14px 8px;
  min-height: 52px;
}
.brand { display: flex; align-items: center; gap: 9px; min-width: 0; }
.brand-mark {
  width: 26px; height: 26px; border-radius: 8px;
  display: grid; place-items: center;
  background: linear-gradient(140deg, var(--brand), #8b5cf6);
  color: #fff; font-weight: 800; font-size: 14px;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
}
.brand-mark.small { width: 32px; height: 32px; border-radius: 10px; font-size: 16px; }
.brand-name {
  font-weight: 700; font-size: var(--fs-md); letter-spacing: -.01em;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.brand-name em { font-style: normal; font-size: 10px; font-weight: 700; color: var(--brand-text); background: var(--brand-soft); padding: 1px 5px; border-radius: 5px; vertical-align: 2px; margin-left: 2px; }

.sidebar-actions { padding: 0 12px 10px; }
.new-chat-btn {
  width: 100%;
  display: flex; align-items: center; justify-content: center; gap: 8px;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--brand); color: #fff;
  font-weight: 600; font-size: var(--fs-base);
  transition: background .16s, transform .1s;
  box-shadow: var(--shadow-sm);
}
.new-chat-btn:hover { background: var(--brand-hover); }
.new-chat-btn:active { transform: scale(.985); }

.search-wrap {
  position: relative;
  margin: 0 12px 10px;
  display: flex; align-items: center;
}
.search-wrap > i {
  position: absolute; left: 11px;
  color: var(--text-muted); font-size: 13px; pointer-events: none;
}
.search-input {
  width: 100%;
  padding: 8px 32px 8px 32px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg-input);
  color: var(--text);
  font-size: var(--fs-sm);
  transition: border-color .16s, box-shadow .16s;
}
.search-input::-webkit-search-cancel-button { display: none; }
.search-input:focus {
  outline: none;
  border-color: var(--brand);
  box-shadow: 0 0 0 3px var(--brand-soft);
}
.clear-search { position: absolute; right: 5px; }

.conv-list {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 2px 8px 12px;
  scrollbar-width: thin;
}

.conv-group-label {
  font-size: var(--fs-xs); font-weight: 600; color: var(--text-muted);
  padding: 12px 8px 5px; letter-spacing: .02em;
}

.conv-item {
  position: relative;
  display: flex; align-items: center; gap: 9px;
  padding: 9px 8px 9px 10px;
  border-radius: 10px;
  color: var(--text);
  cursor: pointer;
  transition: background .13s;
  user-select: none;
}
.conv-item:hover { background: var(--bg-hover); }
.conv-item.active { background: var(--bg-active); }
.conv-item.active .conv-item-title { font-weight: 600; color: var(--brand-text); }

.conv-item-icon {
  font-size: 14px; color: var(--text-muted); flex-shrink: 0; width: 16px; text-align: center;
}
.conv-item.active .conv-item-icon { color: var(--brand); }

.conv-item-body { flex: 1; min-width: 0; }
.conv-item-title {
  font-size: var(--fs-sm); line-height: 1.35;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-item-meta { font-size: 11px; color: var(--text-muted); margin-top: 1px; }

.conv-item-actions {
  display: flex; gap: 2px; flex-shrink: 0;
  opacity: 0; transition: opacity .15s;
}
.conv-item:hover .conv-item-actions,
.conv-item:focus-within .conv-item-actions,
.conv-item.menu-open .conv-item-actions { opacity: 1; }
/* 触屏设备常显 */
@media (hover: none) {
  .conv-item-actions { opacity: .7; }
}

.sidebar-foot {
  border-top: 1px solid var(--border);
  padding: 8px 10px calc(10px + var(--safe-b));
}
.side-btn {
  width: 100%;
  display: flex; align-items: center; gap: 10px;
  padding: 9px 10px; border-radius: 10px;
  color: var(--text-soft); font-size: var(--fs-base); font-weight: 500;
  transition: background .13s, color .13s;
}
.side-btn:hover { background: var(--bg-hover); color: var(--text); }
.side-btn i { font-size: 15px; }

.sidebar-backdrop {
  position: fixed; inset: 0;
  background: var(--bg-overlay);
  z-index: 60;
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  animation: fadeIn .18s ease;
}

/* ---------- 图标按钮 ---------- */
.icon-btn {
  width: 30px; height: 30px;
  display: grid; place-items: center;
  border-radius: 8px;
  color: var(--text-soft);
  font-size: 15px;
  flex-shrink: 0;
  transition: background .13s, color .13s;
}
.icon-btn:hover { background: var(--bg-hover); color: var(--text); }
.icon-btn.tiny { width: 24px; height: 24px; font-size: 12px; }

/* ---------- 主区域 ---------- */
.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  position: relative;
  background: var(--bg);
  padding-right: var(--safe-r);
}

.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px;
  min-height: 54px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: relative;
  z-index: 5;
}
.title-block { flex: 1; min-width: 0; }
.conv-title {
  margin: 0;
  font-size: var(--fs-md); font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  letter-spacing: -.01em;
}
.conv-sub {
  font-size: 11.5px; color: var(--text-muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  margin-top: 1px;
}
.conv-sub:empty { display: none; }

/* ---------- 消息区 ---------- */
.messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 22px 0 18px;
  scrollbar-width: thin;
  scroll-behavior: auto;
  overscroll-behavior: contain;
}

.msg-row {
  max-width: var(--composer-max);
  margin: 0 auto;
  padding: 0 20px;
  display: flex; gap: 12px;
  margin-bottom: 20px;
  animation: msgIn .22s var(--ease);
}
@keyframes msgIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

.msg-avatar {
  width: 30px; height: 30px; border-radius: 9px;
  display: grid; place-items: center;
  font-size: 13px; flex-shrink: 0;
  font-weight: 700;
}
.msg-row.user .msg-avatar { background: var(--bg-active); color: var(--brand-text); }
.msg-row.assistant .msg-avatar {
  background: linear-gradient(140deg, var(--brand), #8b5cf6);
  color: #fff; font-weight: 800; font-size: 14px;
}
.msg-col { flex: 1; min-width: 0; }
.msg-role { font-size: 11.5px; font-weight: 600; color: var(--text-muted); margin-bottom: 5px; letter-spacing: .02em; }

.msg-row.user .msg-body {
  background: var(--bg-soft);
  border: 1px solid var(--border);
  border-radius: 4px var(--radius-lg) var(--radius-lg) var(--radius-lg);
  padding: 11px 15px;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
}
.msg-row.assistant .msg-body {
  padding: 1px 0;
  word-break: break-word;
  overflow-wrap: anywhere;
}

.msg-images {
  display: flex; flex-wrap: wrap; gap: 8px;
  margin-bottom: 10px;
}
.msg-row.assistant .msg-images { margin-bottom: 10px; }
.msg-thumb {
  width: 108px; height: 108px; object-fit: cover;
  border-radius: 10px; border: 1px solid var(--border);
  cursor: zoom-in; background: var(--bg-soft);
  transition: transform .15s;
}
.msg-thumb:hover { transform: scale(1.02); }

.msg-actions {
  display: flex; align-items: center; gap: 4px;
  margin-top: 8px;
  opacity: 0;
  transition: opacity .18s;
  flex-wrap: wrap;
}
.msg-row:hover .msg-actions,
.msg-row:focus-within .msg-actions { opacity: 1; }
@media (hover: none) { .msg-actions { opacity: .75; } }

.msg-action {
  display: inline-flex; align-items: center; gap: 5px;
  height: 26px; padding: 0 8px;
  border-radius: 7px;
  color: var(--text-muted); font-size: var(--fs-xs);
  transition: background .13s, color .13s;
}
.msg-action:hover { background: var(--bg-hover); color: var(--text); }

.token-usage {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 11px; color: var(--text-muted);
  padding: 2px 8px; border-radius: 6px;
  background: var(--bg-soft);
  border: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.token-usage i { font-size: 10px; }
.token-usage b { font-weight: 600; color: var(--text-soft); }

.msg-badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 11px; padding: 1px 7px; border-radius: 999px;
  background: var(--bg-soft); border: 1px solid var(--border);
  color: var(--text-muted);
}
.msg-badge.warn { background: var(--warning-soft); border-color: transparent; color: var(--warning); }
.msg-badge.err { background: var(--danger-soft); border-color: transparent; color: var(--danger); }

.msg-error {
  border: 1px solid var(--danger);
  background: var(--danger-soft);
  border-radius: var(--radius);
  padding: 10px 13px;
  font-size: var(--fs-sm);
  color: var(--danger);
  word-break: break-word;
}
.msg-error .err-title { font-weight: 700; margin-bottom: 3px; display: flex; align-items: center; gap: 6px; }
.msg-error pre { margin: 6px 0 0; font-size: 11.5px; white-space: pre-wrap; font-family: var(--font-mono); opacity: .9; }

/* 思考中指示 */
.thinking-indicator { display: inline-flex; align-items: center; gap: 8px; color: var(--text-muted); font-size: var(--fs-sm); padding: 3px 0; }
.thinking-dots { display: inline-flex; gap: 4px; }
.thinking-dots span {
  width: 6px; height: 6px; border-radius: 50%; background: var(--brand);
  animation: bounce 1.25s infinite ease-in-out;
}
.thinking-dots span:nth-child(2) { animation-delay: .16s; }
.thinking-dots span:nth-child(3) { animation-delay: .32s; }
@keyframes bounce {
  0%, 60%, 100% { transform: translateY(0); opacity: .35; }
  30% { transform: translateY(-4px); opacity: 1; }
}

.cursor-blink {
  display: inline-block; width: 7px; height: 15px;
  background: var(--brand); border-radius: 1.5px;
  vertical-align: -2px; margin-left: 2px;
  animation: blink 1s steps(2, start) infinite;
}
@keyframes blink { to { visibility: hidden; } }

/* 空状态 */
.empty-state {
  height: 100%;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 6px; padding: 30px 22px; text-align: center;
}
.empty-logo {
  width: 58px; height: 58px; border-radius: 18px;
  display: grid; place-items: center;
  background: linear-gradient(140deg, var(--brand), #8b5cf6);
  color: #fff; font-size: 26px; font-weight: 800;
  margin-bottom: 10px;
  box-shadow: var(--shadow-md);
}
.empty-title { font-size: var(--fs-lg); font-weight: 700; letter-spacing: -.01em; }
.empty-desc { color: var(--text-muted); font-size: var(--fs-sm); max-width: 420px; }
.empty-tips {
  margin-top: 18px; display: flex; flex-wrap: wrap; gap: 8px;
  justify-content: center; max-width: 560px;
}
.empty-tip {
  display: inline-flex; align-items: center; gap: 7px;
  padding: 8px 13px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--bg-soft);
  font-size: var(--fs-sm); color: var(--text-soft);
  transition: border-color .15s, color .15s, background .15s;
}
.empty-tip:hover { border-color: var(--brand); color: var(--brand-text); background: var(--brand-soft); }

.scroll-bottom-btn {
  position: absolute;
  bottom: 156px;
  left: 50%; transform: translateX(-50%);
  width: 34px; height: 34px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-md);
  color: var(--text-soft); z-index: 8;
  animation: fadeIn .18s ease;
}
.scroll-bottom-btn:hover { color: var(--brand); border-color: var(--brand); }
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

/* ---------- 输入区 ---------- */
.composer-wrap {
  padding: 0 20px calc(12px + var(--safe-b));
  background: var(--bg);
}
.composer {
  max-width: var(--composer-max);
  margin: 0 auto;
  border: 1px solid var(--border);
  border-radius: var(--radius-xl);
  background: var(--bg-elevated);
  box-shadow: var(--shadow-sm);
  transition: border-color .18s, box-shadow .18s;
  padding: 6px 8px 6px;
}
.composer.is-focus { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.composer.is-dragover { border-color: var(--brand); border-style: dashed; background: var(--brand-soft); }

.composer-input-row { display: flex; }
.composer-input {
  flex: 1; width: 100%;
  border: none; background: transparent;
  resize: none; outline: none;
  padding: 8px 8px 4px;
  font-size: var(--fs-md);
  line-height: 1.55;
  max-height: 220px;
  overflow-y: auto;
  font-family: inherit;
  scrollbar-width: thin;
}
.composer-input::placeholder { color: var(--text-muted); }

.composer-bar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 2px 2px 0;
}
.composer-bar-left, .composer-bar-right { display: flex; align-items: center; gap: 6px; min-width: 0; }
.composer-bar-left { overflow-x: auto; scrollbar-width: none; }
.composer-bar-left::-webkit-scrollbar { display: none; }

.chip-btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 30px; padding: 0 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  color: var(--text-soft);
  font-size: var(--fs-xs); font-weight: 500;
  white-space: nowrap; flex-shrink: 0;
  transition: border-color .14s, color .14s, background .14s;
}
.chip-btn:hover { border-color: var(--border-strong); color: var(--text); }
.chip-btn i { font-size: 13px; }
.chip-btn.model-chip { max-width: 230px; }
.chip-btn.model-chip #modelChipText {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 150px;
}
.chip-btn.model-chip i:last-child { font-size: 10px; opacity: .6; }
.chip-btn .bi-cpu { color: var(--brand); }
.chip-btn.active { border-color: var(--brand); color: var(--brand-text); background: var(--brand-soft); }

.char-count { font-size: 11px; color: var(--text-muted); font-variant-numeric: tabular-nums; }

.send-btn {
  width: 34px; height: 34px; border-radius: 50%;
  display: grid; place-items: center;
  background: var(--brand); color: #fff;
  font-size: 15px; flex-shrink: 0;
  transition: background .15s, transform .1s;
  box-shadow: var(--shadow-sm);
}
.send-btn:hover:not(:disabled) { background: var(--brand-hover); }
.send-btn:active:not(:disabled) { transform: scale(.94); }
.send-btn:disabled { background: var(--border-strong); color: var(--bg); opacity: .6; }
.send-btn.stop { background: var(--danger); }
.send-btn.stop:hover { background: #cf3d43; }

.composer-hint {
  max-width: var(--composer-max);
  margin: 6px auto 0;
  text-align: center;
  font-size: 11px;
  color: var(--text-muted);
  min-height: 14px;
}
.composer-hint.warn { color: var(--warning); }
.composer-hint.err { color: var(--danger); }

/* 附件预览 */
.attachments {
  display: flex; flex-wrap: wrap; gap: 8px;
  padding: 8px 8px 4px;
}
.attach-item {
  position: relative;
  width: 62px; height: 62px; border-radius: 10px;
  overflow: hidden; border: 1px solid var(--border);
  background: var(--bg-soft);
}
.attach-item img { width: 100%; height: 100%; object-fit: cover; display: block; }
.attach-remove {
  position: absolute; top: 2px; right: 2px;
  width: 18px; height: 18px; border-radius: 50%;
  display: grid; place-items: center;
  background: rgba(0, 0, 0, .62); color: #fff; font-size: 10px;
}
.attach-remove:hover { background: var(--danger); }

/* ==========================================================================
   弹窗 / 通用控件
   ========================================================================== */
.modal-backdrop {
  position: fixed; inset: 0;
  background: var(--bg-overlay);
  display: flex; align-items: center; justify-content: center;
  z-index: 200; padding: 18px;
  animation: fadeIn .16s ease;
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
}
.modal-backdrop.stacked { z-index: 260; }

.modal {
  width: 100%;
  background: var(--bg-elevated);
  border-radius: var(--radius-lg);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  display: flex; flex-direction: column;
  max-height: calc(100vh - 36px);
  max-height: calc(100dvh - 36px);
  overflow: hidden;
  animation: modalIn .2s var(--ease);
}
@keyframes modalIn { from { opacity: 0; transform: translateY(10px) scale(.99); } to { opacity: 1; transform: none; } }
.modal-sm { max-width: 400px; }
.modal-md { max-width: 560px; }
.modal-lg { max-width: 720px; }

.modal-head {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.modal-title { margin: 0; font-size: var(--fs-md); font-weight: 700; flex: 1; letter-spacing: -.01em; }
.modal-body {
  padding: 16px;
  overflow-y: auto;
  flex: 1;
  scrollbar-width: thin;
}
.modal-text { margin: 0; color: var(--text-soft); font-size: var(--fs-base); line-height: 1.65; }
.modal-foot {
  display: flex; align-items: center; justify-content: flex-end; gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  flex-shrink: 0;
  background: var(--bg-soft);
  padding-bottom: calc(12px + var(--safe-b));
}
.modal-foot.space-between { justify-content: space-between; }
.foot-actions { display: flex; gap: 8px; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 34px; padding: 0 14px;
  border-radius: 9px;
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  color: var(--text);
  font-size: var(--fs-sm); font-weight: 600;
  transition: background .14s, border-color .14s, color .14s;
  white-space: nowrap;
}
.btn:hover { background: var(--bg-hover); }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-hover); border-color: var(--brand-hover); }
.btn.danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn.danger:hover { filter: brightness(1.08); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--bg-hover); }
.btn.sm { height: 28px; padding: 0 10px; font-size: var(--fs-xs); }

.icon-btn.danger:hover { background: var(--danger-soft); color: var(--danger); }

/* Tab */
.tabs {
  display: flex; gap: 4px;
  padding: 10px 16px 0;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.tab {
  position: relative;
  padding: 8px 12px 11px;
  font-size: var(--fs-base); font-weight: 600;
  color: var(--text-muted);
  transition: color .15s;
}
.tab::after {
  content: ''; position: absolute; left: 8px; right: 8px; bottom: -1px;
  height: 2px; border-radius: 2px; background: transparent;
  transition: background .18s;
}
.tab:hover { color: var(--text-soft); }
.tab.active { color: var(--brand-text); }
.tab.active::after { background: var(--brand); }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* 设置项 */
.setting-group { margin-bottom: 18px; }
.setting-group:last-child { margin-bottom: 0; }
.group-title {
  font-size: var(--fs-xs); font-weight: 700; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: .06em;
  margin: 0 0 8px 2px;
}
.setting-row {
  display: flex; align-items: center; gap: 14px;
  padding: 11px 12px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  transition: background .14s, border-color .14s;
}
.setting-row + .setting-row { margin-top: 2px; }
.setting-row:hover { background: var(--bg-soft); border-color: var(--border); }
.setting-label { display: flex; align-items: flex-start; gap: 11px; flex: 1; min-width: 0; }
.setting-label > i { font-size: 16px; color: var(--text-muted); margin-top: 2px; flex-shrink: 0; }
.sl-title { font-size: var(--fs-base); font-weight: 600; }
.sl-desc { font-size: var(--fs-xs); color: var(--text-muted); line-height: 1.5; margin-top: 1px; }

.segmented {
  display: inline-flex; padding: 2px;
  background: var(--bg-soft); border: 1px solid var(--border);
  border-radius: 10px; flex-shrink: 0;
}
.segmented button {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 5px 11px; border-radius: 8px;
  font-size: var(--fs-xs); font-weight: 600; color: var(--text-muted);
  transition: background .16s, color .16s, box-shadow .16s;
  white-space: nowrap;
}
.segmented button:hover { color: var(--text); }
.segmented button.active {
  background: var(--bg-elevated); color: var(--brand-text);
  box-shadow: var(--shadow-sm);
}

.select-wrap { position: relative; flex-shrink: 0; }
.select-wrap > i {
  position: absolute; right: 10px; top: 50%; transform: translateY(-50%);
  font-size: 11px; color: var(--text-muted); pointer-events: none;
}
.select {
  appearance: none; -webkit-appearance: none;
  height: 32px; padding: 0 30px 0 12px;
  border-radius: 9px;
  border: 1px solid var(--border-strong);
  background: var(--bg-elevated);
  font-size: var(--fs-sm); font-weight: 500;
  cursor: pointer;
  max-width: 240px;
}
.select:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }

/* 开关 */
.switch { position: relative; display: inline-flex; flex-shrink: 0; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 0; height: 0; }
.switch-track {
  width: 42px; height: 24px; border-radius: 999px;
  background: var(--border-strong);
  display: inline-block; position: relative;
  transition: background .2s var(--ease);
}
.switch-thumb {
  position: absolute; top: 2px; left: 2px;
  width: 20px; height: 20px; border-radius: 50%;
  background: #fff; box-shadow: var(--shadow-sm);
  transition: transform .2s var(--ease);
}
.switch input:checked + .switch-track { background: var(--brand); }
.switch input:checked + .switch-track .switch-thumb { transform: translateX(18px); }
.switch input:focus-visible + .switch-track { box-shadow: 0 0 0 3px var(--brand-soft); }

.check-row {
  display: flex; align-items: center; gap: 9px;
  font-size: var(--fs-base); cursor: pointer; padding: 6px 2px;
  user-select: none;
}
.check-row input[type="checkbox"] {
  width: 17px; height: 17px; accent-color: var(--brand); cursor: pointer; flex-shrink: 0;
}
.toggles { display: flex; flex-direction: column; gap: 2px; }
.toggles .check-row + .check-row { border-top: 1px dashed var(--border); padding-top: 10px; margin-top: 4px; }

.data-stats {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 8px; margin-bottom: 6px;
}
.stat-card {
  padding: 10px 12px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--bg-soft);
}
.stat-val { font-size: var(--fs-lg); font-weight: 700; font-variant-numeric: tabular-nums; }
.stat-label { font-size: 11px; color: var(--text-muted); margin-top: 1px; }

.row-actions { display: flex; gap: 8px; flex-shrink: 0; }

.about-group { padding-top: 4px; }
.about-line { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: var(--radius); background: var(--bg-soft); }

/* ---------- 模型列表 ---------- */
.models-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin-bottom: 14px; flex-wrap: wrap;
}
.model-list { display: flex; flex-direction: column; gap: 8px; }

.model-card {
  display: flex; align-items: center; gap: 12px;
  padding: 12px 13px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--bg);
  transition: border-color .15s, background .15s;
}
.model-card:hover { border-color: var(--border-strong); }
.model-card.is-current { border-color: var(--brand); background: var(--brand-soft); }
.model-card-icon {
  width: 36px; height: 36px; border-radius: 10px;
  display: grid; place-items: center;
  background: var(--bg-active); color: var(--brand-text);
  font-size: 16px; flex-shrink: 0;
}
.model-card-body { flex: 1; min-width: 0; }
.model-card-name { font-size: var(--fs-base); font-weight: 600; display: flex; align-items: center; gap: 7px; flex-wrap: wrap; }
.model-card-meta {
  font-size: var(--fs-xs); color: var(--text-muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  margin-top: 2px; font-family: var(--font-mono);
}
.model-tag {
  display: inline-flex; align-items: center; gap: 3px;
  font-size: 10px; font-weight: 600; padding: 1px 6px; border-radius: 5px;
  background: var(--bg-active); color: var(--brand-text);
}
.model-tag.muted { background: var(--bg-hover); color: var(--text-muted); }
.model-card-actions { display: flex; gap: 2px; flex-shrink: 0; }

.empty-inline {
  text-align: center; padding: 28px 16px; color: var(--text-muted);
  font-size: var(--fs-sm); border: 1px dashed var(--border);
  border-radius: var(--radius);
}
.empty-inline i { font-size: 26px; display: block; margin-bottom: 8px; opacity: .6; }

/* ---------- 表单字段 ---------- */
.field { margin-bottom: 13px; }
.field-label {
  display: block; font-size: var(--fs-sm); font-weight: 600;
  margin-bottom: 5px; color: var(--text-soft);
}
.field-hint { font-size: 11px; color: var(--text-muted); margin-top: 4px; line-height: 1.5; }
.field-group-title {
  font-size: var(--fs-xs); font-weight: 700; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: .06em;
  margin: 18px 0 6px;
  padding-top: 12px; border-top: 1px solid var(--border);
}
.input {
  width: 100%;
  height: 36px; padding: 0 11px;
  border-radius: 9px;
  border: 1px solid var(--border-strong);
  background: var(--bg-input);
  font-size: var(--fs-sm);
  transition: border-color .15s, box-shadow .15s;
}
.input:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
.input.mono { font-family: var(--font-mono); font-size: 12.5px; }
.input.textarea {
  height: auto; padding: 9px 11px; resize: vertical;
  line-height: 1.55; font-family: inherit; min-height: 66px;
}
.input-with-btn { display: flex; gap: 6px; align-items: center; }
.input-with-btn .input { flex: 1; }
.input-with-btn .icon-btn { border: 1px solid var(--border-strong); border-radius: 9px; width: 36px; height: 36px; }

.notice {
  display: flex; gap: 9px; align-items: flex-start;
  padding: 10px 12px; border-radius: var(--radius);
  background: var(--info-soft); color: var(--info);
  font-size: var(--fs-xs); line-height: 1.55; margin-bottom: 16px;
}
.notice i { font-size: 14px; margin-top: 1px; flex-shrink: 0; }

.test-result {
  margin-top: 14px; padding: 11px 13px;
  border-radius: var(--radius);
  font-size: var(--fs-sm); line-height: 1.55;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  word-break: break-word;
}
.test-result.ok { border-color: transparent; background: var(--success-soft); color: var(--success); }
.test-result.err { border-color: transparent; background: var(--danger-soft); color: var(--danger); }
.test-result.pending { border-color: transparent; background: var(--info-soft); color: var(--info); }
.test-result .tr-title { font-weight: 700; display: flex; align-items: center; gap: 6px; }
.test-result pre {
  margin: 7px 0 0; font-family: var(--font-mono); font-size: 11.5px;
  white-space: pre-wrap; opacity: .92; max-height: 160px; overflow: auto;
}

/* ==========================================================================
   Popover（模型/思考强度选择）
   ========================================================================== */
.popover-layer {
  position: fixed; inset: 0; z-index: 150;
}
.popover {
  position: absolute;
  min-width: 210px; max-width: min(340px, calc(100vw - 24px));
  max-height: 340px; overflow-y: auto;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  padding: 5px;
  animation: popIn .16s var(--ease);
  scrollbar-width: thin;
}
@keyframes popIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.popover.up { transform-origin: bottom center; }
.popover-label {
  font-size: 10.5px; font-weight: 700; color: var(--text-muted);
  text-transform: uppercase; letter-spacing: .06em;
  padding: 7px 9px 4px;
}
.popover-item {
  display: flex; align-items: center; gap: 9px;
  width: 100%; padding: 8px 9px;
  border-radius: 8px; text-align: left;
  font-size: var(--fs-sm); color: var(--text);
  transition: background .12s;
}
.popover-item:hover { background: var(--bg-hover); }
.popover-item.active { background: var(--bg-active); color: var(--brand-text); font-weight: 600; }
.popover-item .pi-body { flex: 1; min-width: 0; }
.popover-item .pi-title { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.popover-item .pi-desc {
  font-size: 11px; color: var(--text-muted); font-weight: 400;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  font-family: var(--font-mono);
}
.popover-item > i.bi-check2 { color: var(--brand); font-size: 14px; }
.popover-sep { height: 1px; background: var(--border); margin: 5px 4px; }

/* 对话操作菜单 */
.menu-item {
  display: flex; align-items: center; gap: 9px;
  width: 100%; padding: 8px 10px; border-radius: 8px;
  font-size: var(--fs-sm); text-align: left; color: var(--text);
}
.menu-item:hover { background: var(--bg-hover); }
.menu-item.danger { color: var(--danger); }
.menu-item.danger:hover { background: var(--danger-soft); }
.menu-item i { font-size: 14px; opacity: .8; }

/* ==========================================================================
   Toast
   ========================================================================== */
.toast-layer {
  position: fixed;
  top: calc(14px + env(safe-area-inset-top, 0px));
  left: 50%; transform: translateX(-50%);
  z-index: 300;
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  pointer-events: none;
  width: max-content; max-width: calc(100vw - 24px);
}
.toast {
  display: flex; align-items: center; gap: 9px;
  min-width: 190px; max-width: min(440px, calc(100vw - 24px));
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-lg);
  font-size: var(--fs-sm); font-weight: 500;
  pointer-events: auto;
  animation: toastIn .26s var(--ease);
}
.toast.out { animation: toastOut .22s ease forwards; }
@keyframes toastIn { from { opacity: 0; transform: translateY(-14px) scale(.97); } to { opacity: 1; transform: none; } }
@keyframes toastOut { to { opacity: 0; transform: translateY(-10px) scale(.97); } }
.toast i.t-icon { font-size: 16px; flex-shrink: 0; }
.toast.success i.t-icon { color: var(--success); }
.toast.error i.t-icon { color: var(--danger); }
.toast.info i.t-icon { color: var(--info); }
.toast.warning i.t-icon { color: var(--warning); }
.toast .t-body { flex: 1; min-width: 0; word-break: break-word; line-height: 1.45; }
.toast .t-close { color: var(--text-muted); font-size: 12px; flex-shrink: 0; }
.toast .t-close:hover { color: var(--text); }

/* ==========================================================================
   Lightbox
   ========================================================================== */
.lightbox {
  position: fixed; inset: 0; z-index: 400;
  background: rgba(0, 0, 0, .86);
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
  padding: calc(24px + env(safe-area-inset-top, 0px)) calc(24px + env(safe-area-inset-right, 0px))
           calc(24px + env(safe-area-inset-bottom, 0px)) calc(24px + env(safe-area-inset-left, 0px));
  animation: fadeIn .18s ease;
  cursor: zoom-out;
  overscroll-behavior: contain;
}
.lightbox img {
  max-width: 100%; max-height: 100%; border-radius: 10px; object-fit: contain;
  cursor: default; box-shadow: 0 18px 60px rgba(0, 0, 0, .55);
  animation: lbIn .2s cubic-bezier(.22, .8, .28, 1);
}
body.lb-open { overflow: hidden; }
/* 可放大的图片统一使用放大指针 */
.md .md-img[data-zoom], .msg-images .msg-thumb[data-zoom] { cursor: zoom-in; }

@keyframes lbIn { from { opacity: 0; transform: scale(.94); } to { opacity: 1; transform: scale(1); } }
.lightbox-close {
  position: absolute; top: calc(14px + env(safe-area-inset-top, 0px)); right: 14px;
  color: #fff; background: rgba(255, 255, 255, .14); width: 36px; height: 36px;
}
.lightbox-close:hover { background: rgba(255, 255, 255, .26); color: #fff; }

/* ==========================================================================
   响应式
   ========================================================================== */

/* 中等屏幕：收窄侧栏 */
@media (max-width: 1100px) {
  :root { --composer-max: 720px; }
}

/* 窄屏桌面 / 平板：侧栏收起为抽屉 */
@media (max-width: 860px) {
  .mobile-only { display: grid !important; }
  #btnSidebarToggle, #btnSidebarOpen { display: grid !important; }

  .sidebar {
    position: fixed; top: 0; bottom: 0; left: 0;
    width: min(84vw, 320px);
    min-width: 0; max-width: none;
    z-index: 70;
    transform: translateX(-102%);
    box-shadow: var(--shadow-lg);
    padding-top: env(safe-area-inset-top, 0px);
  }
  .sidebar.open { transform: none; }

  .msg-row { padding: 0 14px; }
  .composer-wrap { padding: 0 12px calc(10px + var(--safe-b)); }
  .scroll-bottom-btn { bottom: 150px; }
  .topbar { padding: 8px 10px; }
}

@media (max-width: 560px) {
  :root {
    --fs-base: 14px;
    --fs-md: 15px;
    --fs-lg: 17px;
  }
  .messages { padding: 14px 0 10px; }
  .msg-row { gap: 9px; margin-bottom: 16px; padding: 0 11px; }
  .msg-avatar { width: 26px; height: 26px; border-radius: 8px; font-size: 11px; }
  .msg-row.assistant .msg-avatar { font-size: 12px; }
  .msg-thumb { width: 88px; height: 88px; }
  .composer { border-radius: 18px; }
  .composer-input { font-size: 15px; } /* 防止 iOS 自动缩放 */
  .chip-btn.model-chip #modelChipText { max-width: 96px; }
  .modal-backdrop { padding: 0; align-items: flex-end; }
  .modal {
    max-height: 92dvh;
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    max-width: 100% !important;
  }
  .modal-foot { padding-bottom: calc(12px + var(--safe-b)); }
  .setting-row { flex-direction: column; align-items: stretch; gap: 9px; }
  .setting-row .segmented, .setting-row .select-wrap, .setting-row .switch { align-self: flex-start; }
  .select { max-width: none; width: 100%; min-width: 150px; }
  .setting-row.danger-row, .setting-row .row-actions { align-self: stretch; }
  .row-actions { justify-content: flex-start; }
  .empty-tips { flex-direction: column; align-items: stretch; }
  .empty-tip { justify-content: center; }
}

/* 极窄屏 */
@media (max-width: 380px) {
  .brand-name { font-size: var(--fs-base); }
  .chip-btn { padding: 0 8px; }
  .chip-btn.model-chip #modelChipText { max-width: 70px; }
}

/* 横屏矮屏 */
@media (max-height: 520px) and (orientation: landscape) {
  .composer-input { max-height: 110px; }
  .modal { max-height: 96dvh; }
}

/* 高对比度偏好 */
@media (prefers-contrast: more) {
  :root { --border: #9aa3b2; --border-strong: #6b7484; }
  html[data-theme="dark"] { --border: #5a6373; --border-strong: #7c8698; }
}

/* 减少动画 */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .001ms !important;
  }
  .messages { scroll-behavior: auto; }
}

/* 打印 */
@media print {
  .sidebar, .composer-wrap, .topbar, .toast-layer { display: none !important; }
  .messages { overflow: visible; padding: 0; }
  .app { height: auto; display: block; }
}

/* 拖拽上传提示 */
.drop-overlay {
  position: fixed; inset: 0; z-index: 190;
  display: flex; align-items: center; justify-content: center;
  background: var(--bg-overlay);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  pointer-events: none;
}
.drop-overlay-inner {
  padding: 26px 34px; border-radius: var(--radius-lg);
  border: 2px dashed var(--brand);
  background: var(--bg-elevated);
  text-align: center; box-shadow: var(--shadow-lg);
}
.drop-overlay-inner i { font-size: 30px; color: var(--brand); }
.drop-overlay-inner div { margin-top: 8px; font-weight: 600; }

/* 骨架屏（模型列表 laoding） */
.skeleton {
  height: 54px; border-radius: var(--radius);
  background: linear-gradient(90deg, var(--bg-soft) 25%, var(--bg-hover) 37%, var(--bg-soft) 63%);
  background-size: 400% 100%;
  animation: shimmer 1.3s ease infinite;
}
@keyframes shimmer { 0% { background-position: 100% 50%; } 100% { background-position: 0 50%; } }
