:root {
  --bg: #0b0c10;
  --sidebar-bg: #0f1017;
  --surface: #171a26;
  --surface-2: #1e2233;
  --border: #262a3a;
  --text: #f2f3f7;
  --text-muted: #9195a8;

  --accent-1: #7c5cff;
  --accent-2: #4f9dff;
  --accent-gradient: linear-gradient(135deg, var(--accent-1), var(--accent-2));

  --user-bubble: linear-gradient(135deg, #7c5cff, #5a7bff);
  --ai-bubble: #171a26;

  --error: #f87171;

  --radius-sm: 10px;
  --radius-md: 16px;
  --radius-lg: 22px;

  --shadow-soft: 0 4px 24px rgba(0, 0, 0, 0.35);
  --shadow-glow: 0 0 30px rgba(124, 92, 255, 0.15);

  --sidebar-width: 264px;
}

[data-theme="light"] {
  --bg: #f6f7fb;
  --sidebar-bg: #ffffff;
  --surface: #ffffff;
  --surface-2: #eef0f6;
  --border: #e2e5ee;
  --text: #191b23;
  --text-muted: #6b7280;
  --ai-bubble: #ffffff;
  --shadow-soft: 0 2px 12px rgba(20, 20, 40, 0.06);
  --shadow-glow: 0 0 24px rgba(124, 92, 255, 0.12);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; padding: 0; }

body {
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

.bg-glow {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background:
    radial-gradient(600px circle at 15% -10%, rgba(124, 92, 255, 0.14), transparent 60%),
    radial-gradient(500px circle at 90% 10%, rgba(79, 157, 255, 0.10), transparent 60%);
}

.app-shell {
  position: relative;
  z-index: 1;
  display: flex;
  height: 100vh;
}

/* ===================== Sidebar ===================== */
.sidebar {
  width: var(--sidebar-width);
  flex-shrink: 0;
  background: var(--sidebar-bg);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 16px 12px;
  gap: 14px;
  z-index: 30;
  transition: transform 0.25s ease;
}

.sidebar-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 4px 0;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 800;
  font-size: 1.15rem;
  letter-spacing: -0.02em;
}

.brand-logo {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border-radius: 9px;
  background: var(--accent-gradient);
  color: #fff;
  font-size: 0.9rem;
  box-shadow: var(--shadow-glow);
}

.sidebar-close { display: none; }

.new-chat-btn {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--accent-gradient);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-family: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 4px 16px rgba(124, 92, 255, 0.3);
  transition: opacity 0.15s ease, transform 0.1s ease;
}
.new-chat-btn:hover { opacity: 0.92; }
.new-chat-btn:active { transform: scale(0.98); }
.new-chat-btn span { font-size: 1.05rem; line-height: 1; }

.sidebar-search input {
  width: 100%;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  color: var(--text);
  font-family: inherit;
  font-size: 0.85rem;
  outline: none;
}
.sidebar-search input::placeholder { color: var(--text-muted); }
.sidebar-search input:focus { border-color: var(--accent-1); }

.sidebar-section-label {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
  padding: 4px 8px 0;
}

.conversation-list {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding-right: 2px;
}
.conversation-list::-webkit-scrollbar { width: 5px; }
.conversation-list::-webkit-scrollbar-thumb { background: var(--surface-2); border-radius: 8px; }

.conversation-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  color: var(--text-muted);
  font-size: 0.87rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: background 0.15s ease, color 0.15s ease;
}
.conversation-item:hover { background: var(--surface); color: var(--text); }
.conversation-item.active {
  background: var(--surface-2);
  color: var(--text);
  font-weight: 600;
}
.conversation-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.conversation-delete {
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.85rem;
  flex-shrink: 0;
  padding: 2px 4px;
  border-radius: 6px;
}
.conversation-item:hover .conversation-delete { opacity: 1; }
.conversation-delete:hover { color: var(--error); background: rgba(248,113,113,0.1); }

.conversation-empty {
  color: var(--text-muted);
  font-size: 0.85rem;
  padding: 10px;
  text-align: center;
}

.sidebar-bottom {
  border-top: 1px solid var(--border);
  padding-top: 12px;
}

.auth-buttons-sidebar {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.user-button-container {
  display: none;
  align-items: center;
  padding: 4px;
}

/* ===================== Buttons ===================== */
.btn {
  border: none;
  border-radius: var(--radius-sm);
  padding: 9px 16px;
  font-family: inherit;
  font-size: 0.87rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform 0.1s ease, opacity 0.15s ease;
}
.btn:active { transform: scale(0.97); }
.btn-block { width: 100%; }

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

.btn-primary {
  background: var(--accent-gradient);
  color: #fff;
  box-shadow: 0 4px 16px rgba(124, 92, 255, 0.3);
}
.btn-primary:hover { opacity: 0.92; }

.link-btn {
  background: none;
  border: none;
  padding: 0;
  color: var(--accent-2);
  font-weight: 600;
  font-family: inherit;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.icon-btn {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.1s ease;
  flex-shrink: 0;
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn:active { transform: scale(0.94); }

.sidebar-open { display: none; }
.sidebar-overlay { display: none; }

/* ===================== Main panel ===================== */
.main-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.app-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 22px;
  background: rgba(19, 21, 32, 0.55);
  backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--border);
}
[data-theme="light"] .app-header { background: rgba(255,255,255,0.7); }

.header-title {
  flex: 1;
  font-weight: 700;
  font-size: 1rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-container {
  flex: 1;
  width: 100%;
  max-width: 820px;
  margin: 0 auto;
  padding: 18px 20px 10px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-window {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 8px 4px 24px;
  overflow-y: auto;
  min-height: 0;
}
.chat-window::-webkit-scrollbar { width: 6px; }
.chat-window::-webkit-scrollbar-thumb { background: var(--surface-2); border-radius: 10px; }

.message { display: flex; gap: 12px; align-items: flex-start; animation: fadeIn 0.25s ease; }
.message-user { flex-direction: row-reverse; }
@keyframes fadeIn { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }

.avatar {
  flex-shrink: 0;
  width: 34px; height: 34px;
  border-radius: 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 0.8rem; font-weight: 700; color: #fff;
}
.avatar-ai { background: var(--accent-gradient); box-shadow: var(--shadow-glow); }
.avatar-user { background: linear-gradient(135deg, #34d399, #10b981); }

.bubble {
  background: var(--ai-bubble);
  border: 1px solid var(--border);
  padding: 14px 18px;
  border-radius: var(--radius-md);
  line-height: 1.6;
  max-width: 75%;
  white-space: pre-wrap;
  word-wrap: break-word;
  box-shadow: var(--shadow-soft);
}
.message-user .bubble {
  background: var(--user-bubble);
  color: #fff;
  border: none;
  box-shadow: 0 6px 20px rgba(124, 92, 255, 0.3);
}
.bubble.error-bubble {
  background: rgba(248, 113, 113, 0.1);
  color: var(--error);
  border-color: rgba(248, 113, 113, 0.3);
}

.typing-dots { display: inline-flex; gap: 4px; align-items: center; margin-left: 6px; }
.typing-dots span { width: 6px; height: 6px; border-radius: 50%; background: var(--text-muted); animation: blink 1.2s infinite ease-in-out; }
.typing-dots span:nth-child(2) { animation-delay: 0.2s; }
.typing-dots span:nth-child(3) { animation-delay: 0.4s; }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

.quick-prompts { display: flex; flex-wrap: wrap; gap: 8px; padding: 4px 4px 14px; }
.quick-btn {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 9px 16px;
  border-radius: 999px;
  font-size: 0.85rem;
  font-family: inherit;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}
.quick-btn:hover { background: var(--surface-2); border-color: var(--accent-1); }
.quick-btn:active { transform: scale(0.97); }
.quick-btn:disabled { opacity: 0.4; cursor: not-allowed; }

.chat-input-bar {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 10px 10px 10px 18px;
  box-shadow: var(--shadow-soft);
  transition: border-color 0.15s ease;
}
.chat-input-bar:focus-within { border-color: var(--accent-1); }

#chat-input {
  flex: 1; resize: none; border: none; outline: none;
  background: transparent; color: var(--text);
  font-size: 1rem; font-family: inherit; max-height: 140px; padding: 8px 0;
}
#chat-input::placeholder { color: var(--text-muted); }
#chat-input:disabled { cursor: not-allowed; }

.send-btn {
  background: var(--accent-gradient);
  color: #fff; border: none; border-radius: 12px;
  width: 42px; height: 42px;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer; flex-shrink: 0;
  transition: opacity 0.15s ease, transform 0.1s ease;
}
.send-btn:hover:not(:disabled) { opacity: 0.9; }
.send-btn:active:not(:disabled) { transform: scale(0.94); }
.send-btn:disabled { opacity: 0.35; cursor: not-allowed; }

.auth-lock-banner {
  display: none;
  text-align: center;
  font-size: 0.85rem;
  color: var(--text-muted);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  margin-top: 10px;
}
.auth-lock-banner.visible { display: block; }

.disclaimer { text-align: center; font-size: 0.75rem; color: var(--text-muted); margin: 12px 0 4px; }

.app-footer {
  text-align: center;
  font-size: 0.78rem;
  color: var(--text-muted);
  padding: 12px 20px;
  border-top: 1px solid var(--border);
}
.footer-dot { margin: 0 8px; }

/* ===================== Responsive (mobile sidebar becomes overlay) ===================== */
@media (max-width: 860px) {
  .sidebar {
    position: fixed;
    top: 0; left: 0; bottom: 0;
    transform: translateX(-100%);
    box-shadow: 0 0 40px rgba(0,0,0,0.4);
  }
  .sidebar.open { transform: translateX(0); }
  .sidebar-close { display: flex; }
  .sidebar-open { display: flex; }
  .sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.5);
    z-index: 25;
  }
  .sidebar-overlay.visible { display: block; }
  .chat-container { padding: 14px 14px 6px; }
  .bubble { max-width: 85%; }
}
