:root {
  --paper: #f6f1e7;
  --paper-2: #fffdf8;
  --ink: #2b2b28;
  --ink-soft: #6b665c;
  --green: #2f6b4f;
  --green-soft: #e3efe6;
  --clay: #a8552f;
  --line: #e2d9c8;
  --shadow: 0 10px 30px rgba(60, 45, 25, 0.08);
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: "Microsoft YaHei", "PingFang SC", "Hiragino Sans GB", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.7;
}

.app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ------------------------------------------------------------ 侧边栏 */
.sidebar {
  width: 296px;
  flex: 0 0 296px;
  background: linear-gradient(180deg, #f2ece0, #efe7d8);
  border-right: 1px solid var(--line);
  padding: 22px 18px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.brand { display: flex; gap: 12px; align-items: center; }

.brand-mark {
  width: 44px; height: 44px; flex: 0 0 44px;
  border-radius: 12px;
  background: var(--green);
  color: #fff;
  display: grid; place-items: center;
  font-family: "Songti SC", "SimSun", serif;
  font-size: 24px;
  box-shadow: var(--shadow);
}

.brand-text h1 {
  margin: 0;
  font-size: 17px;
  font-family: "Songti SC", "SimSun", serif;
  letter-spacing: 0.5px;
  color: #24402f;
}

.brand-text p { margin: 2px 0 0; font-size: 12px; color: var(--ink-soft); }

.panel {
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px;
  box-shadow: var(--shadow);
}

.panel h2 {
  margin: 0 0 10px;
  font-size: 13px;
  font-weight: 700;
  color: var(--green);
  letter-spacing: 1px;
}

.meta-list { list-style: none; margin: 0; padding: 0; font-size: 13px; }

.meta-list li {
  display: flex; justify-content: space-between; gap: 8px;
  padding: 5px 0;
  border-bottom: 1px dashed var(--line);
}
.meta-list li:last-child { border-bottom: none; }
.meta-list span { color: var(--ink-soft); }
.meta-list b { color: var(--ink); font-weight: 600; text-align: right; word-break: break-all; }

.meta-source { margin: 10px 0 0; font-size: 11px; color: #9b9384; word-break: break-all; }

.examples { display: flex; flex-direction: column; gap: 8px; }

.examples button {
  text-align: left;
  background: #fbf7ef;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 9px 11px;
  font-size: 12.5px;
  color: #4a453c;
  cursor: pointer;
  transition: all .16s ease;
  font-family: inherit;
  line-height: 1.5;
}
.examples button:hover { background: var(--green-soft); border-color: #b9d3c2; color: #24402f; transform: translateX(2px); }

.panel-foot { margin-top: auto; }

.ghost-btn {
  width: 100%;
  padding: 9px;
  border-radius: 10px;
  border: 1px solid #d8cdb8;
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
  font-size: 13px;
  font-family: inherit;
  transition: all .15s ease;
}
.ghost-btn:hover { background: #fff; color: var(--clay); border-color: #e0c3ae; }

.disclaimer { margin: 10px 0 0; font-size: 11px; line-height: 1.6; color: #9b9384; }

/* ------------------------------------------------------------ 主区域 */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }

.topbar {
  height: 58px;
  flex: 0 0 58px;
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 24px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 253, 248, 0.85);
  backdrop-filter: blur(6px);
}

.topbar-title { display: flex; align-items: center; gap: 9px; font-size: 13.5px; color: var(--ink-soft); }

.pulse {
  width: 9px; height: 9px; border-radius: 50%;
  background: #c9c2b4;
  box-shadow: 0 0 0 0 rgba(47, 107, 79, .5);
}
.pulse.ok { background: #3f9a6b; animation: pulse 2.4s infinite; }
.pulse.err { background: #c0533a; animation: none; }
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(63, 154, 107, .45); }
  70% { box-shadow: 0 0 0 8px rgba(63, 154, 107, 0); }
  100% { box-shadow: 0 0 0 0 rgba(63, 154, 107, 0); }
}

.tag {
  font-size: 12px;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--green-soft);
  color: var(--green);
  border: 1px solid #cfe2d6;
}

/* 身份标签：引用点击埋点要记"谁点的"，没登录系统时靠它 */
.user-chip {
  cursor: pointer;
  font-family: inherit;
  background: #f3ece0;
  color: #8a7f6b;
  border-color: #e6dbc8;
  max-width: 150px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.user-chip:hover { border-color: #cbbfa6; color: var(--clay); }
.user-chip.set { background: var(--green-soft); color: var(--green); border-color: #cfe2d6; }

/* ------------------------------------------------------------ 消息 */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 26px 24px 10px;
  scroll-behavior: smooth;
}

.msg { display: flex; gap: 12px; margin-bottom: 22px; max-width: 920px; }
.msg.user { flex-direction: row-reverse; margin-left: auto; }

.avatar {
  width: 34px; height: 34px; flex: 0 0 34px;
  border-radius: 10px;
  display: grid; place-items: center;
  font-size: 13px;
  font-family: "Songti SC", "SimSun", serif;
}
.msg.user .avatar { background: #efe4d2; color: var(--clay); }
.msg.assistant .avatar { background: var(--green); color: #fff; }

.bubble {
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 14px 16px;
  box-shadow: var(--shadow);
  min-width: 0;
  flex: 1;
}
.msg.user .bubble { background: #f3ece0; border-color: #e6dbc8; flex: 0 1 auto; }

.bubble .content { word-wrap: break-word; overflow-wrap: anywhere; }
.bubble .content h4 {
  margin: 14px 0 6px;
  font-size: 14.5px;
  color: var(--green);
  font-family: "Songti SC", "SimSun", serif;
}
.bubble .content h4:first-child { margin-top: 0; }
.bubble .content p { margin: 6px 0; }
.bubble .content ul { margin: 6px 0; padding-left: 20px; }
.bubble .content li { margin: 3px 0; }
.bubble .content strong { color: #24402f; }
.bubble .content code {
  background: #f1ece0;
  border-radius: 4px;
  padding: 1px 5px;
  font-size: 13px;
}
.bubble .content table {
  border-collapse: collapse;
  width: 100%;
  margin: 10px 0;
  font-size: 13px;
  display: block;
  overflow-x: auto;
}
.bubble .content th,
.bubble .content td {
  border: 1px solid var(--line);
  padding: 6px 9px;
  text-align: left;
  vertical-align: top;
}
.bubble .content th {
  background: var(--green-soft);
  color: #24402f;
  font-weight: 600;
  white-space: nowrap;
}
.bubble .content tbody tr:nth-child(even) { background: #fbf8f1; }

.msg.user .bubble .content { white-space: pre-wrap; }

.cursor::after {
  content: "▌";
  color: var(--green);
  animation: blink 1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.status-line {
  font-size: 12.5px;
  color: var(--ink-soft);
  display: flex; align-items: center; gap: 7px;
  margin-bottom: 8px;
}
.spinner {
  width: 12px; height: 12px;
  border: 2px solid #d6cebd;
  border-top-color: var(--green);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.error-box {
  margin-top: 10px;
  background: #fdf1ed;
  border: 1px solid #f0cec3;
  color: #a4402a;
  padding: 9px 12px;
  border-radius: 10px;
  font-size: 13px;
}

/* 引用条目 */
.refs { margin-top: 14px; border-top: 1px dashed var(--line); padding-top: 11px; }
.refs-head {
  font-size: 12.5px; color: var(--green); cursor: pointer;
  display: flex; align-items: center; gap: 6px; user-select: none;
  background: none; border: none; padding: 0; font-family: inherit;
}
.refs-head .caret { transition: transform .18s ease; display: inline-block; }
.refs.collapsed .caret { transform: rotate(-90deg); }
.refs.collapsed .ref-list { display: none; }
.ref-list { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }

.ref-card {
  background: #fbf8f1;
  border: 1px solid var(--line);
  border-left: 3px solid #cbbfa6;
  border-radius: 9px;
  padding: 9px 12px;
  font-size: 13px;
}
.ref-card.top { border-left-color: var(--green); background: #f4f9f5; }
.ref-title { display: flex; justify-content: space-between; gap: 10px; align-items: baseline; }
.ref-title b { color: #24402f; font-weight: 600; }
.ref-title .code { color: #9b9384; font-size: 11.5px; margin-right: 6px; font-family: Consolas, monospace; }
.ref-score { font-size: 11.5px; color: var(--green); white-space: nowrap; }
.ref-def { margin: 6px 0 0; color: #5d574c; font-size: 12.5px; line-height: 1.65; }
.ref-aliases { color: #9b9384; font-size: 11.5px; }

.no-match {
  margin-top: 12px;
  font-size: 12.5px;
  color: #96703a;
  background: #fdf8ec;
  border: 1px solid #f0e2c2;
  border-radius: 9px;
  padding: 8px 11px;
}

/* 回答里的引用超链接：点开看原文 */
.bubble .content .cite-link {
  color: var(--green);
  text-decoration: none;
  border-bottom: 1px solid #bcd6c5;
  background: #eef5f0;
  border-radius: 5px;
  padding: 0 3px;
  font-size: .93em;
  font-family: Consolas, "Courier New", monospace;
  white-space: nowrap;
  cursor: pointer;
}
.bubble .content .cite-link:hover { background: #dcecdf; border-bottom-color: var(--green); }
.bubble .content .cite-link:focus-visible { outline: 2px solid #9cc6ac; outline-offset: 1px; }

.refs-hint { margin: 8px 0 0; font-size: 11.5px; color: #9b9384; }
.ref-card.clickable { cursor: pointer; transition: border-color .15s ease, background .15s ease; }
.ref-card.clickable:hover { border-color: #b9d3c2; background: #f6fbf7; }
.ref-card.clickable:focus-visible { outline: 2px solid #9cc6ac; outline-offset: 1px; }
.ref-cite {
  color: var(--green);
  font-size: 11.5px;
  font-family: Consolas, "Courier New", monospace;
  margin-right: 5px;
}

/* ------------------------------------------------------------ 引用原文弹窗 */
body.modal-open { overflow: hidden; }
.modal-backdrop {
  position: fixed; inset: 0; z-index: 120;
  background: rgba(43, 35, 22, .45);
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  box-shadow: 0 18px 48px rgba(50, 38, 20, .28);
  width: 100%; max-width: 760px; max-height: 84vh;
  display: flex; flex-direction: column;
  animation: modal-in .16s ease-out;
}
@keyframes modal-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
.modal-head {
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  padding: 15px 18px 11px; border-bottom: 1px solid var(--line);
}
.modal-heading { min-width: 0; }
.modal-head h3 { margin: 0; font-size: 15.5px; color: #24402f; }
.modal-sub { margin: 4px 0 0; font-size: 12px; color: var(--ink-soft); word-break: break-all; }
.modal-close {
  border: none; background: none; cursor: pointer;
  font-size: 22px; line-height: 1; color: #a89f8f; padding: 0 2px;
}
.modal-close:hover { color: var(--clay); }
.modal-body {
  overflow-y: auto; padding: 14px 18px;
  font-size: 13.5px; line-height: 1.8; color: #4c473e;
  white-space: pre-wrap; word-break: break-word;
}
.modal-foot {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 18px 13px; border-top: 1px solid var(--line);
}
.modal-meta { font-size: 11.5px; color: #9b9384; word-break: break-all; }
.modal-actions { display: flex; gap: 8px; flex: none; }

/* ------------------------------------------------------------ 输入区 */
.composer { padding: 12px 24px 18px; border-top: 1px solid var(--line); background: rgba(255, 253, 248, .8); }

.composer-box {
  display: flex; align-items: flex-end; gap: 10px;
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 14px;
  padding: 10px 12px;
  box-shadow: var(--shadow);
  max-width: 980px;
  margin: 0 auto;
  transition: border-color .15s ease;
}
.composer-box:focus-within { border-color: #b9d3c2; }

#input {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  font-family: inherit;
  font-size: 14.5px;
  line-height: 1.65;
  max-height: 160px;
  color: var(--ink);
}
#input::placeholder { color: #b3aa99; }

.composer-actions { display: flex; gap: 8px; align-items: center; }

.send-btn, .stop-btn {
  border: none;
  border-radius: 10px;
  padding: 8px 18px;
  font-size: 14px;
  cursor: pointer;
  font-family: inherit;
  transition: all .15s ease;
  white-space: nowrap;
}
.send-btn { background: var(--green); color: #fff; }
.send-btn:hover { background: #265a42; }
.send-btn:disabled { background: #c6c0b3; cursor: not-allowed; }
.stop-btn { background: #f3e7e0; color: var(--clay); border: 1px solid #e6cdc0; }

.hint { max-width: 980px; margin: 8px auto 0; font-size: 11.5px; color: #a89f8f; }

/* ------------------------------------------------------------ Toast */
.toast {
  position: fixed; left: 50%; bottom: 34px; transform: translateX(-50%);
  background: #3b3831; color: #fff;
  padding: 9px 16px; border-radius: 10px; font-size: 13px;
  box-shadow: 0 8px 24px rgba(0,0,0,.2);
  z-index: 99;
}

/* ------------------------------------------------------------ 滚动条 */
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-thumb { background: #d8cfbc; border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: #c7bca4; }
::-webkit-scrollbar-track { background: transparent; }

/* ------------------------------------------------------------ 响应式 */
@media (max-width: 900px) {
  .sidebar { display: none; }
  .messages { padding: 18px 14px 8px; }
  .composer { padding: 10px 14px 14px; }
}
