/* ================= 全局 ================= */
* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
:root {
  --bg: #0f1117;
  --panel: #171a23;
  --panel2: #1f2330;
  --border: #2a2f3d;
  --primary: #4f7cff;
  --primary-hover: #3d68e8;
  --text: #e7eaf3;
  --text-sub: #8a92a6;
  --danger: #f05252;
  --ok: #22c55e;
  --warn: #f59e0b;
  --mine: #4f7cff;
  --other: #262b38;
}
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg); color: var(--text); font-size: 14px;
  height: 100vh; overflow: hidden;
}
.hidden { display: none !important; }
button { font-family: inherit; cursor: pointer; }
input, textarea {
  font-family: inherit; outline: none; border: 1px solid var(--border);
  background: var(--panel2); color: var(--text); border-radius: 8px;
  padding: 10px 12px; font-size: 14px; width: 100%;
}
input:focus, textarea:focus { border-color: var(--primary); }
.btn-primary {
  background: var(--primary); color: #fff; border: none; border-radius: 8px;
  padding: 10px 18px; font-size: 14px; font-weight: 600;
}
.btn-primary:hover { background: var(--primary-hover); }
.btn-ghost {
  background: transparent; color: var(--text-sub); border: 1px solid var(--border);
  border-radius: 8px; padding: 10px 18px;
}
.icon-btn {
  background: transparent; border: none; color: var(--text-sub);
  font-size: 18px; width: 34px; height: 34px; border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
}
.icon-btn:hover { background: var(--panel2); color: var(--text); }
.empty-tip { color: var(--text-sub); text-align: center; padding: 40px 16px; }
.empty-tip.small { padding: 16px; font-size: 13px; }

/* ================= 登录页 ================= */
.auth-body {
  display: flex; align-items: center; justify-content: center;
  background: radial-gradient(circle at 30% 20%, #1a2137, #0b0d13 70%);
  height: 100vh;
}
.auth-card {
  width: 92%; max-width: 380px; background: var(--panel);
  border: 1px solid var(--border); border-radius: 16px;
  padding: 32px 26px; box-shadow: 0 20px 60px rgba(0,0,0,.5);
}
.auth-logo { text-align: center; font-size: 24px; margin-bottom: 24px; }
.auth-tabs { display: flex; gap: 8px; margin-bottom: 20px; background: var(--panel2); padding: 4px; border-radius: 10px; }
.tab {
  flex: 1; background: transparent; border: none; color: var(--text-sub);
  padding: 10px; border-radius: 8px; font-size: 14px; font-weight: 600;
}
.tab.active { background: var(--primary); color: #fff; }
.auth-form { display: flex; flex-direction: column; gap: 12px; }
.auth-form .btn-primary { margin-top: 6px; padding: 12px; }
.auth-msg { text-align: center; margin-top: 14px; font-size: 13px; min-height: 18px; }

/* ================= 聊天布局 ================= */
.app { display: flex; height: 100vh; }
.sidebar {
  width: 320px; flex-shrink: 0; background: var(--panel);
  border-right: 1px solid var(--border); display: flex; flex-direction: column;
}
.side-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 14px 10px; border-bottom: 1px solid var(--border);
}
.me { display: flex; align-items: center; gap: 10px; min-width: 0; }
.me-info { display: flex; flex-direction: column; min-width: 0; }
.me-info b { font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.me-info span { font-size: 12px; color: var(--text-sub); }
.side-actions { display: flex; gap: 2px; }

/* 头像 */
.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0;
  background: linear-gradient(135deg, #4f7cff, #8a5cf6);
  background-size: cover; background-position: center;
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-weight: 700; font-size: 16px;
}
.avatar.small { width: 44px; height: 44px; font-size: 16px; }
.avatar.tiny { width: 30px; height: 30px; font-size: 13px; }

/* 搜索 */
.search-box { padding: 10px 12px; position: relative; }
.search-result {
  position: absolute; left: 12px; right: 12px; top: 56px; z-index: 20;
  background: var(--panel2); border: 1px solid var(--border); border-radius: 10px;
  max-height: 300px; overflow-y: auto; box-shadow: 0 10px 30px rgba(0,0,0,.4);
}
.search-item { display: flex; align-items: center; gap: 10px; padding: 10px 12px; cursor: pointer; }
.search-item:hover { background: #2a2f3d; }
.search-item em { color: var(--text-sub); font-style: normal; font-size: 12px; }

/* 会话列表 */
.conv-list { flex: 1; overflow-y: auto; }
.conv-item {
  display: flex; align-items: center; gap: 12px; padding: 12px 14px;
  cursor: pointer; border-bottom: 1px solid rgba(42,47,61,.4);
}
.conv-item:hover, .conv-item.active { background: var(--panel2); }
.conv-main { flex: 1; min-width: 0; }
.conv-top { display: flex; justify-content: space-between; align-items: center; }
.conv-name { font-size: 14px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-name em { color: var(--text-sub); font-style: normal; font-size: 11px; font-weight: 400; }
.conv-time { font-size: 11px; color: var(--text-sub); flex-shrink: 0; margin-left: 8px; }
.conv-bottom { display: flex; justify-content: space-between; align-items: center; margin-top: 4px; }
.conv-preview { font-size: 13px; color: var(--text-sub); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge {
  background: var(--danger); color: #fff; font-size: 11px; font-weight: 700;
  min-width: 18px; height: 18px; border-radius: 9px; padding: 0 5px;
  display: flex; align-items: center; justify-content: center; margin-left: 6px;
}

/* ================= 聊天区 ================= */
.chat-area { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.chat-header {
  height: 56px; display: flex; align-items: center; padding: 0 16px; gap: 8px;
  border-bottom: 1px solid var(--border); background: var(--panel);
}
.chat-title { flex: 1; font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-actions { display: flex; gap: 2px; }
.back-btn { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 18px 16px; display: flex; flex-direction: column; gap: 14px; }
.msg { display: flex; gap: 10px; max-width: 78%; }
.msg.other { align-self: flex-start; }
.msg.mine { align-self: flex-end; flex-direction: row-reverse; }
.msg-avatar {
  width: 36px; height: 36px; border-radius: 50%; flex-shrink: 0;
  background-size: cover; background-position: center;
  background: linear-gradient(135deg,#4f7cff,#8a5cf6);
  display: flex; align-items: center; justify-content: center;
  color: #fff; font-size: 14px; font-weight: 700;
}
.msg-body { display: flex; flex-direction: column; min-width: 0; }
.msg.mine .msg-body { align-items: flex-end; }
.msg-meta { font-size: 11px; color: var(--text-sub); margin-bottom: 4px; }
.msg-bubble {
  padding: 10px 14px; border-radius: 12px; line-height: 1.55;
  word-break: break-word; white-space: pre-wrap; font-size: 14px;
  background: var(--other); color: var(--text); border-top-left-radius: 4px;
}
.msg.mine .msg-bubble { background: var(--mine); color: #fff; border-top-left-radius: 12px; border-top-right-radius: 4px; }
.msg-bubble.recalled { color: var(--text-sub); font-style: italic; background: transparent; border: 1px dashed var(--border); }
.msg-bubble.image { padding: 4px; background: var(--panel2); }
.msg-bubble.image img { max-width: 240px; max-height: 240px; border-radius: 8px; display: block; cursor: pointer; }
.msg-tools { display: flex; gap: 8px; margin-top: 4px; opacity: 0; transition: opacity .15s; }
.msg:hover .msg-tools { opacity: 1; }
.mt {
  background: transparent; border: none; color: var(--text-sub);
  font-size: 11px; padding: 2px 4px; border-radius: 4px;
}
.mt:hover { color: var(--primary); background: var(--panel2); }

/* 输入栏 */
.input-bar {
  display: flex; align-items: flex-end; gap: 8px; padding: 12px 16px;
  border-top: 1px solid var(--border); background: var(--panel);
}
.input-bar textarea {
  flex: 1; resize: none; max-height: 120px; line-height: 1.5;
  padding: 10px 12px; border-radius: 10px;
}
.input-bar .btn-primary { padding: 10px 20px; flex-shrink: 0; }

/* ================= 弹窗 ================= */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.6);
  display: flex; align-items: center; justify-content: center; z-index: 100;
  padding: 16px;
}
.modal-box {
  width: 100%; max-width: 420px; background: var(--panel);
  border: 1px solid var(--border); border-radius: 16px;
  padding: 22px; max-height: 88vh; overflow-y: auto;
}
.modal-box h3 { font-size: 17px; margin-bottom: 16px; }
.modal-box label { display: block; font-size: 12px; color: var(--text-sub); margin: 12px 0 6px; }
.modal-tabs { display: flex; gap: 8px; margin-bottom: 12px; }
.mtab {
  flex: 1; background: var(--panel2); border: none; color: var(--text-sub);
  padding: 9px; border-radius: 8px; font-size: 13px; font-weight: 600;
}
.mtab.active { background: var(--primary); color: #fff; }
.pick-list { max-height: 180px; overflow-y: auto; margin-top: 10px; }
.pick-item { padding: 10px 12px; border-radius: 8px; cursor: pointer; }
.pick-item:hover { background: var(--panel2); }
.pick-item em { color: var(--text-sub); font-style: normal; font-size: 12px; }
.picked-list { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; }
.tag {
  background: var(--panel2); border: 1px solid var(--border);
  border-radius: 20px; padding: 4px 10px; font-size: 12px;
  display: flex; align-items: center; gap: 6px;
}
.tag i { cursor: pointer; font-style: normal; color: var(--text-sub); }
.tag i:hover { color: var(--danger); }
#groupNameWrap { margin-top: 12px; }
.modal-foot { display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px; }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 60px; transform: translateX(-50%);
  background: rgba(30,34,45,.95); color: #fff; padding: 10px 20px;
  border-radius: 10px; font-size: 13px; z-index: 999;
  box-shadow: 0 8px 24px rgba(0,0,0,.4); border: 1px solid var(--border);
}

/* ================= 管理后台 ================= */
.admin-body { overflow: auto; height: auto; min-height: 100vh; background: var(--bg); }
.admin-wrap { max-width: 1200px; margin: 0 auto; padding: 20px; }
.admin-header {
  display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between;
  gap: 12px; padding-bottom: 16px; border-bottom: 1px solid var(--border); margin-bottom: 20px;
}
.admin-header h1 { font-size: 20px; }
.admin-nav { display: flex; flex-wrap: wrap; gap: 8px; }
.anav {
  background: var(--panel); border: 1px solid var(--border); color: var(--text-sub);
  padding: 8px 16px; border-radius: 8px; font-size: 13px; font-weight: 600;
}
.anav.active { background: var(--primary); border-color: var(--primary); color: #fff; }
.anav.danger { color: var(--danger); border-color: rgba(240,82,82,.4); }
.admin-page { animation: fade .2s; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 16px; }
.stat-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: 12px;
  padding: 24px; text-align: center;
}
.stat-card span { font-size: 32px; font-weight: 700; color: var(--primary); }
.stat-card p { color: var(--text-sub); font-size: 13px; margin-top: 8px; }

.toolbar { display: flex; gap: 10px; margin-bottom: 16px; }
.toolbar input { flex: 1; max-width: 320px; }
.table-wrap { overflow-x: auto; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; }
.admin-table { width: 100%; border-collapse: collapse; min-width: 640px; }
.admin-table th, .admin-table td { padding: 12px 14px; text-align: left; font-size: 13px; border-bottom: 1px solid var(--border); }
.admin-table th { color: var(--text-sub); font-weight: 600; background: var(--panel2); white-space: nowrap; }
.admin-table tr:last-child td { border-bottom: none; }
.admin-table tr:hover td { background: rgba(79,124,255,.04); }
.ops { display: flex; gap: 6px; flex-wrap: wrap; }
.mini {
  background: var(--panel2); border: 1px solid var(--border); color: var(--text);
  padding: 5px 10px; border-radius: 6px; font-size: 12px;
}
.mini:hover { border-color: var(--primary); }
.mini.ok { color: var(--ok); border-color: rgba(34,197,94,.4); }
.mini.warn { color: var(--warn); border-color: rgba(245,158,11,.4); }
.mini.danger { color: var(--danger); border-color: rgba(240,82,82,.4); }
.mini:disabled { opacity: .4; cursor: not-allowed; }
.badge-admin { background: rgba(79,124,255,.2); color: #7c9dff; padding: 2px 8px; border-radius: 6px; font-size: 12px; }
.badge-ok { color: var(--ok); }
.badge-ban { color: var(--danger); font-weight: 600; }
.badge-warn { color: var(--warn); }
.pager { display: flex; align-items: center; gap: 10px; justify-content: flex-end; margin-top: 14px; color: var(--text-sub); font-size: 13px; }

/* ================= 移动端适配（含平板 1024px 以下） ================= */
@media (max-width: 1024px) {
  /* app 作为绝对定位容器，撑满视口 */
  .app {
    position: relative;
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
  }

  /* 会话列表：默认铺满整屏（手机上先展示列表） */
  .sidebar {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    width: 100%;
    z-index: 20;
    display: flex;
    flex-direction: column;
  }
  /* 进入聊天后隐藏列表 */
  body.chat-open .sidebar { display: none; }

  /* 聊天区：绝对定位铺满，默认隐藏 */
  .chat-area {
    position: absolute;
    top: 0; left: 0; right: 0; bottom: 0;
    width: 100%;
    height: 100%;
    z-index: 10;
    display: none;
    flex-direction: column;
    overflow: hidden;
  }
  body.chat-open .chat-area { display: flex; }

  /* ---- 关键修复：输入栏强制可见，不依赖 JS 的 display ---- */
  .chat-area .input-bar {
    display: flex !important;
    flex-shrink: 0;
    align-items: flex-end;
    gap: 8px;
    padding: 8px 10px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom, 0px));
    background: var(--panel);
    border-top: 1px solid var(--border);
  }
  .input-bar textarea {
    font-size: 16px;          /* 防止 iOS 聚焦时自动放大 */
    min-height: 40px;
    max-height: 100px;
  }
  .input-bar .btn-primary { padding: 10px 16px; }
  .input-bar .icon-btn { width: 38px; height: 38px; font-size: 20px; }

  /* 消息区自适应剩余高度 */
  .messages {
    flex: 1 1 auto;
    min-height: 0;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .chat-header { flex-shrink: 0; }
  .back-btn { display: flex; }
  .msg { max-width: 88%; }
  .msg-bubble.image img { max-width: 180px; }
  .msg-tools { opacity: 1; }  /* 触屏没有 hover，操作按钮常显 */

  /* 管理后台 */
  .admin-nav { width: 100%; }
  .anav { flex: 1; text-align: center; padding: 8px; font-size: 12px; }
}

/* 超小屏（窄手机）进一步压缩间距 */
@media (max-width: 400px) {
  .input-bar { padding: 6px 8px; padding-bottom: calc(6px + env(safe-area-inset-bottom, 0px)); }
  .input-bar .btn-primary { padding: 9px 14px; font-size: 13px; }
}