:root {
  --bg: #e9edf1;
  --surface: #ffffff;
  --side: #f4f6f8;
  --ink: #1b2430;
  --muted: #687384;
  --line: #d5dbe2;
  --mine: #1f5e5b;
  --mine-ink: #ffffff;
  --theirs: #ffffff;
  --theirs-ink: #1b2430;
  --accent: #1f5e5b;
  --active: #dde6ea;
  --danger: #b3261e;
  --badge: #c2410c;
  --banner: #fff4d6;
  --banner-ink: #6b4e00;
  --font: "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "Noto Sans SC", sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11161b;
    --surface: #1b222a;
    --side: #151b21;
    --ink: #e6ebf0;
    --muted: #8d98a7;
    --line: #2b343e;
    --mine: #2e7d77;
    --theirs: #222b34;
    --theirs-ink: #e6ebf0;
    --accent: #5fb3ab;
    --active: #24303a;
    --danger: #ff8a80;
    --badge: #f97316;
    --banner: #3a3015;
    --banner-ink: #f3dc9b;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.55;
  -webkit-text-size-adjust: 100%;
}
[hidden] { display: none !important; }
button, input, textarea { font: inherit; color: inherit; }
button:focus-visible, input:focus-visible, textarea:focus-visible, .conv:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
ul { list-style: none; margin: 0; padding: 0; }

.primary {
  border: 0; border-radius: 10px;
  background: var(--mine); color: var(--mine-ink);
  padding: 9px 16px; font-weight: 600; cursor: pointer; white-space: nowrap;
}
.primary:disabled { opacity: .55; cursor: default; }
.ghost-button {
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); padding: 6px 12px; cursor: pointer;
  font-size: 14px; white-space: nowrap; position: relative;
}
.text-button { border: 0; background: none; color: var(--muted); padding: 6px 4px; cursor: pointer; }
.text-button:hover { color: var(--ink); }

/* 头像：有图片显示图片，没有就显示昵称第一个字 */
.avatar {
  flex: none; width: 36px; height: 36px; border-radius: 50%;
  display: inline-grid; place-items: center; overflow: hidden;
  font-size: 15px; font-weight: 700; color: #fff; background: #6b7a8c;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; display: block; }
.avatar-lg { width: 72px; height: 72px; font-size: 28px; }

.badge {
  display: inline-block; min-width: 18px; height: 18px; padding: 0 5px; margin-left: 6px;
  border-radius: 9px; background: var(--badge); color: #fff;
  font-size: 11px; line-height: 18px; text-align: center; font-weight: 700;
}

/* ---------- 登录 ---------- */
.login {
  min-height: 100%; display: grid; place-items: center;
  padding: max(24px, env(safe-area-inset-top)) 20px max(24px, env(safe-area-inset-bottom));
}
.login-card { width: 100%; max-width: 340px; display: grid; gap: 16px; }
.login-title { margin: 0 0 8px; font-size: 28px; font-weight: 700; }
.field { display: grid; gap: 6px; margin: 0; }
.field > span { font-size: 14px; color: var(--muted); }
.field small { font-size: 12px; color: var(--muted); }
.field input {
  width: 100%; border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); padding: 10px 12px;
}
.readonly { color: var(--ink) !important; font-size: 15px !important; overflow-wrap: anywhere; }
.form-error { min-height: 1.4em; margin: 0; color: var(--danger); font-size: 14px; }
.form-error.ok { color: var(--accent); }

/* ---------- 主画面 ---------- */
.app { height: 100%; height: 100dvh; display: flex; }

.sidebar {
  width: 320px; flex: none; display: flex; flex-direction: column;
  background: var(--side); border-right: 1px solid var(--line); min-height: 0;
}
.side-header {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
  border-bottom: 1px solid var(--line);
}
.me-button {
  display: flex; align-items: center; gap: 10px; min-width: 0;
  border: 0; background: none; padding: 4px; border-radius: 10px; cursor: pointer;
}
.me-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.net-banner { margin: 0; padding: 8px 12px; background: var(--banner); color: var(--banner-ink); font-size: 13px; }

.side-label { margin: 12px 12px 6px; font-size: 13px; font-weight: 600; color: var(--muted); }

.incoming { border-bottom: 1px solid var(--line); padding-bottom: 8px; }
.incoming-list li, .plain-list li {
  display: flex; align-items: center; gap: 10px; padding: 8px 12px;
}
.person { flex: 1; min-width: 0; }
.person-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person-sub { font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-actions { display: flex; gap: 6px; }
.row-actions .primary, .row-actions .ghost-button { padding: 5px 10px; font-size: 13px; }

.conv-list { flex: 1; overflow-y: auto; padding: 6px; }
.conv {
  width: 100%; display: flex; align-items: center; gap: 10px; text-align: left;
  border: 0; background: none; border-radius: 12px; padding: 10px; cursor: pointer;
}
.conv:hover { background: var(--active); }
.conv[aria-current="true"] { background: var(--active); }
.conv-top { display: flex; align-items: baseline; gap: 8px; }
.conv-time { margin-left: auto; font-size: 11px; color: var(--muted); flex: none; }
.conv-bottom { display: flex; align-items: center; gap: 8px; }
.conv-bottom .person-sub { flex: 1; }
.conv .badge { margin-left: 0; }
.side-empty { margin: 24px 16px; color: var(--muted); font-size: 14px; }

/* ---------- 聊天 ---------- */
.chat-pane { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chat-empty { margin: auto; color: var(--muted); }
.chat { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.chat-header {
  display: flex; align-items: center; gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  border-bottom: 1px solid var(--line);
}
.chat-title { margin: 0; font-size: 17px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.back-button { display: none; border: 0; background: none; font-size: 28px; line-height: 1; padding: 0 6px 4px 0; cursor: pointer; }

.messages {
  flex: 1; overflow-y: auto; padding: 16px 14px 8px;
  display: flex; flex-direction: column; gap: 4px; overscroll-behavior: contain;
}
.msg-empty { margin: auto; color: var(--muted); text-align: center; font-size: 15px; }
.day { align-self: center; margin: 14px 0 6px; font-size: 12px; color: var(--muted); }
.msg { display: flex; flex-direction: column; max-width: min(78%, 520px); }
.msg.mine { align-self: flex-end; align-items: flex-end; }
.msg.theirs { align-self: flex-start; align-items: flex-start; }
.msg.gap { margin-top: 10px; }
.bubble { padding: 8px 12px; border-radius: 16px; white-space: pre-wrap; overflow-wrap: anywhere; }
.mine .bubble { background: var(--mine); color: var(--mine-ink); border-bottom-right-radius: 5px; }
.theirs .bubble { background: var(--theirs); color: var(--theirs-ink); border-bottom-left-radius: 5px; box-shadow: 0 0 0 1px var(--line); }
.time { font-size: 11px; color: var(--muted); margin: 2px 4px 0; }

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; resize: none; max-height: 160px;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--surface); padding: 9px 12px; line-height: 1.45;
}

/* ---------- 弹窗 ---------- */
.dialog {
  width: min(420px, calc(100vw - 24px)); max-height: calc(100dvh - 48px);
  border: 1px solid var(--line); border-radius: 16px; padding: 0 18px 18px; overflow-y: auto;
  background: var(--surface); color: var(--ink);
}
.dialog::backdrop { background: rgba(10, 14, 20, .45); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; margin: 0 -18px 12px; padding: 12px 12px 12px 18px; border-bottom: 1px solid var(--line); }
.dialog-head h2 { margin: 0; font-size: 17px; }
.close-button { border: 0; background: none; font-size: 24px; line-height: 1; padding: 4px 8px; cursor: pointer; color: var(--muted); }
.search-row { display: flex; gap: 8px; }
.search-row input { flex: 1; min-width: 0; border: 1px solid var(--line); border-radius: 10px; background: var(--bg); padding: 9px 12px; }
.search-result { margin: 14px 0 4px; min-height: 24px; font-size: 14px; color: var(--muted); }
.result-card { display: flex; align-items: center; gap: 12px; padding: 12px; border: 1px solid var(--line); border-radius: 12px; color: var(--ink); }
.result-card .person-sub { white-space: normal; }
.plain-list li { padding: 8px 0; }
.profile-form { display: grid; gap: 14px; }
.avatar-row { display: flex; align-items: center; gap: 16px; }
.file-button { cursor: pointer; }
.dialog-actions { display: flex; justify-content: space-between; align-items: center; }
.dialog-actions.end { justify-content: flex-end; }
.password-form { margin-top: 20px; padding-top: 16px; border-top: 1px solid var(--line); }
.sub-title { margin: 0; font-size: 15px; }

.boot-error { margin: 40px 20px; padding: 16px; border-radius: 10px; background: var(--banner); color: var(--banner-ink); }

/* ---------- 手机：一次只显示列表或聊天 ---------- */
@media (max-width: 759px) {
  .sidebar { width: 100%; border-right: 0; }
  .chat-pane { display: none; }
  .app.chat-open .sidebar { display: none; }
  .app.chat-open .chat-pane { display: flex; }
  .back-button { display: block; }
  .chat-header { padding-left: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  * { scroll-behavior: auto !important; }
}
