/* piagent-relay PWA —— 手机优先，深色为主。
   不引入任何字体/图标库：断网也要能看。 */

:root {
  --bg: #14161a;
  --bg-raised: #1b1e24;
  --bg-sunken: #101215;
  --line: #2a2f38;
  --text: #e6e8eb;
  --text-dim: #9aa3af;
  --accent: #7cd992;
  --warn: #e0b24a;
  --error: #e06a6a;
  --say: #6aa9e0;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f7f8fa;
    --bg-raised: #ffffff;
    --bg-sunken: #eef0f4;
    --line: #dde1e7;
    --text: #1a1d22;
    --text-dim: #5f6875;
    --accent: #1f9d55;
    --warn: #a8730a;
    --error: #c0392b;
    --say: #2f6fb0;
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans SC", "PingFang SC",
    "Microsoft YaHei", sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  padding-bottom: var(--safe-bottom);
}

code,
pre {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: 0.85em;
}

/* ---------------------------------------------------------------- topbar */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: calc(var(--safe-top) + 10px) 14px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}

.topbar h1 {
  flex: 1;
  margin: 0;
  font-size: 1.05rem;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

button {
  font: inherit;
  color: inherit;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 14px;
  cursor: pointer;
}

button:active {
  transform: scale(0.98);
}

button.icon {
  background: transparent;
  border: none;
  font-size: 1.6rem;
  line-height: 1;
  padding: 0 10px;
}

.pill {
  font-size: 0.75rem;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--line);
  color: var(--text-dim);
  white-space: nowrap;
}

.pill[data-state="online"] {
  color: var(--accent);
  border-color: color-mix(in srgb, var(--accent) 50%, var(--line));
}

.pill[data-state="offline"],
.pill[data-state="error"] {
  color: var(--error);
  border-color: color-mix(in srgb, var(--error) 50%, var(--line));
}

/* ---------------------------------------------------------------- 列表 */

main {
  padding: 12px 14px 40px;
}

.sessions {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.session {
  display: grid;
  grid-template-columns: 10px 1fr;
  grid-template-areas:
    "dot name"
    "dot meta";
  gap: 2px 12px;
  padding: 14px;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  cursor: pointer;
}

.session:active {
  background: var(--bg-sunken);
}

/* 只有本地缓存的旧会话（机器下线 / 会话已结束）：弱化一点，别和在线会话混同。 */
.session[data-history="true"] .session-name {
  font-weight: 500;
  color: var(--text-dim);
}
.session[data-history="true"] {
  background: var(--bg-sunken);
}

.session-name {
  grid-area: name;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session-meta {
  grid-area: meta;
  color: var(--text-dim);
  font-size: 0.82rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dot {
  grid-area: dot;
  align-self: center;
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--text-dim);
}

.dot[data-idle="false"] {
  background: var(--accent);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 22%, transparent);
}

.empty {
  margin: 40px 0;
  text-align: center;
  color: var(--text-dim);
}

/* ---------------------------------------------------------------- 动态流 */

#detail {
  padding: 8px 14px calc(96px + var(--safe-bottom));
}

.stream {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 8px;
}

.line {
  display: grid;
  /* 单栏：时间占顶部一行，正文吃满整宽。
     原先 `58px 1fr` 把时间当左侧独立一栏，正文永远被挤掉 58px，
     正文卡片左边空一大片——时间本来也不需要独占一列。 */
  grid-template-columns: minmax(0, 1fr);
  gap: 2px;
  padding: 8px 12px 10px;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-left: 3px solid var(--line);
  border-radius: 10px;
  font-size: 0.9rem;
  overflow-wrap: anywhere;
}

.line[data-kind="tool_start"],
.line[data-kind="tool_end"] {
  border-left-color: var(--accent);
}

.line[data-kind="blocked"] {
  border-left-color: var(--warn);
}

.line[data-kind="assistant_text"] {
  border-left-color: var(--say);
}

.say {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  line-height: 1.5;
}

.say-more {
  margin-top: 6px;
  color: var(--text-dim);
  font-size: 0.78rem;
}

.line[data-kind="notice"][data-level="error"] {
  border-left-color: var(--error);
}

.line time {
  color: var(--text-dim);
  font-size: 0.7rem;
  font-variant-numeric: tabular-nums;
  letter-spacing: 0.02em;
}

.tool-head {
  font-weight: 600;
}

.tool-head[data-error="true"] {
  color: var(--error);
}

.line pre {
  margin: 6px 0 0;
  padding: 8px;
  background: var(--bg-sunken);
  border-radius: 8px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  max-height: 40vh;
  overflow-y: auto;
  color: var(--text-dim);
}

.jump {
  position: fixed;
  left: 50%;
  bottom: calc(84px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 20;
}

.jump button {
  background: var(--accent);
  color: #08130b;
  border: none;
  font-weight: 600;
  box-shadow: 0 6px 20px rgb(0 0 0 / 35%);
}

/* ---------------------------------------------------------------- 审批卡片 */

.approvals {
  display: grid;
  gap: 10px;
  margin-top: 12px;
}

.approvals[hidden] {
  display: none;
}

.approval {
  display: grid;
  gap: 8px;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--bg-raised);
  /* 左侧一道警告色：一眼能看出「这里需要我动手」 */
  border: 1px solid var(--line);
  border-left: 3px solid var(--warn);
  box-shadow: 0 6px 20px rgb(0 0 0 / 30%);
}

.approval-head {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--warn);
}

.approval-command {
  margin: 0;
  padding: 10px 12px;
  border-radius: 10px;
  background: var(--bg-sunken);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.82rem;
  line-height: 1.45;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 30vh;
  overflow-y: auto;
}

.approval-foot {
  display: flex;
  align-items: center;
  gap: 8px;
}

.approval-left {
  flex: 1;
  font-size: 0.82rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.approval-foot button {
  height: 38px;
  padding: 0 16px;
  font-weight: 600;
  border-radius: 10px;
}

.approval-foot button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.approval-foot .allow {
  background: var(--accent);
  color: #08130b;
  border: none;
}

.approval-foot .deny {
  background: transparent;
  color: var(--error);
  border: 1px solid color-mix(in srgb, var(--error) 55%, var(--line));
}

/* ---------------------------------------------------------------- 问卷卡片 */

.quizzes {
  display: grid;
  gap: 10px;
  margin-top: 12px;
}

.quizzes[hidden] {
  display: none;
}

.quiz {
  display: grid;
  gap: 10px;
  padding: 12px 14px;
  border-radius: var(--radius);
  background: var(--bg-raised);
  border: 1px solid var(--line);
  /* 蓝色左边：和审批（警告黄）区分开，别让人把「回答问题」当成「批准危险命令」 */
  border-left: 3px solid var(--accent);
  box-shadow: 0 6px 20px rgb(0 0 0 / 30%);
}

.quiz-head {
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--accent);
}

.quiz-body {
  display: grid;
  gap: 14px;
}

.quiz-q {
  display: grid;
  gap: 8px;
}

.quiz-question {
  font-size: 0.92rem;
  line-height: 1.45;
}

.quiz-chip {
  margin-left: 6px;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  font-size: 0.72rem;
  color: var(--text-dim);
  vertical-align: 1px;
}

.quiz-options {
  display: grid;
  gap: 6px;
}

.quiz-option {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 10px;
  align-items: start;
  padding: 9px 11px;
  border-radius: 10px;
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  cursor: pointer;
}

.quiz-option input {
  grid-row: span 2;
  margin-top: 3px;
}

.quiz-option-label {
  font-size: 0.88rem;
  font-weight: 600;
  line-height: 1.35;
}

.quiz-option-desc {
  grid-column: 2;
  font-size: 0.78rem;
  line-height: 1.4;
  color: var(--text-dim);
}

.quiz-option-note {
  grid-column: 2;
  font-size: 0.72rem;
  color: var(--text-dim);
  opacity: 0.8;
}

.quiz-custom {
  width: 100%;
  min-height: 38px;
  padding: 9px 11px;
  border-radius: 10px;
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  font-size: 0.86rem;
}

.quiz-foot {
  display: flex;
  align-items: center;
  gap: 8px;
}

.quiz-left {
  flex: 1;
  font-size: 0.82rem;
  color: var(--text-dim);
  font-variant-numeric: tabular-nums;
}

.quiz-foot button {
  height: 38px;
  padding: 0 16px;
  font-weight: 600;
  border-radius: 10px;
}

.quiz-foot button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.quiz-foot .allow {
  background: var(--accent);
  color: #08130b;
  border: none;
}

.quiz-foot .deny {
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--line);
}

/* ---------------------------------------------------------------- toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(140px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 30;
  max-width: min(92vw, 520px);
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  box-shadow: 0 8px 28px rgb(0 0 0 / 40%);
  font-size: 0.88rem;
}

.toast[data-kind="error"] {
  border-color: var(--error);
  color: var(--error);
}

.toast[data-kind="warn"] {
  border-color: var(--warn);
  color: var(--warn);
}

/* ---------------------------------------------------------------- 配对页 */

body.pair {
  display: grid;
  place-items: center;
  padding: 24px 18px;
}

.pair-card {
  width: min(100%, 420px);
  padding: 24px 20px;
  background: var(--bg-raised);
  border: 1px solid var(--line);
  border-radius: 18px;
}

.pair-card h1 {
  margin: 0 0 6px;
  font-size: 1.2rem;
}

.hint {
  color: var(--text-dim);
  font-size: 0.85rem;
}

.hint[data-kind="error"] {
  color: var(--error);
}

.hint[data-kind="ok"] {
  color: var(--accent);
}

#pair-form {
  display: grid;
  gap: 14px;
  margin: 18px 0;
}

#pair-form label {
  display: grid;
  gap: 6px;
  font-size: 0.85rem;
  color: var(--text-dim);
}

#pair-form input {
  font: inherit;
  font-size: 1.1rem;
  padding: 12px;
  color: var(--text);
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: 10px;
}

#pair-form input:focus-visible,
button:focus-visible,
.session:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

#pair-form button {
  background: var(--accent);
  color: #08130b;
  border: none;
  font-weight: 600;
  padding: 12px;
  border-radius: 10px;
}

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

.composer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 25;
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 8px;
  align-items: end;
  padding: 10px 14px calc(10px + var(--safe-bottom));
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
}

.composer[hidden] {
  display: none;
}

#input {
  font: inherit;
  color: var(--text);
  background: var(--bg-sunken);
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  resize: none;
  min-height: 42px;
  max-height: 30vh;
  line-height: 1.4;
  overflow-y: auto;
}

#input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: -1px;
}

.composer button {
  height: 42px;
  padding: 0 16px;
  font-weight: 600;
  border-radius: 10px;
}

.composer button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

#send {
  background: var(--accent);
  color: #08130b;
  border: none;
}

#abort {
  background: transparent;
  color: var(--warn);
  border: 1px solid color-mix(in srgb, var(--warn) 55%, var(--line));
}

/* 平板/桌面：列表限宽居中，别让 12 寸屏上一行字横跨 1000px */
@media (min-width: 700px) {
  main,
  #detail {
    max-width: 760px;
    margin: 0 auto;
  }
  .topbar {
    padding-inline: max(14px, calc(50vw - 380px));
  }
  /* 固定定位的元素不参与 main 的居中，要自己限宽居中 */
  .composer {
    max-width: 760px;
    left: 50%;
    right: auto;
    width: 100%;
    transform: translateX(-50%);
  }
}
