:root {
  --bg: #16151f;
  --bg-2: #1e1c2a;
  --bg-3: #262433;
  --panel: #201e2e;
  --border: #322f45;
  --text: #eceaf6;
  --text-dim: #9a96b0;
  --accent: #f472b6;
  --accent-2: #a78bfa;
  --accent-grad: linear-gradient(135deg, #f472b6, #a78bfa);
  --danger: #f87171;
  --user-bubble: #3b3560;
  --assistant-bubble: #26243a;
  --radius: 14px;
  --shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", "Helvetica Neue", sans-serif;
  font-size: 15px;
  line-height: 1.55;
}

#app {
  display: flex;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ---------- 侧边栏 ---------- */
#sidebar {
  width: 270px;
  min-width: 270px;
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
}

.logo { font-weight: 700; font-size: 17px; display: flex; align-items: center; gap: 6px; }
.logo-icon { font-size: 20px; }

.char-list {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.char-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 10px;
  border-radius: var(--radius);
  cursor: pointer;
  border: 1px solid transparent;
  transition: background .15s, border-color .15s;
}
.char-item:hover { background: var(--bg-3); }
.char-item.active { background: var(--bg-3); border-color: var(--accent); }

.char-avatar {
  width: 42px; height: 42px;
  border-radius: 10px;
  background: var(--bg-3);
  display: flex; align-items: center; justify-content: center;
  font-size: 22px;
  overflow: hidden;
  flex-shrink: 0;
}
.char-avatar img { width: 100%; height: 100%; object-fit: cover; }

.char-item-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.char-item-desc { font-size: 12px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.sidebar-footer { padding: 12px; border-top: 1px solid var(--border); }

/* ---------- 主区域 ---------- */
#main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.view.hidden { display: none; }

.empty {
  margin: auto;
  text-align: center;
  color: var(--text-dim);
  padding: 24px;
}
.empty-icon { font-size: 64px; }
.empty h2 { color: var(--text); margin: 12px 0 6px; }
.muted { color: var(--text-dim); font-size: 13px; }

/* 聊天头部 */
.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.char-info { display: flex; align-items: center; gap: 12px; min-width: 0; }
.char-info .char-avatar { width: 46px; height: 46px; border-radius: 12px; }
.char-info-name { font-weight: 700; font-size: 16px; }
.char-info-tags { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 2px; }
.tag {
  font-size: 11px;
  color: var(--accent-2);
  background: rgba(167, 139, 250, 0.14);
  padding: 1px 8px;
  border-radius: 999px;
}
.chat-header-actions { display: flex; gap: 6px; }

/* 消息区 */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.msg { display: flex; gap: 10px; max-width: 86%; }
.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.msg.assistant { align-self: flex-start; }

.msg-avatar {
  width: 34px; height: 34px;
  border-radius: 9px;
  background: var(--bg-3);
  display: flex; align-items: center; justify-content: center;
  font-size: 18px;
  flex-shrink: 0;
  overflow: hidden;
}
.msg-avatar img { width: 100%; height: 100%; object-fit: cover; }

.msg-body { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.msg.user .msg-body { align-items: flex-end; }

.msg-bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--assistant-bubble);
  border: 1px solid var(--border);
  white-space: pre-wrap;
  word-break: break-word;
  max-width: 100%;
}
.msg.user .msg-bubble { background: var(--user-bubble); border-color: transparent; }

.msg-bubble.editing { outline: 2px solid var(--accent); }

.msg-bubble .cursor { display: inline-block; width: 8px; height: 1em; background: var(--accent); vertical-align: text-bottom; animation: blink 1s steps(1) infinite; }
@keyframes blink { 50% { opacity: 0; } }

.msg-actions { display: flex; gap: 4px; opacity: 0; transition: opacity .12s; }
.msg:hover .msg-actions { opacity: 1; }
.msg-action {
  font-size: 11px;
  color: var(--text-dim);
  background: none;
  border: none;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 6px;
}
.msg-action:hover { color: var(--text); background: var(--bg-3); }
.msg-action.danger:hover { color: var(--danger); }

.msg-editarea {
  width: 100%;
  min-width: 320px;
  min-height: 60px;
  background: var(--bg-3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  resize: vertical;
  font-family: inherit;
  font-size: 14px;
}

/* 输入区 */
.input-area { padding: 12px 18px 16px; border-top: 1px solid var(--border); background: var(--panel); }
.input-bar { display: flex; align-items: flex-end; gap: 8px; }
.input-bar textarea {
  flex: 1;
  resize: none;
  max-height: 160px;
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text);
  padding: 11px 14px;
  font-family: inherit;
  font-size: 14px;
  line-height: 1.5;
  outline: none;
}
.input-bar textarea:focus { border-color: var(--accent); }
.input-hint { font-size: 12px; color: var(--text-dim); margin-top: 6px; min-height: 16px; }

/* ---------- 按钮 ---------- */
.btn {
  border: none;
  border-radius: 10px;
  padding: 9px 16px;
  font-size: 14px;
  cursor: pointer;
  background: var(--bg-3);
  color: var(--text);
  transition: filter .15s, transform .05s;
  white-space: nowrap;
}
.btn:hover { filter: brightness(1.12); }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary { background: var(--accent-grad); color: #1a1030; font-weight: 600; }
.btn-danger { background: var(--danger); color: #2a0d0d; font-weight: 600; }
.btn-block { width: 100%; }
.btn-sm { padding: 5px 10px; font-size: 13px; border-radius: 8px; }

.icon-btn {
  background: none;
  border: none;
  font-size: 18px;
  cursor: pointer;
  padding: 6px 8px;
  border-radius: 8px;
  color: var(--text-dim);
  line-height: 1;
}
.icon-btn:hover { background: var(--bg-3); color: var(--text); }
.icon-btn.danger:hover { color: var(--danger); }
.icon-btn.recording { color: var(--danger); animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .4; } }

/* ---------- 表单 ---------- */
.form-panel { flex: 1; overflow-y: auto; padding: 22px 24px; }
.form-panel-inner { max-width: 760px; margin: 0 auto; display: flex; flex-direction: column; gap: 14px; }
.form-title { font-size: 20px; font-weight: 700; margin: 0; }
.form-section { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; display: flex; flex-direction: column; gap: 12px; }
.form-section-title { font-weight: 600; font-size: 14px; color: var(--accent-2); margin: 0; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 13px; color: var(--text-dim); }
.field input[type="text"], .field input[type="password"], .field input[type="number"], .field textarea, .field select {
  background: var(--bg-3);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  padding: 9px 12px;
  font-family: inherit;
  font-size: 14px;
  outline: none;
}
.field input:focus, .field textarea:focus, .field select:focus { border-color: var(--accent); }
.field textarea { resize: vertical; min-height: 70px; }
.field .hint { font-size: 12px; color: var(--text-dim); }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }

.avatar-upload { display: flex; align-items: center; gap: 14px; }
.avatar-upload .char-avatar { width: 72px; height: 72px; border-radius: 14px; font-size: 36px; }

.form-actions { display: flex; gap: 10px; justify-content: flex-end; padding-bottom: 20px; }

/* ---------- 弹窗 ---------- */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 40;
}
.modal-container {
  position: fixed;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: min(680px, 94vw);
  max-height: 88vh;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  z-index: 50;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.modal-title { font-weight: 700; font-size: 16px; }
.modal-body { padding: 16px 18px; overflow-y: auto; display: flex; flex-direction: column; gap: 14px; }
.modal-footer { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }
.hidden { display: none !important; }

/* 世界书 */
.lore-entry { background: var(--bg-3); border: 1px solid var(--border); border-radius: 10px; padding: 12px; display: flex; flex-direction: column; gap: 8px; }
.lore-entry.disabled { opacity: .55; }
.lore-entry-head { display: flex; align-items: center; gap: 8px; justify-content: space-between; }

/* ---------- Toast ---------- */
.toast-container { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; align-items: center; }
.toast {
  background: var(--bg-3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 14px;
  box-shadow: var(--shadow);
  animation: toast-in .2s ease;
}
.toast.error { border-color: var(--danger); color: #fca5a5; }
.toast.success { border-color: #34d399; color: #6ee7b7; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* 滚动条 */
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: #3a3652; border-radius: 8px; }
::-webkit-scrollbar-thumb:hover { background: #4a4668; }

/* 移动端 */
@media (max-width: 720px) {
  #sidebar { width: 210px; min-width: 210px; }
  .msg { max-width: 96%; }
}
