:root {
  color-scheme: dark;
  --bg: #0b0d10;
  --bg-elev: #12161c;
  --bg-soft: #1a1f26;
  --line: #2a323d;
  --text: #e8eef6;
  --muted: #8b97a8;
  --accent: #60a5fa;
  --accent-2: #6ee7b7;
  --danger: #f87171;
  --me: #1d4ed8;
  --them: #1f2937;
  --radius: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; background: var(--bg); color: var(--text); }
body { -webkit-tap-highlight-color: transparent; }
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

#app {
  min-height: 100%;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

.view {
  flex: 1;
  display: none;
  flex-direction: column;
  min-height: 100dvh;
}
.view.active { display: flex; }

/* Auth */
#view-auth {
  align-items: center;
  justify-content: center;
  padding: calc(24px + var(--safe-t)) 16px calc(24px + var(--safe-b));
  background:
    radial-gradient(1000px 500px at 10% -10%, rgba(96,165,250,.15), transparent 50%),
    radial-gradient(800px 400px at 100% 0%, rgba(110,231,183,.1), transparent 45%),
    var(--bg);
}
.auth-card {
  width: min(440px, 100%);
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 28px 22px;
  box-shadow: 0 20px 60px rgba(0,0,0,.35);
}
.logo {
  font-weight: 700;
  letter-spacing: .04em;
  font-size: 1.1rem;
  background: linear-gradient(90deg, var(--accent-2), var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  margin-bottom: 4px;
}
.tagline { color: var(--muted); margin: 0 0 22px; font-size: .92rem; line-height: 1.4; }
.auth-card h1 { margin: 0 0 8px; font-size: 1.45rem; }
.muted { color: var(--muted); line-height: 1.45; }
.muted.tiny { font-size: .8rem; }
.passphrase {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin: 14px 0;
  padding: 12px;
  background: var(--bg-soft);
  border: 1px dashed var(--line);
  border-radius: 12px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: .95rem;
}
.passphrase span {
  padding: 8px 10px;
  background: var(--bg);
  border-radius: 8px;
  text-align: center;
}
.passphrase.small { grid-template-columns: 1fr 1fr 1fr; font-size: .8rem; }
.check {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  margin: 12px 0 16px;
  color: var(--muted);
  font-size: .9rem;
}
.check input { margin-top: 3px; }
.input {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px 14px;
  outline: none;
  margin: 0 0 10px;
}
.input:focus { border-color: var(--accent); }
.input.area { resize: vertical; min-height: 72px; }
.input.mono { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: .82rem; }
.input.grow { flex: 1; margin: 0; }
.btn {
  border: 1px solid var(--line);
  background: var(--bg-soft);
  color: var(--text);
  border-radius: 12px;
  padding: 11px 16px;
  font-weight: 600;
  transition: transform .08s ease, opacity .15s ease, background .15s ease;
}
.btn:hover { background: #222933; }
.btn:active { transform: scale(.98); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn.primary {
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  border-color: transparent;
  width: 100%;
  margin-bottom: 8px;
}
.btn.primary:hover { filter: brightness(1.06); }
.btn.ghost {
  background: transparent;
  width: 100%;
  color: var(--muted);
  border-color: transparent;
}
.btn.danger {
  background: rgba(248,113,113,.12);
  border-color: rgba(248,113,113,.45);
  color: #fecaca;
  width: 100%;
}
.btn.small { width: auto; padding: 8px 12px; font-size: .85rem; margin-bottom: 8px; }
.error { color: var(--danger); font-size: .88rem; margin-top: 8px; }

/* Main */
#view-main {
  background: var(--bg);
  position: relative;
  display: flex;
  flex-direction: column;
}
.panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-height: 100dvh;
}
.panel[hidden] { display: none !important; }
.logo.sm {
  font-size: 1.15rem;
  margin: 0;
}
.brand-block { min-width: 0; }
.back-btn { flex-shrink: 0; }
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: calc(10px + var(--safe-t)) 12px 10px;
  border-bottom: 1px solid var(--line);
  background: rgba(11,13,16,.85);
  backdrop-filter: blur(10px);
  position: sticky;
  top: 0;
  z-index: 5;
}
.inbox-topbar { justify-content: space-between; }
.inbox-topbar .top-actions { gap: 8px; }
.inbox-topbar .icon-btn {
  width: auto;
  min-width: 44px;
  height: 44px;
  padding: 0 12px;
  border: 1px solid var(--line);
  background: var(--bg-soft);
  font-weight: 700;
  font-size: .95rem;
  gap: 6px;
}
.inbox-topbar .icon-btn.primary-action {
  background: linear-gradient(135deg, #3b82f6, #2563eb);
  border-color: transparent;
  color: #fff;
}
.peer-meta { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
.peer-title { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.peer-sub { color: var(--muted); font-size: .78rem; }
.dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: #4b5563; flex-shrink: 0;
  box-shadow: 0 0 0 3px rgba(75,85,99,.2);
}
.dot.online { background: var(--accent-2); box-shadow: 0 0 0 3px rgba(110,231,183,.18); }
.dot.connecting { background: #fbbf24; box-shadow: 0 0 0 3px rgba(251,191,36,.18); }
.top-actions { display: flex; gap: 4px; flex-shrink: 0; align-items: center; }
.icon-btn {
  width: 40px; height: 40px;
  border-radius: 12px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.icon-btn:hover { background: var(--bg-soft); }
.icon-btn:disabled { opacity: .35; }
.icon-btn.active { color: var(--accent-2); background: rgba(110,231,183,.1); }

/* Inbox list */
.inbox-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 10px calc(16px + var(--safe-b));
}
.inbox-empty {
  margin: 48px auto;
  max-width: 300px;
  text-align: center;
  color: var(--muted);
  line-height: 1.5;
  font-size: .95rem;
}
.inbox-empty strong { color: var(--text); display: block; margin-bottom: 6px; font-size: 1.05rem; }
.inbox-item {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  text-align: left;
  border: 1px solid var(--line);
  background: var(--bg-elev);
  color: var(--text);
  border-radius: 14px;
  padding: 12px;
  margin-bottom: 8px;
  cursor: pointer;
  transition: background .12s ease, border-color .12s ease;
}
.inbox-item:hover { background: var(--bg-soft); border-color: #3a4554; }
.inbox-item.active { border-color: rgba(96,165,250,.45); }
.inbox-item.unread { border-color: rgba(110,231,183,.35); }
.inbox-avatar {
  width: 44px;
  height: 44px;
  border-radius: 14px;
  background: linear-gradient(135deg, #1d4ed8, #0f766e);
  display: grid;
  place-items: center;
  font-weight: 700;
  letter-spacing: .02em;
  flex-shrink: 0;
}
.inbox-body { flex: 1; min-width: 0; }
.inbox-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
}
.inbox-name {
  font-weight: 650;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.inbox-time {
  color: var(--muted);
  font-size: .72rem;
  flex-shrink: 0;
}
.inbox-preview {
  color: var(--muted);
  font-size: .86rem;
  margin-top: 3px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.inbox-item.unread .inbox-preview { color: var(--text); font-weight: 500; }
.inbox-badge {
  min-width: 22px;
  height: 22px;
  padding: 0 6px;
  border-radius: 999px;
  background: var(--accent-2);
  color: #042f2e;
  font-size: .72rem;
  font-weight: 700;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 12px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.msg {
  max-width: min(78%, 520px);
  padding: 10px 12px;
  border-radius: 14px;
  line-height: 1.4;
  word-wrap: break-word;
  white-space: pre-wrap;
  font-size: .95rem;
  position: relative;
}
.msg.me {
  align-self: flex-end;
  background: var(--me);
  border-bottom-right-radius: 4px;
}
.msg.them {
  align-self: flex-start;
  background: var(--them);
  border: 1px solid var(--line);
  border-bottom-left-radius: 4px;
}
.msg .time {
  display: block;
  margin-top: 4px;
  font-size: .7rem;
  opacity: .65;
}
.msg .msg-actions {
  display: flex;
  gap: 6px;
  margin-top: 6px;
}
.msg .msg-actions button {
  border: 0;
  background: rgba(0,0,0,.18);
  color: inherit;
  font-size: .72rem;
  font-weight: 600;
  padding: 4px 8px;
  border-radius: 999px;
  opacity: .85;
}
.msg.me .msg-actions button { background: rgba(255,255,255,.16); }
.msg-quote {
  display: block;
  margin: 0 0 8px;
  padding: 6px 8px;
  border-left: 3px solid var(--accent-2);
  background: rgba(0,0,0,.18);
  border-radius: 0 8px 8px 0;
  font-size: .8rem;
  opacity: .95;
  cursor: pointer;
}
.msg.me .msg-quote { border-left-color: #93c5fd; background: rgba(0,0,0,.15); }
.msg-quote .q-name { font-weight: 700; display: block; margin-bottom: 2px; opacity: .9; }
.msg-quote .q-text {
  opacity: .8;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.msg-body { white-space: pre-wrap; }
.msg-media {
  display: block;
  margin: 0 0 8px;
  max-width: min(100%, 280px);
  border-radius: 10px;
  overflow: hidden;
  background: rgba(0,0,0,.25);
}
.msg-media img,
.msg-media video {
  display: block;
  width: 100%;
  max-height: 280px;
  object-fit: contain;
  cursor: zoom-in;
  background: #0a0a0a;
}
.msg-media audio {
  width: 100%;
  min-width: 200px;
}
.msg-file {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  margin: 0 0 8px;
  border-radius: 10px;
  background: rgba(0,0,0,.2);
  text-decoration: none;
  color: inherit;
  max-width: 280px;
}
.msg-file .file-icon {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  background: rgba(96,165,250,.2);
  font-size: .75rem;
  font-weight: 700;
  flex-shrink: 0;
}
.msg-file .file-meta { min-width: 0; }
.msg-file .file-name {
  font-weight: 600;
  font-size: .88rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.msg-file .file-size { font-size: .72rem; opacity: .7; }
.day-sep {
  align-self: center;
  color: var(--muted);
  font-size: .75rem;
  margin: 8px 0;
  padding: 4px 10px;
  background: var(--bg-soft);
  border-radius: 999px;
}
.empty-hint {
  margin: auto;
  text-align: center;
  color: var(--muted);
  max-width: 280px;
  line-height: 1.5;
  font-size: .92rem;
}

.typing-bar {
  padding: 0 16px 4px;
  color: var(--muted);
  font-size: .8rem;
  font-style: italic;
}

.reply-bar,
.attach-preview {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-top: 1px solid var(--line);
  background: var(--bg-soft);
}
.reply-bar-body,
.attach-preview-body {
  flex: 1;
  min-width: 0;
  border-left: 3px solid var(--accent);
  padding-left: 10px;
}
.reply-bar-label {
  font-size: .75rem;
  color: var(--accent-2);
  font-weight: 650;
}
.reply-bar-text {
  font-size: .85rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.attach-preview-body {
  display: flex;
  align-items: center;
  gap: 10px;
  border-left-color: var(--accent-2);
}
.attach-preview-body img,
.attach-preview-body video {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 8px;
  background: #000;
}
.attach-preview-body .ap-meta { min-width: 0; }
.attach-preview-body .ap-name {
  font-weight: 600;
  font-size: .9rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.attach-preview-body .ap-size { font-size: .75rem; color: var(--muted); }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 12px 12px calc(14px + var(--safe-b));
  border-top: 1px solid var(--line);
  background: var(--bg-elev);
}
.composer .attach-btn {
  width: 48px;
  height: 52px;
  border-radius: 14px;
  border: 1px solid var(--line);
  background: var(--bg-soft);
  flex-shrink: 0;
}
.composer .attach-btn:disabled { opacity: .4; }
.composer .msg-box {
  flex: 1;
  margin: 0;
  min-height: 52px;
  max-height: 160px;
  padding: 14px 16px;
  font-size: 1.05rem;
  line-height: 1.4;
  resize: none;
  overflow-y: auto;
  border-radius: 16px;
}
.composer .send-btn {
  width: auto;
  min-width: 88px;
  margin: 0;
  min-height: 52px;
  padding: 14px 18px;
  font-size: 1rem;
  border-radius: 16px;
  flex-shrink: 0;
}

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 80;
  background: rgba(0,0,0,.9);
  display: grid;
  place-items: center;
  padding: 24px;
}
.lightbox img {
  max-width: min(96vw, 1100px);
  max-height: 90vh;
  object-fit: contain;
  border-radius: 8px;
}
.lightbox-close {
  position: absolute;
  top: calc(12px + var(--safe-t));
  right: 12px;
  width: 44px;
  height: 44px;
  border-radius: 999px;
  border: 0;
  background: rgba(255,255,255,.12);
  color: #fff;
  font-size: 1.1rem;
}

/* Call */
.call-stage {
  position: relative;
  background: #000;
  aspect-ratio: 16 / 10;
  max-height: 42vh;
  overflow: hidden;
}
#remote-video, #local-video {
  width: 100%;
  height: 100%;
  object-fit: cover;
  background: #000;
}
#local-video {
  position: absolute;
  width: 28%;
  max-width: 160px;
  height: auto;
  aspect-ratio: 3/4;
  right: 10px;
  bottom: 10px;
  border-radius: 12px;
  border: 1px solid rgba(255,255,255,.25);
  object-fit: cover;
  z-index: 2;
}
.call-stage.audio-only #remote-video,
.call-stage.audio-only #local-video { display: none; }
.call-stage.audio-only {
  aspect-ratio: auto;
  min-height: 120px;
  display: grid;
  place-items: center;
}
.call-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  padding: 12px;
  background: linear-gradient(transparent 40%, rgba(0,0,0,.55));
  pointer-events: none;
}
.call-overlay .call-controls { pointer-events: auto; display: flex; gap: 10px; }
.call-label { color: #fff; margin-bottom: 10px; font-size: .9rem; text-shadow: 0 1px 4px #000; }
.round-btn {
  min-width: 64px;
  height: 44px;
  border-radius: 999px;
  border: 0;
  background: rgba(255,255,255,.14);
  color: #fff;
  backdrop-filter: blur(6px);
  font-weight: 600;
  padding: 0 14px;
}
.round-btn.danger { background: #dc2626; }

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 100;
  overflow: auto;
}
/* When JS sets hidden=false, never let [hidden] residual styles win */
.modal:not([hidden]) {
  display: grid !important;
}
.modal[hidden] {
  display: none !important;
}
.modal-card {
  width: min(440px, 100%);
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 20px;
  margin: auto;
}
.modal-card.settings { width: min(480px, 100%); max-height: min(90dvh, 900px); overflow: auto; }
.modal-card h2 { margin: 0 0 8px; }
.modal-card h3 { margin: 20px 0 8px; font-size: .95rem; color: var(--muted); font-weight: 650; letter-spacing: .02em; text-transform: uppercase; }
.danger-title { color: var(--danger) !important; }
.qr-box {
  display: grid;
  place-items: center;
  background: #fff;
  border-radius: 12px;
  padding: 12px;
  margin: 12px 0;
  min-height: 200px;
}
.qr-box canvas, .qr-box img { max-width: 220px; height: auto; }
.row { display: flex; gap: 8px; }
.row .btn { flex: 1; width: auto; }
.row.wrap { flex-wrap: wrap; }
.divider {
  text-align: center;
  color: var(--muted);
  font-size: .8rem;
  margin: 16px 0 10px;
  position: relative;
}
.field { display: block; margin-bottom: 10px; }
.field > span { display: block; color: var(--muted); font-size: .8rem; margin-bottom: 6px; }
.contact-list { list-style: none; margin: 0; padding: 0; }
.contact-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--bg-soft);
  border-radius: 10px;
  margin-bottom: 6px;
  font-size: .9rem;
}
.contact-list .cid { color: var(--muted); font-family: ui-monospace, monospace; font-size: .72rem; }
.contact-list button {
  border: 0;
  background: transparent;
  color: var(--accent);
  font-weight: 600;
  padding: 4px 8px;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(24px + var(--safe-b));
  transform: translateX(-50%);
  background: #111827;
  border: 1px solid var(--line);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 999px;
  z-index: 60;
  font-size: .9rem;
  box-shadow: 0 10px 30px rgba(0,0,0,.35);
  max-width: min(90vw, 360px);
  text-align: center;
}

@media (max-width: 480px) {
  .msg { max-width: 88%; }
  .passphrase { grid-template-columns: 1fr; }
}
