:root {
  --bg: #f6f7f9; --panel: #ffffff; --side: #111827; --side-fg: #e5e7eb; --fg: #111827; --muted: #6b7280;
  --line: #e5e7eb; --accent: #0f766e; --accent-fg: #fff; --danger: #b91c1c; --warn-bg: #fff7ed; --warn: #c2410c;
  --user: #e0f2f1; --tool: #f3f4f6; --code: #0b1020; --code-fg: #e5e7eb;
}
@media (prefers-color-scheme: dark) {
  :root { --bg: #0b0f17; --panel: #121826; --side: #070a10; --fg: #e5e7eb; --muted: #9ca3af; --line: #263041;
    --user: #12312f; --tool: #1a2232; --warn-bg: #2a1a0e; --warn: #fdba74; }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; }
body { background: var(--bg); color: var(--fg); font: 15px/1.55 "Noto Sans Bengali", "Hind Siliguri", system-ui, sans-serif; }
.hidden { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
button { border: 1px solid var(--line); background: var(--panel); padding: 7px 12px; border-radius: 8px; cursor: pointer; }
button.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
button.danger { background: var(--danger); color: #fff; border-color: var(--danger); }
button.icon { border: none; background: none; font-size: 18px; padding: 6px 10px; }
button.wide { width: 100%; }
input, select, textarea { border: 1px solid var(--line); background: var(--panel); border-radius: 8px; padding: 7px 10px; }
.muted { color: var(--muted); }
.err { color: var(--danger); min-height: 1.2em; }
.grow { flex: 1; }
pre { background: var(--code); color: var(--code-fg); padding: 10px 12px; border-radius: 8px; overflow-x: auto; font-size: 13px; }
code { font-family: ui-monospace, "JetBrains Mono", Consolas, monospace; font-size: .92em; }

.auth { display: grid; place-items: center; height: 100%; padding: 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 28px; width: min(360px, 100%); display: grid; gap: 10px; }
.card h1 { margin: 0; }

.app { display: grid; grid-template-columns: 260px 1fr; grid-template-rows: 100%; height: 100vh; height: 100dvh; overflow: hidden; }
aside { background: var(--side); color: var(--side-fg); padding: 14px; display: flex; flex-direction: column; gap: 12px; overflow: hidden; }
.brand { font-weight: 700; font-size: 18px; }
.conv-list { flex: 1; min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 2px; }
.conv { padding: 8px 10px; border-radius: 8px; cursor: pointer; display: flex; gap: 6px; align-items: center; }
.conv:hover, .conv.active { background: #1f2937; }
.conv .t { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv .x { opacity: 0; border: none; background: none; color: inherit; padding: 0 4px; }
.conv:hover .x { opacity: .7; }
.conv .dot { width: 8px; height: 8px; border-radius: 50%; background: #34d399; }

main { display: flex; flex-direction: column; min-width: 0; min-height: 0; height: 100%; overflow: hidden; }
header, .composer, .attachments { flex-shrink: 0; }
header { display: flex; align-items: center; gap: 10px; padding: 8px 14px; border-bottom: 1px solid var(--line); background: var(--panel); flex-wrap: wrap; }
#menuBtn { display: none; }
.tabs { display: flex; gap: 4px; }
.tabs button { border: none; background: none; padding: 8px 12px; border-radius: 8px; }
.tabs button.active { background: var(--user); font-weight: 600; }
.brain { margin-left: auto; display: flex; gap: 6px; }
.tab { flex: 1; min-height: 0; overflow: hidden; display: flex; flex-direction: column; }

.messages { flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; padding: 18px; display: flex; flex-direction: column; gap: 14px; }
.msg { max-width: 920px; width: 100%; margin: 0 auto; }
.msg.user .bubble { background: var(--user); margin-left: auto; max-width: 80%; width: fit-content; white-space: pre-wrap; }
.bubble { padding: 10px 14px; border-radius: 12px; background: var(--panel); border: 1px solid var(--line); overflow-wrap: anywhere; }
.bubble > :first-child { margin-top: 0; } .bubble > :last-child { margin-bottom: 0; }
.bubble table { border-collapse: collapse; } .bubble td, .bubble th { border: 1px solid var(--line); padding: 4px 8px; }
.meta { font-size: 12px; color: var(--muted); margin: 3px 4px; }
.tool { background: var(--tool); border: 1px solid var(--line); border-radius: 10px; margin: 6px 0; font-size: 13px; }
.tool summary { padding: 6px 10px; cursor: pointer; }
.tool pre { margin: 0 8px 8px; max-height: 360px; }
.tool.err summary { color: var(--danger); }
.approval { border: 2px solid var(--warn); background: var(--warn-bg); border-radius: 12px; padding: 10px 14px; }
.approval .row { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.approval input { flex: 1; min-width: 160px; }
.notice { font-size: 13px; color: var(--warn); text-align: center; }
.typing::after { content: "▍"; animation: blink 1s infinite; } @keyframes blink { 50% { opacity: 0; } }
.composer { display: flex; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--line); background: var(--panel); }
.attachments { display: flex; gap: 8px; padding: 8px 18px 0; flex-wrap: wrap; background: var(--panel); border-top: 1px solid var(--line); }
.attachments .att { position: relative; }
.attachments img { height: 64px; border-radius: 8px; border: 1px solid var(--line); display: block; }
.attachments button { position: absolute; top: -6px; right: -6px; padding: 0 6px; border-radius: 50%; font-size: 12px; }
.msg .imgs { display: flex; gap: 6px; justify-content: flex-end; flex-wrap: wrap; margin-bottom: 4px; }
.msg .imgs img { max-height: 180px; max-width: 260px; border-radius: 10px; border: 1px solid var(--line); cursor: zoom-in; }
#micBtn.rec { background: var(--danger); color: #fff; border-radius: 50%; animation: blink 1s infinite; }
.composer.drag { outline: 2px dashed var(--accent); }
.composer textarea { flex: 1; resize: vertical; min-height: 46px; max-height: 300px; }

.toolbar { display: flex; gap: 8px; padding: 12px 18px; align-items: center; }
.toolbar input { flex: 1; }
.table-wrap { flex: 1; min-height: 0; overflow: auto; padding: 0 18px 18px; }
table#devTable { width: 100%; border-collapse: collapse; background: var(--panel); }
#devTable th, #devTable td { text-align: left; padding: 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
#devTable td small { color: var(--muted); display: block; }
#devTable .acts { white-space: nowrap; }
.badge { font-size: 11px; padding: 1px 6px; border-radius: 6px; background: var(--tool); border: 1px solid var(--line); }
.badge.ro { color: var(--warn); }

.kn { display: grid; grid-template-columns: 280px 1fr; flex: 1; overflow: hidden; }
.kn-list { overflow-y: auto; padding: 12px 18px; border-right: 1px solid var(--line); }
.kn-list h3 { font-size: 13px; color: var(--muted); margin: 12px 0 4px; }
.kn-list .f { padding: 4px 8px; border-radius: 6px; cursor: pointer; font-size: 13px; overflow-wrap: anywhere; }
.kn-list .f:hover, .kn-list .f.active { background: var(--user); }
.kn-edit { display: flex; flex-direction: column; overflow: hidden; }
.kn-edit textarea { flex: 1; margin: 0 18px 18px; font-family: ui-monospace, Consolas, monospace; font-size: 13px; resize: none; }

.settings { max-width: 760px; padding: 12px 18px; overflow-y: auto; }
#tab-settings { overflow-y: auto; display: block; }
fieldset { border: 1px solid var(--line); border-radius: 10px; margin: 0 0 14px; padding: 10px 14px; display: grid; gap: 10px; background: var(--panel); }
legend { font-weight: 600; padding: 0 6px; }
.settings label { display: grid; gap: 4px; }
label.chk { display: flex; gap: 8px; align-items: center; }
label.warn { color: var(--warn); }

dialog { border: 1px solid var(--line); border-radius: 14px; background: var(--panel); color: var(--fg); width: min(720px, 96vw); padding: 0; }
dialog::backdrop { background: rgba(0,0,0,.45); }
.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; padding: 18px; }
.grid h3 { grid-column: 1 / -1; margin: 0; }
.grid label { display: grid; gap: 4px; font-size: 13px; }
.grid .full { grid-column: 1 / -1; }
.row { display: flex; gap: 8px; }

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  aside { position: fixed; inset: 0 30% 0 0; z-index: 5; transform: translateX(-100%); transition: transform .2s; }
  aside.open { transform: none; }
  #menuBtn { display: block; }
  .brain { margin-left: 0; width: 100%; } .brain select { flex: 1; min-width: 0; }
  .kn { grid-template-columns: 1fr; } .kn-list { max-height: 35vh; border-right: none; border-bottom: 1px solid var(--line); }
  .grid { grid-template-columns: 1fr; }
  .msg.user .bubble { max-width: 95%; }
  .messages, .composer, .toolbar, .table-wrap, .settings { padding-left: 16px; padding-right: 16px; }
}

.acct-row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.dot.off { width: 9px; height: 9px; border-radius: 50%; background: #9ca3af; display: inline-block; }
.dot.on { width: 9px; height: 9px; border-radius: 50%; background: #16a34a; display: inline-block; }
.login-box { border: 1px dashed var(--line); border-radius: 10px; padding: 10px 14px; display: grid; gap: 8px; }
.login-box p { margin: 0; }
.login-box textarea { font-family: ui-monospace, Consolas, monospace; font-size: 12px; resize: none; background: var(--tool); }
a.btn { display: inline-block; padding: 7px 12px; border-radius: 8px; text-decoration: none; border: 1px solid var(--line); }
a.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.small { font-size: 13px; }

.brain-order { display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center; margin-bottom: 8px; }
.brain-order > span { flex-basis: 100%; }

/* ping monitor */
#tab-ping { overflow-y: auto; }
.ping-wrap { max-width: 900px; margin: 0 auto; padding: 16px; display: grid; gap: 12px; }
.ping-head { display: flex; gap: 10px; align-items: baseline; flex-wrap: wrap; }
.ping-live { font-size: 13px; font-weight: 600; color: #16a34a; white-space: nowrap; }
.ping-live.stale { color: var(--danger); }
.ping-list { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.ping-row { display: grid; grid-template-columns: 14px minmax(0, 1fr) 140px 64px; gap: 10px; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--line); cursor: pointer; }
.ping-row:last-child { border-bottom: none; }
.ping-row:hover, .ping-row.active { background: var(--tool); }
.ping-row .pdot { width: 9px; height: 9px; border-radius: 50%; background: #22c55e; }
.ping-row .pdot.down { background: var(--danger); }
.ping-row .pname { font-weight: 600; line-height: 1.2; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ping-row .phost { color: var(--muted); font-size: 12px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ping-row canvas { width: 140px; height: 34px; }
.ping-row .pms { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.pms.good { color: #16a34a; } .pms.mid { color: #d97706; } .pms.bad { color: var(--danger); }
.ping-add input[name=name] { width: 180px; }
.ping-detail { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 14px; display: grid; gap: 10px; }
.ping-detail .row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.ping-ranges { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.ping-ranges button { padding: 4px 10px; }
.ping-ranges button.active { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
a.btn { border: 1px solid var(--line); padding: 4px 10px; border-radius: 8px; text-decoration: none; color: inherit; }
.ping-chart { position: relative; }
.ping-chart canvas { width: 100%; height: 280px; display: block; }
.ping-tip { position: absolute; top: 4px; pointer-events: none; background: var(--code); color: var(--code-fg); font-size: 12px; padding: 4px 8px; border-radius: 6px; white-space: nowrap; }
.ping-stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 8px; }
.ping-stats div { background: var(--tool); border-radius: 8px; padding: 8px 10px; }
.ping-stats b { display: block; font-size: 18px; font-variant-numeric: tabular-nums; }
@media (max-width: 560px) {
  .ping-row { grid-template-columns: 12px minmax(0, 1fr) 90px 54px; padding: 10px; }
  .ping-row canvas { width: 90px; }
  .ping-add input[name=name] { width: 100%; }
}
