:root {
  --bg: #0b0f17;
  --panel: #111825;
  --panel-2: #0c121c;
  --elev: #18212f;
  --elev-2: #1e2836;
  --border: #222c3a;
  --text: #e8ecf3;
  --muted: #8a97a8;
  --faint: #5a6678;
  --accent: #303f9f;
  --accent-hover: #283593;
  --accent-soft: rgba(48, 63, 159, 0.22);
  --on-accent: #ffffff;
  --danger: #ff6b7d;
  --ok: #34d399;
  --radius: 14px;
  --radius-sm: 10px;
  --shadow: 0 18px 50px rgba(0, 0, 0, 0.45);
  --appbar-h: 56px;
  --rail-w: 320px;
}

* { box-sizing: border-box; }

/* The [hidden] attribute must win over any element's explicit `display`. */
[hidden] { display: none !important; }

html, body { height: 100%; }
body {
  margin: 0;
  color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  overflow: hidden;               /* the app manages its own scroll areas */
  overscroll-behavior: none;
}

button { font: inherit; cursor: pointer; }
[data-icon] { display: inline-flex; }
[data-icon] svg { width: 20px; height: 20px; }
.spacer { flex: 1; }

/* ===================== Icon buttons ===================== */
.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  transition: background 0.15s, color 0.15s;
}
.icon-btn:hover { background: var(--elev); color: var(--text); }

/* ===================== Auth (logged out) ===================== */
#auth-shell {
  height: 100%;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 1.5rem 1rem;
  background:
    radial-gradient(1000px 560px at 50% -10%, rgba(48, 63, 159, 0.10), transparent 60%),
    var(--bg);
}
#auth-shell > div { width: 100%; max-width: 27rem; }
.app-header { text-align: center; margin-bottom: 1.4rem; }
.brand { font-size: 1.15rem; font-weight: 700; display: inline-flex; align-items: center; gap: 0.55rem; }
.brand-mark { color: var(--accent); }
.brand-mark svg { width: 22px; height: 22px; }

section.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.35rem;
  box-shadow: var(--shadow);
}
h2 { font-size: 1.05rem; margin: 0 0 0.9rem; }
h3 { font-size: 1rem; margin: 0 0 0.6rem; }
label { display: block; font-size: 0.8rem; color: var(--muted); margin-bottom: 0.4rem; }

input[type="text"], input[type="file"], textarea {
  width: 100%;
  padding: 0.7rem 0.8rem;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
  font-family: inherit;
}
input::placeholder, textarea::placeholder { color: var(--faint); }
input:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
input[type="text"], input[type="file"] { margin-bottom: 0.85rem; }
input[type="file"]::file-selector-button {
  margin-right: 0.7rem; padding: 0.4rem 0.7rem; border: 1px solid var(--border);
  border-radius: 8px; background: var(--elev); color: var(--text); cursor: pointer;
}

button:not(.icon-btn):not(.menu-item) {
  padding: 0.62rem 1.05rem; border: 1px solid transparent; border-radius: var(--radius-sm);
  background: var(--accent); color: var(--on-accent); font-weight: 600; font-size: 0.9rem;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
button:not(.icon-btn):not(.menu-item):hover { background: var(--accent-hover); }
button:not(.icon-btn):not(.menu-item):active { transform: translateY(1px); }
button.secondary { background: transparent; color: var(--text); border-color: var(--border); }
button.secondary:hover { background: var(--elev); }
button.danger { background: transparent; color: var(--danger); border-color: rgba(255, 107, 125, 0.4); }
button.danger:hover { background: rgba(255, 107, 125, 0.12); }

.actions { display: flex; gap: 0.6rem; flex-wrap: wrap; align-items: center; }
.hint { color: var(--muted); font-size: 0.8rem; }
.status { margin-top: 0.7rem; color: var(--muted); font-size: 0.82rem; min-height: 1.1em; }
.muted { color: var(--muted); }

/* ===================== App shell (logged in) ===================== */
/* --app-h is set from visualViewport so the keyboard shrinks the app instead of
   pushing it off-screen; falls back to 100dvh where the API is absent. */
#app { height: var(--app-h, 100dvh); display: flex; flex-direction: column; }

.appbar {
  height: var(--appbar-h);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0 0.6rem;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.appbar-brand { font-weight: 700; display: inline-flex; align-items: center; gap: 0.45rem; font-size: 0.98rem; flex-shrink: 0; }
.appbar-brand .brand-mark svg { width: 18px; height: 18px; }
/* Wide screens: push the conversation group (peer + menu) to the right, away
   from the brand. Overridden on mobile to sit left of the burger. */
.appbar-convo { display: flex; align-items: center; gap: 0.5rem; min-width: 0; margin-left: auto; }
.appbar-convo .chat-peer-meta strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.popover-brand { display: flex; align-items: center; gap: 0.4rem; padding: 0.4rem 0.55rem; font-weight: 700; font-size: 0.9rem; }
.popover-brand .brand-mark { color: var(--accent); }
.popover-brand .brand-mark svg { width: 16px; height: 16px; }
.account-btn {
  display: inline-flex; align-items: center; gap: 0.5rem;
  padding: 0.25rem 0.5rem 0.25rem 0.3rem; border: 1px solid transparent; border-radius: 999px;
  background: transparent; color: var(--text);
}
.account-btn:hover { background: var(--elev); }
.me-handle { font-size: 0.9rem; font-weight: 600; }

.app-body { flex: 1; display: flex; min-height: 0; }

/* Draggable divider to resize the rail (desktop). */
.rail-resize { flex-shrink: 0; width: 6px; cursor: col-resize; background: transparent; transition: background 0.15s; }
.rail-resize:hover, .rail-resize.dragging { background: var(--accent); }

/* --- Rail --- */
.rail {
  width: var(--rail-w);
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
}
.rail-search {
  position: relative;
  padding: 0.7rem;
  border-bottom: 1px solid var(--border);
}
.rail-search .search-icon {
  position: absolute; left: 1.3rem; top: 50%; transform: translateY(-50%); color: var(--faint);
}
.rail-search .search-icon svg { width: 18px; height: 18px; }
.rail-search input { margin: 0; padding-left: 2.4rem; }
.rail-expand { display: none; }        /* mobile-only expand affordance */
#rail-backdrop { display: none; }      /* click-away catcher when the strip is expanded */

.search-results { border-bottom: 1px solid var(--border); }
.search-results .result {
  display: flex; align-items: center; gap: 0.6rem; padding: 0.55rem 0.7rem;
}
.search-results .result .grow { flex: 1; min-width: 0; }
.search-results .result small { color: var(--muted); }

.contacts-list { flex: 1; overflow-y: auto; padding: 0.35rem; }
.contact-tile {
  display: flex; align-items: center; gap: 0.65rem;
  padding: 0.55rem 0.6rem; border-radius: var(--radius-sm); cursor: pointer;
  transition: background 0.12s;
}
.contact-tile:hover { background: var(--elev); }
.contact-tile.active { background: var(--accent-soft); }
.contact-tile .grow { flex: 1; min-width: 0; }
.contact-tile .row1 { display: flex; align-items: center; justify-content: space-between; gap: 0.5rem; }
.contact-tile .name-wrap { display: inline-flex; align-items: center; gap: 0.3rem; min-width: 0; }
.contact-tile .name { font-weight: 600; font-size: 0.93rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.contact-tile .preview {
  color: var(--muted); font-size: 0.8rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.contact-tile .time { color: var(--faint); font-size: 0.68rem; flex-shrink: 0; }
.unread-dot {
  width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0;
  background: #6d8cff; box-shadow: 0 0 7px 1px rgba(109, 140, 255, 0.8);
}
/* Distinct (amber) badge: the peer is asking to erase this conversation. */
.erase-dot {
  width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0;
  background: #ff9f43; box-shadow: 0 0 7px 1px rgba(255, 159, 67, 0.85);
}
.contacts-empty { color: var(--muted); font-size: 0.82rem; text-align: center; padding: 1.2rem 0.5rem; }

/* Avatar */
.avatar {
  width: 40px; height: 40px; border-radius: 50%; flex-shrink: 0;
  display: inline-flex; align-items: center; justify-content: center;
  background: linear-gradient(160deg, var(--elev-2), var(--elev));
  border: 1px solid var(--border);
  font-weight: 700; font-size: 0.95rem; color: var(--text); text-transform: uppercase;
}
.avatar.sm { width: 30px; height: 30px; font-size: 0.8rem; }

/* --- Chat --- */
.chat { flex: 1; display: flex; flex-direction: column; min-height: 0; background: var(--bg); }
.chat-empty {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 0.6rem; color: var(--faint);
}
.chat-empty svg { width: 40px; height: 40px; }
.chat-convo { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.request-banner { flex-shrink: 0; padding: 0.75rem; border-bottom: 1px solid var(--border); background: var(--accent-soft); }
.request-banner p { margin: 0 0 0.6rem; font-size: 0.9rem; }
.request-banner .actions button { padding: 0.42rem 0.75rem; font-size: 0.82rem; }

/* Rail: pending-request section */
.rail-section-title {
  font-size: 0.68rem; text-transform: uppercase; letter-spacing: 0.07em; font-weight: 700;
  color: var(--muted); padding: 0.55rem 0.7rem 0.25rem;
}
.contact-tile.request { border-left: 2px solid var(--accent); }
.contact-tile.request .preview { color: var(--text); }

/* Manage (block/silence) modal */
.manage-row { display: flex; align-items: center; gap: 0.6rem; padding: 0.6rem 0; border-bottom: 1px solid var(--border); }
.manage-row:last-child { border-bottom: none; }
.manage-row .grow { flex: 1; min-width: 0; }
.manage-row .handle { font-weight: 600; }
.manage-row select {
  padding: 0.35rem 0.5rem; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px;
}
.manage-empty { color: var(--muted); font-size: 0.85rem; padding: 0.8rem 0; text-align: center; }

.account-visibility { margin-bottom: 1rem; }
.account-visibility label { margin-bottom: 0.35rem; }
.account-visibility select {
  width: 100%; padding: 0.55rem 0.7rem; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border); border-radius: var(--radius-sm);
}
.account-visibility .hint { margin: 0.4rem 0 0; }

.fp-row { display: flex; align-items: center; gap: 0.5rem; }
.fp-row code {
  flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-size: 0.78rem; color: var(--muted); font-family: ui-monospace, monospace;
}
#copy-fp-status { min-height: 0; margin: 0.2rem 0 0; }
.qr-box { background: #fff; border-radius: 12px; padding: 14px; display: flex; justify-content: center; margin-bottom: 1rem; }
.qr-box svg { width: 220px; height: 220px; display: block; }
.scan-box { border-radius: 12px; overflow: hidden; background: #000; margin-bottom: 0.5rem; }
.scan-box video { width: 100%; max-height: 60vh; display: block; object-fit: cover; }

.chat-header {
  flex-shrink: 0; display: flex; align-items: center; gap: 0.6rem;
  padding: 0.55rem 0.7rem; border-bottom: 1px solid var(--border); background: var(--panel);
}
.chat-peer-meta { display: flex; flex-direction: column; line-height: 1.2; min-width: 0; }
.chat-peer-meta strong { font-size: 0.98rem; }
.peer-verified { color: var(--ok); font-size: 0.72rem; display: inline-flex; align-items: center; gap: 0.2rem; }
.peer-verified svg { width: 13px; height: 13px; }

.convo-messages {
  flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 0.3rem;
  padding: 1rem;
}
.msg {
  max-width: min(78%, 44rem);
  padding: 0.5rem 0.72rem; border-radius: 16px;
  word-break: break-word; white-space: pre-wrap; font-size: 0.94rem; line-height: 1.45;
}
/* Incoming is emphasized; own messages are subdued. */
.msg.them {
  align-self: flex-start;
  background: linear-gradient(180deg, var(--accent), var(--accent-hover));
  color: var(--on-accent);
  border-bottom-left-radius: 5px;
}
.msg.mine {
  align-self: flex-end;
  background: var(--elev);
  border: 1px solid var(--border);
  border-bottom-right-radius: 5px;
}
.msg-time { display: block; margin-top: 3px; font-size: 0.65rem; opacity: 0.65; text-align: right; }

.convo-compose {
  flex-shrink: 0; display: flex; align-items: flex-end; gap: 0.5rem;
  padding: 0.6rem 0.7rem; border-top: 1px solid var(--border); background: var(--panel);
}
.convo-compose textarea {
  flex: 1; resize: none; overflow-y: hidden; max-height: 75vh;
  border-radius: 20px; padding: 0.55rem 0.9rem; line-height: 1.4;
}
.send-btn { background: var(--accent); color: var(--on-accent); flex-shrink: 0; }
.send-btn:hover { background: var(--accent-hover); color: var(--on-accent); }
/* Status line below the composer only takes space when it has a message. */
#convo-status { margin: 0; min-height: 0; padding: 0.35rem 0.7rem 0.55rem; }
#convo-status:empty { display: none; }

/* Scrollbars */
.convo-messages::-webkit-scrollbar, .contacts-list::-webkit-scrollbar, textarea::-webkit-scrollbar { width: 8px; }
.convo-messages::-webkit-scrollbar-thumb, .contacts-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 999px; }

/* ===================== Popovers & context menu ===================== */
#popover-backdrop { position: fixed; inset: 0; z-index: 40; }
.popover {
  position: fixed; z-index: 50; min-width: 15rem;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 0.4rem;
}
.popover-head { display: flex; align-items: center; gap: 0.6rem; padding: 0.5rem 0.55rem; }
.profile-head {
  width: 100%; border: none; background: transparent; color: var(--text);
  border-radius: var(--radius-sm); text-align: left;
}
.profile-head:hover { background: var(--elev); }
.profile-head .grow { flex: 1; display: flex; flex-direction: column; line-height: 1.2; }
.profile-head small { font-size: 0.72rem; }
.profile-head .chev { color: var(--faint); }
.profile-head .chev svg { width: 16px; height: 16px; }
.pop-divider { height: 1px; background: var(--border); margin: 0.35rem 0.3rem; }
.account-id { display: flex; align-items: center; gap: 0.65rem; margin-bottom: 0.9rem; }
.account-id strong { font-size: 1.05rem; }
.pin-badge { color: var(--faint); display: inline-flex; }
.pin-badge svg { width: 14px; height: 14px; }
.popover-fp { padding: 0.2rem 0.6rem 0.6rem; border-bottom: 1px solid var(--border); margin-bottom: 0.35rem; }
.popover-fp .muted { display: block; font-size: 0.72rem; margin-bottom: 0.15rem; }
.popover-fp code { font-size: 0.72rem; word-break: break-all; color: var(--muted); font-family: ui-monospace, monospace; }
.menu-item {
  display: flex; align-items: center; gap: 0.6rem; width: 100%;
  padding: 0.55rem 0.6rem; border: none; background: transparent; color: var(--text);
  border-radius: var(--radius-sm); text-align: left; font-size: 0.9rem;
}
.menu-item:hover { background: var(--elev); }
.menu-item.danger { color: var(--danger); }
.menu-item.danger:hover { background: rgba(255, 107, 125, 0.12); }
.menu-item svg { width: 18px; height: 18px; color: var(--muted); }
.menu-item.danger svg { color: var(--danger); }

/* ===================== Modals ===================== */
.modal-overlay {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(4, 7, 12, 0.66); backdrop-filter: blur(3px);
  align-items: center; justify-content: center; padding: 1rem;
}
.modal-overlay:not([hidden]) { display: flex; }
.modal {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 1.35rem; max-width: 27rem; width: 100%; box-shadow: var(--shadow);
}
.modal p { margin: 0 0 1.1rem; line-height: 1.5; }
.modal-actions { justify-content: flex-end; }
.safety-number {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 1.15rem; letter-spacing: 0.06em;
  text-align: center; background: var(--panel-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 0.9rem; margin: 0.9rem 0 1.1rem; word-spacing: 0.35rem;
}

/* ===================== Responsive (mobile) ===================== */
@media (max-width: 720px) {
  .app-body { position: relative; }
  .rail-resize { display: none; }
  /* Tight width: give the conversation the room — the title lives in the burger.
     Peer info sits left (after the burger); the ⋮ menu goes to the right. */
  body.in-convo .appbar-brand { display: none; }
  .appbar-convo { margin-left: 0.15rem; }
  #convo-menu-btn { margin-left: auto; }

  /* Collapsed rail = a narrow strip of avatars; the conversation stays beside it. */
  .rail { width: 58px; }
  .rail-search { display: flex; align-items: center; justify-content: center; padding: 0.4rem 0.2rem; }
  .rail-search input, .rail-search .search-icon { display: none; }
  .rail-expand { display: inline-flex; width: 100%; height: 40px; }
  .contacts-list { padding: 0.3rem 0.2rem; }
  .contact-tile {
    position: relative; justify-content: center; gap: 0;
    width: -moz-fit-content; width: fit-content; margin: 3px auto; padding: 4px; border-radius: 50%;
  }
  .contact-tile .grow, .contact-tile .time { display: none; }
  .contact-tile .unread-dot, .contact-tile .erase-dot {
    position: absolute; top: 1px; right: 1px; width: 13px; height: 13px;
    border: 2px solid var(--panel);
  }
  .contact-tile .unread-dot { box-shadow: 0 0 9px 2px rgba(109, 140, 255, 0.95); }
  .contact-tile .erase-dot { box-shadow: 0 0 9px 2px rgba(255, 159, 67, 0.95); }

  /* Expanded: rail overlays the conversation; a backdrop catches click-away. */
  .app-body.rail-open .rail {
    position: absolute; top: 0; left: 0; height: 100%; width: min(300px, 84vw);
    z-index: 30; box-shadow: 0 0 40px rgba(0, 0, 0, 0.55);
  }
  .app-body.rail-open .rail-search { justify-content: stretch; padding: 0.7rem; }
  .app-body.rail-open .rail-search input { display: block; }
  .app-body.rail-open .rail-search .search-icon { display: flex; }
  .app-body.rail-open .rail-expand { display: none; }
  .app-body.rail-open .contact-tile {
    justify-content: flex-start; gap: 0.65rem; width: auto; margin: 0; padding: 0.55rem 0.6rem; border-radius: var(--radius-sm);
  }
  .app-body.rail-open .contact-tile .grow, .app-body.rail-open .contact-tile .time { display: block; }
  .app-body.rail-open .contact-tile .unread-dot,
  .app-body.rail-open .contact-tile .erase-dot { position: static; border: none; width: 10px; height: 10px; }
  .app-body.rail-open #rail-backdrop {
    display: block; position: absolute; inset: 0; z-index: 25; background: rgba(0, 0, 0, 0.45);
  }

  .msg { max-width: 85%; }
}
