:root {
  --bg: #f4f5f2; --surface: #fff; --surface-2: #eef0ec; --line: #dde0d9; --text: #1c211e; --muted: #5e675f;
  --accent: #0d6b57; --accent-ink: #fff; --accent-soft: #dcefe8; --danger: #b3261e; --warn: #8a5a00; --warn-soft: #fbefd5;
  --bubble-out: #dcefe8; --bubble-in: #fff; --shadow: 0 1px 2px rgb(0 0 0 / .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131614; --surface: #1c201d; --surface-2: #252a26; --line: #333a35; --text: #e7eae6; --muted: #9ca69e;
    --accent: #4fc3a1; --accent-ink: #06231c; --accent-soft: #1d3a31; --danger: #f2877f; --warn: #e9b949; --warn-soft: #3b2f12;
    --bubble-out: #1d3a31; --bubble-in: #252a26; --shadow: none;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 20px; } h2 { font-size: 15px; } h3 { font-size: 13px; }
button, input, select, textarea { font: inherit; color: inherit; }
input, select, textarea {
  width: 100%; padding: 7px 9px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface);
}
textarea { resize: vertical; min-height: 70px; }
input[type=checkbox] { width: auto; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
label { display: block; font-size: 12px; color: var(--muted); }
label > input, label > select, label > textarea { margin-top: 3px; color: var(--text); font-size: 14px; }
label.check { display: flex; gap: 8px; align-items: center; color: var(--text); font-size: 14px; }
button { padding: 7px 12px; border: 1px solid var(--line); border-radius: 6px; background: var(--surface); cursor: pointer; }
button:hover { border-color: var(--muted); }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
button.danger { color: var(--danger); }
button:disabled { opacity: .5; cursor: default; }
a { color: var(--accent); }
.muted { color: var(--muted); } .small { font-size: 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; min-width: 0; }
.stack { display: grid; gap: 12px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 16px; box-shadow: var(--shadow); }
.badge { display: inline-block; padding: 1px 7px; border-radius: 99px; background: var(--surface-2); font-size: 11px; color: var(--muted); }
.badge.bot { background: var(--accent-soft); color: var(--accent); }
.badge.human { background: var(--warn-soft); color: var(--warn); }

.shell { display: grid; grid-template-columns: 200px 1fr; height: 100vh; }
.nav { display: flex; flex-direction: column; gap: 2px; padding: 14px 10px; background: var(--surface); border-right: 1px solid var(--line); }
.brand { font-weight: 700; font-size: 16px; padding: 4px 10px 12px; }
.nav button { text-align: left; border: 0; background: none; padding: 8px 10px; }
.nav button[aria-current=page] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav .foot { margin-top: auto; padding: 10px; font-size: 12px; color: var(--muted); }
.main { overflow: auto; min-width: 0; }
.page { padding: 20px 24px; display: grid; gap: 16px; max-width: 1200px; }
.page-head { display: flex; gap: 12px; align-items: center; justify-content: space-between; flex-wrap: wrap; }

.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login form { width: min(360px, 100%); }

.inbox { display: grid; grid-template-columns: 280px 1fr 280px; height: 100vh; }
.inbox-list { border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; background: var(--surface); }
.inbox-tools { padding: 10px; display: grid; gap: 8px; border-bottom: 1px solid var(--line); }
.inbox-items { overflow: auto; flex: 1; }
.conv { display: block; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line); border-radius: 0; background: none; padding: 10px 12px; }
.conv[aria-current=true] { background: var(--accent-soft); }
.conv .preview { color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.chat-head { padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--surface); }
.back { display: none; }
.chat-log { flex: 1; overflow: auto; padding: 16px; display: flex; flex-direction: column; gap: 6px; }
.msg { max-width: 75%; padding: 7px 10px; border-radius: 8px; white-space: pre-wrap; overflow-wrap: anywhere; box-shadow: var(--shadow); }
.msg.in { align-self: flex-start; background: var(--bubble-in); border: 1px solid var(--line); }
.msg.out { align-self: flex-end; background: var(--bubble-out); }
.msg.note { align-self: center; background: var(--warn-soft); color: var(--warn); font-size: 12px; box-shadow: none; }
.msg .meta { display: block; font-size: 11px; color: var(--muted); margin-top: 2px; }
.msg.failed { outline: 1px solid var(--danger); }
.composer { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--line); background: var(--surface); align-items: flex-end; }
.composer textarea { min-height: 38px; height: 38px; flex: 1; min-width: 110px; }
.composer button { flex: none; white-space: nowrap; }
.composer select { flex: 0 1 130px; min-width: 90px; height: 38px; }
.side { border-left: 1px solid var(--line); padding: 14px; overflow: auto; background: var(--surface); display: grid; gap: 12px; align-content: start; }
.empty { display: grid; place-items: center; height: 100%; color: var(--muted); padding: 24px; text-align: center; }

.kanban { display: grid; grid-auto-flow: column; grid-auto-columns: minmax(220px, 1fr); gap: 12px; overflow-x: auto; padding-bottom: 8px; }
.column { background: var(--surface-2); border-radius: 8px; padding: 10px; display: flex; flex-direction: column; gap: 8px; min-height: 60vh; }
.column.over { outline: 2px dashed var(--accent); }
.deal { background: var(--surface); border: 1px solid var(--line); border-radius: 6px; padding: 9px; cursor: grab; box-shadow: var(--shadow); width: 100%; text-align: left; }

table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; }
th, td { padding: 8px 10px; border-bottom: 1px solid var(--line); vertical-align: top; }
.table-wrap { overflow-x: auto; }

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
.kpi b { display: block; font-size: 26px; line-height: 1.2; font-variant-numeric: tabular-nums; }
.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 12px; }
.hbar { display: grid; grid-template-columns: 110px 1fr 36px; gap: 8px; align-items: center; padding: 3px 0; }
.hbar .track { height: 12px; }
.hbar .fill { height: 100%; min-width: 2px; background: var(--accent); border-radius: 0 4px 4px 0; }
.columns-chart { display: flex; align-items: flex-end; gap: 2px; height: 150px; border-bottom: 1px solid var(--line); }
.columns-chart .col { flex: 1; height: 100%; display: flex; align-items: flex-end; min-width: 2px; }
.columns-chart .col:hover .bar { opacity: .75; }
.columns-chart .bar { width: 100%; background: var(--accent); border-radius: 4px 4px 0 0; min-height: 1px; }
#chart-tip { position: fixed; z-index: 30; pointer-events: none; background: var(--text); color: var(--bg); font-size: 12px; padding: 3px 7px; border-radius: 4px; display: none; }

.builder { display: grid; grid-template-columns: 1fr 300px; height: calc(100vh - 130px); border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--surface); }
.canvas-wrap { overflow: auto; position: relative; user-select: none; background-image: radial-gradient(var(--line) 1px, transparent 1px); background-size: 18px 18px; touch-action: none; }
.canvas { position: relative; width: 2400px; height: 1400px; }
.canvas svg { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.canvas path { fill: none; stroke: var(--muted); stroke-width: 2; }
.canvas path.draft { stroke: var(--accent); stroke-dasharray: 5 4; }
.node { position: absolute; width: 200px; background: var(--surface); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 1px 3px rgb(0 0 0 / .1); cursor: move; user-select: none; }
.node.selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.node.drop { box-shadow: 0 0 0 2px var(--accent); }
.node-head { padding: 6px 10px; font-weight: 600; font-size: 12px; border-bottom: 1px solid var(--line); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.node-kind { font-weight: 400; color: var(--muted); font-size: 11px; display: block; }
.node-body { padding: 6px 10px; font-size: 12px; color: var(--muted); max-height: 48px; overflow: hidden; }
.port-row { display: flex; justify-content: flex-end; align-items: center; gap: 6px; padding: 3px 0 3px 10px; font-size: 11px; color: var(--muted); position: relative; min-height: 22px; }
.port { width: 14px; height: 14px; border-radius: 50%; border: 2px solid var(--accent); background: var(--surface); margin-right: -7px; cursor: crosshair; touch-action: none; flex: none; padding: 0; }
.port.linked { background: var(--accent); }
.inspector { border-left: 1px solid var(--line); padding: 14px; overflow: auto; display: grid; gap: 10px; align-content: start; }
.option-box { border: 1px solid var(--line); border-radius: 6px; padding: 8px; display: grid; gap: 6px; }
.problems { border-left: 3px solid var(--warn); background: var(--warn-soft); padding: 8px 12px; border-radius: 0 6px 6px 0; }
.problems ul { margin: 4px 0 0; padding-left: 18px; }

#toast { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); z-index: 40; }
#toast div { background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 6px; margin-top: 6px; max-width: 90vw; }

@media (max-width: 1000px) {
  .inbox { grid-template-columns: 240px 1fr; }
  .side { display: none; }
  .builder { grid-template-columns: 1fr; height: auto; }
  .canvas-wrap { height: 55vh; }
  .inspector { border-left: 0; border-top: 1px solid var(--line); }
}
@media (max-width: 680px) {
  .shell { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .nav { flex-direction: row; overflow-x: auto; padding: 6px; border-right: 0; border-bottom: 1px solid var(--line); }
  .nav .brand, .nav .foot { display: none; }
  .back { display: inline-block; }
  .nav button { white-space: nowrap; }
  .page { padding: 16px; }
  .inbox { grid-template-columns: 1fr; height: auto; min-height: calc(100vh - 50px); }
  .inbox.has-chat .inbox-list { display: none; }
  .inbox:not(.has-chat) .chat { display: none; }
  .chat { height: calc(100vh - 50px); }
}
