:root {
  --bg: #eef1f0;
  --panel: #ffffff;
  --panel-2: #f6f8f7;
  --line: #e3e8e6;
  --line-2: #d3dbd8;
  --text: #17201d;
  --text-2: #55635e;
  --text-3: #8a9893;
  --brand: #0f6e5c;
  --brand-2: #0b5a4b;
  --brand-soft: #e3f2ee;
  --rail: #0e2721;
  --rail-2: #173a32;
  --out: #d9f6e6;
  --out-line: #c3eccf;
  --in: #ffffff;
  --chat-bg: #efeae2;
  --warn: #b26a00;
  --warn-soft: #fff3df;
  --danger: #c0362c;
  --danger-soft: #fdecea;
  --info: #2563c9;
  --info-soft: #e8f0fd;
  --read: #2f8fe0;
  --shadow: 0 1px 2px rgba(16, 32, 28, .06), 0 8px 24px rgba(16, 32, 28, .08);
  --radius: 12px;
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font: 14px/1.45 var(--font);
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--brand); }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
svg { display: block; }

/* ---------- Botões e campos ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 34px; padding: 0 14px; border-radius: 8px;
  border: 1px solid var(--line-2); background: var(--panel);
  font-weight: 550; font-size: 13px; white-space: nowrap;
  transition: background .15s, border-color .15s, box-shadow .15s;
}
.btn:hover { background: var(--panel-2); }
.btn:focus-visible, .icon-btn:focus-visible, .tab:focus-visible, .conv:focus-visible { outline: 2px solid var(--brand); outline-offset: 2px; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { background: var(--brand-2); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); border-color: #f2c4bf; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--panel-2); }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.btn svg { width: 16px; height: 16px; }

.icon-btn {
  width: 36px; height: 36px; display: grid; place-items: center;
  border-radius: 8px; border: 0; background: transparent; color: var(--text-2);
}
.icon-btn:hover { background: var(--panel-2); color: var(--text); }
.icon-btn svg { width: 20px; height: 20px; }

.field { display: grid; gap: 6px; margin-bottom: 14px; }
.field label { font-size: 12px; font-weight: 600; color: var(--text-2); }
.input {
  width: 100%; height: 40px; padding: 0 12px;
  border: 1px solid var(--line-2); border-radius: 8px; background: #fff;
  transition: border-color .15s, box-shadow .15s;
}
textarea.input { height: auto; padding: 10px 12px; resize: vertical; }
.input:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(15, 110, 92, .15); }

/* ---------- Login ---------- */
.auth {
  min-height: 100%; display: grid; place-items: center; padding: 24px;
  background:
    radial-gradient(1200px 600px at 10% -10%, rgba(15,110,92,.18), transparent 60%),
    radial-gradient(900px 500px at 110% 110%, rgba(15,110,92,.14), transparent 60%),
    var(--bg);
}
.auth-card {
  width: 100%; max-width: 380px; background: var(--panel);
  border-radius: 16px; padding: 32px; box-shadow: var(--shadow);
}
.auth-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 24px; }
.logo {
  width: 40px; height: 40px; border-radius: 10px; background: var(--brand);
  display: grid; place-items: center; color: #fff; font-weight: 800; font-size: 13px; letter-spacing: .5px;
}
.auth h1 { font-size: 20px; margin: 0; }
.auth p.sub { margin: 2px 0 0; color: var(--text-2); font-size: 13px; }
.auth .btn.primary { width: 100%; height: 42px; justify-content: center; margin-top: 6px; }
.auth .erro { color: var(--danger); font-size: 13px; min-height: 18px; margin: 4px 0 0; }

/* ---------- Layout principal ---------- */
.shell {
  height: 100vh; height: 100dvh;
  display: grid; grid-template-columns: 64px 360px minmax(0, 1fr) auto;
}
.rail {
  background: var(--rail); color: #cfe3dc;
  display: flex; flex-direction: column; align-items: center; gap: 6px; padding: 14px 0;
}
.rail .logo { margin-bottom: 14px; background: #1a8a73; }
.rail-btn {
  width: 44px; height: 44px; border-radius: 12px; border: 0; background: transparent; color: #a9c7bd;
  display: grid; place-items: center; position: relative;
}
.rail-btn:hover { background: var(--rail-2); color: #fff; }
.rail-btn.ativo { background: #1d4a40; color: #fff; }
.rail-btn svg { width: 22px; height: 22px; }
.rail-btn .dot {
  position: absolute; top: 6px; right: 6px; min-width: 18px; height: 18px; padding: 0 5px;
  border-radius: 9px; background: #25d366; color: #06301f; font-size: 11px; font-weight: 700; line-height: 18px;
}
.rail .spacer { flex: 1; }
.me {
  width: 38px; height: 38px; border-radius: 50%; border: 2px solid #2a5a4f;
  display: grid; place-items: center; font-weight: 700; font-size: 13px; background: #2b6a5c; color: #fff;
}

/* ---------- Lista de conversas ---------- */
.list { background: var(--panel); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.list-head { padding: 18px 16px 10px; }
.list-head h2 { margin: 0 0 12px; font-size: 20px; letter-spacing: -.2px; display: flex; align-items: center; justify-content: space-between; }
.search { position: relative; }
.search svg { position: absolute; left: 11px; top: 11px; width: 18px; height: 18px; color: var(--text-3); }
.search .input { padding-left: 36px; background: var(--panel-2); border-color: transparent; }
.search .input:focus { background: #fff; }

.tabs { display: flex; flex-wrap: wrap; gap: 6px; padding: 4px 16px 12px; border-bottom: 1px solid var(--line); }
.tabs::-webkit-scrollbar { display: none; }
.tab {
  flex: none; border: 1px solid var(--line-2); background: #fff; border-radius: 999px;
  height: 30px; padding: 0 12px; font-size: 12.5px; font-weight: 600; color: var(--text-2);
  display: inline-flex; align-items: center; gap: 6px;
}
.tab:hover { border-color: var(--brand); color: var(--brand); }
.tab.ativo { background: var(--brand); border-color: var(--brand); color: #fff; }
.tab .n { font-size: 11px; background: rgba(0,0,0,.08); border-radius: 999px; padding: 0 6px; line-height: 17px; }
.tab.ativo .n { background: rgba(255,255,255,.22); }

.conv-list { flex: 1; overflow-y: auto; min-height: 0; }
.conv {
  display: grid; grid-template-columns: 48px minmax(0, 1fr); gap: 12px; align-items: center;
  width: 100%; padding: 12px 16px; border: 0; background: transparent; text-align: left;
  border-bottom: 1px solid var(--line); position: relative;
}
.conv:hover { background: var(--panel-2); }
.conv.ativo { background: var(--brand-soft); }
.conv.ativo::before { content: ""; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: var(--brand); }
.conv .linha { display: flex; align-items: center; gap: 8px; min-width: 0; }
.conv .nome { font-weight: 650; font-size: 14.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.conv .hora { font-size: 11.5px; color: var(--text-3); flex: none; }
.conv.nova .hora { color: #129c55; font-weight: 700; }
.conv .preview { color: var(--text-2); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; display: flex; align-items: center; gap: 4px; }
.conv .preview .ticks { flex: none; }
.conv.nova .preview { color: var(--text); font-weight: 550; }
.conv .badge { flex: none; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px; background: #25d366; color: #fff; font-size: 11.5px; font-weight: 700; display: grid; place-items: center; }
.conv .meta { display: flex; gap: 6px; margin-top: 4px; flex-wrap: wrap; }

.chip {
  display: inline-flex; align-items: center; gap: 4px; height: 20px; padding: 0 8px; border-radius: 999px;
  font-size: 11px; font-weight: 650; background: var(--panel-2); color: var(--text-2); white-space: nowrap;
}
.chip svg { width: 12px; height: 12px; }
.chip.ad { background: var(--info-soft); color: var(--info); }
.chip.warn { background: var(--warn-soft); color: var(--warn); }
.chip.danger { background: var(--danger-soft); color: var(--danger); }
.chip.ok { background: var(--brand-soft); color: var(--brand); }

.avatar {
  width: 48px; height: 48px; border-radius: 50%; display: grid; place-items: center;
  font-weight: 700; font-size: 16px; color: #fff; flex: none; letter-spacing: .3px;
}
.avatar.sm { width: 40px; height: 40px; font-size: 14px; }
.avatar.lg { width: 84px; height: 84px; font-size: 28px; }

.vazio-lista { padding: 48px 24px; text-align: center; color: var(--text-3); }
.vazio-lista svg { width: 40px; height: 40px; margin: 0 auto 10px; color: var(--line-2); }

/* ---------- Conversa ---------- */
.chat { display: flex; flex-direction: column; min-width: 0; min-height: 0; background: var(--chat-bg); position: relative; }
.chat-head {
  height: 64px; flex: none; display: flex; align-items: center; gap: 12px; padding: 0 16px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.chat-head .info { min-width: 0; flex: 1; }
.chat-head .titulo { font-weight: 700; font-size: 15px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chat-head .sub { font-size: 12.5px; color: var(--text-2); display: flex; gap: 8px; align-items: center; overflow: hidden; white-space: nowrap; }
.chat-head .acoes { display: flex; gap: 6px; align-items: center; }
.voltar { display: none; }

.status-pill { display: inline-flex; align-items: center; gap: 5px; font-weight: 650; font-size: 11.5px; padding: 2px 8px; border-radius: 999px; }
.status-pill::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.st-aguardando { background: var(--warn-soft); color: var(--warn); }
.st-humano { background: var(--brand-soft); color: var(--brand); }
.st-encerrada { background: var(--panel-2); color: var(--text-3); }

.msgs {
  flex: 1; overflow-y: auto; padding: 18px 6% 10px; min-height: 0;
  background-color: var(--chat-bg);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='80' height='80' viewBox='0 0 80 80'%3E%3Cg fill='%23d9d2c5' fill-opacity='.35'%3E%3Ccircle cx='10' cy='10' r='1.6'/%3E%3Ccircle cx='50' cy='30' r='1.2'/%3E%3Ccircle cx='30' cy='60' r='1.4'/%3E%3Ccircle cx='70' cy='70' r='1'/%3E%3C/g%3E%3C/svg%3E");
}
.dia { text-align: center; margin: 14px 0 10px; }
.dia span { background: #fff; color: var(--text-2); font-size: 11.5px; font-weight: 600; padding: 4px 12px; border-radius: 8px; box-shadow: 0 1px 1px rgba(0,0,0,.06); }

.msg { display: flex; margin: 2px 0; }
.msg.saida { justify-content: flex-end; }
.msg + .msg.mudou { margin-top: 10px; }
.bolha {
  max-width: min(560px, 75%); padding: 6px 9px 6px; border-radius: 10px; position: relative;
  box-shadow: 0 1px 1px rgba(0,0,0,.07); word-wrap: break-word; overflow-wrap: anywhere;
}
.entrada .bolha { background: var(--in); border-top-left-radius: 3px; }
.saida .bolha { background: var(--out); border-top-right-radius: 3px; }
.bolha .autor { font-size: 11.5px; font-weight: 700; color: var(--brand); margin-bottom: 2px; }
.bolha .texto { white-space: pre-wrap; font-size: 14px; }
.bolha .rodape { float: right; margin: 6px 0 -3px 12px; font-size: 11px; color: var(--text-3); display: inline-flex; align-items: center; gap: 3px; }
.bolha .tipo-tag { font-size: 11px; font-weight: 650; color: var(--text-2); display: flex; align-items: center; gap: 4px; margin-bottom: 2px; }
.bolha.template { border: 1px dashed #9dd7b6; }
.bolha.falhou { background: var(--danger-soft); }
.bolha .erro-envio { color: var(--danger); font-size: 12px; margin-top: 4px; display: flex; gap: 4px; }
.bolha img.midia { display: block; max-width: 320px; width: 100%; border-radius: 6px; margin-bottom: 4px; cursor: zoom-in; background: #ddd; }
.bolha video.midia { display: block; max-width: 320px; width: 100%; border-radius: 6px; margin-bottom: 4px; }
.bolha audio { display: block; width: 280px; max-width: 100%; margin: 2px 0 4px; }
.doc { display: flex; align-items: center; gap: 10px; padding: 10px; border-radius: 8px; background: rgba(0,0,0,.04); text-decoration: none; color: var(--text); margin-bottom: 4px; }
.doc svg { width: 28px; height: 28px; color: var(--danger); flex: none; }
.doc span { font-weight: 600; font-size: 13px; overflow: hidden; text-overflow: ellipsis; }
.midia-indisp { font-size: 12.5px; color: var(--text-3); font-style: italic; margin-bottom: 4px; }

.ticks { display: inline-flex; color: var(--text-3); }
.ticks svg { width: 16px; height: 11px; }
.ticks.lida { color: var(--read); }
.ticks.falhou { color: var(--danger); }

.novas-abaixo {
  position: absolute; right: 24px; bottom: 96px; border: 0; border-radius: 999px;
  background: #fff; box-shadow: var(--shadow); height: 36px; padding: 0 14px; font-weight: 600; font-size: 12.5px; color: var(--brand);
  display: none;
}
.novas-abaixo.on { display: inline-flex; align-items: center; gap: 6px; }

/* ---------- Composer ---------- */
.composer { flex: none; background: var(--panel-2); border-top: 1px solid var(--line); padding: 10px 14px 12px; }
.janela { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--text-2); margin: 0 4px 8px; }
.janela .barra { flex: 1; height: 4px; border-radius: 4px; background: var(--line); overflow: hidden; max-width: 160px; }
.janela .barra i { display: block; height: 100%; background: var(--brand); border-radius: 4px; }
.janela.pouca .barra i { background: var(--warn); }
.janela.pouca { color: var(--warn); }
.compor { display: flex; align-items: flex-end; gap: 8px; }
.compor textarea {
  flex: 1; resize: none; max-height: 160px; min-height: 44px; padding: 11px 14px; border-radius: 22px;
  border: 1px solid var(--line-2); background: #fff; line-height: 1.4;
}
.compor textarea:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px rgba(15,110,92,.12); }
.enviar {
  width: 44px; height: 44px; border-radius: 50%; border: 0; background: var(--brand); color: #fff;
  display: grid; place-items: center; flex: none; transition: transform .1s, background .15s;
}
.enviar:hover { background: var(--brand-2); }
.enviar:active { transform: scale(.94); }
.enviar[disabled] { background: #9fbdb5; cursor: not-allowed; }
.enviar svg { width: 20px; height: 20px; }
.compor .icon-btn { height: 44px; width: 44px; border-radius: 50%; }

.fechada {
  display: flex; align-items: center; gap: 14px; padding: 12px 14px; border-radius: 10px;
  background: var(--warn-soft); border: 1px solid #f1d6a8; color: #6b4200;
}
.fechada svg { width: 22px; height: 22px; flex: none; color: var(--warn); }
.fechada .t { flex: 1; font-size: 13px; }
.fechada .t b { display: block; font-size: 13.5px; }

.chat-vazio { flex: 1; display: grid; place-items: center; text-align: center; color: var(--text-2); background: var(--panel-2); }
.chat-vazio .ilustra { width: 120px; height: 120px; border-radius: 50%; background: var(--brand-soft); display: grid; place-items: center; margin: 0 auto 18px; color: var(--brand); }
.chat-vazio .ilustra svg { width: 52px; height: 52px; }
.chat-vazio h3 { margin: 0 0 6px; color: var(--text); font-size: 18px; }
.chat-vazio p { margin: 0; max-width: 320px; }

/* ---------- Painel do contato ---------- */
.side { width: 320px; background: var(--panel); border-left: 1px solid var(--line); overflow-y: auto; min-height: 0; }
.side.fechado { display: none; }
.side-head { padding: 24px 20px 18px; text-align: center; border-bottom: 1px solid var(--line); position: relative; }
.side-fechar { position: absolute; top: 10px; right: 10px; }
.side-head .avatar { margin: 0 auto 12px; }
.side-head .nome-ed { font-size: 17px; font-weight: 700; border: 1px solid transparent; border-radius: 8px; padding: 4px 8px; text-align: center; width: 100%; background: transparent; }
.side-head .nome-ed:hover { border-color: var(--line-2); }
.side-head .nome-ed:focus { outline: none; border-color: var(--brand); background: #fff; }
.side-head .tel { color: var(--text-2); font-size: 13.5px; display: inline-flex; align-items: center; gap: 6px; margin-top: 4px; }
.side-sec { padding: 16px 20px; border-bottom: 1px solid var(--line); }
.side-sec h4 { margin: 0 0 10px; font-size: 11.5px; letter-spacing: .6px; text-transform: uppercase; color: var(--text-3); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 8px 12px; font-size: 13px; }
.kv dt { color: var(--text-3); }
.kv dd { margin: 0; text-align: right; font-weight: 550; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.anuncio-card { background: var(--info-soft); border-radius: 10px; padding: 10px 12px; font-size: 13px; color: #1c3f7a; }
.anuncio-card b { display: block; margin-bottom: 2px; }
.notas { width: 100%; min-height: 110px; }
.salvo { font-size: 11.5px; color: var(--text-3); height: 16px; margin-top: 4px; }
.crm-btn { width: 100%; justify-content: center; }

/* ---------- Modal ---------- */
.modal-fundo { position: fixed; inset: 0; background: rgba(10, 25, 20, .45); display: grid; place-items: center; padding: 20px; z-index: 50; animation: fade .12s; }
.modal { width: 100%; max-width: 640px; max-height: 88vh; background: var(--panel); border-radius: 16px; box-shadow: 0 20px 60px rgba(0,0,0,.25); display: flex; flex-direction: column; overflow: hidden; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 18px 20px; overflow-y: auto; }
.modal-foot { padding: 14px 20px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 8px; background: var(--panel-2); }
@keyframes fade { from { opacity: 0; } }

.tpl-grid { display: grid; grid-template-columns: 220px 1fr; gap: 16px; min-height: 320px; }
.tpl-lista { border-right: 1px solid var(--line); padding-right: 12px; overflow-y: auto; max-height: 55vh; }
.tpl-item { width: 100%; text-align: left; border: 0; background: transparent; padding: 9px 10px; border-radius: 8px; display: block; }
.tpl-item:hover { background: var(--panel-2); }
.tpl-item.ativo { background: var(--brand-soft); }
.tpl-item b { display: block; font-size: 13px; overflow: hidden; text-overflow: ellipsis; }
.tpl-item small { color: var(--text-3); font-size: 11.5px; }
.preview-tpl { background: var(--chat-bg); border-radius: 10px; padding: 14px; margin-top: 6px; }
.preview-tpl .bolha { max-width: 100%; }

.tabela { width: 100%; border-collapse: collapse; font-size: 13px; }
.tabela th, .tabela td { text-align: left; padding: 9px 8px; border-bottom: 1px solid var(--line); }
.tabela th { color: var(--text-3); font-weight: 600; font-size: 11.5px; text-transform: uppercase; letter-spacing: .4px; }

/* ---------- Toasts e imagem ampliada ---------- */
#toasts { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 80; }
.toast { background: #17201d; color: #fff; padding: 10px 16px; border-radius: 10px; font-size: 13px; box-shadow: var(--shadow); animation: sobe .18s ease-out; max-width: 90vw; }
.toast.erro { background: var(--danger); }
@keyframes sobe { from { transform: translateY(8px); opacity: 0; } }
.zoom { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 90; cursor: zoom-out; }
.zoom img { max-width: 92vw; max-height: 92vh; border-radius: 6px; }

.carregando { display: grid; place-items: center; height: 100vh; color: var(--text-3); }
.spin { width: 28px; height: 28px; border: 3px solid var(--line); border-top-color: var(--brand); border-radius: 50%; animation: gira .8s linear infinite; }
@keyframes gira { to { transform: rotate(360deg); } }

/* ---------- Responsivo ---------- */
@media (max-width: 1280px) {
  .side { position: absolute; right: 0; top: 0; bottom: 0; z-index: 20; box-shadow: -10px 0 30px rgba(0,0,0,.08); }
  .shell { grid-template-columns: 64px 340px minmax(0, 1fr); position: relative; }
}
@media (max-width: 860px) {
  .shell { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  .rail { flex-direction: row; justify-content: space-around; padding: 6px 10px; order: 2; }
  .rail .logo, .rail .spacer { display: none; }
  .list, .chat { grid-row: 1; grid-column: 1; }
  .shell.com-chat .list { display: none; }
  .shell:not(.com-chat) .chat { display: none; }
  .voltar { display: grid; }
  .chat-head .acoes .btn span { display: none; }
  .msgs { padding: 14px 10px; }
  .bolha { max-width: 86%; }
  .side { width: 100%; }
  .tpl-grid { grid-template-columns: 1fr; }
  .tpl-lista { border-right: 0; max-height: 30vh; }
}
@media (max-width: 860px) { .chat-head .sub .tel-h { display: none; } }
