:root {
  --bg: #ffffff;
  --bg-muted: #e8eaee;
  --bg-elev: #ffffff;
  --fg: #1a1a1a;
  --fg-muted: #555;
  --border: #e0e0e0;
  --border-input: #b8bac0;
  --input-bg: #f3f4f6;
  --accent: #2563eb;
  --accent-fg: #ffffff;
  --error: #dc2626;
  --bubble-user: #dbeafe;
  --bubble-assistant: #f3f4f6;
  --shadow: 0 1px 3px rgba(0,0,0,.08);
  --radius: 8px;
  --sidebar-w: 260px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0a0a0c;
    --bg-muted: #17171a;
    --bg-elev: #26262b;
    --fg: #ececec;
    --fg-muted: #a8a8a8;
    --border: #2c2c30;
    --border-input: #4a4a50;
    --input-bg: #131316;
    --accent: #3b82f6;
    --accent-fg: #ffffff;
    --bubble-user: #1e3a8a;
    --bubble-assistant: #27272a;
  }
}
:root[data-theme="dark"] {
  --bg: #0a0a0c;
  --bg-muted: #17171a;
  --bg-elev: #26262b;
  --fg: #ececec;
  --fg-muted: #a8a8a8;
  --border: #2c2c30;
  --border-input: #4a4a50;
  --input-bg: #131316;
  --accent: #3b82f6;
  --accent-fg: #ffffff;
  --bubble-user: #1e3a8a;
  --bubble-assistant: #27272a;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
               "Microsoft YaHei", "Noto Sans CJK SC", Roboto, sans-serif;
  background: var(--bg);
  color: var(--fg);
  font-size: 15px;
  line-height: 1.5;
}

button {
  font: inherit;
  cursor: pointer;
  border: none;
  background: var(--bg-muted);
  color: var(--fg);
  padding: 6px 12px;
  border-radius: var(--radius);
}
button:hover { background: var(--border); }
button.primary, #send-btn, #submit-btn {
  background: var(--accent);
  color: var(--accent-fg);
}
button.primary:hover, #send-btn:hover, #submit-btn:hover { filter: brightness(1.1); }

input, select, textarea {
  font: inherit;
  border: 1px solid var(--border-input);
  background: var(--input-bg);
  color: var(--fg);
  padding: 8px 10px;
  border-radius: var(--radius);
  outline: none;
  width: 100%;
}
input:focus, textarea:focus, select:focus { border-color: var(--accent); }

/* --- Auth page --- */
.auth-page {
  display: grid;
  place-items: center;
  min-height: 100vh;
  background: var(--bg-muted);
  padding: 24px;
}
.auth-card {
  width: 100%;
  max-width: 360px;
  background: var(--bg-elev);
  padding: 28px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.auth-card h1 {
  margin: 0 0 16px;
  font-size: 22px;
  text-align: center;
}
.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 16px;
  background: var(--bg-muted);
  padding: 4px;
  border-radius: var(--radius);
}
.tab {
  flex: 1;
  background: transparent;
  padding: 8px;
}
.tab.active {
  background: var(--bg-elev);
  font-weight: 600;
  box-shadow: var(--shadow);
}
.field { display: block; margin-bottom: 12px; }
.field[hidden] { display: none; }
.field span {
  display: block;
  font-size: 13px;
  color: var(--fg-muted);
  margin-bottom: 4px;
}
.error { color: var(--error); font-size: 13px; margin: 8px 0; }
.success { color: #15803d; font-size: 13px; margin: 8px 0; }
#submit-btn { width: 100%; padding: 10px; margin-top: 4px; }
#submit-btn:disabled { opacity: 0.75; cursor: progress; }

.spinner {
  display: inline-block;
  width: 12px;
  height: 12px;
  border: 2px solid currentColor;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
  vertical-align: -1px;
  margin-right: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.theme-switch {
  display: inline-flex;
  gap: 2px;
  padding: 3px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12px;
}
.theme-switch button {
  background: transparent;
  padding: 4px 12px;
  color: var(--fg-muted);
  border-radius: 999px;
  line-height: 1.4;
}
.theme-switch button:hover { color: var(--fg); background: transparent; }
.theme-switch button.active {
  background: var(--bg-muted);
  color: var(--fg);
}

.theme-switch-floating {
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 10;
}

/* --- Chat page --- */
.chat-page {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  width: 100%;
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

#sidebar {
  background: var(--bg-muted);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
#sidebar header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}
#sidebar h1 { margin: 0; font-size: 16px; }
#new-chat-btn {
  font-size: 13px;
  padding: 6px 12px;
  background: var(--accent);
  color: var(--accent-fg);
  font-weight: 500;
}
#new-chat-btn:hover { filter: brightness(1.1); background: var(--accent); }

#conversation-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}
.conv-row {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-bottom: 4px;
  border-radius: var(--radius);
}
.conv-row:hover { background: var(--border); }
.conv-row.active { background: var(--bg-elev); }
.conv-row .conv-item {
  flex: 1;
  background: transparent;
  text-align: left;
  padding: 8px 10px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--fg);
  border-radius: var(--radius);
}
.conv-row .conv-item:hover { background: transparent; }
.conv-row.active .conv-item { font-weight: 600; }
.conv-row .conv-delete {
  visibility: hidden;
  background: transparent;
  color: var(--fg-muted);
  padding: 4px 8px;
  font-size: 14px;
  line-height: 1;
}
.conv-row:hover .conv-delete,
.conv-row.active .conv-delete { visibility: visible; }
.conv-row .conv-delete:hover { color: var(--error); background: transparent; }

#sidebar footer {
  border-top: 1px solid var(--border);
  padding: 10px 14px;
  font-size: 13px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.quota { color: var(--fg-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.quota.warn { color: #d97706; }
.quota.over { color: var(--error); font-weight: 600; }
.theme-switch-sidebar { align-self: stretch; display: flex; justify-content: center; }
.theme-switch-sidebar .theme-switch { width: 100%; justify-content: space-between; }
.theme-switch-sidebar .theme-switch button { flex: 1; text-align: center; padding: 4px 8px; }

.user-row { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.me-name {
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--fg-muted);
}
#change-pw-btn, #logout-btn { font-size: 12px; padding: 4px 10px; }

#chat {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 0;
}

#chat-header {
  display: grid;
  grid-template-columns: minmax(0, auto) minmax(0, 1fr) minmax(0, auto);
  gap: 16px;
  align-items: center;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.header-left { display: flex; gap: 10px; align-items: center; min-width: 0; }
.header-right { display: flex; gap: 8px; align-items: center; min-width: 0; justify-content: flex-end; }
#chat-header .title {
  text-align: center;
  color: var(--fg-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-weight: 500;
}
#sidebar-toggle { display: none; font-size: 18px; }
.header-field {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  color: var(--fg-muted);
}
.header-field[hidden] { display: none; }
.header-field > span { white-space: nowrap; }
#model-select, #reasoning-select { width: auto; padding: 4px 8px; }
.icon-btn {
  background: transparent;
  color: var(--fg-muted);
  font-size: 16px;
  padding: 2px 6px;
  line-height: 1;
}
.icon-btn:hover { color: var(--fg); background: var(--bg-muted); }

/* Provider badges + latency dots */
.provider-badge {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 18px;
  height: 18px;
  border-radius: 4px;
  color: #ffffff;
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
  flex-shrink: 0;
}
.provider-badge.anthropic { background: #cc785c; }
.provider-badge.google { background: #4285f4; }
.provider-badge.openai { background: #10a37f; }
.provider-badge.zhipu { background: #d4451d; }

.latency-dots {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}
.latency-dots i {
  display: inline-block;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--border);
}
.latency-dots i.on { background: #16a34a; }

#messages {
  overflow-y: auto;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
#messages > :first-child { margin-top: auto; }
.bubble {
  max-width: 75%;
  padding: 10px 14px;
  border-radius: var(--radius);
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.bubble.user, .bubble.error { white-space: pre-wrap; }
.bubble.user { background: var(--bubble-user); align-self: flex-end; }
.bubble.assistant { background: var(--bubble-assistant); align-self: flex-start; }
.bubble.error { background: transparent; color: var(--error); align-self: flex-start; }
.msg-row { display: flex; flex-direction: column; max-width: 75%; gap: 4px; }
.msg-row.user { align-self: flex-end; align-items: flex-end; }
.msg-row.assistant { align-self: flex-start; align-items: flex-start; }
.msg-row .bubble { max-width: 100%; }
.msg-meta {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 11px;
  color: var(--fg-muted);
  padding: 0 4px;
}
.msg-meta .provider-badge {
  width: 14px;
  height: 14px;
  font-size: 9px;
  border-radius: 3px;
}

/* Markdown content inside assistant bubbles */
.bubble.assistant > :first-child { margin-top: 0; }
.bubble.assistant > :last-child { margin-bottom: 0; }
.bubble.assistant p { margin: 0 0 0.6em; }
.bubble.assistant h1, .bubble.assistant h2, .bubble.assistant h3,
.bubble.assistant h4, .bubble.assistant h5, .bubble.assistant h6 {
  margin: 0.8em 0 0.4em; line-height: 1.25;
}
.bubble.assistant h1 { font-size: 1.5em; }
.bubble.assistant h2 { font-size: 1.3em; }
.bubble.assistant h3 { font-size: 1.15em; }
.bubble.assistant ul, .bubble.assistant ol { margin: 0 0 0.6em; padding-left: 1.5em; }
.bubble.assistant li { margin-bottom: 0.2em; }
.bubble.assistant blockquote {
  margin: 0 0 0.6em;
  padding: 0.2em 0.9em;
  border-left: 3px solid var(--border);
  color: var(--fg-muted);
}
.bubble.assistant code {
  background: rgba(127, 127, 127, 0.15);
  padding: 0.1em 0.35em;
  border-radius: 4px;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.92em;
}
.bubble.assistant pre {
  position: relative;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 14px;
  overflow-x: auto;
  margin: 0.4em 0 0.8em;
}
.bubble.assistant pre code {
  background: transparent;
  padding: 0;
  display: block;
  white-space: pre;
  font-size: 0.9em;
}
.bubble.assistant table {
  border-collapse: collapse;
  margin: 0.4em 0 0.8em;
  font-size: 0.95em;
}
.bubble.assistant th, .bubble.assistant td {
  border: 1px solid var(--border);
  padding: 4px 10px;
  text-align: left;
}
.bubble.assistant th { background: var(--bg-muted); }
.bubble.assistant a { color: var(--accent); }
.bubble.assistant a:hover { text-decoration: underline; }
.bubble.assistant hr { border: 0; border-top: 1px solid var(--border); margin: 0.8em 0; }
.bubble.assistant img { max-width: 100%; height: auto; border-radius: 6px; }

.copy-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  font-size: 12px;
  padding: 2px 8px;
  background: var(--bg-muted);
  color: var(--fg-muted);
  border: 1px solid var(--border);
  border-radius: 4px;
  opacity: 0;
  transition: opacity .15s;
}
.bubble.assistant pre:hover .copy-btn,
.bubble.assistant pre:focus-within .copy-btn { opacity: 1; }

#composer {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}
#input { flex: 1; resize: vertical; min-height: 40px; max-height: 200px; }
#send-btn { padding: 0 20px; }

/* --- Modal --- */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: grid;
  place-items: center;
  z-index: 100;
  padding: 16px;
}
.modal[hidden] { display: none; }
.modal-card {
  width: 100%;
  max-width: 360px;
  background: var(--bg-elev);
  padding: 24px;
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  max-height: 90vh;
  overflow-y: auto;
}
.modal-card.wide { max-width: 560px; }
.modal-card h2 {
  margin: 0 0 16px;
  font-size: 18px;
}
.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 12px;
}
.modal-actions button { padding: 8px 16px; }

.model-info-list {
  list-style: none;
  padding: 0;
  margin: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.model-info-list li {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 10px;
  align-items: start;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.model-info-list .name { font-weight: 600; display: flex; align-items: center; gap: 6px; }
.model-info-list .desc { color: var(--fg-muted); font-size: 13px; margin-top: 2px; }
.model-info-list .price { font-size: 12px; color: var(--fg-muted); white-space: nowrap; text-align: right; }
.model-info-list .price.free { color: #16a34a; font-weight: 600; }

/* --- Mobile --- */
@media (max-width: 720px) {
  .chat-page { grid-template-columns: 1fr; }
  #sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: 80%;
    max-width: 320px;
    transform: translateX(-100%);
    transition: transform .2s ease;
    z-index: 10;
  }
  #sidebar.open { transform: translateX(0); }
  #sidebar-toggle { display: inline-block; }
  #chat-header {
    grid-template-columns: 1fr;
    gap: 8px;
  }
  .header-left, .header-right { justify-content: flex-start; flex-wrap: wrap; }
  #chat-header .title { text-align: left; }
  .me-name { max-width: 100px; }
}
