:root {
  --bg-primary: #f5f5f5;
  --bg-sidebar: #f0f0f0;
  --bg-main: #ffffff;
  --bg-user-bubble: #3b82f6;
  --bg-ai-bubble: #f3f4f6;
  --text-primary: #1f2937;
  --text-secondary: #6b7280;
  --text-user-bubble: #ffffff;
  --border-color: #e5e7eb;
  --hover-bg: #e8e8e8;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --danger: #ef4444;
  --sidebar-width: 280px;
  --topbar-height: 52px;
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans SC", sans-serif;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --transition: 0.2s ease;
}

* { margin: 0; padding: 0; box-sizing: border-box; }
body {
  font-family: var(--font-family);
  background: var(--bg-primary);
  color: var(--text-primary);
  height: 100vh; width: 100vw;
  overflow: hidden;
}

.app { display: flex; height: 100vh; }

/* ===== Sidebar ===== */
.sidebar {
  width: var(--sidebar-width);
  min-width: var(--sidebar-width);
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border-color);
  display: flex; flex-direction: column;
  height: 100vh;
}
.sidebar-header { padding: 14px 14px 10px; }
.brand { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.brand-logo { font-size: 22px; }
.brand-name { font-size: 18px; font-weight: 600; letter-spacing: 1px; }
.new-chat-btn {
  width: 100%; padding: 9px 12px; border: none; border-radius: 10px;
  background: #fff; color: var(--text-primary); font-size: 14px; font-weight: 500;
  cursor: pointer; box-shadow: var(--shadow); transition: var(--transition);
  display: flex; align-items: center; justify-content: center; gap: 6px;
}
.new-chat-btn:hover { background: #fafafa; transform: translateY(-1px); }
.new-chat-btn .plus { font-size: 16px; color: var(--accent); }

.agent-picker { padding: 4px 14px 10px; }
.field-label { font-size: 12px; color: var(--text-secondary); display: block; margin-bottom: 5px; }
.field-label.inline { display: inline; margin: 0 6px 0 0; }

.select {
  width: 100%; padding: 8px 10px; border: 1px solid var(--border-color);
  border-radius: 9px; background: #fff; font-size: 13px; color: var(--text-primary);
  cursor: pointer; outline: none; transition: var(--transition);
}
.select:focus { border-color: var(--accent); }

.conv-list-wrap { flex: 1; overflow-y: auto; padding: 6px 10px; }
.conv-list-header { font-size: 12px; color: var(--text-secondary); padding: 6px 4px; }
.conv-list { list-style: none; }
.conv-item {
  display: flex; align-items: center; gap: 8px; padding: 9px 10px; border-radius: 9px;
  cursor: pointer; margin-bottom: 2px; transition: var(--transition); position: relative;
}
.conv-item:hover { background: var(--hover-bg); }
.conv-item.active { background: #e3e3e3; }
.conv-item .conv-name {
  flex: 1; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.conv-item .conv-del {
  opacity: 0; color: var(--text-secondary); font-size: 13px; padding: 2px 4px; border-radius: 6px;
}
.conv-item:hover .conv-del { opacity: 1; }
.conv-item .conv-del:hover { color: var(--danger); background: rgba(239,68,68,0.1); }

.sidebar-footer {
  padding: 10px 14px; font-size: 11px; color: var(--text-secondary);
  border-top: 1px solid var(--border-color); display: flex; align-items: center; gap: 6px;
}
.sidebar-footer .dot { width: 7px; height: 7px; border-radius: 50%; background: #22c55e; }

/* ===== Main ===== */
.main { flex: 1; display: flex; flex-direction: column; background: var(--bg-main); min-width: 0; }
.topbar {
  height: var(--topbar-height); min-height: var(--topbar-height);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 18px; border-bottom: 1px solid var(--border-color);
}
.topbar-title { font-size: 15px; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar-right { display: flex; align-items: center; }
.model-select { width: 220px; }

.chat-area { flex: 1; overflow-y: auto; position: relative; }
.welcome {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; text-align: center; color: var(--text-secondary);
}
.welcome-logo { font-size: 56px; margin-bottom: 12px; }
.welcome h1 { font-size: 26px; color: var(--text-primary); margin-bottom: 8px; }
.welcome p { font-size: 14px; }

.messages { max-width: 820px; margin: 0 auto; padding: 24px 20px 40px; }
.msg { display: flex; margin-bottom: 18px; gap: 12px; }
.msg.user { flex-direction: row-reverse; }
.msg .avatar {
  width: 34px; height: 34px; min-width: 34px; border-radius: 8px; display: flex;
  align-items: center; justify-content: center; font-size: 18px; background: #e5e7eb;
}
.msg.user .avatar { background: var(--accent); color: #fff; }
.msg .bubble {
  max-width: 72%; padding: 11px 14px; border-radius: 14px; font-size: 14px; line-height: 1.7;
  white-space: pre-wrap; word-break: break-word;
}
.msg.assistant .bubble { background: var(--bg-ai-bubble); border-top-left-radius: 4px; }
.msg.user .bubble { background: var(--bg-user-bubble); color: var(--text-user-bubble); border-top-right-radius: 4px; }
.msg .bubble.thinking { color: var(--text-secondary); font-style: italic; }
.typing span { display: inline-block; animation: blink 1.2s infinite; }
.typing span:nth-child(2) { animation-delay: 0.2s; }
.typing span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%,100% { opacity: 0.2; } 50% { opacity: 1; } }

/* ===== Composer ===== */
.composer { border-top: 1px solid var(--border-color); padding: 12px 18px 16px; background: var(--bg-main); }
.kb-bar { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 8px; }
.kb-chip {
  display: inline-flex; align-items: center; gap: 5px; font-size: 12px;
  background: #eff6ff; color: var(--accent); padding: 4px 8px; border-radius: 20px;
}
.kb-chip .x { cursor: pointer; font-weight: 700; }
.composer-row { display: flex; align-items: flex-end; gap: 10px; max-width: 820px; margin: 0 auto; }
.icon-btn {
  width: 38px; height: 38px; min-width: 38px; border: 1px solid var(--border-color);
  border-radius: 10px; background: #fff; cursor: pointer; font-size: 16px; transition: var(--transition);
}
.icon-btn:hover { background: #f5f5f5; }
.input {
  flex: 1; resize: none; border: 1px solid var(--border-color); border-radius: 12px;
  padding: 10px 14px; font-size: 14px; font-family: inherit; line-height: 1.5; outline: none;
  max-height: 160px; transition: var(--transition);
}
.input:focus { border-color: var(--accent); }
.send-btn {
  width: 38px; height: 38px; min-width: 38px; border: none; border-radius: 10px;
  background: var(--accent); color: #fff; font-size: 18px; cursor: pointer; transition: var(--transition);
}
.send-btn:hover { background: var(--accent-hover); }
.send-btn:disabled { background: #cbd5e1; cursor: not-allowed; }

/* ===== Modal ===== */
.modal { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; z-index: 50; }
.modal.hidden { display: none; }
.modal-mask { position: absolute; inset: 0; background: rgba(0,0,0,0.35); }
.modal-panel {
  position: relative; width: 520px; max-width: 92vw; max-height: 80vh; background: #fff;
  border-radius: 16px; box-shadow: 0 12px 40px rgba(0,0,0,0.2); display: flex; flex-direction: column;
}
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border-color); }
.modal-head h2 { font-size: 16px; }
.modal-body { padding: 18px 20px; overflow-y: auto; }
.upload-zone { display: flex; align-items: center; gap: 12px; padding: 14px; border: 1px dashed var(--border-color); border-radius: 12px; margin-bottom: 16px; }
.upload-btn { padding: 8px 14px; border: 1px solid var(--accent); color: var(--accent); background: #fff; border-radius: 9px; cursor: pointer; font-size: 13px; }
.upload-btn:hover { background: #eff6ff; }
.upload-hint { font-size: 12px; color: var(--text-secondary); }
.kb-list { list-style: none; }
.kb-item { display: flex; align-items: center; gap: 10px; padding: 10px 8px; border-bottom: 1px solid var(--border-color); }
.kb-item .kb-name { flex: 1; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.kb-item .kb-meta { font-size: 11px; color: var(--text-secondary); }
.kb-item .kb-act { font-size: 12px; cursor: pointer; padding: 4px 8px; border-radius: 7px; }
.kb-item .kb-act.attach { color: var(--accent); }
.kb-item .kb-act.attach.on { background: var(--accent); color: #fff; }
.kb-item .kb-act.del { color: var(--danger); }

.empty { text-align: center; color: var(--text-secondary); font-size: 13px; padding: 20px; }

/* ===== Sidebar footer button ===== */
.footer-btn {
  width: 100%; padding: 7px 10px; border: 1px solid var(--border-color); border-radius: 9px;
  background: #fff; color: var(--text-primary); font-size: 12px; cursor: pointer; transition: var(--transition);
  display: flex; align-items: center; justify-content: center; gap: 6px; margin-bottom: 8px;
}
.footer-btn:hover { background: #fafafa; border-color: var(--accent); color: var(--accent); }

/* ===== API settings form ===== */
.prov-form { display: flex; flex-direction: column; gap: 6px; margin-bottom: 18px; }
.prov-form label { font-size: 12px; color: var(--text-secondary); margin-top: 4px; }
.text-input {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border-color); border-radius: 9px;
  font-size: 13px; font-family: inherit; color: var(--text-primary); outline: none; transition: var(--transition);
  background: #fff;
}
.text-input:focus { border-color: var(--accent); }
.prov-form-actions { display: flex; gap: 10px; margin-top: 8px; }
.ghost-btn { padding: 8px 14px; border: 1px solid var(--border-color); background: #fff; color: var(--text-secondary); border-radius: 9px; cursor: pointer; font-size: 13px; }
.ghost-btn:hover { background: #f5f5f5; }
.prov-tip { font-size: 12px; color: var(--text-secondary); margin-top: 10px; line-height: 1.5; }
