/* ============================================================
   Simple Messenger v2 – Telegram-inspired CSS
   ============================================================ */

/* ── Reset ─────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
button { font-family: inherit; cursor: pointer; }
textarea, input, select { font-family: inherit; }

/* ── Design tokens – Light ─────────────────────────────────── */
:root {
  --bg:            #f4f6f8;
  --surface:       #ffffff;
  --sidebar-bg:    #2b5278;
  --sidebar-hover: rgba(255,255,255,.08);
  --sidebar-active:#3a6898;
  --sidebar-txt:   #cdd7e1;
  --sidebar-txt-d: #ffffff;
  --accent:        #2b9af3;
  --accent-dark:   #1a78c2;
  --danger:        #ef4444;
  --text:          #1a1a1a;
  --text-muted:    #6b7280;
  --border:        #e5e7eb;
  --bubble-me:     #effdde;
  --bubble-me-txt: #1a1a1a;
  --bubble-them:   #ffffff;
  --bubble-them-txt:#1a1a1a;
  --radius:        12px;
  --font:          'Segoe UI', system-ui, -apple-system, sans-serif;
  --chat-bg:       #e4ddd6;   /* telegram-ish wallpaper */
  --composer-bg:   #ffffff;
  --input-bg:      #f0f2f5;
  --drawer-bg:     #2b5278;
  --modal-bg:      #ffffff;
  --toggle-off:    #b0b8c1;
  --toggle-on:     var(--accent);
}

/* ── Dark mode tokens ───────────────────────────────────────── */
[data-theme="dark"] {
  --bg:            #17212b;
  --surface:       #1f2d3d;
  --sidebar-bg:    #17212b;
  --sidebar-hover: rgba(255,255,255,.06);
  --sidebar-active:#2b3e50;
  --sidebar-txt:   #8a9ab0;
  --sidebar-txt-d: #cdd7e1;
  --accent:        #2b9af3;
  --accent-dark:   #1a78c2;
  --text:          #d9dee5;
  --text-muted:    #6b7280;
  --border:        #2b3e50;
  --bubble-me:     #2b5278;
  --bubble-me-txt: #d9dee5;
  --bubble-them:   #1f2d3d;
  --bubble-them-txt:#d9dee5;
  --chat-bg:       #0e1621;
  --composer-bg:   #17212b;
  --input-bg:      #242f3d;
  --drawer-bg:     #17212b;
  --modal-bg:      #1f2d3d;
  --toggle-off:    #3a4a5c;
}

html, body { height: 100%; }
body { font-family: var(--font); background: var(--bg); color: var(--text); }
/* Prevent scroll only when messenger app is loaded */
body:has(.layout) { overflow: hidden; }

/* ── Utility ────────────────────────────────────────────────── */
.hidden { display: none !important; }

/* ── Auth pages ─────────────────────────────────────────────── */
.auth-page {
  min-height: 100vh; overflow-y: auto;
  display: flex; align-items: center; justify-content: center;
  padding: 1rem; background: var(--bg);
}
.auth-card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: 0 4px 24px rgba(0,0,0,.12);
  padding: 2.5rem 2rem;
  width: 100%; max-width: 420px;
}
.auth-logo   { font-size: 2.5rem; text-align: center; display: block; margin-bottom: .5rem; }
.auth-title  { font-size: 1.5rem; font-weight: 700; text-align: center; }
.auth-subtitle { color: var(--text-muted); text-align: center; margin: .25rem 0 1.5rem; }
.auth-footer { text-align: center; margin-top: 1.25rem; font-size: .9rem; color: var(--text-muted); }
.auth-footer a { color: var(--accent); text-decoration: none; font-weight: 600; }
.auth-footer a:hover { text-decoration: underline; }

/* ── Buttons ────────────────────────────────────────────────── */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: .65rem 1.25rem; border: none; border-radius: 8px;
  font-size: 1rem; font-weight: 600;
  cursor: pointer; transition: background .15s, opacity .15s;
}
.btn-primary  { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-dark); }
.btn-primary:disabled { opacity: .6; cursor: not-allowed; }
.btn-full     { width: 100%; margin-top: .5rem; }
.btn-outline  { background: transparent; color: var(--accent); border: 1.5px solid var(--accent); border-radius: 8px; }
.btn-outline:hover { background: var(--accent); color: #fff; }
.btn-ghost    { background: transparent; color: var(--text-muted); border: none; }
.btn-ghost:hover { background: var(--input-bg); }
.btn-sm       { padding: .35rem .85rem; font-size: .85rem; }

/* Icon button */
.icon-btn {
  width: 40px; height: 40px;
  display: inline-flex; align-items: center; justify-content: center;
  background: none; border: none; border-radius: 50%;
  font-size: 1.15rem; color: var(--sidebar-txt);
  cursor: pointer; transition: background .15s, color .15s;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--sidebar-hover); color: var(--sidebar-txt-d); }

/* ── Alerts ─────────────────────────────────────────────────── */
.alert { padding: .75rem 1rem; border-radius: 8px; margin-bottom: 1rem; font-size: .9rem; }
.alert-error { background: #fee2e2; color: #b91c1c; border: 1px solid #fca5a5; }
.form-group { display: flex; flex-direction: column; gap: .35rem; margin-bottom: 1rem; }
.form-group label { font-size: .875rem; font-weight: 600; color: var(--text); }
.form-group input, .form-group textarea, .form-group select {
  padding: .65rem .85rem; border: 1.5px solid var(--border);
  border-radius: 8px; font-size: 1rem; outline: none;
  background: var(--input-bg); color: var(--text);
  transition: border-color .15s;
}
.form-group input:focus,
.form-group textarea:focus { border-color: var(--accent); }
.form-hint { font-size: .75rem; color: var(--text-muted); }

/* ── Layout ─────────────────────────────────────────────────── */
.layout {
  display: flex; height: 100vh; height: 100dvh; width: 100vw;
  overflow: hidden; position: relative;
}

/* ── Drawer (left slide-in) ─────────────────────────────────── */
.drawer {
  position: fixed; top: 0; left: 0; bottom: 0;
  width: 280px; z-index: 200;
  background: var(--drawer-bg);
  display: flex; flex-direction: column;
  transform: translateX(-100%);
  transition: transform .25s ease;
  box-shadow: 4px 0 24px rgba(0,0,0,.3);
}
.drawer.open { transform: translateX(0); }
.drawer-overlay {
  display: none; position: fixed; inset: 0; z-index: 199;
  background: rgba(0,0,0,.4);
}
.drawer-overlay.visible { display: block; }

.drawer-header {
  display: flex; align-items: center; gap: .5rem;
  padding: 1rem 1rem .75rem;
  border-bottom: 1px solid rgba(255,255,255,.07);
}
.drawer-title { color: #fff; font-weight: 700; font-size: 1.05rem; }

.drawer-avatar-wrap {
  display: flex; align-items: center; gap: .85rem;
  padding: 1.25rem 1rem;
  border-bottom: 1px solid rgba(255,255,255,.07);
}
.drawer-user-info { display: flex; flex-direction: column; gap: .15rem; }
.drawer-display-name { color: #fff; font-weight: 600; font-size: .95rem; }
.drawer-username { color: var(--sidebar-txt); font-size: .8rem; }

.drawer-nav { flex: 1; padding: .5rem 0; }
.drawer-nav-item {
  display: flex; align-items: center; gap: .75rem;
  width: 100%; padding: .75rem 1.25rem;
  background: none; border: none;
  color: var(--sidebar-txt); font-size: .925rem;
  cursor: pointer; transition: background .12s;
  text-align: left;
}
.drawer-nav-item:hover { background: var(--sidebar-hover); color: #fff; }
.nav-icon { font-size: 1.1rem; width: 1.5rem; text-align: center; }
.drawer-footer { padding: 1rem; color: var(--sidebar-txt); font-size: .75rem; text-align: center; }

/* ── Sidebar ────────────────────────────────────────────────── */
.sidebar {
  width: 360px; flex-shrink: 0;
  background: var(--sidebar-bg);
  display: flex; flex-direction: column;
  border-right: 1px solid rgba(0,0,0,.15);
  overflow: hidden;
}

.sidebar-header {
  display: flex; align-items: center; gap: .25rem;
  padding: .6rem .75rem;
  border-bottom: 1px solid rgba(255,255,255,.07);
  flex-shrink: 0;
}
.search-wrap { flex: 1; }
.search-input {
  width: 100%;
  background: rgba(255,255,255,.1);
  border: none; border-radius: 20px;
  padding: .45rem .9rem;
  color: #fff; font-size: .875rem; outline: none;
}
.search-input::placeholder { color: var(--sidebar-txt); }

/* Chat list */
.chat-list { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; }
.chat-list::-webkit-scrollbar { width: 4px; }
.chat-list::-webkit-scrollbar-thumb { background: rgba(255,255,255,.15); border-radius: 4px; }
.empty-state-small {
  padding: 2rem 1rem; color: var(--sidebar-txt);
  font-size: .875rem; text-align: center; line-height: 1.6;
}

/* Chat list item */
.chat-item {
  display: flex; align-items: center; gap: .75rem;
  padding: .75rem 1rem; cursor: pointer;
  border-bottom: 1px solid rgba(255,255,255,.04);
  transition: background .12s;
}
.chat-item:hover { background: var(--sidebar-hover); }
.chat-item.active { background: var(--sidebar-active); }

.chat-item-info { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: .15rem; }
.chat-item-top { display: flex; justify-content: space-between; align-items: baseline; gap: .5rem; }
.chat-item-name { font-weight: 600; font-size: .925rem; color: var(--sidebar-txt-d);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; }
.chat-item-time { font-size: .72rem; color: var(--sidebar-txt); flex-shrink: 0; }
.chat-item-preview { font-size: .825rem; color: var(--sidebar-txt);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ── Avatar ─────────────────────────────────────────────────── */
.avatar {
  width: 48px; height: 48px; border-radius: 50%;
  background: var(--accent); color: #fff;
  display: inline-flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: .9rem; flex-shrink: 0;
  text-transform: uppercase; overflow: hidden;
}
.avatar img { width: 100%; height: 100%; object-fit: cover; border-radius: 50%; }
.avatar-sm  { width: 34px; height: 34px; font-size: .75rem; }
.avatar-lg  { width: 56px; height: 56px; font-size: 1.1rem; }
.avatar-xl  { width: 80px; height: 80px; font-size: 1.5rem; }

/* ── Chat panel ─────────────────────────────────────────────── */
.chat-panel {
  flex: 1; display: flex; flex-direction: column;
  background: var(--chat-bg); min-width: 0; overflow: hidden;
}
.empty-chat {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  color: var(--text-muted); gap: 1rem;
}
.empty-chat-icon { font-size: 3.5rem; }
.empty-chat-text { font-size: 1.05rem; opacity: .7; }

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

/* Chat header */
.chat-header {
  display: flex; align-items: center; gap: .75rem;
  padding: .6rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border); flex-shrink: 0;
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
}
.chat-header-avatar { width: 40px; height: 40px; }
.chat-header-info { flex: 1; min-width: 0; }
.chat-header-name { font-weight: 600; font-size: .95rem; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-header-sub  { font-size: .78rem; color: var(--text-muted); }

/* Back button – visible only on mobile */
.back-btn { display: none; color: var(--accent); }

/* ── Messages ───────────────────────────────────────────────── */
.msg-list {
  flex: 1; overflow-y: auto; padding: .75rem 1rem;
  display: flex; flex-direction: column; gap: .35rem;
  -webkit-overflow-scrolling: touch;
}
.msg-list::-webkit-scrollbar { width: 5px; }
.msg-list::-webkit-scrollbar-thumb { background: rgba(0,0,0,.12); border-radius: 4px; }
.msgs-loading { text-align: center; color: var(--text-muted); font-size: .85rem; padding: 1rem; }
.msg-load-error { text-align: center; padding: .75rem 1rem; font-size: .85rem; color: var(--danger); }
.msg-load-error button { margin-left: .5rem; font-size: .8rem; background: none; border: 1px solid var(--danger); color: var(--danger); border-radius: 4px; padding: 2px 8px; cursor: pointer; }
.msg-load-error button:hover { background: var(--danger); color: #fff; }

/* Message row wrapper */
.msg-row {
  display: flex; flex-direction: column; max-width: 65%;
}
.msg-row.mine   { align-self: flex-end; align-items: flex-end; }
.msg-row.theirs { align-self: flex-start; align-items: flex-start; }

.msg-sender-name {
  font-size: .72rem; font-weight: 600; color: var(--accent);
  margin-bottom: .15rem; padding: 0 .4rem;
}

/* Bubble */
.msg-bubble {
  padding: .5rem .85rem;
  border-radius: 18px;
  font-size: .9375rem; line-height: 1.5;
  word-break: break-word; white-space: pre-wrap;
  max-width: 100%;
  box-shadow: 0 1px 2px rgba(0,0,0,.1);
}
.mine   .msg-bubble { background: var(--bubble-me); color: var(--bubble-me-txt); border-bottom-right-radius: 4px; }
.theirs .msg-bubble { background: var(--bubble-them); color: var(--bubble-them-txt); border-bottom-left-radius: 4px; }

/* Sticker bubble */
.msg-bubble.sticker-bubble {
  background: transparent; box-shadow: none;
  padding: .25rem;
}
.sticker-img { width: 120px; height: 120px; }

/* Voice bubble */
.voice-player {
  display: flex; align-items: center; gap: .5rem;
  min-width: 200px;
}
.voice-player audio { flex: 1; height: 36px; max-width: 220px; }
.voice-dur { font-size: .72rem; color: var(--text-muted); flex-shrink: 0; }

.msg-time {
  font-size: .7rem; color: var(--text-muted);
  margin-top: .2rem; padding: 0 .25rem;
}

/* Date separator */
.date-sep {
  text-align: center; font-size: .75rem; color: var(--text-muted);
  margin: .5rem 0; user-select: none;
}
.date-sep span {
  background: rgba(0,0,0,.08); border-radius: 12px;
  padding: .2rem .75rem;
}

/* ── Sticker picker ─────────────────────────────────────────── */
.sticker-picker {
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding: .75rem; flex-shrink: 0;
}
.sticker-grid {
  display: flex; flex-wrap: wrap; gap: .5rem;
}
.sticker-btn {
  width: 64px; height: 64px;
  background: var(--input-bg); border: 2px solid transparent;
  border-radius: 10px; padding: 4px; cursor: pointer;
  transition: border-color .12s, transform .1s;
  display: flex; align-items: center; justify-content: center;
}
.sticker-btn:hover { border-color: var(--accent); transform: scale(1.08); }
.sticker-btn img  { width: 100%; height: 100%; object-fit: contain; }

/* ── Composer ───────────────────────────────────────────────── */
.composer {
  display: flex; align-items: flex-end; gap: .4rem;
  padding: .6rem .75rem;
  padding-bottom: calc(.6rem + env(safe-area-inset-bottom, 0px));
  background: var(--composer-bg);
  border-top: 1px solid var(--border); flex-shrink: 0;
}
.composer-input {
  flex: 1; resize: none;
  border: 1.5px solid var(--border); border-radius: 20px;
  padding: .55rem .9rem;
  font-size: .9375rem; line-height: 1.45;
  max-height: 120px; overflow-y: auto; outline: none;
  background: var(--input-bg); color: var(--text);
  transition: border-color .15s;
}
.composer-input:focus { border-color: var(--accent); }

.composer-btn { color: var(--text-muted); }
.send-btn { color: var(--accent); }
.send-btn:hover { background: rgba(43,154,243,.1); color: var(--accent-dark); }
.record-btn.recording { color: var(--danger); animation: pulse 1s infinite; }

@keyframes pulse { 0%,100% { opacity:1; } 50% { opacity:.4; } }

/* Recording bar */
.recording-bar {
  display: flex; align-items: center; gap: .75rem;
  padding: .65rem 1rem;
  padding-bottom: calc(.65rem + env(safe-area-inset-bottom, 0px));
  background: var(--surface); border-top: 1px solid var(--border);
  flex-shrink: 0;
}
.rec-dot {
  width: 10px; height: 10px; border-radius: 50%;
  background: var(--danger); animation: pulse 1s infinite;
}
.rec-time { font-weight: 600; color: var(--danger); font-size: .9rem; min-width: 2.5rem; }
.rec-label { flex: 1; color: var(--text-muted); font-size: .875rem; }
.rec-cancel { color: var(--text-muted); }

/* ── Modals ─────────────────────────────────────────────────── */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 300;
  background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center;
  padding: 1rem;
}
.modal {
  background: var(--modal-bg);
  border-radius: var(--radius);
  width: 100%; max-width: 420px;
  max-height: 90vh; overflow: hidden;
  display: flex; flex-direction: column;
  box-shadow: 0 16px 48px rgba(0,0,0,.25);
}
.modal-wide { max-width: 520px; }

.modal-header {
  display: flex; align-items: center;
  padding: 1rem 1.25rem .75rem;
  border-bottom: 1px solid var(--border);
}
.modal-title { flex: 1; font-size: 1.1rem; font-weight: 700; color: var(--text); }
.modal-body  { flex: 1; overflow-y: auto; padding: 1rem 1.25rem; }
.modal-divider { border: none; border-top: 1px solid var(--border); margin: 1rem 0; }
.modal-actions { display: flex; gap: .75rem; justify-content: flex-end; margin-top: 1.25rem; }

/* User picker list */
.user-pick-list { margin-top: .75rem; display: flex; flex-direction: column; gap: .35rem; }
.user-pick-item {
  display: flex; align-items: center; gap: .75rem;
  padding: .65rem .85rem; border-radius: 10px;
  cursor: pointer; transition: background .12s;
}
.user-pick-item:hover { background: var(--input-bg); }
.user-pick-name { font-weight: 600; color: var(--text); font-size: .9rem; }
.user-pick-handle { font-size: .8rem; color: var(--text-muted); }

/* Profile modal */
.profile-avatar-section {
  display: flex; flex-direction: column; align-items: center;
  gap: .75rem; margin-bottom: 1.25rem;
}
/* User profile modal */
.user-profile-body { text-align: center; }
.user-profile-name { font-size: 1.15rem; font-weight: 700; color: var(--text); margin-top: .5rem; }
.user-profile-username { font-size: .9rem; color: var(--text-muted); margin-top: .15rem; }

/* Chat header clickable elements */
#chatHeaderAvatar, #chatHeaderName, #chatHeaderSub { cursor: pointer; }

.settings-title { font-size: .95rem; font-weight: 700; color: var(--text); margin-bottom: .75rem; }
.form-label { font-size: .875rem; font-weight: 600; color: var(--text); }
.form-input {
  padding: .6rem .85rem; border: 1.5px solid var(--border);
  border-radius: 8px; font-size: .925rem; outline: none;
  background: var(--input-bg); color: var(--text); width: 100%;
  transition: border-color .15s;
}
.form-input:focus { border-color: var(--accent); }
.form-input:disabled { opacity: .55; cursor: not-allowed; }
.form-select { cursor: pointer; }
.form-inline { flex-direction: row; align-items: center; justify-content: space-between; }
.form-inline .form-label { margin: 0; }

/* Toggle switch */
.toggle { position: relative; display: inline-block; width: 44px; height: 24px; }
.toggle input { opacity: 0; width: 0; height: 0; }
.toggle-slider {
  position: absolute; inset: 0; border-radius: 24px;
  background: var(--toggle-off); cursor: pointer;
  transition: background .2s;
}
.toggle-slider::before {
  content: ''; position: absolute;
  width: 18px; height: 18px; border-radius: 50%;
  background: #fff; left: 3px; top: 3px;
  transition: transform .2s;
}
.toggle input:checked + .toggle-slider { background: var(--toggle-on); }
.toggle input:checked + .toggle-slider::before { transform: translateX(20px); }

/* ── Toast ──────────────────────────────────────────────────── */
.toast {
  position: fixed; bottom: 1.5rem; left: 50%; transform: translateX(-50%);
  background: #323232; color: #fff; font-size: .9rem;
  padding: .65rem 1.25rem; border-radius: 24px;
  box-shadow: 0 4px 16px rgba(0,0,0,.25); z-index: 500;
  max-width: 90vw; text-align: center;
  opacity: 0; transition: opacity .2s;
  pointer-events: none;
}
.toast.show { opacity: 1; pointer-events: auto; }

/* ── Mobile (≤768px) ────────────────────────────────────────── */
@media (max-width: 768px) {
  .sidebar { width: 100%; flex-shrink: 0; }
  .chat-panel { position: absolute; inset: 0; transform: translateX(100%); transition: transform .25s ease; }
  .layout.chat-open .sidebar { display: none; }
  .layout.chat-open .chat-panel { transform: translateX(0); position: relative; flex: 1; }
  .back-btn { display: inline-flex !important; }
  .msg-row { max-width: 85%; }
  .modal { max-height: 85vh; max-height: 85dvh; }
  .modal-body { overflow-y: auto; }
  .user-profile-body { text-align: center; padding: 1.5rem 1rem; }
}

/* ── Desktop (>768px) ───────────────────────────────────────── */
@media (min-width: 769px) {
  .chat-panel { position: relative; transform: none !important; }
  .sidebar { display: flex !important; }
}

/* Keep scrolling, but remove scrollbar buttons/arrows (WebKit/Blink) */
.composer-input {
  overflow-y: auto;              /* keep scroll */
  scrollbar-width: thin;         /* Firefox */
}

/* Chrome/Edge/WebView2 */
.composer-input::-webkit-scrollbar {
  width: 6px;                    /* thin scrollbar */
}

.composer-input::-webkit-scrollbar-button {
  display: none;                 /* removes the arrow buttons */
  width: 0;
  height: 0;
}

.composer-input::-webkit-scrollbar-corner {
  background: transparent;
}

/* Extra: some WebView variants */
.composer-input::-webkit-scrollbar-track-piece:start,
.composer-input::-webkit-scrollbar-track-piece:end {
  background: transparent;
}