:root {
  --bg: #f3f5f7;
  --panel: #ffffff;
  --panel-2: #f7f8fa;
  --text: #1f2329;
  --muted: #6b7280;
  --line: #e6e8eb;
  --primary: #2563eb;
  --primary-soft: #eaf1ff;
  --danger: #dc2626;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.06);
  --sidebar: #0f172a;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif; color: var(--text); background: var(--bg); }
button, input, textarea { font: inherit; }
button { cursor: pointer; }
.mobile-only { display: none; }

/* AUTH */
.auth-view {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px 16px;
  background:
    radial-gradient(1200px 500px at 10% -10%, #dbeafe 0%, transparent 55%),
    radial-gradient(900px 400px at 100% 0%, #ecfeff 0%, transparent 50%),
    var(--bg);
}
.auth-card {
  width: min(420px, 100%);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 22px;
}
.auth-brand { display: flex; gap: 12px; align-items: center; margin-bottom: 16px; }
.auth-brand h1 { margin: 0; font-size: 20px; }
.auth-brand p { margin: 4px 0 0; color: var(--muted); font-size: 13px; line-height: 1.5; }
.logo {
  width: 44px; height: 44px; border-radius: 14px; display: grid; place-items: center;
  font-weight: 800; color: #fff; background: linear-gradient(135deg, #60a5fa, #2563eb);
}
.logo.sm { width: 36px; height: 36px; border-radius: 12px; font-size: 14px; }
.tabs { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 14px; }
.tab {
  border: 1px solid var(--line); background: var(--panel-2); color: var(--muted);
  border-radius: 10px; padding: 10px; font-weight: 600;
}
.tab.active { background: var(--primary-soft); color: var(--primary); border-color: #bfdbfe; }
#authForm label { display: block; margin: 10px 0 6px; font-size: 13px; color: var(--muted); }
#authForm input {
  width: 100%; border: 1px solid var(--line); border-radius: 10px; padding: 11px 12px; background: #fff;
}
#authForm input:focus { outline: none; border-color: #93c5fd; box-shadow: 0 0 0 3px rgba(37,99,235,.12); }
.btn-primary {
  border: 0; border-radius: 10px; padding: 11px 14px; background: var(--primary); color: #fff; font-weight: 700;
}
.btn-primary:disabled { opacity: .55; cursor: not-allowed; }
.btn-secondary {
  border: 1px solid rgba(255,255,255,.12); border-radius: 10px; padding: 10px 12px;
  background: rgba(255,255,255,.06); color: #e5e7eb;
}
.btn-danger {
  border: 0; border-radius: 10px; padding: 10px 12px;
  background: rgba(239,68,68,.16); color: #fecaca;
}
.btn-block { width: 100%; margin-top: 14px; }
.error-box {
  margin-top: 12px; background: #fef2f2; color: #991b1b; border: 1px solid #fecaca;
  border-radius: 10px; padding: 10px 12px; font-size: 13px; white-space: pre-wrap;
}
.auth-tip { margin: 14px 0 0; color: var(--muted); font-size: 12px; line-height: 1.6; }

/* CHAT LAYOUT */
.chat-view { display: grid; grid-template-columns: 300px 1fr; min-height: 100%; }
.sidebar {
  background: var(--sidebar); color: #e5e7eb; display: flex; flex-direction: column;
  min-height: 100vh; padding: 14px; gap: 12px;
}
.side-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.brand-mini { display: flex; gap: 10px; align-items: center; }
.brand-mini strong { display: block; font-size: 14px; }
.brand-mini span { display: block; font-size: 12px; color: #94a3b8; margin-top: 2px; }
.session-list { flex: 1; overflow: auto; display: flex; flex-direction: column; gap: 6px; padding-right: 2px; }
.session-item {
  text-align: left; border: 1px solid rgba(255,255,255,.06); background: rgba(255,255,255,.04);
  color: #e5e7eb; border-radius: 12px; padding: 10px 12px;
}
.session-item.active, .session-item:hover { background: rgba(37,99,235,.28); border-color: rgba(96,165,250,.35); }
.session-item .t { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.session-item .s { margin-top: 4px; font-size: 11px; color: #94a3b8; display: flex; justify-content: space-between; gap: 8px; }
.session-empty { color: #94a3b8; font-size: 12px; padding: 12px 4px; line-height: 1.6; }
.side-foot { border-top: 1px solid rgba(255,255,255,.08); padding-top: 12px; }
.small-label { display: block; font-size: 12px; color: #94a3b8; margin-bottom: 6px; }
#systemPrompt {
  width: 100%; border: 1px solid rgba(255,255,255,.1); background: rgba(15,23,42,.55);
  color: #f8fafc; border-radius: 10px; padding: 10px 12px; resize: vertical; min-height: 70px;
}
.side-actions { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 10px; }
.hint { margin-top: 10px; font-size: 11px; line-height: 1.55; color: #94a3b8; }

.main { display: grid; grid-template-rows: auto 1fr auto; min-width: 0; min-height: 100vh; background: var(--bg); }
.topbar {
  height: 60px; border-bottom: 1px solid var(--line); background: rgba(255,255,255,.94);
  display: flex; align-items: center; justify-content: space-between; padding: 0 16px; position: sticky; top: 0; z-index: 3;
}
.top-left { display: flex; align-items: center; gap: 10px; min-width: 0; }
.title { font-size: 16px; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 55vw; }
.meta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.status {
  display: inline-flex; align-items: center; gap: 8px; font-size: 12px; color: var(--muted);
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 999px; padding: 6px 10px; white-space: nowrap;
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: #94a3b8; }
.dot.ok { background: #22c55e; }
.dot.busy { background: #f59e0b; }
.dot.err { background: #ef4444; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--line);
  background: #fff; color: var(--text); font-size: 18px; line-height: 1;
}

.chat-wrap { overflow: auto; padding: 18px 14px 8px; }
.chat { width: min(860px, 100%); margin: 0 auto; display: flex; flex-direction: column; gap: 14px; }
.empty { margin: 8vh auto 0; text-align: center; color: var(--muted); width: min(640px, 100%); }
.empty h2 { margin: 0 0 8px; color: var(--text); font-size: 26px; }
.empty p { margin: 0 auto; line-height: 1.6; max-width: 520px; }
.msg { display: grid; grid-template-columns: 38px 1fr; gap: 12px; }
.avatar {
  width: 38px; height: 38px; border-radius: 12px; display: grid; place-items: center;
  font-size: 13px; font-weight: 700;
}
.msg.user .avatar { background: #dbeafe; color: #1d4ed8; }
.msg.assistant .avatar { background: #ecfdf5; color: #047857; }
.bubble {
  border: 1px solid var(--line); background: #fff; border-radius: 16px; padding: 12px 14px;
  box-shadow: var(--shadow); line-height: 1.7; font-size: 14.5px; overflow-wrap: anywhere; white-space: pre-wrap;
}
.msg.user .bubble { background: var(--primary-soft); border-color: #d7e7ff; }
.role { font-size: 12px; color: var(--muted); font-weight: 700; margin-bottom: 6px; }
.bubble code { font-family: Consolas, "SFMono-Regular", monospace; background: #f1f5f9; padding: 1px 5px; border-radius: 5px; }
.bubble pre {
  margin: 10px 0 0; padding: 12px; background: #0f172a; color: #e2e8f0; border-radius: 12px;
  overflow: auto; white-space: pre;
}
.bubble pre code { background: transparent; color: inherit; padding: 0; }
.typing { display: inline-flex; gap: 4px; height: 18px; align-items: center; }
.typing span { width: 6px; height: 6px; border-radius: 50%; background: #94a3b8; animation: blink 1.2s infinite ease-in-out; }
.typing span:nth-child(2) { animation-delay: .15s; }
.typing span:nth-child(3) { animation-delay: .3s; }
@keyframes blink { 0%,80%,100%{opacity:.25} 40%{opacity:1} }

.composer-wrap { border-top: 1px solid var(--line); background: rgba(255,255,255,.96); padding: 12px 14px 16px; }
.composer {
  width: min(860px, 100%); margin: 0 auto; border: 1px solid var(--line); background: #fff;
  border-radius: 18px; box-shadow: var(--shadow); padding: 10px; display: grid; grid-template-columns: 1fr auto; gap: 10px; align-items: end;
}
.composer textarea {
  border: 0; background: transparent; color: var(--text); resize: none; min-height: 52px; max-height: 180px;
  padding: 10px 8px; font-size: 14.5px; outline: none;
}
.send-btn {
  width: 44px; height: 44px; border: 0; border-radius: 12px; background: var(--primary); color: #fff;
  font-size: 18px; font-weight: 700;
}
.send-btn:disabled { opacity: .45; cursor: not-allowed; }
.footer-note {
  width: min(860px, 100%); margin: 8px auto 0; font-size: 12px; color: var(--muted);
  display: flex; justify-content: space-between; gap: 12px;
}
.error-banner {
  width: min(860px, 100%); margin: 0 auto 12px; background: #fef2f2; color: #991b1b;
  border: 1px solid #fecaca; border-radius: 12px; padding: 10px 12px; font-size: 13px; white-space: pre-wrap;
}

@media (max-width: 900px) {
  .mobile-only { display: inline-grid; place-items: center; }
  .chat-view { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed; inset: 0 auto 0 0; width: min(86vw, 320px); z-index: 20;
    transform: translateX(-105%); transition: transform .2s ease; box-shadow: 20px 0 40px rgba(0,0,0,.25);
  }
  .sidebar.open { transform: translateX(0); }
  .title { max-width: 48vw; }
}