:root {
  --bg: #0f1115;
  --panel: #171a21;
  --panel-2: #1e222b;
  --border: #2a2f3a;
  --text: #e7e9ee;
  --muted: #9aa3b2;
  --accent: #5b8cff;
  --accent-2: #3f6fe0;
  --danger: #ff6b6b;
  --bubble-me: #2f4a8c;
  --bubble-them: #232834;
  --radius: 12px;
  font-synthesis: none;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 15px;
}

input, button { font: inherit; }

button {
  cursor: pointer;
  border: none;
  border-radius: 8px;
  padding: 9px 14px;
  background: var(--panel-2);
  color: var(--text);
  transition: background 0.15s, opacity 0.15s;
}
button:hover { background: #262b36; }
button.primary { background: var(--accent); color: #fff; }
button.primary:hover { background: var(--accent-2); }
button.ghost { background: transparent; color: var(--muted); }
button.ghost:hover { background: var(--panel-2); color: var(--text); }
button.block { width: 100%; }
button:disabled { opacity: 0.5; cursor: default; }

input {
  background: var(--panel);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 10px 12px;
  width: 100%;
  outline: none;
}
input:focus { border-color: var(--accent); }

.error { color: var(--danger); font-size: 13px; margin: 4px 0 0; }
.info { color: #6fd08a; font-size: 13px; margin: 4px 0 0; line-height: 1.5; }
.hint { color: var(--muted); font-size: 12.5px; line-height: 1.5; margin-top: 14px; }

/* ---- Auth ---- */
.auth {
  display: grid;
  place-items: center;
  height: 100dvh;
  padding: 20px;
}
.auth-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px;
  width: 100%;
  max-width: 380px;
}
.auth-card h1 { margin: 0 0 10px; font-size: 26px; line-height: 0; }
.wordmark { height: 38px; display: block; }
.brand { display: flex; align-items: center; }
.brand .wordmark-sm { height: 24px; display: block; }
@keyframes wm-in { from { opacity: 0; transform: translateY(6px) scale(0.98); } to { opacity: 1; transform: none; } }
.auth .wordmark { animation: wm-in 0.5s ease-out both; }
.tagline { color: var(--muted); margin: 0 0 20px; font-size: 13.5px; line-height: 1.5; }
.tabs { display: flex; gap: 6px; margin-bottom: 18px; }
.tab { flex: 1; background: transparent; color: var(--muted); border: 1px solid var(--border); }
.tab.active { background: var(--panel-2); color: var(--text); border-color: var(--accent); }
#auth-form { display: flex; flex-direction: column; gap: 12px; }

/* ---- App layout ---- */
.app { display: grid; grid-template-columns: 280px 1fr; height: 100dvh; }

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 12px;
  min-width: 0;
}
.sidebar-head { display: flex; align-items: center; justify-content: space-between; }
.brand { font-weight: 600; }
.me { color: var(--muted); font-size: 13px; }
.me strong { color: var(--text); }

.new-buttons { display: flex; flex-direction: column; gap: 8px; }
.room-list { list-style: none; margin: 6px 0 0; padding: 0; overflow-y: auto; flex: 1; }
.room-list .room-name .kind { color: var(--muted); margin-right: 2px; }
.room-list li {
  padding: 11px 12px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.room-list li:hover { background: var(--panel-2); }
.room-list li.active { background: var(--panel-2); box-shadow: inset 2px 0 0 var(--accent); }
.room-list .room-name { font-weight: 500; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; display: flex; align-items: center; gap: 6px; min-width: 0; }
.room-list .room-name .label { overflow: hidden; text-overflow: ellipsis; }
.room-list .room-count { color: var(--muted); font-size: 12px; }
.room-list .room-meta { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }
.verified-tick { color: #4ade80; font-size: 12px; }

/* presence dots */
.dot { width: 8px; height: 8px; border-radius: 50%; flex-shrink: 0; background: #5a6271; }
.dot.online { background: #4ade80; box-shadow: 0 0 6px rgba(74, 222, 128, 0.6); }

/* unread badge */
.badge {
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
}

.head-actions { display: flex; gap: 8px; align-items: center; }
.menu-wrap { position: relative; }
.menu-dropdown {
  position: absolute; right: 0; top: calc(100% + 6px); z-index: 50;
  background: var(--panel); border: 1px solid var(--border); border-radius: 10px;
  padding: 6px; min-width: 180px; box-shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
  display: flex; flex-direction: column; gap: 2px;
}
.menu-item { text-align: left; background: transparent; padding: 9px 12px; border-radius: 7px; width: 100%; font-size: 14px; }
.menu-item:hover { background: var(--panel-2); }

/* key-change warning banner */
.key-warning {
  background: rgba(255, 107, 107, 0.12);
  border-bottom: 1px solid var(--danger);
  color: var(--danger);
  padding: 10px 20px;
  font-size: 13px;
}

/* verify modal */
.safety-label { color: var(--muted); font-size: 12px; margin-bottom: 6px; }
.safety-number {
  display: block;
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px;
  font-size: 16px;
  letter-spacing: 1px;
  line-height: 1.8;
  word-spacing: 4px;
  margin-bottom: 16px;
  color: var(--text);
}
.verify-members { list-style: none; padding: 0; margin: 0 0 8px; display: flex; flex-direction: column; gap: 10px; }
.verify-members li { background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; }
.vm-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.vm-name { display: flex; align-items: center; gap: 7px; font-weight: 500; }
.vm-fp { color: var(--muted); font-family: ui-monospace, monospace; font-size: 12.5px; margin-top: 6px; letter-spacing: 0.5px; }
.vm-status { font-size: 12px; }
.vm-status.verified { color: #4ade80; }
.vm-status.changed { color: var(--danger); }
.vm-status.unverified { color: var(--muted); }

/* ---- Chat ---- */
.chat { display: flex; flex-direction: column; min-width: 0; }
.chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.chat-head-title { min-width: 0; flex: 1; }
.chat-head h2 { margin: 0; font-size: 17px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.members { color: var(--muted); font-size: 12.5px; }
.head-actions { flex-shrink: 0; }
.back-btn { display: none; font-size: 24px; line-height: 1; padding: 2px 10px; flex-shrink: 0; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.empty-state { color: var(--muted); margin: auto; text-align: center; }

.msg { max-width: 70%; padding: 9px 13px; border-radius: 14px; line-height: 1.45; word-wrap: break-word; }
.msg .sender { display: block; font-size: 11.5px; color: var(--muted); margin-bottom: 2px; }
.msg .time { font-size: 10.5px; color: var(--muted); margin-left: 8px; }
.msg.them { background: var(--bubble-them); align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.me { background: var(--bubble-me); align-self: flex-end; border-bottom-right-radius: 4px; }
.msg.system { align-self: center; background: transparent; color: var(--muted); font-size: 12.5px; font-style: italic; }
.msg.tampered { border: 1px dashed var(--danger); color: var(--danger); }
.msg { position: relative; }
.msg .meta { display: inline-flex; align-items: center; gap: 5px; margin-left: 8px; vertical-align: baseline; }
.msg .status { font-size: 11px; color: rgba(255,255,255,0.55); letter-spacing: -2px; }
.msg .status.delivered { color: #7fd0ff; letter-spacing: -2px; }
.msg-del {
  position: absolute; top: -8px; right: -6px;
  width: 20px; height: 20px; padding: 0; line-height: 18px;
  font-size: 11px; border-radius: 50%;
  background: var(--panel-2); color: var(--muted);
  border: 1px solid var(--border);
  opacity: 0; transition: opacity 0.12s;
}
.msg:hover .msg-del { opacity: 1; }
.msg-del:hover { background: var(--danger); color: #fff; }

.msg-link { color: #cfe0ff; text-decoration: underline; word-break: break-all; }
.msg.them .msg-link { color: #9cc0ff; }

/* link preview card inside a message bubble */
.link-preview {
  display: block;
  margin-top: 8px;
  padding: 8px 10px;
  border-left: 3px solid var(--accent);
  background: rgba(0, 0, 0, 0.18);
  border-radius: 6px;
  text-decoration: none;
  color: inherit;
}
.link-preview:hover { background: rgba(0, 0, 0, 0.28); }
.link-preview .lp-thumb { display: block; width: 100%; max-height: 170px; object-fit: cover; border-radius: 5px; margin-bottom: 7px; }
.link-preview .lp-site { display: block; font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.3px; margin-bottom: 2px; }
.link-preview .lp-title { display: block; font-weight: 600; font-size: 13.5px; line-height: 1.3; }
.link-preview .lp-desc { display: block; font-size: 12.5px; color: var(--muted); margin-top: 3px; line-height: 1.35; max-height: 3.4em; overflow: hidden; }
.icon-btn.off { opacity: 0.55; }

.typing-indicator {
  padding: 4px 20px 8px;
  color: var(--muted);
  font-size: 12.5px;
  font-style: italic;
  min-height: 0;
}
.icon-btn { padding: 6px 9px; font-size: 15px; line-height: 1; }
.sidebar-head .head-actions { display: flex; align-items: center; gap: 4px; }

.send { display: flex; gap: 10px; padding: 16px 20px; border-top: 1px solid var(--border); }
.send input { flex: 1; }

/* ---- Modal ---- */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,0.55); display: grid; place-items: center; padding: 20px; }
.modal-card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; width: 100%; max-width: 420px; }
.modal-card h3 { margin: 0 0 16px; }
#modal-form { display: flex; flex-direction: column; gap: 14px; }
#modal-form label { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--muted); }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; margin-top: 6px; }

/* ---- Mobile: one pane at a time (room list <-> chat) ---- */
/* Single-pane layout for phones in BOTH orientations: narrow (portrait) OR
   short (landscape phone, wide but ~<500px tall). */
@media (max-width: 640px), (max-height: 500px) {
  html, body { font-size: 16px; } /* >=16px inputs avoid iOS auto-zoom */
  .app, .auth { height: 100dvh; }

  .app { grid-template-columns: 1fr; }
  .sidebar { border-right: none; padding: 14px; }

  /* Default (no room open): show the list, hide the chat. */
  .chat { display: none; }
  .app.show-chat .sidebar { display: none; }
  .app.show-chat .chat { display: flex; }

  .back-btn { display: inline-flex; align-items: center; }

  .new-buttons { flex-direction: row; }
  .room-list li { padding: 14px 12px; } /* larger tap targets */

  .messages { padding: 14px; }
  .msg { max-width: 85%; }
  .send { padding: 12px 14px; }
  .chat-head { padding: 12px 14px; }

  .auth-card { padding: 24px; }
  .modal { padding: 14px; }
  .modal-card { padding: 20px; }
  .safety-number { font-size: 15px; }
}

/* ---- Video calling ---- */
.call-incoming {
  position: fixed; top: 16px; left: 50%; transform: translateX(-50%); z-index: 110;
  background: var(--panel); border: 1px solid var(--accent); border-radius: 12px;
  padding: 12px 16px; display: flex; align-items: center; gap: 14px;
  box-shadow: 0 8px 30px rgba(0,0,0,0.5);
}
.call-incoming-acts { display: flex; gap: 8px; }
.call-overlay {
  position: fixed; inset: 0; z-index: 120; background: #0a0c10;
  display: flex; flex-direction: column;
}
.call-title { padding: 12px 16px; color: var(--muted); font-size: 13px; text-align: center; }
.call-grid {
  flex: 1; display: grid; gap: 8px; padding: 8px;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  align-content: center; justify-content: center; overflow: auto;
}
/* aspect-ratio is a pre-load default; JS overrides it per tile to match the
   camera's actual orientation. max-height keeps portrait tiles in bounds. */
.call-tile { position: relative; background: #15181f; border-radius: 12px; overflow: hidden; aspect-ratio: 4 / 3; max-height: 78vh; margin: 0 auto; }
.call-tile.portrait { max-width: min(56vh, 100%); } /* don't let a phone-portrait tile get too wide */
.call-tile video { width: 100%; height: 100%; object-fit: cover; background: #15181f; transform: scaleX(-1); }
.call-tile.remote video { transform: none; }
.call-avatar { display: none; }
.call-tile.audio-only video { display: none; }
.call-tile.audio-only .call-avatar {
  display: flex; align-items: center; justify-content: center;
  width: 100%; height: 100%; font-size: 52px; font-weight: 700; color: #fff;
  background: linear-gradient(135deg, #3f6fe0, #6f9bff);
}
.call-tile .call-name {
  position: absolute; bottom: 8px; left: 8px; background: rgba(0,0,0,0.55);
  color: #fff; font-size: 12px; padding: 2px 8px; border-radius: 6px;
}
.call-tile .call-muted { position: absolute; top: 8px; right: 8px; font-size: 14px; opacity: 0.85; }
.call-controls { display: flex; gap: 14px; justify-content: center; padding: 16px; }
.call-btn {
  width: 54px; height: 54px; border-radius: 50%; font-size: 20px; padding: 0;
  background: var(--panel-2); border: 1px solid var(--border);
}
.call-btn.active-off { background: #3a2030; color: var(--danger); }
.call-hangup { background: var(--danger); color: #fff; }
.call-hangup:hover { background: #e05555; }

/* ---- Enable-notifications banner ---- */
.push-banner {
  position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); z-index: 90;
  background: var(--panel); border: 1px solid var(--accent); border-radius: 12px;
  padding: 12px 16px; display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  justify-content: center; max-width: 92vw; box-shadow: 0 8px 30px rgba(0, 0, 0, 0.5);
  font-size: 13.5px;
}
.push-banner-acts { display: flex; gap: 8px; }

/* ---- Toasts ---- */
.toasts { position: fixed; top: 16px; left: 50%; transform: translateX(-50%); z-index: 100; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast {
  background: var(--panel-2); border: 1px solid var(--accent); color: var(--text);
  padding: 11px 16px; border-radius: 10px; font-size: 14px; max-width: 90vw;
  box-shadow: 0 6px 24px rgba(0,0,0,0.4); animation: toast-in 0.25s ease-out;
}
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px); } to { opacity: 1; transform: none; } }

/* ---- Invites / requests ---- */
.invites-section { margin: 2px 0; }
.invites-head { font-size: 11px; text-transform: uppercase; letter-spacing: 0.4px; color: var(--muted); padding: 4px 12px; }
.invites-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.invites-list li {
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px;
  padding: 8px 10px; display: flex; align-items: center; gap: 8px;
}
.invites-list .inv-label { flex: 1; min-width: 0; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.invites-list .inv-label .from { font-weight: 600; }
.invites-list.clickable .inv-label { cursor: pointer; }
.invites-list .inv-acts { display: flex; gap: 4px; flex-shrink: 0; }
.invites-list .inv-acts button { padding: 4px 9px; font-size: 12px; }

.request-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 14px 20px; border-top: 1px solid var(--border); background: var(--panel);
}
#request-text { color: var(--muted); font-size: 13.5px; }
.request-acts { display: flex; gap: 8px; flex-shrink: 0; }

/* ---- Admin panel ---- */
.admin-btn { margin-top: 2px; color: var(--muted); }
.admin-card { max-width: 560px; }
.admin-list { max-height: 50vh; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; margin: 6px 0 12px; }
.admin-row { background: var(--panel-2); border: 1px solid var(--border); border-radius: 8px; padding: 10px 12px; display: flex; align-items: center; gap: 10px; }
.admin-row .who { flex: 1; min-width: 0; }
.admin-row .uname { font-weight: 600; display: flex; align-items: center; gap: 6px; }
.admin-row .umeta { color: var(--muted); font-size: 12px; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.admin-row .acts { display: flex; gap: 6px; flex-shrink: 0; }
.admin-row .acts button { padding: 6px 10px; font-size: 13px; }
.tag { font-size: 10.5px; font-weight: 700; padding: 2px 6px; border-radius: 5px; text-transform: uppercase; letter-spacing: 0.3px; }
.tag.admin { background: rgba(91,140,255,0.18); color: var(--accent); }
.tag.disabled { background: rgba(255,107,107,0.16); color: var(--danger); }
.tag.unverified { background: rgba(154,163,178,0.16); color: var(--muted); }
.btn-danger { background: rgba(255,107,107,0.14) !important; color: var(--danger) !important; }
.btn-danger:hover { background: rgba(255,107,107,0.24) !important; }

[hidden] { display: none !important; }
