:root {
  --brand: #0e7a63;
  --brand-dark: #0a5f4d;
  --brand-soft: #e8f4f0;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --line: #e5e8ec;
  --line-strong: #d3d8de;
  --text: #16181d;
  --text-2: #4b5563;
  --text-3: #8b95a1;
  --amber: #b45309;
  --amber-soft: #fef3e2;
  --danger: #b42318;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05), 0 8px 24px -12px rgba(16, 24, 40, .12);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Microsoft YaHei", "PingFang SC", "Hiragino Sans GB", sans-serif;
  font-size: 15px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

button { font-family: inherit; font-size: inherit; cursor: pointer; }

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

/* ------------------------------- 侧边栏 ------------------------------- */
.sidebar {
  width: 264px;
  flex: 0 0 264px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  padding: 14px 12px;
  gap: 12px;
  overflow: hidden;
}

.brand { display: flex; align-items: center; gap: 10px; padding: 4px 6px 8px; }
.logo {
  width: 36px; height: 36px; border-radius: 10px;
  background: linear-gradient(150deg, #12a184, var(--brand-dark));
  color: #fff; display: grid; place-items: center;
  box-shadow: 0 4px 12px -4px rgba(14, 122, 99, .6);
}
.brand-text strong { display: block; font-size: 16px; letter-spacing: .5px; }
.brand-text span { font-size: 12px; color: var(--text-3); }

.new-chat {
  display: flex; align-items: center; justify-content: center; gap: 7px;
  padding: 9px 12px; border-radius: 10px;
  border: 1px solid var(--brand); background: var(--brand); color: #fff;
  font-weight: 500; font-size: 14px; transition: .15s;
}
.new-chat:hover { background: var(--brand-dark); border-color: var(--brand-dark); }

.side-section { display: flex; flex-direction: column; gap: 4px; min-height: 0; }
.side-section.grow { flex: 1 1 auto; min-height: 0; overflow: hidden; }
.side-section h4 {
  margin: 6px 6px 2px; font-size: 11px; font-weight: 600;
  letter-spacing: .08em; color: var(--text-3); text-transform: uppercase;
}

.scene-list { display: flex; flex-direction: column; gap: 1px; }
.scene {
  display: flex; align-items: center; gap: 8px;
  padding: 7px 8px; border: 0; background: none; border-radius: 8px;
  color: var(--text-2); font-size: 13.5px; text-align: left; transition: .12s;
}
.scene:hover { background: var(--brand-soft); color: var(--brand-dark); }
.scene .dot {
  width: 5px; height: 5px; border-radius: 50%;
  background: var(--line-strong); flex: 0 0 5px;
}
.scene:hover .dot { background: var(--brand); }

.history { min-height: 0; overflow-y: auto; display: flex; flex-direction: column; gap: 1px; padding-bottom: 6px; }
.hist-item {
  display: flex; align-items: center; gap: 6px;
  padding: 7px 8px; border-radius: 8px; border: 0; background: none;
  color: var(--text-2); font-size: 13px; text-align: left; width: 100%;
}
.hist-item span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.hist-item:hover { background: #f2f4f6; }
.hist-item.active { background: var(--brand-soft); color: var(--brand-dark); font-weight: 500; }
.hist-del {
  border: 0; background: none; color: var(--text-3); font-size: 15px; line-height: 1;
  padding: 2px 4px; border-radius: 5px; opacity: 0;
}
.hist-item:hover .hist-del { opacity: 1; }
.hist-del:hover { background: #e9ecf0; color: var(--danger); }

.side-footer {
  border-top: 1px solid var(--line); padding-top: 10px;
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
}
.ghost {
  border: 1px solid var(--line); background: var(--panel); color: var(--text-2);
  border-radius: 8px; padding: 6px 10px; font-size: 13px;
  display: inline-flex; align-items: center; gap: 6px;
}
.ghost:hover { background: #f2f4f6; }
.model-chip { font-size: 11px; color: var(--text-3); font-variant-numeric: tabular-nums; }

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

.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 18px; background: rgba(255, 255, 255, .86);
  backdrop-filter: blur(8px); border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}
.title-wrap { flex: 1; min-width: 0; }
.title-wrap h1 {
  margin: 0; font-size: 15px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.title-wrap .sub { font-size: 11.5px; color: var(--text-3); }
.icon-btn {
  border: 1px solid transparent; background: none; color: var(--text-2);
  width: 32px; height: 32px; border-radius: 8px; display: grid; place-items: center;
}
.icon-btn:hover { background: #f2f4f6; border-color: var(--line); }
.menu-btn { display: none; }

.chat {
  flex: 1; overflow-y: auto; padding: 26px 18px 8px;
  display: flex; flex-direction: column; gap: 20px; scroll-behavior: smooth;
}
.chat-inner-narrow { max-width: 820px; margin: 0 auto; width: 100%; }

/* 欢迎态 */
.welcome { max-width: 760px; margin: 4vh auto 0; text-align: center; padding: 0 8px; }
.welcome .badge {
  display: inline-flex; align-items: center; gap: 6px;
  background: var(--brand-soft); color: var(--brand-dark);
  border-radius: 999px; padding: 4px 12px; font-size: 12px; font-weight: 500;
}
.welcome h2 { font-size: 24px; margin: 14px 0 8px; letter-spacing: -.01em; }
.welcome p { color: var(--text-2); font-size: 14px; margin: 0 auto; max-width: 560px; }
.welcome .maker {
  margin-top: 10px; font-size: 12px; color: var(--text-3);
  letter-spacing: .02em;
}
.welcome .grid {
  display: grid; grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px; margin-top: 22px; text-align: left;
}
.wcard {
  border: 1px solid var(--line); background: var(--panel); border-radius: var(--radius);
  padding: 13px 14px; transition: .15s;
}
.wcard:hover { border-color: var(--brand); box-shadow: var(--shadow); transform: translateY(-1px); }
.wcard b { display: block; font-size: 13.5px; margin-bottom: 3px; }
.wcard small { color: var(--text-3); font-size: 12px; line-height: 1.5; display: block; }
.wcard button { all: unset; cursor: pointer; display: block; width: 100%; }

/* 消息 */
.msg { display: flex; gap: 12px; max-width: 820px; margin: 0 auto; width: 100%; }
.msg .avatar {
  width: 30px; height: 30px; border-radius: 9px; flex: 0 0 30px;
  display: grid; place-items: center; font-size: 12px; font-weight: 600;
}
.msg.user .avatar { background: #eef1f4; color: var(--text-2); }
.msg.assistant .avatar {
  background: linear-gradient(150deg, #12a184, var(--brand-dark)); color: #fff;
}
.msg .body { flex: 1; min-width: 0; }
.msg .who { font-size: 11.5px; color: var(--text-3); margin-bottom: 3px; }
.msg.user .bubble {
  background: #eef1f4; border-radius: 12px; padding: 9px 13px;
  display: inline-block; max-width: 100%; white-space: pre-wrap; word-break: break-word;
}
.msg.assistant .bubble { padding: 0; }

.copy-row { margin-top: 6px; display: flex; gap: 6px; opacity: 0; transition: .15s; }
.msg:hover .copy-row { opacity: 1; }
.copy-btn {
  border: 1px solid var(--line); background: var(--panel); color: var(--text-3);
  border-radius: 6px; padding: 2px 8px; font-size: 11.5px;
}
.copy-btn:hover { color: var(--brand-dark); border-color: var(--brand); }

.dots { display: inline-flex; gap: 4px; padding: 6px 0; }
.dots i {
  width: 5px; height: 5px; border-radius: 50%; background: var(--text-3);
  animation: blink 1.2s infinite;
}
.dots i:nth-child(2) { animation-delay: .18s; }
.dots i:nth-child(3) { animation-delay: .36s; }
@keyframes blink { 0%, 60%, 100% { opacity: .25 } 30% { opacity: 1 } }

/* 思考中：只给进度，不把模型内部的英文自言自语摊给业务人员看 */
.status {
  display: flex; align-items: center; gap: 8px;
  margin: 0 0 2px; font-size: 13px; color: var(--text-3);
}
.status b { font-variant-numeric: tabular-nums; color: var(--text-2); font-weight: 600; }
.spin {
  width: 12px; height: 12px; border-radius: 50%; flex: 0 0 12px;
  border: 2px solid var(--line-strong); border-top-color: var(--brand);
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.err {
  border-left: 3px solid var(--danger); background: #fff5f4;
  padding: 9px 12px; border-radius: 0 8px 8px 0; color: var(--danger); font-size: 13.5px;
}

/* Markdown */
.md { font-size: 14.5px; line-height: 1.72; word-break: break-word; }
.md > *:first-child { margin-top: 0; }
.md > *:last-child { margin-bottom: 0; }
.md h1 { font-size: 19px; margin: 20px 0 10px; padding-bottom: 6px; border-bottom: 1px solid var(--line); }
.md h2 { font-size: 16.5px; margin: 20px 0 8px; }
.md h3 { font-size: 15px; margin: 16px 0 6px; }
.md h4 { font-size: 14px; margin: 14px 0 4px; color: var(--text-2); }
.md p { margin: 9px 0; }
.md strong { font-weight: 600; }
.md em { font-style: normal; background: var(--amber-soft); color: var(--amber); padding: 0 3px; border-radius: 3px; }
.md ul, .md ol { margin: 8px 0; padding-left: 22px; }
.md li { margin: 3px 0; }
.md li > ul, .md li > ol { margin: 2px 0; }
.md ul ul { list-style: disc; }
.md ul ul ul { list-style: square; }
.md blockquote {
  margin: 10px 0; padding: 8px 14px; border-left: 3px solid var(--brand);
  background: var(--brand-soft); border-radius: 0 8px 8px 0; color: var(--text-2);
}
.md code {
  background: #f1f3f5; border-radius: 4px; padding: 1px 5px;
  font-size: 12.8px; font-family: "SF Mono", Consolas, "Courier New", monospace;
}
.md pre {
  background: #1a1d23; color: #e6e9ee; padding: 12px 14px; border-radius: 10px;
  overflow-x: auto; margin: 10px 0; font-size: 12.8px; line-height: 1.6;
}
.md pre code { background: none; padding: 0; color: inherit; }
.md hr { border: 0; border-top: 1px solid var(--line); margin: 16px 0; }
.md a { color: var(--brand-dark); text-decoration: underline; text-underline-offset: 2px; }
.md .table-wrap { overflow-x: auto; margin: 12px 0; border: 1px solid var(--line); border-radius: 10px; }
.md table { border-collapse: collapse; width: 100%; font-size: 13.3px; }
.md th, .md td {
  border-bottom: 1px solid var(--line); border-right: 1px solid var(--line);
  padding: 7px 10px; text-align: left; vertical-align: top;
}
.md th { background: #f7f9fa; font-weight: 600; white-space: nowrap; }
.md tr:last-child td { border-bottom: 0; }
.md th:last-child, .md td:last-child { border-right: 0; }
.md tbody tr:hover { background: #fafbfc; }

.cursor::after {
  content: ""; display: inline-block; width: 7px; height: 15px;
  background: var(--brand); margin-left: 2px; vertical-align: -2px;
  animation: caret .9s steps(2) infinite;
}
@keyframes caret { 0%, 50% { opacity: 1 } 50.1%, 100% { opacity: 0 } }

/* 输入区 */
.composer-wrap { flex: 0 0 auto; padding: 8px 18px 14px; }
.composer {
  max-width: 820px; margin: 0 auto; display: flex; align-items: flex-end; gap: 8px;
  background: var(--panel); border: 1px solid var(--line-strong);
  border-radius: 14px; padding: 8px 8px 8px 14px; box-shadow: var(--shadow);
  transition: border-color .15s;
}
.composer:focus-within { border-color: var(--brand); }.composer textarea {
  flex: 1; border: 0; outline: 0; resize: none; background: none;
  font-family: inherit; font-size: 14.5px; line-height: 1.6;
  max-height: 190px; padding: 5px 0; color: var(--text);
}
.composer textarea::placeholder { color: var(--text-3); }
.send {
  border: 0; background: var(--brand); color: #fff; width: 34px; height: 34px;
  border-radius: 9px; display: grid; place-items: center; flex: 0 0 34px; transition: .15s;
}
.send:hover { background: var(--brand-dark); }
.send:disabled { background: var(--line-strong); cursor: not-allowed; }
.send.stop { background: var(--danger); }
.hint {
  max-width: 820px; margin: 8px auto 0; font-size: 11px; line-height: 1.5;
  color: var(--text-3); text-align: center; padding-bottom: 2px;
}

/* 弹窗 */
.overlay {
  position: fixed; inset: 0; background: rgba(16, 24, 40, .38);
  opacity: 0; pointer-events: none; transition: .18s; z-index: 30;
}
.overlay.show { opacity: 1; pointer-events: auto; }
.modal {
  position: fixed; inset: 0; display: grid; place-items: center; z-index: 50;
  opacity: 0; pointer-events: none; transition: .18s; padding: 16px;
}
.modal.show { opacity: 1; pointer-events: auto; }
.modal-card {
  background: var(--panel); border-radius: 14px; padding: 20px;
  width: min(440px, 100%); box-shadow: 0 24px 60px -20px rgba(16, 24, 40, .4);
  display: flex; flex-direction: column; gap: 12px;
}
.modal-card h3 { margin: 0; font-size: 16px; }
.modal-desc { margin: -4px 0 2px; font-size: 12.5px; color: var(--text-3); line-height: 1.6; }
.modal-card label { display: flex; flex-direction: column; gap: 5px; font-size: 13px; color: var(--text-2); }
.modal-card input {
  border: 1px solid var(--line-strong); border-radius: 9px; padding: 9px 11px;
  font-size: 13.5px; outline: 0; font-family: inherit;
}
.modal-card input:focus { border-color: var(--brand); }
.modal-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 4px; }
.modal-foot > div { display: flex; gap: 8px; }
.primary {
  border: 1px solid var(--brand); background: var(--brand); color: #fff;
  border-radius: 9px; padding: 7px 16px; font-size: 13.5px;
}
.primary:hover { background: var(--brand-dark); }

/* 附件与语音 */
.tool {
  border: 1px solid transparent; background: none; color: var(--text-3);
  width: 32px; height: 32px; border-radius: 8px; display: grid; place-items: center;
  flex: 0 0 32px; margin-bottom: 1px; transition: .15s;
}
.tool:hover { background: #f2f4f6; color: var(--brand-dark); }
.tool.rec {
  background: #fdeae8; color: var(--danger);
  animation: micpulse 1.3s ease-out infinite;
}
@keyframes micpulse {
  0% { box-shadow: 0 0 0 0 rgba(180, 35, 24, .35); }
  70%, 100% { box-shadow: 0 0 0 7px rgba(180, 35, 24, 0); }
}

.attach-strip { max-width: 820px; margin: 0 auto 6px; display: none; flex-wrap: wrap; gap: 6px; }
.attach-strip.show { display: flex; }
.chip {
  display: inline-flex; align-items: center; gap: 7px; max-width: 100%;
  border: 1px solid var(--line-strong); background: var(--panel);
  border-radius: 9px; padding: 4px 9px; font-size: 12px; color: var(--text-2);
}
.chip.parsing { opacity: .72; }
.chip.error { border-color: #f0c2bd; background: #fff5f4; color: var(--danger); }
.chip-name {
  font-weight: 500; max-width: 230px; overflow: hidden;
  text-overflow: ellipsis; white-space: nowrap;
}
.chip-meta { color: var(--text-3); white-space: nowrap; }
.chip.error .chip-meta { color: var(--danger); }
.chip-x { cursor: pointer; color: var(--text-3); padding: 0 2px; font-size: 14px; line-height: 1; }
.chip-x:hover { color: var(--danger); }

#toast {
  position: fixed; left: 50%; bottom: 104px; transform: translate(-50%, 14px);
  background: rgba(22, 24, 29, .93); color: #fff; font-size: 13px; line-height: 1.6;
  padding: 9px 15px; border-radius: 10px; max-width: min(540px, 88vw); z-index: 60;
  opacity: 0; pointer-events: none; transition: .2s; text-align: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }

/* 响应式 */
@media (max-width: 860px) {
  .sidebar {
    position: fixed; z-index: 40; top: 0; bottom: 0; left: 0;
    transform: translateX(-100%); transition: transform .2s ease;
    box-shadow: 0 0 40px rgba(16, 24, 40, .18);
    height: 100%;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    padding-bottom: calc(14px + env(safe-area-inset-bottom, 0px));
  }
  .sidebar.open { transform: none; }
  /* 手机上让整条侧栏滚动，内部不再嵌套滚动，否则底部「设置」会被挤出去 */
  .side-section.grow { flex: 0 0 auto; overflow: visible; }
  .history { overflow: visible; max-height: none; }
  .side-footer {
    position: sticky; bottom: 0; background: var(--panel);
    padding-bottom: calc(4px + env(safe-area-inset-bottom, 0px));
    border-top: 1px solid var(--line);
  }
  .menu-btn { display: grid; }
  .chat { padding: 18px 12px 6px; }
  .composer-wrap { padding: 6px 12px calc(10px + env(safe-area-inset-bottom, 0px)); }
  .welcome .grid { grid-template-columns: 1fr; }
  .topbar { padding: 8px 12px; }
  .title-wrap .sub { display: none; }
}
