:root {
  --bg: #eef2f7;
  --surface: #ffffff;
  --surface-soft: #f8fafc;
  --line: #dbe3ef;
  --line-strong: #cbd5e1;
  --text: #102033;
  --subtle: #64748b;
  --muted: #94a3b8;
  --primary: #2563eb;
  --primary-strong: #1d4ed8;
  --primary-soft: #eaf1ff;
  --success: #059669;
  --warning: #d97706;
  --danger: #dc2626;
  --shadow: 0 14px 34px rgba(15, 23, 42, 0.08);
  --radius: 8px;
}

* { box-sizing: border-box; }

html, body, #app {
  width: 100%;
  height: 100%;
  margin: 0;
}

body {
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Arial, sans-serif;
  color: var(--text);
  background: var(--bg);
  font-size: 14px;
  line-height: 1.5;
}

button, input, textarea, select {
  font: inherit;
}

button {
  cursor: pointer;
}

.hidden { display: none !important; }

.auth-screen {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    linear-gradient(135deg, rgba(37, 99, 235, 0.12), transparent 34%),
    linear-gradient(315deg, rgba(5, 150, 105, 0.10), transparent 32%),
    #f8fafc;
}

.auth-card {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: var(--shadow);
  padding: 32px;
}

.brand-mark {
  width: 44px;
  height: 44px;
  display: grid;
  place-items: center;
  border-radius: 10px;
  background: var(--primary);
  color: #fff;
  font-weight: 800;
  letter-spacing: 0;
}

.brand-mark.small {
  width: 34px;
  height: 34px;
  border-radius: 8px;
  font-size: 13px;
}

.auth-card h1 {
  margin: 18px 0 4px;
  font-size: 24px;
}

.auth-card p {
  margin: 0 0 24px;
  color: var(--subtle);
}

.form-stack,
.admin-grid {
  display: grid;
  gap: 14px;
}

label span,
.compact-label span {
  display: block;
  color: var(--subtle);
  font-size: 13px;
  margin-bottom: 6px;
}

input, textarea, select {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  color: var(--text);
  outline: none;
  padding: 10px 11px;
  transition: border-color .15s, box-shadow .15s, background .15s;
}

input:focus, textarea:focus, select:focus {
  border-color: rgba(37, 99, 235, .7);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}

.form-message {
  min-height: 20px;
  color: var(--danger);
}

.workspace {
  height: 100%;
  display: grid;
  grid-template-columns: 280px minmax(460px, 1fr) 360px;
  overflow: hidden;
}

.project-rail,
.project-editor,
.run-panel {
  min-height: 0;
}

.project-rail {
  background: #fbfdff;
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
}

.rail-brand {
  height: 72px;
  display: flex;
  align-items: center;
  gap: 11px;
  padding: 0 20px;
  border-bottom: 1px solid var(--line);
}

.rail-brand strong,
.rail-brand span {
  display: block;
}

.rail-brand span {
  color: var(--muted);
  font-size: 12px;
}

.project-tools {
  padding: 16px;
  display: grid;
  gap: 10px;
  border-bottom: 1px solid var(--line);
}

.search-box {
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface);
  padding: 0 10px;
}

.search-box span {
  color: var(--muted);
}

.search-box input {
  border: 0;
  box-shadow: none;
  padding-left: 0;
}

.project-list {
  flex: 1;
  overflow: auto;
  padding: 10px;
}

.project-item {
  width: 100%;
  text-align: left;
  display: grid;
  gap: 4px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  border-radius: 8px;
  padding: 12px;
}

.project-item:hover {
  background: var(--surface-soft);
}

.project-item.active {
  background: var(--primary-soft);
  border-color: rgba(37, 99, 235, .24);
  color: #173b86;
}

.project-item strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.project-item span,
.rail-empty {
  color: var(--subtle);
  font-size: 12px;
}

.rail-empty {
  padding: 24px 12px;
  text-align: center;
}

.rail-user {
  min-height: 76px;
  border-top: 1px solid var(--line);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: #1f2937;
  color: #fff;
  font-weight: 800;
}

.user-meta {
  min-width: 0;
  flex: 1;
}

.user-meta strong,
.user-meta span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.user-meta span {
  color: var(--subtle);
  font-size: 12px;
}

.project-editor {
  background: var(--surface);
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
}

.editor-header {
  min-height: 72px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 14px 22px;
  border-bottom: 1px solid var(--line);
  flex-shrink: 0;
}

.project-title-group {
  min-width: 0;
  flex: 1;
}

.project-name-input {
  width: 100%;
  border: 0;
  box-shadow: none;
  padding: 0;
  font-size: 22px;
  font-weight: 750;
  background: transparent;
}

.project-name-input:focus {
  box-shadow: none;
}

.project-subtitle {
  color: var(--muted);
  font-size: 12px;
  margin-top: 3px;
}

.header-actions,
.composer-actions,
.model-actions,
.detail-actions,
.history-actions,
.split-row,
.field-edit-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.editor-empty {
  flex: 1;
  display: grid;
  place-content: center;
  justify-items: center;
  color: var(--subtle);
  text-align: center;
  padding: 24px;
}

.editor-empty h2 {
  margin: 12px 0 4px;
  color: var(--text);
}

.empty-symbol {
  width: 58px;
  height: 58px;
  border: 1px solid var(--line);
  background: var(--surface-soft);
  color: var(--primary);
  border-radius: 14px;
  display: grid;
  place-items: center;
  font-size: 28px;
}

.editor-body {
  min-height: 0;
  flex: 1;
  display: flex;
  flex-direction: column;
  background: linear-gradient(#fff, #fff 62%, #f8fafc);
  overflow: hidden;
}

.editor-strip {
  display: grid;
  grid-template-columns: minmax(160px, .8fr) minmax(260px, 1.2fr);
  gap: 18px;
  padding: 16px 22px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-soft);
  flex-shrink: 0;
}

.eyebrow {
  display: block;
  margin-bottom: 8px;
  color: var(--subtle);
  font-size: 12px;
  font-weight: 700;
}

.chip-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 7px;
  min-height: 32px;
}

.chip {
  border: 1px solid rgba(37, 99, 235, .24);
  background: #fff;
  color: #1e40af;
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 12px;
  white-space: nowrap;
}

.chip.subtle {
  border-color: var(--line);
  color: var(--subtle);
}

.hint {
  color: var(--muted);
  font-size: 12px;
}

.inline-input {
  width: 132px;
  padding: 7px 9px;
  font-size: 13px;
}

.inline-input.wide {
  width: 220px;
}

.validation-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 22px;
  border-bottom: 1px solid rgba(217, 119, 6, .25);
  background: #fff7ed;
  flex-shrink: 0;
}

.validation-bar span {
  color: #9a3412;
  font-size: 12px;
  font-weight: 650;
}

.message-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 22px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overscroll-behavior: contain;
}

.message-empty {
  margin: auto;
  display: grid;
  gap: 6px;
  text-align: center;
  color: var(--subtle);
}

.message-card {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 8px;
  overflow: hidden;
  max-width: 860px;
  flex: 0 0 auto;
}

.message-card.editing {
  border-color: rgba(37, 99, 235, .65);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, .12);
}

.message-card.user {
  align-self: flex-end;
  width: min(82%, 820px);
}

.message-card.system,
.message-card.assistant {
  align-self: flex-start;
  width: min(82%, 820px);
}

.message-role {
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  color: var(--subtle);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
  background: var(--surface-soft);
}

.message-card.user .message-role {
  background: var(--primary-soft);
  color: #1d4ed8;
}

.message-card.assistant .message-role {
  background: #ecfdf5;
  color: #047857;
}

.message-body {
  width: 100%;
  display: block;
  min-height: 44px;
  border: 0;
  background: #fff;
  color: var(--text);
  text-align: left;
  padding: 13px 14px;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.55;
  cursor: text;
}

.message-body:hover {
  background: #fbfdff;
}

.inline-edit-wrap {
  display: grid;
  gap: 10px;
  padding: 12px;
  background: #fff;
}

.inline-editor {
  min-height: 160px;
  max-height: min(52vh, 520px);
  resize: vertical;
  overflow: auto;
  line-height: 1.55;
  white-space: pre-wrap;
}

.inline-edit-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.inline-image-cols {
  max-width: 260px;
  padding: 8px 10px;
  font-size: 13px;
}

.inline-edit-actions {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  flex-shrink: 0;
}

.message-actions {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
}

.message-actions button,
.history-actions button,
.history-actions a,
.admin-user button {
  border: 0;
  background: transparent;
  color: var(--primary);
  text-decoration: none;
  padding: 2px 5px;
}

.composer {
  border-top: 1px solid var(--line);
  padding: 14px 22px 18px;
  background: #fff;
  box-shadow: 0 -10px 24px rgba(15, 23, 42, .04);
  flex-shrink: 0;
}

.role-tabs {
  display: inline-flex;
  padding: 3px;
  background: var(--surface-soft);
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-bottom: 10px;
}

.role-tab {
  border: 0;
  background: transparent;
  border-radius: 6px;
  color: var(--subtle);
  padding: 6px 13px;
  font-weight: 700;
  font-size: 13px;
}

.role-tab.active {
  background: #fff;
  color: var(--primary);
  box-shadow: 0 1px 2px rgba(15, 23, 42, .08);
}

#message-input {
  min-height: 104px;
  resize: vertical;
}

.composer-actions {
  justify-content: space-between;
  margin-top: 10px;
}

.run-panel {
  background: #f8fafc;
  border-left: 1px solid var(--line);
  overflow: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.side-section {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  display: grid;
  gap: 12px;
}

.section-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.section-title h3 {
  margin: 0;
  font-size: 15px;
}

.template-summary {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.template-summary div {
  border: 1px solid var(--line);
  background: var(--surface-soft);
  border-radius: 7px;
  padding: 10px;
}

.template-summary span,
.template-summary strong {
  display: block;
}

.template-summary span {
  color: var(--subtle);
  font-size: 12px;
}

.template-summary strong {
  font-size: 24px;
  color: var(--primary);
}

.compact-label {
  display: grid;
  gap: 0;
}

.dropzone {
  position: relative;
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  min-height: 58px;
  display: grid;
  place-items: center;
  background: var(--surface-soft);
  color: var(--subtle);
}

.dropzone input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.split-row > * {
  flex: 1;
}

.check-row {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--subtle);
}

.check-row input {
  width: auto;
}

.model-editor {
  display: grid;
  gap: 8px;
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.model-actions {
  justify-content: flex-end;
}

/* 模型配置下拉框旁边的「编辑/删除」操作行 */
.model-row-actions {
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}
.model-row-actions .mini-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}
.model-row-actions .mini-btn.danger:not(:disabled) {
  color: var(--danger);
  border-color: #fecaca;
}
.model-row-actions .mini-btn.danger:not(:disabled):hover {
  background: #fff1f2;
}

.history-section {
  min-height: 180px;
  display: flex;
  flex-direction: column;
  /* 让历史区在侧栏里可滚动，而不是把整个页面撑高溢出 */
  max-height: 540px;
  overflow: hidden;
}

.task-history {
  display: grid;
  gap: 9px;
  /* 内容超出时在框内滚动 */
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
  padding-right: 2px;
}

/* 历史分页控件 */
.history-pager {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--line);
  font-size: 12px;
  color: var(--muted);
  flex-shrink: 0;
}
.history-pager button {
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 6px;
  padding: 3px 10px;
  cursor: pointer;
  color: var(--text);
  font-weight: 700;
}
.history-pager button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

.history-empty {
  padding: 18px;
  text-align: center;
  color: var(--muted);
  background: var(--surface-soft);
  border-radius: 7px;
}

.history-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
  display: grid;
  gap: 8px;
}

.history-top,
.history-meta {
  display: flex;
  justify-content: space-between;
  gap: 10px;
  align-items: center;
}

.history-top strong {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-meta {
  color: var(--subtle);
  font-size: 12px;
}

.progress {
  height: 6px;
  background: #e2e8f0;
  border-radius: 999px;
  overflow: hidden;
}

.progress span {
  display: block;
  height: 100%;
  background: var(--primary);
}

.history-actions {
  justify-content: flex-end;
}

/* 历史项里的删除按钮：红色文字，悬停浅红底，跟其它操作区分 */
.history-actions button.danger {
  color: var(--danger);
}
.history-actions button.danger:hover {
  background: #fff1f2;
}

.btn,
.mini-btn,
.icon-btn {
  border: 1px solid transparent;
  border-radius: 7px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  font-weight: 700;
  transition: background .15s, border-color .15s, color .15s, transform .15s;
}

.btn {
  min-height: 38px;
  padding: 8px 13px;
}

.btn-block {
  width: 100%;
}

.btn-primary {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.btn-primary:hover {
  background: var(--primary-strong);
}

.btn-secondary {
  background: #eef2ff;
  border-color: #c7d2fe;
  color: #3730a3;
}

.btn-secondary:hover {
  background: #e0e7ff;
}

.btn-ghost {
  background: transparent;
  border-color: var(--line);
  color: var(--subtle);
}

.btn-danger {
  background: #fef2f2;
  border-color: #fecaca;
  color: var(--danger);
}

.mini-btn {
  min-height: 30px;
  padding: 5px 9px;
  background: var(--surface-soft);
  border-color: var(--line);
  color: var(--subtle);
  font-size: 12px;
}

.icon-btn {
  width: 34px;
  height: 34px;
  background: #fff;
  border-color: var(--line);
  color: var(--subtle);
  flex: 0 0 auto;
}

.icon-btn.danger:hover {
  color: var(--danger);
  border-color: #fecaca;
  background: #fff7f7;
}

.pill,
.status {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 3px 8px;
  font-size: 12px;
  font-weight: 800;
  background: var(--surface-soft);
  color: var(--subtle);
  border: 1px solid var(--line);
}

.status.running { color: var(--primary); background: var(--primary-soft); border-color: #bfdbfe; }
.status.waiting { color: var(--warning); background: #fffbeb; border-color: #fde68a; }
.status.done { color: var(--success); background: #ecfdf5; border-color: #bbf7d0; }
.status.paused, .status.interrupted { color: #475569; background: #f1f5f9; }
.status.canceled, .status.failed { color: var(--danger); background: #fef2f2; border-color: #fecaca; }

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 24px;
}

.modal-overlay {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, .36);
  backdrop-filter: blur(2px);
}

.modal-card {
  position: relative;
  width: min(560px, 100%);
  max-height: min(86vh, 760px);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
}

.modal-wide {
  width: min(1120px, 100%);
}

.modal-header {
  min-height: 58px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.modal-header h3 {
  margin: 0;
}

.modal-close {
  border: 0;
  background: transparent;
  color: var(--subtle);
  font-size: 24px;
}

.test-grid {
  min-height: 520px;
  display: grid;
  grid-template-columns: 320px 1fr;
  overflow: hidden;
}

.test-form {
  border-right: 1px solid var(--line);
  background: var(--surface-soft);
  padding: 16px;
  overflow: auto;
  display: grid;
  align-content: start;
  gap: 12px;
}

.test-output {
  min-width: 0;
  display: flex;
  flex-direction: column;
  background: #0f172a;
  color: #e5e7eb;
}

.test-tabs {
  display: flex;
  background: #1e293b;
  border-bottom: 1px solid #334155;
}

.test-tab {
  border: 0;
  background: transparent;
  color: #94a3b8;
  padding: 12px 18px;
}

.test-tab.active {
  color: #dbeafe;
  background: rgba(37, 99, 235, .18);
}

.test-output pre,
.log-box {
  margin: 0;
  overflow: auto;
  white-space: pre-wrap;
  word-break: break-word;
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
}

.test-output pre {
  flex: 1;
  padding: 18px;
}

.detail-toolbar {
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
  padding: 12px 18px;
  border-bottom: 1px solid var(--line);
}

.detail-actions {
  margin-left: auto;
}

.log-box {
  min-height: 480px;
  background: #0f172a;
  color: #d1d5db;
  padding: 16px;
}

.admin-grid {
  padding: 18px;
  overflow: auto;
}

.admin-users {
  display: grid;
  gap: 8px;
  border-top: 1px solid var(--line);
  padding-top: 12px;
}

.admin-user {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px;
}

.admin-user strong,
.admin-user span {
  display: block;
}

.admin-user span {
  color: var(--muted);
  font-size: 12px;
}

#toast-container {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 80;
  display: grid;
  gap: 8px;
}

.toast {
  min-width: 220px;
  max-width: 380px;
  padding: 11px 13px;
  border-radius: 8px;
  background: #172033;
  color: #fff;
  box-shadow: var(--shadow);
  transition: opacity .25s, transform .25s;
}

.toast.error {
  background: #991b1b;
}

.toast.leaving {
  opacity: 0;
  transform: translateY(8px);
}

@media (max-width: 1180px) {
  .workspace {
    grid-template-columns: 240px minmax(420px, 1fr);
  }
  .run-panel {
    position: fixed;
    top: 72px;
    right: 0;
    bottom: 0;
    width: 360px;
    box-shadow: var(--shadow);
  }
}

@media (max-width: 860px) {
  html, body, #app {
    height: auto;
    min-height: 100%;
  }
  .workspace {
    min-height: 100vh;
    height: auto;
    display: block;
    overflow: visible;
  }
  .project-rail,
  .project-editor,
  .run-panel {
    width: 100%;
    border: 0;
  }
  .project-rail {
    height: auto;
  }
  .project-list {
    max-height: 220px;
  }
  .run-panel {
    position: static;
    box-shadow: none;
  }
  .editor-header,
  .editor-strip,
  .composer-actions,
  .detail-toolbar {
    flex-direction: column;
    align-items: stretch;
  }
  .editor-strip,
  .test-grid {
    grid-template-columns: 1fr;
  }
  .field-edit-row {
    flex-wrap: wrap;
    align-items: flex-start;
  }
  .field-edit-row .chip-row {
    flex: 1 1 100%;
  }
  .message-card.user,
  .message-card.system,
  .message-card.assistant {
    width: 100%;
  }
  .inline-input,
  .inline-input.wide {
    width: 100%;
  }
}
