* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #111b21;
  --panel: #202c33;
  --panel-2: #2a3942;
  --bubble-in: #202c33;
  --bubble-out: #005c4b;
  --text: #e9edef;
  --muted: #8696a0;
  --accent: #00a884;
  --danger: #f15c6d;
  --radius: 10px;
}

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

.hidden { display: none !important; }

/* ---------- login ---------- */
.login-view {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}
.login-card {
  background: var(--panel);
  padding: 40px;
  border-radius: 12px;
  width: 340px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.login-card h1 { color: var(--accent); font-size: 26px; }
.login-card .subtitle { color: var(--muted); font-size: 13px; margin-bottom: 8px; }
.login-card input {
  padding: 12px;
  border-radius: 8px;
  border: 1px solid var(--panel-2);
  background: var(--panel-2);
  color: var(--text);
  font-size: 14px;
}
.login-card button {
  padding: 12px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.error { color: var(--danger); font-size: 13px; }

/* ---------- app layout ---------- */
.app-view { height: 100vh; display: flex; flex-direction: column; }

.status-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 6px 12px;
  background: var(--panel);
  border-bottom: 1px solid #000;
  font-size: 13px;
}
.status-dot { display: inline-block; width: 9px; height: 9px; border-radius: 50%; margin-right: 6px; }
.status-dot.open { background: var(--accent); }
.status-dot.connecting, .status-dot.offline { background: #f5a623; }
.status-dot.loggedOut { background: var(--danger); }
.status-bar .btn { margin-left: auto; }

.btn {
  padding: 6px 14px;
  border: none;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  font-size: 13px;
  cursor: pointer;
}
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn.ghost { background: transparent; border: 1px solid var(--panel-2); color: var(--text); }

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

/* ---------- sidebar ---------- */
.sidebar {
  width: 320px;
  min-width: 320px;
  background: var(--panel);
  border-right: 1px solid #000;
  display: flex;
  flex-direction: column;
}
.sidebar-header { padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.sidebar-header input {
  width: 100%;
  padding: 9px 12px;
  border-radius: 8px;
  border: none;
  background: var(--panel-2);
  color: var(--text);
  font-size: 14px;
}
.sync-btn { width: 100%; text-align: center; }
.chat-list { flex: 1; overflow-y: auto; }
.chat-empty { color: var(--muted); text-align: center; padding: 24px 16px; font-size: 13px; }
.chat-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
}
.chat-item:hover, .chat-item.active { background: var(--panel-2); }
.chat-avatar {
  width: 44px;
  height: 44px;
  min-width: 44px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  text-transform: uppercase;
  overflow: hidden;
}
.chat-avatar img { width: 100%; height: 100%; object-fit: cover; border-radius: 50%; }
.chat-item-body { flex: 1; min-width: 0; }
.chat-item-top { display: flex; justify-content: space-between; align-items: baseline; gap: 8px; min-width: 0; }
.chat-item-name { font-weight: 600; font-size: 15px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1 1 auto; min-width: 0; }
.chat-item-time { font-size: 11px; color: var(--muted); white-space: nowrap; flex-shrink: 0; }
.chat-item-preview { font-size: 13px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-item-unread {
  background: var(--accent);
  color: #fff;
  border-radius: 10px;
  font-size: 11px;
  padding: 1px 7px;
  font-weight: 700;
}

/* ---------- chat pane ---------- */
.chat-pane { flex: 1; display: flex; flex-direction: column; min-width: 0; position: relative; }
.chat-header {
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid #000;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 10px;
}
.chat-header-avatar {
  width: 36px;
  height: 36px;
  min-width: 36px;
  border-radius: 50%;
  overflow: hidden;
  background: var(--panel-2);
}
.chat-header-avatar img { width: 100%; height: 100%; object-fit: cover; }
.msg-sender {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 2px;
}
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  background:
    radial-gradient(circle at 20% 20%, rgba(0, 168, 132, 0.06), transparent 40%),
    var(--bg);
}
.msg {
  max-width: 70%;
  padding: 7px 10px 6px;
  border-radius: var(--radius);
  font-size: 14px;
  line-height: 1.35;
  position: relative;
  word-wrap: break-word;
}
.msg-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin: 2px 0;
}
.msg-row .msg-col { display: flex; flex-direction: column; max-width: 75%; min-width: 0; }
.msg-row .msg-col .msg { max-width: 100%; }
.msg-avatar {
  width: 28px;
  height: 28px;
  min-width: 28px;
  border-radius: 50%;
  background: var(--panel-2);
  color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  overflow: hidden;
  margin-bottom: 2px;
  text-transform: uppercase;
}
.msg-avatar img { width: 100%; height: 100%; object-fit: cover; }
.msg-avatar.hidden {
  display: flex !important;
  visibility: hidden;
}
.msg.in { background: var(--bubble-in); align-self: flex-start; border-top-left-radius: 2px; }
.msg.out { background: var(--bubble-out); align-self: flex-end; border-top-right-radius: 2px; }
.reply-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 16px;
  background: var(--panel);
  border-top: 1px solid rgba(0, 168, 132, 0.4);
  font-size: 13px;
}
.reply-bar-info { min-width: 0; }
.reply-bar-name { color: var(--accent); font-weight: 600; }
.reply-bar-text { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.msg .meta { font-size: 11px; color: var(--muted); text-align: right; margin-top: 3px; }
.ticks { font-weight: 400; }
.tick { color: var(--muted); }
.tick.read { color: #53bdeb; }

.reply-btn {
  position: absolute;
  top: -8px;
  right: -4px;
  width: 24px;
  height: 24px;
  border: none;
  border-radius: 50%;
  background: var(--panel-2);
  color: var(--text);
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 0;
  z-index: 5;
}
.msg:hover .reply-btn { display: inline-flex; }

.reply-preview {
  font-size: 12px;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.05);
  border-left: 3px solid var(--accent);
  border-radius: 4px;
  padding: 3px 6px;
  margin-bottom: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.msg .media {
  max-width: 100%;
  max-height: 320px;
  border-radius: 6px;
  display: block;
  margin-bottom: 4px;
}
.msg .sticker {
  width: 120px;
  height: 120px;
  object-fit: contain;
  margin-bottom: 0;
}
.media-missing {
  color: var(--muted);
  font-size: 12px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 6px;
  padding: 6px 8px;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 4px;
}
.msg .caption { margin-bottom: 2px; }
.msg .mention { color: #53bdeb; }
.msg a { color: #53bdeb; text-decoration: none; }
.msg a:hover { text-decoration: underline; }
.msg code {
  background: rgba(255, 255, 255, 0.12);
  border-radius: 4px;
  padding: 1px 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
}
.msg pre {
  background: rgba(255, 255, 255, 0.08);
  border-radius: 6px;
  padding: 6px 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 12px;
  white-space: pre-wrap;
  word-wrap: break-word;
  margin: 2px 0;
}
.msg b, .msg i, .msg s { unicode-bidi: plaintext; }
.emoji {
  width: 1em;
  height: 1em;
  vertical-align: -0.15em;
  display: inline-block;
}
.notif-result {
  position: fixed;
  bottom: 4px;
  left: 4px;
  max-width: 100%;
  background: var(--panel);
  color: var(--muted);
  font: 12px/1.3 monospace;
  padding: 4px 8px;
  border-radius: 6px;
  z-index: 200;
  opacity: 0.95;
  pointer-events: none;
  word-break: break-word;
}
  height: 1.2em;
  width: 1.2em;
  margin: 0 0.05em 0 0.1em;
  vertical-align: -0.2em;
  display: inline-block;
}
.msg.system { align-self: center; background: transparent; color: var(--muted); font-size: 12px; }
.msg-date {
  align-self: center;
  background: var(--panel-2);
  color: var(--muted);
  font-size: 12px;
  padding: 4px 12px;
  border-radius: 8px;
  margin: 8px 0;
}

.composer {
  display: flex;
  gap: 8px;
  padding: 10px 16px;
  background: var(--panel);
  align-items: center;
}
.composer input[type="text"] {
  flex: 1;
  padding: 11px 14px;
  border-radius: 8px;
  border: none;
  background: var(--panel-2);
  color: var(--text);
  font-size: 14px;
}
.composer button {
  padding: 10px 18px;
  border: none;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  font-weight: 600;
  cursor: pointer;
}
.attach-btn {
  width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 24px;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
}

.mention-dropdown {
  position: absolute;
  bottom: 70px;
  left: 16px;
  right: 16px;
  max-height: 240px;
  overflow-y: auto;
  background: var(--panel-2);
  border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
  z-index: 50;
  padding: 4px 0;
}
.mention-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 12px;
  cursor: pointer;
  font-size: 14px;
}
.mention-item:hover, .mention-item.active { background: rgba(0, 168, 132, 0.15); }
.mention-item-avatar {
  width: 30px;
  height: 30px;
  min-width: 30px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  overflow: hidden;
  text-transform: uppercase;
}
.mention-item-avatar img { width: 100%; height: 100%; object-fit: cover; }
.mention-item-sub { font-size: 12px; color: var(--muted); }

.back-btn {
  display: none;
  width: 32px;
  height: 32px;
  min-width: 32px;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  padding: 0;
}

@media (max-width: 768px) {
  .back-btn { display: inline-flex; }
  .status-bar { flex-wrap: wrap; }
  .layout { display: block; }
  .sidebar {
    width: 100%;
    height: 100%;
    min-width: 0;
    border-right: none;
  }
  .chat-pane { display: none; flex-direction: column; }
  body.chat-view .sidebar { display: none; }
  body.chat-view .chat-pane { display: flex; height: 100%; min-height: 0; }
}

/* ---------- QR / modal ---------- */
.qr-overlay,
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-overlay { z-index: 90; }
.qr-card {
  background: #fff;
  color: #111;
  border-radius: 12px;
  padding: 28px;
  width: 320px;
  text-align: center;
}
.qr-card h2 { margin-bottom: 8px; font-size: 18px; }
.qr-card p { font-size: 13px; color: #555; margin-bottom: 14px; }
.qr-card img { width: 220px; height: 220px; image-rendering: pixelated; }
.qr-card .btn { margin-top: 14px; color: #111; border-color: #ddd; }

.modal {
  background: var(--panel);
  border-radius: 12px;
  width: min(680px, 92vw);
  max-height: 84vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--panel-2);
  font-weight: 600;
}
.modal-body { padding: 12px 16px; overflow-y: auto; }
.audit-row {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 6px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  font-size: 13px;
}
.audit-row .who { color: var(--accent); font-weight: 600; }
.audit-row .when { color: var(--muted); white-space: nowrap; }

.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 0;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}
.user-row .tag { font-size: 11px; padding: 2px 8px; border-radius: 8px; background: var(--panel-2); color: var(--muted); }
.user-row .tag.admin { background: rgba(0, 168, 132, 0.2); color: var(--accent); }
.form-row { display: flex; gap: 8px; margin-bottom: 10px; }
.form-row input, .form-row select {
  flex: 1;
  padding: 8px 10px;
  border-radius: 6px;
  border: 1px solid var(--panel-2);
  background: var(--panel-2);
  color: var(--text);
  font-size: 13px;
}
.qr-card {
  background: #fff;
  color: #111;
  border-radius: 12px;
  padding: 28px;
  width: 320px;
  text-align: center;
}
.qr-card h2 { margin-bottom: 8px; font-size: 18px; }
.qr-card p { font-size: 13px; color: #555; margin-bottom: 14px; }
.qr-card img { width: 220px; height: 220px; image-rendering: pixelated; }
.qr-card .btn { margin-top: 14px; color: #111; border-color: #ddd; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel-2);
  color: var(--text);
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 14px;
  z-index: 200;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-thumb { background: var(--panel-2); border-radius: 3px; }
