:root {
  --bg: #0f1419;
  --panel: #161d24;
  --panel2: #1d2731;
  --border: #2a3540;
  --text: #e6edf3;
  --muted: #8b98a5;
  --accent: #25d366;
  --accent2: #128c7e;
  --danger: #e74c3c;
  --warn: #f1c40f;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  gap: 10px;
}

.brand { display: flex; align-items: center; gap: 12px; }
.logo { font-size: 28px; }
.brand h1 { font-size: 18px; }
.status-line { font-size: 12px; color: var(--muted); }

.tabs { display: flex; gap: 6px; }
.tab {
  background: var(--panel2);
  color: var(--muted);
  border: 1px solid var(--border);
  padding: 8px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
}
.tab.active { color: var(--text); border-color: var(--accent); background: #16302a; }

main { flex: 1; display: flex; overflow: hidden; }

.tab-panel { display: none; flex: 1; }
.tab-panel.active { display: flex; }

#tab-chat { flex-direction: row; }

.sidebar {
  width: 320px;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  background: var(--panel);
}

.sidebar-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px;
}
.sidebar-head h2 { font-size: 15px; }
#btn-refresh {
  background: none; border: 1px solid var(--border); color: var(--text);
  border-radius: 6px; padding: 4px 10px; cursor: pointer; font-size: 14px;
}

.simulate-box {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--panel2);
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.simulate-box h3 { font-size: 12px; color: var(--muted); font-weight: 600; }
.row { display: flex; gap: 8px; }

input, textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 14px;
  outline: none;
}
input:focus { border-color: var(--accent); }

button.primary {
  background: var(--accent2);
  border: none;
  color: white;
  padding: 9px 16px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  white-space: nowrap;
}
button.primary:hover { background: var(--accent); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

.conv-list { flex: 1; overflow-y: auto; list-style: none; }
.conv-item {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  align-items: center;
}
.conv-item:hover { background: var(--panel2); }
.conv-item.active { background: #1c3a30; border-left: 3px solid var(--accent); }
.conv-name { font-weight: 600; font-size: 14px; }
.conv-preview { color: var(--muted); font-size: 12px; margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
.badge { font-size: 11px; padding: 2px 8px; border-radius: 10px; background: var(--panel2); color: var(--muted); }
.badge.inst { background: #3a2f1d; color: #f0c674; }
textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 12px;
  font-size: 14px;
  outline: none;
  font-family: inherit;
  resize: vertical;
}
textarea:focus { border-color: var(--accent); }
select {
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 9px 10px;
  font-size: 14px;
  outline: none;
}
.kb-form { display: flex; flex-direction: column; gap: 10px; }
.kb-form .row { align-items: center; }
.badge.neg { background: #3a1d1d; color: #ff8080; }
.badge.pos { background: #1d3a26; color: #7ee2a8; }

.chat-area { flex: 1; display: flex; flex-direction: column; }
.chat-empty {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--muted);
}
.chat-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}
.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 13px; }

.switch { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); cursor: pointer; }

.chat-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  background: var(--bg);
}
.msg {
  max-width: 65%;
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-word;
}
.msg.in { background: var(--panel2); align-self: flex-start; border-bottom-left-radius: 4px; }
.msg.out { background: #0e5c4a; align-self: flex-end; border-bottom-right-radius: 4px; }
.msg .meta { display: block; font-size: 11px; color: var(--muted); margin-top: 6px; }

.chat-input { display: flex; gap: 10px; padding: 14px; border-top: 1px solid var(--border); background: var(--panel); }

#tab-kb, #tab-settings {
  overflow-y: auto;
  padding: 24px;
  flex-direction: column;
  gap: 20px;
}
.kb-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 20px; }

.card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.card h2 { font-size: 16px; }

.kb-stats { display: flex; gap: 10px; flex-wrap: wrap; }
.kb-stats .stat {
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 14px;
  text-align: center;
}
.kb-stats .stat b { display: block; font-size: 18px; }
.kb-stats .stat span { font-size: 11px; color: var(--muted); }

.doc-list { list-style: none; display: flex; flex-direction: column; gap: 8px; }
.doc-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--panel2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 13px;
}
.doc-item .del { background: none; border: 1px solid var(--danger); color: var(--danger); border-radius: 6px; padding: 3px 10px; cursor: pointer; font-size: 12px; }
.doc-item .del:hover { background: var(--danger); color: white; }
.doc-item .view { background: none; border: 1px solid var(--accent); color: var(--accent); border-radius: 6px; padding: 3px 10px; cursor: pointer; font-size: 12px; }
.doc-item .view:hover { background: var(--accent); color: white; }
.doc-actions { display: flex; gap: 6px; }

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 200;
}
.modal-box {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
  width: min(640px, 92vw);
  max-height: 85vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-close { background: none; border: none; color: var(--muted); font-size: 26px; cursor: pointer; line-height: 1; }
.modal-close:hover { color: var(--text); }
.modal-actions { display: flex; justify-content: space-between; gap: 10px; }
button.danger { background: none; border: 1px solid var(--danger); color: var(--danger); padding: 9px 16px; border-radius: 8px; cursor: pointer; font-weight: 600; }
button.danger:hover { background: var(--danger); color: white; }

.kb-results { display: flex; flex-direction: column; gap: 10px; }
.kb-result { background: var(--panel2); border-left: 3px solid var(--accent); padding: 10px 12px; border-radius: 6px; font-size: 13px; white-space: pre-wrap; }

.sys-info { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.sys-info dt { color: var(--muted); font-size: 12px; }
.sys-info dd { font-size: 13px; margin-bottom: 8px; }
.hint { margin-top: 10px; }
.hint code { background: var(--panel2); padding: 1px 6px; border-radius: 4px; }
.guide { margin-left: 20px; display: flex; flex-direction: column; gap: 8px; font-size: 14px; }
.guide em { color: var(--accent); font-style: normal; }

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel2);
  border: 1px solid var(--border);
  padding: 12px 20px;
  border-radius: 10px;
  font-size: 14px;
  z-index: 100;
  box-shadow: 0 4px 20px rgba(0,0,0,.5);
}
.toast.error { border-color: var(--danger); }
