: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: 100%;
  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: 100%; }

.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; }

/* 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; }

.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) ---- */
@media (max-width: 640px) {
  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; }
}

/* ---- 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; }
