:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #16181d;
  --text-2: #4a5160;
  --muted: #7a8190;
  --accent: #1f6feb;
  --accent-soft: #e6effd;
  --good: #1a7f4b;
  --good-soft: #e3f4ea;
  --warn: #9a6200;
  --warn-soft: #fcf1dc;
  --bad: #c0362c;
  --bad-soft: #fbe6e4;
  --bubble-in: #ffffff;
  --bubble-out: #dff3e4;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 1px 3px rgba(16, 24, 40, .08);
  --radius: 10px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1f232b;
    --border: #2b3039;
    --text: #e8eaee;
    --text-2: #b6bcc7;
    --muted: #858c99;
    --accent: #5b9dff;
    --accent-soft: #1b2a42;
    --good: #4cc38a;
    --good-soft: #15291f;
    --warn: #e0a84a;
    --warn-soft: #2e2413;
    --bad: #f07167;
    --bad-soft: #321a19;
    --bubble-in: #1f232b;
    --bubble-out: #183326;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
}
[hidden] { display: none !important; }
a { color: var(--accent); text-decoration: none; }
h1 { font-size: 22px; margin: 0 0 4px; font-weight: 650; }
h2 { font-size: 16px; margin: 0 0 12px; font-weight: 650; }
h3 { font-size: 14px; margin: 0 0 8px; font-weight: 650; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--bad); margin: 0; }
.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }

input, select, textarea, button { font: inherit; color: inherit; }
input, select, textarea {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  min-height: 38px;
  width: 100%;
}
textarea { min-height: 90px; resize: vertical; }
input[type=checkbox], input[type=radio] { width: auto; min-height: 0; accent-color: var(--accent); }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--text-2); }
label.inline { flex-direction: row; align-items: center; gap: 8px; }

.btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 8px 14px;
  min-height: 38px;
  cursor: pointer;
  font-weight: 550;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); }
.btn.small { padding: 4px 10px; min-height: 30px; font-size: 13px; }
.btn:disabled { opacity: .55; cursor: default; }
.link-btn { background: none; border: 0; color: var(--accent); cursor: pointer; padding: 0; }
.icon-btn { background: none; border: 0; font-size: 20px; cursor: pointer; padding: 6px 10px; color: var(--text); }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card {
  width: min(360px, 100%);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px;
  display: flex; flex-direction: column; gap: 14px;
  box-shadow: var(--shadow);
}
.login-card p { margin: -8px 0 4px; }

/* ---------- layout ---------- */
.layout { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.topbar { display: none; }
.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  padding: 18px 12px;
  display: flex; flex-direction: column; gap: 10px;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.brand { font-weight: 700; font-size: 15px; }
.side-brand { padding: 0 8px 4px; }
.nav-links { display: flex; flex-direction: column; gap: 2px; margin-top: 6px; }
.nav-links a { color: var(--text-2); padding: 8px 10px; border-radius: 8px; }
.nav-links a:hover { background: var(--surface-2); }
.nav-links a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-links .sep { height: 1px; background: var(--border); margin: 8px 4px; }
.side-footer { margin-top: auto; padding: 8px; display: flex; justify-content: space-between; gap: 8px; font-size: 13px; }
.wa-pill {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 12px; padding: 4px 10px; border-radius: 99px;
  background: var(--surface-2); color: var(--text-2); align-self: flex-start; margin-left: 4px;
}
.wa-pill::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.wa-pill.ok::before { background: var(--good); }
.wa-pill.bad { background: var(--bad-soft); color: var(--bad); }
.wa-pill.bad::before { background: var(--bad); }

.main { padding: 24px 28px 60px; min-width: 0; max-width: 1400px; }
.main.wide { max-width: none; }
.page-head { display: flex; flex-wrap: wrap; align-items: flex-end; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.page-head p { margin: 2px 0 0; }

@media (max-width: 860px) {
  .layout { grid-template-columns: 1fr; }
  .topbar {
    display: flex; align-items: center; gap: 8px;
    position: sticky; top: 0; z-index: 20;
    background: var(--surface); border-bottom: 1px solid var(--border);
    padding: 6px 8px;
  }
  .topbar .wa-pill { margin-left: auto; }
  .sidebar {
    position: fixed; z-index: 30; left: 0; top: 0; width: 250px;
    transform: translateX(-100%); transition: transform .18s ease;
    box-shadow: 0 0 30px rgba(0,0,0,.25);
  }
  .sidebar.open { transform: none; }
  .sidebar > .wa-pill { display: none; }
  .main { padding: 16px 16px 60px; }
}

/* ---------- blocos ---------- */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.stack { display: flex; flex-direction: column; gap: 16px; }
.row { display: flex; flex-wrap: wrap; gap: 10px; align-items: flex-end; }
.row > label { flex: 1 1 160px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
@media (max-width: 860px) { .grid-2 { grid-template-columns: 1fr; } }

.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 14px; }
.filters input, .filters select { width: auto; min-width: 140px; }
.filters input[type=search] { min-width: 220px; flex: 1 1 220px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button { border: 0; background: none; padding: 7px 12px; cursor: pointer; color: var(--text-2); border-right: 1px solid var(--border); }
.seg button:last-child { border-right: 0; }
.seg button.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* ---------- indicadores ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); }
.tile .label { color: var(--text-2); font-size: 13px; }
.tile .value { font-size: 26px; font-weight: 650; margin-top: 4px; letter-spacing: -.01em; }
.tile .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.tile.hero .value { font-size: 40px; }
a.tile { color: inherit; display: block; }
a.tile:hover { border-color: var(--accent); }

.section-title { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin: 26px 0 10px; }
.section-title h2 { margin: 0; }

.funnel { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.funnel .tile { border-top: 3px solid var(--stage, var(--border)); }

/* ---------- tabelas ---------- */
.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
table { border-collapse: collapse; width: 100%; }
th, td { padding: 9px 12px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
th { font-size: 12px; font-weight: 600; color: var(--text-2); background: var(--surface-2); white-space: nowrap; position: sticky; top: 0; }
tr:last-child td { border-bottom: 0; }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover { background: var(--surface-2); }
tr.total td { font-weight: 650; background: var(--surface-2); }
td .name { font-weight: 550; }
.empty { padding: 28px; text-align: center; color: var(--muted); }

/* ---------- etiquetas ---------- */
.badge { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; padding: 2px 8px; border-radius: 99px; background: var(--surface-2); color: var(--text-2); white-space: nowrap; }
.badge.meta { background: var(--accent-soft); color: var(--accent); }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; flex: none; background: var(--c, var(--muted)); }

/* ---------- conversa ---------- */
.lead-layout { display: grid; grid-template-columns: 380px 1fr; gap: 16px; align-items: start; }
@media (max-width: 1000px) { .lead-layout { grid-template-columns: 1fr; } }
.kv { display: grid; grid-template-columns: 110px 1fr; gap: 6px 10px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }
.timeline { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; font-size: 13px; }
.timeline li { display: flex; gap: 8px; align-items: baseline; }
.timeline .dot { position: relative; top: 1px; }
.chat {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; display: flex; flex-direction: column; gap: 6px; max-height: 75vh; overflow-y: auto;
}
.bubble { max-width: 78%; align-self: flex-start; padding: 7px 10px 5px; border-radius: 10px; background: var(--bubble-in); box-shadow: var(--shadow); word-break: break-word; }
.bubble > div { white-space: pre-wrap; }
.bubble.out { align-self: flex-end; background: var(--bubble-out); }
.bubble time { display: block; font-size: 11px; color: var(--muted); text-align: right; margin-top: 2px; }
.bubble.hit { outline: 2px solid var(--accent); }
.bubble audio { width: 260px; max-width: 100%; height: 36px; margin: 2px 0; display: block; }
.bubble .media-img { max-width: min(260px, 100%); max-height: 320px; border-radius: 8px; display: block; background: var(--surface-2); }
.bubble .media-line { display: flex; justify-content: space-between; gap: 10px; font-size: 12px; color: var(--muted); }
.bubble .doc { display: inline-block; padding: 4px 0; word-break: break-word; }
.media-erro { font-size: 12px; color: var(--bad); display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.day-sep { align-self: center; font-size: 12px; color: var(--muted); background: var(--surface); padding: 2px 10px; border-radius: 99px; margin: 6px 0; }

/* ---------- kanban ---------- */
.kanban { display: flex; gap: 12px; overflow-x: auto; padding-bottom: 12px; align-items: flex-start; }
.col { flex: 0 0 250px; background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius); display: flex; flex-direction: column; max-height: calc(100vh - 170px); }
.col-head { padding: 10px 12px; display: flex; align-items: center; gap: 8px; font-weight: 600; border-top: 3px solid var(--c); border-radius: var(--radius) var(--radius) 0 0; }
.col-head .count { margin-left: auto; color: var(--muted); font-weight: 500; font-variant-numeric: tabular-nums; }
.col-body { padding: 8px; display: flex; flex-direction: column; gap: 8px; overflow-y: auto; min-height: 80px; }
.col.drop { outline: 2px dashed var(--accent); outline-offset: -2px; }
.kcard { background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 9px 10px; cursor: grab; box-shadow: var(--shadow); }
.kcard:active { cursor: grabbing; }
.kcard .name { font-weight: 600; }
.kcard .meta-line { display: flex; justify-content: space-between; gap: 8px; margin-top: 4px; font-size: 12px; color: var(--muted); }
.kcard .ad { font-size: 12px; color: var(--accent); margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* ---------- modal e aviso ---------- */
.modal { position: fixed; inset: 0; background: rgba(10, 12, 16, .45); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal-card { background: var(--surface); border-radius: 14px; padding: 20px; width: min(460px, 100%); max-height: 90vh; overflow-y: auto; display: flex; flex-direction: column; gap: 12px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; flex-wrap: wrap; }
.stage-buttons { display: flex; flex-direction: column; gap: 6px; }
.stage-buttons .btn { display: flex; align-items: center; gap: 8px; justify-content: flex-start; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; z-index: 60; max-width: calc(100% - 32px); }
.toast.bad { background: var(--bad); color: #fff; }

.notice { border-radius: var(--radius); padding: 12px 14px; font-size: 13px; background: var(--warn-soft); color: var(--text); border: 1px solid transparent; }
.notice.info { background: var(--accent-soft); }
.notice.bad { background: var(--bad-soft); }
.notice strong { font-weight: 650; }

.qr { display: grid; place-items: center; background: #fff; border-radius: 12px; padding: 12px; width: 280px; height: 280px; }
.qr img { width: 256px; height: 256px; }
.steps { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 4px; }

.stage-edit { display: grid; grid-template-columns: auto 1fr; gap: 12px; align-items: start; }
.stage-edit .order { display: flex; flex-direction: column; gap: 4px; }
.stage-edit .fields { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stage-edit .fields .wide { grid-column: 1 / -1; }
@media (max-width: 700px) { .stage-edit .fields { grid-template-columns: 1fr; } }
pre.response { white-space: pre-wrap; word-break: break-all; font-size: 12px; background: var(--surface-2); padding: 8px; border-radius: 6px; margin: 6px 0 0; max-width: 520px; }
