/* ============ 主题变量：简约风格，纯色背景 + 细边框 ============ */
:root {
  --accent: #2563eb;
  --accent-weak: rgba(37, 99, 235, 0.1);
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-2: #f2f3f5;
  --border: #e4e6ea;
  --text: #1f2430;
  --text-dim: #61697a;
  --text-faint: #949cad;
  --danger: #dc2626;
  --success: #16a34a;
  --warning: #d97706;
  --radius: 8px;
  --radius-sm: 6px;
  --font: "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
}

html[data-theme="dark"] {
  --accent-weak: rgba(96, 165, 250, 0.14);
  --bg: #16181d;
  --panel: #1d2026;
  --panel-2: #23272e;
  --border: #2d323b;
  --text: #e6e8ec;
  --text-dim: #9aa3b2;
  --text-faint: #6f7885;
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

a { color: var(--accent); }

/* ============ 通用控件 ============ */
button, input, select, textarea { font-family: inherit; font-size: 14px; color: inherit; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
}
.btn:hover { background: var(--panel-2); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary { border-color: var(--accent); background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent); filter: brightness(0.94); }
.btn-danger { border-color: var(--border); color: var(--danger); background: var(--panel); }
.btn-danger:hover { background: rgba(220, 38, 38, 0.08); }
.btn-sm { padding: 4px 10px; font-size: 12.5px; }
.btn-ghost { border-color: transparent; background: transparent; }
.btn-ghost:hover { background: var(--panel-2); }

.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; }
.field label { font-size: 13px; color: var(--text-dim); }
/* 管理员后台权限勾选 */
.perm-list { display: flex; flex-wrap: wrap; gap: 8px 16px; }
.perm-item { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text); }
.perm-item input { width: auto; }

input[type="text"], input[type="password"], input[type="number"], select, textarea {
  width: 100%;
  padding: 8px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--panel);
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: vertical; min-height: 70px; }
input[type="color"] {
  width: 40px; height: 30px; padding: 2px;
  border: 1px solid var(--border); border-radius: var(--radius-sm);
  background: var(--panel); cursor: pointer;
}

.switch { display: inline-flex; align-items: center; gap: 9px; cursor: pointer; user-select: none; }
.switch input { display: none; }
.switch .track {
  width: 36px; height: 20px; border-radius: 999px;
  background: #c8ccd4; position: relative; transition: background 0.15s;
}
html[data-theme="dark"] .switch .track { background: #3a4049; }
.switch .track::after {
  content: ""; position: absolute; top: 2px; left: 2px;
  width: 16px; height: 16px; border-radius: 50%; background: #fff;
  transition: transform 0.15s;
}
.switch input:checked + .track { background: var(--accent); }
.switch input:checked + .track::after { transform: translateX(16px); }

.badge {
  display: inline-block; padding: 1px 7px; border-radius: 4px;
  font-size: 11.5px; border: 1px solid var(--border); color: var(--text-dim);
  background: var(--panel-2);
}
.badge-admin { color: var(--warning); border-color: currentColor; background: transparent; }
.badge-active, .badge-online { color: var(--success); border-color: currentColor; background: transparent; }
.badge-banned { color: var(--danger); border-color: currentColor; background: transparent; }
.badge-bot { color: var(--accent); border-color: currentColor; background: transparent; }

.count {
  min-width: 18px; height: 18px; padding: 0 5px;
  display: inline-grid; place-items: center;
  border-radius: 999px; background: var(--danger); color: #fff;
  font-size: 11px; font-weight: 600;
}

.avatar {
  display: grid; place-items: center; flex: none;
  width: 34px; height: 34px; border-radius: 50%;
  font-weight: 600; color: #fff; font-size: 14px;
  overflow: hidden;
}
.avatar-sm { width: 26px; height: 26px; font-size: 12px; }
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }

/* 聊天顶栏的人设快速切换 */
.persona-switch { width: auto; max-width: 160px; padding: 4px 8px; font-size: 12.5px; }

/* 人设预设库：下拉 + 一排小按钮 */
.persona-box { display: flex; flex-direction: column; gap: 8px; }
.persona-ops { display: flex; flex-wrap: wrap; gap: 6px; }

/* 模型名称：输入框 + 检测按钮 + 检测结果下拉 */
.model-pick { display: flex; align-items: center; gap: 8px; }
.model-pick input { flex: 1; min-width: 0; }
.model-list { margin-top: 8px; }

/* 个人资料里的头像编辑区 */
.avatar-edit { display: flex; align-items: center; gap: 12px; }
.avatar-edit .avatar-preview {
  width: 64px; height: 64px; border-radius: 50%; overflow: hidden; flex: none;
  display: grid; place-items: center; color: #fff; font-weight: 600; font-size: 24px;
}
.avatar-edit .avatar-preview img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar-edit .avatar-ops { display: flex; flex-wrap: wrap; gap: 8px; }

.muted { color: var(--text-dim); }
.faint { color: var(--text-faint); font-size: 12.5px; }
.center { text-align: center; }
.hidden { display: none !important; }
.spacer { flex: 1; }
.row { display: flex; align-items: center; gap: 9px; }
.empty { padding: 18px 16px; color: var(--text-faint); font-size: 12.5px; }
.online-dot { width: 7px; height: 7px; border-radius: 50%; background: var(--success); flex: none; }
.online-dot.off { background: var(--text-faint); opacity: 0.5; }

/* ============ 登录 / 注册 ============ */
.auth-wrap { height: 100vh; height: 100dvh; display: grid; place-items: center; padding: 24px; }

.auth-card {
  width: min(380px, 100%);
  padding: 32px 28px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--panel);
}

.auth-logo { display: flex; align-items: center; gap: 9px; margin-bottom: 22px; }
.auth-logo .mark {
  width: 30px; height: 30px; border-radius: var(--radius-sm);
  display: grid; place-items: center; background: var(--accent); color: #fff; font-size: 15px;
}
.auth-logo .name { font-size: 16px; font-weight: 600; }
.auth-logo .slogan { font-size: 12px; color: var(--text-faint); }

.tabs { display: flex; gap: 18px; border-bottom: 1px solid var(--border); margin-bottom: 20px; }
.tabs button {
  padding: 0 0 9px; border: none; background: none; cursor: pointer;
  color: var(--text-dim); border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); }

.alert {
  padding: 8px 11px; border-radius: var(--radius-sm); font-size: 13px; margin-bottom: 14px;
  border: 1px solid var(--danger); color: var(--danger); background: transparent;
}
.alert-ok { border-color: var(--success); color: var(--success); }

/* ============ 聊天主界面 ============ */
.app {
  height: 100vh;
  height: 100dvh; /* 移动端浏览器地址栏收起/展开时不跳动 */
  display: grid;
  grid-template-columns: 240px 1fr 200px;
}

/* 移动端抽屉遮罩与仅移动端可见的按钮，默认都不显示 */
.side-backdrop { display: none; }
.only-mobile { display: none !important; }

/* 抽屉按钮上的未读角标 */
#btnMenu { position: relative; font-size: 16px; }
#btnMenu .count { position: absolute; top: -3px; right: -3px; }

.panel { background: var(--panel); display: flex; flex-direction: column; min-height: 0; overflow: hidden; }
.side { border-right: 1px solid var(--border); }
.members-panel { border-left: 1px solid var(--border); }

.side-head { padding: 14px 16px; border-bottom: 1px solid var(--border); }
.brand { display: flex; align-items: center; gap: 9px; }
.brand-mark {
  width: 28px; height: 28px; border-radius: var(--radius-sm);
  display: grid; place-items: center; background: var(--accent); color: #fff; font-size: 14px;
}
.brand-name { font-weight: 600; font-size: 14.5px; }
.brand-slogan { font-size: 11.5px; color: var(--text-faint); }

.section-title {
  padding: 14px 16px 6px;
  font-size: 12px; color: var(--text-faint);
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.section-title .btn { margin: -4px 0; }

.scroll { overflow-y: auto; min-height: 0; }
.scroll::-webkit-scrollbar { width: 7px; }
.scroll::-webkit-scrollbar-thumb { background: var(--border); border-radius: 7px; }

/* 侧栏列表项：频道与私聊共用 */
.nav-item {
  display: flex; align-items: center; gap: 9px;
  width: 100%; padding: 7px 16px; text-align: left;
  border: none; border-left: 2px solid transparent;
  background: none; color: var(--text-dim); cursor: pointer;
}
.nav-item:hover { background: var(--panel-2); }
.nav-item.active { background: var(--accent-weak); border-left-color: var(--accent); color: var(--text); font-weight: 600; }
.nav-item .hash { color: var(--text-faint); }
.nav-item .label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item .sub { font-size: 11.5px; color: var(--text-faint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.nav-item .body { flex: 1; min-width: 0; }

.side-foot {
  margin-top: auto; padding: 10px 12px; border-top: 1px solid var(--border);
  display: flex; align-items: center; gap: 9px;
}
.side-foot .me { min-width: 0; flex: 1; }
.side-foot .me-name { font-size: 13.5px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* 聊天区 */
.chat-head {
  padding: 12px 18px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 10px;
}
.chat-title { font-size: 15px; font-weight: 600; }
.chat-sub { font-size: 12px; color: var(--text-faint); margin-top: 1px; }

.announce {
  margin: 12px 18px 0; padding: 8px 12px;
  border: 1px solid var(--border); border-left: 2px solid var(--accent);
  border-radius: var(--radius-sm); background: var(--panel-2);
  font-size: 12.5px; color: var(--text-dim); line-height: 1.6;
}

.messages { flex: 1; padding: 16px 18px; display: flex; flex-direction: column; gap: 12px; }

.msg { display: flex; gap: 10px; }
/* 竖向 flex + align-items 收缩，让气泡宽度贴合文字，
   而不是被上面那行（昵称/时间/撤回）的宽度撑开 */
.msg-body { min-width: 0; max-width: min(620px, 80%); display: flex; flex-direction: column; align-items: flex-start; }
.msg-head { display: flex; align-items: center; gap: 7px; margin-bottom: 3px; max-width: 100%; }
.msg-author { font-size: 12.5px; font-weight: 600; }
.msg-time { font-size: 11px; color: var(--text-faint); }
.msg-bubble {
  padding: 8px 12px; border-radius: var(--radius);
  border: 1px solid var(--border); background: var(--panel-2);
  line-height: 1.6; word-break: break-word; white-space: pre-wrap;
  max-width: 100%;
}
.msg.mine { flex-direction: row-reverse; }
.msg.mine .msg-body { align-items: flex-end; }
.msg.mine .msg-head { flex-direction: row-reverse; }
.msg.mine .msg-bubble { border-color: var(--accent); background: var(--accent); color: #fff; }
.msg.mine .msg-bubble a { color: #fff; text-decoration: underline; }
.msg-actions { opacity: 0; }
.msg:hover .msg-actions { opacity: 1; }
.msg-actions button {
  border: none; background: none; padding: 0; cursor: pointer;
  font-size: 11px; color: var(--text-faint);
}
.msg-actions button:hover { color: var(--danger); }
/* 已撤回的占位气泡 */
.msg-bubble.revoked,
.msg.mine .msg-bubble.revoked {
  background: transparent; border-style: dashed; border-color: var(--border);
  color: var(--text-faint); font-style: italic;
}
/* 定时销毁倒计时 */
.msg-expire { font-size: 11px; color: var(--warning); white-space: nowrap; }

.msg-system { align-self: center; font-size: 11.5px; color: var(--text-faint); }

.typing { padding: 0 18px 4px; height: 17px; font-size: 12px; color: var(--text-faint); }

.composer { padding: 12px 18px 14px; border-top: 1px solid var(--border); }
.composer-box {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 6px 6px 6px 11px;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--panel);
}
.composer-box:focus-within { border-color: var(--accent); }
.composer-box textarea {
  flex: 1; border: none; background: none; padding: 5px 0;
  min-height: 22px; max-height: 130px; resize: none; outline: none; line-height: 1.55;
}
.composer-box textarea:focus { border: none; }
.send-btn {
  width: 32px; height: 32px; flex: none; border: none; border-radius: var(--radius-sm);
  background: var(--accent); color: #fff; cursor: pointer; display: grid; place-items: center;
}
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.composer-foot { display: flex; justify-content: space-between; margin-top: 6px; font-size: 11.5px; color: var(--text-faint); }
.destroy-pick { display: inline-flex; align-items: center; gap: 5px; margin-right: 10px; }
.destroy-pick select { width: auto; padding: 1px 5px; font-size: 11.5px; border-radius: var(--radius-sm); }

.emoji-bar { display: flex; gap: 3px; flex-wrap: wrap; margin-bottom: 7px; }
.emoji-bar button {
  border: 1px solid var(--border); background: var(--panel); cursor: pointer;
  width: 26px; height: 26px; border-radius: var(--radius-sm); font-size: 14px; line-height: 1; padding: 0;
}
.emoji-bar button:hover { background: var(--panel-2); }

/* 成员 / 用户选择 */
.member {
  display: flex; align-items: center; gap: 8px; width: 100%;
  padding: 6px 14px; border: none; background: none; cursor: pointer; text-align: left; color: inherit;
}
.member:hover { background: var(--panel-2); }
.member-name { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.member-bio { font-size: 11px; color: var(--text-faint); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ============ 弹窗 / 提示 ============ */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(20, 24, 31, 0.4);
  display: grid; place-items: center; padding: 20px;
}
.modal {
  width: min(420px, 100%); max-height: 84vh; overflow-y: auto;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--panel); padding: 22px;
}
.modal h3 { margin: 0 0 16px; font-size: 15px; }
.modal-foot { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 8px; margin-top: 6px; }
.modal hr { border: none; border-top: 1px solid var(--border); margin: 20px 0; }

.pick-list { max-height: 320px; overflow-y: auto; border: 1px solid var(--border); border-radius: var(--radius-sm); }

#toasts { position: fixed; top: 16px; left: 50%; transform: translateX(-50%); z-index: 90; display: grid; gap: 6px; }
.toast {
  padding: 7px 14px; border-radius: var(--radius-sm); font-size: 13px;
  border: 1px solid var(--border); background: var(--panel); color: var(--text);
}
.toast.err { border-color: var(--danger); color: var(--danger); }
.toast.ok { border-color: var(--success); color: var(--success); }

/* ============ 管理后台 ============ */
.admin { height: 100vh; height: 100dvh; display: grid; grid-template-rows: auto 1fr; }
.admin-head {
  display: flex; align-items: center; gap: 10px; padding: 12px 18px;
  border-bottom: 1px solid var(--border); background: var(--panel);
}
.admin-body { display: grid; grid-template-columns: 190px 1fr; min-height: 0; }
.admin-nav { padding: 12px 0; border-right: 1px solid var(--border); background: var(--panel); align-content: start; }
.admin-nav button {
  display: block; width: 100%; text-align: left;
  padding: 8px 18px; border: none; border-left: 2px solid transparent;
  background: none; color: var(--text-dim); cursor: pointer;
}
.admin-nav button:hover { background: var(--panel-2); }
.admin-nav button.active { background: var(--accent-weak); border-left-color: var(--accent); color: var(--text); font-weight: 600; }
.admin-main { padding: 22px 24px; overflow-y: auto; background: var(--bg); }
.admin-main h2 { margin: 0 0 3px; font-size: 17px; }
.admin-main > p.faint { margin: 0 0 20px; }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; margin-bottom: 22px; }
.stat { padding: 14px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--panel); }
.stat .k { font-size: 12px; color: var(--text-faint); }
.stat .v { font-size: 22px; font-weight: 600; margin-top: 4px; }

.table-wrap { border: 1px solid var(--border); border-radius: var(--radius); overflow: auto; background: var(--panel); }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
thead th {
  position: sticky; top: 0; background: var(--panel-2); text-align: left;
  padding: 9px 14px; font-size: 12px; color: var(--text-faint);
  border-bottom: 1px solid var(--border); white-space: nowrap; font-weight: 500;
}
tbody td { padding: 9px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
.table-actions { display: flex; gap: 5px; flex-wrap: wrap; }

.settings-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(270px, 1fr)); gap: 0 20px; }
.card { padding: 16px 18px; margin-bottom: 16px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--panel); }
.card h4 { margin: 0 0 14px; font-size: 13.5px; color: var(--text-dim); font-weight: 600; }

/* ============ 响应式 ============ */
/* 平板：隐藏右侧成员栏 */
@media (max-width: 1080px) {
  .app { grid-template-columns: 226px 1fr; }
  .members-panel { display: none; }
}

/* 手机：侧栏改为抽屉，聊天区独占一屏 */
@media (max-width: 760px) {
  body { overscroll-behavior: none; }

  .only-mobile { display: inline-flex !important; }
  .auth-wrap { height: var(--app-h, 100dvh); padding: 16px; align-content: center; }
  .auth-card { padding: 24px 18px; }
  .auth-card input { font-size: 16px; } /* 避免 iOS 聚焦时缩放 */
  /* --app-h 由 JS 按 visualViewport 高度设置，键盘弹出时布局不会被顶出屏幕 */
  .app { grid-template-columns: 1fr; height: var(--app-h, 100dvh); }

  .side {
    position: fixed;
    z-index: 50;
    top: 0; left: 0; bottom: 0;
    width: min(80vw, 300px);
    border-right: 1px solid var(--border);
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    padding-bottom: env(safe-area-inset-bottom);
  }
  body.side-open .side { transform: none; }
  body.side-open .side-backdrop {
    display: block;
    position: fixed; inset: 0; z-index: 45;
    background: rgba(20, 24, 31, 0.4);
  }

  /* 抽屉里的条目加大点击区域 */
  .nav-item { padding: 10px 16px; }
  .side-foot { padding: 10px 12px calc(10px + env(safe-area-inset-bottom)); }

  .chat-head { padding: 10px 12px; }
  .chat-title { font-size: 14.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .chat-sub { display: none; }
  #connState { display: none; }

  .announce { margin: 10px 12px 0; }
  .messages { padding: 12px; gap: 10px; }
  .msg-body { max-width: 84%; }
  /* 触屏没有 hover，撤回按钮常显 */
  .msg-actions { opacity: 1; }
  .typing { padding: 0 12px 4px; }

  .composer { padding: 8px 12px calc(10px + env(safe-area-inset-bottom)); }
  /* 16px 可避免 iOS Safari 聚焦时自动放大页面 */
  .composer-box textarea { font-size: 16px; }
  .send-btn { width: 36px; height: 36px; }
  .composer-foot span:last-child { display: none; }
  .emoji-bar {
    flex-wrap: nowrap; overflow-x: auto; margin-bottom: 6px;
    padding-bottom: 2px; -webkit-overflow-scrolling: touch;
  }
  .emoji-bar::-webkit-scrollbar { display: none; }
  .emoji-bar button { width: 32px; height: 32px; flex: none; font-size: 17px; }

  .modal { padding: 18px; border-radius: var(--radius); }
  #toasts { left: 12px; right: 12px; transform: none; }

  /* 管理后台 */
  .admin-body { grid-template-columns: 1fr; }
  .admin-nav { display: flex; overflow-x: auto; border-right: none; border-bottom: 1px solid var(--border); padding: 0; }
  .admin-nav button { white-space: nowrap; border-left: none; border-bottom: 2px solid transparent; padding: 10px 14px; }
  .admin-nav button.active { border-left: none; border-bottom-color: var(--accent); }
  .admin-head { padding: 10px 12px; }
  .admin-head .brand-slogan, .admin-head #whoami { display: none; }
  .admin-main { padding: 16px 12px calc(16px + env(safe-area-inset-bottom)); }
  .stat-grid { grid-template-columns: repeat(auto-fit, minmax(104px, 1fr)); }
  .settings-grid { grid-template-columns: 1fr; }
  .table-actions .btn { padding: 5px 9px; }
  thead th, tbody td { padding: 8px 10px; white-space: nowrap; }
}

