:root {
  --bg: #f0f2f5;
  --panel: #ffffff;
  --panel-2: #f7f9fa;
  --text: #111b21;
  --text-soft: #667781;
  --border: #e2e8ec;
  --brand: #25d366;
  --brand-dark: #128c7e;
  --teal: #075e54;
  --accent: #34b7f1;
  --danger: #ef4444;
  --amber: #f59e0b;
  --chat-bg: #efeae2;
  --bubble-out: #d9fdd3;
  --bubble-in: #ffffff;
  --shadow: 0 1px 3px rgba(11,20,26,.08), 0 1px 2px rgba(11,20,26,.06);
  --radius: 12px;
}
:root[data-theme="dark"] {
  --bg: #0b141a;
  --panel: #111b21;
  --panel-2: #1a252c;
  --text: #e9edef;
  --text-soft: #8696a0;
  --border: #22333b;
  --chat-bg: #0b141a;
  --bubble-out: #005c4b;
  --bubble-in: #202c33;
  --shadow: 0 1px 3px rgba(0,0,0,.4);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--text); font-size: 14px; -webkit-font-smoothing: antialiased;
}
#app { display: grid; grid-template-columns: 248px 1fr; height: 100vh; }

/* Sidebar */
.sidebar { background: var(--teal); color: #fff; display: flex; flex-direction: column; padding: 16px 12px; }
:root[data-theme="dark"] .sidebar { background: #111b21; border-right: 1px solid var(--border); }
.brand { display: flex; align-items: center; gap: 10px; padding: 8px 8px 18px; }
.brand-logo { width: 34px; height: 34px; background: #fff; border-radius: 9px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.brand-logo svg { width: 26px; height: 26px; display: block; }
.brand-name { font-weight: 700; font-size: 15px; }
.brand-sub { font-size: 11px; opacity: .7; }
.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav a {
  display: flex; align-items: center; gap: 12px; padding: 11px 14px; border-radius: 10px;
  color: #e9edef; text-decoration: none; font-weight: 500; opacity: .85; transition: .15s;
}
.nav a .ico { width: 21px; height: 21px; fill: currentColor; flex-shrink: 0; opacity: .95; }
.nav a:hover { background: rgba(255,255,255,.08); opacity: 1; }
.nav a.active { background: var(--brand); color: #06231a; opacity: 1; box-shadow: var(--shadow); }
:root[data-theme="dark"] .nav a.active { color: #fff; background: var(--brand-dark); }
.sidebar-foot { display: flex; align-items: center; gap: 8px; padding-top: 12px; border-top: 1px solid rgba(255,255,255,.12); }
.sender-badge { flex: 1; font-size: 12px; background: rgba(255,255,255,.1); padding: 8px 10px; border-radius: 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.theme-toggle { background: rgba(255,255,255,.12); border: 0; color: #fff; width: 34px; height: 34px; border-radius: 8px; cursor: pointer; font-size: 15px; }

/* Main */
.main { overflow: hidden; display: flex; flex-direction: column; }
.view { overflow-y: auto; padding: 24px 28px; height: 100%; }
.view.no-pad { padding: 0; }

.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; gap: 12px; flex-wrap: wrap; }
.page-head h1 { font-size: 22px; margin: 0; font-weight: 700; }
.page-head p { margin: 4px 0 0; color: var(--text-soft); font-size: 13px; }

/* Cards & grid */
.grid { display: grid; gap: 16px; }
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 14px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 18px; box-shadow: var(--shadow); }
.stat .stat-label { color: var(--text-soft); font-size: 12px; text-transform: uppercase; letter-spacing: .04em; }
.stat .stat-value { font-size: 28px; font-weight: 700; margin-top: 6px; }
.stat .stat-sub { font-size: 12px; color: var(--text-soft); margin-top: 2px; }

/* Buttons & inputs */
button, .btn { font-family: inherit; font-size: 13px; }
.btn { display: inline-flex; align-items: center; gap: 6px; background: var(--brand-dark); color: #fff; border: 0; padding: 9px 16px; border-radius: 9px; cursor: pointer; font-weight: 600; transition: .15s; }
.btn:hover { filter: brightness(1.07); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.secondary { background: var(--panel-2); color: var(--text); border: 1px solid var(--border); }
.btn.danger { background: var(--danger); }
.btn.small { padding: 6px 11px; font-size: 12px; border-radius: 8px; }
.btn.ghost { background: transparent; color: var(--brand-dark); border: 1px solid var(--border); }
input, select, textarea {
  font-family: inherit; font-size: 14px; width: 100%; padding: 10px 12px; border-radius: 9px;
  border: 1px solid var(--border); background: var(--panel); color: var(--text); outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--brand-dark); }
textarea { resize: vertical; min-height: 80px; }
label.field { display: block; margin-bottom: 14px; }
label.field > span { display: block; font-size: 12px; font-weight: 600; color: var(--text-soft); margin-bottom: 6px; }
.row { display: flex; gap: 12px; flex-wrap: wrap; }
.row > * { flex: 1; min-width: 160px; }

/* Table */
table { width: 100%; border-collapse: collapse; background: var(--panel); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
th, td { text-align: left; padding: 12px 14px; border-bottom: 1px solid var(--border); font-size: 13px; }
th { background: var(--panel-2); color: var(--text-soft); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
tr:last-child td { border-bottom: 0; }
tr:hover td { background: var(--panel-2); }
.table-wrap { overflow-x: auto; }

/* Badges */
.badge { display: inline-flex; align-items: center; gap: 5px; padding: 3px 9px; border-radius: 999px; font-size: 11px; font-weight: 600; }
.badge.green { background: rgba(37,211,102,.15); color: var(--brand-dark); }
.badge.amber { background: rgba(245,158,11,.15); color: #b45309; }
.badge.red { background: rgba(239,68,68,.15); color: #b91c1c; }
.badge.blue { background: rgba(52,183,241,.15); color: #0369a1; }
.badge.gray { background: var(--panel-2); color: var(--text-soft); }
:root[data-theme="dark"] .badge.green { color: #6ee7a8; }
:root[data-theme="dark"] .badge.amber { color: #fcd34d; }
:root[data-theme="dark"] .badge.red { color: #fca5a5; }

/* Chat layout */
.chat { display: grid; grid-template-columns: 320px 1fr; height: 100%; }
.thread-list { border-right: 1px solid var(--border); background: var(--panel); overflow-y: auto; }
.thread-list .tl-head { padding: 14px 16px; border-bottom: 1px solid var(--border); font-weight: 700; display:flex; justify-content:space-between; align-items:center; }
.thread { display: flex; gap: 10px; padding: 12px 16px; cursor: pointer; border-bottom: 1px solid var(--border); align-items: center; }
.thread:hover { background: var(--panel-2); }
.thread.active { background: var(--panel-2); }
.avatar { width: 42px; height: 42px; border-radius: 50%; background: var(--brand-dark); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; flex-shrink: 0; }
.thread-main { flex: 1; min-width: 0; }
.thread-name { font-weight: 600; display: flex; justify-content: space-between; gap: 6px; }
.thread-name small { color: var(--text-soft); font-weight: 400; }
.thread-last { color: var(--text-soft); font-size: 12px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-panel { display: flex; flex-direction: column; height: 100%; background: var(--chat-bg); }
.chat-head { padding: 12px 18px; background: var(--panel); border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 12px; }
.chat-head .who { flex: 1; }
.chat-head .who b { display: block; }
.chat-head .who small { color: var(--text-soft); }
.chat-body { flex: 1; overflow-y: auto; padding: 18px; display: flex; flex-direction: column; gap: 8px; }
.bubble { max-width: 68%; padding: 8px 11px; border-radius: 10px; box-shadow: var(--shadow); font-size: 14px; line-height: 1.35; word-wrap: break-word; position: relative; }
.bubble .meta { font-size: 10px; color: var(--text-soft); margin-top: 4px; text-align: right; }
.bubble.out { align-self: flex-end; background: var(--bubble-out); }
.bubble.in { align-self: flex-start; background: var(--bubble-in); }
:root[data-theme="dark"] .bubble.out, :root[data-theme="dark"] .bubble.in { color: var(--text); }
.chat-compose { padding: 12px 16px; background: var(--panel); border-top: 1px solid var(--border); }
.window-note { font-size: 12px; padding: 8px 12px; border-radius: 8px; margin-bottom: 8px; }
.window-note.open { background: rgba(37,211,102,.12); color: var(--brand-dark); }
.window-note.closed { background: rgba(245,158,11,.14); color: #b45309; }
:root[data-theme="dark"] .window-note.open { color: #6ee7a8; }
:root[data-theme="dark"] .window-note.closed { color: #fcd34d; }
.compose-row { display: flex; gap: 8px; align-items: flex-end; }
.compose-row textarea { min-height: 44px; max-height: 120px; }
.chat-empty { display: flex; align-items: center; justify-content: center; height: 100%; color: var(--text-soft); flex-direction: column; gap: 8px; }

/* Params editor */
.param-row { display: flex; gap: 8px; margin-bottom: 8px; }
.param-row input { flex: 1; }
.chip-list { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { background: var(--panel-2); border: 1px solid var(--border); border-radius: 999px; padding: 4px 10px; font-size: 12px; display: inline-flex; gap: 6px; align-items: center; cursor: pointer; }
.chip.on { background: var(--brand-dark); color: #fff; border-color: var(--brand-dark); }

/* Misc */
.muted { color: var(--text-soft); }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; }
.empty { text-align: center; padding: 48px 20px; color: var(--text-soft); }
.empty .big { font-size: 40px; margin-bottom: 8px; }
.spinner { width: 20px; height: 20px; border: 2px solid var(--border); border-top-color: var(--brand-dark); border-radius: 50%; animation: spin .7s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.center { display: flex; align-items: center; justify-content: center; padding: 40px; }
.toolbar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 16px; }
.bar-chart { display: flex; align-items: flex-end; gap: 10px; height: 160px; padding: 10px 0; overflow-x: auto; }
.bar-chart .bar-col { flex: 0 0 auto; width: 40px; display: flex; flex-direction: column; align-items: center; gap: 6px; }
.bar-chart .bar-pair { display: flex; gap: 3px; align-items: flex-end; height: 120px; }
.bar-chart .bar { width: 14px; background: var(--brand); border-radius: 3px 3px 0 0; }
.bar-chart .bar.in { background: var(--accent); }
.bar-chart .bar-label { font-size: 10px; color: var(--text-soft); }

/* Toast & modal */
.toast { position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(20px); background: #111b21; color: #fff; padding: 12px 20px; border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.25); opacity: 0; pointer-events: none; transition: .25s; z-index: 100; font-size: 13px; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.error { background: var(--danger); }
.toast.success { background: var(--brand-dark); }
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.5); display: flex; align-items: center; justify-content: center; z-index: 90; padding: 20px; }
.modal-overlay[hidden] { display: none; }
.modal { background: var(--panel); border-radius: var(--radius); padding: 22px; width: 100%; max-width: 520px; max-height: 88vh; overflow-y: auto; box-shadow: 0 20px 60px rgba(0,0,0,.3); }
.modal h3 { margin: 0 0 14px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

@media (max-width: 820px) {
  #app { grid-template-columns: 68px 1fr; }
  .brand-name, .brand-sub, .nav a span:not(.ico), .sender-badge { display: none; }
  .nav a { justify-content: center; }
  .chat { grid-template-columns: 1fr; }
}
