:root {
  --bg: #0b0f17;
  --bg-elevated: #121826;
  --bg-surface: #171f31;
  --bg-hover: #1e2940;
  --border: rgba(148, 163, 184, 0.14);
  --border-strong: rgba(148, 163, 184, 0.25);
  --text: #eef2ff;
  --text-muted: #94a3b8;
  --accent: #7c3aed;
  --accent-2: #10b981;
  --accent-soft: rgba(124, 58, 237, 0.16);
  --user-bubble: #24304a;
  --code-bg: #090d14;
  --danger: #ef4444;
  --warn: #f59e0b;
  --ok: #10b981;
  --shadow: 0 16px 40px rgba(0, 0, 0, 0.35);
  --radius: 14px;
}

[data-theme="light"] {
  --bg: #f8fafc;
  --bg-elevated: #ffffff;
  --bg-surface: #f1f5f9;
  --bg-hover: #e2e8f0;
  --border: rgba(15, 23, 42, 0.1);
  --border-strong: rgba(15, 23, 42, 0.18);
  --text: #0f172a;
  --text-muted: #475569;
  --accent: #6366f1;
  --accent-2: #059669;
  --accent-soft: rgba(99, 102, 241, 0.12);
  --user-bubble: #e0e7ff;
  --code-bg: #0f172a;
  --shadow: 0 12px 30px rgba(15, 23, 42, 0.1);
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

.app-shell {
  display: flex;
  height: 100dvh;
  overflow: hidden;
}

/* Sidebar */
.sidebar {
  width: 280px;
  flex-shrink: 0;
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px;
  gap: 12px;
  z-index: 30;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
}

.brand-icon {
  width: 34px;
  height: 34px;
  border-radius: 10px;
}

.brand-name {
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.brand-sub {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.new-chat-btn {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 10px 14px;
  border-radius: 11px;
  border: 1px solid var(--border-strong);
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.18), rgba(16, 185, 129, 0.12));
  color: var(--text);
  font-weight: 600;
  font-size: 0.88rem;
  cursor: pointer;
  transition: transform 0.12s ease, border-color 0.15s ease, background 0.15s ease;
}

.new-chat-btn:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
}

.sidebar-section-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  padding: 4px 6px 0;
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding-right: 2px;
}

.conv-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: 10px;
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.86rem;
  transition: background 0.15s ease, color 0.15s ease;
}

.conv-item:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.conv-item.active {
  background: var(--accent-soft);
  color: var(--text);
  font-weight: 500;
}

.conv-title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conv-delete {
  opacity: 0;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  padding: 2px 5px;
  border-radius: 6px;
  font-size: 0.85rem;
  transition: opacity 0.15s, color 0.15s, background 0.15s;
}

.conv-item:hover .conv-delete,
.conv-item.active .conv-delete {
  opacity: 0.75;
}

.conv-delete:hover {
  opacity: 1;
  color: var(--danger);
  background: rgba(239, 68, 68, 0.12);
}

.sidebar-footer {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.status-card {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 11px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
}

.status-dot {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  flex-shrink: 0;
  background: var(--warn);
}

.status-ok .status-dot {
  background: var(--ok);
  box-shadow: 0 0 0 4px rgba(16, 185, 129, 0.18);
}

.status-offline .status-dot {
  background: var(--danger);
  box-shadow: 0 0 0 4px rgba(239, 68, 68, 0.18);
}

.status-loading .status-dot {
  background: var(--warn);
  animation: pulse 1.4s infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.45; transform: scale(0.85); }
}

.status-title {
  font-size: 0.8rem;
  font-weight: 600;
}

.status-sub {
  font-size: 0.72rem;
  color: var(--text-muted);
}

.footer-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.ghost-btn, .primary-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 12px;
  border-radius: 10px;
  font-size: 0.82rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}

.ghost-btn {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
}

.ghost-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.primary-btn {
  border: none;
  background: linear-gradient(135deg, #6366f1, #7c3aed);
  color: #fff;
}

.primary-btn:hover {
  filter: brightness(1.08);
}

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 9px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}

.icon-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

/* Main Area */
.main-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  background:
    radial-gradient(900px 450px at 80% -10%, rgba(124, 58, 237, 0.12), transparent 60%),
    radial-gradient(700px 400px at 10% 110%, rgba(16, 185, 129, 0.08), transparent 60%),
    var(--bg);
}

.topbar {
  height: 56px;
  padding: 0 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(11, 15, 23, 0.55);
  backdrop-filter: blur(10px);
}

[data-theme="light"] .topbar {
  background: rgba(255, 255, 255, 0.72);
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.topbar-title {
  font-weight: 600;
  font-size: 0.94rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.model-pill {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 5px 11px;
  border-radius: 999px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  color: var(--text-muted);
}

/* Messages */
.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px 16px;
  scroll-behavior: smooth;
}

.welcome-view {
  max-width: 720px;
  margin: 6vh auto 0;
  text-align: center;
  padding: 12px;
}

.welcome-avatar img {
  width: 64px;
  height: 64px;
  border-radius: 18px;
  box-shadow: 0 12px 32px rgba(99, 102, 241, 0.35);
}

.welcome-view h1 {
  margin: 18px 0 10px;
  font-size: 2rem;
  letter-spacing: -0.03em;
}

.gradient-text {
  background: linear-gradient(135deg, #818cf8, #c084fc 55%, #34d399);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.welcome-desc {
  color: var(--text-muted);
  font-size: 0.96rem;
  line-height: 1.6;
  max-width: 540px;
  margin: 0 auto 28px;
}

.suggestions-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  text-align: left;
}

.suggestion-card {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 14px 15px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 0.88rem;
  line-height: 1.45;
  cursor: pointer;
  transition: transform 0.14s ease, border-color 0.15s ease, background 0.15s ease;
}

.suggestion-card:hover {
  transform: translateY(-2px);
  border-color: var(--accent);
  background: var(--bg-surface);
}

.suggestion-emoji {
  font-size: 1.15rem;
  line-height: 1.2;
}

.messages-list {
  max-width: 820px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.msg-row {
  display: flex;
  gap: 12px;
  align-items: flex-start;
}

.msg-row.user {
  justify-content: flex-end;
}

.msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  flex-shrink: 0;
  overflow: hidden;
  margin-top: 2px;
}

.msg-avatar img {
  width: 100%;
  height: 100%;
}

.msg-body {
  max-width: 86%;
  min-width: 0;
}

.msg-row.user .msg-bubble {
  background: var(--user-bubble);
  border: 1px solid var(--border);
  padding: 11px 15px;
  border-radius: 16px 16px 4px 16px;
  white-space: pre-wrap;
  word-wrap: break-word;
  line-height: 1.55;
  font-size: 0.94rem;
}

.msg-row.assistant .msg-bubble {
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  padding: 14px 18px;
  border-radius: 4px 16px 16px 16px;
  line-height: 1.65;
  font-size: 0.94rem;
  overflow-wrap: break-word;
}

.msg-bubble p {
  margin: 0 0 10px;
}

.msg-bubble p:last-child {
  margin-bottom: 0;
}

.msg-bubble ul, .msg-bubble ol {
  margin: 6px 0 10px;
  padding-left: 22px;
}

.msg-bubble li {
  margin: 4px 0;
}

.msg-bubble h1, .msg-bubble h2, .msg-bubble h3, .msg-bubble h4 {
  margin: 14px 0 8px;
  line-height: 1.3;
}

.msg-bubble h1 { font-size: 1.25rem; }
.msg-bubble h2 { font-size: 1.12rem; }
.msg-bubble h3 { font-size: 1.02rem; }

.msg-bubble blockquote {
  margin: 10px 0;
  padding: 8px 14px;
  border-left: 3px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 0 8px 8px 0;
  color: var(--text-muted);
}

.msg-bubble code {
  font-family: "JetBrains Mono", "Cascadia Code", Consolas, monospace;
  font-size: 0.85em;
  background: rgba(148, 163, 184, 0.16);
  padding: 2px 6px;
  border-radius: 5px;
}

.code-block {
  margin: 12px 0;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--code-bg);
  color: #e2e8f0;
}

.code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 12px;
  font-size: 0.74rem;
  color: #94a3b8;
  background: rgba(255, 255, 255, 0.04);
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
}

.code-copy-btn {
  border: none;
  background: rgba(255, 255, 255, 0.08);
  color: #e2e8f0;
  font-size: 0.72rem;
  padding: 3px 9px;
  border-radius: 6px;
  cursor: pointer;
}

.code-copy-btn:hover {
  background: rgba(255, 255, 255, 0.18);
}

.code-block pre {
  margin: 0;
  padding: 12px 14px;
  overflow-x: auto;
  font-size: 0.84rem;
  line-height: 1.5;
}

.code-block pre code {
  background: transparent;
  padding: 0;
  color: inherit;
}

.thinking-box {
  margin-bottom: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg-surface);
  overflow: hidden;
}

.thinking-box summary {
  cursor: pointer;
  padding: 8px 12px;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  user-select: none;
}

.thinking-content {
  padding: 8px 12px 12px;
  font-size: 0.82rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  white-space: pre-wrap;
  line-height: 1.5;
}

.msg-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 6px;
  font-size: 0.73rem;
  color: var(--text-muted);
}

.msg-action-btn {
  border: none;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.74rem;
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 6px;
}

.msg-action-btn:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.typing-dots {
  display: inline-flex;
  gap: 4px;
  padding: 4px 2px;
}

.typing-dots span {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
  animation: bounce 1.2s infinite ease-in-out;
}

.typing-dots span:nth-child(2) { animation-delay: 0.16s; }
.typing-dots span:nth-child(3) { animation-delay: 0.32s; }

@keyframes bounce {
  0%, 80%, 100% { transform: scale(0.6); opacity: 0.4; }
  40% { transform: scale(1); opacity: 1; }
}

.error-banner {
  color: #fca5a5;
  background: rgba(239, 68, 68, 0.12);
  border: 1px solid rgba(239, 68, 68, 0.3);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.88rem;
}

/* Composer */
.composer-wrap {
  padding: 10px 16px 14px;
  max-width: 852px;
  width: 100%;
  margin: 0 auto;
}

.composer {
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: 18px;
  padding: 10px 12px 8px;
  box-shadow: var(--shadow);
  transition: border-color 0.15s ease;
}

.composer:focus-within {
  border-color: var(--accent);
}

.composer textarea {
  width: 100%;
  border: none;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  line-height: 1.5;
  max-height: 180px;
  padding: 4px 6px;
}

.composer textarea::placeholder {
  color: var(--text-muted);
}

.composer-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 6px;
  gap: 8px;
}

.composer-left-tools, .composer-right-tools {
  display: flex;
  align-items: center;
  gap: 8px;
}

.pill-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 11px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  font-size: 0.78rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}

.pill-toggle:hover {
  background: var(--bg-hover);
  color: var(--text);
}

.pill-toggle[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--text);
}

.queue-notice {
  font-size: 0.78rem;
  color: var(--warn);
}

.send-btn {
  width: 38px;
  height: 38px;
  border-radius: 12px;
  border: none;
  background: linear-gradient(135deg, #6366f1, #7c3aed);
  color: #fff;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 0.12s ease, opacity 0.15s ease;
}

.send-btn:hover:not(:disabled) {
  transform: scale(1.04);
}

.send-btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.stop-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 13px;
  border-radius: 11px;
  border: 1px solid rgba(239, 68, 68, 0.4);
  background: rgba(239, 68, 68, 0.15);
  color: #fca5a5;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
}

.stop-btn:hover {
  background: rgba(239, 68, 68, 0.25);
}

.composer-footnote {
  text-align: center;
  font-size: 0.72rem;
  color: var(--text-muted);
  margin-top: 8px;
}

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}

.modal-overlay[hidden] {
  display: none;
}

.modal-card {
  width: 100%;
  max-width: 500px;
  background: var(--bg-elevated);
  border: 1px solid var(--border-strong);
  border-radius: 16px;
  box-shadow: var(--shadow);
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  margin: 0;
  font-size: 1rem;
}

.modal-body {
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.field label {
  font-size: 0.85rem;
}

.field small {
  color: var(--text-muted);
  font-size: 0.75rem;
}

.field input[type="range"] {
  accent-color: var(--accent);
}

.field textarea {
  border-radius: 10px;
  border: 1px solid var(--border-strong);
  background: var(--bg-surface);
  color: var(--text);
  padding: 10px;
  font: inherit;
  font-size: 0.86rem;
  resize: vertical;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
  padding: 12px 18px;
  border-top: 1px solid var(--border);
}

/* Responsive */
.mobile-only {
  display: none;
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.55);
  z-index: 25;
}

@media (max-width: 820px) {
  .mobile-only {
    display: inline-flex;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    transform: translateX(-100%);
    transition: transform 0.2s ease;
    box-shadow: var(--shadow);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .suggestions-grid {
    grid-template-columns: 1fr;
  }

  .msg-body {
    max-width: 94%;
  }
}
