
:root{
  --bg:#0b1220;
  --panel:#111827;
  --card:#0f172a;
  --border:#1f2a44;
  --text:#e5e7eb;
  --muted:#9ca3af;
  --primary:#2563eb;
  --danger:#ef4444;
  --shadow: 0 10px 30px rgba(0,0,0,.25);
  --radius: 16px;
  --font: system-ui,-apple-system,Segoe UI,Roboto,Ubuntu,Cantarell,Noto Sans,sans-serif;
}
*{box-sizing:border-box}
html,body{height:100%; overflow:hidden}
body{
  margin:0;
  background:linear-gradient(135deg, var(--bg), #050814);
  color:var(--text);
  font-family:var(--font);
}
a{color:inherit}
.hidden{display:none !important}

.app{
  display:grid;
  grid-template-columns: 360px 1fr;
  height:100vh;
  height:100dvh;
}
.sidebar{
  border-right:1px solid var(--border);
  background:rgba(17,24,39,.9);
  backdrop-filter: blur(10px);
  display:flex;
  flex-direction:column;
  padding:12px;
  gap:12px;
}
.brand{
  display:flex;
  align-items:center;
  justify-content:space-between;
  padding:10px 12px;
  border:1px solid var(--border);
  border-radius:var(--radius);
  background:rgba(15,23,42,.8);
}
.brand-title{font-weight:700; letter-spacing:.2px}
.brand-actions{display:flex; gap:8px}
.panel{
  border:1px solid var(--border);
  border-radius:var(--radius);
  background:rgba(15,23,42,.6);
  padding:12px;
  box-shadow:var(--shadow);
}
.tabs{display:flex; gap:8px; margin-bottom:10px}
.tab{
  flex:1;
  padding:10px;
  border-radius:12px;
  border:1px solid var(--border);
  background:transparent;
  color:var(--text);
  cursor:pointer;
}
.tab.active{background:rgba(37,99,235,.2); border-color:rgba(37,99,235,.4)}
.form{display:flex; flex-direction:column; gap:10px}
input,select,textarea{
  padding:12px;
  border-radius:12px;
  border:1px solid var(--border);
  background:rgba(17,24,39,.6);
  color:var(--text);
  outline:none;
}
.password-wrap{
  position:relative;
  display:flex;
  align-items:center;
}
.password-wrap input{
  width:100%;
  padding-right:44px; /* space for eye */
}
.eye-btn{
  position:absolute;
  right:8px;
  top:50%;
  transform:translateY(-50%);
  width:34px;
  height:34px;
  display:flex;
  align-items:center;
  justify-content:center;
  border-radius:10px;
  background:rgba(255,255,255,.06);
}
.eye-btn:active{ transform:translateY(-50%) scale(.98); }

input::placeholder{color:rgba(156,163,175,.7)}
.btn{
  padding:10px 12px;
  border-radius:12px;
  border:1px solid var(--border);
  background:rgba(17,24,39,.6);
  color:var(--text);
  cursor:pointer;

  touch-action: manipulation;

  -webkit-tap-highlight-color: transparent;
}
.btn.primary{background:rgba(37,99,235,.8); border-color:rgba(37,99,235,.8)}
.btn.danger{background:rgba(239,68,68,.85); border-color:rgba(239,68,68,.85)}
.icon-btn{
  border:1px solid var(--border);
  background:rgba(17,24,39,.6);
  border-radius:12px;
  padding:8px 10px;
  color:var(--text);
  cursor:pointer;

  touch-action: manipulation;

  -webkit-tap-highlight-color: transparent;
}
.error{
  margin-top:10px;
  padding:10px;
  border-radius:12px;
  border:1px solid rgba(239,68,68,.5);
  background:rgba(239,68,68,.15);
}
.hint{color:var(--muted); font-size:12px}
.muted{color:var(--muted); font-size:12px}

.search-row{display:flex; gap:8px; margin-bottom:10px}
.list{
  display:flex;
  flex-direction:column;
  gap:8px;
  max-height: calc(100vh - 360px);
  overflow:auto;
  padding-right:4px;
}
.item{
  display:flex;
  gap:10px;
  align-items:center;
  padding:10px;
  border:1px solid var(--border);
  border-radius:14px;
  cursor:pointer;
  background:rgba(17,24,39,.35);
}
.item:hover{background:rgba(17,24,39,.6)}
.item.active{border-color:rgba(37,99,235,.6); background:rgba(37,99,235,.12)}
.avatar{
  width:40px;
  height:40px;
  border-radius:14px;
  background:rgba(37,99,235,.25);
  display:flex;
  align-items:center;
  justify-content:center;
  font-weight:700;
}
.item-title{font-weight:600}
.item-sub{color:var(--muted); font-size:12px; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; max-width:220px}
.sidebar-footer{display:flex; gap:8px; margin-top:10px; flex-wrap:wrap}

.main{
  display:flex;
  flex-direction:column;
  min-height:0;
}
.topbar{
  display:flex;
  align-items:center;
  justify-content:space-between;
  padding:12px 14px;
  border-bottom:1px solid var(--border);
  background:rgba(17,24,39,.85);
  backdrop-filter: blur(10px);
}
.topbar-left{display:flex; align-items:center; gap:10px; min-width:0}
.back-btn{display:none}
.msg.pending{opacity:.7}

.chat-title{display:flex; flex-direction:column}
.topbar-actions{display:flex; gap:8px}

.content{
  flex:1;
  position:relative;
  padding:14px;
  overflow:hidden;
  display:flex;
  flex-direction:column;
  min-height:0;
}
.empty{
  height:100%;
  display:flex;
  flex-direction:column;
  justify-content:center;
  align-items:center;
  gap:10px;
  border:1px dashed rgba(31,42,68,.8);
  border-radius:var(--radius);
  background:rgba(15,23,42,.35);
}
.empty-title{font-size:22px; font-weight:800}

.chat{flex:1; min-height:0; display:flex; flex-direction:column}
.messages{
  flex:1;
  overflow:auto;
  padding:6px;
  min-height:0;
  border:1px solid var(--border);
  border-radius:var(--radius);
  background:rgba(15,23,42,.35);
}
.msg-row{display:flex; margin:10px 0}
.msg{
  max-width:min(680px, 92%);
  padding:10px 12px;
  border-radius:16px;
  border:1px solid var(--border);
  background:rgba(17,24,39,.7);
  box-shadow: 0 10px 20px rgba(0,0,0,.12);
}
.msg.me{
  background:rgba(37,99,235,.22);
  border-color:rgba(37,99,235,.3);
}
.msg-meta{margin-top:6px; font-size:11px; color:var(--muted)}
.msg a{color:#93c5fd; text-decoration:none}
.msg a:hover{text-decoration:underline}
.composer{
  display:flex;
  gap:8px;
  padding-top:10px;

  padding-bottom: calc(10px + env(safe-area-inset-bottom));
}
/* Multiline message composer */
#messageInput{
  flex:1;
  min-height:40px;
  max-height:140px;
  resize:none;
  overflow:auto;
  padding:10px 12px;
  border-radius:14px;
  border:1px solid var(--border);
  background:rgba(17,24,39,.7);
  color:var(--text);
  outline:none;
  line-height:1.25;
}
#messageInput:focus{ border-color: rgba(96,165,250,.65); }
.composer{ align-items:flex-end; }

.typing{margin-top:6px; color:var(--muted); font-size:12px}

.modal{
  position:fixed;
  inset:0;
  display:flex;
  align-items:center;
  justify-content:center;
  background:rgba(0,0,0,.55);
  backdrop-filter: blur(8px);
  z-index: 1000;
}
.modal-card{
  width:min(560px, 94vw);
  border-radius:22px;
  border:1px solid var(--border);
  background:rgba(15,23,42,.95);
  box-shadow: var(--shadow);
  overflow:hidden;
}
.modal-head{
  display:flex;
  justify-content:space-between;
  align-items:center;
  padding:12px 14px;
  border-bottom:1px solid var(--border);
}
.modal-body{padding:14px; display:flex; flex-direction:column; gap:10px}

.call{
  position:fixed;
  inset:0;
  display:flex;
  align-items:center;
  justify-content:center;
  background:rgba(0,0,0,.7);
}
.call-card{
  width:min(820px, 96vw);
  border-radius:24px;
  border:1px solid var(--border);
  background:rgba(15,23,42,.95);
  box-shadow: var(--shadow);
  overflow:hidden;
  padding:14px;
  display:flex;
  flex-direction:column;
  gap:12px;
}
.call-title{font-weight:800}
.call-media{
  display:grid;
  grid-template-columns: 1fr 260px;
  gap:12px;
}
#remoteVideo{
  width:100%;
  border-radius:18px;
  border:1px solid var(--border);
  background:black;
  min-height:360px;
}
#localVideo{
  width:100%;
  border-radius:18px;
  border:1px solid var(--border);
  background:black;
  min-height:160px;
}
.call-actions{display:flex; justify-content:flex-end}

@media (max-width: 920px){
  /* Ensure no horizontal overflow on mobile */
  .app{ grid-template-columns: 1fr !important; width:100vw; max-width:100vw; overflow-x:hidden; }
  .sidebar, .main{ width:100%; max-width:100vw; min-width:0; }
  .content, .messages, .composer{ max-width:100vw; }
  .msg-row, .msg{ max-width:100%; }

  .app{grid-template-columns: 1fr}
  /* Mobile: WhatsApp-like navigation (list OR chat) */
  body:not(.chat-mode) .main{display:none}
  body.chat-mode .main{display:flex}
  .sidebar{position:relative; border-right:none; border-bottom:1px solid var(--border)}
  .list{max-height: 320px}
  .call-media{grid-template-columns: 1fr}
  #localVideo{min-height:200px}
}


/* Branding */
.brand-left{display:flex; align-items:center; gap:10px}
.brand-logo{width:28px; height:28px; display:block}
.sidebar-footer{
  margin-top: 10px;
  padding: 8px 6px 0;
  display:flex;
  justify-content:center;
  gap:10px;
  flex-wrap:wrap;
  color: var(--muted);
  font-size: 12px;
}
.sidebar-footer a{
  color: var(--muted);
  text-decoration:none;
}
.sidebar-footer a:hover{color: var(--text); text-decoration:underline}

/* Splash screen */
.splash{
  position:fixed;
  inset:0;
  z-index:9999;
  background: radial-gradient(1200px 600px at 20% 10%, rgba(37,99,235,.25), transparent 55%),
              radial-gradient(900px 500px at 90% 30%, rgba(34,197,94,.22), transparent 55%),
              rgba(2,6,23,.92);
  display:flex;
  align-items:center;
  justify-content:center;
  padding: 18px;
}
.splash-card{
  width:min(520px, 94vw);
  border:1px solid var(--border);
  border-radius: 22px;
  background: rgba(15,23,42,.65);
  box-shadow: var(--shadow);
  padding: 22px 18px;
  text-align:center;
}
.splash-logo{width:64px; height:64px; margin: 0 auto 10px; display:block}
.splash-title{font-size: 20px; font-weight: 800; margin-bottom: 6px}
.splash-sub{opacity:.9; line-height:1.55}
.splash-hint{margin-top: 14px; opacity: .7; font-size: 12px}
.splash.hide{animation: splashFade .45s ease forwards}
@keyframes splashFade{to{opacity:0; visibility:hidden}}


/* Footer legal links in sidebar */
.sidebar-legal{
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  line-height: 1.4;
  display:flex;
  flex-wrap:wrap;
  gap:6px;
  opacity: .9;
}
.sidebar-legal a{
  color: inherit;
  text-decoration: none;
}
.sidebar-legal a:hover{ text-decoration: underline; }
.sidebar-legal .sep{ opacity: .7; }


@media (max-width: 920px){
  body.chat-mode .sidebar{display:none}
  /* Mobile (chat): keep a single header (topbar) */
  body.chat-mode .topbar{display:flex}
  body.chat-mode .content{padding:10px}
  body.chat-mode .back-btn{display:inline-flex}
  body.chat-mode .topbar-actions{gap:6px}
}





/* Message deletion */
.msg-meta{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:10px;
}
.msg-del{
  border:1px solid var(--border);
  background:rgba(17,24,39,.35);
  color:var(--text);
  border-radius:10px;
  padding:2px 8px;
  font-size:12px;
  cursor:pointer;
}
.msg-del:active{transform:scale(.98)}

.msg-row.selected .msg{
  outline:2px solid rgba(59,130,246,.7);
}

.delete-toolbar{
  position:fixed;
  left:12px;
  right:12px;
  bottom: calc(80px + env(safe-area-inset-bottom));
  z-index: 50;
}
.delete-toolbar-inner{
  display:flex;
  align-items:center;
  justify-content:space-between;
  gap:10px;
  padding:10px 12px;
  border-radius:14px;
  border:1px solid var(--border);
  background:rgba(17,24,39,.88);
  box-shadow: 0 10px 24px rgba(0,0,0,.25);
}
.delete-count{font-size:13px; color:var(--text)}
.delete-actions{display:flex; gap:8px}

/* Slightly calmer splash hint */
.splash-hint{opacity:.85}


/* Alerts enable banner (mobile-friendly) */
.alerts-banner{
  position: fixed;
  left: 12px;
  right: 12px;
  bottom: 12px;
  z-index: 60;
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  background: rgba(17,24,39,0.96);
  color: #fff;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0,0,0,0.25);
}
.alerts-banner .alerts-banner-text{
  font-size: 13px;
  line-height: 1.25;
}
.btn.small{
  padding: 8px 10px;
  font-size: 13px;
  border-radius: 10px;
}
@media (min-width: 920px){
  .alerts-banner{ left: auto; right: 16px; width: 420px; }
}


body.modal-open{ overflow:hidden; }

/* Fullscreen modal on mobile for Status / Groups */
@media (max-width: 900px){
  .modal.modal--mobile-full{
    align-items: stretch;
    justify-content: stretch;
    background: rgba(0,0,0,.70);
  }
  .modal.modal--mobile-full .modal-card{
    width: 100vw;
    height: 100vh;
    border-radius: 0;
    border-left: 0;
    border-right: 0;
    border-top: 0;
    box-shadow: none;
    display:flex;
    flex-direction:column;
  }
  .modal.modal--mobile-full .modal-body{
    flex:1;
    overflow:auto;
    -webkit-overflow-scrolling: touch;
  }
}


/* Emoji picker */
.emoji-panel{
  position:fixed;
  left:0; right:0;
  bottom:56px;
  max-height:42vh;
  overflow:auto;
  background:rgba(17,24,39,.98);
  border-top:1px solid rgba(255,255,255,.08);
  padding:10px;
  display:grid;
  grid-template-columns:repeat(8, 1fr);
  gap:6px;
  z-index:1200;
}
@media (min-width: 820px){
  .emoji-panel{
    left:auto; right:auto;
    width:360px;
    bottom:74px;
    border:1px solid rgba(255,255,255,.12);
    border-radius:14px;
    margin-left:12px;
  }
}
.emoji-panel.hidden{display:none}
.emoji-btn{
  font-size:22px;
  line-height:1;
  padding:8px 0;
  border-radius:10px;
  border:1px solid rgba(255,255,255,.06);
  background:rgba(255,255,255,.03);
  cursor:pointer;
}
.emoji-btn:active{transform:scale(0.97)}

/* WhatsApp-like Status viewer */
.status-list{display:flex;flex-direction:column;gap:10px;margin-top:12px}
.status-item{
  display:flex; align-items:center; gap:12px;
  width:100%;
  padding:10px;
  background:rgba(255,255,255,.03);
  border:1px solid rgba(255,255,255,.06);
  border-radius:14px;
  cursor:pointer;
  text-align:left;
}
.status-avatar{
  width:46px; height:46px;
  border-radius:50%;
  position:relative;
  flex:0 0 auto;
  display:grid; place-items:center;
  background:rgba(255,255,255,.06);
}
.status-ring{
  position:absolute; inset:-2px;
  border-radius:50%;
  border:2px solid rgba(37,99,235,.85);
}
.status-initial{font-weight:700}
.status-meta{display:flex;flex-direction:column;gap:2px;min-width:0}
.status-name{font-weight:700;white-space:nowrap;overflow:hidden;text-overflow:ellipsis}

.story-viewer{
  position:fixed; inset:0;
  background:#000;
  z-index:2000;
  display:flex;
  flex-direction:column;
}
.story-viewer.hidden{display:none}
.story-top{
  padding:10px 10px 0 10px;
}
.story-progress{
  display:flex;
  gap:2px;
  width:100%;
}
.story-seg{
  flex:1 1 auto;
  height:2px;
  background:rgba(255,255,255,.25);
  border-radius:999px;
  overflow:hidden;
}
.story-seg-fill{
  height:100%;
  width:0%;
  background:#fff;
}
.story-header{
  display:flex;
  align-items:center;
  gap:10px;
  margin-top:10px;
  color:#fff;
}
.story-user{
  font-weight:700;
  max-width:60vw;
  overflow:hidden;
  white-space:nowrap;
  text-overflow:ellipsis;
}
.story-time{opacity:.75;font-size:.92em}
.story-actions{margin-left:auto;display:flex;gap:8px;align-items:center}
.story-sound{
  background:rgba(255,255,255,.1);
  border:1px solid rgba(255,255,255,.12);
  color:#fff;
  border-radius:12px;
  width:40px;height:34px;
}
.story-sound.hidden{display:none}
.story-close{
  background:rgba(255,255,255,.1);
  border:1px solid rgba(255,255,255,.12);
  color:#fff;
  border-radius:12px;
  width:40px;height:34px;
}
.story-content{
  touch-action:none;
  position:relative;
  flex:1 1 auto;
  display:flex;
  align-items:center;
  justify-content:center;
  overflow:hidden;
}
.story-media{
  max-width:100%;
  max-height:100%;
  object-fit:contain;
}
.story-audio{
  width:min(92vw, 520px);
}
.story-text{
  padding:22px;
  font-size:24px;
  line-height:1.25;
  text-align:center;
  color:#fff;
  max-width:92vw;
  word-break:break-word;
}
.story-tap{
  position:absolute;
  top:0; bottom:0;
  width:40%;
}
.story-tap-left{left:0}
.story-tap-right{right:0}
.story-hint{
  position:absolute;
  bottom:18px;
  left:50%;
  transform:translateX(-50%);
  color:rgba(255,255,255,.75);
  font-size:12px;
  padding:6px 10px;
  border-radius:999px;
  background:rgba(0,0,0,.35);
  border:1px solid rgba(255,255,255,.10);
}


/* --- Mobile width safety (avoid horizontal scroll) --- */
html, body { max-width: 100%; overflow-x: hidden; }
* { box-sizing: border-box; }
.messages { overflow-x: hidden; }
.msg, .item-title, .item-sub { overflow-wrap: anywhere; word-break: break-word; }
.composer { width: 100%; }
#messageInput { min-width: 0; }

/* --- Avatars --- */
.avatar{
  width:40px;
  height:40px;
  border-radius: 999px;
  background: rgba(148,163,184,.18);
  border: 1px solid rgba(148,163,184,.22);
  display:flex;
  align-items:center;
  justify-content:center;
  font-weight:700;
  flex:0 0 auto;
  overflow:hidden;
  user-select:none;
}
.avatar--lg{ width:42px; height:42px; }
.avatar.square{ border-radius: 12px; }
.avatar img{ width:100%; height:100%; object-fit:cover; display:block; }
.avatar .initials{ font-size:14px; letter-spacing:.5px; }

/* --- Avatar cropper (WhatsApp-like) --- */
.cropper{
  display:flex;
  flex-direction:column;
  gap:12px;
}
.cropper-top{
  display:flex;
  gap:12px;
  align-items:center;
  flex-wrap:wrap;
}
.cropper-viewport{
  width:min(280px, 80vw);
  aspect-ratio: 1 / 1;
  border-radius: 18px;
  border:1px solid var(--border);
  background: rgba(15,23,42,.35);
  overflow:hidden;
  position:relative;
  touch-action:none;
}
.cropper-viewport.round{
  border-radius: 999px;
}
.cropper-viewport img{
  position:absolute;
  left:50%;
  top:50%;
  transform: translate(-50%, -50%) scale(1);
  transform-origin: center center;
  will-change: transform;
  max-width:none;
  max-height:none;
  user-select:none;
  -webkit-user-drag:none;
  pointer-events:none;
}
.cropper-actions{
  display:flex;
  justify-content:flex-end;
  gap:10px;
  flex-wrap:wrap;
}



/* Admin delete button in conversation list */
.item .item-actions{margin-left:auto;display:flex;gap:6px;align-items:center}
.item .item-del{border:1px solid #233253;background:#0f1a33;color:#e5e7eb;border-radius:10px;padding:6px 8px;cursor:pointer}
.item .item-del:active{transform:scale(0.98)}

/* Status feed avatar image */
.status-avatar img{position:absolute;inset:0;width:100%;height:100%;object-fit:cover;border-radius:999px}
.status-avatar .status-initial{position:relative;z-index:1}

/* Call modal */
.call-modal{display:flex;flex-direction:column;gap:10px}
.call-title{font-weight:900;font-size:18px}
.call-sub{color:#cbd5e1}
.call-actions{display:flex;gap:8px;justify-content:flex-end;flex-wrap:wrap}
.btn.danger{background:#b91c1c;border-color:#7f1d1d}

/* Modal without close button */
.modal.modal--no-close .modal-head{justify-content:space-between}

.chat-mobile-actions{margin-left:auto;display:flex;gap:8px;align-items:center}
.topbar-actions{display:flex;gap:8px;align-items:center}


.icon-btn.disabled{opacity:.5; cursor:not-allowed}
.icon-btn:disabled{opacity:.5; cursor:not-allowed}

@media (max-width: 920px){
  body.chat-mode .topbar{display:flex}
}



/* Splash close button (manual fallback) */
.splash-card{ position: relative; }
.splash-close{
  position: absolute;
  top: 10px;
  right: 10px;
  width: 32px;
  height: 32px;
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,.18);
  background: rgba(0,0,0,.25);
  color: rgba(255,255,255,.9);
  font-size: 20px;
  line-height: 28px;
  cursor: pointer;
}
.splash-close:active{ transform: scale(.98); }


/* Responsive search row (Android 11 small screens): keep Search + buttons inside frame */
.search-row{
  display:flex;
  gap:10px;
  align-items:center;
  flex-wrap:wrap;
}
.search-row input{
  flex: 1 1 220px;
  min-width: 0; /* prevent overflow in flex containers */
}
.search-row .btn{
  flex: 0 0 auto;
  white-space: nowrap;
}
@media (max-width: 420px){
  .search-row{
    gap:8px;
  }
  .search-row input{
    flex: 1 1 100%;
  }
  /* Put buttons on the next line, side by side */
  #btnNewChat{
    flex: 1 1 0;
  }
}
