/* ===== Base ===== */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0; height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #0f1115; color: #e8e8ea;
  overscroll-behavior: none; -webkit-font-smoothing: antialiased;
}
a { color: #6db3ff; text-decoration: none; }
.hidden { display: none !important; }
.muted { color: #8a8f99; font-size: 13px; }
.toast {
  position: fixed; bottom: calc(20px + env(safe-area-inset-bottom)); left: 50%;
  transform: translateX(-50%);
  background: #333; color: #fff; padding: 10px 20px; border-radius: 20px;
  z-index: 9999; max-width: 90vw; font-size: 14px; box-shadow: 0 4px 12px rgba(0,0,0,.4);
}
input, button, textarea { font-family: inherit; font-size: 16px; }

/* ===== Auth / Setup ===== */
.auth-wrap { display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 20px;
  padding-top: calc(20px + env(safe-area-inset-top));
  padding-bottom: calc(20px + env(safe-area-inset-bottom)); }
.auth-card { background: #1a1d24; border-radius: 14px; padding: 28px 24px;
  width: 100%; max-width: 400px; box-shadow: 0 8px 32px rgba(0,0,0,.5); }
.auth-card h1 { margin: 0 0 6px; font-size: 22px; }
.auth-card .sub { color: #8a8f99; font-size: 14px; margin-bottom: 20px; }
.auth-card input { width: 100%; padding: 14px; margin: 8px 0;
  border-radius: 10px; border: 1px solid #2c3038;
  background: #12151a; color: #e8e8ea; }
.auth-card input:focus { outline: none; border-color: #2563eb; }
.auth-card button { width: 100%; padding: 14px; margin: 14px 0 8px;
  border-radius: 10px; border: none;
  background: #2563eb; color: #fff; font-weight: 600; cursor: pointer; }
.auth-card button:hover { background: #1e50b8; }
.auth-card button:disabled { opacity: .5; cursor: not-allowed; }
.auth-card .status { color: #8a8f99; font-size: 13px; min-height: 18px; text-align: center; }

/* ===== Chat layout ===== */
.chat-wrap { display: flex; height: 100vh; height: 100dvh; }
.sidebar {
  width: 300px; border-right: 1px solid #1f232b;
  display: flex; flex-direction: column; flex-shrink: 0;
  background: #12151a;
}
.sidebar-header {
  padding: 14px;
  padding-top: calc(14px + env(safe-area-inset-top));
  border-bottom: 1px solid #1f232b;
  position: relative;
}
.sidebar-header input {
  width: 100%; padding: 10px 12px;
  background: #0f1115; border: 1px solid #2c3038;
  color: #e8e8ea; border-radius: 8px;
}
.sidebar-user {
  padding: 10px 14px; border-bottom: 1px solid #1f232b;
  font-size: 13px; display: flex; justify-content: space-between; align-items: center;
}
.sidebar-user .name { color: #e8e8ea; font-weight: 600; }
.sidebar-user a { color: #6db3ff; font-size: 12px; }
.thread-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.empty-threads { padding: 20px; text-align: center; line-height: 1.6; }
.thread {
  padding: 14px 16px; border-bottom: 1px solid #1f232b;
  cursor: pointer; transition: background .1s;
}
.thread:hover, .thread.active { background: #1a1d24; }
.thread-name { display: block; font-weight: 500; }
.search-results {
  display: none; position: absolute; left: 14px; right: 14px; top: 100%;
  background: #1a1d24; border: 1px solid #2c3038;
  border-radius: 8px; margin-top: 4px;
  max-height: 260px; overflow-y: auto; z-index: 20;
  box-shadow: 0 4px 16px rgba(0,0,0,.5);
}
.search-results.open { display: block; }
.search-hit { padding: 12px 14px; cursor: pointer; font-size: 14px; }
.search-hit:hover { background: #2c3038; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.main-header {
  padding: 14px 16px;
  padding-top: calc(14px + env(safe-area-inset-top));
  border-bottom: 1px solid #1f232b;
  display: flex; align-items: center; gap: 10px;
  background: #12151a;
}
.main-header .back-btn {
  display: none; background: none; border: none; color: #e8e8ea;
  font-size: 22px; padding: 4px 8px; cursor: pointer;
}
#thread-title { flex: 1; font-weight: 600; font-size: 15px; }
.main-header .call-btn {
  background: none; border: 1px solid #2c3038;
  color: #e8e8ea; padding: 8px 10px; border-radius: 8px;
  cursor: pointer; font-size: 16px;
}
.main-header .call-btn:hover { background: #1a1d24; }

#messages {
  flex: 1; overflow-y: auto; padding: 16px;
  padding-bottom: calc(8px + env(safe-area-inset-bottom));
  -webkit-overflow-scrolling: touch;
  display: flex; flex-direction: column; gap: 6px;
}
.bubble {
  max-width: min(75%, 600px); padding: 10px 14px;
  border-radius: 16px; font-size: 15px; line-height: 1.4;
  word-wrap: break-word; overflow-wrap: break-word;
}
.bubble.me { align-self: flex-end; background: #2563eb; color: #fff; border-bottom-right-radius: 4px; }
.bubble.them { align-self: flex-start; background: #1f232b; color: #e8e8ea; border-bottom-left-radius: 4px; }
.bubble.media { padding: 6px; background: #1f232b; }
.bubble.media img, .bubble.media video {
  max-width: 100%; max-height: 340px; border-radius: 10px; display: block;
}
.dl-link { display: inline-block; font-size: 12px; margin-top: 6px; }

#typing {
  padding: 4px 16px; font-size: 13px; color: #8a8f99;
  min-height: 20px;
}
#composer {
  border-top: 1px solid #1f232b;
  padding: 10px 12px;
  padding-bottom: calc(10px + env(safe-area-inset-bottom));
  background: #12151a;
}
#composer form { display: flex; gap: 8px; align-items: center; }
#msg-input {
  flex: 1; padding: 12px 14px;
  background: #0f1115; border: 1px solid #2c3038;
  color: #e8e8ea; border-radius: 22px;
  font-size: 16px; /* prevents iOS zoom */
}
#msg-input:focus { outline: none; border-color: #2563eb; }
.attach-btn, .send-btn {
  background: #2563eb; color: #fff; border: none;
  width: 42px; height: 42px; border-radius: 50%;
  cursor: pointer; font-size: 18px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.attach-btn { background: #2c3038; }
.attach-btn:hover, .send-btn:hover { filter: brightness(1.15); }

/* ===== Mobile ===== */
@media (max-width: 720px) {
  .sidebar { width: 100%; border-right: none; }
  .main { display: none; }
  body.thread-open .sidebar { display: none; }
  body.thread-open .main { display: flex; }
  .main-header .back-btn { display: block; }
  .bubble { max-width: 85%; }
  .bubble.media img, .bubble.media video { max-height: 260px; }
}

/* ===== Admin dashboard ===== */
.admin-wrap { max-width: 1100px; margin: 0 auto; padding: 24px;
  padding-top: calc(24px + env(safe-area-inset-top));
  padding-bottom: calc(24px + env(safe-area-inset-bottom)); }
.admin-wrap h1 { margin-top: 0; font-size: 22px; }
.admin-wrap h2 { font-size: 17px; margin-top: 32px; color: #c9ccd1; }
.card { background: #1a1d24; border-radius: 12px; padding: 20px; margin-top: 14px; }
.card label { display: block; font-size: 13px; color: #8a8f99; margin-bottom: 6px; }
.card input, .card select {
  width: 100%; padding: 12px; margin-bottom: 10px;
  background: #12151a; border: 1px solid #2c3038; color: #e8e8ea;
  border-radius: 8px;
}
.card button {
  background: #2563eb; color: #fff; border: none;
  padding: 12px 20px; border-radius: 8px; font-weight: 600; cursor: pointer;
}
.card button:disabled { opacity: .5; cursor: not-allowed; }
.card .result { margin-top: 12px; font-size: 13px; word-break: break-all; }
.users-table { width: 100%; border-collapse: collapse; }
.users-table th, .users-table td {
  padding: 10px 12px; text-align: left; font-size: 13px;
  border-bottom: 1px solid #2c3038;
}
.users-table th { color: #8a8f99; font-weight: 500; }
.btn-toggle {
  background: #2c3038; color: #e8e8ea; border: none;
  padding: 6px 12px; border-radius: 6px; cursor: pointer; font-size: 12px;
}
.danger-zone { border: 1px solid #7a1f1f; background: #2a1414; }
.danger-zone h2 { color: #ff5c5c; }
.danger-btn {
  background: #d32f2f !important; color: #fff;
  padding: 12px 20px; border: none; border-radius: 8px;
  font-weight: 600; cursor: pointer;
}

/* Mobile admin table → cards */
@media (max-width: 720px) {
  .users-table thead { display: none; }
  .users-table, .users-table tbody, .users-table tr, .users-table td { display: block; width: 100%; }
  .users-table tr { background: #12151a; border-radius: 10px; margin-bottom: 10px; padding: 12px; }
  .users-table td { border: none; padding: 4px 0; }
  .users-table td::before {
    content: attr(data-label) ": ";
    color: #8a8f99; font-weight: 500; display: inline-block; min-width: 80px;
  }
}

/* ===== Call overlay ===== */
#call-overlay {
  display: none; position: fixed; inset: 0;
  background: rgba(0,0,0,.92); z-index: 1000;
  align-items: center; justify-content: center;
  padding: 20px;
}
#call-overlay[style*="flex"] { display: flex; }
.call-panel {
  background: #1a1d24; border-radius: 14px; padding: 20px;
  max-width: 500px; width: 100%; text-align: center;
  position: relative;
}
.call-panel #call-remote {
  width: 100%; max-height: 60vh; background: #000;
  border-radius: 10px; margin: 12px 0;
}
.call-panel #call-local {
  position: absolute; bottom: 90px; right: 30px;
  width: 110px; height: 82px; background: #000;
  border-radius: 8px; border: 2px solid #2563eb;
  object-fit: cover;
}
.call-title { font-size: 15px; font-weight: 600; margin-bottom: 6px; }
.call-actions { margin-top: 10px; }
.call-actions .danger {
  background: #d32f2f; color: #fff; border: none;
  padding: 12px 24px; border-radius: 8px; font-weight: 600;
  cursor: pointer; font-size: 15px;
}

/* Sidebar Badges and Typing */
.badge {
  background: #2563eb;
  color: #fff;
  border-radius: 12px;
  padding: 2px 8px;
  font-size: 11px;
  font-weight: bold;
}
.typing-indicator {
  display: block;
  color: #6db3ff;
  font-size: 12px;
  margin-top: 4px;
  font-style: italic;
}

/* Pull to refresh styles */
.ptr-container {
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 60px;
  display: flex;
  justify-content: center;
  align-items: center;
  transform: translateY(-60px);
  transition: transform 0.2s ease-out;
  z-index: 100;
  pointer-events: none;
}
.ptr-spinner {
  width: 24px;
  height: 24px;
  border: 3px solid rgba(255,255,255,0.2);
  border-top-color: #2563eb;
  border-radius: 50%;
  animation: ptr-spin 1s linear infinite;
}
@keyframes ptr-spin {
  to { transform: rotate(360deg); }
}
/* Make chat area relative for the absolute spinner */
.main { position: relative; }
