:root {
  --bg: #f7f6f2; --side: #efede6; --card: #ffffff; --line: #e3e0d7; --text: #23211e;
  --muted: #8a857b; --accent: #b4532a; --accent-soft: #f3e3d9; --ok: #3f7d4e; --err: #b3261e;
  --code: #f1efe9; --radius: 12px;
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans CJK SC", sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1b1a; --side: #232220; --card: #2a2927; --line: #3a3835; --text: #ecebe7;
    --muted: #9b968c; --accent: #e08a5f; --accent-soft: #3d2c22; --ok: #7cc28d; --err: #f28b82;
    --code: #262523; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); }
body { font-size: 15px; line-height: 1.6; -webkit-text-size-adjust: 100%; }
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }
[hidden] { display: none !important; }
.muted { color: var(--muted); }
.err { color: var(--err); }

/* login */
#login { height: 100%; display: grid; place-items: center; padding: 16px; }
#login form { width: min(340px, 100%); display: grid; gap: 12px; }
#login h1 { font-size: 22px; font-weight: 600; margin: 0 0 8px; }
#login input { padding: 12px 14px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--card); color: var(--text); font-size: 16px; }
#login button { padding: 12px; border-radius: var(--radius); background: var(--text); color: var(--bg); font-weight: 600; }

/* layout */
#app { height: 100%; display: grid; grid-template-columns: 260px minmax(0, 1fr) 320px; }
#side { background: var(--side); border-right: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
#main { display: flex; flex-direction: column; min-width: 0; min-height: 0; height: 100%; }
#panel { background: var(--side); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
#scrim { display: none; }
.only-mobile, .only-narrow { display: none !important; }

@media (max-width: 1100px) {
  #app { grid-template-columns: 260px minmax(0, 1fr); }
  .only-narrow { display: inline-grid !important; }
  #panel { position: fixed; right: 0; top: 0; bottom: 0; width: min(360px, 92vw); z-index: 20;
           transform: translateX(100%); transition: transform .2s ease; }
  #app.panel-open #panel { transform: none; }
  #app.panel-open #scrim, #app.side-open #scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 15; }
}
@media (max-width: 760px) {
  #app { grid-template-columns: minmax(0, 1fr); }
  .only-mobile { display: inline-grid !important; }
  #side { position: fixed; left: 0; top: 0; bottom: 0; width: min(300px, 86vw); z-index: 20;
          transform: translateX(-100%); transition: transform .2s ease; }
  #app.side-open #side { transform: none; }
}

/* sidebar */
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 16px 8px; }
.brand { font-weight: 650; letter-spacing: .02em; }
.new { margin: 4px 12px 12px; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--card); text-align: left; }
.new:hover { border-color: var(--muted); }
#conv-list { overflow-y: auto; padding: 0 8px 16px; }
.conv { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.conv:hover { background: color-mix(in srgb, var(--text) 6%, transparent); }
.conv.on { background: color-mix(in srgb, var(--text) 10%, transparent); }
.conv .t { flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.conv .dot { width: 7px; height: 7px; border-radius: 50%; background: var(--accent); animation: pulse 1.4s infinite; flex: none; }
.conv .del { opacity: 0; color: var(--muted); padding: 0 4px; }
.conv:hover .del { opacity: 1; }
@media (hover: none) { .conv .del { opacity: .6; } }
@keyframes pulse { 50% { opacity: .3; } }

/* main */
.main-head { display: flex; align-items: center; gap: 10px; padding: 12px 20px; border-bottom: 1px solid var(--line); min-height: 52px; }
.main-head h2 { font-size: 15px; font-weight: 600; margin: 0; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#run-state { font-size: 13px; color: var(--accent); white-space: nowrap; }
.icon { width: 34px; height: 34px; display: inline-grid; place-items: center; border-radius: 8px; color: var(--muted); font-size: 17px; flex: none; }
.icon:hover { background: color-mix(in srgb, var(--text) 8%, transparent); color: var(--text); }

#msgs { flex: 1; overflow-y: auto; padding: 24px 20px 12px; }
#msgs > * { max-width: 760px; margin-left: auto; margin-right: auto; }
.empty { color: var(--muted); text-align: center; margin-top: 18vh !important; }
.user { display: flex; justify-content: flex-end; margin: 18px auto; }
.user .b { background: var(--card); border: 1px solid var(--line); border-radius: 16px 16px 4px 16px; padding: 9px 14px; max-width: 85%; white-space: pre-wrap; word-break: break-word; }
.text { margin: 10px auto; word-break: break-word; }
.text p { margin: .5em 0; }
.text img { max-width: 100%; border-radius: 8px; border: 1px solid var(--line); cursor: zoom-in; }
.text pre { background: var(--code); padding: 10px 12px; border-radius: 8px; overflow-x: auto; font-size: 13px; }
.text code { background: var(--code); padding: 1px 5px; border-radius: 5px; font-size: .9em; }
.text pre code { background: none; padding: 0; }
.text table { border-collapse: collapse; display: block; overflow-x: auto; }
.text th, .text td { border: 1px solid var(--line); padding: 4px 10px; }
.text a { color: var(--accent); }

.steps { margin: 6px auto; font-size: 13.5px; color: var(--muted); }
.steps > summary { cursor: pointer; list-style: none; display: flex; align-items: center; gap: 6px; padding: 3px 0; }
.steps > summary::-webkit-details-marker { display: none; }
.steps > summary::before { content: "▸"; font-size: 11px; transition: transform .15s; }
.steps[open] > summary::before { transform: rotate(90deg); }
.step { padding: 3px 0 3px 18px; position: relative; }
.step::before { content: ""; position: absolute; left: 4px; top: 11px; width: 6px; height: 6px; border-radius: 50%; background: var(--line); }
.step.done::before { background: var(--ok); }
.step.fail::before { background: var(--err); }
.step.run::before { background: var(--accent); animation: pulse 1.4s infinite; }
.step details pre { white-space: pre-wrap; word-break: break-all; background: var(--code); color: var(--text); padding: 8px 10px; border-radius: 8px; font-size: 12px; max-height: 320px; overflow: auto; margin: 4px 0; }
.step summary { cursor: pointer; }
.note { margin: 10px auto; font-size: 13px; color: var(--muted); }
.note.bad { color: var(--err); }
.thinking { margin: 12px auto; color: var(--muted); font-size: 13.5px; }
.thinking::after { content: "…"; animation: pulse 1.4s infinite; }

#composer { padding: 8px 16px calc(14px + env(safe-area-inset-bottom)); }
#composer > * { max-width: 760px; margin-left: auto; margin-right: auto; }
.box { display: flex; align-items: flex-end; gap: 6px; background: var(--card); border: 1px solid var(--line); border-radius: 18px; padding: 6px; box-shadow: 0 1px 2px rgba(0,0,0,.04); }
.box:focus-within { border-color: var(--muted); }
#input { flex: 1; border: 0; outline: 0; resize: none; background: transparent; color: var(--text); font: inherit; font-size: 16px; padding: 7px 4px; max-height: 40vh; }
.send, .stop { width: 34px; height: 34px; border-radius: 50%; background: var(--text); color: var(--bg); font-weight: 700; flex: none; }
.send:disabled { opacity: .3; cursor: default; }
.stop { background: var(--accent); color: #fff; font-size: 12px; }
#attach-list { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
#attach-list:empty { display: none; }
.chip { font-size: 12.5px; background: var(--accent-soft); border-radius: 999px; padding: 3px 10px; }
#plan-chip { font-size: 13px; color: var(--muted); margin-bottom: 6px; cursor: pointer; }
#plan-chip b { color: var(--text); font-weight: 600; }

/* panel */
.tabs { display: flex; align-items: center; gap: 4px; padding: 10px 12px; border-bottom: 1px solid var(--line); }
.tabs button[data-tab] { padding: 6px 12px; border-radius: 8px; color: var(--muted); }
.tabs button.on { background: var(--card); color: var(--text); box-shadow: 0 0 0 1px var(--line); }
.tabs .icon { margin-left: auto; }
#panel section { overflow-y: auto; padding: 12px 16px 24px; flex: 1; }
#panel h3 { font-size: 12px; font-weight: 600; color: var(--muted); letter-spacing: .06em; margin: 14px 0 8px; }
#status-line { font-size: 13px; }
.plan { list-style: none; padding: 0; margin: 0; }
.plan li { padding: 4px 0 4px 26px; position: relative; font-size: 14px; }
.plan li::before { content: ""; position: absolute; left: 2px; top: 8px; width: 14px; height: 14px; border-radius: 50%; border: 1.5px solid var(--line); }
.plan li.in_progress::before { border-color: var(--accent); border-top-color: transparent; animation: spin 1s linear infinite; }
.plan li.completed { color: var(--muted); }
.plan li.completed::before { border-color: var(--ok); background: var(--ok); }
.plan li.completed::after { content: "✓"; position: absolute; left: 5px; top: 3px; color: #fff; font-size: 11px; }
.plan li.muted::before { display: none; }
.plan li.muted { padding-left: 0; }
@keyframes spin { to { transform: rotate(360deg); } }
.agent { background: var(--card); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px; font-size: 13.5px; }
.agent .h { display: flex; gap: 8px; align-items: center; }
.agent .h b { font-weight: 600; flex: 1; min-width: 0; }
.agent .s { font-size: 12px; color: var(--muted); }
.agent .s.running { color: var(--accent); }
.agent .s.completed { color: var(--ok); }
.agent .s.failed { color: var(--err); }
.agent .a { color: var(--muted); font-size: 12.5px; margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.files-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
#crumbs { flex: 1; min-width: 0; font-size: 13px; }
#crumbs a { color: var(--accent); cursor: pointer; }
.small { font-size: 12.5px; padding: 4px 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--card); white-space: nowrap; }
#file-list { list-style: none; padding: 0; margin: 0; }
#file-list li { display: flex; gap: 8px; align-items: center; padding: 7px 6px; border-radius: 8px; cursor: pointer; font-size: 14px; }
#file-list li:hover { background: color-mix(in srgb, var(--text) 6%, transparent); }
#file-list .n { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#file-list .z { color: var(--muted); font-size: 12px; white-space: nowrap; }
#file-list .dl { color: var(--muted); padding: 0 4px; text-decoration: none; }

#viewer { position: fixed; inset: 0; background: rgba(0,0,0,.85); display: grid; place-items: center; z-index: 50; padding: 16px; }
#viewer img { max-width: 100%; max-height: 100%; border-radius: 6px; }
#viewer .icon { position: fixed; top: 12px; right: 12px; color: #fff; }
