:root {
  --bg: #f4f6fb;
  --surface: #ffffff;
  --surface-2: #f8f9fc;
  --border: #e3e7ef;
  --text: #1b2132;
  --muted: #6b7489;
  --primary: #0a7cff;
  --primary-soft: #e6f1ff;
  --primary-text: #ffffff;
  --side: #10152b;
  --side-text: #aab3cf;
  --side-active: #1d2645;
  --ok: #12a150;
  --ok-soft: #e3f6ea;
  --warn: #c77700;
  --warn-soft: #fff3dc;
  --danger: #d93a3a;
  --danger-soft: #fde8e8;
  --wa: #1fa855;
  --ig: #d6246e;
  --bubble-in: #ffffff;
  --bubble-out: #0a7cff;
  --bubble-out-text: #ffffff;
  --chat-bg: #eef1f7;
  --shadow: 0 1px 2px rgba(16, 21, 43, .06), 0 4px 16px rgba(16, 21, 43, .05);
  --radius: 12px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0c1020;
    --surface: #151a2e;
    --surface-2: #1a2036;
    --border: #262e48;
    --text: #e7eaf3;
    --muted: #8e97b3;
    --primary: #3d95ff;
    --primary-soft: #16294a;
    --side: #080b17;
    --side-active: #1a2140;
    --ok-soft: #10301f;
    --warn-soft: #3a2a0c;
    --danger-soft: #3a1717;
    --bubble-in: #1f263f;
    --bubble-out: #2c7be5;
    --chat-bg: #0f1426;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--primary); }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 22px; font-weight: 700; }
h2 { font-size: 16px; font-weight: 650; }
h3 { font-size: 14px; font-weight: 650; }
code { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12.5px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 6px; padding: 1px 6px; word-break: break-all; }

/* ---------- layout ---------- */
.shell { display: grid; grid-template-columns: 232px 1fr; height: 100vh; }
.side { background: var(--side); color: var(--side-text); display: flex; flex-direction: column; padding: 18px 12px; gap: 4px; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 10px; color: #fff; font-weight: 700; font-size: 17px; padding: 4px 10px 18px; }
.brand-mark { width: 30px; height: 30px; border-radius: 9px; background: linear-gradient(135deg, #0a7cff, #7b5cff); display: grid; place-items: center; font-size: 16px; }
.nav a { display: flex; align-items: center; gap: 11px; padding: 9px 12px; border-radius: 9px; color: var(--side-text); text-decoration: none; font-weight: 500; }
.nav a:hover { background: rgba(255, 255, 255, .05); color: #fff; }
.nav a.active { background: var(--side-active); color: #fff; }
.nav .ico { width: 18px; text-align: center; opacity: .9; }
.nav .badge { margin-left: auto; background: var(--primary); color: #fff; border-radius: 99px; font-size: 11px; padding: 0 7px; font-weight: 600; }
.side-foot { margin-top: auto; padding: 12px 10px 0; border-top: 1px solid rgba(255, 255, 255, .07); display: grid; gap: 8px; font-size: 12px; }
.chan-status { display: flex; align-items: center; gap: 8px; }
.dot { width: 8px; height: 8px; border-radius: 50%; background: #5b647f; flex: none; }
.dot.on { background: #2fd07a; box-shadow: 0 0 0 3px rgba(47, 208, 122, .15); }
.side-foot button { background: none; border: 0; color: var(--side-text); padding: 0; text-align: left; cursor: pointer; }
.side-foot button:hover { color: #fff; }

.main { overflow-y: auto; min-width: 0; }
.page { padding: 28px 32px 48px; max-width: 1240px; margin: 0 auto; }
.page-head { display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; margin-bottom: 22px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; color: var(--muted); }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.stack { display: grid; gap: 14px; align-content: start; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.grid-kpi { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 14px; margin-bottom: 18px; }

/* ---------- componentes ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px; min-width: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 14px; }
.kpi .label { color: var(--muted); font-size: 12.5px; font-weight: 500; }
.kpi .value { font-size: 26px; font-weight: 700; margin-top: 4px; font-variant-numeric: tabular-nums; }
.kpi .sub { color: var(--muted); font-size: 12px; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 7px; border: 1px solid var(--border); background: var(--surface); padding: 8px 14px; border-radius: 9px; font-weight: 550; cursor: pointer; white-space: nowrap; text-decoration: none; color: var(--text); }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: var(--primary-text); }
.btn.primary:hover { filter: brightness(1.07); }
.btn.danger { color: var(--danger); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.sm { padding: 5px 10px; font-size: 12.5px; border-radius: 8px; }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.icon-btn { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 4px 6px; border-radius: 6px; }
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

.field { display: grid; gap: 6px; }
.field > span { font-weight: 550; font-size: 13px; }
.field small { color: var(--muted); font-size: 12px; }
.input, select.input, textarea.input { width: 100%; border: 1px solid var(--border); background: var(--surface); border-radius: 9px; padding: 8px 11px; outline: none; }
.input:focus { border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
textarea.input { resize: vertical; min-height: 80px; }

.pill { display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; font-weight: 600; border-radius: 99px; padding: 2px 9px; background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); white-space: nowrap; }
.pill.ok { background: var(--ok-soft); color: var(--ok); border-color: transparent; }
.pill.warn { background: var(--warn-soft); color: var(--warn); border-color: transparent; }
.pill.danger { background: var(--danger-soft); color: var(--danger); border-color: transparent; }
.pill.blue { background: var(--primary-soft); color: var(--primary); border-color: transparent; }
.pill.test { background: #fff0c2; color: #8a5a00; border-color: transparent; letter-spacing: .02em; }
.tag { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; font-weight: 550; border-radius: 6px; padding: 2px 8px; color: #fff; }
.tag button { background: none; border: 0; color: inherit; cursor: pointer; padding: 0 0 0 2px; opacity: .8; }
.ch { display: inline-flex; width: 18px; height: 18px; border-radius: 5px; align-items: center; justify-content: center; font-size: 10px; font-weight: 700; color: #fff; flex: none; }
.ch.whatsapp { background: var(--wa); }
.ch.instagram { background: linear-gradient(45deg, #f9a825, #d6246e, #7b3fe4); }

.switch { position: relative; display: inline-block; width: 36px; height: 20px; flex: none; vertical-align: middle; }
.switch input { opacity: 0; width: 0; height: 0; }
.switch span { position: absolute; inset: 0; background: var(--border); border-radius: 99px; cursor: pointer; transition: .15s; }
.switch span::after { content: ""; position: absolute; width: 16px; height: 16px; left: 2px; top: 2px; background: #fff; border-radius: 50%; transition: .15s; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.switch input:checked + span { background: var(--primary); }
.switch input:checked + span::after { transform: translateX(16px); }

table.table { width: 100%; border-collapse: collapse; }
.table th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 10px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.table td { padding: 11px 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tr:last-child td { border-bottom: 0; }
.table tr.click { cursor: pointer; }
.table tr.click:hover td { background: var(--surface-2); }
.table-wrap { overflow-x: auto; }

.empty { text-align: center; padding: 42px 16px; color: var(--muted); }
.empty .big { font-size: 34px; margin-bottom: 8px; }
.empty h3 { color: var(--text); margin-bottom: 4px; }

.notice { border-radius: 10px; padding: 11px 14px; background: var(--primary-soft); color: var(--text); border: 1px solid transparent; display: flex; gap: 10px; align-items: flex-start; }
.notice.warn { background: var(--warn-soft); }
.notice.danger { background: var(--danger-soft); }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 16px; overflow-x: auto; }
.tabs button { background: none; border: 0; padding: 9px 12px; cursor: pointer; color: var(--muted); font-weight: 550; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs button.active { color: var(--primary); border-bottom-color: var(--primary); }

/* ---------- modal e toast ---------- */
.overlay { position: fixed; inset: 0; background: rgba(8, 11, 23, .5); display: grid; place-items: center; padding: 16px; z-index: 50; }
.modal { background: var(--surface); border-radius: 14px; width: min(560px, 100%); max-height: calc(100vh - 32px); overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 20px 0; }
.modal-body { padding: 16px 20px; display: grid; gap: 14px; }
.modal-foot { padding: 0 20px 18px; display: flex; justify-content: flex-end; gap: 8px; }
.toasts { position: fixed; right: 18px; bottom: 18px; display: grid; gap: 8px; z-index: 60; }
.toast { background: var(--side); color: #fff; padding: 10px 14px; border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.25); max-width: 380px; }
.toast.error { background: var(--danger); }

/* ---------- login ---------- */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: radial-gradient(1200px 600px at 20% -10%, rgba(10,124,255,.18), transparent), var(--bg); }
.auth .card { width: min(400px, 100%); padding: 28px; }
.auth .brand { color: var(--text); padding: 0 0 16px; }

/* ---------- chat ao vivo ---------- */
.live { display: grid; grid-template-columns: 320px minmax(0, 1fr) 300px; height: 100vh; }
.conv-list { border-right: 1px solid var(--border); background: var(--surface); display: flex; flex-direction: column; min-height: 0; }
.conv-list .head { padding: 16px 14px 10px; display: grid; gap: 10px; border-bottom: 1px solid var(--border); }
.chips { display: flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--border); background: var(--surface); border-radius: 99px; padding: 3px 10px; font-size: 12px; cursor: pointer; color: var(--muted); font-weight: 550; }
.chip.active { background: var(--primary-soft); border-color: transparent; color: var(--primary); }
.conv-items { overflow-y: auto; flex: 1; }
.conv { display: flex; gap: 10px; padding: 11px 14px; cursor: pointer; border-bottom: 1px solid var(--border); align-items: center; }
.conv:hover { background: var(--surface-2); }
.conv.active { background: var(--primary-soft); }
.avatar { width: 38px; height: 38px; border-radius: 50%; background: #c9d4ea; color: #34405f; display: grid; place-items: center; font-weight: 700; flex: none; position: relative; }
.avatar .ch { position: absolute; right: -3px; bottom: -3px; width: 16px; height: 16px; font-size: 9px; border: 2px solid var(--surface); box-sizing: content-box; }
.conv .name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .preview { color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .meta { display: grid; justify-items: end; gap: 4px; font-size: 11px; color: var(--muted); }
.unread { background: var(--primary); color: #fff; border-radius: 99px; min-width: 18px; height: 18px; font-size: 11px; display: grid; place-items: center; padding: 0 5px; font-weight: 600; }

.thread { display: flex; flex-direction: column; min-height: 0; background: var(--chat-bg); }
.thread-head { display: flex; align-items: center; gap: 10px; padding: 12px 18px; background: var(--surface); border-bottom: 1px solid var(--border); }
.msgs { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 6px; }
.bubble { max-width: min(72%, 520px); padding: 8px 12px; border-radius: 14px; background: var(--bubble-in); box-shadow: 0 1px 1px rgba(0,0,0,.05); word-wrap: break-word; align-self: flex-start; }
.bubble .txt { white-space: pre-wrap; }
.bubble.out { align-self: flex-end; background: var(--bubble-out); color: var(--bubble-out-text); border-bottom-right-radius: 4px; }
.bubble.in { border-bottom-left-radius: 4px; }
.bubble.system { align-self: center; background: transparent; box-shadow: none; color: var(--muted); font-size: 12px; }
.bubble.comment { border: 1px dashed var(--ig); }
.bubble.failed { background: var(--danger-soft); color: var(--text); border: 1px solid var(--danger); }
.bubble .foot { font-size: 10.5px; opacity: .75; margin-top: 3px; display: flex; gap: 6px; justify-content: flex-end; }
.bubble .btns { display: grid; gap: 4px; margin-top: 8px; }
.bubble .btns span, .bubble .btns button { display: block; text-align: center; background: rgba(255,255,255,.18); border-radius: 8px; padding: 5px 8px; font-size: 13px; font-weight: 550; border: 0; color: inherit; }
.bubble.in .btns button { background: var(--primary-soft); color: var(--primary); cursor: pointer; }
.bubble .err { font-size: 12px; color: var(--danger); margin-top: 4px; }
.day-sep { align-self: center; font-size: 11px; color: var(--muted); background: var(--surface); border-radius: 99px; padding: 2px 10px; margin: 8px 0; }
.composer { background: var(--surface); border-top: 1px solid var(--border); padding: 12px 14px; display: grid; gap: 8px; }
.composer textarea { min-height: 44px; max-height: 160px; }

.details { border-left: 1px solid var(--border); background: var(--surface); overflow-y: auto; padding: 18px 16px; display: grid; gap: 18px; align-content: start; }
.details .section { display: grid; gap: 8px; }
.details .section > h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }

/* ---------- editor de fluxo ---------- */
.flow-editor { display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 20px; align-items: start; }
.canvas { background-color: var(--surface-2); background-image: radial-gradient(var(--border) 1px, transparent 1px); background-size: 18px 18px; border: 1px solid var(--border); border-radius: var(--radius); padding: 22px; display: grid; justify-items: center; gap: 0; }
.start-node { background: var(--ok); color: #fff; border-radius: 99px; padding: 5px 14px; font-weight: 600; font-size: 12.5px; }
.connector { width: 2px; height: 22px; background: var(--border); position: relative; }
.node { width: min(460px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); overflow: hidden; }
.node-head { display: flex; align-items: center; gap: 8px; padding: 8px 10px 8px 12px; border-bottom: 1px solid var(--border); font-weight: 600; font-size: 13px; }
.node-head .kind-ico { width: 24px; height: 24px; border-radius: 7px; display: grid; place-items: center; font-size: 13px; color: #fff; }
.node-body { padding: 12px; display: grid; gap: 10px; }
.node .btn-row { display: grid; grid-template-columns: 1fr 1fr 1fr auto; gap: 6px; align-items: center; }
.add-step { position: relative; }
.add-menu { position: absolute; top: calc(100% + 6px); left: 50%; transform: translateX(-50%); background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 12px 32px rgba(0,0,0,.15); padding: 6px; display: grid; grid-template-columns: 1fr 1fr; gap: 2px; width: 340px; z-index: 5; }
.add-menu button { display: flex; gap: 8px; align-items: center; text-align: left; background: none; border: 0; padding: 8px; border-radius: 8px; cursor: pointer; font-size: 13px; }
.add-menu button:hover { background: var(--surface-2); }
.phone { position: sticky; top: 20px; background: #111; border-radius: 30px; padding: 10px; box-shadow: var(--shadow); }
.phone-screen { background: var(--chat-bg); border-radius: 22px; height: 560px; display: flex; flex-direction: column; overflow: hidden; }
.phone-top { background: var(--surface); padding: 10px 14px; font-weight: 600; font-size: 13px; display: flex; gap: 8px; align-items: center; border-bottom: 1px solid var(--border); }
.phone .msgs { padding: 12px; }
.phone .bubble { max-width: 85%; font-size: 13px; }
.phone-input { display: flex; gap: 6px; padding: 8px; background: var(--surface); border-top: 1px solid var(--border); }
.phone-input input { flex: 1; border-radius: 99px; }

.guide ol { margin: 0; padding-left: 20px; display: grid; gap: 8px; }
.copy { display: flex; gap: 6px; align-items: center; }
.copy code { flex: 1; padding: 7px 10px; }
.bars { display: flex; align-items: flex-end; gap: 6px; height: 160px; padding-top: 8px; }
.bars .bar { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; gap: 2px; height: 100%; min-width: 0; }
.bars .bar i { display: block; border-radius: 3px 3px 0 0; min-height: 0; }
.bars .bar .in { background: var(--primary); }
.bars .bar .out { background: #9fc6ff; border-radius: 0; }
.bars-x { display: flex; gap: 6px; font-size: 10.5px; color: var(--muted); margin-top: 6px; }
.bars-x span { flex: 1; text-align: center; min-width: 0; overflow: hidden; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.checklist { display: grid; gap: 10px; }
.checklist .item { display: flex; gap: 10px; align-items: center; }
.check { width: 22px; height: 22px; border-radius: 50%; border: 2px solid var(--border); display: grid; place-items: center; font-size: 12px; flex: none; }
.check.done { background: var(--ok); border-color: var(--ok); color: #fff; }

.mobile-bar { display: none; }

@media (max-width: 1100px) {
  .live { grid-template-columns: 280px minmax(0, 1fr); }
  .details { display: none; }
  .flow-editor { grid-template-columns: 1fr; }
  .phone { position: static; }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: fixed; inset: 0 30% 0 0; z-index: 40; transform: translateX(-105%); transition: transform .2s; }
  .side.open { transform: none; }
  .mobile-bar { display: flex; align-items: center; gap: 10px; padding: 10px 16px; background: var(--side); color: #fff; position: sticky; top: 0; z-index: 30; }
  .mobile-bar button { background: none; border: 0; color: #fff; font-size: 20px; }
  .page { padding: 18px 16px 40px; }
  .grid-2 { grid-template-columns: 1fr; }
  .live { grid-template-columns: 1fr; height: calc(100vh - 48px); }
  .live.has-thread .conv-list { display: none; }
  .live:not(.has-thread) .thread { display: none; }
  .node .btn-row { grid-template-columns: 1fr 1fr; }
  .add-menu { width: 280px; grid-template-columns: 1fr; }
}

.row > .input.grow { min-width: 180px; }
