:root {
  --bg: #0f141a;
  --bg-elev: #171e26;
  --bg-bar: #1b2530;
  --line: #24303c;
  --text: #e8edf2;
  --text-dim: #8a97a6;
  --accent: #3aa0ff;
  --accent-press: #2b7fd4;
  --out: #245a8d;
  --in: #202b36;
  --danger: #ff5a5a;
  --ok: #3ad07a;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
/* The hidden attribute must ALWAYS win, even over display:flex on .modal —
   otherwise the camera overlay covers the whole app (black screen bug). */
[hidden] { display: none !important; }
html, body {
  margin: 0; height: 100%; background: var(--bg); color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Noto Sans", sans-serif;
  font-size: 16px; overscroll-behavior: none;
}
#app { height: 100dvh; position: relative; overflow: hidden; }

.view { position: absolute; inset: 0; display: none; flex-direction: column;
  background: var(--bg); }
.view.is-active { display: flex; }

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

/* ---------- top bar ---------- */
.top-bar {
  display: flex; align-items: center; gap: 8px;
  padding: calc(var(--safe-top) + 8px) 12px 8px;
  background: var(--bg-bar); border-bottom: 1px solid var(--line);
  min-height: 52px; position: sticky; top: 0; z-index: 5;
}
.top-title { font-weight: 600; font-size: 1.05rem; flex: 1;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.icon-btn { width: 40px; height: 40px; border-radius: 50%; font-size: 1.25rem;
  display: flex; align-items: center; justify-content: center; color: var(--text); }
.icon-btn:active { background: rgba(255,255,255,.08); }

/* ---------- login ---------- */
#view-login { justify-content: center; align-items: center; padding: 24px; }
.login-card { width: 100%; max-width: 360px; }
.brand { text-align: center; margin-bottom: 28px; }
.brand-logo { width: 64px; height: 64px; border-radius: 18px; margin: 0 auto 12px;
  background: linear-gradient(135deg, var(--accent), #6f5cff);
  display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 1.4rem; }
.brand h1 { margin: 0; font-size: 1.5rem; letter-spacing: .02em; }
.brand-sub { margin: 4px 0 0; color: var(--text-dim); font-size: .9rem; }
.field { margin-bottom: 12px; }
.field label { display: block; font-size: .82rem; color: var(--text-dim); margin-bottom: 6px; }
.field input {
  width: 100%; padding: 14px; border-radius: 12px; background: var(--bg-elev);
  border: 1px solid var(--line); color: var(--text); outline: none;
}
.field input:focus { border-color: var(--accent); }
.otp-field input, #f-enroll-otp { letter-spacing: .3em; text-align: center; font-size: 1.2rem; }
.btn-primary {
  width: 100%; padding: 14px; border-radius: 12px; background: var(--accent);
  color: #fff; font-weight: 600; margin-top: 6px;
}
.btn-primary:active { background: var(--accent-press); }
.btn-primary[disabled] { opacity: .55; }
.error { color: var(--danger); font-size: .9rem; margin: 6px 0 10px; text-align: center; }
.login-notice { color: var(--text-dim); font-size: .85rem; text-align: center; margin-top: 14px; }
.enroll p { color: var(--text-dim); font-size: .88rem; line-height: 1.4; }
.qr { background: #fff; padding: 10px; border-radius: 12px; width: 200px; height: 200px;
  margin: 12px auto; display: flex; align-items: center; justify-content: center; }
.qr svg { width: 100%; height: 100%; }
.enroll-uri { display: block; text-align: center; color: var(--accent);
  font-size: .85rem; margin-bottom: 12px; text-decoration: none; }

/* ---------- chat list ---------- */
.chat-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.chat-row { display: flex; align-items: center; gap: 12px; padding: 12px 14px;
  border-bottom: 1px solid var(--line); }
.chat-row:active { background: var(--bg-elev); }
.avatar { width: 48px; height: 48px; border-radius: 50%; flex: 0 0 auto;
  display: flex; align-items: center; justify-content: center; font-weight: 600; color: #fff;
  background: #3a4b5c; text-transform: uppercase; }
.chat-main { flex: 1; min-width: 0; }
.chat-line1 { display: flex; justify-content: space-between; gap: 8px; }
.chat-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-time { color: var(--text-dim); font-size: .78rem; flex: 0 0 auto; }
.chat-line2 { display: flex; justify-content: space-between; gap: 8px; margin-top: 2px; }
.chat-preview { color: var(--text-dim); font-size: .88rem; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis; flex: 1; }
.badge { background: var(--accent); color: #fff; border-radius: 999px; font-size: .72rem;
  min-width: 20px; height: 20px; padding: 0 6px; display: flex; align-items: center;
  justify-content: center; flex: 0 0 auto; }
.empty { text-align: center; color: var(--text-dim); padding: 40px; }

/* ---------- conversation ---------- */
.messages { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  display: flex; flex-direction: column; padding: 12px; gap: 6px; }
.msg { max-width: 82%; padding: 8px 11px; border-radius: 14px; position: relative;
  word-wrap: break-word; white-space: pre-wrap; font-size: .96rem; line-height: 1.35; }
.msg.in { background: var(--in); align-self: flex-start; border-bottom-left-radius: 5px; }
.msg.out { background: var(--out); align-self: flex-end; border-bottom-right-radius: 5px; }
.msg .sender { font-size: .78rem; color: var(--accent); margin-bottom: 2px; font-weight: 600; }
.msg .meta { font-size: .68rem; color: var(--text-dim); text-align: right; margin-top: 3px; }
.msg .thumb { max-width: 220px; max-height: 220px; border-radius: 10px; display: block;
  margin: 2px 0; background: #0a0e12; }
.msg .attach { display: flex; align-items: center; gap: 8px; color: var(--text);
  padding: 6px 2px; }
.attach-ico { width: 34px; height: 34px; border-radius: 8px; background: rgba(255,255,255,.12);
  display: flex; align-items: center; justify-content: center; flex: 0 0 auto; }
.attach-note { font-size: .72rem; color: var(--text-dim); }

.composer { display: flex; align-items: center; gap: 4px; padding: 6px 8px;
  padding-bottom: calc(6px + var(--safe-bottom)); background: var(--bg-bar);
  border-top: 1px solid var(--line); }
.composer input { flex: 1; padding: 11px 14px; border-radius: 20px; background: var(--bg-elev);
  border: 1px solid var(--line); color: var(--text); outline: none; }
.composer input:focus { border-color: var(--accent); }
.icon-btn.send { color: var(--accent); }

/* ---------- camera ---------- */
.modal { position: fixed; inset: 0; background: #000; z-index: 50; display: flex; }
.camera-wrap { position: relative; width: 100%; height: 100%; }
#camera-video { width: 100%; height: 100%; object-fit: cover; background: #000; }
.camera-timer { position: absolute; top: calc(var(--safe-top) + 12px); left: 50%;
  transform: translateX(-50%); background: var(--danger); color: #fff; padding: 3px 10px;
  border-radius: 999px; font-size: .85rem; }
.camera-controls { position: absolute; left: 0; right: 0;
  bottom: calc(24px + var(--safe-bottom)); display: flex; align-items: center;
  justify-content: space-around; }
.cam-btn { color: #fff; font-size: 1.3rem; min-width: 56px; height: 44px; }
.cam-shot { width: 68px; height: 68px; border-radius: 50%; background: #fff;
  border: 4px solid rgba(255,255,255,.5); }
.cam-shot.recording { background: var(--danger); }

/* ---------- toast ---------- */
.toast { position: fixed; left: 50%; bottom: calc(80px + var(--safe-bottom));
  transform: translateX(-50%); background: #000d; color: #fff; padding: 10px 16px;
  border-radius: 10px; font-size: .88rem; z-index: 60; max-width: 88vw; text-align: center; }

.spinner { border: 2px solid var(--line); border-top-color: var(--accent); border-radius: 50%;
  width: 22px; height: 22px; animation: spin .8s linear infinite; margin: 30px auto; }
@keyframes spin { to { transform: rotate(360deg); } }
