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

:root {
  --bg-primary: #111b21;
  --bg-secondary: #202c33;
  --bg-chat: #0b141a;
  --bg-sidebar: #111b21;
  --bg-input: #2a3942;
  --bg-msg-incoming: #202c33;
  --bg-msg-outgoing: #005c4b;
  --bg-msg-manual: #1a3a5c;
  --text-primary: #e9edef;
  --text-secondary: #8696a0;
  --border-color: #313d45;
  --accent: #00a884;
  --accent-hover: #06cf9c;
  --danger: #ea4335;
  --unread-badge: #00a884;
  --hover: #2a3942;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
}

#app {
  display: flex;
  height: 100vh;
  max-width: 1600px;
  margin: 0 auto;
  overflow: hidden;
}

.hidden {
  display: none !important;
}

/* ===== LOGIN ===== */
#login-screen {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-primary);
}

.login-box {
  background: var(--bg-secondary);
  border-radius: 12px;
  padding: 40px;
  width: 360px;
  text-align: center;
  border: 1px solid var(--border-color);
}

.login-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.login-box h2 {
  font-size: 22px;
  font-weight: 600;
  margin-bottom: 6px;
}

.login-box p {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 24px;
}

#login-form input {
  width: 100%;
  padding: 12px 14px;
  border-radius: 8px;
  border: 1px solid var(--border-color);
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 15px;
  outline: none;
  margin-bottom: 12px;
}

#login-form input:focus {
  border-color: var(--accent);
}

#login-form button {
  width: 100%;
  padding: 12px;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s;
}

#login-form button:hover {
  background: var(--accent-hover);
}

#login-error {
  color: var(--danger);
  font-size: 13px;
  margin-top: 12px;
}

/* ===== SIDEBAR ===== */
#sidebar {
  width: 380px;
  min-width: 320px;
  border-right: 1px solid var(--border-color);
  display: flex;
  flex-direction: column;
  background: var(--bg-sidebar);
  min-height: 0;
  overflow: hidden;
}

.sidebar-header {
  padding: 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
}

.sidebar-header h2 {
  font-size: 20px;
  font-weight: 600;
  margin-bottom: 12px;
  color: var(--text-primary);
}

.search-box input {
  width: 100%;
  padding: 8px 14px;
  border-radius: 8px;
  border: none;
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
}

.search-box input::placeholder {
  color: var(--text-secondary);
}

#contacts-list {
  flex: 1;
  overflow-y: auto;
}

/* Contact item */
.contact-item {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--border-color);
  transition: background 0.15s;
}

.contact-item:hover {
  background: var(--hover);
}

.contact-item.active {
  background: var(--bg-input);
}

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

.contact-info {
  flex: 1;
  min-width: 0;
}

.contact-name {
  font-size: 16px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.contact-last-msg {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.contact-meta {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  margin-left: 8px;
  flex-shrink: 0;
}

.contact-time {
  font-size: 12px;
  color: var(--text-secondary);
}

.unread-badge {
  background: var(--unread-badge);
  color: #fff;
  border-radius: 50%;
  min-width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  margin-top: 4px;
}

/* ===== CHAT AREA ===== */
#chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--bg-chat);
  min-height: 0;
  overflow: hidden;
}

/* Empty state */
#empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-secondary);
}

#empty-state .empty-icon {
  font-size: 64px;
  margin-bottom: 16px;
}

#empty-state h3 {
  font-size: 28px;
  font-weight: 300;
  color: var(--text-primary);
  margin-bottom: 8px;
}

#empty-state p {
  font-size: 14px;
}

/* Chat view */
#chat-view {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

#chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  background: var(--bg-secondary);
  border-bottom: 1px solid var(--border-color);
}

.chat-contact-info {
  display: flex;
  align-items: center;
}

.chat-contact-info .avatar {
  width: 40px;
  height: 40px;
  font-size: 16px;
}

#chat-contact-name {
  font-size: 16px;
  font-weight: 500;
}

.subtitle {
  font-size: 12px;
  color: var(--text-secondary);
}

.chat-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

#chat-search-input {
  padding: 6px 12px;
  border-radius: 6px;
  border: none;
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 13px;
  outline: none;
  width: 200px;
}

#chat-search-input::placeholder {
  color: var(--text-secondary);
}

#chat-search-input:focus {
  outline: 1px solid var(--accent);
}

.chat-actions button {
  padding: 6px 14px;
  border-radius: 6px;
  border: none;
  cursor: pointer;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.2s;
}

#btn-toggle-bot {
  background: var(--accent);
  color: #fff;
}

#btn-toggle-bot:hover {
  background: var(--accent-hover);
}

#btn-toggle-bot.paused {
  background: var(--danger);
}

/* Messages */
#messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 20px 60px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

#messages-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: auto;
}

.message {
  max-width: 65%;
  padding: 6px 10px 6px 10px;
  border-radius: 8px;
  font-size: 14px;
  line-height: 1.45;
  word-wrap: break-word;
  position: relative;
}

.message.incoming {
  align-self: flex-start;
  background: var(--bg-msg-incoming);
  border-top-left-radius: 0;
}

.message.outgoing {
  align-self: flex-end;
  background: var(--bg-msg-outgoing);
  border-top-right-radius: 0;
}

.message.manual {
  align-self: flex-end;
  background: var(--bg-msg-manual);
  border-top-right-radius: 0;
}

.message-content {
  white-space: pre-wrap;
}

.message-time {
  font-size: 11px;
  color: rgba(255,255,255,0.5);
  text-align: right;
  margin-top: 2px;
}

.message-label {
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  margin-bottom: 2px;
}

.message.outgoing .message-label {
  color: rgba(255,255,255,0.6);
}

.message.manual .message-label {
  color: #7cb3e0;
}

/* Date separator */
.date-separator {
  text-align: center;
  margin: 16px 0;
}

.date-separator span {
  background: var(--bg-secondary);
  color: var(--text-secondary);
  font-size: 12px;
  padding: 5px 14px;
  border-radius: 8px;
}

/* Input area */
#message-input-area {
  display: flex;
  align-items: flex-end;
  padding: 10px 16px;
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
  gap: 10px;
}

.input-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.input-actions button {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.15s;
  flex-shrink: 0;
}

.input-actions button:hover {
  color: var(--text-primary);
  background: var(--bg-input);
}

/* Templates dropdown */
.template-wrapper {
  position: relative;
}

#templates-dropdown {
  position: absolute;
  bottom: 48px;
  left: 0;
  width: 300px;
  max-height: 320px;
  overflow-y: auto;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 10px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.4);
  z-index: 50;
}

.templates-header {
  padding: 12px 16px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border-color);
}

.template-item {
  padding: 10px 16px;
  cursor: pointer;
  border-bottom: 1px solid var(--border-color);
  transition: background 0.15s;
}

.template-item:last-child {
  border-bottom: none;
}

.template-item:hover {
  background: var(--hover);
}

.template-item .template-title {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-primary);
  margin-bottom: 2px;
}

.template-item .template-preview {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#message-input {
  flex: 1;
  background: var(--bg-input);
  border: none;
  border-radius: 8px;
  color: var(--text-primary);
  font-size: 15px;
  padding: 10px 14px;
  resize: none;
  outline: none;
  max-height: 150px;
  font-family: inherit;
  line-height: 1.4;
}

#message-input::placeholder {
  color: var(--text-secondary);
}

#btn-send {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s;
}

#btn-send:hover {
  background: var(--accent-hover);
}

/* ===== MODAL ===== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0,0,0,0.6);
}

.modal-content {
  position: relative;
  background: var(--bg-secondary);
  border-radius: 12px;
  width: 420px;
  max-width: 90vw;
  max-height: 80vh;
  overflow-y: auto;
  border: 1px solid var(--border-color);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-color);
}

.modal-header h3 {
  font-size: 16px;
  font-weight: 600;
}

#modal-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 24px;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
}

#modal-close:hover {
  color: var(--text-primary);
}

#modal-body {
  padding: 20px;
}

.modal-row {
  display: flex;
  justify-content: space-between;
  padding: 8px 0;
  border-bottom: 1px solid var(--border-color);
  font-size: 14px;
}

.modal-row:last-child {
  border-bottom: none;
}

.modal-row .label {
  color: var(--text-secondary);
  flex-shrink: 0;
  margin-right: 12px;
}

.modal-row .value {
  text-align: right;
  word-break: break-all;
}

.modal-section {
  font-size: 13px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--accent);
  margin-top: 16px;
  margin-bottom: 8px;
}

.modal-section:first-child {
  margin-top: 0;
}

#chat-contact-phone {
  cursor: pointer;
}

#chat-contact-phone:hover {
  text-decoration: underline;
  color: var(--accent);
}

.message.highlight {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ===== MESSAGE ACTIONS ===== */
.message {
  /* permite posicionar o botão de ações no canto */
  padding-right: 28px;
}

.msg-action-btn {
  position: absolute;
  top: 2px;
  right: 2px;
  width: 22px;
  height: 22px;
  border: none;
  background: transparent;
  border-radius: 4px;
  color: rgba(255, 255, 255, 0.65);
  cursor: pointer;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.message:hover .msg-action-btn {
  display: flex;
}

.msg-action-btn:hover {
  background: rgba(0, 0, 0, 0.35);
  color: #fff;
}

.message.incoming .msg-action-btn {
  color: var(--text-secondary);
}

.favorite-star {
  color: #f4c542;
  margin-right: 4px;
  font-size: 12px;
}

.message-forwarded {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  color: rgba(255, 255, 255, 0.55);
  font-style: italic;
  margin-bottom: 2px;
}

.message.incoming .message-forwarded {
  color: var(--text-secondary);
}

.message.deleted {
  opacity: 0.85;
}

/* Citação dentro da mensagem (resposta) */
.message-quote {
  background: rgba(0, 0, 0, 0.25);
  border-left: 3px solid var(--accent);
  border-radius: 4px;
  padding: 4px 8px;
  margin-bottom: 4px;
  cursor: pointer;
  max-width: 100%;
  overflow: hidden;
}

.message.incoming .message-quote {
  background: rgba(255, 255, 255, 0.06);
}

.message-quote:hover {
  filter: brightness(1.1);
}

.message-quote-author {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 2px;
}

.message-quote-text {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.75);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.message.incoming .message-quote-text {
  color: var(--text-secondary);
}

@keyframes quoteFlash {
  0%, 100% { background-color: inherit; }
  30% { background-color: rgba(0, 168, 132, 0.35); }
}

.message.quote-flash {
  animation: quoteFlash 1.2s ease;
}

/* Barra de resposta acima do input */
#reply-bar {
  background: var(--bg-secondary);
  border-top: 1px solid var(--border-color);
  padding: 8px 16px;
}

.reply-bar-inner {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg-input);
  border-radius: 6px;
  padding: 8px 10px;
}

.reply-bar-bar {
  width: 3px;
  align-self: stretch;
  background: var(--accent);
  border-radius: 2px;
  flex-shrink: 0;
}

.reply-bar-content {
  flex: 1;
  min-width: 0;
}

.reply-bar-author {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  margin-bottom: 2px;
}

.reply-bar-text {
  font-size: 13px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#reply-bar-cancel {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 20px;
  line-height: 1;
  padding: 0 4px;
}

#reply-bar-cancel:hover {
  color: var(--text-primary);
}

.deleted-content {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: rgba(255, 255, 255, 0.55);
  font-style: italic;
}

.message.incoming .deleted-content {
  color: var(--text-secondary);
}

/* Menu flutuante */
.message-menu {
  position: fixed;
  z-index: 200;
  background: var(--bg-secondary);
  border: 1px solid var(--border-color);
  border-radius: 8px;
  padding: 4px;
  min-width: 180px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.4);
  display: flex;
  flex-direction: column;
}

.message-menu .menu-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: none;
  border: none;
  color: var(--text-primary);
  cursor: pointer;
  font-size: 14px;
  text-align: left;
  border-radius: 6px;
}

.message-menu .menu-item:hover {
  background: var(--hover);
}

.message-menu .menu-item.danger {
  color: var(--danger);
}

.message-menu .menu-item.danger:hover {
  background: rgba(234, 67, 53, 0.12);
}

/* ===== FORWARD MODAL ===== */
.modal-close-btn {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 24px;
  cursor: pointer;
  padding: 0 4px;
  line-height: 1;
}

.modal-close-btn:hover {
  color: var(--text-primary);
}

.modal-body-scroll {
  padding: 16px 20px;
  overflow-y: auto;
  max-height: 60vh;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 12px 20px;
  border-top: 1px solid var(--border-color);
}

.btn-primary,
.btn-secondary {
  padding: 8px 16px;
  border-radius: 6px;
  border: none;
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-primary:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.btn-secondary {
  background: var(--bg-input);
  color: var(--text-primary);
}

.btn-secondary:hover {
  background: var(--hover);
}

.forward-preview {
  background: var(--bg-input);
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 12px;
  white-space: pre-wrap;
  max-height: 90px;
  overflow-y: auto;
  border-left: 3px solid var(--accent);
}

.forward-search {
  width: 100%;
  padding: 8px 12px;
  border-radius: 6px;
  border: none;
  background: var(--bg-input);
  color: var(--text-primary);
  font-size: 14px;
  outline: none;
  margin-bottom: 10px;
}

#forward-contacts {
  max-height: 240px;
  overflow-y: auto;
  margin-bottom: 10px;
}

.forward-contact-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 6px;
  border-radius: 6px;
  cursor: pointer;
}

.forward-contact-row:hover {
  background: var(--hover);
}

.forward-contact-row input[type=checkbox] {
  accent-color: var(--accent);
  width: 16px;
  height: 16px;
  cursor: pointer;
}

.avatar.small {
  width: 32px;
  height: 32px;
  font-size: 13px;
  margin-right: 0;
}

.forward-contact-info {
  flex: 1;
  min-width: 0;
}

.forward-contact-name {
  font-size: 14px;
  font-weight: 500;
}

.forward-contact-phone {
  font-size: 12px;
  color: var(--text-secondary);
}

.forward-other {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 0;
  border-top: 1px solid var(--border-color);
  margin-top: 6px;
}

.forward-other label {
  font-size: 12px;
  color: var(--text-secondary);
}

.forward-other input {
  padding: 8px 10px;
  border-radius: 6px;
  border: none;
  background: var(--bg-input);
  color: var(--text-primary);
  outline: none;
  font-size: 14px;
}

#forward-add-other {
  align-self: flex-start;
  padding: 6px 12px;
  border-radius: 6px;
  border: none;
  background: var(--bg-input);
  color: var(--text-primary);
  cursor: pointer;
  font-size: 13px;
}

#forward-add-other:hover {
  background: var(--hover);
}

.forward-selected {
  margin-top: 10px;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.forward-chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  background: var(--bg-input);
  border-radius: 14px;
  padding: 4px 10px;
  font-size: 12px;
}

.forward-chip button {
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  font-size: 14px;
  line-height: 1;
  padding: 0;
}

.forward-chip button:hover {
  color: var(--danger);
}

.muted {
  color: var(--text-secondary);
  font-size: 13px;
}

/* ===== FAVORITES MODAL ===== */
#btn-favorites {
  width: 32px;
  height: 32px;
  padding: 0 !important;
  border-radius: 50%;
  background: transparent;
  color: var(--text-secondary);
  display: flex;
  align-items: center;
  justify-content: center;
}

#btn-favorites:hover {
  background: var(--bg-input);
  color: #f4c542;
}

.favorite-item {
  padding: 12px;
  border-bottom: 1px solid var(--border-color);
}

.favorite-item:last-child {
  border-bottom: none;
}

.favorite-header {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.favorite-author {
  font-weight: 600;
  color: var(--accent);
}

.favorite-content {
  font-size: 14px;
  white-space: pre-wrap;
  word-break: break-word;
}

.favorite-actions {
  margin-top: 6px;
}

.favorite-actions button {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 12px;
  cursor: pointer;
  padding: 4px 0;
}

.favorite-actions button:hover {
  color: var(--danger);
}

/* Scrollbar */
::-webkit-scrollbar {
  width: 6px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border-color);
  border-radius: 3px;
}

/* Responsive */
@media (max-width: 768px) {
  #sidebar {
    width: 100%;
    min-width: unset;
  }

  #chat-area {
    display: none;
  }

  #app.chat-open #sidebar {
    display: none;
  }

  #app.chat-open #chat-area {
    display: flex;
  }

  #messages-container {
    padding: 12px 16px;
  }

  .message {
    max-width: 85%;
  }
}
