:root {
  --bg: #f5f6f8;
  --panel: #ffffff;
  --border: #e3e5e8;
  --text: #1f2329;
  --text-muted: #8a919c;
  --primary: #4f6ef7;
  --primary-dark: #3d5ce0;
  --danger: #e5484d;
  --user-bubble: #4f6ef7;
  --ai-bubble: #ffffff;
  --radius: 10px;
  --shadow: 0 1px 4px rgba(0, 0, 0, 0.06);
}

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

html, body { height: 100%; }

body {
  font-family: "Segoe UI", "Microsoft YaHei", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

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

/* ---------- Sidebar ---------- */
#sidebar {
  width: 280px;
  min-width: 280px;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-right: 1px solid var(--border);
}

.sidebar-header {
  padding: 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-header h1 {
  font-size: 18px;
  margin-bottom: 12px;
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

.conversation-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  margin-bottom: 4px;
  border-radius: var(--radius);
  cursor: pointer;
  border: 1px solid transparent;
}

.conversation-item:hover { background: var(--bg); }

.conversation-item.active {
  background: #eef1fe;
  border-color: #d7defc;
}

.conversation-item .title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-size: 14px;
}

.conversation-item .del {
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 16px;
  padding: 2px 6px;
  border-radius: 6px;
  visibility: hidden;
}

.conversation-item:hover .del { visibility: visible; }
.conversation-item .del:hover { color: var(--danger); background: #fde8e8; }

.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ---------- Buttons ---------- */
.btn {
  border: none;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 14px;
  padding: 10px 14px;
  transition: background 0.15s, opacity 0.15s;
}

.btn.primary { background: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.ghost { background: transparent; color: var(--text); border: 1px solid var(--border); }
.btn.ghost:hover { background: var(--bg); }
.btn.danger { background: var(--danger); color: #fff; }
.btn.danger:hover { background: #d13b40; }
.btn.small { padding: 6px 10px; font-size: 13px; }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* ---------- Main ---------- */
#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#chat-header {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 20px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}

.chat-title-wrap { flex: 1; min-width: 0; }

#chat-title {
  width: 100%;
  border: none;
  outline: none;
  font-size: 16px;
  font-weight: 600;
  background: transparent;
  color: var(--text);
}

.provider-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--text-muted);
}

.provider-bar select {
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #fff;
  font-size: 13px;
  max-width: 180px;
}

#message-list {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ---------- Messages ---------- */
.message {
  display: flex;
  gap: 10px;
  max-width: 820px;
  width: 100%;
  margin: 0 auto;
}

.message .avatar {
  width: 34px;
  height: 34px;
  min-width: 34px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  color: #fff;
  font-weight: 600;
}

.message.user { flex-direction: row-reverse; }
.message.user .avatar { background: var(--user-bubble); }
.message.ai .avatar { background: #10a37f; }
.message.error .avatar { background: var(--danger); }

.message .bubble {
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--ai-bubble);
  box-shadow: var(--shadow);
  line-height: 1.65;
  font-size: 14px;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  flex: 1;
  min-width: 0;
}

.message.user .bubble { background: var(--user-bubble); color: #fff; }
.message.error .bubble { background: #fde8e8; color: #7a1f23; }

.bubble pre {
  background: #f4f5f7;
  padding: 10px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 13px;
}

.message.user .bubble pre { background: #3d5ce0; }

.bubble code {
  font-family: Consolas, "Courier New", monospace;
  font-size: 13px;
}

.message .meta {
  font-size: 12px;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.cursor {
  display: inline-block;
  width: 8px;
  height: 16px;
  background: currentColor;
  vertical-align: -2px;
  animation: blink 1s step-start infinite;
}

.bubble.stream-placeholder { color: var(--text-muted); }

@keyframes blink { 50% { opacity: 0; } }

/* ---------- Composer ---------- */
#composer {
  padding: 12px 20px 16px;
  background: var(--panel);
  border-top: 1px solid var(--border);
}

.composer-status {
  font-size: 12px;
  min-height: 18px;
  margin-bottom: 6px;
  color: var(--primary);
}

#message-input {
  width: 100%;
  max-height: 160px;
  resize: none;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 14px;
  font-family: inherit;
  outline: none;
  line-height: 1.5;
}

#message-input:focus { border-color: var(--primary); }

.composer-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 10px;
}

/* ---------- Modal ---------- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal.hidden { display: none; }

.modal-content {
  background: var(--panel);
  border-radius: 12px;
  width: min(860px, 94vw);
  max-height: 88vh;
  display: flex;
  flex-direction: column;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.2);
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 { font-size: 18px; }

.modal-close {
  border: none;
  background: transparent;
  font-size: 24px;
  cursor: pointer;
  color: var(--text-muted);
  line-height: 1;
}

.modal-body { padding: 20px; overflow-y: auto; }

.proxy-bar {
  background: #f0f4ff;
  border: 1px solid #d7defc;
  border-radius: var(--radius);
  padding: 12px 14px;
  margin-bottom: 18px;
}

.proxy-bar label {
  display: block;
  font-size: 13px;
  color: var(--text-muted);
}

.proxy-bar input {
  display: block;
  width: 100%;
  margin-top: 8px;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
  background: #fff;
}

.proxy-bar input:focus { border-color: var(--primary); outline: none; }

.proxy-bar .hint {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 8px;
  line-height: 1.6;
}

.proxy-bar code {
  font-family: Consolas, "Courier New", monospace;
  background: #e4e9fd;
  padding: 1px 5px;
  border-radius: 4px;
  font-size: 12px;
}

.settings-grid {
  display: grid;
  grid-template-columns: 280px 1fr;
  gap: 20px;
}

.settings-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.settings-toolbar h3 { font-size: 15px; }

.provider-list {
  list-style: none;
  max-height: 420px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.provider-list li {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}

.provider-list li:last-child { border-bottom: none; }
.provider-list li:hover { background: var(--bg); }
.provider-list li.active { background: #eef1fe; }

.provider-list .p-name { font-weight: 600; font-size: 14px; }
.provider-list .p-sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; }

#provider-form label {
  display: block;
  margin-bottom: 14px;
  font-size: 13px;
  color: var(--text-muted);
}

#provider-form input,
#provider-form select {
  display: block;
  width: 100%;
  margin-top: 6px;
  padding: 9px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  color: var(--text);
}

#provider-form input:focus,
#provider-form select:focus { border-color: var(--primary); outline: none; }

.form-actions { text-align: right; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: #333;
  color: #fff;
  padding: 10px 18px;
  border-radius: 8px;
  font-size: 14px;
  z-index: 200;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.25);
}

.toast.hidden { display: none; }
.toast.error { background: var(--danger); }

/* ---------- Empty state ---------- */
.empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-muted);
}

.empty-state .icon { font-size: 48px; margin-bottom: 12px; }
.empty-state p { font-size: 15px; line-height: 1.8; }

@media (max-width: 720px) {
  #sidebar { position: fixed; z-index: 50; height: 100%; transform: translateX(-100%); transition: transform 0.2s; }
  #sidebar.open { transform: translateX(0); }
  .settings-grid { grid-template-columns: 1fr; }
}
