:root {
  --bg: #f6f1ec;
  --panel: #fffaf6;
  --ink: #2b2522;
  --muted: #8a7f78;
  --line: #e7ddd5;
  --me: #d9775b;
  --me-ink: #fff;
  --him: #ffffff;
  --accent: #d9775b;
  --code: #f1e9e2;
  --danger: #c0473a;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1a19;
    --panel: #24211f;
    --ink: #ece4de;
    --muted: #978c84;
    --line: #36312e;
    --me: #c96a50;
    --me-ink: #fff;
    --him: #2b2725;
    --accent: #e08868;
    --code: #332e2b;
    --danger: #e0705f;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; overflow: hidden; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.6 system-ui, -apple-system, "PingFang SC", "HarmonyOS Sans SC", "MiSans", "Noto Sans SC", sans-serif;
  -webkit-tap-highlight-color: transparent;
}
button, textarea, input { font: inherit; color: inherit; }
[hidden] { display: none !important; }

/* login */
#login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
#login-form { width: min(320px, 100%); display: grid; gap: 14px; text-align: center; }
#login .eyes { font-size: 56px; line-height: 1; }
#login h1 { margin: 0 0 8px; font-size: 22px; font-weight: 600; letter-spacing: .1em; }
#login input {
  padding: 12px 14px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--panel); outline: none; text-align: center;
}
#login input:focus { border-color: var(--accent); }
#login button {
  padding: 12px; border: 0; border-radius: 12px; background: var(--accent); color: #fff; font-weight: 600;
}
.error { color: var(--danger); min-height: 1.4em; margin: 0; font-size: 14px; }

/* app shell */
#app, #list { height: var(--app-h, 100dvh); display: flex; flex-direction: column; }
header {
  display: flex; align-items: center; justify-content: space-between;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  border-bottom: 1px solid var(--line); background: var(--bg);
}
.title { display: flex; align-items: baseline; gap: 10px; }
.name { font-weight: 600; font-size: 17px; }
.status { font-size: 13px; color: var(--muted); }
.status.busy::after { content: ""; display: inline-block; width: 1.2em; text-align: left; animation: dots 1.2s steps(4) infinite; }
@keyframes dots { 0% { content: ""; } 25% { content: "."; } 50% { content: ".."; } 75% { content: "..."; } }
.ghost { border: 1px solid var(--line); background: transparent; border-radius: 999px; padding: 4px 12px; font-size: 13px; color: var(--muted); }
.ghost.on { color: var(--accent); border-color: var(--accent); }

/* thread */
#thread { flex: 1; min-height: 0; overflow-y: auto; padding: 16px 16px 8px; display: flex; flex-direction: column; gap: 10px; overscroll-behavior: contain; }
.msg { max-width: 82%; padding: 9px 13px; border-radius: 18px; white-space: pre-wrap; word-break: break-word; }
.msg.user { align-self: flex-end; background: var(--me); color: var(--me-ink); border-bottom-right-radius: 6px; }
.msg.assistant { align-self: flex-start; background: var(--him); border: 1px solid var(--line); border-bottom-left-radius: 6px; }
.msg.assistant p { margin: 0 0 .6em; } .msg.assistant p:last-child { margin: 0; }
.msg code { background: var(--code); border-radius: 5px; padding: 1px 5px; font: 14px/1.5 ui-monospace, "SF Mono", Menlo, Consolas, monospace; }
.msg pre { background: var(--code); border-radius: 10px; padding: 10px 12px; overflow-x: auto; white-space: pre; margin: .5em 0; }
.msg pre code { background: none; padding: 0; }
.msg a { color: var(--accent); }
.msg.user a { color: inherit; }
.msg ul, .msg ol { margin: .3em 0; padding-left: 1.3em; white-space: normal; }
.msg blockquote { margin: .4em 0; padding-left: .8em; border-left: 3px solid var(--line); color: var(--muted); }

.meta { align-self: flex-start; font-size: 13px; color: var(--muted); max-width: 90%; }
.meta.system { align-self: center; }
details.thinking { align-self: flex-start; max-width: 88%; font-size: 13px; color: var(--muted); }
details.thinking summary { cursor: pointer; list-style: none; }
details.thinking summary::before { content: "▸ "; }
details.thinking[open] summary::before { content: "▾ "; }
details.thinking div { white-space: pre-wrap; padding: 6px 0 2px 12px; border-left: 2px solid var(--line); margin-top: 4px; }
.tool { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 12px; }
.time { align-self: center; font-size: 12px; color: var(--muted); margin: 6px 0 2px; }

/* terminal peek */
.term { border-top: 1px solid var(--line); background: var(--panel); padding: 8px 12px; }
.term pre {
  margin: 0; max-height: 34vh; overflow: auto; font: 11px/1.35 ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  white-space: pre; color: var(--ink);
}
.keys { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.keys button { border: 1px solid var(--line); background: var(--bg); border-radius: 8px; padding: 5px 10px; font-size: 13px; }

/* composer */
#composer {
  display: flex; align-items: flex-end; gap: 8px;
  padding: 8px 12px calc(env(safe-area-inset-bottom) + 10px);
  border-top: 1px solid var(--line); background: var(--bg);
}
#input {
  flex: 1; resize: none; max-height: 40vh; min-height: 42px; padding: 9px 14px;
  border-radius: 21px; border: 1px solid var(--line); background: var(--panel); outline: none;
}
#input:focus { border-color: var(--accent); }
#send {
  flex: none; width: 42px; height: 42px; border-radius: 50%; border: 0; display: grid; place-items: center;
  background: var(--accent); color: #fff;
}
#send.stop svg { display: none; }
#send.stop::before { content: ""; width: 12px; height: 12px; border-radius: 2px; background: currentColor; }

/* back button */
.icon { border: 0; background: transparent; font-size: 28px; line-height: 1; padding: 0 10px 2px 0; color: var(--muted); }
#app header .title { flex: 1; min-width: 0; }
#app header .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* window list */

#windows { flex: 1; min-height: 0; overflow-y: auto; padding: 6px 0 calc(env(safe-area-inset-bottom) + 16px); }
.window-row {
  display: block; width: 100%; text-align: left; border: 0; background: transparent;
  padding: 12px 16px; border-bottom: 1px solid var(--line); -webkit-user-select: none; user-select: none;
}
.window-row:active { background: var(--panel); }
.row-top, .row-bottom { display: flex; gap: 8px; align-items: baseline; }
.row-title { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-time { font-size: 12px; color: var(--muted); flex: none; }
.row-bottom { font-size: 13px; color: var(--muted); margin-top: 2px; }
.row-model { flex: none; color: var(--accent); }
.row-preview { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.empty { text-align: center; color: var(--muted); margin-top: 30vh; }

/* dialogs */
dialog {
  border: 0; border-radius: 18px; padding: 18px 16px 12px; width: min(320px, calc(100% - 32px));
  background: var(--panel); color: var(--ink); box-shadow: 0 10px 40px rgba(0,0,0,.2);
}
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog form { display: grid; gap: 8px; }
.picker-title { margin: 0 0 6px; text-align: center; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#model-options { display: grid; gap: 8px; }
.option { border: 1px solid var(--line); background: var(--bg); border-radius: 12px; padding: 11px; font-weight: 500; }
.option.danger { color: var(--danger); }
.ghost.wide { width: 100%; padding: 9px; border-radius: 12px; }

/* typewriter caret */
.msg.typing::after { content: "▍"; color: var(--accent); animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

/* 头部两行：标题 + 状态 */
#app header .title { flex-direction: column; align-items: flex-start; gap: 0; line-height: 1.3; }
#app header .title .status + .status:not(:empty)::before { content: " · "; }
#app header .title { flex-direction: row; flex-wrap: wrap; column-gap: 8px; align-items: baseline; }
#ctx:empty { display: none; }
#usage { font-size: 12px; }
.status.warn { color: var(--danger); }
header, #composer, .term { flex: none; }
/* 还没落盘的自己的消息 */
.msg.user.pending { opacity: .6; }
/* 小窗：压缩边距 */
@media (max-height: 420px) {
  header { padding-top: 6px; padding-bottom: 6px; }
  #thread { padding-top: 8px; }
  #composer { padding-bottom: 6px; }
  .term pre { max-height: 22vh; }
}

/* 附件 */
#attach { flex: none; width: 42px; height: 42px; border: 0; background: transparent; color: var(--muted); display: grid; place-items: center; }
#tray { flex: none; display: flex; gap: 8px; overflow-x: auto; padding: 8px 12px 0; background: var(--bg); border-top: 1px solid var(--line); }
.chip { position: relative; flex: none; height: 64px; min-width: 64px; max-width: 160px; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); overflow: hidden; display: grid; place-items: center; font-size: 12px; color: var(--muted); padding: 0 8px; }
.chip img { height: 100%; width: auto; display: block; margin: 0 -8px; }
.chip .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 140px; }
.chip button { position: absolute; top: 3px; right: 3px; width: 20px; height: 20px; border-radius: 50%; border: 0; background: rgba(0,0,0,.55); color: #fff; font-size: 12px; line-height: 20px; padding: 0; }
.chip.uploading { opacity: .5; }
.msg .pics { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 4px; }
.msg .pics img { max-width: 180px; max-height: 220px; border-radius: 10px; display: block; }
.msg .filechip { display: inline-block; padding: 4px 10px; border-radius: 8px; background: rgba(255,255,255,.2); font-size: 13px; margin: 0 4px 4px 0; text-decoration: none; color: inherit; }
