:root {
  color-scheme: light;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #f4f6f8;
  color: #111827;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  height: 100vh;
}

button,
textarea {
  font: inherit;
}

.app {
  display: grid;
  grid-template-columns: 264px 1fr 280px;
  height: 100vh;
}

/* ---------- sidebar ---------- */

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  background: #0f172a;
  color: #e2e8f0;
  overflow: hidden;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.sidebar-head h1 {
  margin: 0;
  font-size: 18px;
  font-weight: 750;
}

.new-chat {
  width: 100%;
  min-height: 38px;
  border: 1px solid #334155;
  border-radius: 8px;
  background: #1d4ed8;
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}

.new-chat:hover {
  background: #2563eb;
}

button.ghost {
  border: 1px solid #334155;
  border-radius: 7px;
  background: transparent;
  color: #cbd5e1;
  padding: 5px 9px;
  cursor: pointer;
  font-size: 12px;
}

button.ghost:hover {
  background: #1e293b;
}

.conv-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.conv-group {
  margin-top: 12px;
  margin-bottom: 4px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: #94a3b8;
  text-transform: uppercase;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 9px;
  border-radius: 8px;
  cursor: pointer;
  color: #cbd5e1;
}

.conv-item:hover {
  background: #1e293b;
}

.conv-item.active {
  background: #1e3a8a;
  color: #fff;
}

.conv-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13.5px;
}

.conv-item .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #22c55e;
  flex: 0 0 auto;
}

.conv-del {
  border: none;
  background: transparent;
  color: #64748b;
  cursor: pointer;
  font-size: 16px;
  line-height: 1;
  opacity: 0;
}

.conv-item:hover .conv-del {
  opacity: 1;
}

.conv-del:hover {
  color: #f87171;
}

/* ---------- chat ---------- */

.chat {
  display: grid;
  grid-template-rows: auto 1fr auto;
  background: #f8fafc;
  overflow: hidden;
}

.chat-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px 20px;
  border-bottom: 1px solid #e5e7eb;
  background: #fff;
}

.chat-head #chat-title {
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.run-state {
  color: #6b7280;
  font-size: 13px;
  font-weight: 650;
}

.messages {
  overflow-y: auto;
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.msg {
  display: flex;
  gap: 10px;
  align-items: flex-start;
}

.msg-user {
  justify-content: flex-end;
}

.msg-ava {
  flex: 0 0 36px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  background: #f1f5f9;
  margin-top: 20px;
}

.msg-ava img {
  width: 36px;
  height: 36px;
  object-fit: cover;
}

.msg-user .msg-ava {
  background: #2563eb;
  color: #fff;
}

.msg-content {
  max-width: 80%;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.msg-user .msg-content {
  align-items: flex-end;
}

.msg-meta {
  font-size: 11px;
  color: #94a3b8;
  padding: 0 4px;
}

.msg-thinking {
  text-align: center;
  font-size: 12px;
  color: #94a3b8;
  padding: 6px;
}

/* persistent "Agent 正在运行中…" indicator */
.msg-thinking.running {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: #6366f1;
  font-weight: 500;
}
.run-dots { display: inline-flex; gap: 4px; }
.run-dots i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: #6366f1;
  display: inline-block;
  animation: run-bounce 1.2s infinite ease-in-out both;
}
.run-dots i:nth-child(2) { animation-delay: 0.16s; }
.run-dots i:nth-child(3) { animation-delay: 0.32s; }
@keyframes run-bounce {
  0%, 80%, 100% { opacity: 0.25; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-4px); }
}

.msg-body {
  max-width: 100%;
  padding: 10px 14px;
  border-radius: 12px;
  line-height: 1.55;
  white-space: pre-wrap;
  word-break: break-word;
}

.msg-user .msg-body {
  background: #2563eb;
  color: #fff;
  border-bottom-right-radius: 4px;
}

.msg-assistant .msg-body {
  background: #fff;
  border: 1px solid #e5e7eb;
  border-bottom-left-radius: 4px;
}

.msg-command .msg-body,
.msg-file .msg-body {
  background: #0f172a;
  color: #dbeafe;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 12.5px;
  max-width: min(720px, 90%);
}

.msg-file .msg-body {
  background: #1e293b;
  color: #bbf7d0;
}

.msg-error .msg-body {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: #991b1b;
}

.msg-stderr {
  justify-content: center;
}

.msg-stderr .msg-body {
  background: transparent;
  color: #94a3b8;
  font-size: 12px;
  max-width: 100%;
}

.msg-system {
  justify-content: center;
}

.msg-system .msg-body {
  background: #eef2ff;
  color: #4338ca;
  font-size: 12.5px;
  border-radius: 999px;
  padding: 4px 14px;
}

.msg-assistant .msg-body h1,
.msg-assistant .msg-body h2,
.msg-assistant .msg-body h3,
.msg-assistant .msg-body h4 {
  margin: 10px 0 4px;
  line-height: 1.3;
}

.msg-assistant .msg-body h1 { font-size: 18px; }
.msg-assistant .msg-body h2 { font-size: 16px; }
.msg-assistant .msg-body h3 { font-size: 14.5px; }
.msg-assistant .msg-body h4 { font-size: 13.5px; }
.msg-assistant .msg-body p { margin: 5px 0; }

.msg-assistant .msg-body ul {
  margin: 5px 0;
  padding-left: 20px;
}

.msg-assistant .msg-body li { margin: 2px 0; }

.msg-assistant .msg-body code {
  background: #eef2f7;
  padding: 1px 5px;
  border-radius: 4px;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 12.5px;
}

.msg-assistant .msg-body pre {
  background: #0f172a;
  color: #dbeafe;
  padding: 10px 12px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 6px 0;
}

.msg-assistant .msg-body pre code {
  background: none;
  padding: 0;
  color: inherit;
}

.msg-assistant .msg-body > :first-child { margin-top: 0; }
.msg-assistant .msg-body > :last-child { margin-bottom: 0; }

.composer {
  display: grid;
  gap: 10px;
  padding: 14px 20px;
  border-top: 1px solid #e5e7eb;
  background: #fff;
}

textarea {
  width: 100%;
  resize: vertical;
  min-height: 76px;
  padding: 12px 14px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  outline: none;
  line-height: 1.55;
  background: #fbfdff;
}

textarea:focus {
  border-color: #2563eb;
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.14);
}

.actions {
  display: flex;
  gap: 10px;
}

#send {
  min-height: 40px;
  padding: 0 18px;
  border: 1px solid #1d4ed8;
  border-radius: 8px;
  background: #2563eb;
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}

#send:hover:not(:disabled) {
  background: #1d4ed8;
}

button.danger {
  min-height: 40px;
  padding: 0 16px;
  border: 1px solid #b91c1c;
  border-radius: 8px;
  background: #dc2626;
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.55;
}

/* ---------- right panel ---------- */

.panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px;
  border-left: 1px solid #e5e7eb;
  background: #fff;
}

.status {
  padding: 8px 12px;
  border: 1px solid #c8ead3;
  border-radius: 8px;
  background: #ecfdf3;
  color: #166534;
  font-size: 13px;
  font-weight: 650;
  text-align: center;
}

.status.warning {
  border-color: #fed7aa;
  background: #fff7ed;
  color: #9a3412;
}

.byok-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 6px 0;
}

.byok-dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #9ca3af; /* 未填：灰 */
  flex: none;
}

.byok-dot.filled { background: #2563eb; } /* 已填：中性蓝 */
.byok-dot.ok { background: #16a34a; }      /* 首次工作流成功：绿 */
.byok-dot.bad { background: #dc2626; }     /* 401：红 */
.byok-dot.upstream { background: #ea580c; } /* 503：橙 */

.byok-state {
  font-size: 13px;
  font-weight: 600;
}

.byok-input {
  width: 100%;
  box-sizing: border-box;
  margin: 6px 0;
}

.byok-actions {
  display: flex;
  gap: 8px;
}

.byok-hint {
  font-size: 11px;
  color: #6b7280;
  margin: 8px 0 0;
}

.conv-item.disabled {
  opacity: 0.45;
  pointer-events: none;
}

/* 仍允许删除被禁用的 chat 会话 */
.conv-item.disabled .conv-del {
  pointer-events: auto;
}

.panel-card {
  border: 1px solid #e5e7eb;
  border-radius: 10px;
  padding: 14px;
}

.panel-card h2 {
  margin: 0 0 8px;
  font-size: 14px;
}

.muted {
  margin: 0;
  color: #6b7280;
  font-size: 12.5px;
  line-height: 1.6;
}

.wf-search {
  width: 100%;
  padding: 7px 9px;
  border: 1px solid #cbd5e1;
  border-radius: 7px;
  font-size: 13px;
  outline: none;
  background: #f8fafc;
  margin-bottom: 8px;
}

.wf-search:focus {
  border-color: #2563eb;
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.1);
}

.wf-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 380px;
  overflow-y: auto;
}

.wf-group {
  font-size: 11px;
  font-weight: 700;
  color: #64748b;
  letter-spacing: 0.04em;
  padding: 8px 0 2px;
  text-transform: uppercase;
}

.wf-btn {
  text-align: left;
  padding: 8px 10px;
  border: 1px solid #cbd5e1;
  border-radius: 8px;
  background: #f8fafc;
  color: #1f2937;
  font-weight: 650;
  cursor: pointer;
  font-size: 13px;
}

.wf-btn:hover {
  background: #eef2ff;
  border-color: #2563eb;
}

.wf-btn-auto {
  border-color: #2563eb;
  background: #eef2ff;
  font-weight: 750;
}

.artifact-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.artifact-link {
  color: #2563eb;
  font-size: 13px;
  text-decoration: none;
  word-break: break-all;
}

.artifact-link:hover {
  text-decoration: underline;
}

#config-form {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#config-form label {
  font-size: 12px;
  font-weight: 700;
  color: #374151;
}

#config-form select,
#config-form textarea {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid #cbd5e1;
  border-radius: 7px;
  font: inherit;
  background: #f8fafc;
  outline: none;
}

.up-input {
  font-size: 12px;
  padding: 4px 0;
}

#config-form select:focus,
#config-form textarea:focus,
.up-input:focus {
  border-color: #2563eb;
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.12);
}

#config-form textarea {
  resize: vertical;
  min-height: 80px;
}

.config-actions {
  display: flex;
  gap: 8px;
}

#config-start {
  flex: 1;
  min-height: 36px;
  border: 1px solid #1d4ed8;
  border-radius: 7px;
  background: #2563eb;
  color: #fff;
  font-weight: 700;
  cursor: pointer;
}

#config-start:hover {
  background: #1d4ed8;
}

#config-cancel {
  min-height: 36px;
  padding: 0 10px;
  border: 1px solid #cbd5e1;
  border-radius: 7px;
  background: #f8fafc;
  color: #1f2937;
  font-size: 12px;
  cursor: pointer;
}

#config-cancel:hover {
  background: #eef2ff;
}

.msg-file .artifact-link {
  color: #bbf7d0;
}

/* ---------- 审批卡（交互式规划） ---------- */
.approval-card {
  display: block;
  width: min(720px, 90%);
  align-self: flex-start;
  box-sizing: border-box;
  background: #fff;
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 12px 14px;
  color: #111827;
  line-height: 1.55;
}
.approval-card .appr-title {
  font-weight: 600;
  margin-bottom: 4px;
}
.approval-card .appr-summary {
  color: #4b5563;
  margin-bottom: 8px;
}
.approval-card .appr-items {
  margin: 0 0 8px 0;
  padding-left: 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.approval-card .appr-item-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  cursor: pointer;
}
.approval-card .appr-item-title {
  font-weight: 600;
}
.approval-card .appr-item-toggle {
  flex: none;
  color: #2563eb;
  font-size: 0.85em;
  white-space: nowrap;
  user-select: none;
}
.approval-card li.appr-has-note .appr-item-toggle {
  color: #16a34a;
}
.approval-card .appr-item-detail {
  color: #6b7280;
  font-size: 0.92em;
}
.approval-card .appr-item-edit[hidden] {
  display: none;
}
.approval-card textarea {
  width: 100%;
  box-sizing: border-box;
  margin-top: 4px;
  border: 1px solid #d1d5db;
  border-radius: 7px;
  padding: 6px 8px;
  font: inherit;
  resize: vertical;
}
.approval-card .appr-comment {
  min-height: 40px;
  margin-bottom: 8px;
}
.approval-card .appr-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.approval-card .appr-countdown {
  margin-right: auto;
  color: #6b7280;
  font-size: 0.9em;
}
.approval-card .appr-approve,
.approval-card .appr-revise {
  border-radius: 7px;
  padding: 6px 12px;
  cursor: pointer;
}
.approval-card .appr-approve {
  background: #2563eb;
  color: #fff;
  border: 1px solid #1d4ed8;
}
.approval-card .appr-approve:hover:not(:disabled) {
  background: #1d4ed8;
}
.approval-card .appr-revise {
  background: transparent;
  color: #111827;
  border: 1px solid #d1d5db;
}
.approval-card .appr-revise:hover:not(:disabled) {
  background: #f1f5f9;
}
.approval-card button:disabled,
.approval-card textarea:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

@media (max-width: 900px) {
  .app {
    grid-template-columns: 220px 1fr;
  }
  .panel {
    display: none;
  }
}
