:root {
  color-scheme: light dark;
  --bg: #f7f7f8;
  --bg-elevated: #ffffff;
  --bg-subtle: #ededf0;
  --bg-hover: #e5e6ea;
  --bg-user-msg: #18181b;
  --fg-user-msg: #fafafa;
  --fg: #111114;
  --fg-muted: #5e6068;
  --fg-subtle: #888a94;
  --border: #e1e2e6;
  --border-strong: #c8cad2;
  --accent: #111114;
  --accent-fg: #ffffff;
  --accent-hover: #27272c;
  --code-bg: #f1f2f5;
  --code-header: #e6e8ed;
  --danger: #dc2626;
  --danger-bg: rgba(220, 38, 38, 0.1);
  --queued: #b45309;
  --queued-bg: rgba(217, 119, 6, 0.12);
  --running: #1d4ed8;
  --running-bg: rgba(37, 99, 235, 0.12);
  --succeeded: #047857;
  --succeeded-bg: rgba(5, 150, 105, 0.12);
  --interrupted: #6b7280;
  --interrupted-bg: rgba(107, 114, 128, 0.14);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-md: 0 12px 32px rgba(0, 0, 0, 0.08);
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 18px;
  --font-sans: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Segoe UI", Roboto, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0b0b0e;
    --bg-elevated: #14151a;
    --bg-subtle: #1c1e25;
    --bg-hover: #252832;
    --bg-user-msg: #f4f4f6;
    --fg-user-msg: #0b0b0e;
    --fg: #f2f2f5;
    --fg-muted: #a1a4b0;
    --fg-subtle: #707482;
    --border: #262832;
    --border-strong: #393c4a;
    --accent: #f4f4f6;
    --accent-fg: #0b0b0e;
    --accent-hover: #dcdce2;
    --code-bg: #101116;
    --code-header: #1a1c24;
    --danger: #f87171;
    --danger-bg: rgba(248, 113, 113, 0.14);
    --queued: #fbbf24;
    --queued-bg: rgba(251, 191, 36, 0.14);
    --running: #60a5fa;
    --running-bg: rgba(96, 165, 250, 0.14);
    --succeeded: #34d399;
    --succeeded-bg: rgba(52, 211, 153, 0.14);
    --interrupted: #9ca3af;
    --interrupted-bg: rgba(156, 163, 175, 0.16);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 16px 40px rgba(0, 0, 0, 0.45);
  }
}

:root[data-theme="dark"] {
  --bg: #0b0b0e;
  --bg-elevated: #14151a;
  --bg-subtle: #1c1e25;
  --bg-hover: #252832;
  --bg-user-msg: #f4f4f6;
  --fg-user-msg: #0b0b0e;
  --fg: #f2f2f5;
  --fg-muted: #a1a4b0;
  --fg-subtle: #707482;
  --border: #262832;
  --border-strong: #393c4a;
  --accent: #f4f4f6;
  --accent-fg: #0b0b0e;
  --accent-hover: #dcdce2;
  --code-bg: #101116;
  --code-header: #1a1c24;
  --danger: #f87171;
  --danger-bg: rgba(248, 113, 113, 0.14);
  --queued: #fbbf24;
  --queued-bg: rgba(251, 191, 36, 0.14);
  --running: #60a5fa;
  --running-bg: rgba(96, 165, 250, 0.14);
  --succeeded: #34d399;
  --succeeded-bg: rgba(52, 211, 153, 0.14);
  --interrupted: #9ca3af;
  --interrupted-bg: rgba(156, 163, 175, 0.16);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 16px 40px rgba(0, 0, 0, 0.45);
}

[hidden] {
  display: none !important;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

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

button {
  cursor: pointer;
}

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

/* ── 按钮与输入 ─────────────────────────────────── */

.btn-primary {
  border: 1px solid transparent;
  background: var(--accent);
  color: var(--accent-fg);
  border-radius: var(--radius-sm);
  padding: 9px 16px;
  font-weight: 600;
  font-size: 14px;
  transition: background-color 0.15s ease, opacity 0.15s ease;
}

.btn-primary:hover:not(:disabled) {
  background: var(--accent-hover);
}

.btn-secondary {
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--fg);
  border-radius: var(--radius-sm);
  padding: 9px 14px;
  font-weight: 500;
  font-size: 14px;
  transition: background-color 0.15s ease, border-color 0.15s ease;
}

.btn-secondary:hover:not(:disabled) {
  background: var(--bg-subtle);
  border-color: var(--border-strong);
}

.btn-ghost {
  border: 1px solid transparent;
  background: transparent;
  color: var(--fg-muted);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  font-size: 13px;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.btn-ghost:hover:not(:disabled) {
  background: var(--bg-subtle);
  color: var(--fg);
}

.btn-compact {
  padding: 5px 10px;
  font-size: 13px;
}

.btn-block {
  width: 100%;
}

.btn-icon-text {
  border: none;
  background: transparent;
  color: var(--fg-subtle);
  padding: 2px 6px;
  font-size: 12px;
  border-radius: 6px;
}

.btn-icon-text:hover:not(:disabled) {
  background: var(--bg-subtle);
  color: var(--fg);
}

.btn-danger-text:hover:not(:disabled) {
  color: var(--danger);
  background: var(--danger-bg);
}

.text-input {
  width: 100%;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--fg);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.15s ease;
}

.text-input:focus {
  border-color: var(--border-strong);
}

/* ── 登录遮罩 ───────────────────────────────────── */

.auth-overlay {
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg);
}

.auth-card {
  width: 100%;
  max-width: 420px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 28px;
  box-shadow: var(--shadow-md);
}

.auth-header {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 22px;
}

.brand-mark {
  width: 40px;
  height: 40px;
  flex-shrink: 0;
  border-radius: 11px;
  background: var(--accent);
  color: var(--accent-fg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 18px;
  letter-spacing: -0.02em;
}

.auth-title {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
  line-height: 1.3;
}

.auth-subtitle {
  margin: 3px 0 0;
  font-size: 13px;
  color: var(--fg-muted);
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.field-label {
  font-size: 13px;
  font-weight: 500;
  color: var(--fg-muted);
}

.token-field {
  display: flex;
  align-items: center;
  gap: 8px;
}

.form-error {
  margin: 0;
  padding: 9px 12px;
  border-radius: var(--radius-sm);
  background: var(--danger-bg);
  color: var(--danger);
  font-size: 13px;
  line-height: 1.45;
}

.auth-footer {
  margin-top: 18px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.health-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--fg-muted);
}

.health-badge::before {
  content: "";
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--fg-subtle);
}

.health-badge[data-state="ok"]::before {
  background: var(--succeeded);
}

.health-badge[data-state="error"]::before {
  background: var(--danger);
}

.auth-note {
  font-size: 12px;
  color: var(--fg-subtle);
}

/* ── 主工作台布局 ───────────────────────────────── */

.workspace {
  height: 100vh;
  height: 100dvh;
  display: flex;
  overflow: hidden;
  background: var(--bg);
}

.sidebar {
  width: 268px;
  flex-shrink: 0;
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100%;
  z-index: 30;
}

.sidebar-top {
  padding: 16px 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 0 4px;
}

.brand-dot {
  width: 10px;
  height: 10px;
  border-radius: 3px;
  background: var(--accent);
}

.brand-name {
  font-weight: 600;
  font-size: 15px;
  letter-spacing: -0.01em;
}

.sidebar-section-head {
  padding: 6px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--fg-subtle);
}

.session-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.session-item {
  width: 100%;
  text-align: left;
  border: 1px solid transparent;
  background: transparent;
  border-radius: var(--radius-sm);
  padding: 9px 11px;
  display: flex;
  flex-direction: column;
  gap: 2px;
  transition: background-color 0.12s ease, border-color 0.12s ease;
}

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

.session-item.is-active {
  background: var(--bg-subtle);
  border-color: var(--border);
}

.session-item-title {
  font-size: 13.5px;
  font-weight: 500;
  color: var(--fg);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-item-time {
  font-size: 11.5px;
  color: var(--fg-subtle);
}

.session-empty {
  margin: 12px 16px;
  font-size: 13px;
  color: var(--fg-subtle);
  line-height: 1.5;
}

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

.btn-footer {
  width: 100%;
  text-align: left;
  padding: 8px 10px;
}

/* ── 主面板 ─────────────────────────────────────── */

.main-pane {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100%;
  background: var(--bg);
}

.topbar {
  height: 54px;
  flex-shrink: 0;
  padding: 0 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-elevated);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.session-heading {
  margin: 0;
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

.mobile-only {
  display: none;
}

.top-banner {
  padding: 9px 20px;
  background: var(--danger-bg);
  color: var(--danger);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 13.5px;
}

.top-banner[data-tone="info"] {
  background: var(--bg-subtle);
  color: var(--fg);
}

.session-files-bar {
  padding: 8px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

.files-bar-label {
  font-size: 12px;
  font-weight: 500;
  color: var(--fg-subtle);
  flex-shrink: 0;
}

.files-chip-list {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.file-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  font-size: 12.5px;
}

.file-chip-name {
  max-width: 200px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-chip-size {
  color: var(--fg-subtle);
  font-size: 11.5px;
}

.file-chip-btn {
  border: none;
  background: transparent;
  color: var(--fg);
  padding: 0 2px;
  font-size: 12px;
  font-weight: 600;
  text-decoration: underline;
  text-underline-offset: 2px;
}

/* ── 聊天舞台与消息流 ───────────────────────────── */

.chat-stage {
  flex: 1;
  overflow-y: auto;
  padding: 24px 20px;
}

.empty-state {
  min-height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.empty-inner {
  width: 100%;
  max-width: 680px;
  text-align: center;
  padding: 20px 8px;
}

.empty-title {
  margin: 0;
  font-size: 28px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.empty-subtitle {
  margin: 10px auto 26px;
  max-width: 520px;
  font-size: 14px;
  color: var(--fg-muted);
}

.starter-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
}

.starter-chip {
  text-align: left;
  padding: 13px 15px;
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--fg);
  font-size: 13.5px;
  line-height: 1.45;
  transition: border-color 0.15s ease, background-color 0.15s ease;
}

.starter-chip:hover {
  border-color: var(--border-strong);
  background: var(--bg-subtle);
}

.conversation-flow {
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.message-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.msg-row {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

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

.msg-row.is-assistant {
  align-items: flex-start;
}

.msg-meta {
  font-size: 11.5px;
  color: var(--fg-subtle);
  padding: 0 4px;
}

.msg-bubble {
  max-width: 88%;
  border-radius: var(--radius-md);
  padding: 12px 16px;
  word-break: break-word;
}

.msg-row.is-user .msg-bubble {
  background: var(--bg-user-msg);
  color: var(--fg-user-msg);
  border-bottom-right-radius: 4px;
}

.msg-row.is-assistant .msg-bubble {
  width: 100%;
  max-width: 100%;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  color: var(--fg);
}

.md-body > *:first-child {
  margin-top: 0;
}

.md-body > *:last-child {
  margin-bottom: 0;
}

.md-body p {
  margin: 0 0 10px;
}

.md-body h3,
.md-body h4 {
  margin: 14px 0 8px;
  font-size: 15px;
  font-weight: 600;
}

.md-body ul,
.md-body ol {
  margin: 6px 0 10px;
  padding-left: 20px;
}

.md-body li {
  margin: 3px 0;
}

.md-body blockquote {
  margin: 8px 0;
  padding: 4px 12px;
  border-left: 3px solid var(--border-strong);
  color: var(--fg-muted);
}

.md-inline-code {
  font-family: var(--font-mono);
  font-size: 0.9em;
  padding: 1px 5px;
  border-radius: 5px;
  background: var(--code-bg);
  border: 1px solid var(--border);
}

.msg-row.is-user .md-inline-code {
  background: rgba(127, 127, 127, 0.25);
  border-color: transparent;
}

.code-block {
  margin: 10px 0;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--code-bg);
  overflow: hidden;
}

.code-block-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 5px 10px;
  background: var(--code-header);
  border-bottom: 1px solid var(--border);
  font-size: 12px;
  color: var(--fg-muted);
}

.code-block pre {
  margin: 0;
  padding: 12px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 13px;
  line-height: 1.5;
  color: var(--fg);
}

/* ── 后台任务卡片 ───────────────────────────────── */

.task-section {
  border-top: 1px solid var(--border);
  padding-top: 16px;
}

.task-section-title {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--fg-muted);
  margin-bottom: 10px;
}

.task-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.task-card {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.task-card-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.task-id-label {
  font-family: var(--font-mono);
  font-size: 12px;
  color: var(--fg-subtle);
}

.task-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 2px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}

.task-pill[data-status="queued"] {
  background: var(--queued-bg);
  color: var(--queued);
}

.task-pill[data-status="running"] {
  background: var(--running-bg);
  color: var(--running);
}

.task-pill[data-status="succeeded"] {
  background: var(--succeeded-bg);
  color: var(--succeeded);
}

.task-pill[data-status="failed"] {
  background: var(--danger-bg);
  color: var(--danger);
}

.task-pill[data-status="interrupted"] {
  background: var(--interrupted-bg);
  color: var(--interrupted);
}

.task-error-text {
  margin: 0;
  font-size: 13px;
  color: var(--danger);
}

.task-outputs {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.output-card {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 6px 11px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg);
  font-size: 13px;
}

/* ── 底部输入框 ─────────────────────────────────── */

.composer-wrap {
  flex-shrink: 0;
  padding: 10px 20px max(16px, env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 82%, transparent);
}

.composer-inner {
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.upload-queue-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
}

.upload-item {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 4px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  font-size: 12px;
}

.upload-item.is-error {
  border-color: var(--danger);
  color: var(--danger);
  background: var(--danger-bg);
}

.upload-item-remove {
  border: none;
  background: transparent;
  padding: 0 2px;
  font-size: 13px;
  line-height: 1;
  color: inherit;
}

.composer-box {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 12px 14px 10px;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s ease;
}

.composer-box:focus-within,
.composer-box.is-dragover {
  border-color: var(--border-strong);
}

.composer-textarea {
  width: 100%;
  border: none;
  background: transparent;
  resize: none;
  outline: none;
  min-height: 26px;
  max-height: 180px;
  overflow-y: auto;
  line-height: 1.55;
}

.composer-actions {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.composer-actions-left,
.composer-actions-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.composer-hint,
.char-count {
  font-size: 12px;
  color: var(--fg-subtle);
}

.btn-send {
  padding: 6px 16px;
}

/* ── 移动端适配 ─────────────────────────────────── */

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 25;
}

@media (max-width: 768px) {
  .mobile-only {
    display: inline-flex;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow-md);
  }

  .sidebar.is-open {
    transform: translateX(0);
  }

  .topbar {
    padding: 0 12px;
  }

  .chat-stage {
    padding: 16px 12px;
  }

  .composer-wrap {
    padding: 8px 12px max(12px, env(safe-area-inset-bottom));
  }

  .starter-grid {
    grid-template-columns: 1fr;
  }

  .msg-bubble {
    max-width: 94%;
  }

  .composer-hint {
    display: none;
  }
}
