:root {
  --bg: #0f1419;
  --bg-elevated: #1a222c;
  --bg-soft: #243040;
  --border: #2f3d4d;
  --text: #e8eef4;
  --muted: #8fa3b8;
  --accent: #2dd4a8;
  --accent-dim: #1a9e7a;
  --danger: #f07178;
  --warn: #e6c07b;
  --info: #61afef;
  --radius: 10px;
  --font: "DM Sans", "Segoe UI", sans-serif;
  --font-display: "Syne", "DM Sans", sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(45, 212, 168, 0.12), transparent 60%),
    radial-gradient(900px 500px at 100% 0%, rgba(97, 175, 239, 0.08), transparent 50%),
    var(--bg);
  color: var(--text);
  font-family: var(--font);
  min-height: 100vh;
}

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

.layout {
  display: grid;
  grid-template-columns: 240px 1fr;
  min-height: 100vh;
}

.sidebar {
  background: rgba(26, 34, 44, 0.92);
  border-right: 1px solid var(--border);
  padding: 1.5rem 1rem;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  font-family: var(--font-display);
  font-weight: 800;
  font-size: 1.6rem;
  letter-spacing: -0.03em;
  color: var(--text);
  padding: 0 0.5rem 1.5rem;
}
.brand span { color: var(--accent); }
.version-tag {
  margin: -0.75rem 0.5rem 1rem;
  font-size: 0.72rem;
  color: var(--muted);
  font-family: ui-monospace, Consolas, monospace;
}

.nav a {
  display: block;
  padding: 0.65rem 0.85rem;
  border-radius: 8px;
  color: var(--muted);
  margin-bottom: 0.25rem;
  text-decoration: none;
}
.nav a:hover, .nav a.active {
  background: var(--bg-soft);
  color: var(--text);
  text-decoration: none;
}

.main { padding: 1.75rem 2rem; }
.page-title {
  font-family: var(--font-display);
  font-size: 1.75rem;
  font-weight: 700;
  margin: 0 0 0.35rem;
  letter-spacing: -0.02em;
}
.page-sub { color: var(--muted); margin: 0 0 1.5rem; }

.grid {
  display: grid;
  gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
}
.stat {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.25rem;
}
.stat .label { color: var(--muted); font-size: 0.85rem; }
.stat .value {
  font-family: var(--font-display);
  font-size: 1.8rem;
  font-weight: 700;
  margin-top: 0.25rem;
}

.panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  margin-bottom: 1.25rem;
}
.panel h2 {
  font-size: 1.05rem;
  margin: 0 0 1rem;
  font-weight: 600;
}

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.92rem;
}
th, td {
  text-align: left;
  padding: 0.7rem 0.5rem;
  border-bottom: 1px solid var(--border);
}
th { color: var(--muted); font-weight: 500; font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; }

.badge {
  display: inline-block;
  padding: 0.15rem 0.55rem;
  border-radius: 999px;
  font-size: 0.75rem;
  font-weight: 600;
  background: var(--bg-soft);
  color: var(--muted);
}
.badge.ok { background: rgba(45,212,168,.15); color: var(--accent); }
.badge.warn { background: rgba(230,192,123,.15); color: var(--warn); }

.impersonate-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  background: rgba(230,192,123,.18);
  border: 1px solid var(--warn);
  color: var(--warn);
  padding: 0.6rem 1rem;
  border-radius: 8px;
  margin-bottom: 1rem;
  font-size: 0.9rem;
  flex-wrap: wrap;
}
.impersonate-banner strong { color: #fff; }
.impersonate-banner form { margin: 0; }
.badge.err { background: rgba(240,113,120,.15); color: var(--danger); }
.badge.info { background: rgba(97,175,239,.15); color: var(--info); }

.search-bar {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 1rem;
}
.search-bar input[type="search"] {
  flex: 1;
}
.pagination {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-top: 1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--border);
  flex-wrap: wrap;
}
.pagination .muted { font-size: 0.85rem; }
.pagination .pages { display: flex; gap: 0.4rem; align-items: center; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  border: 0;
  border-radius: 8px;
  padding: 0.55rem 1rem;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  background: var(--accent);
  color: #04261c;
  text-decoration: none;
}
.btn:hover { background: #3ee0b5; text-decoration: none; }
.btn:disabled, .btn[disabled] {
  opacity: 0.4;
  cursor: not-allowed;
  background: var(--bg-soft);
  color: var(--muted);
}
input:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.secondary {
  background: var(--bg-soft);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn.danger { background: var(--danger); color: #1a0a0b; }
.btn.sm { padding: 0.35rem 0.7rem; font-size: 0.85rem; }
.btn.disabled { opacity: 0.4; cursor: not-allowed; pointer-events: none; background: var(--bg-soft); color: var(--muted); }

form.inline { display: inline; }
.row-actions {
  display: flex;
  align-items: flex-start;
  gap: 0.5rem;
  flex-wrap: wrap;
}
.row-actions details { flex: 0 0 auto; }
.row-actions details[open] { flex-basis: 100%; }
/* Integrações: 4 botões (Testar/Duplicar/Editar/Excluir) na mesma linha */
#integracoes .row-actions {
  flex-wrap: nowrap;
  gap: 0.3rem;
  align-items: center;
  white-space: nowrap;
}
#integracoes .row-actions .btn {
  padding: 0.22rem 0.48rem;
  font-size: 0.76rem;
  border-radius: 6px;
}
.panel > details > summary.panel-summary {
  list-style: none;
  cursor: pointer;
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.15rem;
  margin: 0;
  user-select: none;
}
.panel > details > summary.panel-summary::-webkit-details-marker { display: none; }
.panel > details > summary.panel-summary::before {
  content: "▸ ";
  color: var(--muted);
  font-weight: 500;
}
.panel > details[open] > summary.panel-summary::before { content: "▾ "; }
.panel > details > summary.panel-summary + * { margin-top: 0.85rem; }
.panel > details[open] > .panel-body { margin-top: 0.85rem; }
.form-grid {
  display: grid;
  gap: 0.85rem;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}
/* Automações: duas colunas legíveis em vez de cinco colunas estreitas.
   Os textos de ajuda permanecem logo abaixo do respectivo campo e o botão
   ocupa apenas sua altura normal, sem esticar junto do texto mais longo. */
.automation-rule-form {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  align-items: start;
}
.automation-rule-form > label {
  align-self: start;
  min-width: 0;
}
.automation-rule-actions {
  grid-column: 1 / -1;
  align-self: start;
  align-items: flex-start;
  margin-top: 0;
}
.automation-rule-actions .btn {
  flex: 0 0 auto;
}
label {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  font-size: 0.85rem;
  color: var(--muted);
}
input, select, textarea {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  padding: 0.55rem 0.7rem;
  font: inherit;
}
textarea { min-height: 100px; resize: vertical; }
/* O ícone nativo do seletor de data/hora (calendário/relógio) vem PRETO por
   padrão no Chrome/Edge — invisível no tema escuro do painel. "color-scheme"
   deixa o MINI-POPUP do navegador (o calendário que abre) em modo escuro.
   Já tentamos deixar o ÍCONE em si visível só com filter:invert no
   ::-webkit-calendar-picker-indicator, mas isso se mostrou inconsistente
   entre versões de Chrome/Edge — em alguns o desenho do calendário some e
   sobra só um quadrado sem contorno nenhum (o problema relatado). Pra
   garantir um ícone branco e visível em QUALQUER navegador, a solução
   definitiva é: escondemos o ícone nativo (continua ali, clicável, só
   fica invisível com opacity:0) e desenhamos o NOSSO próprio ícone por
   cima via CSS mask (sempre branco, não depende do SVG do navegador).
   Ver .dt-input-wrap abaixo — precisa de um <span class="dt-input-wrap">
   ao redor do <input>, feito nos templates que usam esses tipos de campo. */
input[type="datetime-local"],
input[type="date"],
input[type="time"] {
  color-scheme: dark;
}
.dt-input-wrap {
  position: relative;
  display: block;
}
.dt-input-wrap input {
  width: 100%;
  padding-right: 2.3rem;
  box-sizing: border-box;
}
.dt-input-wrap input::-webkit-calendar-picker-indicator {
  opacity: 0;
  position: absolute;
  right: 0;
  top: 0;
  width: 2.3rem;
  height: 100%;
  margin: 0;
  cursor: pointer;
}
.dt-input-wrap::after {
  content: "";
  position: absolute;
  right: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  width: 16px;
  height: 16px;
  background-color: #fff;
  pointer-events: none;
  -webkit-mask-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V10h14v10zM5 8V6h14v2H5z"/></svg>');
  mask-image: url('data:image/svg+xml;utf8,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M19 4h-1V2h-2v2H8V2H6v2H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V6c0-1.1-.9-2-2-2zm0 16H5V10h14v10zM5 8V6h14v2H5z"/></svg>');
  -webkit-mask-size: contain;
  mask-size: contain;
  -webkit-mask-repeat: no-repeat;
  mask-repeat: no-repeat;
}
.actions { display: flex; gap: 0.6rem; flex-wrap: wrap; margin-top: 1rem; }
.mono { font-family: ui-monospace, Consolas, monospace; font-size: 0.85rem; }
.muted { color: var(--muted); }
code {
  font-family: ui-monospace, Consolas, monospace;
  font-size: 0.85em;
  background: var(--bg-soft);
  padding: 0.1rem 0.35rem;
  border-radius: 4px;
}
.field-hint {
  font-size: 0.8rem;
  color: var(--muted);
  font-weight: normal;
  line-height: 1.5;
  margin: 0.1rem 0 0.35rem;
}
.qr-box {
  background: #fff;
  padding: 0.75rem;
  border-radius: 12px;
  display: inline-block;
}
.qr-box img { display: block; width: 220px; height: 220px; }
.login-wrap {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 2rem;
}
.login-card {
  width: min(420px, 100%);
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 2rem;
  text-align: center;
}
.login-card .brand { font-size: 2.4rem; padding: 0 0 0.5rem; }
.tabs { display: flex; gap: 0.5rem; margin-bottom: 1rem; flex-wrap: wrap; }
.tabs a {
  padding: 0.4rem 0.8rem;
  border-radius: 999px;
  background: var(--bg-soft);
  color: var(--muted);
  text-decoration: none;
  font-size: 0.85rem;
}
.tabs a:hover { color: var(--text); }

.alert-banner {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  border-radius: var(--radius);
  padding: 0.85rem 1.1rem;
  margin-bottom: 1rem;
  border: 1px solid var(--warn);
  background: rgba(230, 192, 123, 0.12);
  color: var(--text);
}
.alert-banner.danger { border-color: var(--danger); background: rgba(240, 113, 120, 0.12); }
.alert-banner .alert-icon { font-size: 1.1rem; }
.alert-banner span:nth-child(2) { flex: 1; }
.alert-banner .btn { margin-left: auto; }

.copy-field { display: flex; align-items: center; gap: 0.5rem; }
.copy-field .btn { padding: 0.4rem 0.75rem; font-size: 0.8rem; }
.copy-field .copied { color: var(--accent); font-size: 0.8rem; }

.help-panel {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  font-size: 0.9rem;
  line-height: 1.55;
  /* "Fica" no canto direito ao rolar a tela — não precisa procurar a ajuda
     depois de rolar a página pra baixo. */
  position: sticky;
  top: 1.25rem;
  transition: width 0.18s ease, padding 0.18s ease, min-width 0.18s ease;
}

/* Botão que abre/fecha o painel — injetado via JS (hzInitHelpPanels, em
   base.html) em QUALQUER .help-panel da tela, sem precisar repetir isso
   em cada template. */
.help-toggle-btn {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  border: none;
  background: transparent;
  color: var(--accent);
  font-weight: 600;
  font-size: 0.85rem;
  cursor: pointer;
  padding: 0;
  font-family: inherit;
}

/* Estado ENCOLHIDO (padrão): só uma aba estreita e vertical no canto
   direito — o resto do conteúdo de ajuda fica escondido até abrir. */
.help-panel[data-collapsed="true"] {
  width: 40px;
  min-width: 40px;
  padding: 0.85rem 0.35rem;
  overflow: hidden;
  cursor: pointer;
}
.help-panel[data-collapsed="true"] > *:not(.help-toggle-btn) { display: none; }
.help-panel[data-collapsed="true"] .help-toggle-btn {
  writing-mode: vertical-rl;
  width: 100%;
  min-height: 140px;
  justify-content: center;
}

/* Estado ABERTO: o botão vira uma linha "Fechar ajuda" no topo, e o
   conteúdo normal (h3/details) aparece embaixo, normalmente. */
.help-panel[data-collapsed="false"] .help-toggle-btn {
  width: 100%;
  justify-content: flex-end;
  color: var(--muted);
  font-size: 0.8rem;
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.75rem;
  margin-bottom: 0.85rem;
}
.help-panel[data-collapsed="false"] .help-toggle-btn:hover { color: var(--accent); }

@media (max-width: 980px) {
  /* Em telas estreitas o grid vira 1 coluna — a aba vertical de 40px não
     faz sentido sobrando sozinha numa linha, então ela vira uma faixa
     horizontal curta em vez de vertical. */
  .help-panel[data-collapsed="true"] {
    width: 100%;
    min-width: 0;
    padding: 0.6rem 0.85rem;
  }
  .help-panel[data-collapsed="true"] .help-toggle-btn {
    writing-mode: horizontal-tb;
    min-height: 0;
  }
  .help-panel { position: static; }
}

.help-panel h2,
.help-panel h3 { font-size: 1rem; margin: 0 0 0.85rem; }
.help-panel ul { margin: 0.5rem 0; padding-left: 1.1rem; }
.help-panel li { margin-bottom: 0.5rem; }
.help-panel details {
  border-bottom: 1px solid var(--border);
  padding-bottom: 0.75rem;
  margin-bottom: 0.75rem;
}
.help-panel details:last-child { border-bottom: none; padding-bottom: 0; margin-bottom: 0; }
.help-panel summary {
  cursor: pointer;
  font-weight: 600;
  font-size: 1rem;
  list-style: none;
}
.help-panel summary::-webkit-details-marker { display: none; }
.help-panel summary::before { content: "▸ "; color: var(--accent); }
.help-panel details[open] summary::before { content: "▾ "; }
.help-panel details > *:not(summary) { margin-top: 0.65rem; }
.help-panel .cite {
  margin-top: 0.85rem;
  padding: 0.75rem 0.9rem;
  border-left: 3px solid var(--accent);
  background: var(--bg-soft);
  border-radius: 6px;
  color: var(--muted);
  font-size: 0.85rem;
}

.hz-modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(6, 10, 14, 0.65);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
  padding: 1rem;
  animation: hz-fade-in 0.15s ease-out;
}
.hz-modal-overlay[hidden] { display: none; }
.hz-modal {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 1.5rem;
  width: min(440px, 100%);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
  animation: hz-pop-in 0.16s ease-out;
}
.hz-modal-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.1rem;
  margin: 0 0 0.65rem;
}
.hz-modal-msg { margin: 0 0 1.4rem; line-height: 1.55; color: var(--text); font-size: 0.92rem; }
.hz-modal-actions { display: flex; justify-content: flex-end; gap: 0.6rem; }
.hz-modal-wide { width: min(1200px, 96vw); max-height: 88vh; overflow-y: auto; }
.hz-modal-wide .table { font-size: 0.88rem; }
/* Evita nome/telefone/grupo quebrando em 2 linhas nas tabelas de relatório
   dentro do modal largo — como o modal agora é bem mais largo, cabe numa
   linha só na maioria dos casos; se ainda faltar espaço, rola horizontal
   (.hz-report) em vez de quebrar o texto. */
.hz-modal-wide .table th,
.hz-modal-wide .table td { white-space: nowrap; }
.hz-report { overflow-x: auto; }
@keyframes hz-fade-in {
  from { opacity: 0; }
  to { opacity: 1; }
}
@keyframes hz-pop-in {
  from { opacity: 0; transform: scale(0.94) translateY(6px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.hz-emoji-panel {
  position: fixed;
  z-index: 2000;
  width: 300px;
  max-height: 320px;
  overflow-y: auto;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.5);
  padding: 0.6rem;
  animation: hz-pop-in 0.12s ease-out;
}
.hz-emoji-panel[hidden] { display: none; }
.hz-emoji-cat-title {
  margin: 0.5rem 0 0.25rem;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.hz-emoji-cat-title:first-child { margin-top: 0; }
.hz-emoji-grid {
  display: grid;
  grid-template-columns: repeat(8, 1fr);
  gap: 2px;
}
.hz-emoji-btn {
  background: none;
  border: none;
  border-radius: 6px;
  font-size: 1.15rem;
  line-height: 1.6;
  cursor: pointer;
  padding: 2px 0;
}
.hz-emoji-btn:hover { background: rgba(255, 255, 255, 0.08); }
.hz-emoji-trigger { line-height: 1; }

.detail-grid {
  display: grid;
  grid-template-columns: 1fr 340px;
  gap: 1.25rem;
  align-items: start;
  transition: grid-template-columns 0.18s ease;
}
/* Ajuda encolhida -> devolve o espaço pro conteúdo principal (a "tela se
   ajusta" conforme pedido). Ajuda aberta -> volta pra coluna de 340px. */
.detail-grid:has(.help-panel[data-collapsed="true"]) {
  grid-template-columns: 1fr 40px;
}

/* Compras não identificadas — cards em vez de tabela com form na última coluna */
.compra-card {
  padding: 1rem 1.15rem;
}
.compra-card-head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.85rem;
}
.compra-card-head-right {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  flex-shrink: 0;
}
.compra-filtros {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.75rem 1rem;
  margin: 0;
}
.compra-filtros label {
  margin: 0;
  min-width: 220px;
  flex: 1;
}
.compra-card-meta {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.4rem;
}
.compra-card-valor {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.1rem;
  white-space: nowrap;
}
.compra-card-dados {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.65rem 1rem;
  margin: 0 0 1rem;
  padding: 0.75rem 0;
  border-top: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
}
.compra-card-dados dt {
  margin: 0;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.compra-card-dados dd {
  margin: 0.2rem 0 0;
  word-break: break-word;
}
.compra-card-form {
  display: grid;
  grid-template-columns: 1.4fr 1fr auto;
  gap: 0.75rem 0.85rem;
  align-items: end;
}
.compra-card-form label {
  margin: 0;
  font-size: 0.85rem;
}
.compra-card-actions {
  display: flex;
  align-items: flex-end;
  padding-bottom: 0.1rem;
}

/* Combobox com busca (Compras não identificadas → Lead) */
.hz-search-select {
  position: relative;
  width: 100%;
}
.hz-search-select-native {
  position: absolute;
  opacity: 0;
  pointer-events: none;
  width: 1px;
  height: 1px;
  margin: 0;
  padding: 0;
  border: 0;
}
.hz-search-select-input {
  width: 100%;
  margin: 0;
}
.hz-search-select-list {
  position: absolute;
  z-index: 40;
  left: 0;
  right: 0;
  top: calc(100% + 4px);
  max-height: 240px;
  overflow: auto;
  margin: 0;
  padding: 0.35rem 0;
  list-style: none;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.35);
}
.hz-search-select-list li {
  padding: 0.45rem 0.75rem;
  cursor: pointer;
  font-size: 0.9rem;
}
.hz-search-select-list li:hover,
.hz-search-select-list li.is-active {
  background: rgba(255, 255, 255, 0.06);
}
.hz-search-select-empty {
  color: var(--muted);
  cursor: default !important;
}

@media (max-width: 900px) {
  .compra-card-dados { grid-template-columns: 1fr; }
  .compra-card-form { grid-template-columns: 1fr; }
}

@media (max-width: 980px) {
  .detail-grid,
  .detail-grid:has(.help-panel[data-collapsed="true"]) { grid-template-columns: 1fr; }
  .automation-rule-form { grid-template-columns: 1fr; }
}

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: relative; height: auto; border-right: 0; border-bottom: 1px solid var(--border); }
  .main { padding: 1.25rem; }
}
