:root {
  --bg: #eef1f4;
  --surface: #ffffff;
  --surface-2: #f6f8fa;
  --ink: #16202b;
  --muted: #5c6b7a;
  --line: #dde3e9;
  --primary: #0b5fa5;
  --primary-ink: #ffffff;
  --danger: #b3261e;

  --s-start: #6b7a8a;
  --s-awaiting_id: #8a6d1f;
  --s-registered: #b7791f;
  --s-ftd: #1e7f4f;
  --s-vip: #5b3fbf;
  --s-other: #6b7a8a;
  --s-unknown: #9aa6b2;

  --role-user: #ffffff;
  --role-user-edge: #9aa6b2;
  --role-bot: #e8f0f8;
  --role-bot-edge: #0b5fa5;
  --role-agent: #fff3dc;
  --role-agent-edge: #c77d12;

  --radius: 8px;
  --font: "IBM Plex Sans", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.45;
  color: var(--ink);
  background: var(--bg);
  font-variant-numeric: tabular-nums;
}
[hidden] { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.92em; }
.muted { color: var(--muted); }
:focus-visible { outline: 2px solid var(--primary); outline-offset: 2px; }

/* Botões */
.btn {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: var(--radius);
  padding: 7px 14px;
  cursor: pointer;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-ink); }
.btn.primary:hover { background: #094d87; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { background: rgba(0,0,0,.05); color: var(--ink); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* Login */
.login {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}
.login-card {
  width: min(360px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 28px;
  display: grid;
  gap: 14px;
}
.login-card h1 { margin: 0; font-size: 22px; font-weight: 600; }
.login-sub { margin: -8px 0 4px; color: var(--muted); }
.login-card label { display: grid; gap: 6px; font-weight: 500; }
.login-card input {
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 9px 11px;
  background: var(--surface);
}
.form-error { margin: 0; min-height: 1.3em; color: var(--danger); }

/* Estrutura */
.app { height: 100%; display: grid; grid-template-rows: 52px 1fr; }
.topbar {
  display: flex; align-items: center; gap: 24px;
  padding: 0 18px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 600; font-size: 15px; }
.tabs { display: flex; gap: 4px; }
.tab {
  border: 0; background: transparent; padding: 8px 12px;
  border-radius: var(--radius); color: var(--muted); cursor: pointer;
}
.tab.active { color: var(--ink); background: var(--surface-2); font-weight: 500; }
.topbar-right { margin-left: auto; display: flex; align-items: center; gap: 14px; color: var(--muted); }
.clock { font-variant-numeric: tabular-nums; }

.view { min-height: 0; }
.chats {
  display: grid;
  grid-template-columns: 220px 360px 1fr;
  height: 100%;
}

/* Rail de bots */
.rail {
  background: var(--surface);
  border-right: 1px solid var(--line);
  overflow-y: auto;
  padding: 10px 8px;
}
.rail-group { margin-top: 14px; }
.rail-group-title { display: flex; align-items: center; gap: 8px; padding: 4px 10px 6px; color: var(--muted); font-size: 12px; font-weight: 500; }
.rail-group-title .flag { font-size: 14px; }
.bot {
  width: 100%; display: grid; grid-template-columns: 30px 1fr auto; gap: 10px; align-items: center;
  border: 0; background: transparent; padding: 7px 8px; border-radius: 10px; cursor: pointer; text-align: left;
}
.bot:hover { background: var(--surface-2); }
.bot.active { background: #e3edf7; }
.bot.active .bot-name { color: var(--primary); font-weight: 600; }
.avatar {
  width: 30px; height: 30px; border-radius: 9px; display: grid; place-items: center;
  font-size: 11px; font-weight: 600; color: #fff; background: var(--s-start); letter-spacing: .02em;
}
.avatar.en { background: #0b5fa5; }
.avatar.es { background: #b7791f; }
.bot.all .avatar { background: var(--ink); font-size: 14px; }
.bot-text { min-width: 0; display: grid; }
.bot-name { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bot-sub { color: var(--muted); font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.bot-count { color: var(--muted); font-size: 12px; text-align: right; display: grid; line-height: 1.2; }
.bot-count b { font-weight: 600; color: var(--ink); }
.bot-count .today { color: var(--s-ftd); font-size: 11px; }
.bot .lock { color: var(--muted); font-size: 10px; margin-left: 4px; }

/* Lista de conversas */
.list { display: grid; grid-template-rows: auto 1fr auto; min-height: 0; border-right: 1px solid var(--line); background: var(--surface-2); }
.list-tools { padding: 12px 12px 8px; display: grid; gap: 8px; background: var(--surface); border-bottom: 1px solid var(--line); }
.list-tools input[type=search] {
  width: 100%; border: 1px solid var(--line); border-radius: var(--radius);
  padding: 8px 10px; background: var(--surface);
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line); background: var(--surface); border-radius: 999px;
  padding: 3px 10px; font-size: 12px; cursor: pointer; color: var(--muted);
}
.chip.active { color: var(--ink); border-color: var(--ink); }
.chip.s-registered.active { color: var(--s-registered); border-color: var(--s-registered); }
.chip.s-ftd.active { color: var(--s-ftd); border-color: var(--s-ftd); }
.chip.s-vip.active { color: var(--s-vip); border-color: var(--s-vip); }
.chip.s-awaiting_id.active { color: var(--s-awaiting_id); border-color: var(--s-awaiting_id); }

.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; }
.conv {
  display: grid; grid-template-columns: 4px 1fr auto; gap: 0 10px;
  padding: 10px 12px 10px 8px; border-bottom: 1px solid var(--line);
  cursor: pointer; background: var(--surface);
}
.conv:hover { background: #f1f4f7; }
.conv.active { background: #e3edf7; }
.conv .edge { border-radius: 2px; background: var(--s-start); }
.conv.s-awaiting_id .edge { background: var(--s-awaiting_id); }
.conv.s-registered .edge { background: var(--s-registered); }
.conv.s-ftd .edge { background: var(--s-ftd); }
.conv.s-vip .edge { background: var(--s-vip); }
.conv.s-unknown .edge { background: var(--s-unknown); }
.conv .name { font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .name.anon { color: var(--muted); font-weight: 400; font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
.conv .sub code { color: var(--muted); }
.conv .when { color: var(--muted); font-size: 12px; white-space: nowrap; }
.conv .sub { grid-column: 2 / 4; color: var(--muted); font-size: 12px; display: flex; gap: 8px; margin-top: 2px; }
.conv .preview {
  grid-column: 2 / 4; color: var(--muted); font-size: 13px; margin-top: 3px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv .preview .who { color: var(--ink); }
.conv .preview.agent .who { color: var(--role-agent-edge); }
.conv-empty { padding: 24px 16px; color: var(--muted); }
.list-foot { padding: 8px; text-align: center; background: var(--surface); border-top: 1px solid var(--line); }

/* Estado (chip inline) */
.state {
  display: inline-block; font-size: 11px; font-weight: 500; border-radius: 999px;
  padding: 1px 8px; vertical-align: middle; color: #fff; background: var(--s-start);
}
.state.s-awaiting_id { background: var(--s-awaiting_id); }
.state.s-registered { background: var(--s-registered); }
.state.s-ftd { background: var(--s-ftd); }
.state.s-vip { background: var(--s-vip); }
.state.s-unknown { background: var(--s-unknown); }

/* Chat */
.chat { display: grid; min-height: 0; background: var(--bg); }
.chat-empty { display: grid; place-content: center; text-align: center; color: var(--ink); }
#chat-body { display: grid; grid-template-rows: auto 1fr auto; min-height: 0; height: 100%; }
.chat-head {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.back { display: none; }
.chat-name { font-weight: 600; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.chat-meta { color: var(--muted); font-size: 12px; display: flex; flex-wrap: wrap; gap: 12px; margin-top: 2px; }
.chat-meta .lead-ok { color: var(--s-ftd); }
.chat-meta .lead-wait { color: var(--s-registered); }
.chat-meta a { color: var(--primary); text-decoration: none; }

.messages { overflow-y: auto; padding: 12px 24px 16px; display: flex; flex-direction: column; }
#msg-stream { display: flex; flex-direction: column; gap: 2px; }
.older { align-self: center; margin-bottom: 8px; }
.day {
  align-self: center; margin: 12px 0 8px; color: var(--muted); font-size: 12px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px;
  position: sticky; top: 0; z-index: 1;
}
.row { display: flex; justify-content: flex-start; margin: 2px 0; }
.row.bot, .row.agent { justify-content: flex-end; }
.row.same { margin-top: 0; }
.msg {
  min-width: 0;
  max-width: min(64%, 620px);
  background: var(--role-user);
  border: 1px solid var(--line);
  border-radius: 12px 12px 12px 4px;
  padding: 7px 10px 5px;
  line-height: 1.4;
}
.row.bot .msg, .row.agent .msg { border-radius: 12px 12px 4px 12px; border-color: transparent; background: var(--role-bot); }
.row.agent .msg { background: var(--role-agent); }
.msg .from { font-size: 11px; color: var(--muted); margin-bottom: 2px; font-weight: 500; }
.row.same .from { display: none; }
.row.agent .from { color: var(--role-agent-edge); }
.row.bot .from { color: var(--role-bot-edge); }
.msg .body { white-space: pre-line; overflow-wrap: break-word; word-break: break-word; }
.msg .body a { color: var(--primary); }
.msg .body strong { font-weight: 600; }
.msg .time { display: block; text-align: right; margin-top: 3px; font-size: 11px; color: var(--muted); line-height: 1; }
.msg .time.est::before { content: "≈ "; }
.msg img { max-width: 100%; max-height: 320px; border-radius: 8px; display: block; }
.msg video { max-width: 100%; max-height: 320px; border-radius: 8px; display: block; }
.msg audio { display: block; max-width: 100%; min-width: 240px; }
.msg img.sticker { max-height: 160px; }
.msg .file { color: var(--primary); }
.msg .caption { margin-top: 6px; }
.row.sending .msg { opacity: .6; }

.composer { display: grid; gap: 6px; padding: 8px 16px 10px; background: var(--surface); border-top: 1px solid var(--line); }
.composer-row { display: grid; grid-template-columns: auto 1fr auto; gap: 8px; align-items: end; }
.btn.icon { padding: 7px 10px; font-size: 16px; line-height: 1.2; }
.attach-preview { display: flex; align-items: center; gap: 10px; background: var(--surface-2); border: 1px solid var(--line); border-radius: var(--radius); padding: 6px 10px; font-size: 13px; }
.attach-preview img { max-height: 60px; border-radius: 4px; }
.attach-preview audio { height: 32px; }
.link { border: 0; background: none; color: var(--primary); cursor: pointer; padding: 0; font-size: 12px; }
.link:hover { text-decoration: underline; }
.composer textarea {
  resize: none; border: 1px solid var(--line); border-radius: var(--radius);
  padding: 9px 11px; max-height: 160px; background: var(--surface);
}
.composer-note { margin: 0; color: var(--muted); font-size: 12px; }

/* Relatórios */
.reports { padding: 18px; overflow-y: auto; display: grid; gap: 18px; align-content: start; }
.report-tools { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; background: var(--surface); padding: 14px; border: 1px solid var(--line); border-radius: 12px; }
.report-tools label { display: grid; gap: 4px; color: var(--muted); font-size: 12px; }
.report-tools input, .report-tools select { border: 1px solid var(--line); border-radius: var(--radius); padding: 7px 9px; background: var(--surface); color: var(--ink); font-size: 14px; }
.report-block { display: grid; gap: 12px; }
.report-block h2 { margin: 0; font-size: 15px; font-weight: 600; }
.report-block h3 { margin: 12px 0 4px; font-size: 14px; font-weight: 500; }
table { border-collapse: collapse; width: 100%; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
th, td { text-align: left; padding: 8px 12px; border-bottom: 1px solid var(--line); }
th { color: var(--muted); font-weight: 500; font-size: 12px; background: var(--surface-2); }
td.num, th.num { text-align: right; }
tr:last-child td { border-bottom: 0; }
tfoot td { font-weight: 600; background: var(--surface-2); }

.presets { display: flex; gap: 6px; align-self: center; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 12px; }
.kpi { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; }
.kpi .v { font-size: 24px; font-weight: 600; line-height: 1.1; }
.kpi .l { color: var(--muted); font-size: 12px; margin-top: 4px; }
.chart {
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 16px 16px 12px;
  display: grid; grid-template-columns: 40px 1fr; grid-template-rows: 220px auto; column-gap: 8px;
}
.chart-y { display: flex; flex-direction: column; justify-content: space-between; align-items: flex-end; color: var(--muted); font-size: 11px; padding-bottom: 18px; }
.chart-bars {
  display: flex; align-items: flex-end; gap: 4px; height: 100%;
  background: linear-gradient(to top, var(--line) 1px, transparent 1px) 0 0 / 100% 50%;
  padding-bottom: 18px; box-sizing: border-box; position: relative;
}
.chart-bars .col { flex: 1 1 0; min-width: 0; height: 100%; display: flex; flex-direction: column; justify-content: flex-end; }
.chart-bars .stack { position: relative; flex: 1; }
.chart-bars .bar { position: absolute; bottom: 0; left: 0; right: 0; border-radius: 3px 3px 0 0; }
.chart-bars .col:hover .bar.c-new { filter: brightness(1.15); }
.chart-bars .lbl { height: 18px; margin-bottom: -18px; text-align: center; font-size: 10px; color: var(--muted); white-space: nowrap; overflow: visible; }
.chart-bars .bar.c-new, .legend i.c-new { background: var(--primary); }
.chart-bars .bar.c-ftd, .legend i.c-ftd { background: var(--s-ftd); }
.chart .legend { grid-column: 1 / 3; display: flex; gap: 16px; font-size: 12px; color: var(--muted); margin-top: 14px; }
.chart .legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 5px; vertical-align: middle; }

/* Disparos */
.broadcasts { padding: 18px; overflow-y: auto; }
.bc-new { display: grid; grid-template-columns: 340px minmax(380px, 520px) 1fr; gap: 16px; align-items: start; }
.bc-col { display: grid; gap: 16px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 16px; display: grid; gap: 14px; }
.card h2 { margin: 0; font-size: 15px; font-weight: 600; }
.bc-field { display: grid; gap: 6px; }
.bc-label { font-size: 12px; color: var(--muted); font-weight: 500; }
.bc-bots { display: flex; flex-wrap: wrap; gap: 6px; }
.chip.on { color: var(--primary); border-color: var(--primary); background: #e3edf7; }
#bc-states .chip.on.s-registered { color: var(--s-registered); border-color: var(--s-registered); background: #fdf3e3; }
#bc-states .chip.on.s-ftd { color: var(--s-ftd); border-color: var(--s-ftd); background: #e6f4ec; }
#bc-states .chip.on.s-vip { color: var(--s-vip); border-color: var(--s-vip); background: #ede8f8; }
#bc-states .chip.on.s-awaiting_id { color: var(--s-awaiting_id); border-color: var(--s-awaiting_id); background: #f7f0dc; }
.bc-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.bc-grid label { display: grid; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 500; }
.bc-inline { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); font-weight: 500; }
.bc-inline input { width: 64px; }
.bc-new input[type=date], .bc-new input[type=number], .bc-new input[type=text], .bc-new select, .bc-new textarea {
  border: 1px solid var(--line); border-radius: var(--radius); padding: 8px 10px; background: var(--surface); color: var(--ink); font-size: 14px; font-weight: 400;
}
.bc-new textarea { resize: vertical; width: 100%; line-height: 1.45; }
.bc-audience { display: grid; gap: 8px; padding-top: 4px; }
.bc-count { font-size: 13px; color: var(--muted); display: grid; gap: 2px; }
.bc-count b { color: var(--ink); font-size: 18px; }
.fmt-bar { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.fmt-bar button { border: 1px solid var(--line); background: var(--surface); border-radius: 6px; width: 32px; height: 28px; cursor: pointer; font-size: 13px; }
.fmt-bar button:hover { background: var(--surface-2); }
.fmt-help { margin-left: auto; font-size: 11px; color: var(--muted); }
.bc-meta { display: flex; justify-content: space-between; font-size: 11px; color: var(--muted); margin-top: -8px; }
.preview-wrap { background: var(--bg); border-radius: 10px; padding: 12px; display: flex; justify-content: flex-end; }
.preview-bubble { max-width: 90%; background: var(--role-agent); border-radius: 12px 12px 4px 12px; padding: 8px 10px; white-space: pre-line; overflow-wrap: break-word; font-size: 14px; line-height: 1.4; }
.preview-bubble a { color: var(--primary); }
.preview-bubble img { max-width: 100%; max-height: 220px; border-radius: 8px; display: block; margin-bottom: 6px; }
.preview-bubble audio { display: block; width: 100%; margin-bottom: 6px; }
.preview-bubble video { max-width: 100%; border-radius: 8px; display: block; margin-bottom: 6px; }
.preview-bubble .file { color: var(--primary); display: block; margin-bottom: 6px; }
.drop { border: 1.5px dashed var(--line); border-radius: 10px; padding: 14px; text-align: center; font-size: 13px; color: var(--muted); }
.drop.over { border-color: var(--primary); background: #e3edf7; }
.drop .small { font-size: 11px; margin-top: 4px; }
.drop-file { display: flex; align-items: center; justify-content: center; gap: 10px; color: var(--ink); }
.bc-launch { gap: 12px; }
.bc-test { display: grid; grid-template-columns: auto 1fr 1fr auto; gap: 8px; align-items: center; }
.btn.big { padding: 11px 16px; font-size: 15px; font-weight: 500; }
.bc-history .card { gap: 10px; }
.bc-item { border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px; display: grid; gap: 8px; }
.bc-item .head { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.bc-item .title { font-weight: 500; }
.bc-item .st { font-size: 11px; font-weight: 500; border-radius: 999px; padding: 1px 8px; color: #fff; background: var(--s-start); white-space: nowrap; }
.bc-item .st.running { background: var(--primary); } .bc-item .st.done { background: var(--s-ftd); } .bc-item .st.paused { background: var(--s-registered); } .bc-item .st.cancelled, .bc-item .st.failed { background: var(--danger); }
.bc-item .meta { color: var(--muted); font-size: 12px; display: flex; flex-wrap: wrap; gap: 10px; }
.bc-item .text { font-size: 13px; white-space: pre-line; max-height: 4.2em; overflow: hidden; color: var(--ink); background: var(--surface-2); border-radius: 8px; padding: 6px 8px; }
.bar { height: 6px; background: var(--line); border-radius: 3px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--primary); transition: width .4s; }
.bc-item .stats { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.bc-item .stats b { color: var(--ink); }
.bc-item .actions { display: flex; gap: 6px; }
.bc-item .errs { font-size: 12px; color: var(--danger); }
.bc-empty { color: var(--muted); font-size: 13px; }
@media (max-width: 1280px) { .bc-new { grid-template-columns: 1fr 1fr; } .bc-history { grid-column: 1 / 3; } }
@media (max-width: 800px) { .bc-new { grid-template-columns: 1fr; } .bc-history { grid-column: auto; } }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%);
  background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 999px;
  box-shadow: 0 6px 20px rgba(0,0,0,.18); max-width: 90vw;
}
.toast.error { background: var(--danger); }

/* Mobile */
@media (max-width: 900px) {
  .chats { grid-template-columns: 1fr; }
  .rail { display: none; }
  .chats.show-chat .list { display: none; }
  .chats:not(.show-chat) .chat { display: none; }
  .back { display: inline-block; }
  .msg { max-width: 88%; }
  .topbar { gap: 12px; padding: 0 12px; }
  .me, .clock { display: none; }
}

@media (prefers-reduced-motion: no-preference) {
  .row.new .msg { animation: in .18s ease-out; }
  @keyframes in { from { opacity: 0; transform: translateY(4px); } }
}
