:root {
  --primary: #241c4e;
  --primary-light: #3d3178;
  --primary-soft: #efedf7;
  --bg: #f5f6fa;
  --text: #2a2a35;
  --text-muted: #6b6b7b;
  --border: #e3e3ec;
  --success: #1f9d55;
  --warning: #d69e2e;
  --danger: #d64545;
  --white: #ffffff;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(36, 28, 78, 0.08), 0 1px 2px rgba(36, 28, 78, 0.06);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

a { color: var(--primary-light); text-decoration: none; }
a:hover { text-decoration: underline; }

/* ---------- Layout admin (sidebar) ---------- */

.app-shell {
  display: flex;
  min-height: 100vh;
}

.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--primary);
  color: #fff;
  display: flex;
  flex-direction: column;
  padding: 24px 0;
}

.sidebar .brand {
  padding: 0 20px 24px;
  display: flex;
  align-items: center;
}

.sidebar .brand img {
  max-width: 100%;
  height: auto;
  filter: brightness(0) invert(1);
}

.sidebar nav {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 0 12px;
}

.sidebar nav a {
  color: rgba(255, 255, 255, 0.75);
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 500;
  text-decoration: none;
}

.sidebar nav a:hover {
  background: rgba(255, 255, 255, 0.08);
  color: #fff;
}

.sidebar nav a.active {
  background: rgba(255, 255, 255, 0.14);
  color: #fff;
}

.main {
  flex: 1;
  padding: calc(52px + clamp(16px, 3vw, 36px)) clamp(16px, 3vw, 36px) clamp(16px, 3vw, 36px);
  max-width: 100%;
  overflow-x: auto;
}

.mobile-topbar,
.mobile-tabbar {
  display: none;
}

/* Barra discreta no topo do conteúdo (desktop) com o menu do usuário. Fixa (não
   estica junto com o sidebar quando a lista tem muitos itens), então o botão de
   sair fica sempre visível em vez de preso no fim da página. */
.desktop-topbar {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  position: fixed;
  top: 0;
  left: 240px;
  right: 0;
  height: 52px;
  padding: 0 24px;
  background: var(--white);
  border-bottom: 1px solid var(--border);
  z-index: 500;
}

.user-menu {
  position: relative;
}

.user-menu-toggle {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 50%;
  background: var(--bg);
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.user-menu-toggle:hover,
.user-menu.is-open .user-menu-toggle {
  background: var(--primary-soft);
  color: var(--primary);
}

.user-menu-dropdown {
  display: none;
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  min-width: 150px;
  background: var(--white);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 10px 30px rgba(36, 28, 78, 0.16);
  padding: 6px;
  z-index: 600;
}

.user-menu.is-open .user-menu-dropdown {
  display: block;
}

.user-menu-dropdown form {
  margin: 0;
}

.user-menu-logout {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  background: none;
  border: none;
  border-radius: 6px;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}

.user-menu-logout:hover {
  background: var(--bg);
  color: var(--danger);
}

.main-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
}

.main-header h1 {
  font-size: 22px;
  margin: 0;
}

/* ---------- Componentes ---------- */

.card {
  background: var(--white);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

.stat-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--white);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px 20px;
  border-left: 4px solid var(--primary);
}

.stat-card .label {
  font-size: 13px;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.stat-card .value {
  font-size: 26px;
  font-weight: 700;
  color: var(--primary);
}

.chart-card {
  max-width: 480px;
  margin-bottom: 24px;
}

.chart-card h2 {
  font-size: 16px;
  margin: 0 0 16px;
}

.btn {
  display: inline-block;
  background: var(--primary);
  color: #fff;
  border: none;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
}

.btn:hover { background: var(--primary-light); text-decoration: none; }

.btn-secondary {
  background: var(--white);
  color: var(--primary);
  border: 1px solid var(--border);
}

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

.btn-danger { background: var(--danger); }

.btn-danger:hover { background: #b93a3a; }

.btn-link {
  background: none;
  border: none;
  color: var(--primary-light);
  cursor: pointer;
  font-size: 13px;
  padding: 0;
  text-decoration: underline;
}

.btn-link-danger { color: var(--danger); }

.table-wrap {
  background: var(--white);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow-x: auto;
}

.main-header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.btn-fab {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

/* ---------- Leads (listagem em cards com acordeão) ---------- */

.leads-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-bottom: 20px;
}

.empty-state {
  background: var(--white);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
  color: var(--text-muted);
  margin: 0;
}

.lead-card {
  background: var(--white);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.lead-card-summary {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 18px;
}

.lead-card-toggle {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  background: none;
  border: none;
  padding: 0;
  cursor: pointer;
  text-align: left;
  font: inherit;
  color: inherit;
}

.lead-icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  flex-shrink: 0;
  border: none;
  border-radius: 50%;
  background: var(--bg);
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, transform 0.15s ease;
}

.lead-icon-btn:hover {
  background: var(--primary-soft);
  color: var(--primary);
}

.lead-icon-btn-whatsapp:hover {
  background: #e4f9ec;
  color: #25d366;
}

.lead-icon-btn-danger:hover {
  background: #fbe7e7;
  color: var(--danger);
}

.lead-card-chevron {
  transition: background 0.15s ease, color 0.15s ease, transform 0.15s ease;
}

.lead-card-toggle:hover .lead-card-chevron {
  background: var(--primary-soft);
  color: var(--primary);
}

.lead-card.is-expanded .lead-card-chevron {
  background: var(--primary-soft);
  color: var(--primary);
  transform: rotate(90deg);
}

.lead-card-main {
  min-width: 0;
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 2px 16px;
}

.lead-card-name {
  font-weight: 600;
  font-size: 15px;
}

.lead-card-toolbar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  gap: 14px;
}

.lead-card-divider {
  width: 1px;
  height: 22px;
  flex-shrink: 0;
  background: var(--border);
}

.lead-card-actions-inline {
  display: flex;
  align-items: center;
  gap: 8px;
}

.lead-card-details {
  display: none;
  flex-direction: column;
  gap: 16px;
  padding: 16px 18px 18px;
  border-top: 1px solid var(--border);
}

.lead-card.is-expanded .lead-card-details {
  display: flex;
}

.lead-detail-section {
  background: var(--bg);
  border-radius: 8px;
  padding: 12px 14px 14px;
}

.lead-detail-heading {
  margin: 0 0 10px;
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

.lead-detail-heading-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin: 0 0 10px;
}

.lead-detail-heading-row .lead-detail-heading {
  margin: 0;
}

.lead-richtext-content p,
.lead-richtext-content div {
  margin: 0 0 4px;
}

.lead-richtext-content p:last-child,
.lead-richtext-content div:last-child {
  margin-bottom: 0;
}

.lead-richtext-content a {
  color: var(--primary);
  word-break: break-word;
}

.lead-richtext-content ul,
.lead-richtext-content ol {
  margin: 0 0 4px;
  padding-left: 18px;
}

.lead-detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px 20px;
}

.lead-card-detail span {
  display: block;
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 2px;
}

.lead-card-detail strong {
  font-size: 13px;
  font-weight: 500;
}

.lead-card-detail.full {
  grid-column: 1 / -1;
}

.lead-detail-copyable {
  display: inline-flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px;
  max-width: 100%;
}

.lead-detail-copyable > span {
  overflow-wrap: anywhere;
}

.lead-detail-actions {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.lead-icon-btn-sm {
  width: 30px;
  height: 30px;
}

.lead-copy-btn {
  position: relative;
  flex-shrink: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 5px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.lead-copy-btn:hover {
  background: var(--primary-soft);
  color: var(--primary);
}

.lead-copy-btn.is-copied {
  color: #1fa971;
}

.lead-copy-btn.is-copied::after {
  content: 'Copiado!';
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  margin-bottom: 4px;
  padding: 2px 6px;
  border-radius: 4px;
  background: #1f2430;
  color: #fff;
  font-size: 11px;
  white-space: nowrap;
  pointer-events: none;
}

.load-more-wrap {
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
}

@media (max-width: 560px) {
  .lead-card-summary {
    flex-wrap: wrap;
  }

  .lead-card-toolbar {
    width: 100%;
    justify-content: flex-end;
    flex-wrap: wrap;
  }

  .lead-card-divider {
    display: none;
  }

  .lead-detail-grid {
    grid-template-columns: 1fr;
  }
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

thead th {
  text-align: left;
  padding: 12px 16px;
  background: var(--primary-soft);
  color: var(--primary);
  font-weight: 600;
  white-space: nowrap;
}

tbody td {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  vertical-align: top;
}

tbody tr:hover { background: #fafafe; }

.badge {
  display: inline-block;
  padding: 3px 10px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}

.badge-aguardando_pagamento { background: #fdf2df; color: var(--warning); }
.badge-ativo { background: #e3f6ea; color: var(--success); }
.badge-cancelado { background: #fbe7e7; color: var(--danger); }
.badge-suspenso { background: #ececf5; color: var(--text-muted); }
.badge-a_contatar { background: #e7f0fe; color: #2f6fed; }
.badge-em_negociacao { background: #f1eafe; color: #8b5cf6; }
.badge-indeciso { background: #e2f6f4; color: #0d9488; }
.badge-nao_fechou { background: #fbe9d9; color: #b45309; }
.badge-segmento { background: var(--primary-soft); color: var(--primary); }

.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 14px;
}

.form-group { margin-bottom: 14px; }

.form-group.full { grid-column: 1 / -1; }

/* Sub-grid para os blocos de endereço: CEP/Logradouro/Número em uma linha e
   Complemento/Bairro/Cidade/Estado na outra, em vez de uma linha por campo,
   para aproveitar a largura do modal e reduzir a altura total do formulário. */
.address-grid {
  display: grid;
  gap: 14px;
}

.address-grid--cep { grid-template-columns: 150px 1fr 110px; }
.address-grid--detail { grid-template-columns: 1fr 1fr 1fr 110px; }

@media (max-width: 720px) {
  .address-grid--cep,
  .address-grid--detail {
    grid-template-columns: 1fr;
  }
}

label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text);
}

input[type="text"],
input[type="email"],
input[type="password"],
select,
textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  background: var(--white);
  color: var(--text);
}

input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary-light);
}

textarea { resize: vertical; min-height: 90px; }

.richtext {
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  background: var(--white);
}

.richtext:focus-within {
  border-color: var(--primary-light);
}

.richtext-toolbar {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 2px;
  padding: 6px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.richtext-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 28px;
  height: 28px;
  padding: 0 6px;
  border: none;
  border-radius: 5px;
  background: transparent;
  color: var(--text-muted);
  font-size: 13px;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease;
}

.richtext-btn:hover {
  background: var(--primary-soft);
  color: var(--primary);
}

.richtext-sep {
  width: 1px;
  height: 20px;
  flex-shrink: 0;
  margin: 0 4px;
  background: var(--border);
}

.richtext-body {
  min-height: 70px;
  max-height: 180px;
  overflow-y: auto;
  padding: 10px 12px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
}

.richtext-body:focus {
  outline: none;
}

.richtext-body p,
.richtext-body div {
  margin: 0 0 6px;
}

.richtext-body a {
  color: var(--primary);
  text-decoration: underline;
}

.richtext-body ul,
.richtext-body ol {
  margin: 0 0 6px;
  padding-left: 20px;
}

.actions-row { margin: 24px 0 4px; display: flex; gap: 10px; }

.alert {
  padding: 12px 16px;
  border-radius: 8px;
  font-size: 14px;
  margin-bottom: 18px;
}

.alert-error { background: #fbe7e7; color: var(--danger); }
.alert-success { background: #e3f6ea; color: var(--success); }

/* ---------- Auth (login / 2FA) ---------- */

.auth-shell {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, var(--primary) 0%, #443a86 100%);
  padding: 20px;
}

.auth-card {
  background: var(--white);
  border-radius: 14px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25);
  padding: 36px 32px;
  width: 100%;
  max-width: 380px;
}

.auth-card .logo-wrap {
  text-align: center;
  margin-bottom: 24px;
}

.auth-card .logo-wrap img {
  max-width: 220px;
  height: auto;
}

.auth-card h1 {
  font-size: 18px;
  text-align: center;
  margin: 0 0 20px;
  color: var(--text-muted);
  font-weight: 500;
}

.auth-card .btn { width: 100%; padding: 12px; font-size: 15px; }

.auth-hint {
  text-align: center;
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 16px;
}

/* ---------- Modal ---------- */

.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(36, 28, 78, 0.45);
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 20px;
}

.modal-overlay.is-open { display: flex; }

.modal-box {
  background: var(--white);
  border-radius: var(--radius);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.25);
  width: 100%;
  max-width: 560px;
  max-height: 92vh;
  overflow-y: auto;
  padding: clamp(16px, 3vw, 24px) clamp(16px, 3vw, 24px) clamp(24px, 4vw, 32px);
}

/* Modais com muitos campos (ex: leads) aproveitam a largura disponível em telas maiores
   para reduzir o número de linhas e evitar rolagem no desktop. */
.modal-box--lg {
  max-width: min(96vw, 960px);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 18px;
}

.modal-header h2 {
  margin: 0;
  font-size: 18px;
}

.modal-close {
  background: transparent;
  border: none;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 22px;
  line-height: 1;
  padding: 0;
}

@media (max-width: 720px) {
  .app-shell { flex-direction: column; }

  .sidebar { display: none; }

  .desktop-topbar { display: none; }

  .mobile-topbar {
    display: flex;
    align-items: center;
    justify-content: center;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    background: var(--primary);
    padding: 10px 16px;
    box-shadow: var(--shadow);
    z-index: 900;
  }

  .mobile-topbar img {
    max-width: 140px;
    height: auto;
    filter: brightness(0) invert(1);
  }

  body {
    font-size: 15px;
  }

  .main-header h1 {
    font-size: 24px;
  }

  .btn,
  .btn-secondary {
    font-size: 15px;
  }

  .lead-card-name {
    font-size: 16px;
  }

  .badge {
    font-size: 13px;
  }

  .lead-detail-heading {
    font-size: 12px;
  }

  .lead-card-detail span {
    font-size: 13px;
  }

  .lead-card-detail strong {
    font-size: 14px;
  }

  .mobile-tabbar {
    display: flex;
    align-items: stretch;
    justify-content: space-around;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    background: var(--primary);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
    box-shadow: 0 -1px 3px rgba(36, 28, 78, 0.12);
    z-index: 900;
  }

  .mobile-tabbar a,
  .mobile-tabbar button {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 2px;
    background: none;
    border: none;
    color: rgba(255, 255, 255, 0.75);
    font-size: 11px;
    font-weight: 500;
    font-family: inherit;
    padding: 6px 2px;
    border-radius: 8px;
    cursor: pointer;
  }

  .mobile-tabbar svg {
    width: 22px;
    height: 22px;
  }

  .lead-icon-btn {
    width: 36px;
    height: 36px;
  }

  .lead-icon-btn svg {
    width: 18px;
    height: 18px;
  }

  .lead-icon-btn-sm,
  .lead-copy-btn {
    width: 34px;
    height: 34px;
  }

  .mobile-tabbar a.active {
    color: #fff;
    background: rgba(255, 255, 255, 0.14);
  }

  .mobile-tabbar-logout {
    flex: 1;
    display: flex;
    margin: 0;
  }

  .main {
    padding-top: calc(60px + clamp(40px, 9vw, 64px));
    padding-bottom: calc(60px + clamp(16px, 3vw, 36px));
  }

  .main-header {
    flex-direction: column;
    align-items: stretch;
    gap: 12px;
  }

  .main-header-actions {
    justify-content: flex-end;
  }

  .btn-fab {
    position: fixed;
    right: 16px;
    bottom: calc(70px + env(safe-area-inset-bottom));
    z-index: 950;
    width: 56px;
    height: 56px;
    padding: 0;
    border-radius: 50%;
    justify-content: center;
    box-shadow: 0 4px 14px rgba(36, 28, 78, 0.35);
  }

  .btn-fab .btn-fab-text {
    display: none;
  }

  .btn-fab .btn-fab-plus {
    font-size: 26px;
    line-height: 1;
  }

  .actions-row { flex-direction: column; }
  .actions-row .btn,
  .actions-row .btn-secondary { width: 100%; text-align: center; }
}

/* Em telas de celular o modal vira full-screen: mais espaço útil, sem bordas cortando conteúdo. */
@media (max-width: 480px) {
  .modal-overlay { padding: 0; }

  .modal-box,
  .modal-box--lg {
    max-width: 100%;
    width: 100%;
    height: 100%;
    max-height: 100vh;
    border-radius: 0;
  }

  .auth-card { padding: 28px 20px; }
}
