:root {
  --canvas: #f5f6f7;
  --panel: #ffffff;
  --panel-soft: #f8f9fa;
  --ink: #18201d;
  --muted: #69736f;
  --faint: #98a19d;
  --line: #e2e6e4;
  --line-strong: #cfd6d2;
  --green: #176b4d;
  --green-soft: #e8f3ee;
  --red: #c44e3b;
  --amber: #a86d12;
  --blue: #356a9a;
  --shadow: 0 12px 30px rgba(24, 32, 29, 0.08);
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
button, input, select, textarea { font: inherit; letter-spacing: 0; }
button { color: inherit; }
body {
  overflow: hidden;
  background: var(--canvas);
  color: var(--ink);
  font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
  -webkit-font-smoothing: antialiased;
}

.workspace {
  display: grid;
  grid-template-columns: 248px minmax(0, 1fr) 312px;
  width: 100%;
  height: 100dvh;
}

.left-rail,
.context-panel,
.agent-main {
  min-width: 0;
  background: var(--panel);
  display: flex;
  flex-direction: column;
}

.left-rail {
  padding: 18px 14px 14px;
  border-right: 1px solid var(--line);
}

.brand-block {
  height: 46px;
  padding: 0 6px;
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-mark {
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  background: var(--green);
  color: white;
  font-family: Georgia, serif;
  font-weight: 700;
}

.brand-mark { width: 32px; height: 32px; border-radius: 7px; font-size: 18px; }
.brand-mark.avatar-brand { overflow: hidden; border-radius: 50%; background: #e7ebe9; }
.brand-mark.avatar-brand img { display: block; width: 100%; height: 100%; object-fit: cover; }
.brand-name { font-size: 15px; line-height: 1.2; font-weight: 750; }
.brand-meta { margin-top: 3px; color: var(--muted); font-size: 11px; }

.primary-action,
.secondary-action {
  min-height: 38px;
  border: 1px solid transparent;
  border-radius: 7px;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  cursor: pointer;
  font-size: 13px;
  font-weight: 650;
}

.primary-action { background: var(--green); color: white; }
.primary-action:hover { background: #125b40; }
.secondary-action { background: var(--panel); border-color: var(--line-strong); }
.secondary-action:hover { background: var(--panel-soft); border-color: #adb8b3; }
.left-rail > .primary-action { width: 100%; margin: 18px 0 20px; }

.icon-button {
  width: 32px;
  height: 32px;
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  display: inline-grid;
  place-items: center;
  cursor: pointer;
  color: var(--muted);
  font-size: 18px;
}
.icon-button:hover { background: #edf0ee; color: var(--ink); }

.history-panel { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.section-heading {
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  text-transform: uppercase;
}
.history-list { overflow-y: auto; min-height: 0; display: flex; flex-direction: column; gap: 3px; }
.history-empty, .activity-empty { padding: 14px 8px; color: var(--faint); font-size: 12px; }
.history-item { position: relative; display: flex; border-radius: 6px; }
.history-item:hover, .history-item.active { background: #f0f3f1; }
.history-item > button:first-child {
  min-width: 0;
  flex: 1;
  padding: 9px 30px 9px 9px;
  border: 0;
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.history-item strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; font-weight: 600; }
.history-item span { display: block; margin-top: 3px; color: var(--faint); font-size: 10px; }
.history-delete {
  position: absolute;
  top: 8px;
  right: 5px;
  width: 24px;
  height: 24px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--faint);
  cursor: pointer;
  opacity: 0;
}
.history-item:hover .history-delete { opacity: 1; }
.history-delete:hover { background: #f7e8e5; color: var(--red); }

.connection-card { padding: 13px 8px 4px; border-top: 1px solid var(--line); }
.connection-card label { display: flex; justify-content: space-between; margin: 10px 0 5px; color: var(--muted); font-size: 10px; font-weight: 650; }
.connection-card label span { color: var(--faint); font-weight: 500; }
.connection-card select,
.secret-field { width: 100%; height: 34px; border: 1px solid var(--line-strong); border-radius: 6px; background: white; }
.connection-card select { padding: 0 8px; color: var(--ink); font-size: 11px; }
.secret-field { display: flex; align-items: center; overflow: hidden; }
.secret-field input { width: 100%; min-width: 0; height: 100%; padding: 0 9px; border: 0; outline: 0; font: 11px ui-monospace, SFMono-Regular, Menlo, monospace; }
.secret-field .icon-button { width: 30px; height: 30px; font-size: 13px; }
.connection-status { margin-top: 9px; display: flex; align-items: center; gap: 6px; color: var(--amber); font-size: 10px; }
.connection-status i, .agent-status i, .tool-event summary i, .activity-item i { display: block; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.connection-status.online { color: var(--green); }

.agent-main { min-height: 0; display: flex; flex-direction: column; background: var(--canvas); }
.topbar {
  height: 64px;
  flex: 0 0 auto;
  padding: 0 22px;
  border-bottom: 1px solid var(--line);
  background: rgba(255,255,255,0.92);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}
.thread-title { min-width: 0; display: flex; flex-direction: column; gap: 3px; }
.thread-title strong { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.thread-title span { color: var(--muted); font-size: 10px; }
.topbar-actions { display: flex; align-items: center; gap: 9px; }
.agent-status { height: 28px; padding: 0 9px; border: 1px solid var(--line); border-radius: 6px; display: flex; align-items: center; gap: 6px; color: var(--green); background: white; font-size: 10px; }
.agent-status.working { color: var(--blue); }
.agent-status.working i { animation: pulse 1s infinite; }
.agent-status.error { color: var(--red); }

.conversation {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 42px max(28px, calc((100% - 780px) / 2)) 32px;
  scroll-behavior: smooth;
}

.empty-state { max-width: 760px; margin: max(7vh, 30px) auto 0; }
.welcome-avatar { width: 54px; height: 54px; margin-bottom: 18px; overflow: hidden; border-radius: 50%; background: #e7ebe9; box-shadow: 0 5px 16px rgba(24,32,29,.12); }
.welcome-avatar img { display: block; width: 100%; height: 100%; object-fit: cover; }
.empty-kicker { color: var(--green); font-size: 10px; font-weight: 800; }
.empty-state h1 { margin: 12px 0 10px; font-family: Georgia, "Songti SC", serif; font-size: 34px; font-weight: 600; letter-spacing: 0; }
.empty-state > p { max-width: 560px; margin: 0; color: var(--muted); font-size: 14px; line-height: 1.75; }
.starter-grid { margin-top: 34px; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.starter-grid button {
  min-height: 78px;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--panel);
  text-align: left;
  cursor: pointer;
}
.starter-grid button:hover { border-color: #aab8b1; box-shadow: 0 4px 12px rgba(24,32,29,0.05); }
.starter-grid span, .starter-grid small { display: block; }
.starter-grid span { font-size: 13px; font-weight: 650; }
.starter-grid small { margin-top: 7px; color: var(--muted); font-size: 11px; }

.message-row { max-width: 780px; margin: 0 auto 30px; }
.user-row { display: flex; justify-content: flex-end; }
.user-message { display: flex; align-items: flex-end; justify-content: flex-end; gap: 9px; max-width: min(680px, 94%); }
.user-avatar { width: 30px; height: 30px; flex: 0 0 auto; overflow: hidden; border-radius: 50%; background: #e7ebe9; }
.user-avatar img { display: block; width: 100%; height: 100%; object-fit: cover; }
.message-label { margin: 0 4px 6px; color: var(--faint); font-size: 10px; }
.message-bubble { max-width: min(620px, 88%); padding: 11px 14px; border-radius: 8px 2px 8px 8px; background: #e7ebe9; font-size: 13px; line-height: 1.65; white-space: pre-wrap; word-break: break-word; }
.assistant-row { display: grid; grid-template-columns: 30px minmax(0, 1fr); gap: 13px; }
.agent-avatar {
  width: 30px;
  height: 30px;
  overflow: hidden;
  border-radius: 50%;
  background: #e7ebe9;
}
.agent-avatar img { display: block; width: 100%; height: 100%; object-fit: cover; }
.assistant-content { min-width: 0; padding-top: 2px; }
.message-copy { color: #26302c; font-size: 14px; line-height: 1.78; }
.message-copy p { margin: 0 0 12px; }
.message-copy h3 { margin: 22px 0 9px; font-size: 14px; }
.message-copy ul { margin: 0 0 14px; padding-left: 19px; }
.message-copy li { margin: 5px 0; }
.message-copy a { color: var(--blue); text-decoration: underline; text-underline-offset: 3px; }
.message-copy .topic-line { margin: 6px 0; padding: 8px 10px; border-left: 2px solid var(--green); background: #edf4f0; }

.pending-title { margin-top: 3px; font-size: 12px; font-weight: 650; }
.pending-steps { height: 22px; display: flex; align-items: center; gap: 5px; }
.pending-steps span { width: 6px; height: 6px; border-radius: 50%; background: var(--green); animation: bounce 1.2s infinite; }
.pending-steps span:nth-child(2) { animation-delay: .15s; }
.pending-steps span:nth-child(3) { animation-delay: .3s; }
.pending-caption { color: var(--faint); font-size: 10px; }

.tool-trace { margin: 0 0 17px; border: 1px solid var(--line); border-radius: 7px; overflow: hidden; background: var(--panel); }
.trace-heading { padding: 8px 11px; border-bottom: 1px solid var(--line); background: var(--panel-soft); color: var(--muted); font-size: 10px; font-weight: 700; }
.tool-event + .tool-event { border-top: 1px solid var(--line); }
.tool-event summary { min-height: 36px; padding: 7px 10px; display: grid; grid-template-columns: 8px auto minmax(0, 1fr) 14px; align-items: center; gap: 8px; cursor: pointer; list-style: none; color: var(--green); }
.tool-event summary::-webkit-details-marker { display: none; }
.tool-event summary span { color: var(--ink); font-size: 11px; font-weight: 650; }
.tool-event summary small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--muted); font-size: 10px; }
.tool-event summary b { color: var(--faint); font-size: 11px; }
.tool-event.failed summary { color: var(--red); }
.tool-event pre { margin: 0; padding: 10px 12px; overflow-x: auto; border-top: 1px solid var(--line); background: #f7f8f8; color: #4c5853; font: 10px/1.55 ui-monospace, SFMono-Regular, Menlo, monospace; }

.decision-grid { margin: 18px 0; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); border: 1px solid var(--line); border-radius: 7px; overflow: hidden; background: white; }
.decision-column { min-width: 0; padding: 12px; border-left: 3px solid; }
.decision-column + .decision-column { border-left-width: 1px; border-left-color: var(--line); }
.decision-column > span { display: block; margin-bottom: 9px; font-size: 10px; font-weight: 750; }
.decision-column.reach > span { color: var(--red); }
.decision-column.match > span { color: var(--amber); }
.decision-column.safety > span { color: var(--blue); }
.decision-column strong, .decision-column small { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.decision-column strong { margin-top: 7px; font-size: 11px; }
.decision-column small { margin-top: 2px; color: var(--faint); font-size: 9px; }
.suggestion-row { margin-top: 17px; display: flex; flex-wrap: wrap; gap: 7px; }
.suggestion-row button { padding: 7px 10px; border: 1px solid var(--line-strong); border-radius: 6px; background: white; color: #41504a; cursor: pointer; font-size: 11px; }
.suggestion-row button:hover { border-color: var(--green); color: var(--green); }

.composer-wrap { flex: 0 0 auto; padding: 12px max(28px, calc((100% - 780px) / 2)) 20px; background: var(--canvas); }
.composer { border: 1px solid var(--line-strong); border-radius: 8px; background: white; box-shadow: var(--shadow); overflow: hidden; }
.composer:focus-within { border-color: #8ea69b; box-shadow: 0 0 0 3px rgba(23,107,77,.08), var(--shadow); }
.composer textarea { display: block; width: 100%; min-height: 48px; max-height: 160px; padding: 14px 15px 6px; resize: none; border: 0; outline: 0; background: transparent; color: var(--ink); font-size: 13px; line-height: 1.55; }
.composer textarea::placeholder { color: #9aa39f; }
.composer-footer { min-height: 40px; padding: 4px 7px 6px 15px; display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.composer-footer > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--faint); font-size: 9px; }
.send-button { width: 30px; height: 30px; border: 0; border-radius: 6px; background: var(--green); color: white; cursor: pointer; font-size: 18px; line-height: 1; }
.send-button:hover { background: #125b40; }
.send-button:disabled { background: #b9c2be; cursor: wait; }

.context-panel { padding: 17px 16px; border-left: 1px solid var(--line); }
.context-header { min-height: 44px; display: flex; align-items: flex-start; justify-content: space-between; }
.context-header strong, .context-header span { display: block; }
.context-header strong { font-size: 13px; }
.context-header span { margin-top: 4px; color: var(--muted); font-size: 10px; }
.progress-track { height: 4px; margin: 9px 0 16px; overflow: hidden; border-radius: 2px; background: #e6eae8; }
.progress-track div { width: 0; height: 100%; background: var(--green); transition: width .35s ease; }
.profile-list { display: flex; flex-direction: column; }
.profile-row { min-height: 34px; padding: 8px 0; border-bottom: 1px solid #edf0ee; display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.profile-row span { color: var(--muted); font-size: 10px; }
.profile-row strong { max-width: 60%; color: var(--faint); text-align: right; font-size: 10px; font-weight: 550; word-break: break-word; }
.profile-row.filled strong { color: var(--ink); }
.context-divider { height: 1px; margin: 20px 0 8px; background: var(--line); }
.activity-count { min-width: 20px; padding: 2px 5px; border-radius: 6px; background: #edf1ef; text-align: center; }
.activity-list { max-height: 240px; overflow-y: auto; }
.activity-item { padding: 9px 2px; display: grid; grid-template-columns: 7px minmax(0, 1fr) auto; align-items: start; gap: 8px; color: var(--green); }
.activity-item + .activity-item { border-top: 1px solid #edf0ee; }
.activity-item i { margin-top: 3px; }
.activity-item div { min-width: 0; }
.activity-item strong, .activity-item span { display: block; }
.activity-item strong { color: var(--ink); font-size: 10px; }
.activity-item span { margin-top: 3px; overflow: hidden; color: var(--muted); font-size: 9px; line-height: 1.45; }
.activity-item time { color: var(--faint); font: 8px ui-monospace, monospace; }
.activity-item.failed { color: var(--red); }
.data-boundary { margin-top: auto; padding: 12px; border-left: 2px solid #c8d0cc; background: var(--panel-soft); }
.data-boundary strong { font-size: 10px; }
.data-boundary p { margin: 5px 0 0; color: var(--muted); font-size: 9px; line-height: 1.55; }

.profile-dialog { width: min(640px, calc(100vw - 28px)); padding: 0; border: 0; border-radius: 8px; box-shadow: 0 24px 70px rgba(20,28,24,.22); }
.profile-dialog::backdrop { background: rgba(20,28,24,.42); }
.profile-dialog form { padding: 20px; }
.dialog-header { display: flex; align-items: flex-start; justify-content: space-between; }
.dialog-header strong, .dialog-header span { display: block; }
.dialog-header strong { font-size: 16px; }
.dialog-header span { margin-top: 4px; color: var(--muted); font-size: 11px; }
.form-grid { margin-top: 18px; display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.form-grid label { color: var(--muted); font-size: 10px; font-weight: 650; }
.form-grid input, .form-grid select { width: 100%; height: 38px; margin-top: 6px; padding: 0 10px; border: 1px solid var(--line-strong); border-radius: 6px; outline: 0; background: white; color: var(--ink); font-size: 12px; }
.form-grid input:focus, .form-grid select:focus { border-color: var(--green); box-shadow: 0 0 0 3px rgba(23,107,77,.08); }
.dialog-actions { margin-top: 22px; display: flex; justify-content: flex-end; gap: 8px; }
.dialog-actions .primary-action { width: auto; margin: 0; }

.mobile-only { display: none; }
.scrim { display: none; }

@keyframes bounce { 0%, 70%, 100% { transform: translateY(0); opacity: .35; } 35% { transform: translateY(-4px); opacity: 1; } }
@keyframes pulse { 50% { opacity: .25; } }

@media (max-width: 1100px) {
  .workspace { grid-template-columns: 224px minmax(0, 1fr); }
  .context-panel { position: fixed; top: 0; right: 0; bottom: 0; z-index: 40; width: min(320px, 88vw); transform: translateX(100%); transition: transform .2s ease; box-shadow: -12px 0 30px rgba(24,32,29,.12); }
  .context-panel.open { transform: translateX(0); }
  .mobile-only { display: inline-grid; }
  .scrim.show { display: block; position: fixed; inset: 0; z-index: 30; background: rgba(24,32,29,.28); }
}

@media (max-width: 760px) {
  .workspace { display: block; }
  .left-rail { position: fixed; top: 0; left: 0; bottom: 0; z-index: 40; width: min(270px, 88vw); transform: translateX(-100%); transition: transform .2s ease; box-shadow: 12px 0 30px rgba(24,32,29,.12); }
  .left-rail.open { transform: translateX(0); }
  .agent-main { height: 100dvh; }
  .topbar { height: 58px; padding: 0 12px; }
  .topbar .secondary-action { display: none; }
  .thread-title { margin-right: auto; }
  .agent-status { display: none; }
  .conversation { padding: 24px 16px 20px; }
  .empty-state { margin-top: 28px; }
  .empty-state h1 { font-size: 28px; }
  .starter-grid { margin-top: 24px; grid-template-columns: 1fr; }
  .starter-grid button { min-height: 66px; }
  .message-row { margin-bottom: 24px; }
  .assistant-row { grid-template-columns: 28px minmax(0, 1fr); gap: 10px; }
  .agent-avatar { width: 28px; height: 28px; }
  .user-avatar { width: 28px; height: 28px; }
  .message-bubble { max-width: 94%; }
  .tool-event summary { grid-template-columns: 8px minmax(0, auto) minmax(0, 1fr) 12px; }
  .tool-event summary small { display: none; }
  .decision-grid { grid-template-columns: 1fr; }
  .decision-column + .decision-column { border-left: 3px solid; border-top: 1px solid var(--line); }
  .decision-column.match { border-left-color: var(--amber); }
  .decision-column.safety { border-left-color: var(--blue); }
  .composer-wrap { padding: 8px 10px 12px; }
  .composer-footer > span { display: none; }
  .composer-footer { justify-content: flex-end; }
  .form-grid { grid-template-columns: 1fr; max-height: 60vh; overflow-y: auto; padding-right: 4px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
