* { box-sizing: border-box; margin: 0; }
:root {
  --bg: #f6f4ef; --panel: #fffdf8; --border: #e3ded2;
  --ink: #2b2926; --dim: #8a8478; --accent: #7a5c3e; --accent-soft: #efe7db;
}
body { font: 14px/1.6 "PingFang SC", "Microsoft YaHei", system-ui, sans-serif; color: var(--ink); background: var(--bg); overflow: hidden; }
#app { display: grid; grid-template-columns: 220px 1fr minmax(300px, 34%); height: 100vh; }
/* 三栏各自滚动：grid 子项默认 min-height:auto 会被内容撑破 100vh，整页跟着滚 */
#sidebar, #chat-pane, #preview-pane { min-height: 0; min-width: 0; overflow: hidden; }
button { font: inherit; cursor: pointer; }

/* 左栏 */
#sidebar { background: var(--panel); border-right: 1px solid var(--border); display: flex; flex-direction: column; }
.side-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.brand { display: flex; flex-direction: column; line-height: 1.25; }
.brand strong { font-weight: 650; }
.brand small { color: var(--dim); font-size: 9px; font-weight: 400; letter-spacing: .02em; }
#btn-new { border: 1px solid var(--border); background: none; border-radius: 6px; width: 26px; height: 26px; }
#session-list { flex: 1; overflow-y: auto; padding: 6px; }
.session-item { padding: 8px 10px; border-radius: 8px; cursor: pointer; display: flex; justify-content: space-between; gap: 6px; }
.session-item:hover { background: var(--accent-soft); }
.session-item.active { background: var(--accent-soft); font-weight: 600; }
.session-item .del { visibility: hidden; color: var(--dim); border: none; background: none; }
.session-item:hover .del { visibility: visible; }
.session-item span.t { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
#btn-settings { margin: 10px; padding: 8px; border: 1px solid var(--border); border-radius: 8px; background: none; color: var(--dim); }

/* 模板区 */
.side-sec { display: flex; align-items: center; justify-content: space-between; padding: 8px 14px 4px; border-top: 1px solid var(--border); color: var(--dim); font-size: 12px; }
#btn-upload-tpl { border: 1px solid var(--border); background: none; border-radius: 6px; width: 24px; height: 24px; color: var(--dim); }
#template-list { max-height: 30%; overflow-y: auto; padding: 4px 6px 6px; }
#template-list .tpl-empty { color: var(--dim); font-size: 12px; padding: 4px 10px; }
.tpl-item { padding: 6px 10px; border-radius: 8px; cursor: pointer; display: flex; justify-content: space-between; gap: 6px; font-size: 13px; }
.tpl-item:hover { background: var(--accent-soft); }
.tpl-item.active { background: var(--accent-soft); font-weight: 600; }
.tpl-item.active span.t::before { content: "✓ "; color: var(--accent); }
.tpl-item .del { visibility: hidden; color: var(--dim); border: none; background: none; }
.tpl-item:hover .del { visibility: visible; }
.tpl-item span.t { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }

/* 中栏聊天 */
#chat-pane { display: flex; flex-direction: column; min-width: 0; }
#messages { flex: 1; overflow-y: auto; padding: 20px 24px; }
.msg { max-width: 46em; margin: 0 auto 14px; }
.msg.user .bubble { background: var(--accent); color: #fff; border-radius: 12px 12px 3px 12px; padding: 9px 13px; margin-left: 20%; white-space: pre-wrap; }
.msg.assistant .bubble { background: var(--panel); border: 1px solid var(--border); border-radius: 12px 12px 12px 3px; padding: 10px 14px; }

/* 助手回复的 markdown 元素 */
.bubble > *:last-child { margin-bottom: 0; }
.bubble p, .bubble ul, .bubble ol, .bubble table, .bubble pre, .bubble blockquote { margin: 0 0 8px; }
.bubble ul, .bubble ol { padding-left: 1.5em; }
.bubble h1, .bubble h2, .bubble h3, .bubble h4 { margin: 6px 0 8px; font-size: 15px; }
.bubble table { border-collapse: collapse; max-width: 100%; display: block; overflow-x: auto; }
.bubble th, .bubble td { border: 1px solid var(--border); padding: 3px 10px; }
.bubble th { background: var(--accent-soft); }
.bubble code { background: var(--accent-soft); border-radius: 4px; padding: 1px 5px; font-size: 12.5px; }
.bubble pre { background: #2f2c28; color: #ece8e0; border-radius: 8px; padding: 10px 12px; overflow-x: auto; }
.bubble pre code { background: none; padding: 0; color: inherit; }
.bubble blockquote { border-left: 3px solid var(--border); color: var(--dim); padding-left: 10px; }
.bubble a { color: var(--accent); }
.reasoning { color: var(--dim); font-size: 12px; border-left: 3px solid var(--border); padding: 2px 10px; margin: 0 auto 6px; max-width: 46em; white-space: pre-wrap; max-height: 90px; overflow-y: auto; }
.toolchip { display: inline-block; font-size: 12px; background: var(--accent-soft); color: var(--accent); border-radius: 20px; padding: 2px 12px; margin: 0 6px 8px 0; }
.toolchip.running::after { content: " …"; }
.toolchip.error { background: #f6dfdc; color: #a4372a; }
.sys-note { text-align: center; color: var(--dim); font-size: 12px; margin: 10px 0; }

/* 输入区 */
#composer { display: flex; gap: 10px; padding: 14px 24px 18px; border-top: 1px solid var(--border); background: var(--panel); }
#input { flex: 1; resize: none; border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; font: inherit; background: var(--bg); }
#input:focus { outline: 1.5px solid var(--accent); }
#btn-send { padding: 0 22px; border: none; border-radius: 10px; background: var(--accent); color: #fff; }
#btn-send:disabled { opacity: .5; cursor: default; }

/* 右栏预览 */
#preview-pane { background: var(--panel); border-left: 1px solid var(--border); display: flex; flex-direction: column; }
.preview-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px; border-bottom: 1px solid var(--border); font-weight: 600; }
#dl-buttons a { font-size: 12px; font-weight: 400; margin-left: 8px; color: var(--accent); text-decoration: none; border: 1px solid var(--accent); border-radius: 6px; padding: 3px 10px; }
#preview-pages { flex: 1; overflow-y: auto; padding: 14px; background: #edeae2; }
#preview-pages img { display: block; width: 100%; margin-bottom: 12px; box-shadow: 0 1px 6px rgba(0,0,0,.15); background: #fff; }
.preview-empty { color: var(--dim); text-align: center; margin-top: 40%; }

/* 设置弹窗 */
dialog { border: 1px solid var(--border); border-radius: 12px; padding: 22px 26px; width: min(440px, 92vw); }
dialog::backdrop { background: rgba(43,41,38,.35); }
#settings-form h3 { margin-bottom: 14px; }
#settings-form label { display: block; margin-bottom: 10px; font-size: 13px; color: var(--dim); }
#settings-form input:not([type=checkbox]) { display: block; width: 100%; margin-top: 3px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; font: inherit; }
#settings-form .row { display: flex; align-items: center; gap: 6px; }
.hint { font-size: 12px; color: var(--dim); margin: 8px 0 14px; }
.identity-row code { display: block; font-size: 12px; word-break: break-all; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; margin-bottom: 8px; min-height: 32px; }
.identity-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 6px; }
.identity-actions button { background: none; border: 1px solid var(--border); border-radius: 8px; padding: 5px 12px; font-size: 12px; cursor: pointer; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 10px; }
.dialog-actions .primary { background: var(--accent); color: #fff; border: none; border-radius: 8px; padding: 7px 18px; }
.dialog-actions button:not(.primary) { background: none; border: 1px solid var(--border); border-radius: 8px; padding: 7px 14px; }

@media (max-width: 900px) {
  #app { grid-template-columns: 1fr; grid-template-rows: 1fr auto; }
  #sidebar, #preview-pane { display: none; }
}
