:root {
  --bg: #faf9f6;
  --sidebar-bg: #f2f0eb;
  --surface: #ffffff;
  --surface-soft: #eeece6;
  --surface-hover: #e6e3dc;
  --border: #e6e3dc;
  --text: #1a1917;
  --text-muted: #73706a;
  --primary: #1a1917;
  --primary-fg: #faf9f6;
  --user-bubble: #eeece6;
  --user-text: #1a1917;
  --danger: #b83a34;
  --code-bg: #1e1d1b;
  --code-text: #eceae4;
  --shadow-sm: 0 1px 3px rgba(26, 25, 23, 0.05);
  --shadow-md: 0 6px 20px rgba(26, 25, 23, 0.07);
  --shadow-lg: 0 14px 36px rgba(26, 25, 23, 0.11);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161615;
    --sidebar-bg: #1d1d1b;
    --surface: #232320;
    --surface-soft: #2a2926;
    --surface-hover: #33322e;
    --border: #2f2e2a;
    --text: #eceae4;
    --text-muted: #96938c;
    --primary: #eceae4;
    --primary-fg: #161615;
    --user-bubble: #2a2926;
    --user-text: #eceae4;
    --danger: #e06c65;
    --code-bg: #111110;
    --code-text: #eceae4;
    --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.25);
    --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.35);
    --shadow-lg: 0 16px 40px rgba(0, 0, 0, 0.45);
  }
}

* {
  box-sizing: border-box;
}

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

.hidden {
  display: none !important;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

/* Toast 轻提示 */
.toast {
  position: fixed;
  top: 18px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--primary);
  color: var(--primary-fg);
  padding: 7px 16px;
  border-radius: 999px;
  font-size: 13.5px;
  z-index: 120;
  box-shadow: var(--shadow-md);
  pointer-events: none;
}

/* 登录页 */
.login-view {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.login-card {
  width: 100%;
  max-width: 380px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 34px 26px 28px;
  box-shadow: var(--shadow-md);
  text-align: center;
}

.login-logo {
  margin: 0 auto 14px;
  display: flex;
  justify-content: center;
}

.login-card h1 {
  margin: 0 0 6px;
  font-size: 23px;
  font-weight: 600;
  letter-spacing: -0.02em;
}

.login-sub {
  margin: 0 0 22px;
  color: var(--text-muted);
  font-size: 14px;
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.password-row {
  display: flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  padding: 4px 8px 4px 14px;
  transition: border-color 0.15s;
}

.password-row:focus-within {
  border-color: var(--text-muted);
}

.password-row input {
  flex: 1;
  border: none;
  background: transparent;
  outline: none;
  padding: 9px 0;
  min-width: 0;
}

.text-pill-btn {
  border: none;
  background: var(--surface-soft);
  color: var(--text-muted);
  border-radius: 8px;
  padding: 5px 10px;
  font-size: 13px;
  cursor: pointer;
}

.text-pill-btn:hover {
  color: var(--text);
}

.primary-btn {
  border: none;
  border-radius: 12px;
  background: var(--primary);
  color: var(--primary-fg);
  font-weight: 600;
  padding: 12px 18px;
  cursor: pointer;
  transition: opacity 0.15s;
}

.primary-btn:hover {
  opacity: 0.9;
}

.primary-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.form-error {
  color: var(--danger);
  font-size: 13.5px;
  text-align: left;
  padding: 2px 4px;
}

/* 主应用骨架 */
.app-shell {
  height: 100dvh;
  display: flex;
  overflow: hidden;
  position: relative;
}

.sidebar {
  width: 264px;
  flex-shrink: 0;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  height: 100%;
  z-index: 40;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 16px 14px 10px;
}

.brand-mark {
  color: var(--primary);
  flex-shrink: 0;
}

.brand-name {
  font-weight: 600;
  font-size: 15.5px;
  letter-spacing: -0.01em;
  flex: 1;
}

.sidebar-top {
  padding: 4px 12px 10px;
}

.new-chat-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  border: 1px solid var(--border);
  border-radius: 11px;
  background: var(--surface);
  color: var(--text);
  padding: 9px 12px;
  font-weight: 500;
  font-size: 14.5px;
  cursor: pointer;
  transition: background 0.15s;
}

.new-chat-btn:hover {
  background: var(--surface-soft);
}

.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.chat-group-label {
  padding: 10px 10px 4px;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-muted);
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 10px;
  border-radius: 10px;
  cursor: pointer;
  transition: background 0.12s;
}

.chat-item:hover {
  background: var(--surface-hover);
}

.chat-item.active {
  background: var(--surface-hover);
  color: var(--text);
  font-weight: 600;
}

.chat-item-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
}

.chat-delete-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  padding: 4px;
  border-radius: 7px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  opacity: 0.65;
  flex-shrink: 0;
}

.chat-delete-btn:hover {
  opacity: 1;
  color: var(--danger);
}

.sidebar-footer {
  padding: 12px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.quota-info {
  font-size: 12px;
  color: var(--text-muted);
  text-align: center;
}

.sidebar-footer-actions {
  display: flex;
  gap: 8px;
}

.ghost-btn {
  flex: 1;
  border: 1px solid var(--border);
  background: transparent;
  border-radius: 9px;
  padding: 7px 10px;
  font-size: 13px;
  cursor: pointer;
}

.ghost-btn:hover {
  background: var(--surface-hover);
}

.danger-text {
  color: var(--danger);
}

/* 顶部栏 */
.main-area {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  height: 100%;
  position: relative;
}

.topbar {
  height: 56px;
  flex-shrink: 0;
  background: var(--bg);
  padding: 0 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  z-index: 20;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.chat-title {
  font-weight: 500;
  font-size: 15px;
  color: var(--text-muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-shrink: 0;
}

/* 极简档位下拉面板 */
.model-dropdown {
  position: relative;
}

.model-trigger {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: transparent;
  border: 1px solid transparent;
  border-radius: 10px;
  padding: 5px 10px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s;
}

.model-trigger:hover {
  background: var(--surface-soft);
}

.model-trigger:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.model-trigger-label {
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chevron-icon {
  color: var(--text-muted);
}

.model-menu {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  width: 244px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 5px;
  box-shadow: var(--shadow-lg);
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.model-option {
  width: 100%;
  border: none;
  background: transparent;
  border-radius: 10px;
  padding: 9px 11px;
  text-align: left;
  cursor: pointer;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
}

.model-option:hover {
  background: var(--surface-soft);
}

.model-option.selected {
  background: var(--surface-soft);
}

.model-opt-main {
  display: flex;
  flex-direction: column;
  gap: 2px;
  min-width: 0;
}

.model-opt-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.model-opt-desc {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.4;
}

.model-opt-check {
  color: var(--text);
  font-weight: 600;
  font-size: 14px;
  flex-shrink: 0;
  margin-top: 1px;
}

.icon-btn {
  border: none;
  background: transparent;
  border-radius: 10px;
  padding: 7px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
}

.icon-btn:hover {
  background: var(--surface-soft);
  color: var(--text);
}

/* 消息区域 */
.messages-wrap {
  flex: 1;
  min-height: 0;
  position: relative;
  display: flex;
  flex-direction: column;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 20px 28px;
  display: flex;
  flex-direction: column;
  gap: 22px;
}

.empty-state {
  margin: auto;
  max-width: 600px;
  width: 100%;
  text-align: center;
  padding: 20px 10px 32px;
}

.empty-state h2 {
  margin: 0 0 22px;
  font-size: 26px;
  font-weight: 600;
  letter-spacing: -0.02em;
  color: var(--text);
}

.starter-pills {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
}

.starter-pill {
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 999px;
  padding: 8px 16px;
  font-size: 14px;
  line-height: 1.4;
  white-space: nowrap;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.starter-pill:hover {
  background: var(--surface-soft);
  border-color: var(--text-muted);
}

/* 消息展示 */
.msg-row {
  width: 100%;
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
}

.msg-row.user {
  align-items: flex-end;
}

.msg-row.model {
  align-items: flex-start;
}

.model-content-col {
  width: 100%;
  min-width: 0;
}

.msg-bubble {
  word-break: break-word;
}

.msg-row.user .msg-bubble {
  max-width: 82%;
  padding: 10px 15px;
  border-radius: 18px;
  border-bottom-right-radius: 5px;
  background: var(--user-bubble);
  color: var(--user-text);
  white-space: pre-wrap;
}

/* 助手回复去框化：如书页正文自然铺在画布上 */
.msg-row.model .msg-bubble {
  padding: 0;
  background: transparent;
  border: none;
  box-shadow: none;
  font-size: 16px;
  line-height: 1.75;
}

.msg-image {
  display: block;
  max-width: 240px;
  max-height: 240px;
  border-radius: 12px;
  margin-bottom: 8px;
  cursor: zoom-in;
}

.msg-actions {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 8px;
  opacity: 0.48;
  transition: opacity 0.15s;
}

.msg-row.model:hover .msg-actions,
.msg-actions:focus-within {
  opacity: 1;
}

@media (hover: none) {
  .msg-actions {
    opacity: 0.72;
  }
}

.msg-action-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 12.5px;
  padding: 3px 8px;
  border-radius: 7px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 4px;
}

.msg-action-btn:hover {
  background: var(--surface-soft);
  color: var(--text);
}

/* 三圆点脉冲动画 */
.typing-dots {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 8px 2px;
}

.typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--text-muted);
  opacity: 0.35;
  animation: dotPulse 1.2s infinite ease-in-out;
}

.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes dotPulse {
  0%, 80%, 100% { transform: scale(0.8); opacity: 0.3; }
  40% { transform: scale(1.15); opacity: 0.9; }
}

.thought-box {
  margin-bottom: 12px;
  background: var(--surface-soft);
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 13.5px;
}

.thought-box summary {
  cursor: pointer;
  color: var(--text-muted);
  font-weight: 500;
}

.thought-content {
  margin-top: 8px;
  color: var(--text-muted);
  white-space: pre-wrap;
}

/* Markdown */
.md-body p { margin: 0 0 12px; }
.md-body p:last-child { margin-bottom: 0; }
.md-body h1, .md-body h2, .md-body h3, .md-body h4 { margin: 16px 0 8px; line-height: 1.4; font-weight: 600; }
.md-body h1 { font-size: 1.25em; }
.md-body h2 { font-size: 1.15em; }
.md-body h3 { font-size: 1.05em; }
.md-body ul, .md-body ol { margin: 6px 0 12px; padding-left: 22px; }
.md-body blockquote {
  margin: 10px 0;
  padding: 4px 12px;
  border-left: 3px solid var(--border);
  color: var(--text-muted);
}
.md-body code.inline-code {
  background: var(--surface-soft);
  padding: 1px 6px;
  border-radius: 5px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.88em;
}
.md-body .code-wrap {
  margin: 12px 0;
  border-radius: 10px;
  overflow: hidden;
  background: var(--code-bg);
  color: var(--code-text);
}
.md-body .code-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 12px;
  font-size: 12px;
  background: rgba(255, 255, 255, 0.05);
}
.md-body .copy-code-btn {
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: transparent;
  color: var(--code-text);
  border-radius: 6px;
  padding: 2px 8px;
  font-size: 12px;
  cursor: pointer;
}
.md-body pre {
  margin: 0;
  padding: 12px;
  overflow-x: auto;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 13.5px;
  line-height: 1.55;
}
.md-body a { color: var(--text); text-decoration: underline; text-underline-offset: 3px; }
.md-error { color: var(--danger); }

.scroll-bottom-btn {
  position: absolute;
  right: 20px;
  bottom: 14px;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-md);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 15;
}

/* 输入舱 */
.composer-wrap {
  padding: 6px 16px calc(16px + env(safe-area-inset-bottom));
  background: var(--bg);
}

.image-preview-bar {
  max-width: 760px;
  margin: 0 auto 8px;
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 8px 12px;
  box-shadow: var(--shadow-sm);
}

.image-thumb-box {
  position: relative;
  display: inline-block;
}

.image-thumb-box img {
  width: 50px;
  height: 50px;
  object-fit: cover;
  border-radius: 8px;
  display: block;
}

.remove-img-btn {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  border: none;
  background: var(--primary);
  color: var(--primary-fg);
  font-size: 11px;
  cursor: pointer;
}

.image-hint {
  font-size: 13px;
  color: var(--text-muted);
}

.composer-pill {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 7px 9px 7px 11px;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s;
}

.composer-pill:focus-within {
  border-color: var(--text-muted);
}

.pill-icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.pill-icon-btn:hover {
  background: var(--surface-soft);
  color: var(--text);
}

.composer-pill textarea {
  flex: 1;
  border: none;
  background: transparent;
  outline: none;
  resize: none;
  max-height: 150px;
  padding: 6px 4px;
  line-height: 1.55;
}

.circle-send-btn,
.circle-stop-btn {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
}

.circle-send-btn {
  background: var(--primary);
  color: var(--primary-fg);
}

.circle-send-btn:disabled {
  background: var(--surface-soft);
  color: var(--text-muted);
  cursor: not-allowed;
}

.circle-stop-btn {
  background: var(--primary);
  color: var(--primary-fg);
}

.stop-square {
  width: 11px;
  height: 11px;
  border-radius: 2px;
  background: currentColor;
}

/* 弹窗与灯箱 */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(26, 25, 23, 0.42);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 100;
}

.modal-card {
  width: 100%;
  max-width: 336px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 22px 20px 18px;
  box-shadow: var(--shadow-lg);
}

.modal-card h3 {
  margin: 0 0 8px;
  font-size: 16.5px;
  font-weight: 600;
}

.modal-card p {
  margin: 0 0 18px;
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1.55;
}

.modal-actions {
  display: flex;
  gap: 10px;
}

.danger-solid-btn {
  flex: 1;
  border: none;
  background: var(--danger);
  color: #fff;
  border-radius: 9px;
  padding: 7px 10px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.lightbox {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 110;
  cursor: zoom-out;
}

.lightbox img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 10px;
}

.mobile-only {
  display: none;
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.36);
  z-index: 30;
}

@media (max-width: 859px) {
  .mobile-only {
    display: inline-flex;
  }

  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow-lg);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .model-trigger-label {
    max-width: 96px;
    font-size: 13.5px;
  }
}
