:root {
  --bg: #ffffff;
  --side: #f5f5f7;
  --line: rgba(0, 0, 0, 0.08);
  --text: #1d1d1f;
  --muted: #86868b;
  --accent: #0a84ff;
  --mine: #0a84ff;
  --mine-text: #ffffff;
  --theirs: #e9e9eb;
  --theirs-text: #1d1d1f;
  --hover: rgba(0, 0, 0, 0.04);
  --selected: #0a84ff;
  --danger: #ff3b30;
  --warn-bg: #fff4e5;
  --warn-text: #8a4b00;
  --field: #ffffff;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.12);
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --side: #1c1c1e;
    --line: rgba(255, 255, 255, 0.1);
    --text: #f5f5f7;
    --muted: #8e8e93;
    --theirs: #26252a;
    --theirs-text: #f5f5f7;
    --hover: rgba(255, 255, 255, 0.06);
    --warn-bg: #3a2a10;
    --warn-text: #ffcc80;
    --field: #1c1c1e;
    --shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; cursor: pointer; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.error { color: var(--danger); min-height: 1.4em; font-size: 14px; }

.icon-btn {
  border: 0; background: transparent; width: 34px; height: 34px; border-radius: 50%;
  display: grid; place-items: center; color: var(--accent); flex: none;
}
.icon-btn:hover { background: var(--hover); }
.icon-btn svg { width: 22px; height: 22px; }

/* ---------- Login ---------- */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; background: var(--side); }
.login-card {
  width: 100%; max-width: 360px; text-align: center; background: var(--bg);
  border-radius: 22px; padding: 40px 28px 24px; box-shadow: var(--shadow);
}
.logo {
  width: 72px; height: 72px; margin: 0 auto 16px; border-radius: 20px; color: #fff;
  background: linear-gradient(160deg, #5ac8fa, #0a84ff); display: grid; place-items: center;
}
.logo svg { width: 48px; height: 48px; }
.login h1 { margin: 0 0 4px; font-size: 26px; letter-spacing: 0.02em; }
.login .muted { margin: 0 0 24px; }
.google-btn { display: flex; justify-content: center; min-height: 44px; }
.dev-login { margin-top: 20px; display: grid; gap: 8px; }
.dev-login p { margin: 0; }
.dev-login input, .dev-login button {
  height: 40px; border-radius: 10px; border: 1px solid var(--line); padding: 0 12px;
  background: var(--field); color: var(--text); font: inherit;
}
.dev-login button { background: var(--accent); color: #fff; border: 0; }
.foot { margin: 16px 0 0 !important; }

/* ---------- Layout ---------- */
.app { display: flex; height: 100dvh; }
.sidebar {
  width: 300px; flex: none; background: var(--side); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding-top: env(safe-area-inset-top);
}
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 8px 20px; }
.side-head h2 { margin: 0; font-size: 22px; }
.contacts { list-style: none; margin: 0; padding: 4px 8px; overflow-y: auto; flex: 1; }
.contact {
  display: flex; align-items: center; gap: 12px; padding: 10px; border-radius: 12px; cursor: pointer;
}
.contact:hover { background: var(--hover); }
.contact.active { background: var(--selected); color: #fff; }
.contact.active .muted { color: rgba(255, 255, 255, 0.8); }
.contact-main { flex: 1; min-width: 0; }
.contact-top { display: flex; justify-content: space-between; gap: 8px; }
.contact-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.contact-time { font-size: 12px; flex: none; }
.contact-preview { font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: var(--accent);
  color: #fff; font-size: 12px; font-weight: 600; display: grid; place-items: center;
}
.contact.active .badge { background: #fff; color: var(--accent); }
.empty-contacts { padding: 16px 20px; line-height: 1.6; }

.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex: none; object-fit: cover;
  display: grid; place-items: center; color: #fff; font-weight: 600; font-size: 16px;
}
.avatar.sm { width: 32px; height: 32px; font-size: 14px; }

.chat { flex: 1; min-width: 0; display: flex; }
.chat-empty { flex: 1; display: grid; place-items: center; }
.chat-view { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-head {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px;
  padding-top: calc(10px + env(safe-area-inset-top));
  border-bottom: 1px solid var(--line); background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: saturate(180%) blur(20px); -webkit-backdrop-filter: saturate(180%) blur(20px);
}
.chat-head .back { display: none; margin-left: -8px; }
.chat-title { display: flex; flex-direction: column; flex: 1; min-width: 0; }
.chat-title strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.e2e { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
.e2e svg { width: 11px; height: 11px; }
.conn { width: 8px; height: 8px; border-radius: 50%; background: #34c759; flex: none; }
.conn.off { background: var(--muted); }

.banner {
  background: var(--warn-bg); color: var(--warn-text); padding: 10px 16px; font-size: 13px;
  border-bottom: 1px solid var(--line); line-height: 1.5;
}
.banner .fp { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-weight: 600; }
.banner button {
  margin: 6px 8px 0 0; border: 0; border-radius: 8px; padding: 4px 12px;
  background: var(--warn-text); color: var(--warn-bg); font-size: 13px;
}

/* ---------- Messages ---------- */
.messages { flex: 1; overflow-y: auto; padding: 12px 16px 8px; display: flex; flex-direction: column; }
.messages .spacer { flex: 1; }
.time-sep { text-align: center; font-size: 12px; color: var(--muted); margin: 14px 0 6px; }
.row { display: flex; margin-top: 2px; }
.row.mine { justify-content: flex-end; }
.row.gap { margin-top: 10px; }
.bubble {
  max-width: min(75%, 520px); padding: 8px 13px; border-radius: 19px; white-space: pre-wrap;
  word-break: break-word; overflow-wrap: anywhere; font-size: 16px; line-height: 1.35;
}
.row.theirs .bubble { background: var(--theirs); color: var(--theirs-text); }
.row.mine .bubble { background: var(--mine); color: var(--mine-text); }
.row.mine.last .bubble { border-bottom-right-radius: 6px; }
.row.theirs.last .bubble { border-bottom-left-radius: 6px; }
.bubble a { color: inherit; text-decoration: underline; }
.bubble.undecryptable { font-style: italic; opacity: 0.7; }
.status { text-align: right; font-size: 11px; color: var(--muted); margin: 2px 4px 0 0; }
.status.failed { color: var(--danger); cursor: pointer; }
.unverified { font-size: 11px; color: var(--warn-text); margin: 2px 0 0 4px; }

/* ---------- Composer ---------- */
.composer {
  display: flex; align-items: flex-end; gap: 8px; padding: 8px 12px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
}
.composer textarea {
  flex: 1; resize: none; border: 1px solid var(--line); border-radius: 20px; padding: 8px 14px;
  font: inherit; font-size: 16px; line-height: 1.35; max-height: 160px; background: var(--field);
  color: var(--text); outline: none;
}
.composer textarea:focus { border-color: color-mix(in srgb, var(--accent) 50%, var(--line)); }
#send {
  width: 34px; height: 34px; border-radius: 50%; border: 0; background: var(--accent); color: #fff;
  display: grid; place-items: center; flex: none; margin-bottom: 2px; transition: opacity 0.15s;
}
#send:disabled { opacity: 0.35; cursor: default; }
#send svg { width: 20px; height: 20px; }

/* ---------- Settings ---------- */
.settings {
  width: min(480px, calc(100% - 32px)); max-height: calc(100dvh - 48px); padding: 0; border: 0;
  border-radius: 18px; background: var(--bg); color: var(--text); box-shadow: var(--shadow);
}
.settings::backdrop { background: rgba(0, 0, 0, 0.35); }
.settings header { display: flex; align-items: center; justify-content: space-between; padding: 14px 12px 6px 20px; }
.settings h3 { margin: 0; font-size: 18px; }
.settings-body { padding: 0 20px 20px; }
.settings h4 { margin: 18px 0 8px; font-size: 13px; color: var(--muted); font-weight: 600; }
.device-list { list-style: none; margin: 0; padding: 0; border: 1px solid var(--line); border-radius: 12px; }
.device-list li { display: flex; align-items: center; gap: 10px; padding: 10px 12px; }
.device-list li + li { border-top: 1px solid var(--line); }
.device-info { flex: 1; min-width: 0; }
.device-label { font-weight: 500; }
.device-fp { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; color: var(--muted); }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--hover); color: var(--muted); margin-left: 6px; }
.tag.warn { background: var(--warn-bg); color: var(--warn-text); }
.link-btn { border: 0; background: none; color: var(--danger); font-size: 13px; padding: 4px; }
.contact-block + .contact-block { margin-top: 12px; }
.contact-block .who { font-size: 14px; margin: 0 0 6px; }
.settings-actions { display: flex; gap: 8px; margin-top: 20px; flex-wrap: wrap; }
.settings-actions button { border: 0; border-radius: 10px; padding: 8px 14px; }
.secondary { background: var(--hover); }
.danger { background: color-mix(in srgb, var(--danger) 12%, transparent); color: var(--danger); }

/* ---------- Mobile ---------- */
@media (max-width: 720px) {
  .sidebar { width: 100%; border-right: 0; }
  .chat { display: none; }
  .app.in-chat .sidebar { display: none; }
  .app.in-chat .chat { display: flex; }
  .chat-head .back { display: grid; }
  .bubble { max-width: 80%; }
}
