* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
}

:root {
  --bg-dark: #090e17;
  --bg-card: #151f30;
  --bg-surface: #1e2b42;
  --bg-input: #0e1726;
  --accent: #3b82f6;
  --accent-glow: #60a5fa;
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --danger: #ef4444;
  --success: #22c55e;
}

html, body {
  background-color: var(--bg-dark);
  color: var(--text-main);
  height: 100%;
  height: 100dvh;
  width: 100%;
  overflow: hidden;
  position: fixed;
  inset: 0;
}

/* =========================================================
   MOBILE FIRST LAYOUT (Default: Vertical Column App)
   ========================================================= */
.chat-app {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: 100dvh;
  width: 100%;
  background: var(--bg-dark);
  position: relative;
  overflow: hidden;
}

.desktop-sidebar {
  display: none !important;
}

.chat-main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100%;
  min-height: 0;
  min-width: 0;
  position: relative;
  overflow: hidden;
}

/* Header Topbar */
.app-topbar {
  padding: calc(0.6rem + env(safe-area-inset-top)) 1rem 0.6rem;
  background: var(--bg-card);
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  flex-shrink: 0;
  z-index: 10;
}
.topbar-left { display: flex; align-items: center; gap: 0.7rem; }
.topbar-info h2 { font-size: 1.05rem; }
.topbar-subtitle { font-size: 0.75rem; color: var(--accent-glow); }
.topbar-actions { display: flex; gap: 0.4rem; }
.icon-btn {
  background: var(--bg-surface);
  border: 1px solid rgba(255,255,255,0.05);
  width: 36px;
  height: 36px;
  border-radius: 10px;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Messages Stream */
.messages-container {
  flex: 1;
  padding: 0.8rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  -webkit-overflow-scrolling: touch;
}

.msg-row {
  display: flex;
  gap: 0.6rem;
  max-width: 88%;
  position: relative;
}
.msg-row.self {
  align-self: flex-end;
  flex-direction: row-reverse;
}
.msg-bubble {
  padding: 0.7rem 0.95rem;
  border-radius: 16px;
  background: var(--bg-card);
  line-height: 1.4;
  word-break: break-word;
}
.msg-row.self .msg-bubble {
  background: var(--accent);
  color: white;
  border-bottom-right-radius: 4px;
}
.msg-row:not(.self) .msg-bubble {
  border-bottom-left-radius: 4px;
}
.msg-meta {
  display: flex;
  justify-content: space-between;
  font-size: 0.72rem;
  opacity: 0.75;
  margin-bottom: 0.25rem;
  gap: 0.8rem;
}
.msg-img-wrap {
  position: relative;
  margin-top: 0.4rem;
  cursor: pointer;
  border-radius: 10px;
  overflow: hidden;
}
.msg-img {
  max-width: 100%;
  max-height: 240px;
  display: block;
}

.msg-original-text {
  margin-top: 0.35rem;
  padding-top: 0.35rem;
  border-top: 1px dashed rgba(255, 255, 255, 0.2);
  font-size: 0.76rem;
  color: var(--text-muted);
  font-style: italic;
}

/* Input Bar */
.chat-status-bar { background: var(--bg-card); flex-shrink: 0; }
.upload-progress-container { padding: 0.4rem 1rem 0; display: flex; flex-direction: column; gap: 0.25rem; }
.upload-info { display: flex; justify-content: space-between; font-size: 0.75rem; color: var(--accent-glow); }
.progress-track { width: 100%; height: 4px; background: var(--bg-dark); border-radius: 4px; overflow: hidden; }
.progress-bar-fill { width: 0%; height: 100%; background: var(--accent); }
.typing-indicator { padding: 0.2rem 1rem; font-size: 0.75rem; color: var(--accent-glow); min-height: 1.1rem; }

.chat-input-bar {
  padding: 0.5rem 0.8rem;
  background: var(--bg-card);
  border-top: 1px solid rgba(255,255,255,0.05);
  display: flex;
  gap: 0.5rem;
  align-items: center;
  flex-shrink: 0;
}
.attach-btn {
  font-size: 1.35rem;
  padding: 0.4rem;
  cursor: pointer;
  color: var(--text-muted);
}
.chat-input-bar input[type="text"] {
  flex: 1;
  padding: 0.75rem 1rem;
  background: var(--bg-input);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 20px;
  color: white;
}
.send-action-btn {
  background: var(--accent);
  border: none;
  color: white;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

/* Mobile Bottom Navigation */
.app-bottom-nav {
  display: flex;
  width: 100%;
  background: var(--bg-card);
  border-top: 1px solid rgba(255,255,255,0.06);
  padding-bottom: env(safe-area-inset-bottom);
  flex-shrink: 0;
  z-index: 20;
}
.bottom-tab-btn {
  flex: 1;
  background: transparent;
  border: none;
  padding: 0.6rem 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  color: var(--text-muted);
  cursor: pointer;
}
.bottom-tab-btn.active { color: var(--accent-glow); }
.tab-icon { font-size: 1.25rem; }
.tab-label { font-size: 0.72rem; }

/* Banners */
.broadcast-banner {
  background: #dc2626;
  color: white;
  padding: 0.5rem 1rem;
  font-size: 0.85rem;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-weight: 600;
  z-index: 1000;
  flex-shrink: 0;
}
.pinned-bar {
  background: var(--bg-surface);
  padding: 0.4rem 1rem;
  font-size: 0.8rem;
  color: var(--accent-glow);
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
  flex-shrink: 0;
}

/* =========================================================
   DESKTOP SCREEN LAYOUT (>= 768px)
   ========================================================= */
@media (min-width: 768px) {
  .chat-app {
    flex-direction: row;
  }
  .desktop-sidebar {
    display: flex !important;
    width: 290px;
    background: var(--bg-card);
    border-right: 1px solid rgba(255, 255, 255, 0.06);
    flex-direction: column;
    height: 100%;
    flex-shrink: 0;
  }
  .app-bottom-nav {
    display: none !important;
  }
  .modal-sheet {
    align-items: center;
    justify-content: center;
  }
  .modal-sheet-content {
    border-radius: 20px !important;
    max-width: 520px;
    width: 100%;
    max-height: 80vh;
  }
  .messages-container {
    padding: 1.5rem;
  }
  .msg-row {
    max-width: 68%;
  }
}

.desktop-sidebar-header {
  padding: 1rem;
  display: flex;
  align-items: center;
  gap: 0.75rem;
  border-bottom: 1px solid rgba(255, 255, 255, 0.05);
}
.desktop-sidebar-nav { padding: 0.8rem; display: flex; flex-direction: column; gap: 0.3rem; }
.sidebar-nav-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  padding: 0.75rem 0.9rem;
  border-radius: 10px;
  background: transparent;
  color: var(--text-muted);
  border: none;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
  text-align: left;
}
.sidebar-nav-btn.active, .sidebar-nav-btn:hover { background: var(--bg-surface); color: white; }
.desktop-sidebar-section {
  flex: 1;
  padding: 0.5rem 0.8rem;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.sidebar-section-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.72rem;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 0.4rem 0.2rem;
}
.icon-btn-mini {
  background: var(--bg-surface);
  border: none;
  color: white;
  border-radius: 6px;
  width: 24px;
  height: 24px;
  cursor: pointer;
}
.sidebar-list { list-style: none; display: flex; flex-direction: column; gap: 0.35rem; }

/* Modals & UI Components */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(4, 7, 13, 0.88);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2000;
  padding: 1.2rem;
}
.modal-card {
  background: var(--bg-card);
  padding: 2rem;
  border-radius: 20px;
  display: flex;
  flex-direction: column;
  gap: 1.2rem;
  width: 100%;
  max-width: 380px;
}
.modal-sheet {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.75);
  z-index: 1500;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
}
.modal-sheet-content {
  background: var(--bg-card);
  border-radius: 24px 24px 0 0;
  padding: 1.2rem;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
}
.sheet-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem; }
.icon-btn-close { background: var(--bg-surface); border: none; color: var(--text-muted); width: 32px; height: 32px; border-radius: 50%; cursor: pointer; }
.sheet-body { overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 0.6rem; }

.search-box {
  padding: 0.75rem 1rem;
  border-radius: 10px;
  background: var(--bg-input);
  color: white;
  border: 1px solid rgba(255, 255, 255, 0.08);
  margin-bottom: 0.5rem;
}
.sheet-subhead { font-size: 0.75rem; text-transform: uppercase; color: var(--text-muted); }
.user-directory-list, .friends-list, .requests-list { list-style: none; display: flex; flex-direction: column; gap: 0.5rem; }
.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 0.9rem;
  background: var(--bg-surface);
  border-radius: 12px;
  gap: 0.8rem;
}
.user-row-meta { display: flex; align-items: center; gap: 0.6rem; flex: 1; overflow: hidden; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: #64748b; }
.status-dot.online { background: var(--success); }
.btn-action-small { padding: 0.4rem 0.8rem; border-radius: 8px; font-size: 0.8rem; border: none; cursor: pointer; background: var(--accent); color: white; }

.user-avatar-sm {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--bg-surface);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  font-size: 1.1rem;
  flex-shrink: 0;
}
.user-avatar-sm img { width: 100%; height: 100%; object-fit: cover; }

.profile-avatar-large {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background: var(--bg-input);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2.2rem;
  border: 2px solid var(--accent);
  overflow: hidden;
}
.profile-avatar-large img { width: 100%; height: 100%; object-fit: cover; }
.avatar-preview-wrapper { display: flex; align-items: center; gap: 1.2rem; }
.avatar-upload-label { padding: 0.5rem 1rem; font-size: 0.85rem; cursor: pointer; display: inline-block; }
.profile-section { background: var(--bg-surface); padding: 1rem; border-radius: 14px; }
.settings-form { display: flex; flex-direction: column; gap: 0.6rem; }
.settings-form input { padding: 0.75rem 1rem; border-radius: 10px; background: var(--bg-input); color: white; border: 1px solid rgba(255, 255, 255, 0.08); }

.auth-header { text-align: center; }
.app-logo { font-size: 2.5rem; }
.auth-tabs { display: flex; background: var(--bg-dark); padding: 4px; border-radius: 12px; gap: 4px; }
.tab-btn { flex: 1; padding: 0.65rem; background: transparent; border: none; border-radius: 8px; color: var(--text-muted); cursor: pointer; }
.tab-btn.active { background: var(--accent); color: #fff; }
#auth-form { display: flex; flex-direction: column; gap: 0.8rem; }
.input-group input { width: 100%; padding: 0.85rem 1rem; border-radius: 12px; border: 1px solid rgba(255,255,255,0.08); background: var(--bg-input); color: white; }
.primary-btn { padding: 0.85rem; background: var(--accent); border: none; border-radius: 12px; color: white; cursor: pointer; }
.error-msg { color: var(--danger); font-size: 0.85rem; text-align: center; }

.gif-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 0.5rem; max-height: 45vh; overflow-y: auto; margin-top: 0.5rem; }
.gif-grid img { width: 100%; height: 110px; object-fit: cover; border-radius: 8px; cursor: pointer; }
.reaction-picker-quick { display: none; position: absolute; top: -34px; right: 10px; background: var(--bg-card); border: 1px solid rgba(255, 255, 255, 0.12); border-radius: 20px; padding: 4px 8px; gap: 8px; z-index: 20; }
.msg-reactions-row { display: flex; gap: 0.35rem; margin-top: 0.35rem; flex-wrap: wrap; }
.reaction-badge { background: var(--bg-surface); border: 1px solid rgba(255, 255, 255, 0.08); border-radius: 12px; padding: 2px 7px; font-size: 0.75rem; cursor: pointer; }
.read-check { font-size: 0.72rem; color: #94a3b8; margin-left: 4px; }
.read-check.read { color: #38bdf8; font-weight: bold; }

.lang-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 0.6rem; }
.lang-option { padding: 0.75rem; background: var(--bg-surface); border: 1px solid rgba(255, 255, 255, 0.05); border-radius: 12px; color: white; cursor: pointer; text-align: left; }
.lang-option.active { background: var(--accent); }

.image-viewer-modal { position: fixed; inset: 0; background: rgba(0, 0, 0, 0.96); z-index: 3000; display: flex; flex-direction: column; }
.image-viewer-bar { display: flex; justify-content: flex-end; gap: 0.8rem; padding: 1rem; }
.viewer-btn { background: var(--bg-surface); color: white; border: 1px solid rgba(255, 255, 255, 0.1); padding: 0.5rem 1rem; border-radius: 10px; cursor: pointer; text-decoration: none; }
.image-viewer-content { flex: 1; display: flex; align-items: center; justify-content: center; }
.image-viewer-content img { max-width: 95%; max-height: 85vh; object-fit: contain; }