:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #fbfaf7;
  --border: #e6e2d9;
  --text: #1f2328;
  --muted: #6b6f76;
  --accent: #2f5d50;
  --accent-soft: #e3eee9;
  --accent-text: #ffffff;
  --user-bubble: #2f5d50;
  --user-text: #ffffff;
  --warn: #9a5b13;
  --warn-soft: #fbf0df;
  --danger: #a33a3a;
  --ok: #2f7d4f;
  --code-bg: #f1efe9;
  --shadow: 0 1px 2px rgba(20, 20, 20, .04), 0 4px 16px rgba(20, 20, 20, .05);
  --radius: 14px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", "Noto Sans", Roboto, Helvetica, Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171a;
    --surface: #1d2024;
    --surface-2: #191c1f;
    --border: #2d3137;
    --text: #e7e9ec;
    --muted: #9aa0a8;
    --accent: #7fb8a4;
    --accent-soft: #233630;
    --accent-text: #0f1513;
    --user-bubble: #2c5146;
    --user-text: #eef6f3;
    --warn: #e3b26b;
    --warn-soft: #3a2f1f;
    --code-bg: #24282d;
    --shadow: none;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--text); font: 15px/1.6 var(--font); -webkit-font-smoothing: antialiased; }
.hidden { display: none !important; }
button { font: inherit; cursor: pointer; }
input, textarea { font: inherit; color: inherit; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.error { color: var(--danger); min-height: 1.4em; margin: 0; font-size: 14px; }

.btn { border: 1px solid var(--border); background: var(--surface); color: var(--text); padding: 8px 14px; border-radius: 10px; }
.btn:hover { border-color: var(--accent); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.primary:disabled { opacity: .5; cursor: default; }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 10px; font-size: 13px; border-radius: 8px; }
.btn.danger { color: var(--danger); }
.icon-btn { border: none; background: transparent; color: var(--muted); width: 36px; height: 36px; border-radius: 10px; display: inline-flex; align-items: center; justify-content: center; font-size: 18px; cursor: pointer; flex: none; }
.icon-btn:hover { background: var(--accent-soft); color: var(--accent); }

/* login */
.login { min-height: 100%; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 18px; padding: 28px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 14px; }
.login-card label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; color: var(--muted); }
.login-card input { border: 1px solid var(--border); background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.brand { font-size: 22px; font-weight: 650; display: flex; align-items: center; gap: 10px; letter-spacing: .2px; }
.logo-dot { width: 14px; height: 14px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 5px var(--accent-soft); }

/* layout */
.app { display: grid; grid-template-columns: minmax(0, 1fr) 380px; height: 100dvh; }
.chat { display: flex; flex-direction: column; min-width: 0; height: 100dvh; }
.topbar { display: flex; align-items: center; justify-content: space-between; padding: 12px 20px; border-bottom: 1px solid var(--border); background: var(--bg); }
.agent { display: flex; align-items: center; gap: 12px; }
.avatar { width: 38px; height: 38px; border-radius: 50%; background: var(--accent); color: var(--accent-text); display: flex; align-items: center; justify-content: center; font-weight: 650; }
.agent-name { font-weight: 620; }
.agent-status { font-size: 13px; color: var(--muted); display: flex; align-items: center; gap: 6px; }
.status-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ok); }
.status-dot.busy { background: var(--warn); animation: pulse 1.2s infinite; }
.status-dot.paused { background: var(--muted); }
@keyframes pulse { 50% { opacity: .35; } }
#panel-toggle { display: none; }

.messages { flex: 1; overflow-y: auto; padding: 24px 20px 12px; scroll-behavior: smooth; }
#message-list, .live-run, .load-more { max-width: 780px; margin: 0 auto; }
.load-more { text-align: center; margin-bottom: 12px; }
.msg { display: flex; margin: 10px 0; }
.msg.user { justify-content: flex-end; }
.bubble { max-width: min(640px, 88%); padding: 10px 14px; border-radius: var(--radius); word-wrap: break-word; overflow-wrap: anywhere; }
.msg.user .bubble { background: var(--user-bubble); color: var(--user-text); border-bottom-right-radius: 4px; white-space: pre-wrap; }
.msg.agent .bubble { background: var(--surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; box-shadow: var(--shadow); }
.msg.system .bubble { background: var(--warn-soft); color: var(--warn); font-size: 14px; margin: 0 auto; }
.meta-line { font-size: 12px; color: var(--muted); margin-top: 4px; display: flex; gap: 8px; align-items: center; }
.msg.user .meta-line { justify-content: flex-end; color: rgba(255, 255, 255, .7); }
.tag { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
.day-sep { text-align: center; font-size: 12px; color: var(--muted); margin: 18px 0 8px; }

.md p { margin: .35em 0; }
.md p:first-child { margin-top: 0; }
.md p:last-child { margin-bottom: 0; }
.md ul, .md ol { padding-left: 1.4em; margin: .4em 0; }
.md h1, .md h2, .md h3, .md h4 { font-size: 1.05em; margin: .8em 0 .3em; }
.md code { font-family: var(--mono); font-size: .88em; background: var(--code-bg); padding: 1px 5px; border-radius: 5px; }
.md pre { background: var(--code-bg); padding: 10px 12px; border-radius: 10px; overflow-x: auto; font-size: 13px; }
.md pre code { background: none; padding: 0; }
.md table { border-collapse: collapse; margin: .5em 0; font-size: 14px; display: block; overflow-x: auto; }
.md th, .md td { border: 1px solid var(--border); padding: 4px 8px; text-align: left; }
.md blockquote { border-left: 3px solid var(--border); margin: .4em 0; padding-left: 10px; color: var(--muted); }
.md img { max-width: 100%; border-radius: 8px; }

.files { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.file-chip { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; padding: 4px 10px; border-radius: 8px; background: var(--accent-soft); color: var(--accent); text-decoration: none; max-width: 100%; }
.msg.user .file-chip { background: rgba(255, 255, 255, .15); color: var(--user-text); }
.file-chip .x { cursor: pointer; opacity: .7; }

.approval { border: 1px solid var(--accent); background: var(--surface); border-radius: var(--radius); padding: 12px 14px; max-width: min(640px, 88%); box-shadow: var(--shadow); }
.approval .q { font-weight: 600; }
.approval .label { font-size: 12px; color: var(--accent); font-weight: 600; letter-spacing: .3px; margin-bottom: 4px; }
.approval pre.detail { white-space: pre-wrap; background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font: 13px/1.5 var(--font); margin: 8px 0; max-height: 260px; overflow: auto; }
.approval .opts { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.approval.answered { border-color: var(--border); opacity: .85; }
.approval .answered-text { font-size: 13px; color: var(--muted); margin-top: 8px; }

.live-run { margin: 10px 0; }
.live-card { background: var(--surface-2); border: 1px dashed var(--border); border-radius: var(--radius); padding: 10px 14px; font-size: 13px; color: var(--muted); }
.live-head { display: flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.spinner { width: 12px; height: 12px; border: 2px solid var(--border); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; flex: none; }
@keyframes spin { to { transform: rotate(360deg); } }
.steps { margin: 8px 0 0; padding: 0; list-style: none; max-height: 280px; overflow-y: auto; }
.steps li { padding: 3px 0; border-top: 1px solid var(--border); font-family: var(--mono); font-size: 12px; white-space: pre-wrap; word-break: break-all; }
.steps li .k { color: var(--accent); font-family: var(--font); font-weight: 600; margin-right: 6px; }

.composer { border-top: 1px solid var(--border); padding: 12px 20px calc(12px + env(safe-area-inset-bottom)); background: var(--bg); }
.composer-row { max-width: 780px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; padding: 6px 6px 6px 8px; box-shadow: var(--shadow); }
.composer-row:focus-within { border-color: var(--accent); }
.composer textarea { flex: 1; border: none; outline: none; resize: none; background: transparent; padding: 7px 4px; max-height: 200px; line-height: 1.5; }
.attachments { max-width: 780px; margin: 0 auto 6px; display: flex; flex-wrap: wrap; gap: 6px; }
.attachments:empty { display: none; }
.send { border-radius: 12px; }

/* panel */
.panel { border-left: 1px solid var(--border); background: var(--surface-2); display: flex; flex-direction: column; height: 100dvh; min-width: 0; }
.tabs { display: flex; gap: 2px; padding: 10px 10px 0; border-bottom: 1px solid var(--border); overflow-x: auto; }
.tabs button { border: none; background: transparent; padding: 8px 10px; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tabs button.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.tab-body { flex: 1; overflow-y: auto; padding: 14px; }
.section-title { font-size: 12px; font-weight: 650; color: var(--muted); text-transform: uppercase; letter-spacing: .6px; margin: 16px 0 8px; display: flex; justify-content: space-between; align-items: center; }
.section-title:first-child { margin-top: 0; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 10px 12px; margin-bottom: 8px; }
.card .title { font-weight: 600; font-size: 14px; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.card .sub { font-size: 12px; color: var(--muted); margin-top: 2px; }
.card .prompt { font-size: 12px; color: var(--muted); margin-top: 6px; white-space: pre-wrap; max-height: 4.8em; overflow: hidden; }
.card .actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.card.paused { opacity: .6; }
.pill { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--code-bg); color: var(--muted); font-weight: 500; }
.pill.agent { background: var(--accent-soft); color: var(--accent); }
.pill.silent { background: var(--code-bg); }
.pill.spoke, .pill.replied { background: var(--accent-soft); color: var(--accent); }
.pill.failed { background: var(--warn-soft); color: var(--danger); }
.pill.deferred { background: var(--warn-soft); color: var(--warn); }
.form { display: flex; flex-direction: column; gap: 8px; }
.form input, .form textarea, .form select { border: 1px solid var(--border); background: var(--surface); border-radius: 8px; padding: 7px 9px; font-size: 14px; width: 100%; }
.form textarea { min-height: 70px; resize: vertical; }
.form .row { display: flex; gap: 8px; }
.hint { font-size: 12px; color: var(--muted); }
.mem-file { cursor: pointer; }
.mem-file:hover { border-color: var(--accent); }
.mem-editor textarea { width: 100%; min-height: 320px; font: 13px/1.5 var(--mono); border: 1px solid var(--border); border-radius: 8px; padding: 8px; background: var(--surface); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: 13px; }
.kv .k { color: var(--muted); }
.tree { font-size: 13px; list-style: none; padding-left: 0; margin: 0; }
.tree ul { list-style: none; padding-left: 16px; margin: 0; }
.tree li { padding: 2px 0; }
.tree a { text-decoration: none; }
.code-box { font-family: var(--mono); font-size: 12px; background: var(--code-bg); padding: 6px 8px; border-radius: 6px; word-break: break-all; }
.run-log { font-family: var(--mono); font-size: 11px; white-space: pre-wrap; word-break: break-all; max-height: 360px; overflow: auto; background: var(--code-bg); border-radius: 8px; padding: 8px; margin-top: 6px; }
.empty { color: var(--muted); font-size: 13px; padding: 8px 0; }

.toast { position: fixed; left: 50%; bottom: 90px; transform: translateX(-50%); background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 10px; font-size: 14px; z-index: 50; max-width: calc(100% - 32px); }
.scrim { position: fixed; inset: 0; background: rgba(0, 0, 0, .3); z-index: 19; }

@media (max-width: 900px) {
  .app { grid-template-columns: minmax(0, 1fr); }
  #panel-toggle { display: inline-flex; }
  .panel { position: fixed; right: 0; top: 0; bottom: 0; width: min(420px, 92vw); z-index: 20; transform: translateX(100%); transition: transform .2s ease; box-shadow: -8px 0 24px rgba(0, 0, 0, .12); }
  .panel.open { transform: translateX(0); }
  .messages { padding: 16px 16px 8px; }
  .topbar, .composer { padding-left: 16px; padding-right: 16px; }
  .bubble, .approval { max-width: 92%; }
}
