:root {
  --bg: #f8f9fa;
  --bg-sidebar: #f0f2f6;
  --card: #ffffff;
  --text: #1f2937;
  --text-2: #6b7280;
  --accent: #3b82f6;
  --accent-dark: #2563eb;
  --ok: #10b981;
  --err: #ef4444;
  --warn: #f59e0b;
  --border: #e5e7eb;
  --radius: 10px;
  --radius-sm: 8px;
  --shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: var(--font);
  color: var(--text);
  background: var(--bg);
}

button { font-family: inherit; cursor: pointer; }
input, textarea, select { font-family: inherit; font-size: 15px; color: var(--text); }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.visually-hidden {
  position: absolute;
  width: 1px; height: 1px;
  margin: -1px; padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

.brand-icon { width: 44px; height: 44px; }
.brand-icon.small { width: 22px; height: 22px; }

.btn {
  border: none;
  border-radius: var(--radius-sm);
  font-weight: 500;
  font-size: 15px;
  padding: 10px 16px;
  transition: all 0.3s ease;
}
.btn:hover:not(:disabled) { transform: translateY(-2px); box-shadow: var(--shadow); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-dark); }
.btn-ghost { background: var(--bg-sidebar); color: var(--text); }
.btn-block { width: 100%; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px; height: 34px;
  border: none;
  border-radius: var(--radius-sm);
  background: transparent;
  color: var(--text-2);
  transition: background 0.2s ease, color 0.2s ease;
}
.icon-btn:hover { background: rgba(0, 0, 0, 0.06); color: var(--text); }

.link-btn {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 13px;
  padding: 4px 0;
}
.link-btn:hover { text-decoration: underline; }

.banner {
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 14px;
  margin-bottom: 14px;
}
.banner-error { background: #fee2e2; border-left: 4px solid var(--err); color: #991b1b; }

#toast-container {
  position: fixed;
  top: 16px;
  left: 50%;
  transform: translateX(-50%);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
  align-items: center;
}
.toast {
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  box-shadow: var(--shadow);
  background: var(--card);
  border-left: 4px solid var(--accent);
  animation: toast-in 0.25s ease;
  max-width: min(92vw, 480px);
}
.toast.error { background: #fee2e2; border-left-color: var(--err); color: #991b1b; }
.toast.success { background: #d1fae5; border-left-color: var(--ok); color: #065f46; }
.toast.info { background: #dbeafe; border-left-color: var(--accent); color: #1e40af; }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(-8px); }
  to { opacity: 1; transform: translateY(0); }
}

.auth-view {
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(600px 300px at 15% 10%, rgba(59, 130, 246, 0.08), transparent),
    radial-gradient(500px 280px at 85% 90%, rgba(16, 185, 129, 0.07), transparent),
    var(--bg);
}
.auth-card {
  width: 100%;
  max-width: 400px;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 32px 28px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.06);
}
.auth-brand { text-align: center; margin-bottom: 22px; }
.auth-brand h1 { margin: 10px 0 4px; font-size: 24px; }
.auth-tagline { margin: 0; color: var(--text-2); font-size: 13px; }

.auth-tabs {
  display: flex;
  background: var(--bg-sidebar);
  border-radius: var(--radius-sm);
  padding: 4px;
  margin-bottom: 18px;
}
.auth-tab {
  flex: 1;
  border: none;
  background: transparent;
  border-radius: 6px;
  padding: 8px 0;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-2);
  transition: all 0.2s ease;
}
.auth-tab.active { background: var(--card); color: var(--text); box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08); }

.field { display: block; margin-bottom: 14px; }
.field-label { display: block; font-size: 13px; color: var(--text-2); margin-bottom: 6px; }
.field input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  background: var(--bg);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
.field input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.15);
  background: var(--card);
}

.turnstile-box { margin: 6px 0 14px; min-height: 0; display: flex; justify-content: center; }
.turnstile-box:empty { margin: 0; }

.auth-advanced { margin-top: 18px; text-align: center; }
#adv-panel { margin-top: 10px; text-align: left; }

.chat-view {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

.sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px 10px calc(10px + env(safe-area-inset-bottom));
}
.sidebar-brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 600;
  font-size: 16px;
  padding: 4px 8px 14px;
}
.new-chat-btn { margin: 0 4px 12px; }

.conv-list { flex: 1; overflow-y: auto; padding: 0 2px; }
.conv-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  font-size: 14px;
  color: var(--text);
  cursor: pointer;
  margin-bottom: 2px;
  transition: background 0.15s ease;
}
.conv-item:hover { background: rgba(0, 0, 0, 0.05); }
.conv-item.active { background: var(--card); box-shadow: inset 3px 0 0 var(--accent), 0 1px 3px rgba(0, 0, 0, 0.06); }
.conv-title {
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conv-del {
  border: none;
  background: none;
  color: var(--text-2);
  font-size: 15px;
  padding: 2px 4px;
  border-radius: 4px;
  opacity: 0;
  transition: opacity 0.15s ease;
}
.conv-item:hover .conv-del, .conv-item.active .conv-del { opacity: 1; }
.conv-del:hover { color: var(--err); background: #fee2e2; }
.conv-confirm { display: flex; gap: 4px; font-size: 12px; }
.conv-confirm button {
  border: none;
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 12px;
}
.conv-confirm .yes { background: var(--err); color: #fff; }
.conv-confirm .no { background: var(--border); color: var(--text); }

.user-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 8px 4px;
  border-top: 1px solid var(--border);
  margin-top: 8px;
}
.user-avatar {
  width: 34px; height: 34px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  font-weight: 600;
  font-size: 15px;
  flex-shrink: 0;
}
.user-meta { flex: 1; min-width: 0; }
.user-name {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.user-balance { font-size: 12.5px; color: var(--text-2); }
.quota-hint {
  font-size: 12px;
  color: #92400e;
  background: #fef3c7;
  border-left: 3px solid var(--warn);
  border-radius: 6px;
  padding: 7px 9px;
  margin: 8px 4px 0;
}

.main-col {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  height: 56px;
  flex-shrink: 0;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 0 16px;
}
.drawer-toggle { display: none; }
.topbar-title {
  font-size: 14.5px;
  font-weight: 500;
  color: var(--text-2);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.msg-list {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px;
  -webkit-overflow-scrolling: touch;
}
.msg-inner-width, .msg-row { max-width: 800px; margin-left: auto; margin-right: auto; }

.empty-state {
  max-width: 800px;
  margin: 12vh auto 0;
  text-align: center;
  color: var(--text-2);
}
.empty-state h2 { color: var(--text); margin: 14px 0 6px; font-size: 22px; }
.empty-state p { margin: 0; font-size: 14px; }

.msg-row { display: flex; margin-bottom: 16px; animation: none; }
.msg-row.appear { animation: msg-in 0.2s ease; }
@keyframes msg-in {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}
.msg-row.user { justify-content: flex-end; }
.msg-row.assistant { justify-content: flex-start; }

.bubble {
  border-radius: var(--radius);
  padding: 12px 14px;
  font-size: 15px;
  line-height: 1.65;
  max-width: 86%;
  overflow-wrap: break-word;
}
.msg-row.user .bubble {
  background: var(--accent);
  color: #fff;
  white-space: pre-wrap;
}
.bubble-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 8px;
  white-space: normal;
}
.bubble-attachments img {
  max-width: 220px;
  max-height: 220px;
  border-radius: 10px;
  display: block;
}
.bubble-file-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: rgba(255, 255, 255, 0.18);
  border-radius: 8px;
  padding: 6px 10px;
  font-size: 12.5px;
}
.msg-row.assistant .bubble {
  background: var(--card);
  border: 1px solid var(--border);
  position: relative;
  min-width: 60px;
}

.msg-actions { margin-top: 6px; display: flex; gap: 6px; }
.msg-copy {
  border: none;
  background: none;
  color: var(--text-2);
  font-size: 12px;
  padding: 2px 6px;
  border-radius: 4px;
}
.msg-copy:hover { background: var(--bg-sidebar); color: var(--text); }

.reasoning {
  background: var(--bg-sidebar);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  margin-bottom: 10px;
  font-size: 13px;
  color: var(--text-2);
}
.reasoning summary { cursor: pointer; font-weight: 500; user-select: none; }
.reasoning[open] summary { margin-bottom: 6px; }
.reasoning-body { white-space: pre-wrap; }

.md-body > :first-child { margin-top: 0; }
.md-body > :last-child { margin-bottom: 0; }
.md-body p { margin: 0.6em 0; }
.md-body h1, .md-body h2, .md-body h3 { color: var(--text); margin: 0.9em 0 0.45em; }
.md-body ul, .md-body ol { padding-left: 1.4em; margin: 0.5em 0; }
.md-body table { border-collapse: collapse; margin: 0.7em 0; display: block; overflow-x: auto; }
.md-body th, .md-body td { border: 1px solid var(--border); padding: 6px 10px; font-size: 14px; }
.md-body th { background: var(--bg); }
.md-body a { color: var(--accent); }
.md-body blockquote {
  margin: 0.6em 0;
  padding: 4px 12px;
  border-left: 4px solid var(--border);
  color: var(--text-2);
}
.md-body code:not(pre code) {
  background: #e5e7eb;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.9em;
}
.md-body pre {
  position: relative;
  background: #f3f4f6;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  overflow-x: auto;
  margin: 0.7em 0;
}
.md-body pre code { background: transparent; padding: 0; font-size: 13.5px; }
.md-body .katex-display { overflow-x: auto; overflow-y: hidden; padding: 4px 0; }
.md-body .katex { font-size: 1.06em; }
.code-copy {
  position: absolute;
  top: 6px; right: 6px;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--text-2);
  font-size: 12px;
  border-radius: 5px;
  padding: 2px 8px;
  opacity: 0;
  transition: opacity 0.15s ease;
}
.md-body pre:hover .code-copy { opacity: 1; }
.code-copy:hover { color: var(--text); }

.caret {
  display: inline-block;
  width: 2px;
  height: 1em;
  background: var(--accent);
  vertical-align: -0.15em;
  margin-left: 2px;
  animation: blink 0.9s steps(2) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.composer-wrap {
  flex-shrink: 0;
  padding: 10px 16px calc(12px + env(safe-area-inset-bottom));
  background: linear-gradient(transparent, var(--bg) 40%);
}
.composer {
  max-width: 800px;
  margin: 0 auto;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.05);
  display: flex;
  flex-direction: column;
  padding: 10px 12px;
  gap: 6px;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}
.composer.dragover {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.18);
}
.composer textarea {
  width: 100%;
  border: none;
  resize: none;
  outline: none;
  background: transparent;
  line-height: 1.5;
  max-height: 190px;
  padding: 4px 2px;
}
.composer-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.composer-spacer { flex: 1; }

.circle-add {
  width: 36px; height: 36px;
  border: 1.5px solid var(--border);
  border-radius: 50%;
  background: transparent;
  color: var(--text-2);
  display: grid;
  place-items: center;
  transition: all 0.2s ease;
}
.circle-add:hover { border-color: var(--accent); color: var(--accent); background: rgba(59, 130, 246, 0.06); }

.attach-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 2px 2px 6px;
}
.attach-chip {
  position: relative;
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  background: var(--bg);
}
.attach-chip.image { width: 72px; height: 72px; }
.attach-chip.image img { width: 100%; height: 100%; object-fit: cover; display: block; }
.attach-chip.file {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  max-width: 220px;
}
.attach-file-icon {
  width: 30px; height: 30px;
  border-radius: 8px;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 10px;
  font-weight: 700;
  flex-shrink: 0;
  text-transform: uppercase;
}
.attach-file-meta { min-width: 0; }
.attach-file-name {
  font-size: 12.5px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.attach-file-size { font-size: 11px; color: var(--text-2); }
.attach-remove {
  position: absolute;
  top: 3px; right: 3px;
  width: 18px; height: 18px;
  border: none;
  border-radius: 50%;
  background: rgba(0, 0, 0, 0.55);
  color: #fff;
  font-size: 11px;
  line-height: 1;
  display: grid;
  place-items: center;
  padding: 0;
}
.attach-chip.file .attach-remove { position: static; background: transparent; color: var(--text-2); width: 20px; height: 20px; flex-shrink: 0; }
.attach-chip.file .attach-remove:hover { color: var(--err); }
.attach-chip .spinner {
  position: absolute;
  inset: 0;
  background: rgba(255, 255, 255, 0.7);
  display: grid;
  place-items: center;
  font-size: 11px;
  color: var(--text-2);
}

.model-pill-wrap { position: relative; }
.model-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  padding: 7px 12px;
  max-width: 240px;
  transition: all 0.2s ease;
}
.model-pill:hover { border-color: var(--accent); background: rgba(59, 130, 246, 0.06); }
.model-pill #model-pill-name {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.model-dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
  flex-shrink: 0;
}
.model-menu {
  position: absolute;
  bottom: calc(100% + 8px);
  right: 0;
  width: 300px;
  max-height: 320px;
  overflow-y: auto;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.12);
  padding: 6px;
  z-index: 30;
}
.model-menu-item {
  width: 100%;
  text-align: left;
  border: none;
  background: transparent;
  border-radius: 8px;
  padding: 9px 10px;
  font-size: 13px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--text);
}
.model-menu-item:hover { background: var(--bg-sidebar); }
.model-menu-item.active { color: var(--accent); font-weight: 600; }
.model-menu-item .check { margin-left: auto; flex-shrink: 0; }
.model-menu-custom {
  display: flex;
  gap: 6px;
  padding: 6px;
  border-top: 1px solid var(--border);
  margin-top: 4px;
}
.model-menu-custom input {
  flex: 1;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 6px 8px;
  font-size: 12.5px;
}
.send-btn {
  width: 38px; height: 38px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  transition: all 0.25s ease;
}
.send-btn:hover:not(:disabled) { background: var(--accent-dark); transform: translateY(-2px); box-shadow: var(--shadow); }
.send-btn:disabled { opacity: 0.45; cursor: not-allowed; }
.send-btn.stop { background: var(--err); }
.composer-hint {
  max-width: 800px;
  margin: 6px auto 0;
  text-align: center;
  font-size: 12px;
  color: var(--text-2);
}

.drawer-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.35);
  z-index: 40;
}

@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    box-shadow: 4px 0 18px rgba(0, 0, 0, 0.12);
  }
  .sidebar.open { transform: translateX(0); }
  .drawer-toggle { display: inline-flex; }
  .bubble { max-width: 92%; }
  .composer-hint { display: none; }
  .msg-list { padding: 16px 10px; }
}
