:root {
  --bg: #f5f6f4; --panel: #ffffff; --ink: #1b2420; --muted: #66726c; --line: #e2e6e3; --soft: #f0f2f0;
  --acc: #0f766e; --acc-ink: #ffffff; --acc-soft: #e1f2ef;
  --ok: #15803d; --warn: #b45309; --err: #b91c1c;
  --say: #64748b; --ask: #2563eb; --decide: #b45309; --act: #15803d; --time: #7c3aed; --flow: #0e7490;
  --wa: #128c7e; --wa-bubble: #d9fdd3; --wa-bg: #efeae2; --ig: #c13584; --tg: #2481cc; --tg-bubble: #effdde; --web: #0f766e;
  --radius: 12px; --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1312; --panel: #171c1b; --ink: #e6ece9; --muted: #93a19b; --line: #29312e; --soft: #1e2523;
    --acc: #2dd4bf; --acc-ink: #04201c; --acc-soft: #12302c;
    --ok: #4ade80; --warn: #fbbf24; --err: #f87171;
    --say: #94a3b8; --ask: #60a5fa; --decide: #fbbf24; --act: #4ade80; --time: #a78bfa; --flow: #22d3ee;
    --wa-bubble: #144d37; --wa-bg: #0b1411; --tg-bubble: #2b5278;
    --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font: 14px/1.45 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
button, input, select, textarea { font: inherit; color: inherit; }
code, pre, .code { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; }
pre { margin: 6px 0; padding: 8px 10px; background: var(--soft); border-radius: 8px; overflow: auto; max-height: 340px; white-space: pre-wrap; word-break: break-word; }
h2 { font-size: 18px; margin: 0 0 8px; } h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 14px 0 8px; }
h3:first-child { margin-top: 0; }
a { color: var(--acc); }
.muted { color: var(--muted); } .small { font-size: 12px; }

/* --- topo --- */
.topbar { position: sticky; top: 0; z-index: 10; display: flex; gap: 16px; align-items: center; padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.brand { display: flex; gap: 10px; align-items: center; }
.brand b { display: block; } .brand small { color: var(--muted); }
.logo { width: 30px; height: 30px; border-radius: 8px; background: var(--acc); position: relative; }
.logo::after { content: ""; position: absolute; inset: 9px 7px 8px; background: var(--acc-ink); clip-path: polygon(0 0, 100% 0, 100% 70%, 40% 70%, 15% 100%, 15% 70%, 0 70%); }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; }
.tabs button { border: 0; background: none; padding: 8px 12px; border-radius: 8px; cursor: pointer; color: var(--muted); font-weight: 500; }
.tabs button:hover { background: var(--soft); color: var(--ink); }
.tabs button.on { background: var(--acc-soft); color: var(--acc); }
.tabs.small button { padding: 6px 10px; font-size: 13px; position: relative; }
#status { margin-left: auto; display: flex; gap: 6px; }
.pill { font-size: 12px; padding: 3px 10px; border-radius: 999px; border: 1px solid currentColor; }
.pill.ok { color: var(--ok); } .pill.warn { color: var(--warn); } .pill.err { color: var(--err); }
main { padding: 16px; }
.tabpane[hidden] { display: none; }

/* --- componentes --- */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; box-shadow: var(--shadow); min-width: 0; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 14px; margin-bottom: 12px; }
button { cursor: pointer; border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 7px 12px; }
button:hover:not(:disabled) { border-color: var(--acc); }
button:disabled { opacity: .5; cursor: default; }
button.primary { background: var(--acc); color: var(--acc-ink); border-color: var(--acc); font-weight: 600; }
button.danger { color: var(--err); }
button.block { width: 100%; margin-top: 8px; }
button.chip { padding: 4px 10px; font-size: 12px; border-radius: 999px; }
button.icon { padding: 2px 8px; border: 0; background: none; color: var(--muted); }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible, .node:focus-visible { outline: 2px solid var(--acc); outline-offset: 2px; }
input, select, textarea { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 7px 9px; min-width: 0; }
textarea { width: 100%; resize: vertical; }
textarea.invalid { border-color: var(--err); }
label { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
label > input:not([type=checkbox]), label > select { width: 100%; }
label.check { flex-direction: row; align-items: center; gap: 8px; }
.lbl { font-size: 12px; color: var(--muted); font-weight: 500; }
.field { margin-bottom: 10px; }
.row { display: flex; gap: 8px; align-items: center; }
.row > label { flex: 1; }
.row.wrap { flex-wrap: wrap; } .row.between { justify-content: space-between; }
.stack { display: flex; flex-direction: column; gap: 6px; margin-top: 12px; }
.seg { display: grid; grid-template-columns: repeat(2, 1fr); gap: 4px; margin-top: 4px; }
.seg button { padding: 6px; font-size: 13px; }
.seg button.on { color: #fff; border-color: transparent; }
.seg .ch-webchat.on { background: var(--web); } .seg .ch-whatsapp.on { background: var(--wa); } .seg .ch-instagram.on { background: var(--ig); } .seg .ch-telegram.on { background: var(--tg); }
.alert.error { border: 1px solid var(--err); color: var(--err); padding: 12px; border-radius: 10px; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--err); }
.dot.ok { background: var(--ok); } .dot.warn { background: var(--warn); } .dot.err { background: var(--err); }
.tabs .dot { position: absolute; top: 4px; right: 3px; }
.toast { position: fixed; bottom: 20px; left: 50%; transform: translate(-50%, 20px); opacity: 0; transition: .25s; background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; z-index: 100; max-width: min(90vw, 560px); }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--err); color: #fff; }

/* --- simulador --- */
.sim-grid { display: grid; grid-template-columns: 260px minmax(320px, 420px) minmax(0, 1fr); gap: 14px; align-items: start; }
.controls .row select { flex: 1; }
.phone-wrap { padding: 0; overflow: hidden; }
.phone { display: flex; flex-direction: column; height: calc(100vh - 120px); min-height: 520px; }
.phone-head { display: flex; gap: 10px; align-items: center; padding: 10px 14px; color: #fff; background: var(--web); }
.ch-whatsapp .phone-head { background: var(--wa); } .ch-telegram .phone-head { background: var(--tg); }
.ch-instagram .phone-head { background: linear-gradient(90deg, #f58529, #dd2a7b 50%, #8134af); }
.phone-head small { display: block; opacity: .85; }
.avatar { width: 34px; height: 34px; border-radius: 50%; background: rgba(255,255,255,.25); display: grid; place-items: center; font-weight: 700; }
.chat { flex: 1; overflow-y: auto; padding: 14px; display: flex; flex-direction: column; gap: 8px; background: var(--soft); }
.ch-whatsapp .chat { background: var(--wa-bg); }
.empty { margin: auto; color: var(--muted); text-align: center; }
.bubble { max-width: 86%; padding: 8px 11px; border-radius: 12px; background: var(--panel); align-self: flex-start; box-shadow: 0 1px 1px rgba(0,0,0,.06); white-space: pre-wrap; word-wrap: break-word; }
.bubble.from-user { align-self: flex-end; background: var(--acc); color: var(--acc-ink); }
.ch-whatsapp .bubble.from-user { background: var(--wa-bubble); color: var(--ink); }
.ch-telegram .bubble.from-user { background: var(--tg-bubble); color: var(--ink); }
.ch-instagram .bubble.from-user { background: #3797f0; color: #fff; }
.bubble.from-agent { border-left: 3px solid var(--time); }
.bubble .who { font-size: 11px; color: var(--muted); margin-bottom: 3px; }
.bubble .tpl { font-size: 13px; }
.opts { display: flex; flex-direction: column; gap: 4px; margin-top: 8px; }
.opts .opt { width: 100%; text-align: center; color: var(--acc); background: transparent; border: 1px solid var(--line); }
.ch-whatsapp .opts .opt { color: #027eb5; }
.opts.quick { flex-direction: row; flex-wrap: wrap; }
.opts.quick .opt { width: auto; border-radius: 999px; color: #3797f0; border-color: #3797f0; }
.ch-telegram .opts.inline .opt { color: var(--tg); }
.wa-list summary { cursor: pointer; color: #027eb5; text-align: center; border-top: 1px solid var(--line); margin-top: 6px; padding-top: 6px; list-style: none; }
.sysline { align-self: center; font-size: 12px; color: var(--muted); background: var(--panel); padding: 3px 10px; border-radius: 999px; }
.composer { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); background: var(--panel); }
.composer input { flex: 1; }
.inspector { padding: 0; display: flex; flex-direction: column; height: calc(100vh - 120px); min-height: 520px; }
.inspector .tabs { padding: 8px; border-bottom: 1px solid var(--line); }
.insp-body { padding: 12px 14px; overflow: auto; flex: 1; }
.kv { display: grid; gap: 4px; margin-bottom: 10px; }
.kv > div { display: flex; justify-content: space-between; gap: 10px; border-bottom: 1px dashed var(--line); padding: 3px 0; }
.kv span { color: var(--muted); }
.st-waiting_input, .st-active { color: var(--ask); } .st-handoff { color: var(--time); } .st-ended { color: var(--muted); } .st-waiting_integration, .st-waiting_timer { color: var(--decide); }
.mini-graph { overflow: auto; max-height: 300px; border: 1px solid var(--line); border-radius: 10px; margin: 8px 0; background: var(--soft); }
.mini-graph svg { transform-origin: 0 0; }
details.http { border: 1px solid var(--line); border-radius: 8px; margin-bottom: 6px; padding: 6px 8px; }
details.http summary { cursor: pointer; display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
details.http .dir { font-size: 11px; font-weight: 700; text-transform: uppercase; }
details.http.in .dir { color: var(--ask); } details.http.out .dir { color: var(--act); } details.http.integration .dir { color: var(--decide); }
.status { font-size: 11px; padding: 1px 6px; border-radius: 6px; }
.status.s2 { background: var(--acc-soft); color: var(--ok); } .status.s5, .status.st { background: #fee2e2; color: #991b1b; }
pre.hdr { color: var(--muted); }
.log { padding: 3px 0; border-bottom: 1px dashed var(--line); font-size: 12px; word-break: break-word; }
.log code { display: inline-block; width: 44px; color: var(--muted); }
.lv-warn code { color: var(--warn); } .lv-error code { color: var(--err); }
.ticket { border: 1px solid var(--line); border-radius: 10px; padding: 10px; margin-bottom: 10px; }
.ticket .row input { flex: 1; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 999px; background: var(--soft); }
.badge.waiting { color: var(--warn); } .badge.active { color: var(--ok); }
.transcript { font-size: 13px; display: grid; gap: 2px; margin: 6px 0; }
.transcript small { color: var(--muted); text-transform: uppercase; font-size: 10px; }
table.metrics { width: 100%; border-collapse: collapse; margin-bottom: 10px; font-size: 13px; }
table.metrics td { border-bottom: 1px solid var(--line); padding: 4px 2px; } table.metrics td:last-child { text-align: right; }

/* --- grafo --- */
.graph .node rect:first-child { fill: var(--panel); stroke: var(--line); stroke-width: 1.2; }
.graph .node { cursor: pointer; }
.graph .node:hover rect:first-child { stroke: var(--acc); }
.graph .node.selected rect:first-child { stroke: var(--acc); stroke-width: 2.5; }
.graph .node.active rect:first-child { stroke: var(--ask); stroke-width: 3; fill: var(--acc-soft); }
.graph .node.has-error rect:first-child { stroke: var(--err); stroke-width: 2; stroke-dasharray: 4 3; }
.graph .stripe { fill: var(--say); }
.graph .cat-ask .stripe { fill: var(--ask); } .graph .cat-decide .stripe { fill: var(--decide); } .graph .cat-act .stripe { fill: var(--act); }
.graph .cat-time .stripe { fill: var(--time); } .graph .cat-flow .stripe { fill: var(--flow); }
.graph .n-type { font-size: 9.5px; font-weight: 700; letter-spacing: .05em; fill: var(--muted); }
.graph .n-id { font-size: 12.5px; font-weight: 600; fill: var(--ink); }
.graph .n-sum { font-size: 11px; fill: var(--muted); }
.graph .edge { fill: none; stroke: var(--muted); stroke-width: 1.3; opacity: .7; }
.graph .edge.alt { stroke-dasharray: 5 4; stroke: var(--warn); }
.graph .arrowhead { fill: var(--muted); }
.graph .edge-label { font-size: 10.5px; fill: var(--muted); paint-order: stroke; stroke: var(--bg); stroke-width: 3px; }
.graph .edge-label.alt { fill: var(--warn); }

/* --- editor --- */
.editor-grid { display: grid; grid-template-columns: 230px minmax(0, 1fr) 340px; gap: 14px; align-items: start; }
.flow-list { display: flex; flex-direction: column; gap: 4px; }
.flow-item { text-align: left; display: grid; grid-template-columns: 12px 1fr; column-gap: 6px; align-items: center; border-color: transparent; }
.flow-item small { grid-column: 2; color: var(--muted); }
.flow-item.plain { display: block; } .flow-item.plain small { display: block; }
.flow-item.on { background: var(--acc-soft); border-color: var(--acc); }
.graph-panel { display: flex; flex-direction: column; gap: 10px; }
.toolbar { flex-wrap: wrap; }
.graph-box { overflow: auto; max-height: calc(100vh - 290px); min-height: 360px; background: var(--soft); border-radius: 10px; border: 1px solid var(--line); }
.validation { border-radius: 10px; padding: 8px 12px; border: 1px solid; }
.validation.ok { border-color: var(--ok); } .validation.err { border-color: var(--err); }
.validation.ok > b { color: var(--ok); } .validation.err > b { color: var(--err); }
.validation ul { margin: 6px 0 0; padding-left: 18px; }
.validation li.err { color: var(--err); } .validation li.warn { color: var(--warn); }
.inspector-edit { max-height: calc(100vh - 110px); overflow: auto; position: sticky; top: 76px; }
.optrow { display: grid; gap: 4px; margin-bottom: 8px; padding-bottom: 8px; border-bottom: 1px dashed var(--line); }
.optrow.is-opt { grid-template-columns: 80px 1fr 28px; grid-template-areas: "id label del" "match match match" "next next next"; }
.optrow.is-var { grid-template-columns: 80px 80px 1fr 28px; grid-template-areas: "id num next del"; }
.optrow .w-id { grid-area: id; } .optrow .w-label { grid-area: label; } .optrow .w-match { grid-area: match; } .optrow .w-num { grid-area: num; }
.optrow .w-next { grid-area: next; } .optrow .w-next select { width: 100%; } .optrow .w-del { grid-area: del; }
.rows .chip { margin-top: 2px; }

/* --- clientes --- */
.tenant-grid { display: grid; grid-template-columns: 230px minmax(0, 1fr); gap: 14px; align-items: start; }
.kvtable { width: 100%; border-collapse: collapse; }
.kvtable td { padding: 4px; vertical-align: top; border-bottom: 1px solid var(--line); }
.kvtable td:first-child { width: 190px; padding-top: 10px; }
.kvtable textarea { min-height: 34px; }
table.hours td { padding: 4px 6px; border-bottom: 1px solid var(--line); }
.range { display: inline-flex; gap: 4px; align-items: center; margin-right: 10px; }
.checks { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }

/* --- diagnóstico / publicar --- */
.diag .panel, .publish .panel { max-width: 980px; margin: 0 auto 14px; }
.summary { margin: 10px 0; padding: 10px 14px; border-radius: 10px; background: var(--soft); font-weight: 600; }
.summary.good { color: var(--ok); } .summary.bad { color: var(--err); }
.diag-group h3 { margin-top: 18px; }
.diag-row { display: grid; grid-template-columns: 22px minmax(0, 1.2fr) minmax(0, 1fr) 70px; gap: 10px; padding: 7px 4px; border-bottom: 1px solid var(--line); align-items: baseline; }
.diag-row .icon { font-weight: 700; } .diag-row.pass .icon { color: var(--ok); } .diag-row.fail .icon { color: var(--err); } .diag-row.fail .detail { color: var(--err); }
.diag-row .detail { color: var(--muted); font-size: 13px; } .diag-row .ms { text-align: right; color: var(--muted); font-size: 12px; }
.publish ol li, .publish ul li { margin-bottom: 6px; }

@media (max-width: 1180px) {
  .sim-grid { grid-template-columns: 240px minmax(0, 1fr); }
  .sim-grid .inspector { grid-column: 1 / -1; height: auto; max-height: 70vh; }
  .editor-grid { grid-template-columns: 200px minmax(0, 1fr); }
  .editor-grid .inspector-edit { grid-column: 1 / -1; position: static; max-height: none; }
}
@media (max-width: 760px) {
  main { padding: 10px; }
  .sim-grid, .editor-grid, .tenant-grid { grid-template-columns: minmax(0, 1fr); }
  .phone { height: 70vh; }
  #status { margin-left: 0; }
  .diag-row { grid-template-columns: 22px minmax(0, 1fr); }
  .diag-row .detail, .diag-row .ms { grid-column: 2; text-align: left; }
}
