:root {
  --bg: #f7f7f8;
  --panel: #ffffff;
  --border: #e4e4e7;
  --text: #1f2328;
  --text-dim: #6b7280;
  --accent: #4f46e5;
  --accent-soft: #eef2ff;
  --user-bubble: #4f46e5;
  --ai-bubble: #ffffff;
  --radius: 12px;
  --sidebar-w: 260px;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body, #app { height: 100%; }
body {
  font-family: -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

#app { display: flex; }

/* ---------- 侧边栏 ---------- */
#sidebar {
  width: var(--sidebar-w);
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: margin-left .2s ease;
}
#sidebar.hidden { margin-left: calc(var(--sidebar-w) * -1); }
.sidebar-head { padding: 16px 14px 10px; }
.logo { font-weight: 700; font-size: 17px; margin-bottom: 12px; }
.btn-new {
  width: 100%;
  padding: 9px 0;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
  background: none;
  color: var(--text);
  font-size: 14px;
  cursor: pointer;
  transition: all .15s;
}
.btn-new:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.chat-list { flex: 1; overflow-y: auto; padding: 6px 10px; }
.chat-item {
  padding: 9px 12px;
  border-radius: 8px;
  font-size: 13.5px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text-dim);
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 6px;
}
.chat-item:hover { background: var(--bg); }
.chat-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.chat-item .del {
  opacity: 0; border: none; background: none; cursor: pointer;
  color: var(--text-dim); font-size: 13px; padding: 0 2px; flex-shrink: 0;
}
.chat-item:hover .del { opacity: 1; }
.chat-item .del:hover { color: #dc2626; }
.sidebar-foot {
  padding: 12px 16px;
  font-size: 12px;
  color: var(--text-dim);
  border-top: 1px solid var(--border);
  display: flex; align-items: center; gap: 6px;
}
.foot-dot { width: 7px; height: 7px; border-radius: 50%; background: #22c55e; display: inline-block; }

/* ---------- 主区域 ---------- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
#topbar {
  height: 54px;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.icon-btn {
  border: none; background: none; font-size: 17px; cursor: pointer;
  color: var(--text-dim); padding: 6px 8px; border-radius: 8px;
}
.icon-btn:hover { background: var(--bg); color: var(--text); }
#model-select {
  padding: 7px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  font-size: 13.5px;
  color: var(--text);
  cursor: pointer;
  outline: none;
}

/* ---------- 消息区 ---------- */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px 12px;
  scroll-behavior: smooth;
}
.welcome { text-align: center; margin-top: 18vh; color: var(--text-dim); }
.welcome-logo { font-size: 42px; margin-bottom: 12px; }
.welcome h1 { font-size: 22px; color: var(--text); margin-bottom: 8px; font-weight: 600; }
.welcome p { font-size: 14px; }

.msg-wrap { display: flex; margin-bottom: 18px; gap: 10px; max-width: 800px; margin-left: auto; margin-right: auto; }
.msg-wrap.user { flex-direction: row-reverse; }
.avatar {
  width: 34px; height: 34px; border-radius: 9px; flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 16px; margin-top: 2px;
}
.msg-wrap.user .avatar { background: var(--accent-soft); }
.msg-wrap.ai .avatar { background: linear-gradient(135deg, #6366f1, #a855f7); color: #fff; }
.bubble {
  max-width: 78%;
  padding: 11px 15px;
  border-radius: var(--radius);
  font-size: 14.5px;
  line-height: 1.7;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg-wrap.user .bubble { background: var(--user-bubble); color: #fff; border-bottom-right-radius: 4px; }
.msg-wrap.ai .bubble { background: var(--ai-bubble); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.bubble .cursor {
  display: inline-block; width: 7px; height: 15px;
  background: var(--accent); vertical-align: text-bottom;
  animation: blink 1s step-end infinite; border-radius: 1px;
}
@keyframes blink { 50% { opacity: 0; } }
.bubble.error { border-color: #fca5a5; color: #b91c1c; background: #fef2f2; }
.typing { display: inline-flex; gap: 4px; padding: 4px 0; }
.typing i { width: 6px; height: 6px; border-radius: 50%; background: var(--text-dim); animation: bounce 1.2s infinite; }
.typing i:nth-child(2) { animation-delay: .2s; }
.typing i:nth-child(3) { animation-delay: .4s; }
@keyframes bounce { 0%,60%,100% { transform: none; opacity:.4 } 30% { transform: translateY(-4px); opacity:1 } }

/* ---------- 工具条 + 输入区 ---------- */
.composer-zone {
  padding: 0 16px 14px;
  max-width: 820px;
  width: 100%;
  margin: 0 auto;
}
.toolbar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
  flex-wrap: wrap;
}
.modes { display: flex; gap: 6px; }
.mode-btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text-dim);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
  transition: all .15s;
}
.mode-btn:hover { border-color: var(--accent); color: var(--accent); }
.mode-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}
.lib-btn {
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 13px;
  cursor: pointer;
  font-family: inherit;
  transition: all .15s;
}
.lib-btn:hover { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.lib-count {
  display: inline-block;
  min-width: 18px;
  text-align: center;
  background: var(--accent);
  color: #fff;
  border-radius: 999px;
  font-size: 11px;
  padding: 1px 5px;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 0;
}
#input {
  flex: 1;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px 16px;
  font-size: 14.5px;
  font-family: inherit;
  line-height: 1.5;
  max-height: 160px;
  outline: none;
  background: var(--panel);
  transition: border-color .15s;
}
#input:focus { border-color: var(--accent); }
#send-btn {
  width: 44px; height: 44px;
  border: none; border-radius: 12px;
  background: var(--accent); color: #fff;
  font-size: 17px; cursor: pointer;
  flex-shrink: 0;
  transition: background .15s, transform .1s;
}
#send-btn:hover { background: #4338ca; }
#send-btn:disabled { background: #c7c7cc; cursor: not-allowed; }
#send-btn.stop { background: #ef4444; }

/* ---------- 聊天里的媒体 ---------- */
.media-bubble { padding: 6px !important; }
.media-bubble img {
  max-width: 320px;
  border-radius: 10px;
  display: block;
  cursor: zoom-in;
}
.media-bubble video {
  max-width: 320px;
  border-radius: 10px;
  display: block;
}
.media-cap {
  font-size: 12px;
  color: var(--text-dim);
  padding: 7px 10px 4px;
  max-width: 320px;
  word-break: break-all;
}
.lib-saved-tag {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11.5px;
  color: #059669;
  background: #ecfdf5;
  border-radius: 999px;
  padding: 2px 9px;
  margin: 8px 10px 6px;
}

/* ---------- 素材库抽屉 ---------- */
.lib-overlay {
  position: fixed; inset: 0; z-index: 90;
  background: rgba(15,17,22,.4);
}
.lib-drawer {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 95;
  width: min(420px, 92vw);
  background: var(--panel);
  border-left: 1px solid var(--border);
  display: flex; flex-direction: column;
  box-shadow: -8px 0 30px rgba(0,0,0,.1);
  animation: slideIn .22s ease;
}
@keyframes slideIn { from { transform: translateX(30px); opacity: 0; } to { transform: none; opacity: 1; } }
.lib-drawer[hidden] { display: none; }
.lib-overlay[hidden] { display: none; }

/* ---------- 设置抽屉 ---------- */
.settings-drawer { width: min(440px, 94vw); }
.settings-body {
  flex: 1; overflow-y: auto;
  padding: 16px;
  display: flex; flex-direction: column; gap: 14px;
}
.setting-section {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--bg);
  padding: 14px 14px 12px;
}
.setting-section h4 {
  font-size: 12.5px; font-weight: 600;
  color: var(--text-dim);
  letter-spacing: .8px;
  margin-bottom: 12px;
}
.setting-grid { display: flex; flex-direction: column; gap: 12px; }
.setting-grid .set-item { display: flex; flex-direction: column; gap: 6px; }
.setting-grid .set-item > span { font-size: 13px; font-weight: 500; color: var(--text); }
.setting-grid .set-item select,
.setting-grid .set-item input[type="text"] {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel);
  color: var(--text);
  font-size: 13.5px;
  font-family: inherit;
  outline: none;
  transition: border-color .15s;
}
.setting-grid .set-item select:focus,
.setting-grid .set-item input[type="text"]:focus { border-color: var(--accent); }
.set-items {
  display: grid; grid-template-columns: 1fr 1fr; gap: 10px;
}
.set-items .set-item {
  display: flex; align-items: center; gap: 8px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
  transition: border-color .15s;
}
.set-items .set-item:hover { border-color: var(--accent); }
.set-items .set-item span { font-size: 13px; }
.set-items .set-item input[type="checkbox"] {
  width: 16px; height: 16px;
  accent-color: var(--accent);
  cursor: pointer;
  flex-shrink: 0;
}
.setting-tip {
  font-size: 12px;
  color: var(--text-dim);
  line-height: 1.6;
  margin-top: 10px;
}
.settings-foot {
  flex-shrink: 0;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--panel);
}
.btn {
  border: none; border-radius: 10px;
  padding: 11px 16px;
  font-size: 14px;
  font-family: inherit;
  cursor: pointer;
  transition: background .15s;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: #4338ca; }
.btn-block { width: 100%; display: block; }
@media (max-width: 480px) {
  .set-items { grid-template-columns: 1fr; }
}
.lib-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 16px 10px; border-bottom: 1px solid var(--border);
}
.lib-head h3 { font-size: 16px; }
.lib-tabs {
  display: flex; gap: 6px; flex-wrap: wrap;
  padding: 10px 14px; border-bottom: 1px solid var(--border);
}
.lib-tab {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-dim);
  border-radius: 999px;
  padding: 4px 12px;
  font-size: 12.5px;
  cursor: pointer;
  font-family: inherit;
  transition: all .15s;
}
.lib-tab:hover { border-color: var(--accent); color: var(--accent); }
.lib-tab.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 600;
}
.lib-grid {
  flex: 1; overflow-y: auto; padding: 14px;
  display: grid; grid-template-columns: 1fr 1fr; gap: 12px;
  align-content: start;
}
.lib-item {
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  background: var(--bg);
  position: relative;
}
.lib-item img, .lib-item video {
  width: 100%; aspect-ratio: 1; object-fit: cover; display: block;
}
.lib-item.text-item {
  cursor: pointer;
  padding: 12px;
  min-height: 110px;
  display: flex; flex-direction: column;
  transition: border-color .15s;
}
.lib-item.text-item:hover { border-color: var(--accent); }
.text-item .t-icon { font-size: 24px; margin-bottom: 6px; }
.text-item .t-title {
  font-size: 13px; font-weight: 600; color: var(--text);
  line-height: 1.45; flex: 1;
  display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical;
  overflow: hidden;
}
.text-item .t-meta { font-size: 11px; color: var(--text-dim); margin-top: 8px; }
.lib-item .lib-del {
  position: absolute; top: 6px; right: 6px;
  border: none; background: rgba(0,0,0,.55); color: #fff;
  border-radius: 8px; width: 24px; height: 24px;
  cursor: pointer; font-size: 12px; line-height: 1;
  opacity: 0; transition: opacity .15s; z-index: 2;
}
.lib-item:hover .lib-del { opacity: 1; }
.lib-item .lib-cap {
  font-size: 11.5px; color: var(--text-dim);
  padding: 6px 8px; line-height: 1.4;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.lib-item .lib-type {
  position: absolute; top: 6px; left: 6px;
  background: rgba(0,0,0,.55); color: #fff;
  border-radius: 6px; font-size: 10.5px; padding: 2px 7px;
}
.lib-empty {
  grid-column: 1 / -1;
  text-align: center; color: var(--text-dim);
  padding: 50px 0; font-size: 13.5px;
}

/* ---------- 文本素材预览弹窗 ---------- */
.text-modal {
  position: fixed; inset: 0; z-index: 120;
  background: rgba(15,17,22,.5);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.text-modal[hidden] { display: none; }
.tm-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  width: min(640px, 100%);
  max-height: 84vh;
  display: flex; flex-direction: column;
  overflow: hidden;
}
.tm-head {
  display: flex; justify-content: space-between; align-items: center;
  padding: 14px 18px; border-bottom: 1px solid var(--border);
}
.tm-head h4 { font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#tm-body {
  flex: 1; overflow-y: auto; padding: 18px;
  white-space: pre-wrap; word-break: break-word;
  font-family: "JetBrains Mono", Consolas, monospace;
  font-size: 13px; line-height: 1.8; color: var(--text);
}
.tm-foot {
  display: flex; gap: 10px; justify-content: flex-end;
  padding: 12px 18px; border-top: 1px solid var(--border);
}
.tm-btn {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 16px;
  font-size: 13px;
  cursor: pointer;
  text-decoration: none;
  font-family: inherit;
}
.tm-btn:hover { border-color: var(--accent); color: var(--accent); }

/* ---------- 存入素材库操作条 ---------- */
.save-bar {
  display: flex; gap: 6px; align-items: center;
  margin-top: 8px; flex-wrap: wrap;
}
.save-chip {
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text-dim);
  border-radius: 999px;
  padding: 3px 11px;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
  transition: all .15s;
}
.save-chip:hover { border-color: var(--accent); color: var(--accent); }
.save-done {
  font-size: 11.5px;
  color: #059669;
  background: #ecfdf5;
  border-radius: 999px;
  padding: 3px 10px;
}

/* ---------- 移动端 ---------- */
@media (max-width: 768px) {
  #sidebar { position: fixed; z-index: 50; height: 100%; box-shadow: 4px 0 16px rgba(0,0,0,.08); }
  .bubble { max-width: 88%; }
  .messages { padding: 16px 10px 8px; }
  .composer-zone { padding: 0 10px 10px; }
  .media-bubble img, .media-bubble video { max-width: 100%; }
}
