:root {
  --bg: #f6f6f3;
  --panel: #ffffff;
  --panel-2: #fbfbf8;
  --line: #e5e3dc;
  --line-strong: #d6d3c9;
  --text: #1b1b18;
  --text-2: #4a4a43;
  --muted: #827f75;
  --accent: #256e5a;
  --accent-hover: #1d5a49;
  --accent-soft: #e7f1ec;
  --user-bubble: #eceee9;
  --code-bg: #f4f4ef;
  --code-line: #e3e2da;
  --shadow: 0 1px 2px rgba(24, 24, 18, .05);
  --shadow-lg: 0 6px 28px rgba(24, 24, 18, .08);
  --r: 12px;
}

[data-theme="dark"] {
  --bg: #14140f;
  --panel: #1c1c17;
  --panel-2: #181814;
  --line: #2c2c24;
  --line-strong: #3a3a30;
  --text: #f1f0e8;
  --text-2: #cfcdc2;
  --muted: #8f8c80;
  --accent: #57a98d;
  --accent-hover: #6dbc9f;
  --accent-soft: #1e2b26;
  --user-bubble: #262620;
  --code-bg: #1a1a15;
  --code-line: #2e2e26;
  --shadow: 0 1px 2px rgba(0, 0, 0, .3);
  --shadow-lg: 0 8px 30px rgba(0, 0, 0, .45);
}

* { box-sizing: border-box; }

/* display 规则会盖掉 hidden 属性，这里强制生效 */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", "Source Han Sans SC", sans-serif;
  font-size: 15px;
  line-height: 1.7;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
::selection { background: var(--accent-soft); }

/* ---------------------------------------------------------------- 登录页 */

.gate {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(620px 380px at 50% -10%, var(--accent-soft), transparent 70%),
    var(--bg);
}

.gate-card {
  width: 100%;
  max-width: 348px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding: 34px 28px 26px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow-lg);
  text-align: center;
}

.gate-mark { color: var(--accent); line-height: 0; }
.gate-card h1 { margin: 4px 0 0; font-size: 19px; font-weight: 650; letter-spacing: -.2px; }
.gate-sub { margin: 0 0 8px; font-size: 13px; color: var(--muted); }

.gate-card input {
  width: 100%;
  padding: 11px 14px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  outline: none;
  font-size: 14px;
  transition: border-color .15s, box-shadow .15s;
  text-align: center;
  letter-spacing: .06em;
}
.gate-card input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  background: var(--panel);
}

.btn-primary {
  width: 100%;
  padding: 11px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: #fff;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: .02em;
  transition: background .15s, transform .1s;
}
.btn-primary:hover { background: var(--accent-hover); }
.btn-primary:active { transform: scale(.99); }
.btn-primary[disabled] { opacity: .55; cursor: default; }

.gate-err {
  min-height: 18px;
  margin: 0;
  font-size: 12.5px;
  color: #c0392b;
}

/* ------------------------------------------------------------------ 布局 */

.app {
  display: grid;
  grid-template-columns: 258px minmax(0, 1fr);
  height: 100%;
}

.sidebar {
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 16px 12px 12px;
  background: var(--panel-2);
  border-right: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 6px 14px;
  font-size: 14px;
  font-weight: 620;
  letter-spacing: .01em;
  color: var(--text-2);
}
.brand-dot {
  width: 8px; height: 8px;
  border-radius: 3px;
  background: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.brand > span:nth-child(2) {
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.brand .ghost-icon { margin-left: auto; }

.new-chat {
  display: flex;
  align-items: center;
  gap: 8px;
  width: 100%;
  padding: 9px 12px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 10px;
  font-size: 13.5px;
  font-weight: 520;
  color: var(--text);
  box-shadow: var(--shadow);
  transition: border-color .15s, box-shadow .15s, transform .1s;
}
.new-chat:hover { border-color: var(--line-strong); }
.new-chat:active { transform: scale(.99); }

.side-label {
  padding: 16px 8px 6px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: var(--muted);
}

.conv-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  scrollbar-width: thin;
}

.conv {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 8px 8px 10px;
  border-radius: 8px;
  font-size: 13px;
  color: var(--muted);
  cursor: pointer;
  transition: background .12s, color .12s;
  user-select: none;
}
.conv:hover { background: rgba(125, 122, 110, .08); color: var(--text-2); }
.conv.active {
  background: var(--panel);
  color: var(--text);
  box-shadow: var(--shadow);
  font-weight: 500;
}
.conv-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conv-del {
  flex: 0 0 auto;
  width: 20px; height: 20px;
  display: grid; place-items: center;
  border: 0; background: none; border-radius: 5px;
  color: var(--muted); opacity: 0;
  transition: opacity .12s, background .12s;
}
.conv:hover .conv-del, .conv.active .conv-del { opacity: .65; }
.conv-del:hover { opacity: 1 !important; background: rgba(125, 122, 110, .14); }

.side-foot {
  display: flex;
  gap: 6px;
  padding-top: 10px;
  margin-top: 6px;
  border-top: 1px solid var(--line);
}
.foot-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 7px;
  border: 0;
  border-radius: 8px;
  background: none;
  font-size: 12.5px;
  color: var(--muted);
  transition: background .12s, color .12s;
}
.foot-btn:hover { background: rgba(125, 122, 110, .09); color: var(--text-2); }

.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  background: var(--bg);
}

/* ------------------------------------------------------------------ 顶栏 */

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 20px;
  border-bottom: 1px solid transparent;
}

.model-pick { position: relative; display: inline-flex; align-items: center; }
.model-pick select {
  appearance: none;
  padding: 5px 26px 5px 10px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  font-size: 13px;
  font-weight: 550;
  color: var(--text-2);
  cursor: pointer;
  transition: background .15s, border-color .15s;
}
.model-pick select:hover { background: var(--panel); border-color: var(--line); }
.model-pick svg {
  position: absolute;
  right: 8px;
  pointer-events: none;
  color: var(--muted);
}

.status {
  margin-left: auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11.5px;
  color: var(--muted);
  letter-spacing: .01em;
}
.status i {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.status.warn i { background: #cf8a2c; box-shadow: 0 0 0 3px rgba(207, 138, 44, .16); }
.status.busy i { animation: pulse 1.3s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .25; } }
.status em { font-style: normal; }

/* ------------------------------------------------------------------ 对话 */

.thread {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 18px 22px 4px;
  scroll-behavior: smooth;
  scrollbar-width: thin;
  scrollbar-color: var(--line-strong) transparent;
}
.thread::-webkit-scrollbar { width: 8px; }
.thread::-webkit-scrollbar-thumb {
  background: var(--line-strong);
  border-radius: 4px;
  border: 2px solid var(--bg);
}

.thread-inner {
  max-width: 720px;
  margin: 0 auto;
  padding-bottom: 12px;
  display: flex;
  flex-direction: column;
  gap: 26px;
}

.welcome { padding: 12vh 0 0; text-align: left; }
.welcome h2 {
  margin: 0 0 6px;
  font-size: 27px;
  font-weight: 620;
  letter-spacing: -.6px;
  line-height: 1.3;
}
.welcome-sub { margin: 0 0 26px; font-size: 14px; color: var(--muted); }

.chips {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 10px;
  max-width: 540px;
}
.chip {
  padding: 13px 15px;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: 11px;
  background: var(--panel);
  font-size: 13.5px;
  color: var(--text-2);
  transition: border-color .15s, transform .12s, box-shadow .15s;
}
.chip:hover {
  border-color: var(--accent);
  color: var(--text);
  box-shadow: 0 3px 14px rgba(37, 110, 90, .09);
  transform: translateY(-1px);
}

.msg { display: flex; gap: 11px; }
.msg.user { justify-content: flex-end; }

.avatar {
  flex: 0 0 auto;
  width: 24px; height: 24px;
  margin-top: 3px;
  border-radius: 7px;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: -.02em;
}

.body { min-width: 0; max-width: 100%; }
.msg.user .body {
  max-width: 84%;
  padding: 10px 16px;
  background: var(--user-bubble);
  border-radius: 16px 16px 4px 16px;
  white-space: pre-wrap;
  word-break: break-word;
}

.md { word-break: break-word; }
.md > *:first-child { margin-top: 0; }
.md > *:last-child { margin-bottom: 0; }
.md p { margin: 0 0 12px; }
.md h1, .md h2, .md h3, .md h4 {
  margin: 20px 0 8px;
  font-weight: 620;
  letter-spacing: -.2px;
  line-height: 1.4;
}
.md h1 { font-size: 20px; }
.md h2 { font-size: 18px; }
.md h3 { font-size: 16px; }
.md h4 { font-size: 15px; }
.md ul, .md ol { margin: 0 0 12px; padding-left: 22px; }
.md li { margin: 3px 0; }
.md blockquote {
  margin: 0 0 12px;
  padding: 2px 0 2px 14px;
  border-left: 2px solid var(--line-strong);
  color: var(--muted);
}
.md hr { border: 0; border-top: 1px solid var(--line); margin: 18px 0; }
.md a { color: var(--accent); text-decoration: underline; text-underline-offset: 2px; }
.md strong { font-weight: 650; }
.md code.inline {
  padding: 1.5px 5px;
  border-radius: 5px;
  background: var(--code-bg);
  border: 1px solid var(--code-line);
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: .875em;
}

.code {
  margin: 0 0 14px;
  border: 1px solid var(--code-line);
  border-radius: 10px;
  overflow: hidden;
  background: var(--code-bg);
}
.code-bar {
  display: flex;
  align-items: center;
  padding: 6px 8px 6px 12px;
  border-bottom: 1px solid var(--code-line);
  font-size: 11.5px;
  color: var(--muted);
  letter-spacing: .04em;
  text-transform: lowercase;
}
.code-bar .copy {
  margin-left: auto;
  padding: 3px 8px;
  border: 0;
  border-radius: 5px;
  background: transparent;
  color: var(--muted);
  font-size: 11.5px;
  transition: background .12s, color .12s;
}
.code-bar .copy:hover { background: var(--line); color: var(--text); }
.code pre { margin: 0; padding: 12px 14px; overflow-x: auto; }
.code code {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  font-size: 12.8px;
  line-height: 1.62;
  white-space: pre;
}

.gen-caret {
  display: inline-block;
  width: 7px;
  height: 15px;
  margin-left: 3px;
  vertical-align: -2px;
  border-radius: 1.5px;
  background: var(--accent);
  animation: caret 1s steps(2) infinite;
}
@keyframes caret { 50% { opacity: 0; } }

.msg-actions {
  display: flex;
  gap: 2px;
  margin-top: 6px;
  opacity: 0;
  transition: opacity .15s;
}
.msg:hover .msg-actions { opacity: 1; }
.act {
  padding: 3px 8px;
  border: 0;
  border-radius: 6px;
  background: none;
  font-size: 11.5px;
  color: var(--muted);
  transition: background .12s, color .12s;
}
.act:hover { background: var(--panel); color: var(--text-2); }

.err-note {
  display: inline-block;
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid #e8c7c1;
  background: #fdf3f1;
  color: #a8382a;
  font-size: 13px;
}
[data-theme="dark"] .err-note {
  border-color: #4a2b26;
  background: #241715;
  color: #e08b7b;
}

/* ---------------------------------------------------------------- 输入区 */

.composer-wrap { padding: 6px 22px 16px; }

.composer {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  max-width: 720px;
  margin: 0 auto;
  padding: 7px 7px 7px 15px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 16px;
  box-shadow: var(--shadow);
  transition: border-color .16s, box-shadow .16s;
}
.composer:focus-within {
  border-color: var(--line-strong);
  box-shadow: 0 4px 20px rgba(24, 24, 18, .07);
}

#input {
  flex: 1;
  width: 100%;
  min-height: 26px;
  max-height: 190px;
  padding: 5px 0;
  border: 0;
  outline: none;
  resize: none;
  background: none;
  font-size: 14.5px;
  line-height: 1.62;
  scrollbar-width: thin;
}
#input::placeholder { color: var(--muted); }

.send, .stop {
  flex: 0 0 auto;
  width: 33px; height: 33px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 10px;
  transition: background .15s, transform .1s, opacity .15s;
}
.send { background: var(--accent); color: #fff; }
.send:hover:not([disabled]) { background: var(--accent-hover); }
.send:active:not([disabled]) { transform: scale(.94); }
.send[disabled] { background: var(--line); color: var(--muted); cursor: default; }
.stop { background: var(--user-bubble); color: var(--text-2); }
.stop:hover { background: var(--line-strong); }

.hint {
  max-width: 720px;
  margin: 9px auto 0;
  text-align: center;
  font-size: 11.5px;
  color: var(--muted);
  opacity: .85;
}

/* ---------------------------------------------------------------- 小屏幕 */

.ghost-icon {
  display: grid;
  place-items: center;
  width: 28px; height: 28px;
  border: 0;
  border-radius: 7px;
  background: none;
  color: var(--muted);
}
.ghost-icon:hover { background: rgba(125, 122, 110, .12); color: var(--text); }

.only-mobile { display: none !important; }
.scrim {
  position: fixed;
  inset: 0;
  background: rgba(20, 20, 16, .32);
  z-index: 15;
  backdrop-filter: blur(2px);
}

@media (max-width: 820px) {
  .app { grid-template-columns: 1fr; }
  .only-mobile { display: grid !important; }

  .sidebar {
    position: fixed;
    top: 0; bottom: 0; left: 0;
    width: 272px;
    z-index: 20;
    transform: translateX(-101%);
    transition: transform .24s cubic-bezier(.32, .72, 0, 1);
    box-shadow: var(--shadow-lg);
  }
  .sidebar.open { transform: none; }

  .thread { padding: 4px 15px 0; }
  .composer-wrap { padding: 6px 15px 12px; }
  .topbar { padding: 10px 12px; }
  .welcome { padding-top: 6vh; }
  .welcome h2 { font-size: 22px; }
  .chips { grid-template-columns: 1fr; }
  .msg.user .body { max-width: 88%; }
  .hint { display: none; }
  .thread-inner { gap: 22px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .001ms !important; transition-duration: .001ms !important; }
}

/* ---------------------------------------------------------- 附件 / 图片 */
.attach-btn {
  flex: 0 0 auto;
  width: 30px; height: 30px;
  display: grid; place-items: center;
  border: 0; border-radius: 9px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: background .15s, color .15s;
}
.attach-btn:hover { background: var(--panel-2); color: var(--text); }

.attach-strip {
  max-width: 720px;
  margin: 0 auto 8px;
  display: flex; flex-wrap: wrap; gap: 8px;
}
.att {
  position: relative;
  display: flex; align-items: center; gap: 7px;
  padding: 4px 6px 4px 4px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 11px;
  max-width: 230px;
  box-shadow: var(--shadow);
}
.att img { width: 34px; height: 34px; object-fit: cover; border-radius: 7px; display: block; }
.att-doc {
  width: 34px; height: 34px; border-radius: 7px;
  display: grid; place-items: center;
  background: var(--accent-soft); color: var(--accent);
  font-size: 10px; font-weight: 700; letter-spacing: .02em;
}
.att-name {
  font-size: 12px; color: var(--text-2);
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  max-width: 120px;
}
.att-del {
  border: 0; background: none; cursor: pointer;
  color: var(--muted); font-size: 16px; line-height: 1;
  padding: 0 2px;
}
.att-del:hover { color: var(--text); }

.u-imgs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 6px; }
.u-imgs img {
  max-width: 190px; max-height: 190px;
  border-radius: 10px; cursor: zoom-in;
  display: block; object-fit: cover;
}

/* 表格 */
.tbl-wrap { overflow-x: auto; margin: 12px 0; }
.tbl-wrap table { border-collapse: collapse; width: 100%; font-size: 13.5px; }
.tbl-wrap th, .tbl-wrap td {
  border: 1px solid var(--line);
  padding: 7px 11px;
  text-align: left;
  vertical-align: top;
}
.tbl-wrap th { background: var(--panel-2); font-weight: 600; }
.tbl-wrap tbody tr:nth-child(even) { background: var(--panel-2); }

/* 图表 */
.chart {
  margin: 12px 0;
  border: 1px solid var(--line);
  border-radius: var(--r);
  overflow: hidden;
  background: var(--panel);
}
.chart-bar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 6px 11px;
  background: var(--panel-2);
  border-bottom: 1px solid var(--line);
  font-size: 11.5px; color: var(--muted); letter-spacing: .06em;
}
.chart-bar button {
  border: 0; background: none; cursor: pointer;
  color: var(--muted); font-size: 11.5px;
}
.chart-bar button:hover { color: var(--text); }
.chart-body {
  padding: 12px;
  overflow-x: auto;
  display: flex;
  justify-content: center;
}
.chart-body svg { max-width: 100%; height: auto; }
.chart-raw {
  margin: 0; white-space: pre-wrap; word-break: break-word;
  font-size: 12px; color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  text-align: left; width: 100%;
}

/* 拖拽覆盖层 */
body.dragging::after {
  content: "松开即可添加图片或 PDF";
  position: fixed; inset: 10px;
  display: grid; place-items: center;
  border: 2px dashed var(--accent);
  border-radius: 16px;
  background: rgba(37, 110, 90, .06);
  color: var(--accent);
  font-size: 15px;
  font-weight: 600;
  z-index: 90;
  pointer-events: none;
}

/* 轻提示 */
#toast {
  position: fixed; left: 50%; bottom: 96px;
  transform: translate(-50%, 10px);
  padding: 9px 16px;
  background: var(--text);
  color: var(--bg);
  border-radius: 10px;
  font-size: 13px;
  opacity: 0; pointer-events: none;
  transition: opacity .18s, transform .18s;
  z-index: 99;
  max-width: 80vw;
}
#toast.show { opacity: .94; transform: translate(-50%, 0); }

/* 联网开关 */
.net-btn {
  flex: 0 0 auto;
  height: 26px;
  padding: 0 10px;
  margin-bottom: 2px;
  border: 1px solid var(--line);
  border-radius: 13px;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
  transition: all .15s;
  white-space: nowrap;
}
.net-btn:hover { border-color: var(--line-strong); color: var(--text-2); }
.net-btn.on {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
  font-weight: 600;
}

/* 参考来源 */
.sources {
  margin-top: 10px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel-2);
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.sources-h {
  font-size: 11.5px;
  color: var(--muted);
  letter-spacing: .06em;
  margin-bottom: 1px;
}
.sources a {
  font-size: 12.5px;
  color: var(--accent);
  text-decoration: none;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.sources a:hover { text-decoration: underline; }
