/* Cipher IM · 扁平极简（Linear 风）
   设计约定：
   1) 层次只用 1px 发丝线和底色微差，不用阴影；阴影只留给浮层（弹窗、toast）
   2) 只有一个强调色，只出现在状态点、未读角标、焦点环、输入中提示
   3) 小圆角（8–10px），密度偏紧凑
   4) 浅色/深色两套变量，靠 <html data-theme> 切换（跟随系统由 theme.ts 解析后写入） */

:root {
  color-scheme: light;

  --bg: #F7F7F8;
  --panel: #FFFFFF;
  --surface: #FFFFFF;
  --surface-2: #F2F2F4;
  --line: #E7E7EA;
  --line-strong: #D8D8DE;

  --text: #141417;
  --text-soft: #3C3C43;
  --muted: #87878F;
  --muted-2: #A6A6AE;

  --accent: #2563EB;
  --accent-soft: #E7EEFF;
  --mine: #1B1B1F;
  --mine-text: #FFFFFF;
  --danger: #D93A3A;
  --ok: #16A34A;
  --warn: #A16207;
  --warn-soft: #FBF3E2;
  --shadow: 0 12px 32px rgba(17, 17, 20, 0.12);

  --header-bg: rgba(247, 247, 248, 0.92);

  --radius: 10px;
  --radius-sm: 8px;
  --font: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
}

:root[data-theme='dark'] {
  color-scheme: dark;

  --bg: #0E0E10;
  --panel: #141416;
  --surface: #1A1A1D;
  --surface-2: #202024;
  --line: #2A2A2F;
  --line-strong: #3A3A41;

  --text: #ECECEF;
  --text-soft: #C6C6CC;
  --muted: #8B8B95;
  --muted-2: #6E6E78;

  --accent: #6E9CFF;
  --accent-soft: #1E2A44;
  --mine: #E9E9EE;
  --mine-text: #121214;
  --danger: #F87171;
  --ok: #4ADE80;
  --warn: #E3B341;
  --warn-soft: #2A2415;
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.55);
  --header-bg: rgba(14, 14, 16, 0.92);
}

* { box-sizing: border-box; }
html, body, #root { height: 100%; margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14.5px;
  -webkit-font-smoothing: antialiased;
  transition: background-color 0.18s ease, color 0.18s ease;
}

button { font-family: inherit; font-size: 14px; cursor: pointer; color: inherit; }
input, textarea { font-family: inherit; font-size: 14.5px; }
::selection { background: var(--accent-soft); }

/* 滚动条：细、无槽，避免打断极简观感 */
* { scrollbar-width: thin; scrollbar-color: var(--line-strong) transparent; }
*::-webkit-scrollbar { width: 8px; height: 8px; }
*::-webkit-scrollbar-thumb { background: var(--line-strong); border-radius: 4px; }
*::-webkit-scrollbar-track { background: transparent; }

.icon { flex: 0 0 auto; display: block; }

.muted { color: var(--muted); }
.tiny { font-size: 12px; line-height: 1.65; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; color: var(--text-soft); }

.center { display: flex; align-items: center; justify-content: center; height: 100%; text-align: center; }

/* ---------- 按钮 ---------- */
button.primary, button.secondary, button.ghost {
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  padding: 9px 14px;
  transition: background-color 0.15s, border-color 0.15s, opacity 0.15s;
}
button.primary { background: var(--mine); color: var(--mine-text); font-weight: 500; }
button.primary:hover { opacity: 0.9; }
button.primary:disabled { opacity: 0.38; cursor: not-allowed; }
button.secondary { background: var(--panel); border-color: var(--line); color: var(--text-soft); }
button.secondary:hover { border-color: var(--line-strong); }
button.ghost { background: transparent; color: var(--muted); padding: 8px; display: inline-flex; align-items: center; justify-content: center; }
button.ghost.only-mobile { display: none; }
button.ghost:hover { color: var(--text); }
button.wide { width: 100%; margin-top: 18px; padding: 12px; }
button.danger { color: var(--danger); border-color: var(--line); }

/* ---------- 登录 / 解锁 ---------- */
.auth-page { display: flex; align-items: center; justify-content: center; height: 100%; padding: 24px; background: var(--bg); }
.auth-card {
  width: 100%; max-width: 392px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 26px 24px 20px;
  box-shadow: none;
}
.brand { display: flex; gap: 12px; align-items: center; margin-bottom: 20px; }
.brand-mark {
  width: 40px; height: 40px; border-radius: var(--radius-sm);
  background: var(--surface-2);
  border: 1px solid var(--line);
  color: var(--text);
  display: flex; align-items: center; justify-content: center;
}
.brand h1 { font-size: 17px; margin: 0 0 4px; font-weight: 600; letter-spacing: -0.01em; }
.brand p { margin: 0; }

.tabs { display: flex; gap: 2px; background: var(--surface-2); padding: 3px; border-radius: var(--radius-sm); margin-bottom: 18px; }
.tab { flex: 1; background: transparent; border: 0; color: var(--muted); padding: 8px; border-radius: 6px; transition: 0.15s; }
.tab.active { background: var(--panel); color: var(--text); font-weight: 500; }

.field { display: block; margin-bottom: 14px; }
.field span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 6px; }
.field input {
  width: 100%; padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.field input::placeholder, .composer textarea::placeholder { color: var(--muted-2); }
.field input:focus { border-color: var(--line-strong); box-shadow: 0 0 0 3px var(--accent-soft); }
.err { color: var(--danger); font-size: 13px; margin: 8px 0; }

/* ---------- 主框架 ---------- */
.app { display: grid; grid-template-columns: 300px 1fr; height: 100%; }

.sidebar {
  background: transparent;
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
  min-height: 0;
}
.side-head { display: flex; align-items: center; gap: 10px; padding: 14px 14px 10px; }
.avatar {
  width: 34px; height: 34px; border-radius: var(--radius-sm);
  background: var(--panel); border: 1px solid var(--line); color: var(--text);
  display: flex; align-items: center; justify-content: center;
  font-weight: 600; font-size: 13px; flex: 0 0 auto;
}
.avatar.group { border-radius: var(--radius-sm); color: var(--accent); }
.who { flex: 1; min-width: 0; }
.who .name { font-weight: 600; font-size: 14.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.who .sub {
  font-size: 11px; color: var(--muted); display: flex; align-items: center; gap: 5px; margin-top: 2px;
  white-space: nowrap; overflow: hidden;
}
.who .sub > span:last-child { overflow: hidden; text-overflow: ellipsis; }
.dot { width: 6px; height: 6px; border-radius: 50%; background: var(--muted-2); display: inline-block; }
.dot.online { background: var(--ok); }
.dot.connecting { background: #D9A521; }
.sep { opacity: 0.5; }

.side-actions { display: flex; gap: 8px; padding: 0 14px 12px; }
.side-actions button { flex: 1; }

.conv-list { flex: 1; overflow-y: auto; padding: 0 6px 12px; }
.conv-item {
  display: flex; gap: 10px; align-items: center; width: 100%;
  background: transparent; border: 1px solid transparent; color: var(--text);
  padding: 9px 9px; border-radius: var(--radius-sm); text-align: left;
  transition: background-color 0.12s, border-color 0.12s;
}
.conv-item:hover { background: var(--panel); }
.conv-item.active { background: var(--panel); border-color: var(--line); }
.conv-main { flex: 1; min-width: 0; }
.conv-top { display: flex; align-items: baseline; gap: 8px; }
.conv-name { font-size: 13.5px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.conv-time { font-size: 11px; color: var(--muted-2); flex: 0 0 auto; }
.conv-sub { display: flex; align-items: center; gap: 6px; margin-top: 2px; }
.preview { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.badge {
  background: var(--accent); color: #fff; font-size: 10.5px; font-weight: 600;
  border-radius: 9px; padding: 1px 6px; flex: 0 0 auto; line-height: 1.5;
}
:root[data-theme='dark'] .badge { color: #101828; }
.empty-hint { color: var(--muted); font-size: 12px; padding: 16px 12px; line-height: 1.7; }

.main { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.chat-head {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--header-bg);
  backdrop-filter: blur(8px);
}
.chat-title { font-weight: 600; font-size: 15px; letter-spacing: -0.01em; }
.chat-sub { font-size: 11px; color: var(--muted); margin-top: 2px; display: flex; align-items: center; gap: 4px; }
.typing { color: var(--accent); }
.chat-head > div:nth-child(2) { flex: 1; min-width: 0; }

.messages { flex: 1; min-height: 0; overflow-y: auto; padding: 16px 16px 8px; display: flex; flex-direction: column; gap: 9px; }
.day-sep { align-self: center; font-size: 10.5px; color: var(--muted-2); padding: 2px 0; margin: 6px 0 2px; letter-spacing: 0.04em; }

.row { display: flex; gap: 8px; align-items: flex-end; max-width: 76%; }
.row.mine { align-self: flex-end; flex-direction: row-reverse; }
.bubble-avatar {
  width: 26px; height: 26px; border-radius: var(--radius-sm);
  background: var(--surface-2); color: var(--muted);
  font-size: 11px; font-weight: 500; display: flex; align-items: center; justify-content: center;
  flex: 0 0 auto;
}
.bubble-wrap { min-width: 0; }
.bubble-name { font-size: 11px; color: var(--muted); margin-bottom: 3px; }
.bubble {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 8px 11px;
  line-height: 1.55;
  word-break: break-word;
  white-space: pre-wrap;
}
.row.mine .bubble { background: var(--mine); color: var(--mine-text); border-color: var(--mine); }
.bubble.failed { border-color: var(--danger); }
.bubble .sys { color: var(--muted); font-size: 12.5px; }
.row.mine .bubble .sys { color: inherit; opacity: 0.75; }
.text { user-select: text; }
.meta { font-size: 10px; color: var(--muted-2); margin-top: 3px; }
.row.mine .meta { text-align: right; }

.file {
  display: flex; gap: 9px; align-items: center;
  background: transparent; border: 0; color: inherit; text-align: left;
  padding: 1px; width: 100%;
}
.row.mine .file { color: var(--mine-text); }
.file-ico { display: flex; color: currentColor; opacity: 0.7; }
.file-name { display: block; font-weight: 500; font-size: 13px; word-break: break-all; }
.file-meta { display: block; font-size: 11px; color: var(--muted); margin-top: 1px; }
.row.mine .file-meta { color: inherit; opacity: 0.65; }

.composer {
  display: flex; gap: 9px; align-items: flex-end;
  padding: 11px 14px calc(11px + env(safe-area-inset-bottom));
  border-top: 1px solid var(--line);
  background: var(--bg);
}
.attach {
  width: 38px; height: 38px; border-radius: var(--radius-sm);
  display: flex; align-items: center; justify-content: center;
  background: var(--panel); border: 1px solid var(--line); color: var(--muted);
  cursor: pointer; flex: 0 0 auto; transition: color 0.15s, border-color 0.15s;
}
.attach:hover { color: var(--text); border-color: var(--line-strong); }
.composer textarea {
  /* 一行起步，最多约 6 行或 30% 视口高，再多就在框内滚动，绝不顶满屏幕 */
  flex: 1; resize: none;
  min-height: 40px;
  max-height: min(158px, 30vh);
  overflow-y: auto;
  padding: 10px 12px;
  background: var(--surface); color: var(--text);
  border: 1px solid var(--line); border-radius: var(--radius); outline: none;
  line-height: 1.5;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.composer textarea:focus { border-color: var(--line-strong); box-shadow: 0 0 0 3px var(--accent-soft); }
button.send { height: 40px; padding: 0 16px; }

/* ---------- 弹窗 ---------- */
.overlay {
  position: fixed; inset: 0; background: rgba(14, 14, 16, 0.35);
  display: flex; align-items: center; justify-content: center; padding: 20px; z-index: 50;
  backdrop-filter: blur(2px);
}
:root[data-theme='dark'] .overlay { background: rgba(0, 0, 0, 0.6); }
.dialog {
  width: 100%; max-width: 420px;
  background: var(--panel); border: 1px solid var(--line);
  border-radius: 14px; padding: 20px;
  box-shadow: var(--shadow);
}
.dialog h3 { margin: 0 0 8px; font-size: 16px; font-weight: 600; letter-spacing: -0.01em; }
.dialog-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }

/* 三段式选择器（主题切换用） */
.seg {
  display: flex; gap: 2px; padding: 3px;
  background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius-sm);
}
.seg button {
  flex: 1; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  background: transparent; border: 0; color: var(--muted);
  padding: 8px 6px; border-radius: 6px; font-size: 12.5px; transition: 0.15s;
}
.seg button.active { background: var(--panel); color: var(--text); font-weight: 500; }
.seg button:hover:not(.active) { color: var(--text-soft); }
.seg-row { margin: 14px 0 4px; }
.seg-label { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); margin-bottom: 7px; }

.safety-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px;
  margin: 14px 0 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12.5px; color: var(--text-soft);
}
.safety-grid span {
  background: var(--surface-2); border: 1px solid var(--line); border-radius: 6px;
  padding: 6px 0; text-align: center; letter-spacing: 0.5px;
}

.kv { display: grid; grid-template-columns: 96px 1fr; gap: 8px 12px; margin: 12px 0; font-size: 12.5px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-all; }

.toast {
  position: fixed; left: 50%; bottom: calc(var(--composer-h, 62px) + 14px); transform: translateX(-50%);
  background: var(--mine); color: var(--mine-text);
  padding: 9px 14px; border-radius: var(--radius-sm); font-size: 12.5px; z-index: 60;
  box-shadow: var(--shadow);
}

.only-mobile { display: none; }

/* ---------- 手机 ---------- */
@media (max-width: 760px) {
  .app { grid-template-columns: 1fr; }
  .sidebar { border-right: 0; }
  button.ghost.only-mobile { display: inline-flex; }
  .row { max-width: 88%; }
  .messages { padding: 12px 12px 6px; }
  .chat-head { padding: 9px 12px; }
  .composer { padding: 9px 10px calc(9px + env(safe-area-inset-bottom)); }
  .dialog { max-width: 100%; }
  body { font-size: 15px; }
}

/* ---------- 信任状态告警 / 安全码 ---------- */

.trust-alert {
  display: flex; align-items: center; gap: 8px;
  width: 100%; padding: 9px 16px; border: 0; border-bottom: 1px solid var(--line);
  background: var(--warn-soft); color: var(--warn);
  font: inherit; font-size: 12.5px; text-align: left; cursor: pointer;
}
.trust-alert:hover { filter: brightness(0.97); }
.trust-alert svg { flex: 0 0 auto; }

.safety-device { border-top: 1px solid var(--line); padding: 12px 0 4px; }
.safety-device:first-of-type { border-top: 0; }
.safety-device-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.safety-device-head .mono { font-size: 11.5px; color: var(--muted); word-break: break-all; }

.pill {
  flex: 0 0 auto; font-size: 11px; padding: 2px 8px; border-radius: 999px;
  border: 1px solid var(--line-strong); color: var(--muted); background: var(--surface-2);
}
.pill.verified { color: var(--ok); border-color: color-mix(in srgb, var(--ok) 45%, var(--line)); background: transparent; }
.pill.changed { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 45%, var(--line)); background: var(--warn-soft); }

.safety-actions { display: flex; justify-content: flex-end; margin: 6px 0 10px; }
.tiny-btn { font-size: 12px; padding: 5px 10px; }

/* ---------- 漫游设备 ---------- */

.roam-list { border: 1px solid var(--line); border-radius: var(--radius-sm); overflow: hidden; margin: 8px 0; }
.roam-item { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 10px; font-size: 12.5px; }
.roam-item + .roam-item { border-top: 1px solid var(--line); }
.roam-item .mono { color: var(--muted); word-break: break-all; }
.roam-actions { display: flex; gap: 8px; margin: 8px 0; }
