/* ============ 主题变量 ============ */
:root {
  --bg: #ffffff;
  --bg-soft: #f7f7f8;
  --sidebar: #f9f9fa;
  --text: #1f2328;
  --text-2: #5f6368;
  --text-3: #8b9096;
  --border: #e6e7ea;
  --hover: rgba(0, 0, 0, 0.05);
  --active: rgba(0, 0, 0, 0.08);
  --accent: #4f46e5;
  --accent-fg: #ffffff;
  --bubble: #f1f2f4;
  --code-bg: rgba(0, 0, 0, 0.06);
  --pre-bg: #1e1f24;
  --pre-head: #2a2b31;
  --pre-fg: #e6e6e6;
  --shadow: 0 10px 32px rgba(0, 0, 0, 0.12);
  --danger: #d93025;
  --warn-bg: #fff7e6;
  --warn-fg: #8a5a00;
  --radius: 12px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'Noto Sans CJK SC', sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, 'Liberation Mono', monospace;
  color-scheme: light;
}

:root[data-theme='dark'] {
  --bg: #1b1c1f;
  --bg-soft: #232428;
  --sidebar: #141517;
  --text: #ececf1;
  --text-2: #b4b6bc;
  --text-3: #8b8e96;
  --border: #2e3036;
  --hover: rgba(255, 255, 255, 0.06);
  --active: rgba(255, 255, 255, 0.1);
  --accent: #818cf8;
  --accent-fg: #11122a;
  --bubble: #2a2b30;
  --code-bg: rgba(255, 255, 255, 0.1);
  --pre-bg: #111214;
  --pre-head: #1d1e22;
  --shadow: 0 10px 32px rgba(0, 0, 0, 0.5);
  --danger: #f28b82;
  --warn-bg: #3a2f17;
  --warn-fg: #f5d38a;
  color-scheme: dark;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) {
    --bg: #1b1c1f;
    --bg-soft: #232428;
    --sidebar: #141517;
    --text: #ececf1;
    --text-2: #b4b6bc;
    --text-3: #8b8e96;
    --border: #2e3036;
    --hover: rgba(255, 255, 255, 0.06);
    --active: rgba(255, 255, 255, 0.1);
    --accent: #818cf8;
    --accent-fg: #11122a;
    --bubble: #2a2b30;
    --code-bg: rgba(255, 255, 255, 0.1);
    --pre-bg: #111214;
    --pre-head: #1d1e22;
    --shadow: 0 10px 32px rgba(0, 0, 0, 0.5);
    --danger: #f28b82;
    --warn-bg: #3a2f17;
    --warn-fg: #f5d38a;
    color-scheme: dark;
  }
}

/* ============ 基础 ============ */
*,
*::before,
*::after {
  box-sizing: border-box;
}
[hidden] {
  display: none !important;
}
html,
body {
  height: 100%;
  margin: 0;
}
body {
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}
button,
input,
textarea {
  font: inherit;
  color: inherit;
}
button {
  cursor: pointer;
}
a {
  color: var(--accent);
}
.sprite {
  position: absolute;
  width: 0;
  height: 0;
  overflow: hidden;
}
.muted {
  color: var(--text-3);
  font-size: 0.9em;
}

/* 主题图标：当前亮色显示月亮，暗色显示太阳 */
.only-dark {
  display: none;
}
:root[data-theme='dark'] .only-dark {
  display: block;
}
:root[data-theme='dark'] .only-light {
  display: none;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme='light']) .only-dark {
    display: block;
  }
  :root:not([data-theme='light']) .only-light {
    display: none;
  }
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 36px;
  height: 36px;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: var(--text-2);
  flex-shrink: 0;
}
.icon-btn:hover {
  background: var(--hover);
  color: var(--text);
}
.icon-btn svg {
  width: 20px;
  height: 20px;
}
.icon-btn.sm {
  width: 28px;
  height: 28px;
  border-radius: 6px;
}
.icon-btn.sm svg {
  width: 16px;
  height: 16px;
}

/* ============ 布局 ============ */
body.chat-page {
  overflow: hidden;
}
.app {
  display: flex;
  height: 100vh;
  height: 100dvh;
}

/* ---- 侧栏 ---- */
.sidebar {
  width: 260px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  min-height: 0;
}
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 10px 6px 16px;
}
.brand {
  font-weight: 650;
  font-size: 16px;
  color: var(--text);
  text-decoration: none;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.new-chat {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 6px 10px 8px;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  font-weight: 500;
}
.new-chat:hover {
  background: var(--hover);
}
.new-chat svg {
  width: 18px;
  height: 18px;
}
.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 8px 12px;
  min-height: 0;
}
.conv-group {
  padding: 14px 10px 4px;
  font-size: 12px;
  color: var(--text-3);
  font-weight: 600;
}
.conv-empty {
  padding: 24px 10px;
  color: var(--text-3);
  font-size: 13px;
  text-align: center;
}
.conv-item {
  position: relative;
  display: flex;
  align-items: center;
  border-radius: 8px;
}
.conv-item:hover {
  background: var(--hover);
}
.conv-item.active {
  background: var(--active);
}
.conv-link {
  flex: 1;
  min-width: 0;
  padding: 7px 10px;
  color: var(--text);
  text-decoration: none;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conv-actions {
  display: none;
  gap: 2px;
  padding-right: 4px;
}
.conv-item:hover .conv-actions,
.conv-item.active .conv-actions {
  display: flex;
}
.conv-rename {
  flex: 1;
  margin: 3px;
  padding: 4px 8px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--bg);
  outline: none;
  font-size: 14px;
}
.sidebar-foot {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 10px;
  border-top: 1px solid var(--border);
}
.user-chip {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 10px;
}
.avatar {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 600;
  font-size: 14px;
  flex-shrink: 0;
}
.user-name {
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.scrim {
  display: none;
}

/* ---- 主区域 ---- */
.main {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  position: relative;
}
.topbar {
  display: flex;
  align-items: center;
  gap: 6px;
  height: 56px;
  padding: 0 10px;
  flex-shrink: 0;
}
.spacer {
  flex: 1;
}
#sidebar-open {
  display: none;
}
body.sidebar-collapsed .sidebar {
  display: none;
}
body.sidebar-collapsed #sidebar-open {
  display: inline-grid;
}

/* ---- 模型选择 ---- */
.model-picker {
  position: relative;
}
.model-btn {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 7px 10px 7px 12px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  font-size: 16px;
  font-weight: 600;
}
.model-btn:hover {
  background: var(--hover);
}
.model-btn svg {
  width: 16px;
  height: 16px;
  color: var(--text-3);
}
.effort-btn {
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 5px 8px 5px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: transparent;
  color: var(--text-2);
  font-size: 13px;
}
.effort-btn:hover {
  background: var(--hover);
  color: var(--text);
}
.effort-btn.active {
  border-color: color-mix(in srgb, var(--accent) 45%, var(--border));
  color: var(--accent);
}
.effort-label {
  color: var(--text-3);
}
.effort-btn svg {
  width: 14px;
  height: 14px;
  color: var(--text-3);
}
.effort-menu {
  width: 240px;
}
.model-menu {
  position: absolute;
  top: calc(100% + 6px);
  left: 0;
  z-index: 30;
  width: 320px;
  max-height: min(70vh, 560px);
  overflow-y: auto;
  padding: 6px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
}
.model-group {
  padding: 8px 10px 4px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-3);
}
.model-opt {
  display: flex;
  align-items: center;
  width: 100%;
  gap: 10px;
  padding: 8px 10px;
  border: 0;
  border-radius: 8px;
  background: transparent;
  text-align: left;
}
.model-opt:hover {
  background: var(--hover);
}
.model-opt-text {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
}
.model-opt-name {
  font-weight: 550;
}
.model-opt-desc {
  font-size: 12px;
  color: var(--text-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.model-opt-check {
  width: 18px;
  height: 18px;
  color: var(--accent);
  visibility: hidden;
}
.model-opt-check svg {
  width: 18px;
  height: 18px;
}
.model-opt[aria-selected='true'] .model-opt-check {
  visibility: visible;
}
.model-empty {
  padding: 16px;
  color: var(--text-3);
  text-align: center;
}

/* ---- 消息区 ---- */
.thread {
  flex: 1;
  overflow-y: auto;
  min-height: 0;
}
.welcome {
  max-width: 780px;
  margin: 18vh auto 0;
  padding: 0 24px;
  text-align: center;
}
.welcome h1 {
  margin: 0 0 8px;
  font-size: 28px;
  font-weight: 600;
}
.welcome p {
  margin: 0;
  color: var(--text-2);
}
.messages {
  max-width: 800px;
  margin: 0 auto;
  padding: 8px 24px 40px;
}
.msg {
  margin: 0 0 28px;
}
.msg-user {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 6px;
}
.msg-images {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.msg-images:empty {
  display: none;
}
.msg-assistant .msg-images {
  margin: 10px 0 4px;
}
.img-item {
  position: relative;
}
.img-item img {
  display: block;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg-soft);
  object-fit: contain;
}
.msg-user .img-item img {
  max-width: 220px;
  max-height: 220px;
}
.msg-assistant .img-item img {
  max-width: min(100%, 512px);
  max-height: 512px;
}
.img-dl {
  position: absolute;
  right: 8px;
  bottom: 8px;
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
  font-size: 12px;
  text-decoration: none;
  opacity: 0;
  transition: opacity 0.15s;
}
.img-item:hover .img-dl {
  opacity: 1;
}
@media (hover: none) {
  .img-dl {
    opacity: 1;
  }
}
.cap-tag {
  margin-left: 6px;
  padding: 0 6px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 500;
  vertical-align: 1px;
  background: color-mix(in srgb, var(--accent) 14%, transparent);
  color: var(--accent);
}

/* ---- 额度 ---- */
.quota {
  margin: 0 10px 6px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
}
.quota-text {
  font-size: 12px;
  color: var(--text-2);
  margin-bottom: 6px;
}
.quota-track {
  height: 4px;
  border-radius: 999px;
  background: var(--active);
  overflow: hidden;
}
.quota-bar {
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: var(--accent);
  transition: width 0.3s;
}
.quota.warn .quota-bar {
  background: var(--danger);
}
.msg-user .bubble {
  max-width: 80%;
  padding: 10px 16px;
  border-radius: 18px;
  background: var(--bubble);
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}
.msg-head {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}
.model-badge {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-2);
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
}
.status-tag {
  font-size: 12px;
  color: var(--text-3);
}
.reasoning {
  margin: 4px 0 10px;
  border-left: 2px solid var(--border);
  padding-left: 12px;
}
.reasoning summary {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  cursor: pointer;
  color: var(--text-3);
  font-size: 13px;
  user-select: none;
  list-style: none;
}
.reasoning summary::-webkit-details-marker {
  display: none;
}
.reasoning summary::after {
  content: '';
  width: 6px;
  height: 6px;
  margin-left: 2px;
  border-right: 1.5px solid currentColor;
  border-bottom: 1.5px solid currentColor;
  transform: rotate(-45deg);
  transition: transform 0.15s;
}
.reasoning[open] summary::after {
  transform: rotate(45deg) translate(-2px, -1px);
}
.reasoning summary:hover {
  color: var(--text-2);
}
.reasoning-body {
  color: var(--text-2);
  font-size: 14px;
  margin-top: 6px;
}
.notices .notice {
  margin-top: 10px;
  padding: 8px 12px;
  border-radius: 8px;
  background: var(--warn-bg);
  color: var(--warn-fg);
  font-size: 13px;
}
.notices .notice.error {
  color: var(--danger);
}
.msg-actions {
  display: flex;
  gap: 2px;
  margin-top: 6px;
  opacity: 0.85;
}

/* 输入中动画 */
.typing {
  display: inline-flex;
  gap: 4px;
  padding: 8px 0;
}
.typing i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-3);
  animation: blink 1.2s infinite ease-in-out;
}
.typing i:nth-child(2) {
  animation-delay: 0.15s;
}
.typing i:nth-child(3) {
  animation-delay: 0.3s;
}
@keyframes blink {
  0%,
  80%,
  100% {
    opacity: 0.25;
  }
  40% {
    opacity: 1;
  }
}
.answer.streaming > :last-child::after {
  content: '';
  display: inline-block;
  width: 8px;
  height: 1em;
  margin-left: 2px;
  vertical-align: -2px;
  background: var(--text);
  animation: blink 1s infinite;
}

/* ============ Markdown ============ */
.md {
  overflow-wrap: anywhere;
}
.md > :first-child {
  margin-top: 0;
}
.md > :last-child {
  margin-bottom: 0;
}
.md p,
.md ul,
.md ol,
.md blockquote,
.md table {
  margin: 0 0 0.9em;
}
.md ul,
.md ol {
  padding-left: 1.5em;
}
.md li + li {
  margin-top: 0.25em;
}
.md h1,
.md h2,
.md h3,
.md h4 {
  margin: 1.3em 0 0.6em;
  line-height: 1.35;
}
.md h1 {
  font-size: 1.5em;
}
.md h2 {
  font-size: 1.3em;
}
.md h3 {
  font-size: 1.12em;
}
.md blockquote {
  padding: 0 1em;
  color: var(--text-2);
  border-left: 3px solid var(--border);
}
.md hr {
  border: 0;
  border-top: 1px solid var(--border);
  margin: 1.5em 0;
}
.md img {
  max-width: 100%;
  border-radius: 8px;
}
.md table {
  border-collapse: collapse;
  display: block;
  overflow-x: auto;
  max-width: 100%;
}
.md th,
.md td {
  border: 1px solid var(--border);
  padding: 6px 12px;
}
.md th {
  background: var(--bg-soft);
}
.md code {
  font-family: var(--mono);
  font-size: 0.88em;
  padding: 0.15em 0.4em;
  border-radius: 5px;
  background: var(--code-bg);
}
.code-block {
  margin: 0 0 1em;
  border-radius: 10px;
  overflow: hidden;
  background: var(--pre-bg);
}
.code-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 12px;
  background: var(--pre-head);
  color: #a9abb3;
  font-size: 12px;
  font-family: var(--mono);
}
.code-copy {
  border: 0;
  background: transparent;
  color: #a9abb3;
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 4px;
}
.code-copy:hover {
  color: #fff;
  background: rgba(255, 255, 255, 0.1);
}
.md pre {
  margin: 0;
  padding: 14px 16px;
  overflow-x: auto;
  color: var(--pre-fg);
  background: var(--pre-bg);
}
.md pre code {
  padding: 0;
  background: none;
  font-size: 13px;
  line-height: 1.55;
  color: inherit;
}

/* ============ 输入框 ============ */
.composer-wrap {
  padding: 0 24px 10px;
  flex-shrink: 0;
}
.composer {
  max-width: 800px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 8px 10px 8px 10px;
  border: 1px solid var(--border);
  border-radius: 26px;
  background: var(--bg-soft);
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.04);
}
.composer.dragging {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 8%, var(--bg-soft));
}
.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 6px;
}
.attach-btn {
  margin-bottom: 0;
}
.attach-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 4px 4px 0;
}
.attach-thumb {
  position: relative;
  width: 64px;
  height: 64px;
  border-radius: 12px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--bg);
}
.attach-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}
.attach-thumb.uploading img {
  opacity: 0.35;
}
.attach-thumb.uploading::after {
  content: '';
  position: absolute;
  inset: 0;
  margin: auto;
  width: 20px;
  height: 20px;
  border: 2px solid var(--text-3);
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
.attach-remove {
  position: absolute;
  top: 3px;
  right: 3px;
  width: 20px;
  height: 20px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.6);
  color: #fff;
}
.attach-remove svg {
  width: 12px;
  height: 12px;
}
.composer:focus-within {
  border-color: var(--text-3);
}
.composer textarea {
  flex: 1;
  min-height: 24px;
  max-height: 220px;
  padding: 6px 0 6px 8px;
  border: 0;
  outline: none;
  resize: none;
  background: transparent;
  line-height: 1.5;
}
.round-btn {
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: var(--text);
  color: var(--bg);
  flex-shrink: 0;
}
.round-btn svg {
  width: 18px;
  height: 18px;
}
.round-btn:disabled {
  opacity: 0.25;
  cursor: default;
}
.disclaimer {
  margin: 8px 0 0;
  text-align: center;
  font-size: 12px;
  color: var(--text-3);
}

/* ============ Toast ============ */
.toast {
  position: fixed;
  left: 50%;
  bottom: 96px;
  transform: translateX(-50%);
  z-index: 100;
  padding: 10px 18px;
  border-radius: 10px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  box-shadow: var(--shadow);
  max-width: calc(100vw - 32px);
}

/* ============ 登录 / 注册 ============ */
.auth-page {
  display: grid;
  place-items: center;
  min-height: 100vh;
  padding: 24px;
  background: var(--bg-soft);
}
.auth-card {
  width: 100%;
  max-width: 380px;
  padding: 32px 28px;
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--bg);
  box-shadow: var(--shadow);
}
.auth-card.center {
  text-align: center;
}
.auth-card .brand {
  display: block;
  margin-bottom: 16px;
  color: var(--accent);
}
.auth-card h1 {
  margin: 0 0 20px;
  font-size: 22px;
}
.auth-card form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.auth-card label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 14px;
  font-weight: 500;
}
.auth-card input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  outline: none;
  font-weight: 400;
}
.auth-card input:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 20%, transparent);
}
.btn-primary {
  display: inline-block;
  padding: 11px 16px;
  border: 0;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 600;
  text-align: center;
  text-decoration: none;
}
.btn-primary:disabled {
  opacity: 0.6;
  cursor: default;
}
.form-error {
  margin: 0;
  color: var(--danger);
  font-size: 13px;
}
.auth-switch {
  margin: 18px 0 0;
  text-align: center;
  font-size: 14px;
  color: var(--text-2);
}
.notice {
  color: var(--text-2);
}

/* ============ 移动端 ============ */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 50;
    width: 280px;
    max-width: 85vw;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
  }
  body.sidebar-collapsed .sidebar {
    display: flex;
  }
  body.sidebar-open .sidebar {
    transform: none;
  }
  body.sidebar-open .scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 40;
    background: rgba(0, 0, 0, 0.4);
  }
  #sidebar-open {
    display: inline-grid;
  }
  .conv-actions {
    display: flex;
  }
  .messages {
    padding: 8px 16px 32px;
  }
  .composer-wrap {
    padding: 0 12px calc(8px + env(safe-area-inset-bottom));
  }
  .msg-user .bubble {
    max-width: 88%;
  }
  .model-menu {
    width: min(320px, calc(100vw - 70px));
  }
  .welcome {
    margin-top: 14vh;
  }
  .kbd-hint {
    display: none;
  }
}
