:root {
  color-scheme: dark;
  --bg: #0f1115;
  --bg2: #151922;
  --bg3: #1c2130;
  --line: #262c3a;
  --text: #e6e8ec;
  --muted: #9ba3b3;
  --accent: #5b9dff;
  --accent2: #3b7ee0;
  --danger: #ef476f;
  --ok: #22c55e;
  --owner: #f5c542;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", "SF Pro Text",
               "Noto Sans Arabic", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
button, input, textarea, select {
  font: inherit; color: inherit;
  background: var(--bg3); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 12px; outline: none;
}
button { cursor: pointer; background: var(--accent); border-color: transparent; }
button:hover { background: var(--accent2); }
button.ghost { background: transparent; border-color: var(--line); }
button.danger { background: var(--danger); }
a { color: var(--accent); }

#app { display: flex; flex-direction: column; height: 100dvh; }

/* Auth screen */
.auth {
  display: grid; place-items: center; height: 100%;
  background: radial-gradient(1200px 500px at 50% -100px, #1c2842, transparent), var(--bg);
}
.auth .card {
  background: var(--bg2); padding: 24px; border: 1px solid var(--line);
  border-radius: var(--radius); width: min(420px, 92vw);
}
.auth h1 { margin: 0 0 4px; font-size: 22px; }
.auth p { color: var(--muted); margin: 4px 0 16px; }
.auth label { display: block; font-size: 12px; color: var(--muted); margin: 10px 0 4px; }
.auth input { width: 100%; }
.auth .row { display: flex; gap: 8px; margin-top: 16px; }
.auth .row button { flex: 1; }
.auth .warn {
  margin-top: 16px; padding: 10px 12px; border-radius: 8px;
  background: #3a2f10; border: 1px solid #6a5223; color: #f5c542; font-size: 13px;
}

/* Main layout */
.main { display: grid; grid-template-columns: 340px 1fr; height: 100dvh; }
@media (max-width: 780px) { .main { grid-template-columns: 100%; }
  .main.chat-open .sidebar { display: none; }
  .main:not(.chat-open) .conv { display: none; } }
.sidebar { border-left: 1px solid var(--line); display: flex; flex-direction: column; background: var(--bg2); }
.sidebar header {
  padding: 12px; border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 10px; justify-content: space-between;
}
.me { display: flex; align-items: center; gap: 8px; min-width: 0; }
.me .avatar { width: 32px; height: 32px; border-radius: 50%; background: var(--bg3); display: grid; place-items: center; font-weight: 700; }
.me strong { max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.badge-owner { background: var(--owner); color: #000; font-size: 10px; padding: 2px 6px; border-radius: 4px; margin-inline-start: 6px; }
.chatlist { flex: 1; overflow: auto; }
.chatitem {
  display: flex; gap: 10px; align-items: center; padding: 10px 12px; cursor: pointer;
  border-bottom: 1px solid var(--line);
}
.chatitem:hover, .chatitem.active { background: var(--bg3); }
.chatitem .avatar { width: 40px; height: 40px; border-radius: 50%; background: var(--bg3); display: grid; place-items: center; }
.chatitem .body { flex: 1; min-width: 0; }
.chatitem .body .name { display: flex; justify-content: space-between; gap: 8px; }
.chatitem .body .name strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chatitem .body .name span { color: var(--muted); font-size: 11px; }
.chatitem .body .last { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chatitem .unread { background: var(--accent); color: #000; font-weight: 700; border-radius: 12px; padding: 1px 8px; font-size: 12px; }

.actions { padding: 8px; display: flex; gap: 6px; border-bottom: 1px solid var(--line); background: var(--bg2); }
.actions button { flex: 1; }
.actions .adminbtn { background: var(--owner); color: #000; }

/* Conversation */
.conv { display: flex; flex-direction: column; background: var(--bg); min-width: 0; }
.conv header {
  padding: 12px 16px; border-bottom: 1px solid var(--line); background: var(--bg2);
  display: flex; align-items: center; gap: 10px;
}
.conv header .avatar { width: 40px; height: 40px; border-radius: 50%; background: var(--bg3); display: grid; place-items: center; }
.conv header .title { flex: 1; min-width: 0; }
.conv header .title .name { font-weight: 700; }
.conv header .title .sub { color: var(--muted); font-size: 12px; }

.messages { flex: 1; overflow: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 78%; padding: 8px 12px; border-radius: 12px; background: var(--bg2); border: 1px solid var(--line); word-wrap: break-word; }
.msg.mine { align-self: flex-end; background: #1e3a6b; border-color: #2a4880; }
.msg .who { font-size: 11px; color: var(--muted); margin-bottom: 2px; }
.msg .when { font-size: 10px; color: var(--muted); margin-top: 4px; text-align: end; }
.msg.deleted { opacity: 0.6; font-style: italic; }
.msg img { max-width: 100%; border-radius: 8px; display: block; }
.msg audio, .msg video { max-width: 100%; }
.msg .reply {
  border-inline-start: 3px solid var(--accent); padding-inline-start: 8px;
  margin-bottom: 4px; color: var(--muted); font-size: 12px; opacity: 0.9;
}
.system {
  align-self: center; background: transparent; border: none; color: var(--muted); font-size: 12px;
}
.typing { align-self: flex-start; color: var(--muted); font-size: 12px; padding: 4px 8px; }

.composer {
  padding: 8px; border-top: 1px solid var(--line); background: var(--bg2);
  display: flex; gap: 6px; align-items: end;
}
.composer textarea {
  flex: 1; resize: none; min-height: 40px; max-height: 160px; overflow: auto;
  border-radius: 20px; padding: 10px 14px;
}
.composer button { border-radius: 20px; }
.composer .icon { min-width: 40px; padding: 8px; }
.rec { background: var(--danger) !important; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: 0.7; } }

.overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.6); display: grid; place-items: center; z-index: 20; }
.modal { background: var(--bg2); border: 1px solid var(--line); border-radius: 12px; padding: 20px; width: min(500px, 92vw); max-height: 85vh; overflow: auto; }
.modal h3 { margin: 0 0 12px; }
.modal .row { display: flex; gap: 8px; margin-top: 12px; }

.toast {
  position: fixed; bottom: 20px; inset-inline-start: 50%; transform: translateX(50%);
  background: var(--danger); color: #fff; padding: 8px 14px; border-radius: 8px; z-index: 100;
}
