/* ─────────────────────────────────────────────────────────────────────── *
 *  Chatty — Dark WhatsApp-inspired theme                                  *
 * ─────────────────────────────────────────────────────────────────────── */

/* ── Variables ─────────────────────────────────────────────────────────── */
:root {
  --bg:           #111b21;
  --sidebar-bg:   #111b21;
  --chat-bg:      #0b141a;
  --header-bg:    #202c33;
  --bubble-sent:  #005c4b;
  --bubble-recv:  #202c33;
  --accent:       #00a884;
  --accent-hover: #00c79a;
  --text:         #e9edef;
  --text-muted:   #8696a0;
  --border:       #2a3942;
  --hover:        #2a3942;
  --input-bg:     #2a3942;
  --danger:       #f15c6d;
  --radius:       10px;
  --sidebar-w:    360px;
  --transition:   0.18s ease;
  font-size: 15px;
}

/* ── Reset ─────────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { 
  height: 100%; 
  width: 100%;
  overflow: clip; 
  position: fixed; 
  overscroll-behavior: none;
}
body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  margin: 0;
}

img { max-width: 100%; height: auto; }
button { cursor: pointer; border: none; background: none; color: inherit; font: inherit; }
input, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }

/* ── Utility ───────────────────────────────────────────────────────────── */
.hidden  { display: none !important; }
.screen  { position: fixed; inset: 0; overflow: hidden; height: 100dvh; width: 100vw; }
.active  { display: flex; }

/* ── Login screen ──────────────────────────────────────────────────────── */
#login-screen {
  align-items: center;
  justify-content: center;
  background: var(--bg);
  padding: 1.5rem;
}
.login-card {
  width: 100%;
  max-width: 380px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
  animation: fadeUp 0.35s ease;
}
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(20px); }
  to   { opacity: 1; transform: translateY(0); }
}
.login-logo img {
  border-radius: 24px;
  box-shadow: 0 8px 32px rgba(0,168,132,.35);
}
.login-card h1 { font-size: 1.8rem; font-weight: 600; color: var(--text); }
.login-subtitle { color: var(--text-muted); font-size: .9rem; }
.login-card form { width: 100%; display: flex; flex-direction: column; gap: .75rem; }
.field input {
  width: 100%;
  padding: .75rem 1rem;
  background: var(--input-bg);
  border: 1.5px solid transparent;
  border-radius: var(--radius);
  color: var(--text);
  outline: none;
  transition: border-color var(--transition);
}
.field input:focus { border-color: var(--accent); }
.login-hint { font-size: .8rem; color: var(--text-muted); min-height: 1.2em; text-align: center; }
.login-error {
  font-size: .85rem;
  color: var(--danger);
  text-align: center;
  padding: .5rem;
  background: rgba(241,92,109,.08);
  border-radius: 8px;
}
.btn-primary {
  padding: .8rem;
  background: var(--accent);
  color: #fff;
  border-radius: var(--radius);
  font-weight: 600;
  font-size: .95rem;
  transition: background var(--transition), transform .1s;
  will-change: transform;
}
.btn-primary:hover  { background: var(--accent-hover); }
.btn-primary:active { transform: scale(.97); }

/* ── Main layout ───────────────────────────────────────────────────────── */
body {
  background: var(--bg);
  overflow: hidden;
  height: 100dvh;
  margin: 0;
}

#main-screen, #login-screen, #admin-screen, .screen {
  width: 100vw;
  height: 100dvh;
  height: -webkit-fill-available;
  position: relative;
  overflow: hidden;
}

#main-screen {
  display: flex;
  flex-direction: row;
}

#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
}



/* ── Sidebar ───────────────────────────────────────────────────────────── */
.sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  overflow: hidden;
  transition: transform var(--transition);
}
.sidebar-header {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: calc(env(safe-area-inset-top, 0px) + 0.75rem) 1rem 0.75rem;
  background: var(--header-bg);
  min-height: calc(env(safe-area-inset-top, 0px) + 60px);
}

.user-avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: grid; place-items: center;
  font-weight: 700; font-size: 1rem;
  flex-shrink: 0;
}
.username-label { font-weight: 600; flex: 1; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.header-actions { display: flex; gap: .25rem; }

.search-wrap { padding: .5rem .75rem; background: var(--bg); }
.search-wrap input {
  width: 100%;
  padding: .5rem .85rem;
  background: var(--input-bg);
  border: none;
  border-radius: 8px;
  outline: none;
  color: var(--text);
  font-size: .9rem;
}
.search-wrap input::placeholder { color: var(--text-muted); }

.sidebar-body { flex: 1; overflow-y: auto; overscroll-behavior: contain; }
.sidebar-body::-webkit-scrollbar { width: 4px; }
.sidebar-body::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.section-header {
  padding: .75rem 1rem .35rem;
  font-size: .75rem;
  font-weight: 700;
  letter-spacing: .05em;
  text-transform: uppercase;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
  cursor: pointer;
  user-select: none;
  transition: color var(--transition);
}
.section-header:hover { color: var(--text); }
.section-header::after {
  content: '▼';
  font-size: .6rem;
  transition: transform var(--transition);
}
.section-header.is-collapsed::after { transform: rotate(-90deg); }

.conv-list {
  display: flex;
  flex-direction: column;
  padding-bottom: 1.5rem;
}
.conv-list.is-collapsed {
  display: none;
}
.conv-item {
  display: flex !important;
  flex-direction: row !important;
  flex-wrap: nowrap !important;
  align-items: center;
  gap: 1rem;
  padding: .85rem 1.25rem;
  cursor: pointer;
  transition: background var(--transition);
  position: relative;
  width: 100%;
}
.conv-item:hover  { background: var(--hover); }
.conv-item.active { background: var(--border); }
.conv-avatar {
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--header-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.15rem;
  flex-shrink: 0;
  font-weight: 600;
  color: var(--accent);
}
.conv-info { 
  flex: 1; 
  min-width: 0; 
  display: flex; 
  flex-direction: column; 
  gap: 2px;
}
.conv-name { 
  font-weight: 600; 
  font-size: 1.05rem; 
  color: var(--text);
  line-height: 1.2; 
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}
.conv-name span {
  white-space: nowrap; 
  overflow: hidden; 
  text-overflow: ellipsis; 
  flex: 1;
}

.conv-last { 
  font-size: .85rem; 
  color: var(--text-muted); 
  white-space: nowrap; 
  overflow: hidden; 
  text-overflow: ellipsis; 
}

.unread-badge {
  background: #ff4b2b;
  color: #fff;
  font-size: .7rem;
  font-weight: 700;
  min-width: 1.2rem;
  height: 1.2rem;
  padding: 0 .4rem;
  border-radius: 1rem;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-left: auto;
  box-shadow: 0 2px 4px rgba(255,75,43,0.3);
}


.conn-status {
  padding: .4rem 1rem;
  font-size: .75rem;
  text-align: center;
  transition: all var(--transition);
}
.conn-status.connected    { color: var(--accent); }
.conn-status.disconnected { color: var(--text-muted); }

/* ── Chat area ─────────────────────────────────────────────────────────── */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--chat-bg);
  overflow: hidden;
  height: 100dvh;
  height: -webkit-fill-available;
  min-height: 0;
}


.welcome-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  color: var(--text-muted);
  text-align: center;
  padding: 2rem;
}
.welcome-icon { font-size: 4rem; }
.welcome-state h2 { color: var(--text); font-size: 1.4rem; }

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

.chat-header {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: calc(env(safe-area-inset-top, 0px) + 0.65rem) 1rem 0.65rem;
  background: var(--header-bg);
  min-height: calc(env(safe-area-inset-top, 0px) + 60px);
  border-bottom: 1px solid var(--border);
}

.chat-avatar {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: grid; place-items: center;
  font-weight: 700;
  flex-shrink: 0;
}
.chat-header-info { flex: 1; min-width: 0; }
.chat-name { font-weight: 600; }
.chat-sub  { font-size: .8rem; color: var(--text-muted); }

/* Messages */
.messages {
  flex: 1;
  flex-shrink: 1;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 1rem .75rem;
  display: flex;
  flex-direction: column;
  gap: .25rem;
  min-height: 0;
  max-height: 100%;
}
.messages::-webkit-scrollbar { width: 4px; }
.messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.load-more-btn {
  width: 100%;
  padding: .75rem;
  background: transparent;
  color: var(--accent);
  border: 1px dashed var(--border);
  border-radius: 8px;
  font-size: .8rem;
  font-weight: 600;
  margin-bottom: .5rem;
  transition: background var(--transition);
}
.load-more-btn:hover { background: var(--hover); }
.load-more-btn:disabled { opacity: 0.5; pointer-events: none; }

.msg-row {
  display: flex;
  flex-direction: column;
  max-width: 72%;
  animation: msgIn .15s ease;
}
@keyframes msgIn {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}
.msg-row.sent  { align-self: flex-end; align-items: flex-end; }
.msg-row.recv  { align-self: flex-start; align-items: flex-start; }

.msg-sender {
  font-size: .72rem;
  color: var(--accent);
  font-weight: 600;
  padding: 0 .5rem;
  margin-bottom: .15rem;
}
.msg-bubble {
  padding: .5rem .75rem;
  border-radius: 8px;
  word-break: break-word;
  line-height: 1.45;
  position: relative;
}
.msg-row.sent .msg-bubble {
  background: var(--bubble-sent);
  border-bottom-right-radius: 2px;
}
.msg-row.recv .msg-bubble {
  background: var(--bubble-recv);
  border-bottom-left-radius: 2px;
}
.msg-image {
  max-width: 260px;
  border-radius: 8px;
  cursor: pointer;
  display: block;
}
.msg-time {
  font-size: .68rem;
  color: var(--text-muted);
  padding: .1rem .5rem;
  margin-top: .1rem;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 4px;
}
.msg-tick {
  font-size: .75rem;
  letter-spacing: -1px;
}
.tick-read {
  color: #53bdeb;
}
.day-divider {
  align-self: center;
  background: var(--header-bg);
  color: var(--text-muted);
  font-size: .72rem;
  padding: .25rem .75rem;
  border-radius: 10px;
  margin: .5rem 0;
}

/* Input area */
.input-wrap {
  display: flex;
  align-items: flex-end;
  gap: .4rem;
  padding: .6rem .75rem;
  padding-bottom: calc(0.6rem + env(safe-area-inset-bottom, 0px));
  background: var(--header-bg);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

.image-preview {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: .4rem;
  padding: .3rem .5rem;
  background: var(--input-bg);
  border-radius: 8px;
}
.image-preview img { height: 48px; width: 48px; object-fit: cover; border-radius: 6px; }
.image-preview button {
  color: var(--text-muted);
  font-size: .9rem;
  line-height: 1;
  padding: 2px;
}
.msg-input {
  flex: 1;
  resize: none;
  background: var(--input-bg);
  border: none;
  border-radius: var(--radius);
  padding: .6rem .85rem;
  color: var(--text);
  outline: none;
  max-height: 140px;
  overflow-y: auto;
  line-height: 1.45;
}
.msg-input::placeholder { color: var(--text-muted); }
.msg-input::-webkit-scrollbar { width: 3px; }
.msg-input::-webkit-scrollbar-thumb { background: var(--border); }

.icon-btn {
  width: 38px; height: 38px;
  border-radius: 50%;
  display: grid; place-items: center;
  color: var(--text-muted);
  font-size: 1.05rem;
  transition: background var(--transition), color var(--transition);
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.back-btn { font-size: 1.2rem; display: none; }

.send-btn {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: grid; place-items: center;
  flex-shrink: 0;
  transition: background var(--transition), transform .1s;
}
.send-btn:hover  { background: var(--accent-hover); }
.send-btn:active { transform: scale(.92); }

/* Emoji panel */
.emoji-panel {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  padding: .5rem;
  background: var(--header-bg);
  border-top: 1px solid var(--border);
  max-height: 180px;
  overflow-y: auto;
}
.emoji-btn-item {
  font-size: 1.3rem;
  padding: .25rem .35rem;
  border-radius: 6px;
  cursor: pointer;
  transition: background .12s;
  background: none; border: none;
}
.emoji-btn-item:hover { background: var(--hover); }

.login-pending {
  color: var(--accent);
  background: rgba(0, 168, 132, 0.1);
  padding: .75rem;
  border-radius: 8px;
  text-align: center;
  font-size: .85rem;
  margin-top: .5rem;
  border: 1px solid var(--accent);
}

/* ── Admin Console ─────────────────────────────────────────────────────── */
#admin-screen { display: flex; flex-direction: row; }
.admin-sidebar {
  width: 280px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}
.admin-nav { padding: 1rem; display: flex; flex-direction: column; gap: .5rem; }
.admin-nav-item {
  padding: .75rem 1rem;
  border-radius: 8px;
  cursor: pointer;
  transition: background var(--transition);
  font-weight: 500;
}
.admin-nav-item:hover { background: var(--hover); }
.admin-nav-item.active { background: var(--border); color: var(--accent); }

.admin-content { flex: 1; overflow-y: auto; padding: 2rem; background: var(--bg); }
.admin-section h2 { margin-bottom: 1.5rem; font-size: 1.4rem; }
.admin-table { display: flex; flex-direction: column; gap: .75rem; }
.admin-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.25rem;
  background: var(--header-bg);
  border-radius: 12px;
  border: 1px solid var(--border);
}
.admin-row-name { font-weight: 600; font-size: 1.1rem; }
.admin-actions { display: flex; gap: .75rem; }

.btn-admin-del { padding: .5rem 1rem; background: var(--danger); color: white; border-radius: 6px; font-size: .8rem; font-weight: 600; }
.btn-admin-app { padding: .5rem 1rem; background: var(--accent); color: white; border-radius: 6px; font-size: .8rem; font-weight: 600; }

/* ── Icons & misc ──────────────────────────────────────────────────────── */
.spinner {
  width: 18px; height: 18px;
  border: 2px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  margin: auto;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ── Mobile (<= 768 px) ────────────────────────────────────────────────── */
@media (max-width: 768px) {
  :root { --sidebar-w: 100%; font-size: 14px; }
  
  html, body { 
    position: fixed; 
    width: 100%; 
    height: 100%; 
    overflow: hidden; 
  }

  .sidebar { 
    width: 100%; 
    position: absolute; 
    inset: 0; 
    z-index: 10; 
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  }

  .chat-area { 
    position: absolute; 
    inset: 0; 
    z-index: 20; 
    transform: translateX(100%); 
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    background: var(--chat-bg);
  }

  /* When chat is open, slide it in and slightly shift sidebar or hide it */
  #main-screen.chat-open .chat-area { transform: translateX(0); }
  #main-screen.chat-open .sidebar { transform: translateX(-20%); }

  .back-btn { display: grid; } /* Show back button on mobile */

  .msg-row { max-width: 90%; }
  
  /* Prevent zoom on input focus for iOS */
  input, textarea { font-size: 16px !important; }
}
