*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f0f0f;
  --bg2: #1a1a1a;
  --bg3: #252525;
  --border: #2e2e2e;
  --text: #e8e8e8;
  --text2: #888;
  --accent: #7c6aff;
  --accent-hover: #9b8dff;
  --user-bubble: #1e1e2e;
  --ai-bubble: #1a1a1a;
  --danger: #ff4444;
  --radius: 12px;
  --sidebar-w: 260px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
}

/* ── Layout ── */
.chat-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
.chat-header select {
  min-width: 100px;
  max-width: 200px;
  padding: 6px;
  background: var(--bg3);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
}
.chat-header label {
  font-size: 13px;
  color: var(--text2);
}

.layout {
  display: flex;
  height: 100vh;
}

/* ── Sidebar ── */
.sidebar {
  overflow-y: auto;
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px;
  gap: 20px;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo {
  font-size: 18px;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: 0.5px;
}

.sidebar-header button {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  width: 32px;
  height: 32px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 18px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s;
}
.sidebar-header button:hover { background: var(--accent); border-color: var(--accent); }

.conversation-section {
  min-height: 100px;
  max-height: 34vh;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.conversation-heading {
  color: var(--text2);
  font-size: 12px;
  text-transform: uppercase;
}
.conversation-list {
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 3px;
}
.conversation-item {
  width: 100%;
  min-height: 36px;
  padding: 7px 9px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--text2);
  text-align: left;
  font: inherit;
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  cursor: pointer;
}
.conversation-item:hover { background: var(--bg3); color: var(--text); }
.conversation-item.active { border-color: var(--border); background: var(--bg3); color: var(--text); }

.sidebar-section {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.sidebar-section label {
  font-size: 12px;
  color: var(--text2);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.sidebar-section select,
.sidebar-section textarea {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s;
  resize: vertical;
}
.sidebar-section select:focus,
.sidebar-section textarea:focus { border-color: var(--accent); }

.sidebar-section input[type="file"] {
  color: var(--text2);
  font-size: 12px;
}

.sidebar-section input[type="file"]::file-selector-button {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 6px 10px;
  margin-right: 10px;
  cursor: pointer;
}

.secondary-btn {
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.secondary-btn:hover {
  background: var(--accent);
  border-color: var(--accent);
}

.sidebar-help {
  color: var(--text2);
  font-size: 12px;
  line-height: 1.5;
}

.sidebar-section input[type="range"] {
  accent-color: var(--accent);
  width: 100%;
  cursor: pointer;
}

.sidebar-footer {
  margin-top: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text2);
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.dot-ok  { background: #4caf50; box-shadow: 0 0 6px #4caf5066; }
.dot-err { background: var(--danger); box-shadow: 0 0 6px #ff444466; }
.dot-loading { background: #ffa726; box-shadow: 0 0 6px #ffa72666; }

/* ── Chat area ── */
.chat-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-header {
  min-height: 54px;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  gap: 10px;
  padding: 8px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
}
.chat-header label { color: var(--text2); font-size: 12px; }
.menu-btn { display: none; }
.chat-header select {
  width: min(220px, 48vw);
  min-width: 120px;
  height: 36px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg3);
  color: var(--text);
  font: inherit;
}
.chat-header select:focus { outline: 1px solid var(--accent); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 32px 16px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  scroll-behavior: smooth;
}

.messages::-webkit-scrollbar { width: 4px; }
.messages::-webkit-scrollbar-track { background: transparent; }
.messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* Welcome screen */
.welcome {
  text-align: center;
  margin: auto;
  color: var(--text2);
}
.welcome h2 { font-size: 24px; color: var(--text); margin-bottom: 8px; }
.welcome p  { font-size: 14px; }

/* Message row */
.msg {
  display: flex;
  flex-direction: column;
  max-width: 820px;
  width: 100%;
  margin: 0 auto;
  gap: 6px;
  animation: fade-in 0.2s ease;
}

@keyframes fade-in {
  from { opacity: 0; transform: translateY(6px); }
  to   { opacity: 1; transform: translateY(0); }
}

.msg-header {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 12px;
  color: var(--text2);
}

.msg-avatar {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  font-weight: 700;
  flex-shrink: 0;
}
.msg.user .msg-avatar { background: var(--accent); color: #fff; }
.msg.ai   .msg-avatar { background: var(--bg3); color: var(--text2); border: 1px solid var(--border); }

.msg-body {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  font-size: 14px;
  line-height: 1.7;
  word-break: break-word;
}

.msg.user .msg-body {
  background: var(--user-bubble);
  border-color: #2a2a4a;
}

.approval-panel {
  margin-top: 12px;
  padding: 12px;
  border: 1px solid var(--accent);
  border-radius: var(--radius);
  background: var(--bg3);
  display: grid;
  gap: 10px;
}
.approval-panel > strong { color: var(--text); }
.approval-tool { min-width: 0; }
.approval-tool pre {
  margin: 6px 0 0;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg);
  overflow-x: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font: 12px/1.5 "Fira Code", Consolas, monospace;
}
.approval-panel small {
  color: var(--text2);
  overflow-wrap: anywhere;
}
.parameter-panel fieldset { border: 1px solid var(--border); border-radius: 8px; margin: 12px 0; padding: 12px; }
.parameter-panel legend { overflow-wrap: anywhere; }
.parameter-field { display: grid; grid-template-columns: 1fr auto; gap: 6px; margin: 12px 0; }
.parameter-field > input:not([type="checkbox"]), .parameter-field > select, .parameter-field > textarea {
  grid-column: 1 / -1; width: 100%; min-width: 0; box-sizing: border-box;
  border: 1px solid var(--border); border-radius: 6px; padding: 8px;
  color: inherit; background: transparent; font: inherit;
}
.parameter-field > small { grid-column: 1 / -1; white-space: pre-wrap; overflow-wrap: anywhere; }
.parameter-field > textarea { resize: vertical; }
.parameter-error { color: #dc2626; white-space: pre-wrap; }

.approval-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.approval-actions button {
  min-height: 36px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--bg2);
  color: var(--text);
  cursor: pointer;
}
.approval-actions button:first-child {
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
}
.approval-actions button:disabled { opacity: 0.55; cursor: wait; }

/* Markdown 样式 */
.msg-body p  { margin-bottom: 0.7em; }
.msg-body p:last-child { margin-bottom: 0; }
.msg-body h1,.msg-body h2,.msg-body h3 { margin: 0.8em 0 0.4em; color: var(--text); }
.msg-body ul, .msg-body ol { padding-left: 1.4em; margin-bottom: 0.7em; }
.msg-body li { margin-bottom: 0.3em; }
.msg-body code {
  background: var(--bg3);
  border: 1px solid var(--border);
  padding: 1px 6px;
  border-radius: 4px;
  font-family: "Fira Code", Consolas, monospace;
  font-size: 13px;
}
.msg-body pre {
  background: #0d0d0d;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 14px;
  overflow-x: auto;
  margin: 0.7em 0;
  position: relative;
}
.msg-body pre code {
  background: none;
  border: none;
  padding: 0;
  font-size: 13px;
}
.msg-body blockquote {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  color: var(--text2);
  margin: 0.7em 0;
}
.msg-body table {
  border-collapse: collapse;
  width: 100%;
  margin: 0.7em 0;
  font-size: 13px;
}
.msg-body th, .msg-body td {
  border: 1px solid var(--border);
  padding: 7px 12px;
  text-align: left;
}
.msg-body th { background: var(--bg3); }
.msg-body a { color: var(--accent); }

/* Copy button on pre */
.pre-wrap { position: relative; }
.copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  background: var(--bg3);
  border: 1px solid var(--border);
  color: var(--text2);
  border-radius: 6px;
  padding: 2px 8px;
  font-size: 11px;
  cursor: pointer;
  opacity: 0;
  transition: opacity 0.15s;
}
.pre-wrap:hover .copy-btn { opacity: 1; }
.copy-btn:hover { color: var(--text); }

/* Cursor blink */
.cursor {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--accent);
  vertical-align: text-bottom;
  margin-left: 2px;
  animation: blink 0.8s step-end infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* ── Input bar ── */
.input-bar {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 16px 24px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.input-bar textarea {
  flex: 1;
  background: var(--bg2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 12px;
  padding: 12px 16px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  resize: none;
  max-height: 180px;
  overflow-y: auto;
  line-height: 1.5;
  transition: border-color 0.15s;
}
.input-bar textarea:focus { border-color: var(--accent); }
.input-bar textarea::placeholder { color: var(--text2); }

.input-bar button {
  background: var(--bg2);
  border: 1px solid var(--border);
  color: var(--text2);
  width: 42px;
  height: 42px;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, color 0.15s;
}

#btn-send {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
#btn-send:hover:not(:disabled) { background: var(--accent-hover); }
#btn-send:disabled { opacity: 0.4; cursor: not-allowed; }

#btn-clear:hover { background: var(--danger); border-color: var(--danger); color: #fff; }

/* ── Responsive ── */
@media (max-width: 640px) {
  .sidebar { display: none; }
  .sidebar.sidebar-open {
    position: absolute;
    inset: 0 auto 0 0;
    z-index: 5;
    display: flex;
    width: min(84vw, 300px);
    min-width: 0;
    box-shadow: 12px 0 32px #0008;
  }
  .menu-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--bg3);
    color: var(--text);
    cursor: pointer;
  }
  .chat-header { justify-content: flex-start; padding: 8px 12px; }
  .chat-header label { display: none; }
  .chat-header .secondary-btn { padding: 7px 9px; }
  .input-bar { padding: 12px; }
}


.agent-activity {
  margin-bottom: 12px;
  padding: 10px 12px;
  border: 1px solid var(--border, #444);
  border-radius: 8px;
  font-size: 0.85em;
}

.agent-activity summary { cursor: pointer; }
.agent-activity details + details { margin-top: 10px; }
.agent-activity-entry {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  margin-top: 8px;
  max-height: 300px;
  overflow-y: auto;
}


#kb-manager {
  width: min(1000px, 94vw);
  max-height: 90vh;
  overflow: auto;
  color: var(--text);
  background: var(--bg2, #222);
  border: 1px solid var(--border, #555);
  border-radius: 10px;
  padding: 20px;
}
#kb-manager::backdrop { background: #0009; }
.kb-manager-header { display: flex; justify-content: space-between; align-items: center; }
#kb-manager section { margin: 20px 0; }
#kb-search-form { display: flex; flex-wrap: wrap; gap: 12px; }
#kb-query { width: min(400px, 70vw); }
#kb-top-k, #kb-min-score { width: 75px; }
.kb-document-row, .kb-candidate {
  border: 1px solid var(--border, #555);
  padding: 12px; margin: 8px 0; border-radius: 6px;
}
.kb-document-row button { margin: 8px 8px 0 0; }
.kb-candidate pre { white-space: pre-wrap; overflow-wrap: anywhere; max-height: 300px; overflow: auto; }
.kb-rejected { opacity: 0.75; }
