:root {
  --bg: #0f0e17;
  --bg2: #1a1825;
  --surface: #22203a;
  --surface2: #2d2b45;
  --border: #3a3860;
  --accent: #a78bfa;
  --accent2: #7c3aed;
  --gold: #f59e0b;
  --gold-light: #fcd34d;
  --text: #e8e6f0;
  --text-muted: #8b89a8;
  --scripture: #34d399;
  --commentary: #60a5fa;
  --guide: #f472b6;
  --danger: #f87171;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 4px 24px rgba(0,0,0,0.4);
}

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

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }

button {
  cursor: pointer;
  border: none;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-family: inherit;
  transition: all 0.15s;
}

input, textarea, select {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: inherit;
  font-size: 0.95rem;
  padding: 10px 14px;
  width: 100%;
  outline: none;
  transition: border 0.15s;
}

input:focus, textarea:focus, select:focus {
  border-color: var(--accent);
}

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

/* ── Sidebar ── */
.sidebar {
  width: 280px;
  min-width: 280px;
  background: var(--bg2);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.sidebar-logo {
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--border);
}

.sidebar-logo h1 {
  font-size: 1.3rem;
  color: var(--gold);
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-logo .subtitle {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 2px;
}

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

.btn-new-chat {
  width: 100%;
  background: var(--accent2);
  color: #fff;
  padding: 10px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
}

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

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

.conversations-list::-webkit-scrollbar { width: 4px; }
.conversations-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.conv-item {
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  margin-bottom: 2px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  transition: background 0.1s;
}

.conv-item:hover { background: var(--surface); }
.conv-item.active { background: var(--surface2); border-left: 3px solid var(--accent); }

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

.conv-delete {
  background: none;
  color: var(--text-muted);
  padding: 2px 4px;
  font-size: 0.75rem;
  opacity: 0;
}

.conv-item:hover .conv-delete { opacity: 1; }
.conv-delete:hover { color: var(--danger); }

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
  font-size: 0.82rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.btn-logout {
  background: none;
  color: var(--text-muted);
  padding: 4px 8px;
  font-size: 0.8rem;
}
.btn-logout:hover { color: var(--danger); }

/* ── Main Chat ── */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.chat-header {
  padding: 16px 24px;
  border-bottom: 1px solid var(--border);
  background: var(--bg2);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.chat-header h2 {
  font-size: 1rem;
  color: var(--text-muted);
  font-weight: 400;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.chat-messages::-webkit-scrollbar { width: 4px; }
.chat-messages::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ── Welcome Screen ── */
.welcome-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  gap: 16px;
  text-align: center;
  padding: 40px;
}

.welcome-icon { font-size: 4rem; }

.welcome-screen h2 {
  color: var(--gold);
  font-size: 1.6rem;
}

.welcome-screen p {
  color: var(--text-muted);
  max-width: 480px;
  line-height: 1.6;
}

.suggestion-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 8px;
}

.chip {
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 8px 14px;
  border-radius: 20px;
  font-size: 0.85rem;
  cursor: pointer;
  transition: all 0.15s;
}

.chip:hover {
  background: var(--surface2);
  border-color: var(--accent);
  color: var(--accent);
}

/* ── Messages ── */
.message {
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
}

.message.user { align-self: flex-end; max-width: 600px; }

.message-bubble {
  padding: 14px 18px;
  border-radius: var(--radius);
  line-height: 1.65;
}

.message.user .message-bubble {
  background: var(--accent2);
  color: #fff;
  border-radius: var(--radius) var(--radius) 4px var(--radius);
}

.message.assistant .message-bubble {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px var(--radius) var(--radius) var(--radius);
}

.message.assistant .message-bubble h1,
.message.assistant .message-bubble h2,
.message.assistant .message-bubble h3 {
  color: var(--gold);
  margin: 12px 0 6px;
}

.message.assistant .message-bubble p { margin-bottom: 10px; }

.message.assistant .message-bubble ul,
.message.assistant .message-bubble ol {
  padding-left: 20px;
  margin-bottom: 10px;
}

.message.assistant .message-bubble li { margin-bottom: 4px; }

.message.assistant .message-bubble strong { color: var(--gold-light); }

.message.assistant .message-bubble em { color: var(--text-muted); }

.message.assistant .message-bubble code {
  background: var(--bg);
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 0.85em;
}

/* ── Sources ── */
.sources-section {
  margin-top: 12px;
  border-top: 1px solid var(--border);
  padding-top: 10px;
}

.sources-title {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 8px;
}

.source-badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 0.75rem;
  margin: 2px 4px 2px 0;
  border: 1px solid transparent;
}

.source-badge.scripture {
  background: rgba(52,211,153,0.1);
  border-color: rgba(52,211,153,0.3);
  color: var(--scripture);
}

.source-badge.commentary {
  background: rgba(96,165,250,0.1);
  border-color: rgba(96,165,250,0.3);
  color: var(--commentary);
}

.source-badge.admin {
  background: rgba(244,114,182,0.1);
  border-color: rgba(244,114,182,0.3);
  color: var(--guide);
}

/* ── Loading ── */
.message.loading .message-bubble {
  display: flex;
  gap: 6px;
  align-items: center;
}

.dot {
  width: 8px; height: 8px;
  border-radius: 50%;
  background: var(--accent);
  animation: bounce 1.2s infinite;
}
.dot:nth-child(2) { animation-delay: 0.2s; }
.dot:nth-child(3) { animation-delay: 0.4s; }

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

/* ── Input Area ── */
.chat-input-area {
  padding: 16px 24px 20px;
  border-top: 1px solid var(--border);
  background: var(--bg2);
}

.input-wrapper {
  display: flex;
  gap: 10px;
  align-items: flex-end;
  max-width: 800px;
  margin: 0 auto;
}

.input-wrapper textarea {
  resize: none;
  min-height: 48px;
  max-height: 160px;
  overflow-y: auto;
  flex: 1;
}

.btn-send {
  background: var(--accent2);
  color: #fff;
  padding: 12px 18px;
  font-size: 1.1rem;
  height: 48px;
  white-space: nowrap;
}

.btn-send:hover:not(:disabled) { background: var(--accent); }
.btn-send:disabled { opacity: 0.4; cursor: not-allowed; }

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

/* ── Auth Pages ── */
.auth-page {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 20px;
}

.auth-card {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 40px;
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow);
}

.auth-logo {
  text-align: center;
  margin-bottom: 28px;
}

.auth-logo h1 {
  color: var(--gold);
  font-size: 1.8rem;
  margin-bottom: 4px;
}

.auth-logo p { color: var(--text-muted); font-size: 0.9rem; }

.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-bottom: 6px;
}

.btn-primary {
  width: 100%;
  background: var(--accent2);
  color: #fff;
  padding: 12px;
  font-size: 1rem;
  font-weight: 600;
  margin-top: 8px;
}

.btn-primary:hover { background: var(--accent); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }

.auth-switch {
  text-align: center;
  margin-top: 20px;
  font-size: 0.88rem;
  color: var(--text-muted);
}

.error-msg {
  background: rgba(248,113,113,0.1);
  border: 1px solid rgba(248,113,113,0.3);
  color: var(--danger);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 0.88rem;
  margin-bottom: 16px;
  display: none;
}

/* ── Admin Portal ── */
.admin-layout {
  display: flex;
  min-height: 100vh;
}

.admin-sidebar {
  width: 240px;
  background: var(--bg2);
  border-right: 1px solid var(--border);
  padding: 24px 0;
}

.admin-sidebar h2 {
  color: var(--gold);
  padding: 0 20px 20px;
  border-bottom: 1px solid var(--border);
  font-size: 1.1rem;
}

.admin-nav { list-style: none; padding: 12px 0; }

.admin-nav li a {
  display: block;
  padding: 10px 20px;
  color: var(--text);
  font-size: 0.9rem;
  transition: background 0.1s;
}

.admin-nav li a:hover,
.admin-nav li a.active {
  background: var(--surface);
  color: var(--accent);
  text-decoration: none;
}

.admin-content {
  flex: 1;
  padding: 32px;
  overflow-y: auto;
}

.admin-content h1 { color: var(--gold); margin-bottom: 24px; font-size: 1.4rem; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  gap: 16px;
  margin-bottom: 32px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  text-align: center;
}

.stat-card .stat-num {
  font-size: 2rem;
  font-weight: 700;
  color: var(--gold);
}

.stat-card .stat-label {
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-top: 4px;
}

.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}

.data-table th {
  text-align: left;
  padding: 10px 12px;
  background: var(--surface);
  color: var(--text-muted);
  font-weight: 600;
  border-bottom: 1px solid var(--border);
}

.data-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}

.data-table tr:hover td { background: var(--surface); }

.btn-sm {
  padding: 4px 10px;
  font-size: 0.78rem;
  border-radius: 6px;
}

.btn-edit { background: var(--surface2); color: var(--text); border: 1px solid var(--border); }
.btn-edit:hover { border-color: var(--accent); color: var(--accent); }
.btn-danger { background: rgba(248,113,113,0.15); color: var(--danger); border: 1px solid rgba(248,113,113,0.3); }
.btn-danger:hover { background: var(--danger); color: #fff; }
.btn-success { background: rgba(52,211,153,0.15); color: var(--scripture); border: 1px solid rgba(52,211,153,0.3); }
.btn-success:hover { background: var(--scripture); color: #000; }

.toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  gap: 12px;
}

.toolbar h2 { font-size: 1.1rem; }

.btn-add {
  background: var(--accent2);
  color: #fff;
  padding: 8px 16px;
  font-weight: 600;
}
.btn-add:hover { background: var(--accent); }

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}

.modal {
  background: var(--bg2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  width: 100%;
  max-width: 640px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: var(--shadow);
}

.modal h2 { color: var(--gold); margin-bottom: 20px; }

.modal-actions {
  display: flex;
  gap: 10px;
  justify-content: flex-end;
  margin-top: 20px;
}

.btn-cancel { background: var(--surface2); color: var(--text); padding: 10px 18px; }
.btn-save { background: var(--accent2); color: #fff; padding: 10px 18px; font-weight: 600; }
.btn-save:hover { background: var(--accent); }

/* ── Responsive ── */
@media (max-width: 768px) {
  .sidebar { width: 0; min-width: 0; display: none; }
  .sidebar.open { display: flex; width: 280px; position: fixed; left: 0; top: 0; bottom: 0; z-index: 50; }
  .mobile-menu-btn { display: flex; }
  .admin-sidebar { display: none; }
}

.mobile-menu-btn {
  display: none;
  background: none;
  color: var(--text);
  font-size: 1.3rem;
  padding: 6px;
}

.hidden { display: none !important; }
