:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --border: #e6e6e9;
  --border-strong: #d4d4d8;
  --text: #18181b;
  --text-2: #52525b;
  --text-3: #a1a1aa;
  --accent: #18181b;
  --accent-fg: #ffffff;
  --green: #16a34a;
  --red: #dc2626;
  --mono: "SF Mono", ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --radius: 8px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

.hidden { display: none !important; }

/* ---------- 按钮 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  padding: 7px 14px;
  font-size: 13px; font-weight: 500; font-family: inherit;
  border: 1px solid var(--border-strong); border-radius: 6px;
  background: var(--surface); color: var(--text);
  cursor: pointer; transition: background .12s, border-color .12s;
  white-space: nowrap;
}
.btn:hover { background: #f4f4f5; }
.btn:focus-visible { outline: 2px solid #18181b33; outline-offset: 2px; }
.btn.primary { background: var(--accent); color: var(--accent-fg); border-color: var(--accent); }
.btn.primary:hover { background: #2e2e33; }
.btn.ghost { border-color: transparent; color: var(--text-2); }
.btn.ghost:hover { background: #f0f0f1; color: var(--text); }
.btn.danger { color: var(--red); }
.btn.danger:hover { background: #fef2f2; border-color: #fecaca; }
.btn.small { padding: 3px 9px; font-size: 12px; }
.btn:disabled { opacity: .55; cursor: not-allowed; }
.btn-block { width: 100%; }

.icon-btn {
  width: 22px; height: 22px; display: inline-flex; align-items: center; justify-content: center;
  border: none; background: none; border-radius: 4px;
  font-size: 14px; color: var(--text-3); cursor: pointer; line-height: 1;
}
.icon-btn:hover { background: #e9e9ec; color: var(--text); }

/* ---------- 输入 ---------- */
.field { display: block; margin-bottom: 14px; }
.field-label { display: block; font-size: 13px; font-weight: 500; margin-bottom: 5px; color: var(--text); }
.field-hint { font-weight: 400; color: var(--text-3); font-size: 12px; }
.field input, .field select {
  width: 100%; padding: 8px 10px;
  font-size: 14px; font-family: inherit;
  border: 1px solid var(--border-strong); border-radius: 6px;
  background: var(--surface); color: var(--text);
  transition: border-color .12s, box-shadow .12s;
}
.field input:focus, .field select:focus {
  outline: none; border-color: #71717a; box-shadow: 0 0 0 3px #18181b14;
}
.field input.mono { font-family: var(--mono); font-size: 13px; }

.form-error {
  margin: 4px 0 12px; padding: 8px 11px;
  font-size: 13px; color: #991b1b; background: #fef2f2;
  border: 1px solid #fecaca; border-radius: 6px;
}

/* ---------- 登录页 ---------- */
.view-auth-wrap, #view-auth {
  display: flex; align-items: center; justify-content: center;
  min-height: 100vh; padding: 24px;
  background:
    radial-gradient(1200px 500px at 50% -10%, #ececf1 0%, transparent 60%),
    var(--bg);
}
.auth-card {
  width: 100%; max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px;
  padding: 32px 32px 28px;
  box-shadow: 0 1px 2px rgba(0,0,0,.04), 0 8px 24px rgba(0,0,0,.05);
}
.auth-brand { text-align: center; margin-bottom: 24px; }
.auth-brand h1 { font-size: 20px; font-weight: 600; letter-spacing: -.01em; }
.auth-sub { font-size: 13px; color: var(--text-3); margin-top: 2px; }
.logo-mark {
  width: 40px; height: 40px; margin: 0 auto 12px;
  display: flex; align-items: center; justify-content: center;
  background: var(--accent); color: var(--accent-fg);
  border-radius: 10px; font-weight: 700; font-size: 15px; letter-spacing: .02em;
}
.logo-mark.small { width: 30px; height: 30px; font-size: 12px; border-radius: 8px; margin: 0; }
.auth-foot { margin-top: 14px; font-size: 12px; color: var(--text-3); text-align: center; }

.tabs { display: flex; border-bottom: 1px solid var(--border); margin-bottom: 20px; }
.tab {
  flex: 1; padding: 8px 0 10px;
  background: none; border: none; font-family: inherit;
  font-size: 13px; font-weight: 500; color: var(--text-3); cursor: pointer;
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tab.active { color: var(--text); border-bottom-color: var(--text); }
.tab:hover { color: var(--text-2); }

/* ---------- 布局 ---------- */
#view-app { display: flex; min-height: 100vh; }

.sidebar {
  width: 240px; flex-shrink: 0;
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
}
.side-brand {
  display: flex; align-items: center; gap: 10px;
  padding: 16px 16px 14px;
}
.side-title { font-size: 14px; font-weight: 600; }
.side-user { font-size: 12px; color: var(--text-3); max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.side-section { padding: 6px 8px 10px; border-top: 1px solid var(--border); }
.side-section:first-of-type { border-top: none; }
.side-section-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 8px 6px;
  font-size: 11px; font-weight: 600; letter-spacing: .06em;
  color: var(--text-3); text-transform: uppercase;
}
.side-list { list-style: none; }
.side-item {
  display: flex; align-items: center; gap: 8px;
  width: 100%; padding: 6px 8px;
  background: none; border: none; border-radius: 6px;
  font-family: inherit; font-size: 13px; color: var(--text-2);
  cursor: pointer; text-align: left;
}
.side-item:hover { background: #f4f4f5; color: var(--text); }
.side-item.active { background: #ececee; color: var(--text); font-weight: 500; }
.side-item .count {
  margin-left: auto; font-size: 11px; color: var(--text-3);
  font-family: var(--mono); background: #f0f0f2;
  padding: 0 6px; border-radius: 8px;
}
.side-item .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.side-foot { margin-top: auto; padding: 12px; border-top: 1px solid var(--border); }

.main { flex: 1; min-width: 0; padding: 28px 36px 60px; }
.main-header {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 16px;
  margin-bottom: 24px;
}
.main-header h2 { font-size: 18px; font-weight: 600; letter-spacing: -.01em; }
.main-sub { font-size: 13px; color: var(--text-3); margin-top: 2px; }

/* ---------- Agent 列表 ---------- */
.agents-toolbar {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 10px;
}
.agents-toolbar .label { font-size: 13px; font-weight: 600; color: var(--text-2); }

table.agents {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius);
  border-collapse: separate; border-spacing: 0;
  overflow: hidden;
}
table.agents th, table.agents td {
  padding: 10px 14px; text-align: left; font-size: 13px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
table.agents th {
  font-size: 11px; font-weight: 600; letter-spacing: .05em;
  color: var(--text-3); text-transform: uppercase;
  background: #fcfcfd;
}
table.agents tbody tr:last-child td { border-bottom: none; }
table.agents tbody tr:hover { background: #fafafb; }

.agent-name { font-weight: 500; }
.mail {
  font-family: var(--mono); font-size: 12px; color: var(--text-2);
  max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.mail.unset { color: var(--text-3); font-style: normal; }
.mail-actions { display: flex; gap: 8px; margin-top: 3px; }
.seen { font-family: var(--mono); font-size: 12px; color: var(--text-3); }
.empty-state {
  padding: 40px 20px; text-align: center; color: var(--text-3); font-size: 13px;
  background: var(--surface); border: 1px dashed var(--border-strong); border-radius: var(--radius);
}

/* 徽标 */
.badge {
  display: inline-flex; align-items: center;
  padding: 1px 8px; border-radius: 999px;
  font-size: 11px; font-weight: 500;
  border: 1px solid var(--border-strong); color: var(--text-2);
  background: #fafafa;
}
.badge.rt { font-family: var(--mono); font-size: 10.5px; }
.badge.ok { border-color: #bbf7d0; background: #f0fdf4; color: #15803d; }
.badge.warn { border-color: #fde68a; background: #fffbeb; color: #b45309; }
.badge.off { border-color: #e4e4e7; background: #f4f4f5; color: #71717a; }
.pl-flag { font-family: var(--mono); font-size: 11px; color: var(--text-2); }
.pl-flag.on { color: var(--green); }

/* ---------- 弹层 ---------- */
#modal-overlay {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(24,24,27,.4);
  display: flex; align-items: flex-start; justify-content: center;
  padding: 10vh 16px 16px;
  backdrop-filter: blur(2px);
}
.modal {
  width: 100%; max-width: 440px;
  background: var(--surface);
  border: 1px solid var(--border); border-radius: 12px;
  padding: 22px 24px;
  box-shadow: 0 12px 40px rgba(0,0,0,.18);
  max-height: 80vh; overflow-y: auto;
}
.modal h3 { font-size: 15px; font-weight: 600; margin-bottom: 4px; }
.modal .modal-sub { font-size: 13px; color: var(--text-3); margin-bottom: 18px; }
.modal-actions {
  display: flex; justify-content: flex-end; gap: 10px; margin-top: 20px;
}

/* 一次性码展示 */
.code-box {
  display: flex; align-items: center; gap: 10px;
  padding: 12px 14px; margin-bottom: 14px;
  background: #18181b; color: #fafafa;
  border-radius: 8px;
}
.code-box code {
  flex: 1; font-family: var(--mono); font-size: 16px;
  letter-spacing: .04em; word-break: break-all;
}
.code-box .btn { background: rgba(255,255,255,.12); border-color: rgba(255,255,255,.2); color: #fff; }
.code-box .btn:hover { background: rgba(255,255,255,.22); }
.code-meta { font-size: 12px; color: var(--text-3); margin-bottom: 6px; }
.code-note { font-size: 12px; color: var(--text-2); background: #fffbeb; border: 1px solid #fde68a; border-radius: 6px; padding: 8px 11px; }

.mail-preview {
  font-family: var(--mono); font-size: 13px; color: var(--text-2);
  background: #f4f4f5; border-radius: 6px; padding: 8px 11px; margin-bottom: 14px;
}

/* ---------- Toast ---------- */
#toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  z-index: 60;
  background: var(--accent); color: var(--accent-fg);
  font-size: 13px; padding: 9px 16px; border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0,0,0,.2);
  max-width: 90vw;
}
#toast.error { background: #b91c1c; }

/* ---------- P2 频道视图 ---------- */
.header-right { display: flex; align-items: center; gap: 14px; }
.view-tabs { border-bottom: none; margin-bottom: 0; }
.view-tabs .tab { flex: none; padding: 4px 10px 6px; }

.channel-layout {
  display: flex; gap: 0; height: calc(100vh - 160px); min-height: 420px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden;
}
.channel-side {
  width: 200px; flex-shrink: 0; border-right: 1px solid var(--border);
  padding: 12px 8px; overflow-y: auto; background: #fcfcfd;
}
.channel-side .side-section-head { padding: 2px 8px 8px; }
.channel-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.board-side {
  width: 240px; flex-shrink: 0; border-left: 1px solid var(--border);
  padding: 12px 10px; background: #fcfcfd; display: flex; flex-direction: column;
}
.board-placeholder {
  flex: 1; border: 1px dashed var(--border-strong); border-radius: 8px;
  display: flex; align-items: center; justify-content: center;
  color: var(--text-3); font-size: 13px;
}
.board-note { font-size: 11px; margin-left: 4px; }

#channel-empty { margin: 24px; }

.channel-room-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 12px 16px; border-bottom: 1px solid var(--border);
}
.channel-room-head h3 { font-size: 15px; font-weight: 600; }
.presence-bar { display: flex; flex-wrap: wrap; gap: 6px 14px; justify-content: flex-end; }
.presence-item { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-2); }
.presence-dot {
  width: 8px; height: 8px; border-radius: 50%; background: var(--text-3); flex-shrink: 0;
}
.presence-dot.online { background: var(--green); }
.presence-dot.busy { background: #f59e0b; }
.presence-dot.idle { background: var(--text-3); }
.presence-detail { color: var(--text-3); }

.timeline {
  flex: 1; overflow-y: auto; padding: 14px 16px;
  display: flex; flex-direction: column; gap: 6px;
}
.tl-row { display: flex; flex-direction: column; }
.tl-head { font-size: 11px; color: var(--text-3); margin-bottom: 1px; }
.tl-head .tl-who { font-weight: 600; color: var(--text-2); }
.tl-row.user .tl-who { color: #1d4ed8; }
.tl-row.agent .tl-who { color: #15803d; }
.tl-bubble {
  align-self: flex-start; max-width: 78%;
  background: #f4f4f5; border-radius: 10px; padding: 6px 11px;
  font-size: 13px; white-space: pre-wrap; word-break: break-word;
}
.tl-row.user .tl-bubble { background: #dbeafe; }
.tl-row.agent .tl-bubble { background: #dcfce7; }

.tl-system {
  align-self: center; font-size: 11px; color: var(--text-3);
  background: #f4f4f5; border-radius: 999px; padding: 1px 10px;
}
.tl-steer {
  font-size: 12px; color: #92400e; background: #fef3c7;
  border-radius: 8px; padding: 4px 10px; align-self: flex-start;
}

/* delta / tool 折叠 */
.tl-fold {
  align-self: flex-start; max-width: 85%;
  border: 1px solid var(--border); border-radius: 8px;
  font-size: 12px; background: var(--surface);
}
.tl-fold > summary {
  list-style: none; cursor: pointer; padding: 4px 10px;
  display: flex; align-items: center; gap: 6px; color: var(--text-2);
  user-select: none;
}
.tl-fold > summary::-webkit-details-marker { display: none; }
.tl-fold > summary:hover { color: var(--text); }
.tl-fold .fold-icon { font-size: 13px; }
.tl-fold .fold-count { color: var(--text-3); font-family: var(--mono); font-size: 11px; }
.tl-fold .fold-body {
  border-top: 1px solid var(--border); padding: 6px 10px;
  font-family: var(--mono); font-size: 11.5px; color: var(--text-2);
  white-space: pre-wrap; word-break: break-word; max-height: 220px; overflow-y: auto;
}

.composer {
  display: flex; gap: 8px; padding: 12px 16px; border-top: 1px solid var(--border);
}
.composer input {
  flex: 1; padding: 8px 12px; font-size: 14px; font-family: inherit;
  border: 1px solid var(--border-strong); border-radius: 6px;
  background: var(--surface); color: var(--text);
}
.composer input:focus { outline: none; border-color: #71717a; box-shadow: 0 0 0 3px #18181b14; }
.composer .btn.steering { background: #b45309; border-color: #b45309; color: #fff; }
.composer .btn.steering:hover { background: #92400e; }

.conn-status { font-size: 11px; color: var(--text-3); font-family: var(--mono); }

@media (max-width: 900px) {
  .board-side { display: none; }
  .channel-side { width: 150px; }
}
@media (max-width: 760px) {
  .channel-layout { height: auto; flex-direction: column; }
  .channel-side { width: 100%; border-right: none; border-bottom: 1px solid var(--border); max-height: 160px; }
  .timeline { min-height: 300px; }
}

/* ---------- 响应式 ---------- */
@media (max-width: 760px) {
  #view-app { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; }
  .main { padding: 18px 14px 50px; }
  .main-header { flex-direction: column; align-items: stretch; }
  .main-header .btn { align-self: flex-end; }
  .table-scroll { overflow-x: auto; }
  table.agents { min-width: 640px; }
}
.table-scroll { width: 100%; }
