:root {
  --bg: #f6f6f4; --surface: #ffffff; --surface-2: #f0efec; --border: #e3e1dc; --text: #1d1c1a; --muted: #75716a;
  --accent: #c8643b; --accent-2: #f6e5dc; --ok: #2f8a4c; --warn: #b7791f; --danger: #c0392b;
  --user-bubble: #1d1c1a; --user-text: #fff; --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
  --radius: 12px; --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161514; --surface: #1f1e1c; --surface-2: #2a2926; --border: #34322f; --text: #ecebe8; --muted: #9a958d;
    --accent: #e07a4f; --accent-2: #3a2a22; --user-bubble: #ecebe8; --user-text: #161514; --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
code, pre { font-family: var(--mono); font-size: 13px; }
h2 { margin: 0; font-size: 22px; } h3 { margin: 0 0 12px; font-size: 16px; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* кнопки и поля */
.btn { border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 8px 14px; border-radius: 9px; font: inherit; font-weight: 500; cursor: pointer; 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.07); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn.sm { padding: 5px 10px; font-size: 13px; }
.btn:disabled { opacity: .5; cursor: default; }
input, select, textarea { font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border); border-radius: 9px; padding: 9px 11px; width: 100%; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: transparent; }
textarea { resize: vertical; }
label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--muted); margin-bottom: 12px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 12px; align-items: start; }
.err { color: var(--danger); font-size: 14px; min-height: 1em; }
.pill { font-size: 12px; padding: 3px 9px; border-radius: 99px; background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); margin-right: 6px; vertical-align: middle; }
.dot.ok { background: var(--ok); } .dot.bad { background: var(--danger); } .dot.busy { background: var(--accent); animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* вход */
.auth { position: fixed; inset: 0; display: grid; place-items: center; padding: 16px; }
.auth-card { width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 32px 28px; display: flex; flex-direction: column; gap: 12px; box-shadow: var(--shadow); text-align: center; }
.auth-card h1 { margin: 4px 0 0; font-size: 22px; }
.auth-card p { margin: 0 0 8px; }
.logo { width: 52px; height: 52px; border-radius: 14px; background: var(--accent); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 26px; margin: 0 auto; }
.logo.sm { width: 30px; height: 30px; border-radius: 8px; font-size: 16px; margin: 0; }

/* каркас */
.app { display: flex; height: 100vh; height: 100dvh; }
.nav { width: 200px; flex-shrink: 0; background: var(--surface); border-right: 1px solid var(--border); padding: 14px 10px; display: flex; flex-direction: column; gap: 2px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; padding: 4px 8px 16px; }
.nav a { display: flex; align-items: center; gap: 10px; padding: 9px 10px; border-radius: 9px; cursor: pointer; color: var(--muted); font-weight: 500; user-select: none; }
.nav a:hover { background: var(--surface-2); color: var(--text); }
.nav a.active { background: var(--accent-2); color: var(--accent); }
.nav-foot { margin-top: auto; font-size: 12px; color: var(--muted); padding: 8px; display: flex; flex-direction: column; gap: 4px; }
main { flex: 1; min-width: 0; overflow: hidden; }
.view { height: 100%; overflow: auto; }
.page-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; padding: 24px 28px 16px; flex-wrap: wrap; }
.page-head p { margin: 4px 0 0; font-size: 14px; }
.page-head select { width: auto; min-width: 180px; }

/* чат */
.chat-layout { display: flex; height: 100%; }
.chat-agents { width: 220px; flex-shrink: 0; border-right: 1px solid var(--border); padding: 12px 8px; overflow: auto; }
.chat-agents .item { padding: 9px 10px; border-radius: 9px; cursor: pointer; margin-bottom: 2px; }
.chat-agents .item:hover { background: var(--surface-2); }
.chat-agents .item.active { background: var(--surface); box-shadow: var(--shadow); border: 1px solid var(--border); }
.chat-agents .item b { font-weight: 600; }
.chat-agents .item small { display: block; color: var(--muted); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-agents .item .sub { padding-left: 12px; }
.chat-main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.chat-head { display: flex; align-items: center; gap: 12px; padding: 12px 20px; border-bottom: 1px solid var(--border); background: var(--surface); }
.chat-title { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chat-title span { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-tools { display: flex; gap: 8px; align-items: center; }
.messages { flex: 1; overflow-y: auto; padding: 20px; display: flex; flex-direction: column; gap: 14px; }
.msg { max-width: min(760px, 88%); }
.msg.user { align-self: flex-end; }
.msg .bubble { padding: 10px 14px; border-radius: 16px; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user-bubble); color: var(--user-text); border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.assistant .bubble { background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.assistant.error .bubble { border-color: var(--danger); }
.msg.system { align-self: center; font-size: 13px; color: var(--muted); text-align: center; max-width: 90%; }
.msg .meta { font-size: 11px; color: var(--muted); margin: 3px 6px; }
.msg.user .meta { text-align: right; }
.bubble p { margin: 0 0 8px; } .bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 4px 0 8px; padding-left: 22px; }
.bubble pre { background: var(--surface-2); padding: 10px 12px; border-radius: 8px; overflow-x: auto; margin: 6px 0; }
.bubble code { background: var(--surface-2); padding: 1px 5px; border-radius: 5px; }
.bubble pre code { background: none; padding: 0; }
.bubble h1, .bubble h2, .bubble h3, .bubble h4 { font-size: 15px; margin: 10px 0 4px; }
.bubble a { color: var(--accent); }
.bubble blockquote { margin: 4px 0; padding-left: 10px; border-left: 3px solid var(--border); color: var(--muted); }
.files { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; }
.files a { font-size: 13px; padding: 5px 10px; border: 1px solid var(--border); border-radius: 8px; color: var(--text); text-decoration: none; background: var(--surface-2); }
.files img { max-width: 320px; max-height: 260px; border-radius: 8px; display: block; border: 1px solid var(--border); }
.empty { margin: auto; text-align: center; color: var(--muted); }
.working { margin: 0 20px 8px; padding: 10px 14px; border: 1px dashed var(--accent); border-radius: 12px; background: var(--accent-2); font-size: 13px; }
.working-head { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.working-head button { margin-left: auto; }
.steps { color: var(--muted); font-family: var(--mono); font-size: 12px; margin-top: 4px; }
.steps div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.spinner { width: 14px; height: 14px; border: 2px solid var(--accent); border-top-color: transparent; border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.composer { display: flex; align-items: flex-end; gap: 8px; padding: 12px 20px 16px; }
.composer-body { flex: 1; background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 4px; }
.composer textarea { border: 0; resize: none; max-height: 220px; padding: 8px 10px; background: transparent; }
.composer textarea:focus { outline: none; }
.attach { cursor: pointer; padding: 10px; color: var(--muted); border-radius: 10px; }
.attach:hover { background: var(--surface-2); color: var(--text); }
.send { padding: 10px 12px; border-radius: 12px; }
.attachments { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 6px; }
.attachments span { font-size: 12px; background: var(--surface-2); padding: 3px 8px; border-radius: 6px; margin-top: 4px; }
.mobile-only { display: none; }

/* агенты */
.agents-layout { display: grid; grid-template-columns: 1fr 440px; gap: 20px; padding: 0 28px 28px; align-items: start; }
.org { overflow-x: auto; padding: 8px 0 24px; }
.tree, .tree ul { list-style: none; margin: 0; padding: 0; }
.tree ul { padding-left: 28px; position: relative; }
.tree li { position: relative; padding: 6px 0 6px 0; }
.tree ul > li::before { content: ""; position: absolute; left: -16px; top: 0; bottom: 0; border-left: 2px solid var(--border); }
.tree ul > li:last-child::before { bottom: auto; height: 32px; }
.tree ul > li::after { content: ""; position: absolute; left: -16px; top: 32px; width: 14px; border-top: 2px solid var(--border); }
.node { display: inline-flex; flex-direction: column; min-width: 240px; max-width: 360px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 14px; cursor: pointer; box-shadow: var(--shadow); }
.node:hover { border-color: var(--accent); }
.node.selected { outline: 2px solid var(--accent); }
.node.drop { background: var(--accent-2); border-color: var(--accent); }
.node.root { background: var(--surface-2); cursor: default; min-width: 160px; }
.node b { font-size: 15px; } .node small { color: var(--muted); }
.node .tags { display: flex; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; position: sticky; top: 12px; }
.panel textarea { font-family: var(--mono); font-size: 13px; min-height: 280px; }

/* память */
.mem-layout { display: grid; grid-template-columns: 300px 1fr; gap: 16px; padding: 0 28px 28px; height: calc(100% - 100px); min-height: 400px; }
.mem-files { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: auto; padding: 8px; }
.mem-files .grp { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); padding: 10px 8px 4px; }
.mem-files .f { padding: 6px 8px; border-radius: 7px; cursor: pointer; font-family: var(--mono); font-size: 13px; display: flex; justify-content: space-between; gap: 8px; }
.mem-files .f:hover { background: var(--surface-2); }
.mem-files .f.active { background: var(--accent-2); color: var(--accent); }
.mem-files .f small { color: var(--muted); }
.mem-editor { display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.mem-editor-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 12px; border-bottom: 1px solid var(--border); gap: 8px; }
.mem-editor textarea { flex: 1; border: 0; border-radius: 0; font-family: var(--mono); font-size: 13px; resize: none; padding: 14px; }

/* терминал */
.term { margin: 0 28px 28px; height: calc(100% - 120px); min-height: 360px; background: #111; border-radius: var(--radius); padding: 8px; overflow: hidden; }
.hint { margin: 0 28px 12px; padding: 10px 14px; border-radius: 10px; background: var(--accent-2); font-size: 14px; }

/* настройки */
.settings { padding: 0 28px 40px; max-width: 760px; display: flex; flex-direction: column; gap: 16px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px 20px; }
.card p { margin: 0 0 12px; font-size: 14px; }
.card textarea { font-family: var(--mono); font-size: 13px; }
.status-line { margin-bottom: 12px; font-size: 14px; }
.rejected { padding: 10px 12px; border-radius: 10px; background: var(--surface-2); font-size: 14px; display: flex; justify-content: space-between; align-items: center; gap: 8px; flex-wrap: wrap; }

dialog { border: 1px solid var(--border); border-radius: 16px; padding: 0; background: var(--surface); color: var(--text); width: min(640px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(0,0,0,.4); }
.dlg { padding: 22px; }
.dlg textarea { font-family: var(--mono); font-size: 13px; }

/* мобильная версия */
@media (max-width: 860px) {
  .app { flex-direction: column-reverse; }
  .nav { width: 100%; flex-direction: row; justify-content: space-around; padding: 4px 4px calc(4px + env(safe-area-inset-bottom)); border-right: 0; border-top: 1px solid var(--border); }
  .brand, .nav-foot { display: none; }
  .nav a { flex-direction: column; gap: 2px; font-size: 11px; padding: 6px 4px; flex: 1; }
  .chat-agents { display: none; }
  .mobile-only { display: block; width: auto; max-width: 45%; }
  .chat-title { display: none; }
  .chat-head { padding: 8px 12px; } .messages { padding: 12px; } .composer { padding: 8px 12px 10px; } .working { margin: 0 12px 8px; }
  .msg { max-width: 94%; }
  .page-head { padding: 16px; }
  .agents-layout, .mem-layout { grid-template-columns: 1fr; padding: 0 16px 16px; height: auto; }
  .mem-files { max-height: 220px; } .mem-editor textarea { min-height: 50vh; }
  .panel { position: static; }
  .term { margin: 0 8px 8px; height: calc(100% - 150px); }
  .hint { margin: 0 16px 12px; }
  .settings { padding: 0 16px 24px; }
  .grid2 { grid-template-columns: 1fr; }
  .node { min-width: 0; max-width: 100%; }
}
a { color: var(--accent); }

/* интеграции */
.steps-list { margin: 0 0 14px; padding-left: 20px; font-size: 14px; color: var(--muted); }
.steps-list li { margin-bottom: 3px; }
.checks { display: flex; flex-wrap: wrap; gap: 6px 16px; margin: 4px 0 12px; }
.checks label, label.inline { flex-direction: row; align-items: center; gap: 8px; color: var(--text); font-size: 14px; margin: 0; }
.checks input, label.inline input { width: auto; }
.chatlist { max-height: 320px; overflow: auto; border: 1px solid var(--border); border-radius: 10px; margin: 8px 0 12px; }
.chatlist label { flex-direction: row; align-items: center; gap: 10px; padding: 7px 12px; margin: 0; color: var(--text); font-size: 14px; border-bottom: 1px solid var(--border); cursor: pointer; }
.chatlist label:last-child { border-bottom: 0; }
.chatlist label:hover { background: var(--surface-2); }
.chatlist input { width: auto; }
.chatlist small { color: var(--muted); margin-left: auto; white-space: nowrap; }
.sub-h { font-size: 13px; font-weight: 600; margin: 14px 0 6px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.card .head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 8px; }
.card .head h3 { margin: 0; }

/* голос */
.mic { border: 0; background: transparent; color: var(--muted); padding: 10px; border-radius: 12px; cursor: pointer; }
.mic:hover { background: var(--surface-2); color: var(--text); }
.mic.rec { background: var(--danger); color: #fff; animation: pulse 1.2s infinite; }
.mic.busy { opacity: .5; cursor: wait; }
.rec-bar { display: flex; align-items: center; gap: 10px; padding: 8px 10px; font-variant-numeric: tabular-nums; }
.rec-dot { width: 10px; height: 10px; border-radius: 50%; background: var(--danger); animation: pulse 1s infinite; }
.meter { flex: 1; height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.meter span { display: block; height: 100%; background: var(--accent); transform-origin: left; transform: scaleX(0); transition: transform .1s; }
.rec-status:not(:empty) { display: block; padding: 8px 10px 0; font-size: 13px; }
.voice-notice { margin: 0 20px 6px; padding: 8px 12px; border-radius: 10px; background: var(--surface-2); font-size: 13px; }
.handsfree.on { background: var(--danger); border-color: var(--danger); color: #fff; }
.speak-stop { color: var(--accent); border-color: var(--accent); }
.autospeak { font-size: 15px; cursor: pointer; }
.msg .play { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 11px; padding: 0 4px; }
.msg .play:hover { color: var(--accent); }
@media (max-width: 860px) { .voice-notice { margin: 0 12px 6px; } .chat-tools { gap: 4px; } .chat-tools .pill { display: none; } }
.srv { border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px 4px; margin-bottom: 10px; }
.srv .row { margin-bottom: 10px; }
.srv-msg { font-size: 13px; }

/* сервисы: плитки */
.int-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px; padding: 0 28px 28px; max-width: 1200px; }
.int-tile { background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 14px 16px; display: flex; flex-direction: column; gap: 6px; cursor: pointer; transition: border-color .15s, box-shadow .15s; min-width: 0; }
.int-tile:hover { border-color: var(--accent); box-shadow: var(--shadow); }
.int-tile-top { display: flex; align-items: center; gap: 10px; margin-bottom: 4px; }
.int-tile-top .dot { margin: 0 0 0 auto; }
.int-icon { width: 36px; height: 36px; border-radius: 10px; display: grid; place-items: center; background: color-mix(in srgb, var(--c) 16%, transparent); color: var(--c); flex-shrink: 0; }
.int-name { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.int-name small { color: var(--muted); font-size: 12px; }
.int-line { font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.int-key { font-size: 12px; color: var(--muted); background: var(--surface-2); padding: 3px 7px; border-radius: 6px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; align-self: flex-start; max-width: 100%; }
.int-tile .btn { margin-top: auto; align-self: flex-start; }
.int-tile .btn:first-of-type { margin-top: 8px; }
.detail-head { padding-bottom: 4px; }
.detail-body { max-width: 900px; }
.key-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; font-size: 14px; margin: 0 0 12px; }
.key-row code { background: var(--surface-2); padding: 3px 8px; border-radius: 6px; font-size: 13px; }
@media (max-width: 860px) {
  .int-grid { padding: 0 12px 16px; grid-template-columns: 1fr 1fr; gap: 8px; }
  .int-tile { padding: 10px; gap: 4px; }
  .int-icon { width: 30px; height: 30px; border-radius: 8px; } .int-icon svg { width: 17px; height: 17px; }
  .int-tile-top { gap: 8px; margin-bottom: 2px; } .int-name b { font-size: 14px; } .int-name small { font-size: 11px; }
  .int-line { font-size: 12px; } .int-key { font-size: 11px; padding: 2px 5px; }
  .int-tile .btn { padding: 4px 9px; font-size: 12px; } .int-tile .btn:first-of-type { margin-top: 4px; }
}
@media (max-width: 300px) { .int-grid { grid-template-columns: 1fr; } }
