* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --sidebar-bg: #171717;
  --sidebar-hover: #2a2a2a;
  --sidebar-active: #343434;
  --sidebar-text: #ececec;
  --sidebar-muted: #8e8e8e;
  --sidebar-width: 260px;

  --bg: #f9f9f9;
  --text: #1a1a1a;
  --text-muted: #6b7280;
  --border: #e5e5e5;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;

  --msg-user-bg: transparent;
  --msg-assistant-bg: transparent;

  --input-bg: #ffffff;
  --input-border: #d1d5db;
  --input-shadow: 0 2px 12px rgba(0,0,0,0.08);

  --source-bg: #f0f4ff;
  --source-border: #dbe4ff;
  --source-text: #1e40af;

  --max-chat-width: 768px;
  --font: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

html, body {
  height: 100%;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.6;
}

/* ===== Login Screen ===== */
.login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background: var(--bg);
}

.login-screen.hidden {
  display: none;
}

.login-card {
  text-align: center;
  padding: 48px 40px;
  background: white;
  border-radius: 16px;
  box-shadow: 0 4px 24px rgba(0,0,0,0.08);
  max-width: 400px;
  width: 90%;
}

.login-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.login-card h1 {
  font-size: 22px;
  font-weight: 600;
  margin-bottom: 6px;
  color: var(--text);
}

.login-card p {
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 28px;
}

.login-input-group {
  display: flex;
  gap: 8px;
}

.login-input-group input {
  flex: 1;
  padding: 10px 14px;
  border: 1px solid var(--input-border);
  border-radius: 10px;
  font-size: 15px;
  font-family: var(--font);
  outline: none;
  transition: border-color 0.2s;
}

.login-input-group input:focus {
  border-color: var(--accent);
}

.login-input-group button {
  padding: 10px 20px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: 10px;
  font-size: 14px;
  font-family: var(--font);
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
  white-space: nowrap;
}

.login-input-group button:hover {
  background: var(--accent-hover);
}

.login-input-group button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.login-error {
  color: #dc2626;
  font-size: 13px;
  margin-top: 12px;
  min-height: 20px;
}

/* ===== Layout ===== */
.app {
  display: flex;
  height: 100vh;
  overflow: hidden;
}

/* ===== Sidebar ===== */
.sidebar {
  width: var(--sidebar-width);
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  overflow: hidden;
}

.sidebar-header {
  padding: 12px;
}

.new-chat-btn {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: transparent;
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 8px;
  color: var(--sidebar-text);
  font-size: 14px;
  font-family: var(--font);
  cursor: pointer;
  transition: background 0.15s;
}

.new-chat-btn:hover {
  background: var(--sidebar-hover);
}

.sidebar-list {
  flex: 1;
  overflow-y: auto;
  padding: 4px 8px;
}

.sidebar-list::-webkit-scrollbar {
  width: 4px;
}

.sidebar-list::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.15);
  border-radius: 4px;
}

.conv-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-radius: 8px;
  color: var(--sidebar-text);
  font-size: 13px;
  cursor: pointer;
  transition: background 0.15s;
  margin-bottom: 2px;
  border: none;
  background: transparent;
  width: 100%;
  text-align: left;
  font-family: var(--font);
}

.conv-item:hover {
  background: var(--sidebar-hover);
}

.conv-item.active {
  background: var(--sidebar-active);
}

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

.conv-delete {
  opacity: 0;
  background: none;
  border: none;
  color: var(--sidebar-muted);
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 4px;
  font-size: 14px;
  transition: opacity 0.15s, color 0.15s;
  flex-shrink: 0;
}

.conv-item:hover .conv-delete {
  opacity: 1;
}

.conv-delete:hover {
  color: #ef4444;
}

.sidebar-footer {
  padding: 14px 16px;
  border-top: 1px solid rgba(255,255,255,0.08);
}

.sidebar-brand {
  color: var(--sidebar-muted);
  font-size: 12px;
}

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

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}

.topbar-title {
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
}

.menu-btn {
  display: none;
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  padding: 6px;
  border-radius: 6px;
}

.menu-btn:hover {
  background: var(--border);
}

/* ===== Chat Scroll Area ===== */
.chat-scroll {
  flex: 1;
  overflow-y: auto;
}

.chat-container {
  max-width: var(--max-chat-width);
  margin: 0 auto;
  padding: 24px 24px 0;
}

/* ===== Welcome ===== */
.welcome {
  text-align: center;
  padding: 80px 20px 40px;
}

.welcome-icon {
  font-size: 48px;
  margin-bottom: 16px;
}

.welcome h1 {
  font-size: 24px;
  font-weight: 600;
  margin-bottom: 8px;
}

.welcome p {
  color: var(--text-muted);
  font-size: 15px;
  margin-bottom: 32px;
}

.welcome-suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
}

.suggestion {
  padding: 10px 18px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: white;
  font-size: 13px;
  font-family: var(--font);
  color: var(--text);
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s;
}

.suggestion:hover {
  border-color: var(--accent);
  box-shadow: 0 1px 4px rgba(37,99,235,0.1);
}

/* ===== Messages ===== */
.message {
  display: flex;
  gap: 16px;
  padding: 20px 0;
  border-bottom: 1px solid transparent;
  animation: fadeIn 0.2s ease;
}

.message + .message {
  border-top: 1px solid var(--border);
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.msg-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 15px;
  flex-shrink: 0;
  font-weight: 600;
  overflow: hidden;
}

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

.message.user .msg-avatar {
  background: var(--accent);
  color: white;
}

.message.assistant .msg-avatar {
  background: #10b981;
  color: white;
  border-radius: 6px;
}

.msg-content {
  flex: 1;
  min-width: 0;
}

.msg-role {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 4px;
}

.msg-text {
  font-size: 15px;
  line-height: 1.7;
  word-wrap: break-word;
  color: var(--text);
}

/* User messages: preserve whitespace */
.message.user .msg-text {
  white-space: pre-wrap;
}

/* ===== Markdown Styles ===== */
.msg-text h1, .msg-text h2, .msg-text h3, .msg-text h4 {
  margin: 16px 0 8px;
  line-height: 1.3;
}

.msg-text h1 { font-size: 1.3em; }
.msg-text h2 { font-size: 1.15em; }
.msg-text h3 { font-size: 1.05em; }

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

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

.msg-text ul, .msg-text ol {
  margin: 8px 0;
  padding-left: 24px;
}

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

.msg-text strong {
  font-weight: 600;
}

.msg-text blockquote {
  border-left: 3px solid var(--accent);
  margin: 12px 0;
  padding: 8px 16px;
  background: #f0f4ff;
  border-radius: 0 6px 6px 0;
  color: #374151;
}

.msg-text code {
  background: #f3f4f6;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.9em;
  font-family: 'SF Mono', 'Fira Code', monospace;
}

.msg-text pre {
  background: #1e1e1e;
  color: #d4d4d4;
  padding: 14px 16px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 12px 0;
  font-size: 13px;
  line-height: 1.5;
}

.msg-text pre code {
  background: none;
  padding: 0;
  color: inherit;
  font-size: inherit;
}

.msg-text table {
  border-collapse: collapse;
  margin: 12px 0;
  width: 100%;
  font-size: 14px;
}

.msg-text th, .msg-text td {
  border: 1px solid var(--border);
  padding: 8px 12px;
  text-align: left;
}

.msg-text th {
  background: #f3f4f6;
  font-weight: 600;
}

.msg-text hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 16px 0;
}

/* ===== Sources Accordion ===== */
.sources-toggle {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-top: 12px;
  padding: 6px 12px;
  background: var(--source-bg);
  border: 1px solid var(--source-border);
  border-radius: 8px;
  font-size: 12px;
  font-family: var(--font);
  color: var(--source-text);
  cursor: pointer;
  transition: background 0.15s;
}

.sources-toggle:hover {
  background: #dbe4ff;
}

.sources-toggle svg {
  transition: transform 0.2s;
}

.sources-toggle.open svg {
  transform: rotate(180deg);
}

.sources-panel {
  display: none;
  margin-top: 8px;
  padding: 12px;
  background: var(--source-bg);
  border: 1px solid var(--source-border);
  border-radius: 8px;
  font-size: 13px;
}

.sources-panel.open {
  display: block;
}

.source-item {
  padding: 6px 0;
  border-bottom: 1px solid var(--source-border);
}

.source-item:last-child {
  border-bottom: none;
}

.source-name {
  font-weight: 500;
  color: var(--source-text);
}

.source-preview {
  font-size: 12px;
  color: var(--text-muted);
  margin-top: 2px;
  font-style: italic;
}

/* ===== Loading ===== */
.loading-dots {
  display: flex;
  gap: 5px;
  padding: 4px 0;
}

.loading-dots span {
  width: 8px;
  height: 8px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: pulse 1.2s infinite;
}

.loading-dots span:nth-child(2) { animation-delay: 0.2s; }
.loading-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes pulse {
  0%, 60%, 100% { opacity: 0.3; transform: scale(0.8); }
  30% { opacity: 1; transform: scale(1); }
}

/* ===== Input Area ===== */
.input-wrapper {
  padding: 12px 24px 24px;
  background: var(--bg);
  flex-shrink: 0;
}

.input-form {
  max-width: var(--max-chat-width);
  margin: 0 auto;
}

.input-box {
  display: flex;
  align-items: flex-end;
  gap: 0;
  background: var(--input-bg);
  border: 1px solid var(--input-border);
  border-radius: 16px;
  box-shadow: var(--input-shadow);
  padding: 8px 8px 8px 16px;
  transition: border-color 0.2s, box-shadow 0.2s;
}

.input-box:focus-within {
  border-color: var(--accent);
  box-shadow: 0 2px 16px rgba(37,99,235,0.12);
}

.input-box textarea {
  flex: 1;
  border: none;
  outline: none;
  resize: none;
  font-size: 15px;
  font-family: var(--font);
  line-height: 1.5;
  max-height: 150px;
  padding: 6px 0;
  background: transparent;
  color: var(--text);
}

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

.input-box button {
  width: 36px;
  height: 36px;
  border: none;
  background: var(--accent);
  color: white;
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background 0.15s, opacity 0.15s;
}

.input-box button:hover {
  background: var(--accent-hover);
}

.input-box button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ===== Error ===== */
.error-text {
  color: #dc2626;
  background: #fef2f2;
  border: 1px solid #fecaca;
  padding: 10px 14px;
  border-radius: 8px;
  font-size: 14px;
  margin-top: 4px;
}

/* ===== Mobile Overlay ===== */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 40;
}

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

/* ===== Responsive ===== */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    z-index: 50;
    transform: translateX(-100%);
    transition: transform 0.25s ease;
  }

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

  .menu-btn {
    display: flex;
  }

  .chat-container {
    padding: 16px 16px 0;
  }

  .input-wrapper {
    padding: 8px 12px 16px;
  }

  .welcome {
    padding: 40px 16px 24px;
  }

  .welcome-suggestions {
    flex-direction: column;
    align-items: center;
  }
}

/* ===== Love Message Popup ===== */
.love-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
  opacity: 0;
  transition: opacity 0.4s ease;
  backdrop-filter: blur(4px);
}

.love-overlay.visible {
  opacity: 1;
}

.love-card {
  background: linear-gradient(135deg, #fff5f5, #ffe0e6);
  border: 2px solid #f9a8b8;
  border-radius: 20px;
  padding: 40px 36px 32px;
  max-width: 420px;
  width: 90%;
  text-align: center;
  box-shadow: 0 20px 60px rgba(200, 50, 80, 0.25);
  transform: scale(0.8) translateY(30px);
  transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.love-overlay.visible .love-card {
  transform: scale(1) translateY(0);
}

.love-heart {
  font-size: 56px;
  color: #e74c6f;
  margin-bottom: 12px;
  animation: heartPulse 1.2s infinite;
}

@keyframes heartPulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.15); }
}

.love-sender {
  font-size: 14px;
  font-weight: 600;
  color: #c0435e;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  margin-bottom: 16px;
}

.love-text {
  font-size: 18px;
  line-height: 1.7;
  color: #4a2030;
  margin-bottom: 28px;
  font-style: italic;
  white-space: pre-wrap;
}

.love-close {
  padding: 12px 28px;
  background: linear-gradient(135deg, #e74c6f, #c0435e);
  color: white;
  border: none;
  border-radius: 12px;
  font-size: 15px;
  font-family: var(--font);
  font-weight: 500;
  cursor: pointer;
  transition: transform 0.15s, box-shadow 0.15s;
}

.love-close:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 16px rgba(231, 76, 111, 0.4);
}
