:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --surface-2: #efede8;
  --text: #1f1e1c;
  --muted: #6b6862;
  --border: #e2dfd8;
  --accent: #3d6b5a;
  --accent-text: #ffffff;
  --bubble-me: #3d6b5a;
  --bubble-me-text: #ffffff;
  --bubble-peer: #ffffff;
  --danger: #b4412f;
  --online: #2f9e5b;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #171715;
    --surface: #22211f;
    --surface-2: #2c2b28;
    --text: #ecebe7;
    --muted: #a19e97;
    --border: #3a3935;
    --accent: #6fa892;
    --accent-text: #0f1a16;
    --bubble-me: #4c8572;
    --bubble-me-text: #ffffff;
    --bubble-peer: #2c2b28;
    --danger: #ef8a76;
    --online: #5cc98a;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}
button, input, textarea { font: inherit; color: inherit; }
button {
  border: 0; border-radius: 10px; padding: 9px 16px; cursor: pointer;
  background: var(--accent); color: var(--accent-text); font-weight: 600;
}
button.secondary { background: var(--surface-2); color: var(--text); font-weight: 500; }
button:disabled { opacity: .5; cursor: default; }
input, textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 10px; background: var(--surface);
}
input:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
label { display: block; font-size: 14px; color: var(--muted); margin: 14px 0 6px; }
.muted { color: var(--muted); font-size: 14px; }
.error { color: var(--danger); font-size: 14px; min-height: 20px; margin-top: 10px; }
.hint { font-size: 14px; min-height: 20px; margin-top: 6px; color: var(--muted); }
.hint.ok { color: var(--accent); }
.hint.error { color: var(--danger); }
.row { display: flex; gap: 8px; }
.row input { flex: 1; min-width: 0; }
.stack { display: flex; flex-direction: column; gap: 8px; }
.wide { width: 100%; margin-top: 8px; }

.icon-button {
  position: relative; display: grid; place-items: center; width: 40px; height: 40px; padding: 0;
  border-radius: 50%; background: transparent; color: var(--text); font-size: 22px; font-weight: 400;
}
.icon-button:hover { background: var(--surface-2); }
.badge {
  display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 10px; background: var(--danger);
  color: #fff; font-size: 12px; font-weight: 600; line-height: 20px; text-align: center;
}
.icon-button .badge { position: absolute; top: -2px; right: -4px; }

.avatar {
  --hue: 160;
  flex: 0 0 auto; display: grid; place-items: center; width: 44px; height: 44px; border-radius: 50%;
  background: hsl(var(--hue) 35% 45%); color: #fff; font-weight: 600; font-size: 18px;
}
.avatar.small { width: 32px; height: 32px; font-size: 14px; }

/* 登录 */
.auth { max-width: 380px; margin: 12vh auto; padding: 0 16px; }
.auth .card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 28px 24px; }
.auth h1 { font-size: 22px; margin: 0 0 4px; }
.auth button { width: 100%; margin-top: 20px; }

/* 主界面 */
.app { display: grid; grid-template-columns: 320px 1fr; height: 100dvh; max-width: 1200px; margin: 0 auto; }
.sidebar { display: flex; flex-direction: column; min-height: 0; border-right: 1px solid var(--border); background: var(--surface); }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; border-bottom: 1px solid var(--border); }
.me-button { display: flex; align-items: center; gap: 10px; background: none; color: var(--text); padding: 4px 8px; font-weight: 600; min-width: 0; }
.me-button span:last-child { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conn-banner { background: var(--danger); color: #fff; font-size: 13px; text-align: center; padding: 4px; }
.conv-list { flex: 1; overflow-y: auto; }
.conv-list .empty { padding: 24px 16px; color: var(--muted); font-size: 14px; text-align: center; }
.conv-item {
  display: flex; gap: 12px; width: 100%; padding: 12px 14px; align-items: center; text-align: left;
  background: none; color: var(--text); border-radius: 0; font-weight: 400;
}
.conv-item:hover { background: var(--surface-2); }
.conv-item.active { background: var(--surface-2); }
.avatar-wrap { position: relative; }
.dot { position: absolute; right: 0; bottom: 0; width: 12px; height: 12px; border-radius: 50%; background: var(--online); border: 2px solid var(--surface); }
.conv-text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.conv-top, .conv-bottom { display: flex; justify-content: space-between; gap: 8px; align-items: center; }
.conv-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { font-size: 12px; color: var(--muted); flex: 0 0 auto; }
.conv-preview { font-size: 14px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.placeholder { margin: auto; color: var(--muted); }
.main-head { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--border); background: var(--bg); }
.main-head .back { display: none; }
.peer { flex: 1; min-width: 0; }
.peer-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.peer-status { font-size: 12px; color: var(--muted); }
.peer-status.online { color: var(--online); }

.messages { flex: 1; overflow-y: auto; padding: 12px 16px; display: flex; flex-direction: column; gap: 4px; }
.messages .empty, .retention { text-align: center; color: var(--muted); font-size: 13px; margin: 8px 0; }
.load-more { align-self: center; font-size: 13px; padding: 6px 14px; margin-bottom: 8px; }
.time-sep { align-self: center; font-size: 12px; color: var(--muted); margin: 10px 0 4px; }
.msg-row { display: flex; }
.msg-row.mine { justify-content: flex-end; }
.bubble {
  max-width: 75%; padding: 8px 12px; border-radius: 18px; white-space: pre-wrap; overflow-wrap: anywhere;
  background: var(--bubble-peer); border: 1px solid var(--border); border-bottom-left-radius: 6px;
}
.mine .bubble { background: var(--bubble-me); color: var(--bubble-me-text); border-color: transparent; border-bottom-left-radius: 18px; border-bottom-right-radius: 6px; }
.bubble img { display: block; max-width: 100%; max-height: 320px; border-radius: 12px; margin: 2px 0; }
.bubble img + .text, .bubble a + .text { display: block; margin-top: 4px; }
.bubble.pending { opacity: .6; }
.bubble.failed { outline: 2px solid var(--danger); }
.receipt { align-self: flex-end; font-size: 12px; color: var(--muted); margin: 0 4px 4px; }
.receipt.failed { color: var(--danger); }

.attach-preview { position: relative; padding: 10px 16px 0; border-top: 1px solid var(--border); background: var(--bg); }
.attach-preview img { height: 72px; border-radius: 10px; display: block; }
.attach-preview .remove {
  position: absolute; top: 4px; left: 76px; width: 24px; height: 24px; padding: 0; border-radius: 50%;
  background: var(--text); color: var(--bg); line-height: 24px; font-size: 16px;
}
.attach-preview:not([hidden]) + .composer { border-top: 0; }
.composer {
  display: flex; gap: 8px; align-items: flex-end; padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--border); background: var(--bg);
}
.composer textarea { flex: 1; resize: none; max-height: 140px; border-radius: 20px; padding: 9px 16px; }
.composer button[type="submit"] { border-radius: 20px; }

/* 无痕聊天：深色独立窗口，覆盖整个界面，和普通聊天明显区分 */
.secret {
  --bg: #15131c; --surface: #1f1c29; --surface-2: #2a2637; --text: #ece9f5; --muted: #a49db8; --border: #393350;
  --bubble-me: #6d55c9; --bubble-me-text: #ffffff; --bubble-peer: #2a2637; --accent: #8f78e8; --accent-text: #ffffff;
  position: fixed; inset: 0; z-index: 10; display: flex; flex-direction: column;
  max-width: 1200px; margin: 0 auto; background: var(--bg); color: var(--text);
}
.secret-head {
  display: flex; align-items: center; gap: 12px; padding: 10px 14px;
  border-bottom: 1px dashed var(--accent); background: var(--surface);
}
.secret-tag {
  display: inline-block; padding: 0 8px; border-radius: 6px; background: var(--accent); color: var(--accent-text);
  font-size: 12px; line-height: 20px; vertical-align: 2px;
}
.secret .peer-status { white-space: normal; }
button.danger { background: var(--danger); color: #fff; flex: 0 0 auto; }

.toast {
  position: fixed; left: 50%; bottom: calc(80px + env(safe-area-inset-bottom)); transform: translateX(-50%); z-index: 20;
  max-width: calc(100vw - 32px); padding: 10px 16px; border-radius: 12px;
  background: var(--text); color: var(--bg); font-size: 14px; box-shadow: 0 4px 16px rgb(0 0 0 / .2);
}

/* 对话框 */
dialog {
  width: min(440px, calc(100vw - 32px)); max-height: calc(100dvh - 32px); overflow-y: auto;
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); color: var(--text); padding: 18px 20px;
}
dialog::backdrop { background: rgb(0 0 0 / .4); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.dialog-head h2 { font-size: 18px; margin: 0; }
dialog h3 { font-size: 14px; color: var(--muted); font-weight: 600; margin: 18px 0 8px; }
.req-list { display: flex; flex-direction: column; gap: 8px; }
.req-item { display: flex; align-items: center; gap: 10px; }
.req-item .req-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.req-item button { padding: 6px 12px; font-size: 14px; }

/* 手机：列表和会话二选一显示 */
@media (max-width: 720px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; }
  .app.in-chat .sidebar { display: none; }
  .app:not(.in-chat) .main { display: none; }
  .main-head .back { display: grid; }
  .bubble { max-width: 85%; }
}
