:root {
  --bg: #0c0e13;
  --panel: #141821;
  --panel-2: #1a1f2b;
  --panel-3: #222838;
  --border: #272e3f;
  --border-strong: #353d52;
  --text: #e7eaf1;
  --muted: #8b94a8;
  --faint: #5f6880;
  --accent: #f5b638;
  --accent-ink: #1a1405;
  --accent-soft: rgba(245, 182, 56, 0.12);
  --ok: #3cc98b;
  --ok-soft: rgba(60, 201, 139, 0.12);
  --warn: #f0a53a;
  --warn-soft: rgba(240, 165, 58, 0.12);
  --err: #f06464;
  --err-soft: rgba(240, 100, 100, 0.12);
  --info: #62a8f2;
  --info-soft: rgba(98, 168, 242, 0.12);
  --radius: 10px;
  --radius-sm: 7px;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  color-scheme: dark;
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  line-height: 1.5;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); -webkit-font-smoothing: antialiased; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
h1 { font-size: 22px; margin: 0 0 18px; letter-spacing: -0.01em; }
h2 { font-size: 15px; margin: 0 0 12px; }
h3 { font-size: 13px; margin: 0 0 8px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); }
p { margin: 0 0 10px; }
code, pre, .mono { font-family: var(--mono); font-size: 12.5px; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.small { font-size: 12px; }
.hidden { display: none !important; }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.grow { flex: 1; min-width: 0; }
.boot { display: grid; place-items: center; height: 100vh; color: var(--muted); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border: 1px solid var(--border-strong); background: var(--panel-2); border-radius: var(--radius-sm);
  padding: 7px 13px; cursor: pointer; white-space: nowrap; transition: background .12s, border-color .12s;
}
.btn:hover { background: var(--panel-3); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--err); border-color: rgba(240, 100, 100, .35); background: transparent; }
.btn.danger:hover { background: var(--err-soft); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--text); background: var(--panel-2); }
.btn.small { padding: 4px 9px; font-size: 12px; }
.btn.block { width: 100%; }
.icon-btn { border: 0; background: transparent; color: var(--muted); cursor: pointer; padding: 3px 6px; border-radius: 5px; }
.icon-btn:hover { color: var(--text); background: var(--panel-3); }
.icon-btn.on { color: var(--accent); }

input[type="text"], input[type="password"], input[type="number"], input[type="search"], select, textarea {
  width: 100%; background: var(--bg); border: 1px solid var(--border-strong); border-radius: var(--radius-sm);
  padding: 8px 10px; outline: none; transition: border-color .12s, box-shadow .12s;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
textarea { resize: vertical; min-height: 70px; line-height: 1.45; }
.field { display: grid; gap: 5px; font-size: 12.5px; color: var(--muted); }
.field > span:first-child { font-weight: 500; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.check { display: inline-flex; align-items: center; gap: 7px; cursor: pointer; font-size: 13px; }
.form-grid { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); }
.form-grid .wide { grid-column: 1 / -1; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.row.end { justify-content: flex-end; }
.row.between { justify-content: space-between; }
.stack { display: grid; gap: 12px; }

.shell { display: grid; grid-template-columns: 228px 1fr; height: 100vh; }
.sidebar { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 16px 12px; gap: 18px; min-height: 0; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; padding: 2px 6px; }
.brand-mark { width: 30px; height: 30px; border-radius: 8px; background: var(--accent); color: var(--accent-ink); display: grid; place-items: center; font-weight: 800; }
.nav { display: grid; gap: 2px; }
.nav-item { text-align: left; border: 0; background: transparent; padding: 9px 11px; border-radius: var(--radius-sm); color: var(--muted); cursor: pointer; font-weight: 500; }
.nav-item:hover { color: var(--text); background: var(--panel-2); }
.nav-item.active { color: var(--text); background: var(--accent-soft); box-shadow: inset 3px 0 0 var(--accent); }
.sidebar-foot { margin-top: auto; display: grid; gap: 10px; padding: 0 6px; }
.upstream { display: flex; align-items: center; gap: 8px; font-size: 12.5px; color: var(--muted); }
.who { font-size: 12.5px; color: var(--muted); }
.dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); flex: none; }
.dot.ok { background: var(--ok); box-shadow: 0 0 0 3px var(--ok-soft); }
.dot.err { background: var(--err); box-shadow: 0 0 0 3px var(--err-soft); }
.dot.warn { background: var(--warn); box-shadow: 0 0 0 3px var(--warn-soft); }

.main { min-width: 0; min-height: 0; overflow: auto; }
.content { padding: 26px 30px 40px; max-width: 1400px; }
.content.flush { padding: 0; max-width: none; height: 100%; }

.panel { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; min-width: 0; }
.panel + .panel { margin-top: 16px; }
.panel-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.panel-head h2 { margin: 0; }
.cards { display: grid; gap: 12px; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); margin-bottom: 16px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; }
.card-label { font-size: 12px; color: var(--muted); display: flex; align-items: center; gap: 7px; }
.card-value { font-size: 24px; font-weight: 700; margin-top: 4px; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.card-sub { font-size: 12px; color: var(--faint); margin-top: 2px; }
.two { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); margin-top: 16px; }
.two .panel + .panel { margin-top: 0; }

.chart { display: flex; align-items: flex-end; gap: 4px; height: 150px; padding-top: 8px; }
.bar { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; height: 100%; min-width: 0; position: relative; }
.bar-fill { background: var(--accent); border-radius: 3px 3px 0 0; min-height: 2px; opacity: .85; }
.bar-err { background: var(--err); border-radius: 3px 3px 0 0; }
.bar:hover .bar-fill { opacity: 1; }
.chart-axis { display: flex; justify-content: space-between; font-size: 11px; color: var(--faint); margin-top: 6px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { text-align: left; font-weight: 600; font-size: 11.5px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); padding: 8px 10px; border-bottom: 1px solid var(--border); white-space: nowrap; }
td { padding: 9px 10px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover td { background: var(--panel-2); }
td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.empty { text-align: center; color: var(--faint); padding: 26px 10px; }

.badge { display: inline-flex; align-items: center; gap: 5px; padding: 2px 8px; border-radius: 999px; font-size: 11.5px; font-weight: 600; background: var(--panel-3); color: var(--muted); white-space: nowrap; }
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.err { background: var(--err-soft); color: var(--err); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.info { background: var(--info-soft); color: var(--info); }
.badge.accent { background: var(--accent-soft); color: var(--accent); }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }

.filters { display: grid; gap: 10px; grid-template-columns: 1.5fr repeat(3, minmax(120px, 1fr)) auto; align-items: end; margin-bottom: 14px; }

.modal-backdrop { position: fixed; inset: 0; background: rgba(4, 6, 10, .72); display: grid; place-items: center; padding: 20px; z-index: 50; }
.modal { background: var(--panel); border: 1px solid var(--border-strong); border-radius: 14px; width: min(760px, 100%); max-height: calc(100vh - 40px); display: flex; flex-direction: column; box-shadow: 0 30px 80px rgba(0, 0, 0, .5); }
.modal.narrow { width: min(480px, 100%); }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.modal-head h2 { margin: 0; }
.modal-body { padding: 18px; overflow: auto; }
.modal-foot { padding: 12px 18px; border-top: 1px solid var(--border); display: flex; justify-content: flex-end; gap: 8px; }
pre.code { background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 12px; overflow: auto; max-height: 360px; white-space: pre-wrap; word-break: break-word; margin: 0; }
.kv { display: grid; grid-template-columns: 140px 1fr; gap: 6px 14px; font-size: 13px; margin-bottom: 16px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.secret-box { display: flex; gap: 8px; align-items: center; background: var(--bg); border: 1px dashed var(--accent); border-radius: var(--radius-sm); padding: 10px 12px; }
.secret-box code { flex: 1; overflow-wrap: anywhere; color: var(--accent); }
.notice { border-radius: var(--radius-sm); padding: 10px 12px; font-size: 13px; background: var(--warn-soft); color: var(--warn); }
.notice.info { background: var(--info-soft); color: var(--info); }

.toasts { position: fixed; right: 18px; bottom: 18px; display: grid; gap: 8px; z-index: 80; }
.toast { background: var(--panel-3); border: 1px solid var(--border-strong); border-left: 3px solid var(--accent); padding: 10px 14px; border-radius: var(--radius-sm); box-shadow: 0 10px 30px rgba(0, 0, 0, .4); max-width: 360px; animation: toast-in .18s ease-out; }
.toast.err { border-left-color: var(--err); }
.toast.ok { border-left-color: var(--ok); }
@keyframes toast-in { from { transform: translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }

.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; background: radial-gradient(900px 500px at 50% -10%, rgba(245, 182, 56, .08), transparent); }
.login { width: min(380px, 100%); background: var(--panel); border: 1px solid var(--border); border-radius: 16px; padding: 28px; display: grid; gap: 14px; }
.login .brand { padding: 0; margin-bottom: 6px; font-size: 16px; }
.form-error { color: var(--err); font-size: 13px; min-height: 18px; }

.chat-layout { display: grid; grid-template-columns: 250px minmax(0, 1fr) 300px; height: 100vh; }
.chat-list { border-right: 1px solid var(--border); padding: 14px 10px; display: flex; flex-direction: column; gap: 10px; min-height: 0; background: var(--panel); }
.chat-items { overflow: auto; display: grid; gap: 2px; align-content: start; }
.chat-item { text-align: left; border: 0; background: transparent; border-radius: var(--radius-sm); padding: 8px 10px; cursor: pointer; display: grid; gap: 1px; color: var(--text); }
.chat-item:hover { background: var(--panel-2); }
.chat-item.active { background: var(--accent-soft); }
.chat-item-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.chat-item-meta { font-size: 11px; color: var(--faint); }
.chat-main { display: flex; flex-direction: column; min-height: 0; min-width: 0; }
.chat-head { display: flex; gap: 8px; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.chat-head .title-input { flex: 1; min-width: 160px; background: transparent; border-color: transparent; font-weight: 600; font-size: 15px; }
.chat-head .title-input:hover { border-color: var(--border-strong); }
.chat-head select { width: auto; max-width: 220px; }
.sys-panel { padding: 12px 14px; border-bottom: 1px solid var(--border); background: var(--panel); display: grid; gap: 8px; }
.messages { flex: 1; overflow: auto; padding: 20px 18px 10px; display: flex; flex-direction: column; gap: 14px; }
.msg { max-width: min(780px, 92%); display: grid; gap: 4px; }
.msg.user { align-self: flex-end; }
.msg.assistant { align-self: flex-start; }
.msg-body { padding: 10px 14px; border-radius: 14px; overflow-wrap: anywhere; }
.msg.user .msg-body { background: var(--accent-soft); border: 1px solid rgba(245, 182, 56, .25); border-bottom-right-radius: 4px; }
.msg.assistant .msg-body { background: var(--panel); border: 1px solid var(--border); border-bottom-left-radius: 4px; }
.msg.error .msg-body { border-color: rgba(240, 100, 100, .4); }
.msg.pending .msg-body:empty::after { content: "…"; color: var(--muted); animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .3; } }
.msg-meta { font-size: 11px; color: var(--faint); padding: 0 4px; }
.msg.user .msg-meta { text-align: right; }
.msg-text { white-space: pre-wrap; }
.msg-body pre { background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 10px; overflow: auto; margin: 8px 0; white-space: pre; }
.msg-body pre:first-child { margin-top: 0; }
.msg-body pre:last-child { margin-bottom: 0; }
.msg-body code.inline { background: var(--panel-3); padding: 1px 5px; border-radius: 4px; }
.chat-empty { margin: auto; text-align: center; color: var(--muted); max-width: 380px; }
.chat-empty h2 { color: var(--text); }
.composer { border-top: 1px solid var(--border); padding: 12px 14px; display: grid; gap: 8px; }
.composer textarea { min-height: 46px; max-height: 240px; resize: none; }
.composer .row { flex-wrap: nowrap; }
.attach { display: inline-flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted); background: var(--panel-2); border-radius: var(--radius-sm); padding: 4px 6px 4px 4px; }
.attach img { width: 38px; height: 38px; object-fit: cover; border-radius: 5px; }

.memory { border-left: 1px solid var(--border); background: var(--panel); padding: 14px; overflow: auto; display: flex; flex-direction: column; gap: 16px; min-height: 0; }
.memory-status { display: flex; align-items: center; gap: 7px; font-size: 12px; color: var(--muted); }
.facts { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.fact { display: flex; gap: 6px; align-items: flex-start; background: var(--panel-2); border: 1px solid var(--border); border-radius: var(--radius-sm); padding: 7px 6px 7px 10px; font-size: 12.5px; }
.fact.pinned { border-color: rgba(245, 182, 56, .35); }
.fact-text { flex: 1; min-width: 0; overflow-wrap: anywhere; cursor: text; }
.fact-actions { display: flex; flex: none; }
.fact input { padding: 3px 6px; font-size: 12.5px; }
.memory textarea { font-size: 12.5px; min-height: 120px; }

@media (max-width: 1100px) {
  .chat-layout { grid-template-columns: 220px minmax(0, 1fr); }
  .memory { display: none; }
  .memory.open { display: flex; position: fixed; right: 0; top: 0; bottom: 0; width: min(320px, 90vw); z-index: 40; box-shadow: -20px 0 50px rgba(0, 0, 0, .5); }
  .filters { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .sidebar { flex-direction: row; align-items: center; padding: 8px 12px; gap: 10px; border-right: 0; border-bottom: 1px solid var(--border); overflow-x: auto; }
  .brand span:last-child, .sidebar-foot .who, .upstream .status-text { display: none; }
  .nav { display: flex; }
  .nav-item { padding: 7px 9px; white-space: nowrap; }
  .nav-item.active { box-shadow: inset 0 -2px 0 var(--accent); }
  .sidebar-foot { margin: 0 0 0 auto; display: flex; align-items: center; padding: 0; }
  .content { padding: 18px 16px 30px; }
  .chat-layout { grid-template-columns: 1fr; height: calc(100vh - 53px); }
  .chat-list { display: none; }
  .chat-list.open { display: flex; position: fixed; left: 0; top: 0; bottom: 0; width: min(300px, 88vw); z-index: 40; box-shadow: 20px 0 50px rgba(0, 0, 0, .5); }
  .filters { grid-template-columns: 1fr; }
  .kv { grid-template-columns: 1fr; gap: 2px; }
  .kv dd { margin-bottom: 8px; }
}

@media (min-width: 761px) {
  .only-mobile { display: none !important; }
}

@media (min-width: 1101px) {
  .only-narrow { display: none !important; }
}

.icon-btn.pin { filter: grayscale(1); opacity: .45; }
.icon-btn.pin:hover { opacity: .8; }
.icon-btn.pin.on { filter: none; opacity: 1; }

.thumbs { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 14px; }
.msg-body .thumbs { margin-bottom: 8px; }
.thumb { border: 1px solid var(--border-strong); background: var(--bg); border-radius: var(--radius-sm); padding: 0; cursor: zoom-in; overflow: hidden; }
.thumb img { display: block; max-width: 220px; max-height: 140px; object-fit: contain; }
.thumb:hover { border-color: var(--accent); }
.lightbox { display: block; max-width: 100%; height: auto; border-radius: var(--radius-sm); }
.img-count { margin-left: 6px; }
