:root {
  --bg: #070a12;
  --bg-elevated: rgba(22, 28, 42, 0.72);
  --bg-sidebar: rgba(12, 16, 28, 0.88);
  --bg-bubble-user: linear-gradient(145deg, #3b5bdb 0%, #5b7cfa 55%, #7c9bff 100%);
  --bg-bubble-ai: rgba(28, 34, 52, 0.85);
  --border: rgba(255, 255, 255, 0.08);
  --border-strong: rgba(255, 255, 255, 0.14);
  --text: #f2f4f8;
  --muted: #9aa3b5;
  --accent: #6e8bff;
  --accent-2: #a78bfa;
  --accent-glow: rgba(110, 139, 255, 0.45);
  --danger: #ff6b7a;
  --danger-bg: rgba(255, 107, 122, 0.1);
  --radius: 18px;
  --radius-sm: 12px;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.4);
  --font: "Inter", "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei",
    system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}
button, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
::selection { background: rgba(110, 139, 255, 0.35); }

.bg-glow {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 0;
  background:
    radial-gradient(900px 500px at 12% -8%, rgba(110, 139, 255, 0.22), transparent 60%),
    radial-gradient(700px 420px at 92% 8%, rgba(167, 139, 250, 0.16), transparent 55%),
    radial-gradient(600px 380px at 50% 110%, rgba(56, 189, 248, 0.08), transparent 50%),
    var(--bg);
}

.app {
  position: relative;
  z-index: 1;
  display: grid;
  grid-template-columns: 300px 1fr;
  height: 100%;
  max-width: 1440px;
  margin: 0 auto;
}

/* —— Sidebar —— */
.sidebar {
  background: var(--bg-sidebar);
  border-right: 1px solid var(--border);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  padding: 22px 18px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 2px 8px;
}
.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  color: #fff;
  background: linear-gradient(145deg, #5b7cfa, #a78bfa);
  box-shadow: 0 8px 24px var(--accent-glow);
  flex-shrink: 0;
}
.brand-text h1 {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.brand-text p {
  margin: 2px 0 0;
  font-size: 0.75rem;
  color: var(--muted);
}

.field { display: flex; flex-direction: column; gap: 8px; }
.field-label {
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}
.select-wrap { position: relative; }
.select-wrap select {
  width: 100%;
  appearance: none;
  -webkit-appearance: none;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 36px 12px 14px;
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.select-wrap select:focus {
  border-color: rgba(110, 139, 255, 0.55);
  box-shadow: 0 0 0 3px rgba(110, 139, 255, 0.18);
}
.select-chevron {
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  pointer-events: none;
}

.sidebar-footer {
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* —— Buttons —— */
.btn {
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  transition: transform 0.12s ease, background 0.15s ease, border-color 0.15s ease, box-shadow 0.15s ease;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.btn:hover { border-color: var(--border-strong); }
.btn:active { transform: scale(0.98); }
.btn.new-chat {
  width: 100%;
  background: linear-gradient(135deg, rgba(110, 139, 255, 0.22), rgba(167, 139, 250, 0.18));
  border-color: rgba(110, 139, 255, 0.35);
  color: #e8ecff;
  font-weight: 600;
  padding: 12px 14px;
}
.btn.new-chat:hover {
  background: linear-gradient(135deg, rgba(110, 139, 255, 0.32), rgba(167, 139, 250, 0.26));
  box-shadow: 0 6px 20px rgba(110, 139, 255, 0.2);
}
.btn.ghost {
  background: transparent;
  border-color: transparent;
  color: var(--muted);
}
.btn.ghost:hover { color: var(--text); background: rgba(255,255,255,0.05); }
.btn.danger {
  color: #ffb4bc;
  border-color: rgba(255, 107, 122, 0.28);
  background: var(--danger-bg);
}
.btn.danger:hover { border-color: rgba(255, 107, 122, 0.45); background: rgba(255, 107, 122, 0.16); }
.btn.block { width: 100%; }
.btn.compact { padding: 8px 12px; font-size: 0.85rem; }
.btn.icon {
  width: 40px; height: 40px; padding: 0;
  display: none;
  align-items: center; justify-content: center;
  background: rgba(255,255,255,0.04);
  border-radius: 12px;
}
.btn.send {
  width: 46px;
  height: 46px;
  padding: 0;
  border: none;
  border-radius: 14px;
  flex-shrink: 0;
  color: #fff;
  background: linear-gradient(145deg, #5b7cfa, #7c5cff);
  box-shadow: 0 8px 22px var(--accent-glow);
}
.btn.send:hover {
  filter: brightness(1.08);
  box-shadow: 0 10px 28px rgba(110, 139, 255, 0.55);
}
.btn.send:disabled {
  opacity: 0.45;
  cursor: not-allowed;
  filter: none;
  box-shadow: none;
}

/* —— Main —— */
.main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 20px;
  border-bottom: 1px solid var(--border);
  background: rgba(7, 10, 18, 0.55);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
}
.topbar-title {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--muted);
  min-width: 0;
}
.topbar-title #model-label {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #34d399;
  box-shadow: 0 0 0 4px rgba(52, 211, 153, 0.18);
  flex-shrink: 0;
}
#btn-new-mobile { display: none; }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 28px 20px 12px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  scroll-behavior: smooth;
}
.messages::-webkit-scrollbar { width: 8px; }
.messages::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.12);
  border-radius: 8px;
}

/* Empty state */
.empty {
  margin: auto;
  text-align: center;
  color: var(--muted);
  max-width: 520px;
  padding: 32px 20px;
  animation: fadeUp 0.45s ease;
}
.empty-icon {
  width: 72px;
  height: 72px;
  margin: 0 auto 18px;
  border-radius: 22px;
  display: grid;
  place-items: center;
  color: #fff;
  background: linear-gradient(145deg, #5b7cfa, #a78bfa);
  box-shadow: 0 16px 40px var(--accent-glow);
}
.empty h2 {
  margin: 0 0 10px;
  color: var(--text);
  font-size: 1.55rem;
  font-weight: 700;
  letter-spacing: -0.03em;
}
.empty p {
  margin: 0 auto 22px;
  line-height: 1.6;
  max-width: 400px;
  font-size: 0.95rem;
}
.suggestions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  text-align: left;
}
.suggestion {
  border: 1px solid var(--border);
  background: rgba(255,255,255,0.03);
  border-radius: 14px;
  padding: 14px 14px;
  color: var(--text);
  font-size: 0.86rem;
  line-height: 1.4;
  text-align: left;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.12s ease;
}
.suggestion:hover {
  border-color: rgba(110, 139, 255, 0.4);
  background: rgba(110, 139, 255, 0.08);
  transform: translateY(-1px);
}
.suggestion span {
  display: block;
  font-size: 0.72rem;
  color: var(--muted);
  margin-bottom: 4px;
  font-weight: 600;
}

@keyframes fadeUp {
  from { opacity: 0; transform: translateY(10px); }
  to { opacity: 1; transform: none; }
}

/* Messages */
.msg {
  max-width: min(720px, 94%);
  padding: 14px 16px;
  border-radius: var(--radius);
  line-height: 1.65;
  white-space: pre-wrap;
  word-break: break-word;
  animation: fadeUp 0.28s ease;
  position: relative;
}
.msg.user {
  align-self: flex-end;
  color: #fff;
  background: var(--bg-bubble-user);
  border: 1px solid rgba(255,255,255,0.12);
  border-bottom-right-radius: 6px;
  box-shadow: 0 10px 28px rgba(59, 91, 219, 0.28);
}
.msg.assistant {
  align-self: flex-start;
  background: var(--bg-bubble-ai);
  border: 1px solid var(--border);
  border-bottom-left-radius: 6px;
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: var(--shadow);
}
.msg .role {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.72rem;
  font-weight: 600;
  color: rgba(255,255,255,0.65);
  margin-bottom: 8px;
  letter-spacing: 0.04em;
}
.msg.assistant .role { color: var(--muted); }
.msg .role-badge {
  width: 20px;
  height: 20px;
  border-radius: 7px;
  display: inline-grid;
  place-items: center;
  font-size: 0.65rem;
  font-weight: 700;
  color: #fff;
  background: linear-gradient(145deg, #5b7cfa, #a78bfa);
}
.msg.user .role-badge {
  background: rgba(255,255,255,0.22);
}
.msg.error {
  align-self: center;
  background: var(--danger-bg);
  border: 1px solid rgba(255, 107, 122, 0.35);
  color: #ffb4bc;
  max-width: 90%;
  border-radius: 14px;
}
.msg .body { font-size: 0.95rem; }
.msg .cursor {
  display: inline-block;
  width: 0.5em;
  height: 1.05em;
  margin-left: 3px;
  border-radius: 2px;
  background: var(--accent);
  vertical-align: text-bottom;
  animation: blink 1s step-end infinite;
}
@keyframes blink { 50% { opacity: 0; } }

/* Composer */
.composer-wrap {
  padding: 8px 20px 18px;
  background: linear-gradient(180deg, transparent, rgba(7, 10, 18, 0.85) 30%);
}
.composer {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  padding: 10px 10px 10px 16px;
  border-radius: 22px;
  border: 1px solid var(--border-strong);
  background: rgba(18, 24, 38, 0.85);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35), inset 0 1px 0 rgba(255,255,255,0.04);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}
.composer:focus-within {
  border-color: rgba(110, 139, 255, 0.5);
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.35), 0 0 0 3px rgba(110, 139, 255, 0.15);
}
.composer textarea {
  flex: 1;
  resize: none;
  min-height: 28px;
  max-height: 160px;
  padding: 10px 0;
  border: none;
  background: transparent;
  outline: none;
  line-height: 1.5;
}
.composer textarea::placeholder { color: #6b7388; }
.composer-hint {
  margin: 10px 0 0;
  text-align: center;
  font-size: 0.72rem;
  color: #6b7388;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  backdrop-filter: blur(2px);
  z-index: 20;
}

@media (max-width: 800px) {
  .app { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    width: min(88vw, 320px);
    z-index: 30;
    transform: translateX(-105%);
    transition: transform 0.22s ease;
    box-shadow: var(--shadow);
  }
  .sidebar.open { transform: translateX(0); }
  .btn.icon { display: inline-flex; }
  #btn-new-mobile { display: inline-flex; }
  .messages { padding: 18px 14px 8px; }
  .composer-wrap { padding: 6px 12px calc(14px + env(safe-area-inset-bottom, 0px)); }
  .suggestions { grid-template-columns: 1fr; }
  .empty h2 { font-size: 1.3rem; }
}

@media (min-width: 801px) {
  .messages {
    padding-left: max(20px, calc((100% - 760px) / 2));
    padding-right: max(20px, calc((100% - 760px) / 2));
  }
  .composer-wrap {
    padding-left: max(20px, calc((100% - 760px) / 2));
    padding-right: max(20px, calc((100% - 760px) / 2));
  }
}

/* Generated images */
.image-gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 10px;
}
.image-gallery img {
  display: block;
  max-width: min(100%, 420px);
  width: 100%;
  height: auto;
  border-radius: 12px;
  border: 1px solid var(--border-strong);
  cursor: zoom-in;
  background: rgba(0, 0, 0, 0.25);
  transition: transform 0.15s ease, box-shadow 0.15s ease;
}
.image-gallery img:hover {
  transform: translateY(-1px);
  box-shadow: 0 12px 28px rgba(0, 0, 0, 0.35);
}
.msg.assistant .body:has(.image-gallery) {
  white-space: normal;
}
