/* 中性灰阶 + 单一强调色。刻意不用渐变、毛玻璃、发光边框与彩色大盘。 */

:root {
  --bg: #ffffff;
  --bg-subtle: #f7f7f8;
  --bg-hover: #ececee;
  --border: #e3e3e6;
  --border-strong: #d0d0d4;
  --text: #1f2023;
  --text-muted: #6b6d74;
  --text-faint: #9a9ca3;
  --accent: #1a63d8;
  --accent-weak: #eaf0fc;
  --danger: #c02b2b;
  --ok: #2f8a4c;
  --warn: #b3761a;
  --radius: 8px;
  --sidebar-w: 260px;
  --drawer-w: 480px;
  --content-w: 760px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1c1f;
    --bg-subtle: #232327;
    --bg-hover: #2c2c31;
    --border: #34343a;
    --border-strong: #45454d;
    --text: #ececee;
    --text-muted: #a3a5ad;
    --text-faint: #7c7e86;
    --accent: #6fa0f5;
    --accent-weak: #23293a;
    --danger: #e26a6a;
    --ok: #63b581;
    --warn: #d5a04e;
  }
}

* { box-sizing: border-box; }

/* 抽屉与弹层都设了 display: flex，会盖掉浏览器给 [hidden] 的 display: none，
   导致 hidden = true 视觉上完全没反应。这条必须显式压过去。 */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.65 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
        "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
}

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

.layout { display: flex; height: 100%; }

/* ---------- 侧栏 ---------- */

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  border-right: 1px solid var(--border);
  background: var(--bg-subtle);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.layout.sidebar-hidden .sidebar { display: none; }

.sidebar-head { padding: 12px; }

.btn-new {
  width: 100%;
  padding: 8px 12px;
  text-align: left;
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  cursor: pointer;
}
.btn-new:hover { background: var(--bg-hover); }

.history { flex: 1; overflow-y: auto; padding: 0 8px 12px; }

.history-group {
  padding: 14px 8px 6px;
  font-size: 12px;
  color: var(--text-faint);
}

.history-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 8px;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
}
.history-item:hover { background: var(--bg-hover); }
.history-item.active { background: var(--accent-weak); }

.history-label { flex: 1; min-width: 0; }
.history-name {
  display: block;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.history-sub { font-size: 12px; color: var(--text-faint); }

.history-menu {
  opacity: 0;
  border: 0;
  background: none;
  cursor: pointer;
  color: var(--text-muted);
  padding: 0 2px;
  border-radius: 4px;
}
.history-item:hover .history-menu { opacity: 1; }
.history-menu:hover { background: var(--border); }

.sidebar-foot {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 10px 14px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-faint);
}

/* ---------- 主区 ---------- */

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
}
.topbar-title { font-weight: 600; }
.topbar-meta { margin-left: auto; font-size: 12px; color: var(--text-faint); }

.icon-btn {
  border: 0;
  background: none;
  cursor: pointer;
  color: var(--text-muted);
  padding: 4px 6px;
  border-radius: 6px;
}
.icon-btn:hover { background: var(--bg-hover); }

.thread { flex: 1; overflow-y: auto; padding: 24px 16px 8px; }

.empty {
  max-width: var(--content-w);
  margin: 12vh auto 0;
  text-align: center;
  color: var(--text-muted);
}
.empty h1 { font-size: 26px; margin: 0 0 8px; color: var(--text); }
.empty p { margin: 0; }

.turn { max-width: var(--content-w); margin: 0 auto 28px; }

.msg-user {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
}
.msg-user .msg-target { font-weight: 600; }
.msg-user dl { margin: 8px 0 0; font-size: 14px; color: var(--text-muted); }
.msg-user dt { font-size: 12px; color: var(--text-faint); margin-top: 6px; }
.msg-user dd { margin: 2px 0 0; white-space: pre-wrap; }

.msg-agent { margin-top: 16px; }

/* ---------- stage 行 ---------- */

.stage {
  border-bottom: 1px solid var(--border);
}
.stage:last-child { border-bottom: 0; }

.stage-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 4px;
  cursor: pointer;
  user-select: none;
}
.stage-head:hover { background: var(--bg-subtle); }

/* 折叠指示。评审反馈看不出阶段可折叠，所以给一个明确的方向提示。 */
.caret {
  flex: 0 0 10px;
  color: var(--text-faint);
  font-size: 10px;
  transition: transform .12s ease;
  transform: rotate(-90deg);
}
.caret.open { transform: rotate(0deg); }

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex: 0 0 8px;
  background: var(--border-strong);
}
.dot.running { background: var(--accent); animation: pulse 1.4s ease-in-out infinite; }
.dot.succeeded { background: var(--ok); }
.dot.failed { background: var(--danger); }
.dot.stale { background: var(--warn); }

@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: .35; } }

.stage-label { flex: 1; }
.stage-skill { font-size: 12px; color: var(--text-faint); font-family: var(--mono); }
.stage-time { font-size: 12px; color: var(--text-faint); font-variant-numeric: tabular-nums; }

.stage-body { padding: 0 4px 12px 26px; }

.stage-error {
  color: var(--danger);
  font-size: 14px;
  white-space: pre-wrap;
  margin: 0 0 8px;
}

.stage-note { font-size: 13px; color: var(--text-muted); margin: 0 0 8px; }

/* 波次分隔。同一批并行的阶段归在一条标记下，否则七个研究阶段同时转圈会像出了故障。 */
.wave-mark {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 18px 0 8px;
  font-size: 12px;
  color: var(--text-faint);
}
.wave-mark::after {
  content: "";
  flex: 1;
  height: 1px;
  background: var(--border);
}

.stage-retry {
  margin-top: 4px;
  padding: 4px 10px;
  font-size: 12px;
  color: var(--text-muted);
  background: none;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  cursor: pointer;
}
.stage-retry:hover { background: var(--bg-hover); color: var(--text); }

/* 质量护照 */
.passport {
  margin-top: 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.passport.failed { border-color: var(--danger); }
.passport.warned { border-color: var(--warn); }

.passport-head {
  padding: 10px 14px;
  font-size: 13px;
  font-weight: 600;
  background: var(--bg-subtle);
  border-bottom: 1px solid var(--border);
}
.passport.failed .passport-head { color: var(--danger); }
.passport.warned .passport-head { color: var(--warn); }
.passport.passed .passport-head { color: var(--ok); }

.gate-list { display: flex; flex-direction: column; }
.gate {
  display: grid;
  grid-template-columns: 20px 150px 1fr;
  gap: 8px;
  padding: 7px 14px;
  font-size: 12.5px;
  border-top: 1px solid var(--border);
}
.gate:first-child { border-top: none; }
.gate-mark { text-align: center; }
.gate.passed .gate-mark { color: var(--ok); }
.gate.failed .gate-mark { color: var(--danger); }
.gate.skipped .gate-mark { color: var(--text-faint); }
.gate.failed .gate-label { color: var(--danger); }
.gate.skipped { color: var(--text-faint); }
.gate-detail { color: var(--text-muted); word-break: break-word; }

.citations { margin: 4px 0 10px; }

.citation-counts { display: flex; flex-wrap: wrap; gap: 6px; }
.citation-count {
  padding: 3px 9px;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12px;
  font-variant-numeric: tabular-nums;
}
.citation-count.ok { border-color: var(--ok); color: var(--ok); }
.citation-count.bad { border-color: var(--danger); color: var(--danger); }
.citation-count.muted { color: var(--text-muted); }

.citation-bad {
  margin: 8px 0 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.citation-bad li {
  display: grid;
  grid-template-columns: minmax(0, 240px) 1fr;
  gap: 8px;
  font-size: 12.5px;
}
.citation-label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.citation-why { color: var(--danger); }
/* 地址单独占一行通栏：要手动去核对的人得能一眼看全，截断了就白给 */
.citation-url {
  grid-column: 1 / -1;
  color: var(--muted);
  font-size: 11.5px;
  word-break: break-all;
}

.files { display: flex; flex-direction: column; gap: 2px; }

.file {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 5px 8px;
  border-radius: 6px;
  border: 0;
  background: none;
  cursor: pointer;
  text-align: left;
  width: 100%;
}
.file:hover { background: var(--bg-hover); }
.file-path { font-family: var(--mono); font-size: 13px; }
.file-meta { font-size: 12px; color: var(--text-faint); margin-left: auto; }

.run-error {
  max-width: var(--content-w);
  margin: 0 auto 20px;
  border: 1px solid var(--border);
  border-left: 3px solid var(--danger);
  border-radius: var(--radius);
  padding: 10px 14px;
  font-size: 14px;
  white-space: pre-wrap;
}

/* ---------- 人工补录与下一轮 ---------- */

.handoff {
  border: 1px solid var(--border);
  border-left: 3px solid var(--accent, #c88);
  border-radius: var(--radius);
  padding: 16px 18px;
}
.handoff h3 { margin: 0 0 14px; font-size: 15px; }
.handoff form { display: flex; flex-direction: column; gap: 14px; }

.handoff-field { display: flex; flex-direction: column; gap: 4px; }
.handoff-label { font-size: 14px; font-weight: 600; }
.handoff-label em {
  margin-left: 6px;
  font-size: 12px;
  font-style: normal;
  font-weight: 400;
  color: var(--text-faint);
}
.handoff-hint { font-size: 12px; color: var(--text-muted); line-height: 1.5; }
.handoff-field textarea {
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 8px 10px;
  font: inherit;
  font-size: 14px;
  background: var(--bg);
  color: inherit;
  resize: vertical;
}

.handoff-actions { display: flex; align-items: center; gap: 12px; }
.handoff-note { font-size: 12px; color: var(--text-faint); }

.btn-primary {
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--bg-hover);
  color: inherit;
  font: inherit;
  font-size: 14px;
  padding: 6px 14px;
  cursor: pointer;
}
.btn-primary:hover { border-color: var(--text-muted); }
.btn-primary:disabled { opacity: 0.5; cursor: default; }

.next-round {
  max-width: var(--content-w);
  margin: 4px auto 24px;
  display: flex;
  align-items: center;
  gap: 14px;
  border-top: 1px dashed var(--border);
  padding-top: 16px;
  font-size: 14px;
  color: var(--text-muted);
}
.next-round button { margin-left: auto; }

.composer-note { color: var(--text-faint); }

/* ---------- 登录 ---------- */

#login-token {
  width: 100%;
  margin-top: 10px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 8px 10px;
  font: inherit;
  font-size: 14px;
  background: var(--bg);
  color: inherit;
}
.login-error { color: var(--danger); font-size: 13px; min-height: 18px; margin: 6px 0 0; }

.whoami {
  margin-left: auto;
  font-size: 12px;
  color: var(--text-faint);
}

/* ---------- 输入区 ---------- */

.composer { border-top: 1px solid var(--border); padding: 12px 16px 16px; }
.composer-inner { max-width: var(--content-w); margin: 0 auto; }

.composer-controls {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-bottom: 8px;
  font-size: 13px;
  color: var(--text-muted);
}
.composer-controls select {
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  padding: 3px 6px;
  margin-left: 4px;
}

.link-btn {
  border: 0;
  background: none;
  color: var(--accent);
  cursor: pointer;
  padding: 0;
}
.link-btn:hover { text-decoration: underline; }

.extra { display: flex; flex-direction: column; gap: 6px; margin-bottom: 8px; }

.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 8px 8px 8px 12px;
  background: var(--bg);
}
.composer-row:focus-within { border-color: var(--accent); }

textarea {
  width: 100%;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  background: var(--bg);
}
.composer-row textarea {
  border: 0;
  padding: 4px 0;
  max-height: 200px;
  overflow-y: auto;
}
textarea:focus { outline: none; }
.extra textarea:focus { border-color: var(--accent); }

.btn-send {
  flex: 0 0 auto;
  padding: 6px 14px;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
}
.btn-send:disabled { background: var(--border-strong); cursor: default; }

.composer-hint { margin: 6px 0 0; font-size: 12px; color: var(--text-faint); }

/* ---------- 抽屉 ---------- */

.drawer {
  width: var(--drawer-w);
  flex: 0 0 var(--drawer-w);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.drawer-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.drawer-title {
  font-family: var(--mono);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.drawer-actions { margin-left: auto; display: flex; align-items: center; gap: 10px; }
.drawer-body { flex: 1; overflow-y: auto; padding: 16px 18px 40px; }

/* ---------- markdown ---------- */

.markdown { font-size: 14.5px; }
.markdown h1, .markdown h2, .markdown h3 { line-height: 1.35; margin: 1.4em 0 .5em; }
.markdown h1 { font-size: 20px; }
.markdown h2 { font-size: 17px; }
.markdown h3 { font-size: 15px; }
.markdown h1:first-child, .markdown h2:first-child { margin-top: 0; }
.markdown p, .markdown ul, .markdown ol { margin: .6em 0; }
.markdown ul, .markdown ol { padding-left: 1.4em; }
.markdown li { margin: .2em 0; }
.markdown code {
  font-family: var(--mono);
  font-size: .88em;
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px 4px;
}
.markdown pre {
  background: var(--bg-subtle);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  overflow-x: auto;
}
.markdown pre code { background: none; border: 0; padding: 0; }
.markdown blockquote {
  margin: .8em 0;
  padding: 2px 0 2px 12px;
  border-left: 3px solid var(--border-strong);
  color: var(--text-muted);
}
.markdown table {
  border-collapse: collapse;
  width: 100%;
  margin: .8em 0;
  font-size: 13.5px;
}
.markdown th, .markdown td {
  border: 1px solid var(--border);
  padding: 6px 9px;
  text-align: left;
  vertical-align: top;
}
.markdown th { background: var(--bg-subtle); font-weight: 600; }
.markdown hr { border: 0; border-top: 1px solid var(--border); margin: 1.4em 0; }
.markdown a { color: var(--accent); }

/* ---------- 弹层 ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .38);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}

.modal {
  background: var(--bg);
  border: 1px solid var(--border-strong);
  border-radius: 10px;
  width: min(440px, calc(100vw - 32px));
  padding: 18px 20px 16px;
}
.modal h2 { margin: 0 0 10px; font-size: 16px; }
.modal-body { font-size: 14px; color: var(--text-muted); }
.modal-body ul { margin: 8px 0 0; padding-left: 1.3em; }
.modal-foot { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }

.btn-plain, .btn-danger {
  padding: 6px 14px;
  border-radius: 6px;
  cursor: pointer;
  border: 1px solid var(--border-strong);
  background: var(--bg);
}
.btn-plain:hover { background: var(--bg-hover); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { filter: brightness(.94); }

@media (max-width: 1100px) {
  .drawer { position: fixed; right: 0; top: 0; bottom: 0; background: var(--bg); z-index: 40; }
}
@media (max-width: 720px) {
  .sidebar { position: fixed; left: 0; top: 0; bottom: 0; z-index: 30; }
  .layout:not(.sidebar-open) .sidebar { display: none; }
  :root { --drawer-w: 100vw; }
}
