:root {
  --ba-blue: #3b82f6;
  --ba-blue-dark: #2563eb;
  --ba-pink: #ec4899;
  --ba-yellow: #f59e0b;
  --bg: #eef4fb;
  --bg-grad: linear-gradient(180deg, #f6f9ff 0%, #eaf2fc 100%);
  --card: #ffffff;
  --card-soft: #f8fafc;
  --border: #e5edf7;
  --text: #1f2937;
  --muted: #6b7280;
  --radius: 20px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg-grad);
  color: var(--text);
  min-height: 100vh;
}
a { color: var(--ba-blue); text-decoration: none; }
button { font-family: inherit; }

.ui-panel {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 4px 18px rgba(37, 99, 235, .07);
}

#toast {
  position: fixed;
  top: 18px; left: 50%;
  transform: translateX(-50%) translateY(-20px);
  padding: 10px 22px;
  border-radius: 999px;
  background: #1f2937;
  color: #fff;
  font-size: 14px;
  opacity: 0;
  transition: all .25s ease;
  z-index: 999;
  pointer-events: none;
  box-shadow: 0 8px 24px rgba(0,0,0,.18);
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
#toast.err { background: #dc2626; }
#toast.ok { background: #059669; }

/* ---------- auth page ---------- */
.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: linear-gradient(160deg, #dbeafe 0%, #eef4fb 40%, #f6f9ff 100%);
}
.auth-box { width: 100%; max-width: 420px; padding: 34px 32px; border-radius: 24px; }
.auth-box h1 { font-size: 22px; display: flex; align-items: center; gap: 10px; margin-bottom: 6px; color: var(--text); }
.auth-box .logo {
  width: 36px; height: 36px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--ba-blue), var(--ba-blue-dark));
  display: inline-flex; align-items: center; justify-content: center;
  color: #fff; font-size: 17px;
}
.auth-box .sub { color: var(--muted); font-size: 13px; margin-bottom: 22px; }
.tabs {
  display: flex; gap: 6px; margin-bottom: 20px;
  background: var(--card-soft); padding: 4px; border-radius: 12px;
  border: 1px solid var(--border);
}
.tabs button {
  flex: 1; padding: 9px; border: 0; border-radius: 9px;
  background: transparent; color: var(--muted); cursor: pointer; font-size: 14px; transition: all .2s;
}
.tabs button.active { background: var(--ba-blue); color: #fff; box-shadow: 0 2px 8px rgba(59,130,246,.35); }
.form-item { margin-bottom: 14px; }
.form-item label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 6px; }
input, select, textarea {
  width: 100%;
  padding: 10px 12px;
  background: var(--card-soft);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color .2s, box-shadow .2s;
  font-family: inherit;
}
input:focus, select:focus, textarea:focus {
  border-color: var(--ba-blue);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, .15);
}
.btn {
  padding: 10px 18px;
  border: 0; border-radius: 11px;
  font-size: 14px; font-weight: 600;
  cursor: pointer;
  background: linear-gradient(135deg, var(--ba-blue), var(--ba-blue-dark));
  color: #fff;
  transition: filter .2s, transform .05s, box-shadow .2s;
  box-shadow: 0 3px 10px rgba(37, 99, 235, .25);
}
.btn:hover { filter: brightness(1.08); box-shadow: 0 5px 16px rgba(37, 99, 235, .35); }
.btn:active { transform: scale(.98); }
.btn.block { width: 100%; }
.btn.ghost { background: #fff; color: var(--text); border: 1px solid var(--border); box-shadow: none; }
.btn.ghost:hover { background: var(--card-soft); border-color: #cbd5e1; }
.btn.danger { background: linear-gradient(135deg, #ef4444, #dc2626); box-shadow: 0 3px 10px rgba(220,38,38,.25); }
.btn.sm { padding: 7px 13px; font-size: 13px; border-radius: 9px; }
.btn:disabled { opacity: .6; cursor: not-allowed; }
.hint { font-size: 12px; color: var(--muted); margin-top: 14px; text-align: center; }

/* ---------- chat page ---------- */
.chat-shell {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  display: flex;
  overflow: hidden;
}
.sidebar {
  width: 264px;
  flex-shrink: 0;
  background: var(--card);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transition: margin-left .25s ease, transform .25s ease;
  z-index: 40;
}
.sidebar.collapsed { margin-left: -264px; }
.side-head { padding: 14px 12px 6px; }
.side-head .brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 15px; padding: 4px 4px 10px; }
.side-head .brand .logo {
  width: 30px; height: 30px; border-radius: 10px;
  background: linear-gradient(135deg, var(--ba-blue), var(--ba-blue-dark));
  display: inline-flex; align-items: center; justify-content: center;
  color: #fff; font-size: 14px;
}
.new-btn { width: 100%; text-align: center; }
.conv-list { flex: 1; overflow-y: auto; padding: 8px; display: flex; flex-direction: column; gap: 4px; }
.conv-list::-webkit-scrollbar { width: 6px; }
.conv-list::-webkit-scrollbar-thumb { background: #cbd5e1; border-radius: 3px; }
.conv-item {
  display: flex; align-items: center; gap: 6px;
  padding: 9px 10px;
  border-radius: 11px;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background .15s;
  user-select: none;
}
.conv-item:hover { background: var(--card-soft); }
.conv-item.active { background: #eff6ff; border-color: #bfdbfe; }
.conv-item .ct { flex: 1; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-item .conv-model {
  flex-shrink: 0;
  font-size: 11px;
  color: var(--muted);
  background: var(--card-soft);
  border: 1px solid var(--border);
  padding: 1px 7px;
  border-radius: 999px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 96px;
}
.conv-item .del {
  flex-shrink: 0;
  width: 22px; height: 22px;
  border: 0; border-radius: 7px;
  background: transparent; color: var(--muted);
  cursor: pointer; font-size: 14px; line-height: 1;
  opacity: 0; transition: opacity .15s, background .15s;
}
.conv-item:hover .del { opacity: 1; }
.conv-item .del:hover { background: #fee2e2; color: #dc2626; }
.conv-empty { color: var(--muted); font-size: 12px; text-align: center; padding: 20px 8px; line-height: 1.8; }
.side-foot { padding: 12px; border-top: 1px solid var(--border); display: flex; flex-direction: column; gap: 8px; }
.side-foot .me { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-foot .btn, .side-foot #adminBtn { width: 100%; text-align: center; }
.overlay {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, .45);
  opacity: 0; visibility: hidden;
  transition: opacity .25s ease;
  z-index: 35;
}
.overlay.show { opacity: 1; visibility: visible; }
.chat-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-head {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  background: rgba(255,255,255,.75);
  backdrop-filter: blur(10px);
}
.head-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
.menu-btn { padding: 7px 11px; flex-shrink: 0; }
.chat-head .title { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-head .title span { color: var(--muted); font-weight: 400; font-size: 12px; margin-left: 8px; }
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}
.msg { display: flex; gap: 10px; max-width: 82%; }
.msg .avatar {
  flex-shrink: 0;
  width: 34px; height: 34px;
  border-radius: 11px;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
  color: #fff;
}
.msg.user { align-self: flex-end; flex-direction: row-reverse; }
.msg.user .avatar { background: linear-gradient(135deg, var(--ba-blue), var(--ba-blue-dark)); }
.msg.ai .avatar { background: linear-gradient(135deg, var(--ba-pink), #d946ef); }
.msg .bubble {
  padding: 11px 14px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
  background: var(--card);
  border: 1px solid var(--border);
  box-shadow: 0 2px 8px rgba(37,99,235,.06);
}
.msg.user .bubble { background: linear-gradient(135deg, var(--ba-blue), var(--ba-blue-dark)); color: #fff; border-color: transparent; box-shadow: 0 3px 12px rgba(37,99,235,.3); }
.msg .bubble pre { background: #f1f5f9; padding: 8px; border-radius: 8px; overflow-x: auto; margin: 6px 0; }
.msg .bubble code { font-family: "SF Mono", Consolas, monospace; font-size: 13px; }
.msg.typing .bubble { color: var(--muted); }
.typing-dots { display: inline-flex; gap: 4px; align-items: center; }
.typing-dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing-dots i:nth-child(2) { animation-delay: .2s; }
.typing-dots i:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%,60%,100% { opacity: .25; } 30% { opacity: 1; } }
.input-bar {
  padding: 14px 20px;
  border-top: 1px solid var(--border);
  background: rgba(255,255,255,.75);
  backdrop-filter: blur(10px);
}
.input-box { max-width: 860px; margin: 0 auto; display: flex; gap: 10px; align-items: flex-end; }
.input-box textarea {
  flex: 1;
  min-height: 46px;
  max-height: 160px;
  resize: none;
  padding: 12px 14px;
  line-height: 1.5;
}
.empty-tip { color: var(--muted); text-align: center; margin: auto; font-size: 14px; line-height: 2; }

/* ---------- 移动端适配 ---------- */
@media (max-width: 768px) {
  .sidebar.collapsed { margin-left: 0; }
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: 280px;
    transform: translateX(-100%);
    box-shadow: 8px 0 30px rgba(15, 23, 42, .25);
  }
  .sidebar.open { transform: translateX(0); }
  .chat-main { width: 100%; }
  .chat-head { padding: 10px 12px; }
  .chat-head .menu-btn { display: inline-flex; }
  .head-actions .btn { padding: 6px 9px; }
  .msg { max-width: 92%; }
  input, select, textarea { font-size: 16px; }
  .input-bar { padding-bottom: max(14px, env(safe-area-inset-bottom)); }
}
@media (min-width: 769px) {
  .sidebar.open { margin-left: 0; }
}

/* ---------- profile page ---------- */
.profile-shell { max-width: 720px; margin: 0 auto; padding: 30px 18px 60px; }
.profile-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 22px; }
.profile-head h1 { font-size: 22px; display: flex; align-items: center; gap: 10px; }
.user-hero { padding: 26px; display: flex; align-items: center; gap: 18px; margin-bottom: 18px; }
.user-hero .uavatar {
  width: 64px; height: 64px; flex-shrink: 0;
  border-radius: 18px;
  background: linear-gradient(135deg, var(--ba-blue), var(--ba-pink));
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 24px; font-weight: 700;
  box-shadow: 0 6px 18px rgba(59,130,246,.3);
}
.user-hero .uname { font-size: 18px; font-weight: 700; }
.user-hero .meta { font-size: 13px; color: var(--muted); margin-top: 4px; }
.ustats { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 18px; }
.ustat { padding: 16px; text-align: center; }
.ustat .num { font-size: 24px; font-weight: 700; color: var(--ba-blue); }
.ustat .lbl { color: var(--muted); font-size: 12px; margin-top: 3px; }

/* ---------- admin page ---------- */
.admin-shell { max-width: 980px; margin: 0 auto; padding: 30px 18px 60px; }
.admin-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 24px; }
.admin-head h1 { font-size: 22px; display: flex; align-items: center; gap: 10px; }
.badge-admin { background: linear-gradient(135deg, #f59e0b, #ef4444); color: #fff; font-size: 11px; padding: 3px 10px; border-radius: 999px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; margin-bottom: 20px; }
.stat { padding: 18px; }
.stat .num { font-size: 26px; font-weight: 700; color: var(--ba-blue); }
.stat .lbl { color: var(--muted); font-size: 13px; margin-top: 4px; }
.panel { padding: 22px; margin-bottom: 20px; }
.panel h2 { font-size: 16px; margin-bottom: 16px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 700px) { .grid2 { grid-template-columns: 1fr; } .stats { grid-template-columns: 1fr; } }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 9px 10px; border-bottom: 1px solid var(--border); }
th { color: var(--muted); font-weight: 500; }
.row-actions { display: flex; gap: 6px; }
.tag { font-size: 11px; padding: 2px 9px; border-radius: 999px; background: var(--card-soft); border: 1px solid var(--border); color: var(--muted); }
.tag.admin { background: #fef3c7; color: #b45309; border-color: #fcd34d; }
.tag.banned { background: #fee2e2; color: #b91c1c; border-color: #fca5a5; }
#userModalBg {
  position: fixed; inset: 0; background: rgba(15,23,42,.5);
  display: none; align-items: center; justify-content: center; z-index: 100;
}
#userModalBg.show { display: flex; }
.modal { width: 100%; max-width: 380px; padding: 26px; border-radius: 22px; }
.topbar { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; }

/* ---------- 模型管理（管理员） ---------- */
.model-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 16px;
  margin-bottom: 14px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--card-soft);
}
.model-row .form-item { margin-bottom: 0; }
.model-row .m-sp,
.model-row .m-del { grid-column: 1 / -1; }
@media (max-width: 700px) { .model-row { grid-template-columns: 1fr; } }

/* ---------- 暗色模式 ---------- */
:root[data-theme="dark"] {
  color-scheme: dark;
  --ba-blue: #60a5fa;
  --ba-blue-dark: #3b82f6;
  --bg: #0f172a;
  --bg-grad: linear-gradient(180deg, #131c31 0%, #0f172a 100%);
  --card: #1e293b;
  --card-soft: #283548;
  --border: #334155;
  --text: #e2e8f0;
  --muted: #94a3b8;
}
:root[data-theme="dark"] .ui-panel { box-shadow: 0 4px 18px rgba(0,0,0,.35); }
:root[data-theme="dark"] #toast { border: 1px solid #374151; }
:root[data-theme="dark"] .auth-wrap { background: linear-gradient(160deg, #0f172a 0%, #1e293b 40%, #131c31 100%); }
:root[data-theme="dark"] .chat-head,
:root[data-theme="dark"] .input-bar { background: rgba(15, 23, 42, .72); }
:root[data-theme="dark"] .btn.ghost { background: var(--card-soft); color: var(--text); border-color: var(--border); box-shadow: none; }
:root[data-theme="dark"] .btn.ghost:hover { background: #334155; border-color: #475569; }
:root[data-theme="dark"] .conv-item.active { background: #1e3a5f; border-color: #1d4ed8; }
:root[data-theme="dark"] .conv-item .del:hover { background: #450a0a; }
:root[data-theme="dark"] .msg.user .bubble { box-shadow: 0 3px 12px rgba(37,99,235,.35); }
:root[data-theme="dark"] .msg .bubble pre { background: #0f172a; }
:root[data-theme="dark"] .tag.admin { background: #422006; color: #fbbf24; border-color: #92400e; }
:root[data-theme="dark"] .tag.banned { background: #450a0a; color: #fca5a5; border-color: #7f1d1d; }
:root[data-theme="dark"] .sidebar { scrollbar-color: #475569 #1e293b; }
:root[data-theme="dark"] ::-webkit-scrollbar { width: 8px; }
:root[data-theme="dark"] ::-webkit-scrollbar-track { background: transparent; }
:root[data-theme="dark"] ::-webkit-scrollbar-thumb { background: #475569; border-radius: 4px; }