: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, .field select {
  width: 100%; border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); padding: 10px 12px; font: inherit; color: inherit;
}
.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; }
.msg.flash .bubble { animation: flash 1.2s ease; }
@keyframes flash { 0%, 40% { box-shadow: 0 0 0 3px var(--badge); } 100% { box-shadow: none; } }
.bubble {
  display: flex; flex-direction: column; gap: 6px;
  padding: 8px 12px; border-radius: 16px; white-space: pre-wrap; overflow-wrap: anywhere; max-width: 100%;
}
.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); }
.bubble.image-only { padding: 0; background: none !important; box-shadow: none !important; }

/* 引用 */
.quote {
  display: grid; gap: 1px; text-align: left; cursor: pointer;
  border: 0; border-left: 3px solid currentColor; border-radius: 6px;
  padding: 4px 8px; font-size: 13px; line-height: 1.4;
  background: rgba(0, 0, 0, .06); color: inherit; opacity: .85; max-width: 100%;
}
.mine .quote { background: rgba(255, 255, 255, .16); }
.quote-name { font-weight: 700; }
.quote-text { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* 图片 */
.img-box {
  display: grid; place-items: center; max-width: 100%;
  border: 0; padding: 0; border-radius: 12px; overflow: hidden; cursor: zoom-in;
  background: var(--active); color: var(--muted); font-size: 12px;
}
.img-box img { width: 100%; height: 100%; object-fit: cover; display: block; }
.img-box.failed { cursor: default; padding: 8px; }
.image-dialog { padding: 0; border: 0; background: transparent; max-width: 96vw; max-height: 96dvh; }
.image-dialog::backdrop { background: rgba(0, 0, 0, .85); }
.image-dialog img { display: block; max-width: 96vw; max-height: 92dvh; object-fit: contain; cursor: zoom-out; }

/* 时间和“引用”按钮 */
.meta { display: flex; align-items: center; gap: 8px; margin: 2px 4px 0; }
.time { font-size: 11px; color: var(--muted); }
.reply-action { border: 0; background: none; padding: 0; font-size: 11px; color: var(--muted); cursor: pointer; }
.reply-action:hover { color: var(--accent); text-decoration: underline; }
@media (hover: hover) {
  .reply-action { opacity: 0; }
  .msg:hover .reply-action, .reply-action:focus-visible { opacity: 1; }
}

/* 输入框上方：引用条、Emoji 面板 */
.reply-bar {
  display: flex; align-items: center; gap: 8px;
  margin: 0 12px; padding: 6px 4px 6px 10px;
  border-left: 3px solid var(--accent); background: var(--active); border-radius: 8px 8px 0 0;
}
.reply-bar-text { flex: 1; min-width: 0; margin: 0; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#reply-bar-name { font-weight: 700; }
.emoji-panel { border-top: 1px solid var(--line); background: var(--surface); }
.emoji-tabs { display: flex; gap: 2px; padding: 6px 8px 0; border-bottom: 1px solid var(--line); overflow-x: auto; }
.emoji-tab { border: 0; background: none; font-size: 20px; padding: 4px 10px 6px; border-radius: 8px 8px 0 0; cursor: pointer; }
.emoji-tab[aria-pressed="true"] { background: var(--active); }
.emoji-grid {
  display: grid; grid-template-columns: repeat(auto-fill, minmax(40px, 1fr));
  max-height: 200px; overflow-y: auto; padding: 6px 8px;
}
.emoji { border: 0; background: none; font-size: 24px; line-height: 1; padding: 6px 0; border-radius: 8px; cursor: pointer; }
.emoji:hover { background: var(--active); }
.icon-button {
  flex: none; width: 38px; height: 38px; display: grid; place-items: center;
  border: 0; background: none; border-radius: 10px; color: var(--muted); font-size: 22px; cursor: pointer;
}
.icon-button:hover { background: var(--active); color: var(--ink); }
.icon-button:disabled { opacity: .45; cursor: default; }

.composer {
  display: flex; align-items: flex-end; gap: 4px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
}
.composer textarea {
  flex: 1; min-width: 0; margin: 0 4px; 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; }
.app-version { margin: 16px 0 0; font-size: 12px; color: var(--muted); text-align: right; }

.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; }
}

/* ---------- 第6版：已读、撤回、翻译、搜索 ---------- */
.read-status { font-size: 11px; color: var(--muted); }
.read-status.read { color: var(--accent); }
.recalled { align-self: center; margin: 6px 0; font-size: 12px; color: var(--muted); }
.translation {
  display: block; margin-top: 2px; padding-top: 6px;
  border-top: 1px dashed currentColor; font-size: 14px; opacity: .85;
}
.translation.pending, .translation.failed { font-size: 12px; opacity: .7; }
.check-field { display: flex; gap: 10px; align-items: flex-start; cursor: pointer; }
.check-field input { margin-top: 4px; width: 18px; height: 18px; flex: none; }
.check-field small { display: block; font-size: 12px; color: var(--muted); }
.header-search { margin-left: auto; width: 34px; height: 34px; }
.search-bar { border-bottom: 1px solid var(--line); padding: 8px 12px; background: var(--surface); }
.search-bar .close-button { flex: none; }
.search-results { max-height: 50vh; overflow-y: auto; margin-top: 6px; }
.search-note { padding: 8px 4px; font-size: 13px; color: var(--muted); }
.search-hit {
  width: 100%; display: grid; gap: 2px; text-align: left; cursor: pointer;
  border: 0; background: none; padding: 8px 6px; border-radius: 8px;
}
.search-hit:hover { background: var(--active); }
.search-hit-head { display: flex; gap: 8px; font-size: 12px; color: var(--muted); }
.search-hit-name { font-weight: 700; color: var(--ink); }
.search-hit-text { font-size: 14px; overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.search-hit mark { background: #ffe08a; color: #1b2430; border-radius: 3px; padding: 0 1px; }
