:root {
  --bg: #f7f7f5;
  --panel: #ffffff;
  --sidebar: #efefec;
  --text: #1f2328;
  --muted: #6b7280;
  --border: #e3e3df;
  --accent: #76b900;
  --accent-text: #ffffff;
  --user-bubble: #e9f3d6;
  --code-bg: #1e1f22;
  --danger: #c93c37;
  --radius: 10px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181a;
    --panel: #1f2023;
    --sidebar: #131416;
    --text: #e6e6e3;
    --muted: #9a9ca3;
    --border: #2e3034;
    --accent: #76b900;
    --accent-text: #0d0d0d;
    --user-bubble: #2a3320;
    --code-bg: #0f1012;
    --danger: #ef6b64;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.6 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}
button, input, select, textarea { font: inherit; color: inherit; }

.btn-primary {
  background: var(--accent); color: var(--accent-text); border: 0;
  border-radius: 8px; padding: 8px 16px; cursor: pointer; font-weight: 600;
}
.btn-primary:disabled { opacity: .5; cursor: not-allowed; }
.btn-primary.stop { background: var(--danger); color: #fff; }
.btn-link { background: none; border: 0; color: var(--muted); cursor: pointer; text-decoration: underline; padding: 0; justify-self: start; }
.icon-btn {
  background: none; border: 1px solid transparent; border-radius: 8px;
  width: 36px; height: 36px; cursor: pointer; font-size: 17px; flex-shrink: 0;
  display: inline-flex; align-items: center; justify-content: center; color: var(--muted);
}
.icon-btn:hover:not(:disabled) { color: var(--text); }
.input-row .icon-btn { height: 40px; }
.icon-btn:hover { background: var(--sidebar); }
.icon-btn:disabled { opacity: .35; cursor: not-allowed; }

/* 布局 */
.layout { display: flex; height: 100%; }
.sidebar {
  width: 260px; flex-shrink: 0; background: var(--sidebar);
  border-right: 1px solid var(--border); display: flex; flex-direction: column; padding: 12px;
}
.new-chat { width: 100%; margin-bottom: 12px; }
.conv-list { list-style: none; margin: 0; padding: 0; overflow-y: auto; flex: 1; }
.conv-list li {
  display: flex; align-items: center; gap: 4px; padding: 8px 10px; border-radius: 8px;
  cursor: pointer; font-size: 14px;
}
.conv-list li:hover { background: var(--border); }
.conv-list li.active { background: var(--panel); box-shadow: 0 0 0 1px var(--border); }
.conv-list .title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-list .del { visibility: hidden; background: none; border: 0; color: var(--muted); cursor: pointer; }
.conv-list li:hover .del { visibility: visible; }
.backdrop { display: none; }

.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  display: flex; align-items: center; gap: 8px; padding: 10px 16px;
  border-bottom: 1px solid var(--border); background: var(--panel);
}
.menu-btn { display: none; }
.model-picker { display: flex; align-items: center; gap: 4px; min-width: 0; }
.model-picker select {
  max-width: 340px; min-width: 0; padding: 6px 8px; border-radius: 8px;
  border: 1px solid var(--border); background: var(--bg);
}
.badges { display: flex; gap: 6px; flex: 1; flex-wrap: wrap; min-width: 0; }
.badge { font-size: 12px; padding: 1px 8px; border-radius: 99px; border: 1px solid var(--border); color: var(--muted); white-space: nowrap; }
.badge.on { border-color: var(--accent); color: var(--accent); }

.settings {
  display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px 20px;
  padding: 12px 16px; background: var(--panel); border-bottom: 1px solid var(--border); font-size: 13px;
}
.settings[hidden] { display: none; }
.settings label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); }
.settings .full { grid-column: 1 / -1; }
.settings textarea, .settings input[type=number] {
  padding: 6px 8px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); resize: vertical;
}
.settings input[type=range] { accent-color: var(--accent); }

/* 消息 */
.messages { flex: 1; overflow-y: auto; padding: 24px 16px; }
.empty { max-width: 560px; margin: 12vh auto 0; text-align: center; color: var(--muted); }
.empty h1 { color: var(--text); font-size: 28px; margin-bottom: 8px; }
.msg { max-width: 820px; margin: 0 auto 20px; }
.msg.user { display: flex; flex-direction: column; align-items: flex-end; }
.msg.user .bubble {
  background: var(--user-bubble); padding: 10px 14px; border-radius: 14px 14px 4px 14px;
  max-width: 85%; white-space: pre-wrap; word-break: break-word;
}
.msg .imgs { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; margin-bottom: 6px; }
.msg .imgs img { max-width: 180px; max-height: 180px; border-radius: 8px; border: 1px solid var(--border); cursor: zoom-in; }
.msg.assistant .meta { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.msg .content { word-break: break-word; }
.msg .content p:first-child { margin-top: 0; }
.msg .content pre {
  background: var(--code-bg); color: #e6e6e3; padding: 12px; border-radius: 8px; overflow-x: auto; font-size: 13px;
}
.msg .content code { font-family: ui-monospace, Consolas, "Courier New", monospace; }
.msg .content :not(pre) > code { background: var(--sidebar); padding: 1px 5px; border-radius: 4px; font-size: .9em; }
.msg .content table { border-collapse: collapse; display: block; overflow-x: auto; }
.msg .content th, .msg .content td { border: 1px solid var(--border); padding: 4px 10px; }
.msg .content img { max-width: 100%; }
.reasoning { margin-bottom: 8px; font-size: 13px; color: var(--muted); }
.reasoning summary { cursor: pointer; user-select: none; }
.reasoning .body { white-space: pre-wrap; border-left: 3px solid var(--border); padding: 4px 0 4px 12px; margin-top: 6px; max-height: 320px; overflow-y: auto; }
.msg .error { color: var(--danger); font-size: 14px; }
.msg .actions { margin-top: 4px; display: flex; gap: 10px; font-size: 12px; }
.msg .actions button { background: none; border: 0; color: var(--muted); cursor: pointer; padding: 0; }
.msg .actions button:hover { color: var(--text); }
.cursor::after { content: "▍"; animation: blink 1s steps(2) infinite; color: var(--accent); }
@keyframes blink { 50% { opacity: 0; } }

/* 输入区 */
.composer { padding: 10px 16px 14px; background: var(--panel); border-top: 1px solid var(--border); }
.attachments { display: flex; gap: 8px; flex-wrap: wrap; max-width: 820px; margin: 0 auto; }
.attachments:not(:empty) { margin-bottom: 8px; }
.attachments .thumb { position: relative; }
.attachments img { width: 64px; height: 64px; object-fit: cover; border-radius: 8px; border: 1px solid var(--border); }
.attachments .thumb button {
  position: absolute; top: -6px; right: -6px; width: 20px; height: 20px; border-radius: 50%;
  border: 0; background: var(--danger); color: #fff; font-size: 12px; cursor: pointer; line-height: 20px; padding: 0;
}
.input-row { display: flex; gap: 8px; align-items: flex-end; max-width: 820px; margin: 0 auto; }
.input-row textarea {
  flex: 1; resize: none; padding: 8px 12px; border: 1px solid var(--border); border-radius: 10px;
  background: var(--bg); max-height: 200px; min-height: 40px; line-height: 1.5;
}
.input-row textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.input-row .btn-primary { height: 40px; }
.hint { max-width: 820px; margin: 6px auto 0; font-size: 12px; color: var(--muted); min-height: 1em; }

/* 看大图 */
.lightbox { position: fixed; inset: 0; background: rgba(0,0,0,.8); display: flex; align-items: center; justify-content: center; z-index: 50; cursor: zoom-out; }
.lightbox img { max-width: 94vw; max-height: 94vh; }

/* 用户栏 */
.user-bar {
  display: flex; align-items: center; gap: 10px; padding: 10px 4px 0; margin-top: 8px;
  border-top: 1px solid var(--border); font-size: 13px;
}
.user-bar .user-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }

/* 管理页 */
.admin-body { overflow-y: auto; }
.admin { max-width: 900px; margin: 0 auto; padding: 24px 16px 48px; }
.admin-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.admin-head h1 { font-size: 22px; margin: 0; }
.admin-head .btn-link { text-decoration: none; }
.admin-add { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.admin-add input[type=text] {
  flex: 1; min-width: 180px; padding: 8px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel);
}
.admin-add .check { display: flex; align-items: center; gap: 4px; color: var(--muted); font-size: 14px; }
.admin-add .check input { accent-color: var(--accent); }
.admin .error { color: var(--danger); min-height: 1em; font-size: 14px; margin: 8px 0; }
.cred {
  border: 1px solid var(--accent); border-radius: var(--radius); background: var(--panel);
  padding: 14px 16px; margin-bottom: 16px;
}
.cred-text { white-space: pre-wrap; font-family: ui-monospace, Consolas, monospace; font-size: 14px; user-select: all; }
.cred-actions { display: flex; align-items: center; gap: 16px; margin-top: 12px; }
.cred .muted { color: var(--muted); font-size: 12px; margin: 10px 0 0; }
.table-wrap { overflow-x: auto; }
.users { width: 100%; border-collapse: collapse; font-size: 14px; }
.users th, .users td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
.users th { color: var(--muted); font-weight: 500; font-size: 13px; }
.users .ops { display: flex; gap: 14px; justify-content: flex-end; }
.users .ops .btn-link { text-decoration: none; color: var(--accent); }
.users .ops .btn-link.danger { color: var(--danger); }
@media (max-width: 640px) {
  .users thead { display: none; }
  .users tr { display: block; border-bottom: 1px solid var(--border); padding: 8px 0; }
  .users td { display: flex; justify-content: space-between; border: 0; padding: 4px 0; }
  .users td::before { content: attr(data-label); color: var(--muted); }
  .users .ops { justify-content: flex-start; padding-top: 8px; }
  .users .ops::before { content: none; }
}

/* 弹窗 */
.dialog {
  border: 1px solid var(--border); border-radius: var(--radius); background: var(--panel); color: var(--text);
  padding: 20px; width: min(360px, calc(100vw - 32px));
}
.dialog::backdrop { background: rgba(0,0,0,.45); }
.dialog form { display: flex; flex-direction: column; gap: 10px; }
.dialog h3 { margin: 0 0 4px; }
.dialog input { padding: 9px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); }
.dialog .error { color: var(--danger); min-height: 1em; margin: 0; font-size: 13px; }
.dialog-actions { display: flex; justify-content: flex-end; align-items: center; gap: 16px; }

/* 登录 */
.login { max-width: 320px; margin: 18vh auto; display: flex; flex-direction: column; gap: 12px; padding: 0 16px; text-align: center; }
.login input { padding: 10px 12px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel); }
.login .error { color: var(--danger); min-height: 1em; margin: 0; }
.login .muted { color: var(--muted); font-size: 13px; margin: 0; }

/* 手机 */
@media (max-width: 760px) {
  .sidebar {
    position: fixed; inset: 0 auto 0 0; z-index: 30; transform: translateX(-100%); transition: transform .2s;
  }
  .sidebar.open { transform: none; }
  .backdrop.show { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 20; }
  .menu-btn { display: inline-block; }
  .topbar { padding: 8px 10px; flex-wrap: wrap; }
  .model-picker { flex: 1; }
  .model-picker select { max-width: none; width: 100%; }
  .badges { order: 5; flex-basis: 100%; }
  .settings { grid-template-columns: 1fr; }
  .messages { padding: 16px 12px; }
  .composer { padding: 8px 10px 10px; }
  .msg.user .bubble { max-width: 92%; }
}
