:root {
  --bg: #12131a; --panel: #1b1c27; --panel2: #16171f; --border: #2b2d42; --border2: #3b3d55;
  --text: #e2e8f0; --dim: #94a3b8; --faint: #64748b; --accent: #a78bfa; --accent-dark: #7c3aed;
  --green: #22c55e; --green-bg: #152a1f; --green-fg: #86efac; --gold: #f59e0b; --gold-fg: #fcd34d; --gold-bg: #3a2a10;
  --red: #f87171; --red-bg: #2a1620; --red-fg: #fca5a5; --sky: #38bdf8; --mono: "JetBrains Mono", Consolas, "Cascadia Mono", monospace;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 13px/1.4 Inter, "Segoe UI", system-ui, sans-serif; }
.hidden { display: none !important; }
a { color: #c4b5fd; }
button, input, select, textarea { font: inherit; color: inherit; }
.btn { background: #12131a; color: var(--text); border: 1px solid var(--border2); border-radius: 8px; padding: 7px 10px; cursor: pointer; text-align: left; }
.btn:hover { background: var(--border); }
.btn.primary { background: var(--accent); color: var(--bg); border-color: var(--accent); font-weight: 700; text-align: center; }
.btn.primary:hover { background: #c4b5fd; }
.btn.small { padding: 4px 8px; font-size: 12px; }
.btn.ghost { background: transparent; color: var(--dim); }
.btn.danger { color: var(--red-fg); background: var(--red-bg); border-color: #5a2a3a; }
.btn.gold { color: var(--gold-fg); }
.btn.blue { background: #3b82f6; border-color: #3b82f6; color: #fff; font-weight: 600; }
.btn.dashed { border-style: dashed; color: var(--dim); background: transparent; margin: 8px 12px 0; }
.btn.link { background: none; border: none; color: #c4b5fd; padding: 6px 0 0; font-weight: 600; }
.btn:disabled { opacity: .5; cursor: default; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row.right { justify-content: flex-end; margin-top: 12px; }
.muted { color: var(--dim); } .small { font-size: 12px; } .mono { font-family: var(--mono); }
.pill { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; border: 1px solid var(--border); white-space: nowrap; }
.pill.ok { background: var(--green-bg); color: var(--green-fg); border-color: #1f5a3a; }
.pill.warn { background: var(--gold-bg); color: var(--gold-fg); border-color: #7a5a1a; }
.pill.bad { background: var(--red-bg); color: var(--red-fg); border-color: #5a2a3a; }
.pill.accent { background: #2a1f3d; color: #c4b5fd; border-color: #4c3a7a; }
.pill.dim { background: #1e1b2e; color: var(--dim); }

/* layout */
.app { display: flex; flex-direction: column; height: 100vh; }
.topbar { display: flex; align-items: center; gap: 12px; padding: 8px 14px; background: var(--panel); border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.brand { display: flex; align-items: center; gap: 8px; }
.brand .mark { display: inline-flex; align-items: center; justify-content: center; width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: var(--bg); font-weight: 800; }
.brand .name { font-weight: 700; font-size: 14px; } .brand .sub { color: var(--dim); font-size: 12px; }
.nav { display: flex; gap: 2px; margin-left: 12px; }
.navbtn { background: transparent; border: none; color: var(--dim); font-weight: 600; padding: 6px 12px; border-radius: 6px; cursor: pointer; }
.navbtn.active, .navbtn:hover { background: var(--border); color: var(--text); }
.spacer { flex: 1; }
.view { flex: 1; min-height: 0; display: flex; }
.sidebar { width: 240px; flex: 0 0 240px; background: var(--panel2); border-right: 1px solid var(--border); display: flex; flex-direction: column; overflow: auto; padding-bottom: 10px; }
.side-title { font-size: 11px; font-weight: 700; letter-spacing: .08em; color: var(--dim); padding: 12px 14px 4px; }
.net-list { display: flex; flex-direction: column; padding: 0 8px; }
.net-head { display: flex; align-items: center; gap: 8px; padding: 8px 8px; cursor: pointer; border-radius: 8px; }
.net-head:hover { background: var(--border); }
.net-head .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); }
.net-head .dot.on { background: var(--green); } .net-head .dot.retry { background: var(--gold); }
.net-head .nm { font-weight: 600; flex: 1; } .net-head .tag { font-family: var(--mono); font-size: 11px; color: var(--dim); }
.net-head .tag.oper { color: var(--gold); }
.row-btn { display: flex; align-items: center; gap: 8px; padding: 7px 10px; border-radius: 6px; background: transparent; border: none; color: #cbd5e1; cursor: pointer; text-align: left; width: 100%; }
.row-btn.sub { margin-left: 8px; width: calc(100% - 8px); }
.row-btn:hover { background: var(--border); }
.row-btn.active { background: var(--border); color: var(--text); font-weight: 600; }
.row-btn.dim { color: var(--dim); font-style: italic; }
.row-btn .badge { margin-left: auto; min-width: 18px; text-align: center; font-size: 11px; font-weight: 700; color: var(--bg); background: var(--accent); border-radius: 999px; padding: 1px 6px; }
.row-btn .badge.hl { background: var(--gold); }
.row-btn .count { margin-left: auto; font-size: 11px; color: var(--dim); font-family: var(--mono); }
.persona-card { margin: auto 10px 0; padding: 10px; background: var(--panel); border: 1px solid var(--border); border-radius: 10px; }
.persona-card .side-title { padding: 0 0 6px; }
select, input[type=text], input:not([type]), input[type=password], input[type=number], input[type=email], textarea { width: 100%; background: #12131a; color: var(--text); border: 1px solid var(--border2); border-radius: 8px; padding: 7px 10px; }
textarea { font-family: var(--mono); min-height: 120px; resize: vertical; }

/* chat */
.chat { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.chat-header { display: flex; align-items: center; gap: 10px; padding: 8px 14px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.chat-title { font-weight: 700; font-size: 15px; }
.chat-sub { flex: 1; min-width: 120px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-size: 12px; }
.chat-actions { display: flex; gap: 4px; }
.chat-log { flex: 1; overflow: auto; padding: 12px 16px; font-family: var(--mono); font-size: 12.5px; background: #0d0e14; }
.line { display: flex; gap: 10px; padding: 1px 0; }
.line .ts { color: var(--faint); flex: none; width: 44px; }
.line .body { flex: 1; min-width: 0; white-space: pre-wrap; word-break: break-word; }
.line.event .body { color: var(--dim); font-style: italic; }
.line.mine .nick { color: #c084fc; } .line.owner .nick { color: var(--sky); } .line.admin .nick { color: #34d399; }
.line.services .body { color: var(--gold-fg); }
.line .nick { font-weight: 600; } .line .tag { color: var(--dim); font-size: 11px; margin-right: 4px; font-family: Inter, system-ui, sans-serif; }
.line .engine { color: var(--dim); font-size: 11px; margin-right: 4px; }
.divider { display: flex; align-items: center; gap: 10px; color: var(--faint); font-family: Inter, system-ui, sans-serif; font-size: 11px; margin: 6px 0; }
.divider::before, .divider::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.composer { display: flex; gap: 8px; padding: 10px 14px; border-top: 1px solid var(--border); background: var(--panel); flex-wrap: wrap; }
.composer #composer-target { width: 150px; flex: none; }
.composer #composer-text { flex: 1 1 240px; min-width: 0; }

/* inspector */
.inspector { width: 300px; flex: 0 0 300px; border-left: 1px solid var(--border); background: var(--panel2); overflow: auto; padding: 10px; display: flex; flex-direction: column; gap: 10px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 10px; }
.card-title { font-size: 11px; font-weight: 700; letter-spacing: .08em; color: var(--dim); margin-bottom: 8px; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; font-size: 12px; }
.kv .k { color: var(--dim); } .kv .v { font-family: var(--mono); overflow-wrap: anywhere; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
.grid2 .btn { font-size: 12px; padding: 7px 8px; }
.statusbar { display: flex; gap: 16px; padding: 4px 14px; background: var(--panel); border-top: 1px solid var(--border); font-family: var(--mono); font-size: 11px; color: var(--dim); white-space: nowrap; overflow: hidden; }
.statusbar #footer-left { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.statusbar .err { color: var(--red); } .statusbar .warn { color: var(--gold-fg); }

/* settings & memory */
.settings-main { flex: 1; min-width: 0; overflow: auto; padding: 20px 24px; }
.settings-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; flex-wrap: wrap; margin-bottom: 16px; }
.settings-head h1 { margin: 0; font-size: 20px; }
.cfg-form { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.cfg-form.single { grid-template-columns: 1fr; }
@media (max-width: 1000px) { .cfg-form { grid-template-columns: 1fr; } }
.field { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; display: flex; flex-direction: column; gap: 4px; }
.field.wide { grid-column: 1 / -1; }
.field label { font-weight: 600; font-size: 12px; } .field .help { color: var(--dim); font-size: 11px; }
.field.check { flex-direction: row; align-items: center; gap: 10px; }
.field.check input { width: auto; }
.field .secret { display: flex; gap: 6px; } .field .secret input { flex: 1; }
.section-note { grid-column: 1 / -1; padding: 10px 12px; background: var(--green-bg); border: 1px solid #1f5a3a; color: var(--green-fg); border-radius: 8px; font-size: 12px; }
.net-tools { grid-column: 1 / -1; display: flex; gap: 8px; flex-wrap: wrap; }
table.tbl { width: 100%; border-collapse: collapse; font-size: 12px; }
table.tbl th, table.tbl td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
table.tbl th { color: var(--dim); font-weight: 600; }
.article { background: var(--panel); border: 1px solid var(--border); border-radius: 10px; padding: 12px; margin-bottom: 10px; }
.article h3 { margin: 0 0 6px; font-size: 13px; font-family: var(--mono); }
pre.box { background: #0d0e14; border: 1px solid var(--border); border-radius: 8px; padding: 12px; white-space: pre-wrap; word-break: break-word; font-family: var(--mono); font-size: 12px; max-height: 70vh; overflow: auto; }

/* log */
.log { flex-direction: column; }
.log-toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; padding: 8px 14px; background: var(--panel); border-bottom: 1px solid var(--border); font-size: 12px; }
.log-toolbar select, .log-toolbar input[type=text], .log-toolbar input:not([type]) { width: auto; padding: 4px 8px; }
.log-body { flex: 1; margin: 0; overflow: auto; padding: 10px 14px; font-family: var(--mono); font-size: 11.5px; background: #08090f; white-space: pre-wrap; word-break: break-word; }
.log-body .DEBUG { color: var(--faint); } .log-body .INFO { color: var(--sky); } .log-body .WARNING { color: var(--gold-fg); } .log-body .ERROR, .log-body .CRITICAL { color: var(--red); font-weight: 700; }

/* login */
.login { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: var(--bg); z-index: 50; }
.login-card { width: min(360px, 92vw); background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 24px; display: flex; flex-direction: column; gap: 8px; }
.login-card .brand { margin-bottom: 8px; }
.login-card label { font-size: 12px; color: var(--dim); margin-top: 6px; }
.error { color: var(--red-fg); font-size: 12px; min-height: 16px; }

/* modal / toasts */
.modal { position: fixed; inset: 0; background: rgba(0,0,0,.55); display: flex; align-items: center; justify-content: center; z-index: 40; padding: 16px; }
.modal-card { width: min(520px, 100%); background: var(--panel); border: 1px solid var(--border2); border-radius: 12px; padding: 18px; max-height: 90vh; overflow: auto; }
.modal-card h2 { margin: 0 0 12px; font-size: 16px; }
.modal-card label { display: block; font-size: 12px; color: var(--dim); margin: 8px 0 4px; }
.modal-card .note { font-size: 12px; color: var(--dim); margin-top: 6px; }
.toasts { position: fixed; right: 14px; bottom: 40px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { background: var(--panel); border: 1px solid var(--border2); border-left: 3px solid var(--accent); border-radius: 8px; padding: 10px 12px; max-width: 420px; font-size: 12px; box-shadow: 0 6px 20px rgba(0,0,0,.4); }
.toast.err { border-left-color: var(--red); } .toast.ok { border-left-color: var(--green); }

.mobile-only { display: none; }
.bottombar { display: none; }
.drawer-backdrop { position: fixed; inset: 0; background: rgba(0,0,0,.5); z-index: 29; }

/* ---------- phones and small tablets ---------- */
@media (max-width: 820px) {
  .app { height: 100dvh; }
  .mobile-only { display: inline-flex; align-items: center; justify-content: center; min-width: 44px; min-height: 40px; font-size: 18px; }
  .topbar { padding: 6px 10px; gap: 8px; flex-wrap: nowrap; }
  .topbar .nav, .topbar #pill-engine, .topbar #pill-oper, .brand .sub { display: none; }
  .topbar #pill-online { font-size: 11px; padding: 3px 8px; }
  .topbar #btn-logout { padding: 4px 8px; }
  .bottombar { display: flex; position: sticky; bottom: 0; z-index: 20; background: var(--panel); border-top: 1px solid var(--border); padding: 4px 4px calc(4px + env(safe-area-inset-bottom)); }
  .bottombar .navbtn { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 2px; padding: 6px 2px; font-size: 11px; border-radius: 8px; min-height: 48px; }
  .bottombar .navbtn .ico { font-size: 18px; line-height: 1; }
  .statusbar { display: none; }

  /* console: chat fills the screen, sidebar and inspector are drawers */
  .view.console { position: relative; }
  .drawer { position: fixed; top: 0; bottom: 0; width: min(86vw, 340px); z-index: 30; transform: translateX(-100%); transition: transform .2s ease; box-shadow: 0 0 30px rgba(0,0,0,.5); max-height: none; padding-bottom: calc(12px + env(safe-area-inset-bottom)); }
  .drawer-left { left: 0; border-right: 1px solid var(--border2); }
  .drawer-right { right: 0; transform: translateX(100%); border-left: 1px solid var(--border2); }
  .drawer.open { transform: translateX(0); }
  .persona-card { margin-top: 16px; }
  .chat-header { padding: 6px 8px; gap: 6px; }
  #btn-inspector { margin-left: auto; }
  .chat-header .chat-actions { order: 5; width: 100%; justify-content: flex-start; overflow-x: auto; }
  .chat-header .chat-actions .btn { min-height: 36px; white-space: nowrap; }
  .chat-sub { display: none; }
  .chat-log { padding: 8px 10px; font-size: 13px; }
  .line .ts { width: 38px; }
  .composer { padding: 8px 8px calc(8px + env(safe-area-inset-bottom)); gap: 6px; }
  .composer #composer-target { width: 110px; }
  .composer #composer-text { flex: 1 1 100%; order: -1; min-height: 44px; font-size: 16px; }
  .composer .btn { min-height: 44px; }
  .btn, .row-btn { min-height: 40px; }
  .grid2 .btn { min-height: 44px; }

  /* settings & memory: sidebar becomes a scrolling chip row */
  .view.settings, .view.memory { flex-direction: column; }
  .view.settings .sidebar, .view.memory .sidebar { width: auto; flex: none; flex-direction: row; align-items: center; gap: 6px; overflow-x: auto; overflow-y: hidden; padding: 8px; border-right: none; border-bottom: 1px solid var(--border); max-height: none; }
  .view.settings .side-title, .view.memory .side-title { display: none; }
  .view.settings .net-list, .view.memory .net-list { flex-direction: row; gap: 6px; padding: 0; }
  .view.settings .row-btn, .view.memory .row-btn { white-space: nowrap; width: auto; border: 1px solid var(--border2); border-radius: 999px; padding: 8px 12px; }
  .view.settings .row-btn .count, .view.memory .row-btn .count { margin-left: 6px; }
  .view.settings .btn.dashed { margin: 0; white-space: nowrap; }
  .view.memory select, .view.memory .btn { width: auto; white-space: nowrap; }
  .settings-main { padding: 12px; }
  .settings-head { margin-bottom: 10px; }
  .settings-head h1 { font-size: 17px; }
  .cfg-form { grid-template-columns: 1fr; gap: 10px; }
  .field input, .field textarea, .field select { font-size: 16px; }
  table.tbl { display: block; overflow-x: auto; }

  /* log */
  .log-toolbar { gap: 6px; padding: 6px 8px; }
  .log-toolbar input:not([type]) { flex: 1 1 120px; }
  .log-body { font-size: 11px; padding: 8px; }

  /* dialogs become bottom sheets */
  .modal { align-items: flex-end; padding: 0; }
  .modal-card { width: 100%; border-radius: 14px 14px 0 0; max-height: 92vh; padding: 16px 16px calc(16px + env(safe-area-inset-bottom)); }
  .modal-card input, .modal-card select, .modal-card textarea { font-size: 16px; min-height: 44px; }
  .modal-card .row.right .btn { flex: 1; text-align: center; min-height: 44px; }
  .toasts { left: 10px; right: 10px; bottom: 70px; }
  .toast { max-width: none; }
  .login-card { padding: 20px; }
}
