:root {
  --sidebar-bg: #4a154b;
  --sidebar-bg-hover: #5c2a5d;
  --active-bg: #1164a3;
  --border: #e8e8e8;
  --text: #1d1c1d;
  --muted: #616061;
  --err: #e01e5a;
  --bubble-mine: #95ec69;
  --bubble-mine-border: #87d95f;
  --bubble-theirs: #fff;
  --bubble-theirs-border: #e0e0e0;
  --page-bg: #ededed;
  --surface: #fff;
  --overlay-bg: rgba(0,0,0,.45);
  --modal-shadow: 0 8px 32px rgba(0,0,0,.2);
  --warn-bg: #fff8e6;
  --warn-text: #9a6700;
  --sys-bg: rgba(0,0,0,.06);
  --err-bg: #ffe8ef;
  --online: #2bac76;
  --btn-send: #07c160;
  --btn-send-hover: #06ad56;
  --btn-primary-bg: var(--sidebar-bg);
  --btn-primary-border: var(--sidebar-bg);
  --radius: 8px;
  --radius-sm: 6px;
  --avatar: 36px;
  --msg-max-width: min(520px, 68%);
}

@media (prefers-color-scheme: dark) {
  :root {
    --sidebar-bg: #1a1d21;
    --sidebar-bg-hover: #272a2e;
    --active-bg: #1264a3;
    --border: #3a3d41;
    --text: #d1d2d3;
    --muted: #9a9b9e;
    --err: #e01e5a;
    --bubble-mine: #07c160;
    --bubble-mine-border: #06ad56;
    --bubble-theirs: #2c2f33;
    --bubble-theirs-border: #3a3d41;
    --page-bg: #1e2124;
    --surface: #2c2f33;
    --overlay-bg: rgba(0,0,0,.65);
    --modal-shadow: 0 8px 32px rgba(0,0,0,.5);
    --warn-bg: #3a3000;
    --warn-text: #d4a017;
    --sys-bg: rgba(255,255,255,.06);
    --err-bg: #3a1020;
    --online: #2bac76;
    --btn-send: #07c160;
    --btn-send-hover: #06ad56;
  }
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body { font: 15px/1.45 system-ui, sans-serif; color: var(--text); background: var(--page-bg); }

/* ========== 根布局 ========== */
.app {
  display: grid;
  grid-template-columns: 260px 1fr;
  grid-template-rows: 1fr;
  grid-template-areas: "sidebar main";
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

/* ========== 侧边栏 ========== */
.sidebar {
  grid-area: sidebar;
  background: var(--sidebar-bg);
  color: #fff;
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 0;
  overflow: hidden;
}

.side-head {
  padding: 14px 16px;
  border-bottom: 1px solid rgba(255,255,255,.1);
  display: grid;
  grid-template-columns: var(--avatar) 1fr;
  gap: 10px;
  align-items: center;
}
.side-head-text { min-width: 0; }
.side-title { font-weight: 800; font-size: 16px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.side-sub { font-size: 11px; opacity: .65; display: flex; align-items: center; gap: 6px; }
#sideMeta:not([hidden])::after { content: "·"; margin-left: 2px; opacity: .7; }

.nav { overflow-y: auto; padding: 14px 0; min-height: 0; }
.nav h4 { padding: 0 16px 6px; font-size: 11px; text-transform: uppercase; opacity: .55; letter-spacing: .04em; }

.nav-btn {
  display: flex; align-items: center; gap: 8px;
  width: 100%; padding: 8px 16px 8px 20px;
  border: 0; background: none; color: inherit; font: inherit;
  cursor: pointer; text-align: left;
}
.nav-btn:hover { background: var(--sidebar-bg-hover); }
.nav-btn.on { background: var(--active-bg); font-weight: 700; }
.nav-btn .lbl { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }

.side-foot { padding: 12px 16px; border-top: 1px solid rgba(255,255,255,.1); }
.side-foot button {
  width: 100%; padding: 9px; border: 1px solid rgba(255,255,255,.25);
  border-radius: var(--radius-sm); background: transparent; color: #fff; cursor: pointer;
}

/* ========== 主区域 ========== */
.main {
  grid-area: main;
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-width: 0; min-height: 0;
}

.top {
  padding: 0 20px; border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 8px;
  min-height: 52px; max-height: 52px;
}
.top h1 { font-size: 17px; font-weight: 800; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.top .meta { color: var(--muted); font-size: 13px; flex-shrink: 0; }
.top .spacer { flex: 1; min-width: 4px; }
.btn-top {
  flex-shrink: 0; border: 1px solid var(--border); background: var(--surface);
  padding: 6px 12px; border-radius: var(--radius-sm); cursor: pointer; font-size: 13px;
}

/* ========== 消息区域 ========== */
.msgs {
  overflow-y: auto;
  padding: 16px 20px;
  background: var(--page-bg);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.msg {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  max-width: var(--msg-max-width);
}
.msg.mine {
  flex-direction: row-reverse;
  margin-left: auto;
}

.msg .bubble-wrap {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 0;
}
.msg.mine .bubble-wrap { align-items: flex-end; }
.msg.theirs .bubble-wrap { align-items: flex-start; }

.msg .bubble {
  padding: 10px 13px;
  border-radius: var(--radius);
}
.msg.theirs .bubble {
  background: var(--bubble-theirs);
  border: 1px solid var(--bubble-theirs-border);
  border-top-left-radius: 2px;
}
.msg.mine .bubble {
  background: var(--bubble-mine);
  border: 1px solid var(--bubble-mine-border);
  border-top-right-radius: 2px;
}
.msg .who { font-size: 12px; font-weight: 600; color: var(--muted); }
.msg .txt { white-space: pre-wrap; word-break: break-word; line-height: 1.45; }
.msg .when { font-size: 11px; color: var(--muted); }

.msg > .avatar { align-self: start; }

.msg.sys {
  align-self: center; max-width: 80%;
  color: var(--muted); font-size: 12px;
  padding: 8px 12px; background: var(--sys-bg);
  border-radius: 4px; text-align: center;
}
.msg.err {
  align-self: center; max-width: 90%;
  background: var(--err-bg); color: var(--err);
  padding: 10px 12px; border-radius: var(--radius);
}

/* ========== 输入区域 ========== */
.compose {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: 12px 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  position: relative;
}

.compose-row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
}

.btn-emoji {
  flex-shrink: 0;
  width: 40px; height: 44px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  font-size: 20px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .15s;
}
.btn-emoji:hover { background: var(--page-bg); }

.compose textarea {
  flex: 1; min-width: 0; min-height: 44px; max-height: 140px; resize: none;
  border: 1px solid var(--border); border-radius: var(--radius);
  padding: 10px 14px; font: inherit; outline: none;
  background: var(--surface); color: var(--text);
}
.compose textarea:focus { border-color: var(--active-bg); box-shadow: 0 0 0 1px var(--active-bg); }
.compose textarea:disabled { opacity: .5; }

.btn-send {
  flex-shrink: 0; min-width: 72px; height: 44px; padding: 0 16px;
  border: none; border-radius: var(--radius); font: inherit; font-weight: 700;
  background: var(--btn-send); color: #fff; cursor: pointer;
}
.btn-send:hover:not(:disabled) { background: var(--btn-send-hover); }
.btn-send:disabled { opacity: .45; cursor: not-allowed; }

/* ========== Emoji 面板 ========== */
.emoji-panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px;
  max-height: 240px;
  overflow-y: auto;
  box-shadow: 0 -4px 16px rgba(0,0,0,.08);
}

.emoji-search {
  margin-bottom: 8px;
}
.emoji-search input {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font: inherit;
  outline: none;
  background: var(--page-bg);
  color: var(--text);
}
.emoji-search input:focus {
  border-color: var(--active-bg);
}

.emoji-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(36px, 1fr));
  gap: 4px;
}

.emoji-item {
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  font-size: 22px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  user-select: none;
  transition: background .1s;
}
.emoji-item:hover {
  background: var(--page-bg);
}

/* ========== Dialog ========== */
dialog {
  border: none; padding: 0; background: transparent;
  position: fixed; inset: 0; margin: auto;
  display: flex; align-items: center; justify-content: center;
  z-index: 100;
  max-width: 100vw; max-height: 100vh;
}
dialog::backdrop { background: var(--overlay-bg); }
dialog:not([open]) { display: none; }

.modal {
  background: var(--surface); border-radius: 12px;
  width: 100%; max-width: 420px;
  padding: 24px; box-shadow: var(--modal-shadow); color: var(--text);
  max-height: 90vh; overflow-y: auto;
}
.modal h2 { font-size: 20px; margin-bottom: 16px; }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 12px; font-weight: 700; color: var(--muted); margin-bottom: 5px; }
.field input, .field select {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border);
  border-radius: var(--radius-sm); font: inherit;
  background: var(--surface); color: var(--text);
}

.warn { font-size: 12px; background: var(--warn-bg); color: var(--warn-text); padding: 10px; border-radius: var(--radius-sm); margin-bottom: 12px; }

.actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 20px; }
.btn {
  padding: 10px 18px; border-radius: var(--radius-sm); font: inherit;
  cursor: pointer; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
}
.btn.primary {
  background: var(--btn-primary-bg); color: #fff;
  border-color: var(--btn-primary-border); font-weight: 700;
}

/* ========== 共享组件 ========== */
.avatar {
  width: var(--avatar); height: var(--avatar); border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 13px; color: #fff; flex-shrink: 0;
  user-select: none;
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: #888; }
.dot.on { background: var(--online); }
.dot.off { background: var(--err); }
.badge {
  min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: var(--err);
  font-size: 11px; font-weight: 700; line-height: 18px;
  text-align: center; color: #fff;
}
.nav-btn.on .badge { background: #fff; color: var(--active-bg); }

.sidebar-backdrop { display: none; }
.btn-menu {
  display: none; flex-shrink: 0;
  width: 40px; height: 40px;
  border: 1px solid var(--border); border-radius: var(--radius);
  background: var(--surface); font-size: 20px; line-height: 1; cursor: pointer;
  align-items: center; justify-content: center;
}

/* ========== 移动端 ========== */
@media (max-width: 768px) {
  :root { --avatar: 32px; --msg-max-width: 85%; }

  .app { grid-template-columns: 1fr; }

  .sidebar {
    position: fixed; left: 0; top: 0; bottom: 0;
    width: min(300px, 88vw); z-index: 200;
    transform: translateX(-100%); transition: transform .22s ease;
    padding-top: env(safe-area-inset-top, 0);
    padding-bottom: env(safe-area-inset-bottom, 0);
  }
  body.sidebar-open .sidebar { transform: translateX(0); }

  .sidebar-backdrop { display: none; }
  .sidebar-backdrop {
    position: fixed; inset: 0; z-index: 190;
    background: var(--overlay-bg);
  }
  body.sidebar-open .sidebar-backdrop { display: block; }
  body.sidebar-open { overflow: hidden; }

  .btn-menu {
    display: inline-flex; align-items: center; justify-content: center;
    width: 40px; height: 40px; flex-shrink: 0;
    border: 1px solid var(--border); border-radius: var(--radius);
    background: var(--surface); font-size: 20px; line-height: 1; cursor: pointer;
  }

  .top { min-height: 48px; max-height: none; padding: 8px 14px; gap: 6px; }
  .top h1 { font-size: 16px; }
  .top .meta { display: none; }
  .btn-top { font-size: 12px; padding: 6px 10px; }

  .msgs { padding: 12px 14px; }
  .msg .avatar { width: 32px; height: 32px; font-size: 12px; }

  .compose { padding: 10px 14px max(14px, env(safe-area-inset-bottom, 0)); gap: 6px; }
  .compose-row { gap: 8px; }
  .btn-emoji { width: 38px; height: 42px; font-size: 18px; }
  .compose textarea { font-size: 16px; min-height: 42px; }
  .btn-send { min-width: 64px; height: 42px; padding: 0 14px; }

  .emoji-panel { max-height: 200px; }

  .nav-btn { min-height: 44px; padding: 10px 16px 10px 20px; }
  .side-foot button { min-height: 44px; }

  dialog { align-items: flex-end; padding: 12px; padding-bottom: max(12px, env(safe-area-inset-bottom, 0)); }
  .modal { max-height: 92dvh; overflow-y: auto; border-radius: 14px 14px 0 0; }
  .field input, .field select, .compose textarea { font-size: 16px; }
}
