* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

/* ===== Tema: preto + escala de cinzas (estilo WhatsApp escuro, sem verde) ===== */
:root {
  --bg: #0b0b0b;
  --panel: #121212;
  --surface: #1e1e1e;
  --surface-2: #2a2a2a;
  --chat-bg: #0d0d0d;
  --msg-out: #2b2b2b;
  --msg-in: #1c1c1c;
  --accent: #3a3a3a;
  --accent-hover: #4a4a4a;
  --avatar: #333333;
  --text-primary: #ededed;
  --text-secondary: #8d8d8d;
  --border: #2a2a2a;
  --unread-badge: #d0d0d0;
  --unread-badge-text: #111111;
  --online: #cfcfcf;
  --danger: #c76b6b;
  --danger-bg: #2a1a1a;
  --header-height: 56px;
}

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg);
  height: 100vh;
  overflow: hidden;
  color: var(--text-primary);
}

::-webkit-scrollbar { width: 8px; }
::-webkit-scrollbar-thumb { background: #333; border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

.screen { display: none; height: 100vh; width: 100%; }
.screen.active { display: flex; flex-direction: column; }
.view { display: none; flex-direction: column; flex: 1; overflow: hidden; }
.view.active { display: flex; }
.hidden { display: none !important; }

/* ========== Auth Screen ========== */
.auth-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  padding: 24px;
  background: linear-gradient(180deg, var(--surface) 0%, var(--surface) 30%, var(--bg) 30%);
}

.auth-logo { text-align: center; margin-bottom: 32px; padding-top: 40px; }
.logo-icon { font-size: 64px; color: var(--text-primary); }
.auth-logo h1 { color: var(--text-primary); font-size: 28px; font-weight: 700; margin-top: 8px; }
.auth-subtitle { color: var(--text-secondary); font-size: 14px; margin-top: 4px; }

.auth-form {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5);
}

.auth-form input {
  width: 100%;
  padding: 14px 16px;
  border: 1.5px solid var(--border);
  border-radius: 8px;
  font-size: 15px;
  margin-bottom: 12px;
  transition: border-color 0.2s;
  outline: none;
  background: var(--panel);
  color: var(--text-primary);
}
.auth-form input::placeholder { color: var(--text-secondary); }
.auth-form input:focus { border-color: var(--accent-hover); }

.btn-primary {
  width: 100%;
  padding: 14px;
  background: var(--accent);
  color: var(--text-primary);
  border: none;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:active { transform: scale(0.98); }

.btn-link {
  width: 100%;
  padding: 12px;
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  margin-top: 4px;
}
.btn-link:hover { color: var(--text-primary); }

.error-msg {
  color: var(--danger);
  font-size: 13px;
  text-align: center;
  margin-top: 12px;
  padding: 8px;
  background: var(--danger-bg);
  border-radius: 6px;
}

/* ========== App Header ========== */
.app-header {
  background: var(--surface);
  color: var(--text-primary);
  padding: 0 16px;
  height: var(--header-height);
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 100;
  border-bottom: 1px solid var(--border);
}
.app-header h2 { font-size: 20px; font-weight: 600; }
.header-actions { display: flex; gap: 4px; }

.icon-btn {
  background: none;
  border: none;
  color: inherit;
  padding: 8px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
}
.icon-btn:hover { background: rgba(255,255,255,0.08); }
.icon-btn .material-icons { font-size: 24px; }

/* ========== Contacts List ========== */
.contacts-search {
  padding: 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 8px;
}
.contacts-search .search-icon { color: var(--text-secondary); font-size: 20px; }
.contacts-search input {
  flex: 1;
  border: none;
  outline: none;
  font-size: 15px;
  padding: 8px 0;
  background: transparent;
  color: var(--text-primary);
}
.contacts-search input::placeholder { color: var(--text-secondary); }

.contacts-list { flex: 1; overflow-y: auto; background: var(--panel); }

.contact-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
}
.contact-item:hover { background: var(--surface-2); }
.contact-item:active { background: #333; }

.contact-avatar {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: var(--avatar);
  color: var(--text-primary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  font-weight: 600;
  margin-right: 12px;
  flex-shrink: 0;
}

.contact-info { flex: 1; min-width: 0; }
.contact-name { font-size: 16px; font-weight: 500; color: var(--text-primary); }
.contact-last-msg {
  font-size: 13px; color: var(--text-secondary); margin-top: 2px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.contact-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; }
.contact-time { font-size: 11px; color: var(--text-secondary); }

.unread-badge {
  background: var(--unread-badge);
  color: var(--unread-badge-text);
  font-size: 11px;
  font-weight: 700;
  min-width: 20px;
  height: 20px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 5px;
}

.online-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--online);
  position: absolute;
  bottom: 2px;
  right: 2px;
  border: 2px solid var(--panel);
}
.contact-avatar-wrapper { position: relative; margin-right: 12px; }

.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 24px;
  color: var(--text-secondary);
}
.empty-state .material-icons { font-size: 64px; margin-bottom: 16px; opacity: 0.4; }
.empty-state p { font-size: 15px; }
.empty-state .small { font-size: 13px; margin-top: 8px; display: flex; align-items: center; gap: 4px; }
.inline-icon { font-size: 16px !important; vertical-align: middle; }

/* ========== Chat View ========== */
.chat-header {
  background: var(--surface);
  color: var(--text-primary);
  padding: 0 8px;
  height: var(--header-height);
  display: flex;
  align-items: center;
  gap: 4px;
  border-bottom: 1px solid var(--border);
}
.chat-contact-info { display: flex; align-items: center; gap: 10px; flex: 1; }
.chat-avatar {
  width: 36px; height: 36px; border-radius: 50%;
  background: rgba(255,255,255,0.12);
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 600;
}
.chat-contact-details { display: flex; flex-direction: column; }
#chat-contact-name { font-size: 16px; font-weight: 600; }
.contact-status { font-size: 12px; opacity: 0.8; }

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px;
  background: var(--chat-bg);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.message {
  max-width: 80%;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.4;
  position: relative;
  word-wrap: break-word;
  color: var(--text-primary);
}
.message-out { align-self: flex-end; background: var(--msg-out); border-bottom-right-radius: 2px; }
.message-in {
  align-self: flex-start; background: var(--msg-in);
  border-bottom-left-radius: 2px; box-shadow: 0 1px 1px rgba(0,0,0,0.3);
}
.message-text { margin-bottom: 2px; }
.message-time { font-size: 11px; color: var(--text-secondary); text-align: right; margin-top: 2px; }

.typing-indicator { padding: 8px 16px; display: flex; align-items: center; gap: 8px; background: var(--chat-bg); }
.typing-dots { display: flex; gap: 3px; }
.typing-dots span {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--text-secondary); animation: typing-bounce 1.4s infinite;
}
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing-bounce { 0%, 60%, 100% { transform: translateY(0); } 30% { transform: translateY(-4px); } }
.typing-text { font-size: 12px; color: var(--text-secondary); font-style: italic; }

.chat-input-area { display: flex; align-items: center; padding: 8px 12px; background: var(--surface); gap: 8px; }
#chat-input {
  flex: 1; padding: 10px 16px; border: none; border-radius: 20px;
  font-size: 15px; background: var(--panel); color: var(--text-primary); outline: none;
}
#chat-input::placeholder { color: var(--text-secondary); }

.send-btn {
  width: 40px; height: 40px; border-radius: 50%;
  background: var(--accent); color: var(--text-primary); border: none; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.2s, transform 0.1s;
}
.send-btn:hover { background: var(--accent-hover); }
.send-btn:active { transform: scale(0.9); }
.send-btn .material-icons { font-size: 20px; }

/* ========== Manage Contacts ========== */
.manage-header {
  background: var(--surface);
  color: var(--text-primary);
  padding: 0 8px;
  height: var(--header-height);
  display: flex;
  align-items: center;
  gap: 8px;
  border-bottom: 1px solid var(--border);
}
.manage-header h3 { font-size: 18px; font-weight: 600; }
.manage-section { padding: 16px; }
.manage-section h4 {
  font-size: 13px; font-weight: 600; color: var(--text-secondary);
  text-transform: uppercase; letter-spacing: 0.5px; margin-bottom: 12px;
}
.search-add { display: flex; gap: 8px; }
.search-add input {
  flex: 1; padding: 10px 14px; border: 1.5px solid var(--border);
  border-radius: 8px; font-size: 14px; outline: none;
  background: var(--panel); color: var(--text-primary);
}
.search-add input::placeholder { color: var(--text-secondary); }
.search-add input:focus { border-color: var(--accent-hover); }
.search-add .icon-btn { background: var(--accent); color: var(--text-primary); border-radius: 8px; width: 40px; }

.search-results { margin-top: 8px; }
.search-result-item, .manage-contact-item {
  display: flex; align-items: center; padding: 10px 12px;
  border-radius: 8px; margin-bottom: 4px; transition: background 0.15s;
}
.search-result-item:hover, .manage-contact-item:hover { background: var(--surface-2); }
.search-result-item .contact-avatar, .manage-contact-item .contact-avatar { width: 40px; height: 40px; font-size: 15px; }
.search-result-item .contact-name, .manage-contact-item .contact-name { flex: 1; font-size: 15px; font-weight: 500; }

.btn-add-contact {
  background: var(--accent); color: var(--text-primary); border: none;
  padding: 6px 12px; border-radius: 6px; font-size: 13px; font-weight: 500; cursor: pointer;
}
.btn-add-contact:hover { background: var(--accent-hover); }
.btn-remove-contact { background: none; border: none; color: var(--danger); cursor: pointer; padding: 6px; border-radius: 50%; }
.btn-remove-contact:hover { background: var(--danger-bg); }
.manage-contacts-list { max-height: calc(100vh - 280px); overflow-y: auto; }

/* ========== Animations ========== */
@keyframes slideIn { from { transform: translateX(100%); } to { transform: translateX(0); } }
@keyframes fadeIn { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }
.view.active { animation: fadeIn 0.2s ease-out; }

/* ========== Responsive ========== */
@media (min-width: 768px) {
  body { display: flex; justify-content: center; align-items: center; }
  #app-screen {
    max-width: 420px; max-height: 800px; border-radius: 16px;
    overflow: hidden; box-shadow: 0 8px 40px rgba(0,0,0,0.6);
    border: 1px solid var(--border);
  }
}
