:root {
  --bg: #0b0f16;
  --bg-soft: #121926;
  --panel: #161f2e;
  --panel-2: #1d2738;
  --line: rgba(255, 255, 255, .08);
  --line-strong: rgba(255, 255, 255, .16);
  --text: #e8eef8;
  --text-dim: #93a3bb;
  --text-faint: #66748c;
  --brand: #4d8dff;
  --brand-2: #7c5cff;
  --accent: #38d39f;
  --danger: #ff6b6b;
  --user-bubble: linear-gradient(135deg, #3b7cff, #6a5cff);
  --bot-bubble: #1b2434;
  --radius: 18px;
  --shadow: 0 12px 32px rgba(0, 0, 0, .45);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f4f6fb;
    --bg-soft: #eaeef7;
    --panel: #ffffff;
    --panel-2: #f0f3fa;
    --line: rgba(15, 23, 42, .08);
    --line-strong: rgba(15, 23, 42, .16);
    --text: #111a2b;
    --text-dim: #5b6b85;
    --text-faint: #8b99b1;
    --bot-bubble: #eef2fa;
    --shadow: 0 12px 32px rgba(15, 23, 42, .12);
  }
}

* { box-sizing: border-box; }

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

body {
  background:
    radial-gradient(1200px 700px at 12% -10%, rgba(77, 141, 255, .18), transparent 60%),
    radial-gradient(900px 600px at 110% 8%, rgba(124, 92, 255, .15), transparent 55%),
    var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

.hidden { display: none !important; }
.muted { color: var(--text-faint); font-size: 12px; }

/* ------------------------------------------------------------ 登录页 */
.auth {
  display: grid;
  place-items: center;
  height: 100dvh;
  padding: 20px;
}

.auth-card {
  width: min(400px, 100%);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  border: 1px solid var(--line-strong);
  border-radius: 24px;
  padding: 28px 26px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
}

.auth-brand { text-align: center; margin-bottom: 20px; }

.logo {
  width: 56px;
  height: 56px;
  border-radius: 18px;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  font-size: 26px;
  margin: 0 auto 12px;
  box-shadow: 0 10px 26px rgba(77, 141, 255, .35);
}

.logo.sm { width: 30px; height: 30px; border-radius: 10px; font-size: 15px; margin: 0; box-shadow: none; }

.auth-brand h1 { font-size: 19px; margin: 0 0 6px; }
.auth-brand p { margin: 0; color: var(--text-dim); font-size: 12.5px; line-height: 1.5; }

.tabs {
  display: flex;
  gap: 6px;
  background: var(--bg-soft);
  padding: 4px;
  border-radius: 12px;
  margin-bottom: 16px;
}

.tabs button {
  flex: 1;
  border: none;
  background: transparent;
  color: var(--text-dim);
  padding: 9px;
  border-radius: 9px;
  font: inherit;
  font-size: 13.5px;
  cursor: pointer;
  transition: .16s;
}

.tabs button.active { background: var(--panel); color: var(--text); box-shadow: 0 2px 8px rgba(0, 0, 0, .18); }

.field { display: block; margin-bottom: 13px; }
.field span { display: block; font-size: 12.5px; color: var(--text-dim); margin-bottom: 6px; }

.field input {
  width: 100%;
  padding: 11px 13px;
  border-radius: 12px;
  border: 1px solid var(--line-strong);
  background: var(--bg-soft);
  color: var(--text);
  font: inherit;
  font-size: 14px;
  outline: none;
  transition: .16s;
}

.field input:focus { border-color: var(--brand); }

.btn {
  appearance: none;
  border: 1px solid var(--line-strong);
  background: var(--panel);
  color: var(--text);
  padding: 9px 14px;
  border-radius: 12px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  transition: .16s;
  white-space: nowrap;
}

.btn:hover { background: var(--panel-2); }
.btn.primary { width: 100%; padding: 11px; background: linear-gradient(135deg, var(--brand), var(--brand-2)); border: none; color: #fff; font-size: 14px; font-weight: 600; }
.btn.primary:disabled { opacity: .6; cursor: not-allowed; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 40%, var(--line-strong)); }

.err {
  background: color-mix(in srgb, var(--danger) 14%, transparent);
  border: 1px solid color-mix(in srgb, var(--danger) 40%, transparent);
  color: var(--danger);
  font-size: 12.5px;
  padding: 8px 11px;
  border-radius: 10px;
  margin-bottom: 12px;
}

.auth-tip { text-align: center; color: var(--text-faint); font-size: 11.5px; margin-top: 14px; }

/* ------------------------------------------------------------ 主界面 */
.app {
  display: grid;
  grid-template-columns: 268px 1fr;
  height: 100dvh;
}

.sidebar {
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg-soft) 70%, transparent);
  min-height: 0;
}

.side-head { padding: 16px 14px 10px; }

.brand { display: flex; align-items: center; gap: 9px; font-weight: 650; font-size: 14.5px; margin-bottom: 14px; }

.btn.new {
  width: 100%;
  justify-content: center;
  background: linear-gradient(135deg, var(--brand), var(--brand-2));
  border: none;
  color: #fff;
  font-weight: 600;
  padding: 10px;
}

.conv-list { flex: 1; overflow-y: auto; padding: 4px 8px 8px; }

.conv {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 11px;
  border-radius: 12px;
  cursor: pointer;
  color: var(--text-dim);
  transition: .14s;
  margin-bottom: 2px;
}

.conv:hover { background: var(--panel-2); color: var(--text); }
.conv.active { background: var(--panel); color: var(--text); border: 1px solid var(--line); }
.conv .c-title { flex: 1; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .c-meta { font-size: 11px; color: var(--text-faint); }
.conv .c-del { opacity: 0; border: none; background: none; color: var(--text-faint); cursor: pointer; font-size: 14px; padding: 0 2px; }
.conv:hover .c-del { opacity: 1; }
.conv .c-del:hover { color: var(--danger); }

.side-foot {
  border-top: 1px solid var(--line);
  padding: 12px 14px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.me { display: flex; align-items: center; gap: 9px; flex: 1; min-width: 0; }
.me .avatar { width: 32px; height: 32px; border-radius: 10px; display: grid; place-items: center; background: var(--panel-2); }
.me #meName { font-size: 13px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ------------------------------------------------------------ 聊天区 */
.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; }

.chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 13px 18px;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 78%, transparent);
  backdrop-filter: blur(14px);
}

.chat-head .title-wrap { flex: 1; min-width: 0; }
.chat-head h2 { margin: 0; font-size: 15px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head .sub { font-size: 11.5px; color: var(--text-faint); margin-top: 2px; }

.icon {
  width: 36px;
  height: 36px;
  border-radius: 11px;
  border: 1px solid var(--line-strong);
  background: var(--panel);
  color: var(--text-dim);
  cursor: pointer;
  font-size: 15px;
  display: grid;
  place-items: center;
  transition: .15s;
}

.icon:hover { background: var(--panel-2); color: var(--text); }
.icon.plain { border: none; background: transparent; }
#menuBtn { display: none; }

.persona-select {
  max-width: 180px;
  padding: 8px 10px;
  border-radius: 11px;
  border: 1px solid var(--line-strong);
  background: var(--panel);
  color: var(--text);
  font: inherit;
  font-size: 12.5px;
  outline: none;
}

.messages { flex: 1; overflow-y: auto; padding: 20px 18px 6px; scroll-behavior: smooth; }
.messages::-webkit-scrollbar { width: 8px; }
.messages::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 8px; }

.row { display: flex; gap: 10px; margin-bottom: 16px; align-items: flex-end; animation: pop .18s ease-out; }
@keyframes pop { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.row.user { flex-direction: row-reverse; }

.row .avatar {
  width: 30px; height: 30px; border-radius: 10px; flex: 0 0 auto;
  display: grid; place-items: center; font-size: 15px;
  background: var(--panel-2); border: 1px solid var(--line);
}
.row.user .avatar { background: linear-gradient(135deg, var(--brand), var(--brand-2)); color: #fff; border: none; }

.col { display: flex; flex-direction: column; gap: 4px; max-width: min(76%, 660px); min-width: 0; }
.row.user .col { align-items: flex-end; }

.bubble {
  padding: 11px 14px;
  border-radius: var(--radius);
  line-height: 1.62;
  font-size: 14.5px;
  background: var(--bot-bubble);
  border: 1px solid var(--line);
  word-break: break-word;
  box-shadow: 0 2px 10px rgba(0, 0, 0, .12);
}
.row.user .bubble { background: var(--user-bubble); color: #fff; border: none; border-bottom-right-radius: 6px; }
.row.bot .bubble { border-bottom-left-radius: 6px; }
.bubble.notice { background: transparent; border: 1px dashed var(--line-strong); color: var(--text-dim); font-size: 13px; }

.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble h2, .bubble h3, .bubble h4 { margin: 10px 0 6px; font-size: 15px; }
.bubble ul { margin: 6px 0; padding-left: 20px; }
.bubble blockquote { margin: 6px 0; padding: 2px 10px; border-left: 3px solid var(--line-strong); color: var(--text-dim); }
.bubble code { background: rgba(127, 127, 127, .18); padding: 1px 5px; border-radius: 6px; font-size: .92em; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.bubble pre { background: rgba(0, 0, 0, .35); padding: 10px 12px; border-radius: 10px; overflow: auto; margin: 8px 0; }
.bubble pre code { background: none; padding: 0; font-size: 13px; white-space: pre; }
.bubble a { color: #8ab4ff; }
.row.user .bubble a { color: #e6edff; }
.bubble img { max-width: 100%; border-radius: 12px; display: block; margin-top: 6px; cursor: zoom-in; }

.meta { font-size: 11px; color: var(--text-faint); padding: 0 4px; }
.sys { text-align: center; font-size: 12px; color: var(--text-faint); margin: 14px 0; }

.typing { display: flex; gap: 4px; padding: 0 0 10px 52px; height: 0; overflow: hidden; transition: .2s; }
.typing.show { height: 26px; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--text-faint); animation: bounce 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%, 60%, 100% { transform: translateY(0); opacity: .4; } 30% { transform: translateY(-5px); opacity: 1; } }

.composer-wrap { padding: 8px 18px calc(12px + env(safe-area-inset-bottom)); }
.previews { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.preview { position: relative; width: 62px; height: 62px; border-radius: 12px; overflow: hidden; border: 1px solid var(--line-strong); }
.preview img { width: 100%; height: 100%; object-fit: cover; }
.preview button { position: absolute; top: 3px; right: 3px; width: 19px; height: 19px; border-radius: 50%; border: none; background: rgba(0, 0, 0, .65); color: #fff; font-size: 12px; cursor: pointer; padding: 0; line-height: 19px; }

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  background: var(--panel); border: 1px solid var(--line-strong);
  border-radius: 20px; padding: 8px; box-shadow: var(--shadow);
}
.composer:focus-within { border-color: color-mix(in srgb, var(--brand) 60%, var(--line-strong)); }
.composer textarea {
  flex: 1; resize: none; border: none; outline: none; background: transparent; color: var(--text);
  font: inherit; font-size: 14.5px; line-height: 1.5; max-height: 150px; padding: 9px 4px;
}
.composer textarea::placeholder { color: var(--text-faint); }
.composer .send { border: none; border-radius: 14px; padding: 10px 16px; font: inherit; font-size: 14px; font-weight: 600; color: #fff; background: linear-gradient(135deg, var(--brand), var(--brand-2)); cursor: pointer; }
.composer .send:disabled { opacity: .45; cursor: not-allowed; }

.statusline { text-align: center; font-size: 11px; color: var(--text-faint); margin-top: 7px; }

/* ------------------------------------------------------------ 右侧面板 */
.panel {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(400px, 92vw);
  background: var(--panel); border-left: 1px solid var(--line-strong);
  box-shadow: -18px 0 40px rgba(0, 0, 0, .35); z-index: 15;
  display: flex; flex-direction: column;
  animation: slideIn .18s ease-out;
}
@keyframes slideIn { from { transform: translateX(20px); opacity: .6; } to { transform: none; opacity: 1; } }

.panel-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px; border-bottom: 1px solid var(--line); }
.panel-head h3 { margin: 0; font-size: 14.5px; }
.panel-tabs { display: flex; gap: 6px; padding: 10px 14px; }
.panel-tabs button { flex: 1; border: 1px solid var(--line); background: var(--bg-soft); color: var(--text-dim); padding: 8px; border-radius: 10px; font: inherit; font-size: 12.5px; cursor: pointer; }
.panel-tabs button.active { background: var(--panel-2); color: var(--text); border-color: var(--line-strong); }
.panel-body { flex: 1; overflow-y: auto; padding: 6px 14px 18px; }

.plugin-item, .persona-item {
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 8px; background: var(--bg-soft);
}
.plugin-item .p-name { font-size: 13.5px; font-weight: 600; display: flex; align-items: center; gap: 6px; }
.plugin-item .p-desc { font-size: 12px; color: var(--text-dim); margin-top: 4px; line-height: 1.5; }
.badge { font-size: 10.5px; padding: 2px 7px; border-radius: 20px; background: color-mix(in srgb, var(--accent) 18%, transparent); color: var(--accent); }
.badge.off { background: color-mix(in srgb, var(--text-faint) 20%, transparent); color: var(--text-faint); }

.persona-item { cursor: pointer; }
.persona-item.active { border-color: var(--brand); background: color-mix(in srgb, var(--brand) 10%, var(--bg-soft)); }
.persona-item .pa-name { font-size: 13.5px; font-weight: 600; }
.persona-item .pa-preview { font-size: 12px; color: var(--text-dim); margin-top: 3px; line-height: 1.5; }

.memory-item { font-size: 12.5px; line-height: 1.6; padding: 8px 10px; border-left: 3px solid var(--line-strong); margin-bottom: 8px; color: var(--text-dim); white-space: pre-wrap; word-break: break-word; }
.memory-empty { color: var(--text-faint); font-size: 12.5px; line-height: 1.7; padding: 10px 4px; }

/* ------------------------------------------------------------ 弹层/提示 */
.modal { position: fixed; inset: 0; background: rgba(4, 8, 14, .62); backdrop-filter: blur(4px); display: grid; place-items: center; padding: 20px; z-index: 30; }
.sheet { width: min(400px, 100%); background: var(--panel); border: 1px solid var(--line-strong); border-radius: 20px; padding: 20px; box-shadow: var(--shadow); }
.sheet h3 { margin: 0 0 12px; font-size: 16px; }
.sheet .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }

#toast {
  position: fixed; left: 50%; bottom: 96px; transform: translate(-50%, 12px);
  opacity: 0; pointer-events: none; background: rgba(20, 28, 42, .96); color: #fff;
  border: 1px solid var(--line-strong); padding: 9px 14px; border-radius: 12px;
  font-size: 13px; transition: .2s; z-index: 40; max-width: 80vw; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .45); z-index: 12; }
.scrim.hidden { display: none; }

/* ------------------------------------------------------------ 移动端 */
@media (max-width: 860px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; top: 0; left: 0; bottom: 0; width: min(80vw, 300px);
    z-index: 20; transform: translateX(-102%); transition: transform .2s ease;
    background: var(--panel); border-right: 1px solid var(--line-strong);
  }
  .sidebar.open { transform: none; }
  #menuBtn { display: grid; }
  .col { max-width: 84%; }
  .chat-head { padding: 11px 12px; }
  .messages { padding: 14px 12px 4px; }
  .composer-wrap { padding: 6px 12px calc(10px + env(safe-area-inset-bottom)); }
  .persona-select { max-width: 120px; }
  .statusline { display: none; }
}

/* ------------------------------------------------------------ 头像 / 背景 / 个性化 */
.me .avatar { overflow: hidden; cursor: pointer; }
.me .avatar:hover { border-color: var(--brand); }
.row .avatar { overflow: hidden; }
.avatar img, .row .avatar img, .me .avatar img {
  width: 100%; height: 100%; object-fit: cover; display: block; border-radius: inherit;
}

.messages.has-bg {
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
}

.sheet.wide { width: min(470px, 100%); }

.avatar-row { display: flex; align-items: center; gap: 14px; margin-bottom: 14px; }
.avatar-pick {
  width: 64px; height: 64px; flex: 0 0 auto; border-radius: 18px;
  display: grid; place-items: center; font-size: 26px;
  background: var(--bg-soft); border: 1px solid var(--line-strong); overflow: hidden;
}
.avatar-pick img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar-actions { flex: 1; min-width: 0; }
.avatar-actions .label { font-size: 12.5px; color: var(--text-dim); margin-bottom: 6px; }
.row-btns { display: flex; gap: 8px; flex-wrap: wrap; }

.swatches { display: flex; gap: 8px; flex-wrap: wrap; }
.swatch {
  width: 52px; height: 36px; border-radius: 10px; cursor: pointer;
  border: 2px solid transparent; box-shadow: inset 0 0 0 1px var(--line-strong);
  background-size: cover; background-position: center;
}
.swatch.active { border-color: var(--brand); }

.pwd { margin: 4px 0 14px; border-top: 1px solid var(--line); padding-top: 10px; }
.pwd summary { cursor: pointer; font-size: 13px; color: var(--text-dim); }
.pwd-body { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
.pwd-body input {
  padding: 9px 12px; border-radius: 10px; border: 1px solid var(--line-strong);
  background: var(--bg-soft); color: var(--text); font: inherit; font-size: 13.5px; outline: none;
}
.pwd-body input:focus { border-color: var(--brand); }
