/* ============================================================
   ChatApp — Main Stylesheet
   ============================================================ */

:root {
  --accent:        #6c63ff;
  --accent-hover:  #5a52d5;
  --accent-light:  rgba(108,99,255,0.15);
  --bg:            #0f0f1a;
  --bg2:           #181828;
  --bg3:           #1f1f35;
  --border:        rgba(255,255,255,0.07);
  --text:          #e8e8f0;
  --text-muted:    rgba(232,232,240,0.45);
  --sidebar-w:     280px;
  --header-h:      60px;
  --compose-h:     76px;
  --radius:        12px;
  --shadow:        0 4px 24px rgba(0,0,0,0.4);
  --transition:    0.18s ease;
}

[data-theme="light"] {
  --bg:         #f5f5f8;
  --bg2:        #ffffff;
  --bg3:        #ededf4;
  --border:     rgba(0,0,0,0.08);
  --text:       #1a1a2e;
  --text-muted: rgba(26,26,46,0.5);
  --shadow:     0 4px 24px rgba(0,0,0,0.08);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { height: 100%; }
body {
  height: 100%;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
  transition: background var(--transition), color var(--transition);
}

/* ---- Scrollbar ---- */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ============================================================
   App Shell
   ============================================================ */
.app-shell {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ============================================================
   Sidebar
   ============================================================ */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100%;
  transition: transform var(--transition);
  z-index: 200;
}

.sidebar-header {
  height: var(--header-h);
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-user {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.sidebar-search {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.search-addon { background: var(--bg3) !important; border-color: var(--border) !important; color: var(--text-muted) !important; }
.search-input { background: var(--bg3) !important; border-color: var(--border) !important; color: var(--text) !important; font-size: .875rem; }
.search-input:focus { box-shadow: none !important; border-color: var(--accent) !important; }
.search-input::placeholder { color: var(--text-muted) !important; }

.sidebar-scroll {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.sidebar-section { margin-bottom: 8px; }

.sidebar-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 16px 4px;
  font-size: .7rem;
  font-weight: 700;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.btn-section-action {
  color: var(--text-muted);
  background: none;
  border: none;
  padding: 2px 4px;
  border-radius: 4px;
  cursor: pointer;
  text-decoration: none;
  line-height: 1;
  transition: color var(--transition);
}
.btn-section-action:hover { color: var(--accent); }

.nav-list { list-style: none; padding: 0; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 16px;
  text-decoration: none;
  color: var(--text);
  border-radius: 0;
  cursor: pointer;
  position: relative;
  transition: background var(--transition);
}
.nav-item:hover { background: var(--bg3); color: var(--text); }
.nav-item.active {
  background: var(--accent-light);
  color: var(--accent);
}
.nav-item.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 4px;
  bottom: 4px;
  width: 3px;
  background: var(--accent);
  border-radius: 0 3px 3px 0;
}

.nav-item-name { font-size: .875rem; font-weight: 500; line-height: 1.2; }
.nav-item-sub { font-size: .75rem; color: var(--text-muted); line-height: 1.2; margin-top: 1px; }
.nav-item-badge {
  min-width: 18px;
  height: 18px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font-size: .65rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
  margin-left: auto;
  flex-shrink: 0;
}

/* ============================================================
   Avatars
   ============================================================ */
.avatar-xs {
  width: 24px; height: 24px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: .6rem; font-weight: 700; color: #fff;
  flex-shrink: 0;
}
.avatar-sm {
  width: 32px; height: 32px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: .75rem; font-weight: 700; color: #fff;
  flex-shrink: 0; position: relative;
}
.avatar-md {
  width: 38px; height: 38px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: .9rem; font-weight: 700; color: #fff;
  flex-shrink: 0;
}
.avatar-lg {
  width: 56px; height: 56px; border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.2rem; font-weight: 700; color: #fff;
}
.avatar-group {
  background: var(--accent-light) !important;
  color: var(--accent) !important;
  font-weight: 800;
  font-size: .9rem;
}

.online-dot {
  position: absolute;
  bottom: 1px; right: 1px;
  width: 8px; height: 8px;
  border-radius: 50%;
  background: #2ecc71;
  border: 2px solid var(--bg2);
}

/* ============================================================
   Chat Main
   ============================================================ */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  min-width: 0;
}

.welcome-screen {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.welcome-content {
  text-align: center;
  max-width: 320px;
}
.welcome-icon {
  font-size: 4rem;
  color: var(--accent);
  margin-bottom: 16px;
  animation: float 3s ease-in-out infinite;
}
@keyframes float {
  0%,100% { transform: translateY(0); }
  50%      { transform: translateY(-8px); }
}

.chat-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* ---- Chat Header ---- */
.chat-header {
  height: var(--header-h);
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
  flex-shrink: 0;
}

/* ---- Messages ---- */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  scroll-behavior: smooth;
}

.messages-loading {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 40px;
}

.message-group {
  display: flex;
  gap: 8px;
  margin-bottom: 2px;
  animation: msgIn 0.2s ease;
}
@keyframes msgIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.message-group.mine {
  flex-direction: row-reverse;
}
.message-group.mine .msg-avatar { display: none; }
.message-group.mine .msg-meta { text-align: right; }

.msg-avatar { flex-shrink: 0; align-self: flex-end; margin-bottom: 2px; }

.msg-content { max-width: 70%; display: flex; flex-direction: column; }

.msg-meta {
  font-size: .7rem;
  color: var(--text-muted);
  margin-bottom: 3px;
  padding: 0 4px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.msg-bubble {
  padding: 9px 13px;
  border-radius: 16px;
  font-size: .875rem;
  line-height: 1.5;
  word-break: break-word;
  background: var(--bg3);
  color: var(--text);
  transition: background var(--transition);
}
.msg-bubble a { color: var(--accent); text-decoration: underline; }

.message-group.mine .msg-bubble {
  background: var(--accent);
  color: #fff;
  border-radius: 16px 16px 4px 16px;
}
.message-group:not(.mine) .msg-bubble {
  border-radius: 4px 16px 16px 16px;
}

.msg-time {
  font-size: .68rem;
  color: var(--text-muted);
  padding: 2px 4px;
  align-self: flex-end;
}
.message-group.mine .msg-time { align-self: flex-start; }

/* ---- Date divider ---- */
.date-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 12px 0;
  color: var(--text-muted);
  font-size: .72rem;
  font-weight: 600;
}
.date-divider::before, .date-divider::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* ---- Compose ---- */
.compose-area {
  border-top: 1px solid var(--border);
  background: var(--bg2);
  padding: 10px 12px;
  flex-shrink: 0;
}

.typing-indicator {
  font-size: .75rem;
  color: var(--text-muted);
  margin-bottom: 6px;
  display: flex;
  align-items: center;
}
.dot {
  width: 5px; height: 5px;
  border-radius: 50%;
  background: var(--text-muted);
  margin: 0 1px;
  animation: dotBounce 1.2s ease-in-out infinite;
}
.dot:nth-child(2) { animation-delay: .2s; }
.dot:nth-child(3) { animation-delay: .4s; }
@keyframes dotBounce {
  0%,80%,100% { transform: translateY(0); }
  40%         { transform: translateY(-4px); }
}

.compose-form { flex: 1; }
.compose-inner {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg3);
  border-radius: var(--radius);
  padding: 4px 4px 4px 12px;
  border: 1px solid var(--border);
  transition: border-color var(--transition);
}
.compose-inner:focus-within { border-color: var(--accent); }

.compose-input {
  flex: 1;
  background: none;
  border: none;
  outline: none;
  color: var(--text);
  font-size: .875rem;
  resize: none;
  max-height: 120px;
  padding: 6px 0;
  line-height: 1.5;
  font-family: inherit;
}
.compose-input::placeholder { color: var(--text-muted); }

.compose-actions { display: flex; align-items: flex-end; gap: 4px; padding-bottom: 2px; }

.char-count { font-size: .7rem; color: var(--text-muted); padding: 0 4px; }

.btn-send {
  width: 36px; height: 36px;
  border-radius: 10px;
  border: none;
  background: var(--accent);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), transform var(--transition);
  cursor: pointer;
}
.btn-send:hover:not(:disabled) { background: var(--accent-hover); transform: scale(1.05); }
.btn-send:disabled { background: var(--bg); color: var(--text-muted); cursor: not-allowed; }

/* ============================================================
   Buttons & Controls
   ============================================================ */
.btn-icon {
  width: 32px; height: 32px;
  border-radius: 8px;
  border: none;
  background: none;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background var(--transition), color var(--transition);
  text-decoration: none;
  font-size: 1rem;
}
.btn-icon:hover { background: var(--bg3); color: var(--text); }

.btn-accent {
  background: var(--accent) !important;
  border-color: var(--accent) !important;
  color: #fff !important;
  transition: background var(--transition);
}
.btn-accent:hover { background: var(--accent-hover) !important; border-color: var(--accent-hover) !important; }

.btn-ghost { background: none; border: none; color: var(--text-muted); cursor: pointer; }
.btn-ghost:hover { color: var(--text); }

.btn-xs {
  padding: .2rem .45rem;
  font-size: .75rem;
  line-height: 1.4;
  border-radius: 6px;
}

/* ============================================================
   Login Page
   ============================================================ */
.login-page {
  background: linear-gradient(135deg, #0d0d1a 0%, #1a1a2e 50%, #16213e 100%);
  min-height: 100vh;
  overflow: auto;
}
.login-bg-animated {
  position: fixed;
  inset: 0;
  background: radial-gradient(ellipse at 20% 50%, rgba(108,99,255,.15) 0%, transparent 60%),
              radial-gradient(ellipse at 80% 20%, rgba(52,152,219,.1) 0%, transparent 50%);
  pointer-events: none;
  z-index: 0;
}
.app-logo-wrap {
  width: 72px; height: 72px;
  border-radius: 20px;
  background: var(--accent-light);
  display: inline-flex;
  align-items: center;
  justify-content: center;
}
.app-logo-icon { font-size: 2rem; color: var(--accent); }

.glass-card {
  background: rgba(255,255,255,0.04);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(255,255,255,0.08);
  border-radius: 20px;
}
.glass-input {
  background: rgba(255,255,255,0.05) !important;
  border-color: rgba(255,255,255,0.1) !important;
  color: #fff !important;
}
.glass-input:focus {
  background: rgba(255,255,255,0.08) !important;
  border-color: var(--accent) !important;
  box-shadow: 0 0 0 3px rgba(108,99,255,0.2) !important;
  color: #fff !important;
}
.glass-input::placeholder { color: rgba(255,255,255,0.3) !important; }
.glass-input-addon {
  background: rgba(255,255,255,0.05) !important;
  border-color: rgba(255,255,255,0.1) !important;
  color: rgba(255,255,255,0.4) !important;
}

.alert-shake { animation: shake .4s ease; }
@keyframes shake {
  0%,100%  { transform: translateX(0); }
  20%,60%  { transform: translateX(-6px); }
  40%,80%  { transform: translateX(6px); }
}

/* ============================================================
   Admin
   ============================================================ */
body.admin-page {
  overflow: auto;
  background: var(--bg);
  color: var(--text);
}

.admin-nav {
  background: var(--bg2);
  border-bottom: 1px solid var(--border);
  padding: 0;
}
.admin-nav .navbar-brand { color: var(--text) !important; }
.admin-nav .nav-link { color: var(--text-muted); font-size: .875rem; }
.admin-nav .nav-link.active, .admin-nav .nav-link:hover { color: var(--accent); }

.admin-content { max-width: 1200px; margin: 0 auto; }

.admin-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.admin-card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  font-size: .875rem;
}

.stat-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
}
.stat-icon { font-size: 1.5rem; margin-bottom: 8px; }
.stat-value { font-size: 2rem; font-weight: 800; line-height: 1; }
.stat-label { font-weight: 600; font-size: .875rem; margin-top: 4px; }

.members-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 8px;
  background: var(--bg3);
  border-radius: 8px;
  padding: 12px;
  max-height: 200px;
  overflow-y: auto;
}
.members-grid .form-check { margin: 0; }
.members-grid .form-check-input { background-color: var(--bg2); border-color: var(--border); }
.members-grid .form-check-input:checked { background-color: var(--accent); border-color: var(--accent); }

.bg-accent { background-color: var(--accent) !important; }

/* ---- Tables ---- */
.table { color: var(--text); }
.table th { font-size: .75rem; text-transform: uppercase; letter-spacing: .05em; color: var(--text-muted); font-weight: 600; border-color: var(--border); }
.table td { border-color: var(--border); vertical-align: middle; font-size: .875rem; }
.table-hover tbody tr:hover { background: var(--bg3); }

/* ---- Forms ---- */
.form-control, .form-select {
  background: var(--bg3) !important;
  border-color: var(--border) !important;
  color: var(--text) !important;
}
.form-control:focus, .form-select:focus {
  border-color: var(--accent) !important;
  box-shadow: 0 0 0 3px rgba(108,99,255,0.15) !important;
  color: var(--text) !important;
  background: var(--bg3) !important;
}
.form-control::placeholder { color: var(--text-muted) !important; }
.form-select option { background: var(--bg2); }
.input-group-text {
  background: var(--bg3) !important;
  border-color: var(--border) !important;
  color: var(--text-muted) !important;
}

/* ============================================================
   Notification Banner
   ============================================================ */
.notif-banner {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--bg2);
  border-top: 1px solid var(--border);
  padding: 12px 20px;
  font-size: .875rem;
  display: flex;
  align-items: center;
  z-index: 1000;
  animation: slideUp .3s ease;
}
@keyframes slideUp {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

/* ============================================================
   Sidebar Overlay (Mobile)
   ============================================================ */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 100;
  backdrop-filter: blur(2px);
}

/* ============================================================
   PWA Helpers
   ============================================================ */
.flex-1    { flex: 1; }
.min-w-0   { min-width: 0; }
.tracking-wide { letter-spacing: .06em; }

/* ---- In-App Notification Toast ---- */
.msg-toast {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 9999;
  display: flex;
  align-items: center;
  gap: 12px;
  background: rgba(30, 30, 50, 0.96);
  backdrop-filter: blur(20px);
  border: 1px solid rgba(108, 99, 255, 0.4);
  border-radius: 14px;
  padding: 12px 14px;
  max-width: 320px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
  cursor: pointer;
  opacity: 0;
  transform: translateX(110%);
  transition: opacity .3s ease, transform .3s cubic-bezier(0.34,1.3,0.64,1);
  user-select: none;
}
.msg-toast-in {
  opacity: 1;
  transform: translateX(0);
}
.msg-toast-avatar {
  width: 36px; height: 36px;
  border-radius: 50%;
  background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  font-size: .8rem; font-weight: 700; color: #fff;
  flex-shrink: 0;
}
.msg-toast-body { flex: 1; min-width: 0; }
.msg-toast-name {
  font-size: .78rem; font-weight: 700;
  color: #e8e8f0;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.msg-toast-text {
  font-size: .73rem;
  color: rgba(232,232,240,.55);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  margin-top: 2px;
}
.msg-toast-close {
  background: transparent; border: none;
  color: rgba(232,232,240,.35); font-size: .9rem;
  cursor: pointer; padding: 2px 4px; border-radius: 6px;
  flex-shrink: 0; line-height: 1;
  transition: color .15s;
}
.msg-toast-close:hover { color: #e8e8f0; }

/* ---- PWA Install Banner ---- */
.pwa-banner,
.ios-hint {
  display: none;
  align-items: center;
  gap: 12px;
  background: rgba(108, 99, 255, 0.12);
  border: 1px solid rgba(108, 99, 255, 0.35);
  border-radius: 16px;
  padding: 14px 16px;
  margin-top: 16px;
  backdrop-filter: blur(16px);
  opacity: 0;
  transform: translateY(10px);
  transition: opacity .35s ease, transform .35s ease;
}
.pwa-banner-in {
  opacity: 1 !important;
  transform: translateY(0) !important;
}
.pwa-banner-icon {
  width: 42px; height: 42px;
  border-radius: 12px;
  background: rgba(108, 99, 255, 0.25);
  display: flex; align-items: center; justify-content: center;
  font-size: 1.25rem;
  color: #a89cff;
  flex-shrink: 0;
}
.pwa-banner-text { flex: 1; min-width: 0; }
.pwa-banner-title {
  font-weight: 700;
  font-size: .9rem;
  color: #e8e8f0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.pwa-banner-sub {
  font-size: .75rem;
  color: rgba(232,232,240,.5);
  margin-top: 1px;
}
.pwa-banner-btn {
  background: var(--accent);
  border: none;
  color: #fff;
  font-size: .8rem;
  font-weight: 700;
  padding: 7px 14px;
  border-radius: 10px;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
  transition: background .18s;
}
.pwa-banner-btn:hover { background: var(--accent-hover); }
.pwa-banner-dismiss {
  background: transparent;
  border: none;
  color: rgba(232,232,240,.4);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 6px;
  flex-shrink: 0;
  transition: color .18s;
}
.pwa-banner-dismiss:hover { color: #e8e8f0; }

/* iOS hint is text-only, slightly different layout */
.ios-hint {
  font-size: .82rem;
  color: rgba(232,232,240,.8);
  justify-content: center;
  text-align: center;
  flex-wrap: wrap;
  gap: 6px;
  padding: 12px 16px;
}

/* ============================================================
   Mobile
   ============================================================ */
@media (max-width: 767.98px) {
  .sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    transform: translateX(-100%);
    box-shadow: var(--shadow);
    z-index: 300;
  }
  .sidebar.open {
    transform: translateX(0);
  }
  .sidebar-overlay.open {
    display: block;
  }
  .chat-main {
    width: 100%;
  }
  .msg-content {
    max-width: 85%;
  }
}

@media (min-width: 768px) {
  .sidebar { transform: none !important; }
}

/* Standalone (PWA) bottom safe area */
@supports (padding-bottom: env(safe-area-inset-bottom)) {
  .compose-area {
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }
}
