:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --line: #dfe3e8;
  --text: #1c2330;
  --muted: #667085;
  --accent: #3b5bfd;
  --accent-soft: #e8edff;
  --danger: #d92d20;
  --ok: #12805c;
  --bubble-bot: #eef0f4;
  --bubble-user: #3b5bfd;
  --c-start: #f59e0b;
  --c-message: #3b5bfd;
  --c-input: #0ea5e9;
  --c-condition: #8b5cf6;
  --c-action: #10b981;
  --c-delay: #f97316;
  --c-random: #ec4899;
  --c-goto: #64748b;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06), 0 4px 12px rgba(16, 24, 40, .06);
  --grid: rgba(16, 24, 40, .07);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f131a; --surface: #171c25; --surface-2: #1e2430; --line: #2b3341; --text: #e6e9ef; --muted: #98a2b3;
    --accent: #7b93ff; --accent-soft: #222c4d; --bubble-bot: #242b38; --bubble-user: #4a66f5;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 12px rgba(0,0,0,.3); --grid: rgba(255,255,255,.05);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f131a; --surface: #171c25; --surface-2: #1e2430; --line: #2b3341; --text: #e6e9ef; --muted: #98a2b3;
  --accent: #7b93ff; --accent-soft: #222c4d; --bubble-bot: #242b38; --bubble-user: #4a66f5;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 12px rgba(0,0,0,.3); --grid: rgba(255,255,255,.05);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }

.app { display: grid; grid-template-columns: 210px 1fr; height: 100vh; }
.nav { background: var(--surface); border-right: 1px solid var(--line); display: flex; flex-direction: column; padding: 14px 10px; gap: 2px; }
.brand { font-weight: 700; font-size: 17px; padding: 4px 10px 14px; }
.logo { color: var(--accent); }
.nav a { padding: 8px 10px; border-radius: 8px; cursor: pointer; color: var(--text); text-decoration: none; }
.nav a:hover { background: var(--surface-2); }
.nav a.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav-foot { margin-top: auto; display: flex; flex-direction: column; gap: 6px; }
.nav-foot small { color: var(--muted); padding: 0 10px; font-size: 11px; }
.main { overflow: auto; min-width: 0; }

.page { padding: 24px 28px; max-width: 1100px; }
.page h1 { font-size: 22px; margin: 0 0 4px; }
.page .sub { color: var(--muted); margin: 0 0 20px; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.spacer { flex: 1; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

.btn { border: 1px solid var(--accent); background: var(--accent); color: #fff; padding: 7px 14px; border-radius: 8px; cursor: pointer; font-weight: 600; white-space: nowrap; }
.btn:hover { filter: brightness(1.07); }
.btn.ghost { background: transparent; color: var(--text); border-color: var(--line); font-weight: 500; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.danger { background: transparent; color: var(--danger); border-color: var(--line); font-weight: 500; }
.btn.sm { padding: 3px 9px; font-size: 12px; }
.btn.chip { background: var(--surface); color: var(--text); border-color: var(--line); font-weight: 500; padding: 5px 10px; font-size: 13px; }
.btn.chip:hover { border-color: var(--accent); }
.x { border: 0; background: none; color: var(--muted); cursor: pointer; padding: 0 4px; font-size: 15px; }
.x:hover { color: var(--danger); }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 16px; box-shadow: var(--shadow); }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 14px; }
.flow-card h3 { margin: 0 0 6px; font-size: 16px; }
.flow-card .meta { color: var(--muted); font-size: 12px; margin-bottom: 10px; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; }
.tag { display: inline-flex; align-items: center; gap: 2px; background: var(--accent-soft); color: var(--accent); border-radius: 999px; padding: 1px 9px; font-size: 12px; font-weight: 500; }
.tag.gray { background: var(--surface-2); color: var(--muted); }

table.tbl { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.tbl th, .tbl td { text-align: left; padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.tbl th { font-size: 12px; color: var(--muted); font-weight: 600; background: var(--surface-2); }
.tbl tr:last-child td { border-bottom: 0; }
.tbl input, .tbl select { width: 100%; }

input, select, textarea { background: var(--surface); border: 1px solid var(--line); border-radius: 7px; padding: 6px 9px; outline: none; min-width: 0; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: vertical; width: 100%; }
input[type="checkbox"] { width: auto; }
.fld { display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }
.fld > span { font-size: 12px; color: var(--muted); font-weight: 600; }
.fld input, .fld select { width: 100%; }
.check { display: flex; align-items: center; gap: 6px; margin-bottom: 10px; }

/* ---------- builder ---------- */
.builder { display: flex; flex-direction: column; height: 100vh; }
.bar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--line); background: var(--surface); flex-wrap: wrap; }
.flow-name { font-weight: 600; font-size: 15px; width: 220px; }
.work { flex: 1; display: grid; grid-template-columns: 1fr 340px; min-height: 0; }
.canvas-wrap { position: relative; overflow: hidden; cursor: grab; background-color: var(--bg);
  background-image: radial-gradient(var(--grid) 1.2px, transparent 1.2px); background-size: 22px 22px; touch-action: none; }
.canvas-wrap.panning { cursor: grabbing; }
.canvas { position: absolute; left: 0; top: 0; transform-origin: 0 0; }
.edges { position: absolute; left: 0; top: 0; width: 1px; height: 1px; overflow: visible; pointer-events: none; }
.edge { fill: none; stroke: var(--muted); stroke-width: 2; opacity: .7; }
.edge.tmp { stroke: var(--accent); stroke-dasharray: 6 4; opacity: 1; }
.edge-hit { fill: none; stroke: transparent; stroke-width: 14; pointer-events: stroke; cursor: pointer; }
.edge-hit:hover + .edge { stroke: var(--danger); opacity: 1; }
.hint { position: absolute; left: 12px; bottom: 10px; color: var(--muted); font-size: 12px; background: var(--surface); padding: 4px 10px; border-radius: 6px; border: 1px solid var(--line); pointer-events: none; }

.node { position: absolute; width: 250px; background: var(--surface); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); cursor: default; user-select: none; }
.node.sel { outline: 2px solid var(--accent); outline-offset: 1px; }
.node-head { display: flex; align-items: center; gap: 6px; padding: 8px 12px; font-weight: 600; font-size: 13px; border-bottom: 1px solid var(--line); cursor: move; position: relative; border-top: 4px solid var(--nc); border-radius: 12px 12px 0 0; }
.node-head .hits { margin-left: auto; font-size: 11px; color: var(--muted); font-weight: 500; background: var(--surface-2); border-radius: 999px; padding: 0 7px; }
.in-dot { position: absolute; left: -7px; top: 14px; width: 12px; height: 12px; border-radius: 50%; background: var(--surface); border: 2px solid var(--nc); }
.node-body { padding: 8px 12px 10px; display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
.node .bubble { background: var(--bubble-bot); border-radius: 10px; padding: 6px 9px; white-space: pre-wrap; word-break: break-word; max-height: 92px; overflow: hidden; }
.node img { width: 100%; border-radius: 8px; display: block; max-height: 110px; object-fit: cover; }
.out-row { display: flex; align-items: center; justify-content: space-between; gap: 6px; position: relative; color: var(--muted); font-size: 12px; min-height: 22px; }
.out-row.btnrow { border: 1px solid var(--line); border-radius: 8px; padding: 3px 9px; color: var(--accent); font-weight: 600; justify-content: center; }
.port { position: absolute; right: -19px; width: 14px; height: 14px; border-radius: 50%; background: var(--surface); border: 2px solid var(--nc); cursor: crosshair; }
.out-row.btnrow .port { right: -20px; }
.port.on { background: var(--nc); }
.port:hover { transform: scale(1.3); }
.t-start { --nc: var(--c-start); } .t-message { --nc: var(--c-message); } .t-input { --nc: var(--c-input); }
.t-condition { --nc: var(--c-condition); } .t-action { --nc: var(--c-action); } .t-delay { --nc: var(--c-delay); }
.t-random { --nc: var(--c-random); } .t-goto { --nc: var(--c-goto); }

.panel { border-left: 1px solid var(--line); background: var(--surface); overflow: auto; padding: 14px 16px; }
.panel h2 { font-size: 16px; margin: 0 0 12px; }
.panel h3 { font-size: 13px; margin: 16px 0 8px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.item { border: 1px solid var(--line); border-radius: 10px; padding: 10px; margin-bottom: 8px; background: var(--surface-2); }
.item-head { display: flex; align-items: center; gap: 6px; margin-bottom: 6px; font-size: 12px; font-weight: 600; color: var(--muted); }
.item-head .x { margin-left: auto; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.create-menu { position: absolute; z-index: 5; background: var(--surface); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); padding: 6px; display: flex; flex-direction: column; min-width: 190px; }
.create-menu button { text-align: left; border: 0; background: none; padding: 6px 10px; border-radius: 6px; cursor: pointer; }
.create-menu button:hover { background: var(--surface-2); }

/* ---------- simulator ---------- */
.sim { display: grid; grid-template-columns: 240px minmax(320px, 420px) 1fr; gap: 16px; padding: 20px; height: 100vh; }
.sim-list { display: flex; flex-direction: column; gap: 6px; overflow: auto; }
.sim-contact { padding: 9px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--surface); cursor: pointer; }
.sim-contact.active { border-color: var(--accent); background: var(--accent-soft); }
.phone { display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--line); border-radius: 22px; box-shadow: var(--shadow); overflow: hidden; min-height: 0; }
.phone-head { padding: 12px 16px; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 10px; }
.avatar { width: 34px; height: 34px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); display: grid; place-items: center; font-weight: 700; }
.chat { flex: 1; overflow: auto; padding: 14px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 82%; padding: 8px 12px; border-radius: 16px; white-space: pre-wrap; word-break: break-word; }
.msg.bot { background: var(--bubble-bot); align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.user { background: var(--bubble-user); color: #fff; align-self: flex-end; border-bottom-right-radius: 4px; }
.msg.user.comment { background: var(--surface-2); color: var(--text); border: 1px dashed var(--line); }
.msg img { max-width: 100%; border-radius: 10px; display: block; }
.msg.img { padding: 4px; }
.qbtns { display: flex; flex-direction: column; gap: 4px; align-self: flex-start; width: 70%; }
.qbtn { border: 1px solid var(--line); background: var(--surface); color: var(--accent); border-radius: 10px; padding: 6px; cursor: pointer; font-weight: 600; }
.qbtn:hover { background: var(--accent-soft); }
.note { align-self: center; font-size: 11px; color: var(--muted); background: var(--surface-2); border-radius: 999px; padding: 2px 10px; text-align: center; max-width: 95%; }
.typing { display: flex; gap: 4px; }
.typing span { width: 7px; height: 7px; border-radius: 50%; background: var(--muted); animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: .2s; } .typing span:nth-child(3) { animation-delay: .4s; }
@keyframes blink { 0%, 80%, 100% { opacity: .25; } 40% { opacity: 1; } }
.composer { border-top: 1px solid var(--line); padding: 10px; display: flex; flex-direction: column; gap: 8px; }
.composer form { display: flex; gap: 6px; }
.composer input { flex: 1; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 4px 10px; cursor: pointer; font-size: 12px; }
.seg button.on { background: var(--accent); color: #fff; }
.sim-side { overflow: auto; }
.kv { display: grid; grid-template-columns: 120px 1fr; gap: 4px 10px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }

.server-only { display: none !important; }
body.server-mode .server-only { display: revert !important; }
body.server-mode .local-only { display: none !important; }
.app.locked .nav > a, .app.locked .nav-foot { visibility: hidden; }
.login { max-width: 380px; margin: 12vh auto; }
.status { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; }
.status::before { content: ""; width: 9px; height: 9px; border-radius: 50%; background: var(--muted); }
.status.on::before { background: var(--ok); }
.status.warn::before { background: #f59e0b; }
.copy { display: flex; gap: 6px; }
.copy input { flex: 1; font-family: ui-monospace, Consolas, monospace; font-size: 12px; }
.steps { padding-left: 20px; margin: 8px 0; }
.steps li { margin-bottom: 6px; }
.chan { display: inline-block; font-size: 11px; border-radius: 999px; padding: 0 7px; background: var(--surface-2); color: var(--muted); }
.chan.ig { background: #fde7f3; color: #b4237a; }
.chan.fb { background: #e7f0fe; color: #1659c7; }

.toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 50; }
.toast { background: var(--text); color: var(--bg); padding: 9px 14px; border-radius: 9px; box-shadow: var(--shadow); max-width: 340px; animation: pop .2s ease-out; }
@keyframes pop { from { transform: translateY(8px); opacity: 0; } }
.modal-back { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: grid; place-items: center; z-index: 40; padding: 16px; }
.modal-back[hidden] { display: none; }
.modal { background: var(--surface); border-radius: 14px; padding: 20px; width: min(520px, 100%); max-height: 90vh; overflow: auto; box-shadow: var(--shadow); }
.modal h2 { margin: 0 0 14px; font-size: 18px; }
.empty { padding: 30px; text-align: center; color: var(--muted); border: 1px dashed var(--line); border-radius: 12px; }

@media (max-width: 900px) {
  .app { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .nav { flex-direction: row; overflow-x: auto; border-right: 0; border-bottom: 1px solid var(--line); padding: 6px; }
  .brand, .nav-foot small { display: none; }
  .nav-foot { margin: 0; flex-direction: row; }
  .nav a { white-space: nowrap; }
  .page { padding: 16px; }
  .work { grid-template-columns: 1fr; grid-template-rows: 1fr 45%; }
  .panel { border-left: 0; border-top: 1px solid var(--line); }
  .builder, .sim { height: auto; min-height: calc(100vh - 50px); }
  .sim { grid-template-columns: 1fr; padding: 16px; }
  .phone { height: 70vh; }
}
