/* app.css — Lattice. "Liquid Glass": translucent, blurred, layered panes
   over a soft aurora field. Light + dark, driven by [data-theme] on <html>. */

:root {
  color-scheme: light dark;
  --font: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Inter', system-ui, 'Segoe UI', Roboto, 'Noto Sans', 'Helvetica Neue', Arial, 'PingFang SC', 'Microsoft YaHei', sans-serif;

  /* light palette */
  --bg: #eef2f9;
  --aurora-1: rgba(99, 102, 241, 0.20);
  --aurora-2: rgba(56, 189, 248, 0.18);
  --aurora-3: rgba(236, 72, 153, 0.12);
  --glass: rgba(255, 255, 255, 0.55);
  --glass-strong: rgba(255, 255, 255, 0.72);
  --glass-border: rgba(255, 255, 255, 0.65);
  --hairline: rgba(15, 23, 42, 0.08);
  --text: #0f172a;
  --text-dim: #59647c;
  --accent: #4f46e5;
  --accent-2: #6366f1;
  --accent-text: #ffffff;
  --bubble-in: rgba(255, 255, 255, 0.78);
  --bubble-out: linear-gradient(135deg, #6366f1, #4f46e5);
  --bubble-out-text: #ffffff;
  --danger: #e11d48;
  --ok: #059669;
  --shadow-lg: 0 20px 60px rgba(30, 41, 90, 0.18);
  --shadow-sm: 0 6px 20px rgba(30, 41, 90, 0.10);
  --radius: 22px;
  --radius-sm: 14px;
  --blur: 30px;
}

:root[data-theme='dark'] {
  color-scheme: dark;
  --bg: #070a14;
  --aurora-1: rgba(99, 102, 241, 0.26);
  --aurora-2: rgba(14, 165, 233, 0.20);
  --aurora-3: rgba(217, 70, 239, 0.16);
  --glass: rgba(22, 28, 45, 0.55);
  --glass-strong: rgba(26, 33, 54, 0.78);
  --glass-border: rgba(148, 163, 184, 0.18);
  --hairline: rgba(148, 163, 184, 0.14);
  --text: #eaf0ff;
  --text-dim: #9aa8c4;
  --accent: #818cf8;
  --accent-2: #a5b4fc;
  --accent-text: #0b1020;
  --bubble-in: rgba(38, 46, 71, 0.82);
  --bubble-out: linear-gradient(135deg, #6366f1, #7c3aed);
  --bubble-out-text: #ffffff;
  --danger: #fb7185;
  --ok: #34d399;
  --shadow-lg: 0 24px 70px rgba(0, 0, 0, 0.5);
  --shadow-sm: 0 8px 24px rgba(0, 0, 0, 0.4);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
/* aurora field */
body::before {
  content: '';
  position: fixed; inset: -20%;
  background:
    radial-gradient(40rem 40rem at 12% -5%, var(--aurora-1), transparent 60%),
    radial-gradient(38rem 38rem at 100% 12%, var(--aurora-2), transparent 60%),
    radial-gradient(46rem 46rem at 50% 115%, var(--aurora-3), transparent 62%);
  filter: saturate(120%);
  z-index: -1;
  animation: drift 24s ease-in-out infinite alternate;
}
@keyframes drift {
  from { transform: translate3d(-2%, -1%, 0) scale(1.02); }
  to   { transform: translate3d(2%, 2%, 0) scale(1.06); }
}

button, input, textarea { font: inherit; color: inherit; }
button { border: none; background: none; cursor: pointer; }
input, textarea { border: none; outline: none; background: none; }

.app {
  height: 100dvh;
  display: flex;
  max-width: 1100px;
  margin: 0 auto;
}

/* ---------- glass primitives ---------- */
.glass {
  background: var(--glass);
  backdrop-filter: blur(var(--blur)) saturate(160%);
  -webkit-backdrop-filter: blur(var(--blur)) saturate(160%);
  border: 1px solid var(--glass-border);
}
.hairline { border-bottom: 1px solid var(--hairline); }

/* ---------- sidebar (chat list) ---------- */
.pane {
  display: flex; flex-direction: column;
  height: 100dvh; min-height: 0;
}
.sidebar {
  width: 360px; flex: 0 0 360px;
  background: var(--glass);
  backdrop-filter: blur(var(--blur)) saturate(160%);
  -webkit-backdrop-filter: blur(var(--blur)) saturate(160%);
  border-right: 1px solid var(--glass-border);
}
.topbar {
  display: flex; align-items: center; gap: 12px;
  padding: max(14px, env(safe-area-inset-top)) 16px 12px;
  border-bottom: 1px solid var(--hairline);
}
.topbar h1 { font-size: 1.25rem; font-weight: 800; letter-spacing: -0.02em; margin: 0; flex: 1; }
.logo {
  width: 34px; height: 34px; border-radius: 11px;
  background: linear-gradient(135deg, var(--accent-2), var(--accent));
  display: grid; place-items: center; color: #fff; font-weight: 900;
  box-shadow: var(--shadow-sm);
}
.icon-btn {
  width: 40px; height: 40px; border-radius: 12px;
  display: grid; place-items: center; color: var(--text-dim);
  transition: background .15s, color .15s, transform .1s;
}
.icon-btn:hover { background: var(--hairline); color: var(--text); }
.icon-btn:active { transform: scale(.94); }
.icon-btn svg { width: 22px; height: 22px; }

.search {
  margin: 10px 14px; padding: 10px 14px; border-radius: 13px;
  background: var(--hairline); display: flex; align-items: center; gap: 8px;
}
.search svg { width: 18px; height: 18px; color: var(--text-dim); flex: 0 0 auto; }
.search input { flex: 1; font-size: .95rem; }

.list { flex: 1; overflow-y: auto; overflow-x: hidden; padding: 4px 8px 90px; }
.list::-webkit-scrollbar { width: 0; }

.row {
  display: flex; align-items: center; gap: 12px;
  padding: 11px 12px; border-radius: 16px; cursor: pointer;
  transition: background .15s;
}
.row:hover { background: var(--hairline); }
.row.active { background: var(--glass-strong); box-shadow: var(--shadow-sm); }
.avatar {
  width: 50px; height: 50px; border-radius: 50%; flex: 0 0 auto;
  display: grid; place-items: center; font-weight: 700; font-size: 1.1rem;
  color: #fff; letter-spacing: -.02em;
}
.avatar.group { border-radius: 30%; }
.row-main { flex: 1; min-width: 0; }
.row-top { display: flex; align-items: baseline; gap: 8px; }
.row-name { font-weight: 650; font-size: .98rem; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-time { font-size: .72rem; color: var(--text-dim); flex: 0 0 auto; }
.row-sub { display: flex; align-items: center; gap: 6px; }
.row-last { font-size: .85rem; color: var(--text-dim); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.badge {
  min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: var(--accent); color: var(--accent-text);
  font-size: .72rem; font-weight: 700; display: grid; place-items: center;
}

.fab {
  position: absolute; bottom: calc(20px + env(safe-area-inset-bottom)); left: 296px;
  width: 56px; height: 56px; border-radius: 20px;
  background: linear-gradient(135deg, var(--accent-2), var(--accent));
  color: #fff; display: grid; place-items: center; box-shadow: var(--shadow-lg);
  transition: transform .12s;
}
.fab:active { transform: scale(.92); }
.fab svg { width: 26px; height: 26px; }

/* ---------- conversation ---------- */
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; height: 100dvh; }
.chat-head {
  display: flex; align-items: center; gap: 12px;
  padding: max(12px, env(safe-area-inset-top)) 16px 12px;
  background: var(--glass); backdrop-filter: blur(var(--blur)) saturate(160%);
  -webkit-backdrop-filter: blur(var(--blur)) saturate(160%);
  border-bottom: 1px solid var(--hairline);
}
.chat-head .back { display: none; }
.chat-title { flex: 1; min-width: 0; }
.chat-title .name { font-weight: 700; font-size: 1.02rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-title .status { font-size: .76rem; color: var(--text-dim); display: flex; align-items: center; gap: 5px; }
.verify-pill {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 4px 10px; border-radius: 999px; font-size: .74rem; font-weight: 650;
  background: var(--hairline); color: var(--text-dim); cursor: pointer;
}
.verify-pill.ok { color: var(--ok); background: color-mix(in srgb, var(--ok) 14%, transparent); }
.verify-pill svg { width: 13px; height: 13px; }

.thread { flex: 1; overflow-y: auto; padding: 18px 16px 8px; display: flex; flex-direction: column; gap: 3px; }
.thread::-webkit-scrollbar { width: 0; }
.daysep { align-self: center; margin: 12px 0; padding: 4px 12px; border-radius: 999px;
  background: var(--glass); font-size: .72rem; color: var(--text-dim); font-weight: 600;
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); }

.msg { max-width: min(78%, 560px); padding: 9px 13px; border-radius: 18px; font-size: .95rem; line-height: 1.4;
  position: relative; word-wrap: break-word; overflow-wrap: anywhere; animation: pop .18s ease; }
@keyframes pop { from { opacity: 0; transform: translateY(6px) scale(.98); } }
.msg.in { align-self: flex-start; background: var(--bubble-in); border-bottom-left-radius: 6px;
  backdrop-filter: blur(12px); -webkit-backdrop-filter: blur(12px); box-shadow: var(--shadow-sm); }
.msg.out { align-self: flex-end; background: var(--bubble-out); color: var(--bubble-out-text); border-bottom-right-radius: 6px; box-shadow: var(--shadow-sm); }
.msg .sender { font-size: .74rem; font-weight: 700; color: var(--accent-2); margin-bottom: 2px; }
.msg .meta { font-size: .66rem; opacity: .7; margin-top: 3px; text-align: right; }
.msg.grouped { margin-top: -1px; }

.file-chip { display: flex; align-items: center; gap: 10px; cursor: pointer; }
.file-chip .fi { width: 40px; height: 40px; border-radius: 11px; background: var(--hairline);
  display: grid; place-items: center; flex: 0 0 auto; }
.file-chip .fi svg { width: 20px; height: 20px; }
.file-chip .fname { font-weight: 600; font-size: .9rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-chip .fsize { font-size: .74rem; opacity: .7; }
.msg.out .file-chip .fi { background: rgba(255,255,255,.2); }
.msg img.photo { max-width: 100%; border-radius: 12px; display: block; }

.composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 10px 12px calc(10px + env(safe-area-inset-bottom));
  background: var(--glass); backdrop-filter: blur(var(--blur)) saturate(160%);
  -webkit-backdrop-filter: blur(var(--blur)) saturate(160%);
  border-top: 1px solid var(--hairline);
}
.composer .input-wrap { flex: 1; background: var(--hairline); border-radius: 22px; padding: 9px 15px; display: flex; }
.composer textarea { flex: 1; resize: none; max-height: 130px; font-size: .96rem; line-height: 1.35; }
.send-btn {
  width: 46px; height: 46px; border-radius: 50%; flex: 0 0 auto;
  background: linear-gradient(135deg, var(--accent-2), var(--accent));
  color: #fff; display: grid; place-items: center; transition: transform .1s, opacity .15s;
}
.send-btn:active { transform: scale(.9); }
.send-btn:disabled { opacity: .4; }
.send-btn svg { width: 22px; height: 22px; }

.empty-chat { flex: 1; display: grid; place-items: center; text-align: center; padding: 40px; color: var(--text-dim); }
.empty-chat .big { width: 76px; height: 76px; border-radius: 26px; margin: 0 auto 16px;
  background: var(--glass); display: grid; place-items: center; box-shadow: var(--shadow-sm);
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px); }
.empty-chat .big svg { width: 38px; height: 38px; color: var(--accent); }

/* ---------- screens (onboarding / settings) ---------- */
/* Full-screen views (onboarding, settings).
   These had NO background, so the shell rendered straight through: two stacked
   headers, chat rows behind the settings cards, and the compose button showing
   through the panel. It was never a stacking-order problem — the layer was
   simply transparent. The aurora is repeated here because it lives on
   body::before at z-index -1, which an opaque layer necessarily hides. */
.screen {
  position: fixed; inset: 0; display: flex; flex-direction: column; overflow-y: auto;
  overscroll-behavior: contain;             /* don't scroll-chain to the shell */
  padding: max(24px, env(safe-area-inset-top)) max(22px, env(safe-area-inset-right))
           calc(24px + env(safe-area-inset-bottom)) max(22px, env(safe-area-inset-left));
  z-index: 60;                              /* above sheets (30) and the FAB */
  background-color: var(--bg);
  background-image:
    radial-gradient(40rem 40rem at 12% -5%, var(--aurora-1), transparent 60%),
    radial-gradient(38rem 38rem at 100% 12%, var(--aurora-2), transparent 60%),
    radial-gradient(46rem 46rem at 50% 115%, var(--aurora-3), transparent 62%);
  background-attachment: fixed;
}
.screen.center { justify-content: center; align-items: center; }
/* On a wide screen (iPad, desktop) a settings row stretched the full window,
   leaving controls marooned at the far right. Keep the column readable. */
.screen > .settings-group, .screen > .topbar, .screen > .btn { width: min(680px, 100%); align-self: center; }
.card { width: min(440px, 100%); border-radius: var(--radius); padding: 28px 24px; box-shadow: var(--shadow-lg);
  background: var(--glass-strong); backdrop-filter: blur(var(--blur)) saturate(160%);
  -webkit-backdrop-filter: blur(var(--blur)) saturate(160%); border: 1px solid var(--glass-border); }
.brand { text-align: center; margin-bottom: 22px; }
.brand .logo { width: 60px; height: 60px; border-radius: 20px; margin: 0 auto 14px; font-size: 1.6rem; }
.brand h2 { margin: 0 0 6px; font-size: 1.5rem; font-weight: 800; letter-spacing: -.02em; }
.brand p { margin: 0; color: var(--text-dim); font-size: .92rem; line-height: 1.5; }

.field { margin-bottom: 16px; }
.field label { display: block; font-size: .82rem; font-weight: 650; margin-bottom: 6px; color: var(--text-dim); }
.field .control { background: var(--hairline); border-radius: var(--radius-sm); padding: 13px 15px;
  border: 1px solid transparent; transition: border-color .15s, background .15s; }
.field .control:focus-within { border-color: var(--accent); background: var(--glass); }
.field input, .field textarea { width: 100%; font-size: .98rem; }

.btn { display: flex; align-items: center; justify-content: center; gap: 8px;
  width: 100%; padding: 14px; border-radius: var(--radius-sm); font-weight: 700; font-size: 1rem;
  transition: transform .1s, opacity .15s, background .15s; }
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .55; }
.btn.primary { background: linear-gradient(135deg, var(--accent-2), var(--accent)); color: var(--accent-text); box-shadow: var(--shadow-sm); }
.btn.ghost { background: var(--hairline); color: var(--text); }
.btn.danger { background: color-mix(in srgb, var(--danger) 16%, transparent); color: var(--danger); }
.btn + .btn { margin-top: 10px; }

.spinner { width: 18px; height: 18px; border: 2px solid currentColor; border-right-color: transparent;
  border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.err { color: var(--danger); font-size: .85rem; margin: 10px 0 0; text-align: center; min-height: 1.2em; }

.warn-box { background: color-mix(in srgb, var(--danger) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--danger) 30%, transparent);
  border-radius: var(--radius-sm); padding: 14px 16px; font-size: .88rem; line-height: 1.55; margin-bottom: 18px; }
.warn-box b { display: block; margin-bottom: 5px; }

/* settings rows */
.settings-group { border-radius: var(--radius); overflow: hidden; margin-bottom: 18px; box-shadow: var(--shadow-sm);
  background: var(--glass-strong); backdrop-filter: blur(var(--blur)); -webkit-backdrop-filter: blur(var(--blur));
  border: 1px solid var(--glass-border); }
.settings-group h3 { margin: 0; padding: 14px 18px 6px; font-size: .78rem; text-transform: uppercase;
  letter-spacing: .06em; color: var(--text-dim); }
.setting { display: flex; align-items: center; gap: 12px; padding: 14px 18px; border-top: 1px solid var(--hairline); }
.setting:first-of-type { border-top: none; }
.setting .lbl { flex: 1; font-size: .95rem; }
.setting .val { color: var(--text-dim); font-size: .9rem; }
.seg { display: flex; background: var(--hairline); border-radius: 11px; padding: 3px; gap: 2px; }
.seg button { padding: 7px 13px; border-radius: 8px; font-size: .84rem; font-weight: 600; color: var(--text-dim); transition: .15s; }
.seg button.on { background: var(--glass-strong); color: var(--text); box-shadow: var(--shadow-sm); }
select.native { background: var(--hairline); border-radius: 10px; padding: 8px 12px; font-weight: 600; }

.mono { font-family: ui-monospace, 'SF Mono', Menlo, monospace; font-size: .8rem; word-break: break-all; color: var(--text-dim); }

/* verification emoji */
.sas { display: flex; justify-content: center; gap: 10px; margin: 20px 0; flex-wrap: wrap; }
.sas span { font-size: 2.2rem; width: 56px; height: 56px; border-radius: 16px; display: grid; place-items: center;
  background: var(--hairline); }

/* modal / sheet */
.sheet-bg { position: fixed; inset: 0; background: rgba(6,10,20,.4); backdrop-filter: blur(3px);
  z-index: 30; display: flex; align-items: flex-end; justify-content: center; animation: fade .2s; }
@keyframes fade { from { opacity: 0; } }
.sheet { width: min(480px, 100%); max-height: 84dvh; border-radius: 26px 26px 0 0; overflow-y: auto;
  background: var(--glass-strong); backdrop-filter: blur(var(--blur)) saturate(160%);
  -webkit-backdrop-filter: blur(var(--blur)) saturate(160%); border: 1px solid var(--glass-border);
  padding: 10px 20px calc(24px + env(safe-area-inset-bottom)); box-shadow: var(--shadow-lg); animation: rise .28s cubic-bezier(.2,.9,.3,1); }
@keyframes rise { from { transform: translateY(100%); } }
.sheet .grip { width: 40px; height: 5px; border-radius: 3px; background: var(--hairline); margin: 6px auto 14px; }
.sheet h3 { margin: 0 0 4px; font-size: 1.2rem; font-weight: 800; }
.sheet p.sub { margin: 0 0 16px; color: var(--text-dim); font-size: .9rem; line-height: 1.5; }
.pick { display: flex; align-items: center; gap: 12px; padding: 11px 8px; border-radius: 14px; cursor: pointer; }
.pick:hover { background: var(--hairline); }
.pick .check { width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--hairline); flex: 0 0 auto; display: grid; place-items: center; }
.pick.sel .check { background: var(--accent); border-color: var(--accent); color: #fff; }
.pick .check svg { width: 14px; height: 14px; opacity: 0; }
.pick.sel .check svg { opacity: 1; }

/* toast */
.toast { position: fixed; left: 50%; bottom: calc(30px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--glass-strong); backdrop-filter: blur(20px); -webkit-backdrop-filter: blur(20px);
  border: 1px solid var(--glass-border); border-radius: 14px; padding: 12px 20px; font-size: .9rem; font-weight: 600;
  box-shadow: var(--shadow-lg); z-index: 70; animation: rise .25s; max-width: 90%; }

.hidden { display: none !important; }

/* ---------- responsive: single-column on phones ---------- */
@media (max-width: 820px) {
  .sidebar { width: 100%; flex: 1 0 100%; }
  .fab { left: auto; right: calc(20px + env(safe-area-inset-right)); }
  .app.in-chat .sidebar { display: none; }
  .app:not(.in-chat) .chat { display: none; }
  .chat { position: fixed; inset: 0; }
  .chat-head .back { display: grid; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ---------- states added by the audit fixes ---------- */

/* key-change warning on the verification pill */
.verify-pill.warn {
  color: var(--danger);
  background: color-mix(in srgb, var(--danger) 16%, transparent);
  font-weight: 700;
}

/* connection badge in the sidebar header */
.conn-badge {
  font-size: .72rem; font-weight: 650; color: var(--text-dim);
  padding: 4px 10px; border-radius: 999px; background: var(--hairline);
  white-space: nowrap;
}

/* unsent / failed outgoing messages */
.msg.failed { outline: 1px solid color-mix(in srgb, var(--danger) 50%, transparent); }
.msg.out .meta { opacity: .85; }

/* tap-to-load images: the placeholder is a normal file chip until decrypted */
.photo-holder { min-width: 180px; }
.photo-holder .file-chip { width: 100%; text-align: left; }
.photo-holder img.photo { animation: pop .2s ease; }
