/* ========== 主题变量 ========== */
:root {
  --radius: 12px;
  --radius-sm: 8px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC",
          "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
  --mono: "SF Mono", "Cascadia Code", Consolas, "Courier New", monospace;
  --sidebar-w: 260px;
}

[data-theme="dark"] {
  --bg: #0d1117;
  --bg-elev: #161b22;
  --bg-elev-2: #1f2630;
  --bg-input: #0d1117;
  --border: #2a3340;
  --border-strong: #3a4657;
  --text: #e6edf3;
  --text-dim: #8b949e;
  --accent: #4f8cff;
  --accent-hover: #3d7bf0;
  --accent-soft: rgba(79, 140, 255, 0.12);
  --user-bubble: #1f3a5f;
  --danger: #f85149;
  --code-bg: #0b0f14;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.45);
}

[data-theme="light"] {
  --bg: #f7f8fa;
  --bg-elev: #ffffff;
  --bg-elev-2: #f0f2f5;
  --bg-input: #ffffff;
  --border: #e1e4e8;
  --border-strong: #c9ced6;
  --text: #1c2128;
  --text-dim: #6b7280;
  --accent: #2563eb;
  --accent-hover: #1d4fd8;
  --accent-soft: rgba(37, 99, 235, 0.08);
  --user-bubble: #e8efff;
  --danger: #d1242f;
  --code-bg: #f3f4f6;
  --shadow: 0 8px 24px rgba(15, 23, 42, 0.1);
}

* { box-sizing: border-box; }

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

/* ========== 通用控件 ========== */
.btn {
  font: inherit;
  font-size: 14px;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, transform 0.05s;
}
.btn:hover { background: var(--bg-elev-2); border-color: var(--border-strong); }
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
  font-weight: 500;
}
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.ghost { background: transparent; }
.btn.ghost:hover { background: var(--bg-elev-2); }
.btn.wide { width: 100%; }
.btn.small { padding: 4px 10px; font-size: 12px; }
.btn.icon-only { padding: 6px 10px; line-height: 1; }

.field { display: block; margin-bottom: 14px; }
.field > span {
  display: block;
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 6px;
}
.field input, .field select, textarea, select {
  width: 100%;
  font: inherit;
  font-size: 14px;
  color: var(--text);
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  outline: none;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.field input:focus, textarea:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.error {
  color: var(--danger);
  font-size: 13px;
  margin: 0 0 12px;
}
.hint { color: var(--text-dim); font-size: 12.5px; margin: 0 0 12px; }

/* ========== 登录页 ========== */
.auth-view {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background:
    radial-gradient(900px 500px at 15% -10%, var(--accent-soft), transparent 70%),
    var(--bg);
}
.auth-card {
  width: min(400px, 100%);
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 30px 28px;
  box-shadow: var(--shadow);
}
.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 650;
  font-size: 19px;
  letter-spacing: -0.02em;
}
.brand.small { font-size: 15px; }
.brand-mark {
  color: var(--accent);
  font-size: 1.15em;
  filter: drop-shadow(0 0 8px var(--accent-soft));
}
.auth-sub {
  color: var(--text-dim);
  font-size: 13.5px;
  margin: 8px 0 22px;
}

.tabs {
  display: flex;
  gap: 4px;
  background: var(--bg-elev-2);
  padding: 4px;
  border-radius: var(--radius-sm);
  margin-bottom: 20px;
}
.tab {
  flex: 1;
  font: inherit;
  font-size: 14px;
  padding: 8px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.tab.active {
  background: var(--bg-elev);
  color: var(--text);
  font-weight: 550;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
}

/* ========== 主布局 ========== */
.app-view {
  display: grid;
  grid-template-columns: var(--sidebar-w) 1fr;
  height: 100vh;
  height: 100dvh;
}
.app-view.sidebar-hidden { grid-template-columns: 0 1fr; }
.app-view.sidebar-hidden .sidebar { transform: translateX(-100%); opacity: 0; pointer-events: none; }

.sidebar {
  display: flex;
  flex-direction: column;
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  overflow: hidden;
  transition: transform 0.2s ease, opacity 0.2s ease;
}
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 14px 14px 10px;
}
.conv-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px 8px;
}
.conv-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 13.5px;
  color: var(--text-dim);
  transition: background 0.12s, color 0.12s;
}
.conv-item:hover { background: var(--bg-elev-2); color: var(--text); }
.conv-item.active {
  background: var(--accent-soft);
  color: var(--text);
  font-weight: 500;
}
.conv-item .conv-name {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conv-item .conv-del {
  opacity: 0;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 14px;
  padding: 0 4px;
  border-radius: 4px;
}
.conv-item:hover .conv-del { opacity: 1; }
.conv-item .conv-del:hover { color: var(--danger); background: var(--bg-elev); }

.sidebar-foot {
  border-top: 1px solid var(--border);
  padding: 10px;
  display: grid;
  gap: 8px;
}
.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 2px;
}
.user-email {
  font-size: 12px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* ========== 主区域 ========== */
.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  height: 100%;
}
.main-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}
.main-head h1 {
  flex: 1;
  font-size: 15px;
  font-weight: 550;
  margin: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.head-right { display: flex; align-items: center; gap: 8px; }
.model-select {
  width: auto;
  font-size: 12.5px;
  padding: 6px 10px;
  max-width: 190px;
}

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0 8px;
  scroll-behavior: smooth;
}
.msg {
  display: flex;
  gap: 12px;
  width: min(780px, 100%);
  margin: 0 auto 20px;
  padding: 0 20px;
}
.msg-avatar {
  flex: 0 0 28px;
  width: 28px;
  height: 28px;
  border-radius: 7px;
  display: grid;
  place-items: center;
  font-size: 13px;
  font-weight: 600;
  background: var(--bg-elev-2);
  color: var(--text-dim);
}
.msg.user .msg-avatar { background: var(--user-bubble); color: var(--text); }
.msg.assistant .msg-avatar { background: var(--accent-soft); color: var(--accent); }
.msg-body { flex: 1; min-width: 0; padding-top: 3px; }
.msg-role {
  font-size: 12px;
  color: var(--text-dim);
  margin-bottom: 5px;
  font-weight: 500;
}
.msg-content {
  font-size: 14.5px;
  line-height: 1.72;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.msg-content > :first-child { margin-top: 0; }
.msg-content > :last-child { margin-bottom: 0; }
.msg-content p { margin: 0 0 0.85em; }
.msg-content ul, .msg-content ol { margin: 0 0 0.85em; padding-left: 1.5em; }
.msg-content li { margin: 0.2em 0; }
.msg-content h1, .msg-content h2, .msg-content h3 {
  margin: 1.1em 0 0.5em;
  line-height: 1.35;
  font-weight: 600;
}
.msg-content h1 { font-size: 1.35em; }
.msg-content h2 { font-size: 1.2em; }
.msg-content h3 { font-size: 1.08em; }
.msg-content a { color: var(--accent); text-decoration: none; }
.msg-content a:hover { text-decoration: underline; }
.msg-content blockquote {
  margin: 0 0 0.85em;
  padding: 2px 0 2px 14px;
  border-left: 3px solid var(--border-strong);
  color: var(--text-dim);
}
.msg-content code {
  font-family: var(--mono);
  font-size: 0.88em;
  background: var(--code-bg);
  border: 1px solid var(--border);
  padding: 1.5px 5px;
  border-radius: 5px;
}
.msg-content pre {
  margin: 0 0 0.9em;
  padding: 13px 15px;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow-x: auto;
}
.msg-content pre code {
  background: none;
  border: 0;
  padding: 0;
  font-size: 13px;
  line-height: 1.6;
}
.msg-content table {
  border-collapse: collapse;
  margin: 0 0 0.9em;
  font-size: 13.5px;
  display: block;
  overflow-x: auto;
}
.msg-content th, .msg-content td {
  border: 1px solid var(--border);
  padding: 7px 11px;
  text-align: left;
}
.msg-content th { background: var(--bg-elev-2); font-weight: 600; }

.cursor {
  display: inline-block;
  width: 7px;
  height: 1.05em;
  background: var(--accent);
  vertical-align: text-bottom;
  border-radius: 1px;
  animation: blink 1s step-end infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.empty-state {
  display: grid;
  place-items: center;
  height: 100%;
  color: var(--text-dim);
  text-align: center;
  padding: 20px;
}
.empty-state .big { font-size: 26px; margin-bottom: 10px; color: var(--accent); }

/* ========== 输入区 ========== */
.composer {
  width: min(820px, 100%);
  margin: 0 auto;
  padding: 12px 20px 20px;
}
.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}
.composer-tools {
  display: flex;
  align-items: center;
  gap: 4px;
  padding-bottom: 4px;
}
.composer-tools .btn { padding: 7px 9px; }
.composer-tools .btn.small { padding: 7px 10px; font-size: 12.5px; }
.composer-tools .btn.active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent);
}
.composer textarea {
  flex: 1;
  resize: none;
  max-height: 200px;
  min-height: 44px;
  line-height: 1.55;
  padding: 11px 13px;
  font-size: 14.5px;
  field-sizing: content;
}

/* 附件预览条 */
.attach-rail {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 8px;
}
.attach-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 10px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  max-width: 240px;
}
.attach-chip img {
  width: 28px;
  height: 28px;
  object-fit: cover;
  border-radius: 4px;
}
.attach-chip .attach-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.attach-chip .attach-del {
  border: 0;
  background: transparent;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 13px;
  padding: 0 2px;
}
.attach-chip .attach-del:hover { color: var(--danger); }

/* 消息内附件展示 */
.msg-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 8px;
}
.msg-attachments img {
  max-width: 260px;
  max-height: 200px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
}
.msg-att-file {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 10px;
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  color: var(--text-dim);
}

/* 搜索中提示 */
.search-badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  color: var(--text-dim);
  background: var(--bg-elev-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 10px;
  margin-bottom: 8px;
}

.dialog-actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}
.prefs-section {
  border-top: 1px solid var(--border);
  margin-top: 22px;
  padding-top: 18px;
}

/* ========== 设置弹窗 ========== */
.dialog {
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--bg-elev);
  color: var(--text);
  padding: 0;
  width: min(520px, calc(100vw - 32px));
  box-shadow: var(--shadow);
}
.dialog::backdrop { background: rgba(0, 0, 0, 0.55); backdrop-filter: blur(2px); }
.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}
.dialog-head h2 { margin: 0; font-size: 16px; font-weight: 600; }
.dialog-body { padding: 20px; max-height: 70vh; overflow-y: auto; }
.dialog-body h3 { font-size: 14px; margin: 0 0 6px; }

.key-list { display: grid; gap: 8px; margin-bottom: 18px; }
.key-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  background: var(--bg-elev-2);
  border-radius: var(--radius-sm);
  font-size: 13.5px;
}
.key-row .key-meta { display: grid; gap: 2px; min-width: 0; }
.key-row .key-name { font-weight: 550; }
.key-row .key-url {
  font-size: 11.5px;
  color: var(--text-dim);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.key-empty { color: var(--text-dim); font-size: 13px; padding: 10px 0 16px; }
.key-form { border-top: 1px solid var(--border); padding-top: 18px; }
.key-form .btn { margin-top: 2px; }

/* ========== 响应式 ========== */
@media (max-width: 760px) {
  .app-view { grid-template-columns: 0 1fr; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: var(--sidebar-w);
    z-index: 50;
    transform: translateX(-100%);
    opacity: 0;
    pointer-events: none;
    transition: transform 0.22s ease, opacity 0.22s ease;
  }
  .app-view:not(.sidebar-hidden) .sidebar {
    transform: translateX(0);
    opacity: 1;
    pointer-events: auto;
    box-shadow: var(--shadow);
  }
  .app-view { grid-template-columns: 0 1fr !important; }
  .msg { padding: 0 14px; }
  .composer { padding: 10px 14px 14px; }
  .model-select { max-width: 120px; }
}

@media (prefers-reduced-motion: reduce) {
  * { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}
