:root {
  --bg: #0c1117;
  --bg-elev: #10151b;
  --bg-elev2: #161d26;
  --bg-input: #1b232e;
  --border: #232c38;
  --text: #e6edf3;
  --text-dim: #8b97a7;
  --text-faint: #5b6675;
  --accent: #2e8fff;
  --accent-dim: #1f6fd0;
  --bubble-in: #1c2530;
  --bubble-out: #16527e;
  --green: #3fb950;
  --yellow: #d29922;
  --red: #f85149;
  --shadow: 0 8px 30px rgba(0, 0, 0, 0.4);
  --radius: 12px;
}

* { box-sizing: border-box; }

/* The [hidden] attribute must always win over class-based display:flex/grid. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; cursor: pointer; }
input, textarea, select { font-family: inherit; }

/* ===================== LOGIN ===================== */
.login-screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  padding: 24px;
  background:
    radial-gradient(1200px 600px at 50% -10%, #16263b 0%, transparent 60%),
    var(--bg);
}
.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px 24px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login-title { margin: 0; font-size: 26px; text-align: center; }
.login-sub { margin: -6px 0 8px; text-align: center; color: var(--text-dim); font-size: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; color: var(--text-dim); }
.field input {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 13px;
  color: var(--text);
  font-size: 15px;
  outline: none;
  transition: border-color .15s;
}
.field input:focus { border-color: var(--accent); }
.advanced { font-size: 13px; color: var(--text-dim); }
.advanced summary { cursor: pointer; padding: 4px 0; user-select: none; }
.advanced .field { margin-top: 10px; }
.checkbox { display: flex; align-items: center; gap: 8px; margin-top: 10px; font-size: 13px; color: var(--text-dim); }
.checkbox input { width: 16px; height: 16px; accent-color: var(--accent); }
.btn-primary {
  margin-top: 6px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 12px;
  font-size: 15px;
  font-weight: 600;
  transition: background .15s, opacity .15s;
}
.btn-primary:hover { background: var(--accent-dim); }
.btn-primary:disabled { opacity: .6; cursor: progress; }
.login-error {
  background: rgba(248, 81, 73, .12);
  border: 1px solid rgba(248, 81, 73, .4);
  color: #ff9b95;
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 13px;
}
.login-status { color: var(--text-dim); font-size: 13px; text-align: center; }
.login-footer { color: var(--text-faint); font-size: 12px; }

/* ===================== APP LAYOUT ===================== */
.app { display: flex; height: 100%; overflow: hidden; }

.sidebar {
  width: 320px;
  min-width: 320px;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100%;
}
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  gap: 8px;
}
.me { display: flex; align-items: center; gap: 10px; min-width: 0; }
.me-info { min-width: 0; }
.me-jid {
  font-size: 13px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  max-width: 150px;
}
.presence-select {
  margin-top: 2px;
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 12px;
  outline: none;
  padding: 0;
}
.presence-select option { background: var(--bg-elev2); color: var(--text); }
.sidebar-actions { display: flex; gap: 2px; }

.avatar {
  width: 40px; height: 40px;
  min-width: 40px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 16px;
  color: #fff;
  background: var(--accent-dim);
  text-transform: uppercase;
}

.icon-btn {
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 18px;
  width: 38px; height: 38px;
  border-radius: 9px;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, color .15s;
}
.icon-btn:hover { background: var(--bg-elev2); color: var(--text); }

.search-box { padding: 10px 12px; border-bottom: 1px solid var(--border); }
.search-box input {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 9px 12px;
  color: var(--text);
  outline: none;
  font-size: 14px;
}

.conversation-list { flex: 1; overflow-y: auto; }
.conv {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 11px 14px;
  cursor: pointer;
  border-bottom: 1px solid rgba(35, 44, 56, .5);
  position: relative;
}
.conv:hover { background: var(--bg-elev2); }
.conv.active { background: var(--bg-elev2); }
.conv .avatar { position: relative; }
.conv-dot {
  position: absolute;
  right: -1px; bottom: -1px;
  width: 12px; height: 12px;
  border-radius: 50%;
  border: 2px solid var(--bg-elev);
  background: var(--text-faint);
}
.conv-dot.available { background: var(--green); }
.conv-dot.away, .conv-dot.xa { background: var(--yellow); }
.conv-dot.dnd { background: var(--red); }
.conv-body { flex: 1; min-width: 0; }
.conv-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; }
.conv-name { font-weight: 600; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-time { font-size: 11px; color: var(--text-faint); white-space: nowrap; }
.conv-preview { font-size: 13px; color: var(--text-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.conv-badge {
  background: var(--accent);
  color: #fff;
  font-size: 11px; font-weight: 700;
  min-width: 19px; height: 19px;
  padding: 0 6px;
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
}
.conv-room .avatar { background: #6b46c1; }

.conn-banner {
  padding: 8px 14px;
  font-size: 12px;
  text-align: center;
  background: rgba(210, 153, 34, .15);
  color: var(--yellow);
  border-top: 1px solid var(--border);
}

/* ===================== CHAT ===================== */
.chat { flex: 1; display: flex; flex-direction: column; min-width: 0; background: var(--bg); }
.empty-chat {
  flex: 1;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  color: var(--text-faint);
  gap: 10px;
}
.empty-emoji { font-size: 56px; opacity: .5; }

.chat-view { flex: 1; display: flex; flex-direction: column; min-height: 0; }
.chat-head {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elev);
}
.back-btn { display: none; font-size: 26px; }
.chat-peer { display: flex; align-items: center; gap: 11px; flex: 1; min-width: 0; }
.peer-name { font-weight: 600; font-size: 15px; }
.peer-status { font-size: 12px; color: var(--text-dim); }
.peer-status.online { color: var(--green); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.load-more {
  align-self: center;
  background: var(--bg-elev2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 16px;
  padding: 6px 14px;
  font-size: 12px;
  margin-bottom: 8px;
}
.date-sep {
  align-self: center;
  font-size: 11px;
  color: var(--text-faint);
  background: var(--bg-elev2);
  padding: 3px 10px;
  border-radius: 10px;
  margin: 10px 0 6px;
}
.msg {
  max-width: min(72%, 560px);
  padding: 8px 12px;
  border-radius: 14px;
  font-size: 14.5px;
  line-height: 1.4;
  word-wrap: break-word;
  overflow-wrap: anywhere;
  position: relative;
}
.msg.in { align-self: flex-start; background: var(--bubble-in); border-bottom-left-radius: 4px; }
.msg.out { align-self: flex-end; background: var(--bubble-out); border-bottom-right-radius: 4px; }
.msg.grouped-top { margin-top: 8px; }
.msg-sender { font-size: 12px; font-weight: 700; margin-bottom: 2px; color: #7fb8ff; }
.msg-body a { color: #9cd1ff; }
.img-loader { margin-top: 6px; }
.img-loader img.inline-img { max-width: 100%; border-radius: 8px; display: block; cursor: zoom-in; }
.img-load-btn {
  background: var(--bg-elev2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 13px;
}
.img-load-btn:hover { color: var(--text); }
.file-link { display: inline-block; color: #9cd1ff; text-decoration: none; padding: 4px 0; }
.file-link:hover { text-decoration: underline; }
.msg-meta {
  display: flex; align-items: center; gap: 5px; justify-content: flex-end;
  font-size: 10.5px; color: var(--text-faint); margin-top: 3px;
}
.msg.out .msg-meta { color: rgba(230, 237, 243, .6); }
.msg-tick { font-size: 11px; }
.msg-tick.read { color: #7fd0ff; }

.typing-indicator { padding: 2px 18px 6px; font-size: 12px; color: var(--text-dim); font-style: italic; }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  padding: 10px 12px;
  border-top: 1px solid var(--border);
  background: var(--bg-elev);
}
.message-input {
  flex: 1;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 10px 15px;
  color: var(--text);
  font-size: 14.5px;
  resize: none;
  outline: none;
  max-height: 140px;
  line-height: 1.4;
}
.message-input:focus { border-color: var(--accent); }
.send-btn { background: var(--accent); color: #fff; }
.send-btn:hover { background: var(--accent-dim); color: #fff; }

/* ===================== MODAL ===================== */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0, 0, 0, .55);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
  z-index: 100;
}
.modal {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 22px;
  width: 100%; max-width: 380px;
  box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 14px;
}
.modal h2 { margin: 0; font-size: 18px; }
.modal p { margin: 0; color: var(--text-dim); font-size: 14px; }
.modal .field input { font-size: 15px; }
.modal-actions { display: flex; gap: 8px; justify-content: flex-end; }
.btn-ghost {
  background: transparent; border: 1px solid var(--border);
  color: var(--text); border-radius: 9px; padding: 9px 16px; font-size: 14px;
}
.btn-ghost:hover { background: var(--bg-elev2); }
.btn-solid {
  background: var(--accent); border: none; color: #fff;
  border-radius: 9px; padding: 9px 16px; font-size: 14px; font-weight: 600;
}
.btn-solid:hover { background: var(--accent-dim); }
.btn-danger { background: var(--red); }

/* ===================== OMEMO ===================== */
.icon-btn.omemo-on { color: var(--green); }
.msg-lock { font-size: 10px; opacity: .65; margin-right: 2px; }
.msg.decrypt-failed { opacity: .75; font-style: italic; }
.fp-list { display: flex; flex-direction: column; gap: 10px; max-height: 240px; overflow-y: auto; }
.fp-row { display: flex; align-items: flex-start; gap: 10px; }
.fp-row input { margin-top: 3px; width: 16px; height: 16px; accent-color: var(--green); }
.fp-dev { font-size: 12px; color: var(--text-dim); margin-bottom: 2px; }
.fp-code {
  display: block;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 11.5px;
  color: var(--text);
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 7px 9px;
  word-break: break-all;
  line-height: 1.5;
}
.fp-code.own { color: var(--green); }

/* ===================== TOAST ===================== */
.toast-wrap {
  position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; gap: 8px; z-index: 200; align-items: center;
}
.toast {
  background: var(--bg-elev2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 10px;
  padding: 10px 16px;
  font-size: 13px;
  box-shadow: var(--shadow);
  max-width: 90vw;
}
.toast.err { border-color: rgba(248, 81, 73, .5); color: #ff9b95; }

/* ===================== CALLS ===================== */
.call-overlay {
  position: fixed; inset: 0; z-index: 300;
  background: #05080c;
  display: flex; align-items: center; justify-content: center;
}
.call-remote {
  width: 100%; height: 100%;
  object-fit: contain;
  background: #05080c;
}
.call-overlay.audio-only .call-remote { display: none; }
.call-local {
  position: absolute; right: 16px; bottom: 96px;
  width: 150px; max-width: 32vw;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, .15);
  object-fit: cover;
  background: #0c1117;
  box-shadow: var(--shadow);
  transform: scaleX(-1); /* mirror self-view */
}
.call-head {
  position: absolute; top: 0; left: 0; right: 0;
  padding: 22px 18px;
  text-align: center;
  background: linear-gradient(to bottom, rgba(0, 0, 0, .55), transparent);
  pointer-events: none;
}
.call-title { font-size: 20px; font-weight: 700; color: #fff; }
.call-status { font-size: 13px; color: rgba(255, 255, 255, .75); margin-top: 4px; }
.call-timer { font-size: 13px; color: rgba(255, 255, 255, .75); margin-top: 2px; }
.call-controls {
  position: absolute; bottom: 26px; left: 0; right: 0;
  display: flex; gap: 18px; justify-content: center;
}
.call-ctl {
  width: 58px; height: 58px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, .14);
  color: #fff;
  font-size: 24px;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s, opacity .15s;
}
.call-ctl:hover { background: rgba(255, 255, 255, .24); }
.call-ctl.off { background: rgba(255, 255, 255, .35); opacity: .6; }
.call-hang { background: var(--red); }
.call-hang:hover { background: #ff6b63; }
.call-accept { background: var(--green); }
.call-accept:hover { background: #4cd566; }

.call-incoming {
  position: fixed; inset: 0; z-index: 320;
  background: rgba(0, 0, 0, .6);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.call-inc-card {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 26px 24px;
  width: 100%; max-width: 340px;
  text-align: center;
  box-shadow: var(--shadow);
}
.call-inc-title { font-size: 20px; font-weight: 700; }
.call-inc-sub { font-size: 14px; color: var(--text-dim); margin-top: 6px; }
.call-inc-actions { display: flex; gap: 28px; justify-content: center; margin-top: 24px; }

/* ===================== RESPONSIVE ===================== */
@media (max-width: 720px) {
  .sidebar { width: 100%; min-width: 0; }
  .app.chat-open .sidebar { display: none; }
  .app:not(.chat-open) .chat { display: none; }
  .back-btn { display: flex; }
  .msg { max-width: 84%; }
}
