/* css/app.css */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f0f1a;
  --bg2: #1a1a2e;
  --bg3: #16213e;
  --surface: #1e1e35;
  --surface2: #252540;
  --border: rgba(255,255,255,0.08);
  --accent: #4f46e5;
  --accent-h: #6366f1;
  --text: #e8e8f0;
  --text2: #9090b0;
  --text3: #606080;
  --danger: #ef4444;
  --success: #22c55e;
  --warning: #f59e0b;
  --radius: 12px;
  --sidebar-w: 320px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

html, body { height: 100%; overflow: hidden; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: var(--bg); color: var(--text); font-size: 15px; line-height: 1.5; }

/* ── Layout ── */
#app { display: flex; height: 100vh; height: 100dvh; }

aside#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  padding-top: env(safe-area-inset-top);
}

main#chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
}

/* ── Sidebar ── */
.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 14px 12px;
  padding-top: max(16px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
}
.sidebar-brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 16px; }
.sidebar-actions { display: flex; gap: 4px; }

.search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 10px 12px;
  padding: 8px 12px;
  background: var(--surface);
  border-radius: 8px;
  color: var(--text2);
}
.search-bar input {
  background: none;
  border: none;
  outline: none;
  color: var(--text);
  flex: 1;
  font-size: 14px;
}

#conv-list { flex: 1; overflow-y: auto; }

.conv-item {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 11px 14px;
  cursor: pointer;
  border-left: 3px solid transparent;
  transition: background .15s;
  position: relative;
}
.conv-item:hover { background: var(--surface); }
.conv-item.active { background: var(--surface); border-left-color: var(--accent); }
.conv-item .conv-meta { flex: 1; min-width: 0; }
.conv-item .conv-name { font-weight: 500; font-size: 14px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv-item .conv-preview { font-size: 12px; color: var(--text2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-top: 2px; }
.conv-item .conv-time { font-size: 11px; color: var(--text3); white-space: nowrap; }
.unread-badge {
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 600;
  border-radius: 10px;
  padding: 1px 6px;
  min-width: 18px;
  text-align: center;
}

.sidebar-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  padding-bottom: calc(10px + var(--safe-bottom));
}
.user-pill {
  display: flex;
  align-items: center;
  gap: 9px;
  cursor: pointer;
  border-radius: 8px;
  padding: 4px 6px;
  transition: background .15s;
  flex: 1;
}
.user-pill:hover { background: var(--surface); }

/* ── Avatar ── */
.avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 15px;
  flex-shrink: 0;
  overflow: hidden;
  color: white;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; }
.avatar.sm { width: 30px; height: 30px; font-size: 12px; }
.avatar.lg { width: 48px; height: 48px; font-size: 20px; }

/* ── Status dot ── */
.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  border: 2px solid var(--bg2);
  background: var(--text3);
}
.status-dot.online { background: var(--success); }
.status-dot.away   { background: var(--warning); }

/* ── Chat header ── */
#chat-header {
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 12px 16px;
  padding-top: max(12px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
  flex-shrink: 0;
}
#chat-header .hdr-info { flex: 1; min-width: 0; }
#chat-header .hdr-name { font-weight: 600; font-size: 15px; }
#chat-header .hdr-sub  { font-size: 12px; color: var(--text2); }
#chat-header .hdr-actions { display: flex; gap: 4px; }

/* ── Messages ── */
#messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 16px 8px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
#messages::-webkit-scrollbar { width: 4px; }
#messages::-webkit-scrollbar-thumb { background: var(--surface2); border-radius: 4px; }

.msg-group { margin: 6px 0; }
.msg-date-sep {
  text-align: center;
  font-size: 12px;
  color: var(--text3);
  margin: 14px 0 8px;
  position: relative;
}
.msg-date-sep::before, .msg-date-sep::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 40%;
  height: 1px;
  background: var(--border);
}
.msg-date-sep::before { left: 0; }
.msg-date-sep::after  { right: 0; }

.msg-row {
  display: flex;
  gap: 8px;
  align-items: flex-end;
  max-width: 72%;
  margin-bottom: 2px;
}
.msg-row.own { flex-direction: row-reverse; margin-left: auto; }
.msg-row.own .msg-avatar { display: none; }

.msg-bubble {
  background: var(--surface);
  border-radius: 16px 16px 16px 4px;
  padding: 9px 13px;
  position: relative;
  max-width: 100%;
  word-break: break-word;
  font-size: 14px;
}
.msg-row.own .msg-bubble {
  background: var(--accent);
  border-radius: 16px 16px 4px 16px;
}

.msg-sender { font-size: 11px; color: var(--accent-h); font-weight: 600; margin-bottom: 3px; }
.msg-time {
  font-size: 10px;
  color: rgba(255,255,255,0.4);
  text-align: right;
  margin-top: 4px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
}
.msg-status svg { width: 14px; height: 14px; opacity: .7; }

/* Media messages */
.msg-image { max-width: 260px; max-height: 220px; border-radius: 10px; cursor: pointer; display: block; }
.msg-video { max-width: 280px; border-radius: 10px; display: block; }
.msg-file {
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(0,0,0,0.2);
  border-radius: 8px;
  padding: 10px 12px;
  min-width: 180px;
  cursor: pointer;
}
.msg-file svg { flex-shrink: 0; opacity: .7; }
.msg-file .file-name { font-size: 13px; font-weight: 500; }
.msg-file .file-size { font-size: 11px; opacity: .6; }

/* System / call messages */
.msg-system {
  text-align: center;
  font-size: 12px;
  color: var(--text3);
  padding: 4px 0;
}
.msg-reply-ref {
  border-left: 3px solid var(--accent-h);
  padding: 4px 8px;
  margin-bottom: 6px;
  font-size: 12px;
  opacity: .7;
  border-radius: 0 6px 6px 0;
  background: rgba(0,0,0,0.15);
}

/* ── Input area ── */
#input-area {
  padding: 10px 14px;
  padding-bottom: calc(10px + var(--safe-bottom));
  border-top: 1px solid var(--border);
  background: var(--bg2);
  display: flex;
  gap: 8px;
  align-items: flex-end;
}
#reply-preview {
  padding: 6px 12px;
  background: var(--surface);
  border-left: 3px solid var(--accent);
  font-size: 13px;
  color: var(--text2);
  display: flex;
  justify-content: space-between;
  align-items: center;
}
#msg-input {
  flex: 1;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 9px 14px;
  color: var(--text);
  font-size: 14px;
  resize: none;
  outline: none;
  max-height: 120px;
  overflow-y: auto;
  line-height: 1.4;
  font-family: inherit;
}
#msg-input:focus { border-color: var(--accent); }
#msg-input::placeholder { color: var(--text3); }

.input-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  border: none;
  background: var(--surface);
  color: var(--text2);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  transition: background .15s, color .15s;
}
.input-btn:hover { background: var(--surface2); color: var(--text); }
.input-btn.send { background: var(--accent); color: #fff; }
.input-btn.send:hover { background: var(--accent-h); }
#file-input { display: none; }

/* ── Empty state ── */
.empty-state { align-items: center; justify-content: center; }
.empty-msg { color: var(--text3); text-align: center; }
.empty-msg p { margin-top: 12px; font-size: 14px; }

/* ── Buttons ── */
button { font-family: inherit; cursor: pointer; }
.icon-btn {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  border: none;
  background: none;
  color: var(--text2);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background .15s;
}
.icon-btn:hover { background: var(--surface); color: var(--text); }

.btn {
  padding: 10px 20px;
  border-radius: 8px;
  border: none;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background .15s;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-h); }
.btn-full { width: 100%; }
.btn-danger { background: var(--danger); color: #fff; }

/* ── Modals ── */
#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}
#modal-overlay.hidden { display: none; }
#modal-box {
  background: var(--bg2);
  border-radius: var(--radius);
  width: min(480px, 95vw);
  max-height: 85vh;
  overflow-y: auto;
  border: 1px solid var(--border);
}
#modal-content { padding: 24px; }
.modal-title { font-size: 17px; font-weight: 600; margin-bottom: 18px; }
.modal-close { float: right; }

/* ── Forms ── */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--text2); margin-bottom: 6px; }
.field input, .field textarea, .field select {
  width: 100%;
  padding: 9px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  font-size: 14px;
  outline: none;
  font-family: inherit;
}
.field input:focus, .field textarea:focus { border-color: var(--accent); }

.alert {
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 14px;
  margin-bottom: 14px;
}
.alert-error   { background: rgba(239,68,68,.15); color: #fca5a5; border: 1px solid rgba(239,68,68,.3); }
.alert-success { background: rgba(34,197,94,.15); color: #86efac; border: 1px solid rgba(34,197,94,.3); }

/* ── Auth pages ── */
.auth-body { background: var(--bg); display: flex; align-items: center; justify-content: center; height: 100vh; overflow: auto; }
.auth-box {
  background: var(--bg2);
  border-radius: var(--radius);
  padding: 32px;
  width: min(400px, 95vw);
  border: 1px solid var(--border);
}
.auth-logo { display: flex; align-items: center; gap: 12px; margin-bottom: 28px; }
.auth-logo h1 { font-size: 22px; font-weight: 700; }
.auth-hint { font-size: 13px; color: var(--text3); text-align: center; margin-top: 16px; }

/* ── Call UI ── */
#call-ui {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: #000;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
#call-ui.hidden { display: none; }
#call-videos { position: absolute; inset: 0; }
#video-remote { width: 100%; height: 100%; object-fit: cover; }
#video-local {
  position: absolute;
  bottom: 100px;
  right: 16px;
  width: 120px;
  height: 90px;
  border-radius: 10px;
  object-fit: cover;
  border: 2px solid rgba(255,255,255,.3);
}
#call-info { position: absolute; top: 40px; color: #fff; text-align: center; }
#call-name { font-size: 22px; font-weight: 600; }
#call-status { font-size: 14px; opacity: .7; margin-top: 6px; }
#call-controls {
  position: absolute;
  bottom: 30px;
  display: flex;
  gap: 20px;
  padding-bottom: var(--safe-bottom);
}
.call-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  border: none;
  background: rgba(255,255,255,0.2);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(8px);
  transition: background .15s;
}
.call-btn:hover { background: rgba(255,255,255,0.35); }
.call-btn.danger { background: var(--danger); }
.call-btn.active { background: var(--accent); }
#incoming-call {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 30px;
}
#incoming-call.hidden { display: none; }
#incoming-info { text-align: center; color: #fff; }
#incoming-avatar { width: 72px; height: 72px; border-radius: 50%; background: var(--accent); display: flex; align-items: center; justify-content: center; font-size: 28px; font-weight: 700; margin: 0 auto 14px; }
#incoming-name { font-size: 22px; font-weight: 600; }
#incoming-type { font-size: 14px; opacity: .7; margin-top: 4px; }
#incoming-actions { display: flex; gap: 40px; }

/* ── Typing indicator ── */
.typing-indicator {
  display: flex;
  gap: 4px;
  padding: 10px 14px;
  background: var(--surface);
  border-radius: 16px 16px 16px 4px;
  width: fit-content;
  margin: 4px 0 4px 46px;
}
.typing-indicator span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text3);
  animation: bounce .9s infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: .15s; }
.typing-indicator span:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { 0%,60%,100% { transform: translateY(0); } 30% { transform: translateY(-6px); } }

/* ── Lightbox ── */
#lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.9);
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
}
#lightbox.hidden { display: none; }
#lightbox img { max-width: 95vw; max-height: 90vh; border-radius: 8px; }
#lightbox-close { position: absolute; top: 16px; right: 16px; color: #fff; font-size: 28px; cursor: pointer; background: none; border: none; }

/* ── User list in modal ── */
.user-list { display: flex; flex-direction: column; gap: 6px; }
.user-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 8px;
  cursor: pointer;
  transition: background .15s;
}
.user-row:hover { background: var(--surface); }
.user-row .uname { font-size: 14px; font-weight: 500; }
.user-row .ustatus { font-size: 12px; color: var(--text2); }

/* ── Admin panel ── */
.invite-row { display: flex; align-items: center; gap: 8px; padding: 8px 10px; background: var(--surface); border-radius: 8px; font-size: 13px; margin-bottom: 6px; }
.invite-url { flex: 1; word-break: break-all; color: var(--text2); }
.invite-copy { cursor: pointer; color: var(--accent); flex-shrink: 0; font-size: 12px; }

/* ── Responsive mobile ── */
@media (max-width: 640px) {
  :root { --sidebar-w: 100vw; }
  #app { position: relative; }
  aside#sidebar { position: absolute; left: 0; top: 0; bottom: 0; z-index: 10; transform: translateX(0); transition: transform .25s; }
  aside#sidebar.hidden { transform: translateX(-100%); }
  main#chat-area { position: absolute; inset: 0; }
  #chat-header { padding-top: env(safe-area-inset-top, 12px); }
}
.hidden { display: none !important; }
