/* Reset & Base */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-900: #111827;
  --bg-800: #1f2937;
  --bg-700: #374151;
  --bg-600: #4b5563;
  --text-primary: #f9fafb;
  --text-secondary: #9ca3af;
  --text-muted: #6b7280;
  --blue-600: #2563eb;
  --blue-700: #1d4ed8;
  --blue-500: #3b82f6;
  --red-500: #ef4444;
  --red-600: #dc2626;
  --green-500: #22c55e;
  --border: #374151;
  --sidebar-width: 280px;
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg-900);
  color: var(--text-primary);
  line-height: 1.5;
}

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

button {
  cursor: pointer;
  border: none;
  font-family: inherit;
  font-size: 0.875rem;
}

input, textarea, select {
  font-family: inherit;
  font-size: 16px; /* Prevents iOS Safari auto-zoom on focus */
}

/* Login Page */
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  padding: 1rem;
}

.login-card {
  background: var(--bg-800);
  border-radius: 12px;
  padding: 2.5rem;
  width: 100%;
  max-width: 400px;
  border: 1px solid var(--border);
}

.login-card h1 {
  text-align: center;
  margin-bottom: 0.5rem;
  font-size: 1.5rem;
}

.login-card .subtitle {
  text-align: center;
  color: var(--text-secondary);
  margin-bottom: 2rem;
  font-size: 0.875rem;
}

.login-card .logo {
  text-align: center;
  font-size: 3rem;
  margin-bottom: 1rem;
}

.form-group {
  margin-bottom: 1rem;
}

.form-group label {
  display: block;
  margin-bottom: 0.375rem;
  font-size: 0.875rem;
  color: var(--text-secondary);
}

.form-group input {
  width: 100%;
  padding: 0.625rem 0.75rem;
  background: var(--bg-700);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text-primary);
  outline: none;
  transition: border-color 0.2s;
}

.form-group input:focus {
  border-color: var(--blue-600);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.625rem 1.25rem;
  border-radius: 8px;
  font-weight: 500;
  transition: background-color 0.2s, opacity 0.2s;
}

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

.btn-primary {
  background: var(--blue-600);
  color: white;
}

.btn-primary:hover:not(:disabled) {
  background: var(--blue-700);
}

.btn-danger {
  background: var(--red-500);
  color: white;
}

.btn-danger:hover:not(:disabled) {
  background: var(--red-600);
}

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  padding: 0.375rem 0.625rem;
}

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

.btn-full { width: 100%; }

.cf-turnstile { margin: 0.75rem 0; display: flex; justify-content: center; }

.error-msg {
  color: var(--red-500);
  font-size: 0.8125rem;
  margin-top: 0.75rem;
  text-align: center;
}

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

/* Sidebar */
.sidebar {
  width: var(--sidebar-width);
  background: var(--bg-800);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  transition: transform 0.3s ease;
}

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

.sidebar-header h2 {
  font-size: 1.125rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.sidebar-actions {
  display: flex;
  gap: 0.25rem;
}

.new-chat-btn {
  background: var(--blue-600);
  color: white;
  border-radius: 8px;
  padding: 0.5rem 0.875rem;
  font-size: 0.8125rem;
  font-weight: 500;
}

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

.conversations-list {
  flex: 1;
  overflow-y: auto;
  padding: 0.5rem;
}

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

.conv-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.625rem 0.75rem;
  border-radius: 8px;
  cursor: pointer;
  font-size: 0.8125rem;
  color: var(--text-secondary);
  transition: background 0.15s;
  margin-bottom: 2px;
}

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

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

.conv-item .conv-delete {
  opacity: 0;
  color: var(--text-muted);
  padding: 0.125rem 0.375rem;
  border-radius: 4px;
  font-size: 0.75rem;
  background: none;
  transition: opacity 0.15s, color 0.15s;
}

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

.sidebar-footer {
  padding: 0.75rem 1rem;
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.8125rem;
}

.sidebar-footer .user-info {
  color: var(--text-secondary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.sidebar-footer .footer-actions {
  display: flex;
  gap: 0.5rem;
  align-items: center;
}

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

.admin-link:hover { color: var(--text-primary); }

.logout-btn {
  background: none;
  color: var(--text-muted);
  font-size: 0.75rem;
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
}

.logout-btn:hover { color: var(--red-500); }

/* Main Chat Area */
.chat-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.chat-header {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.menu-toggle {
  display: none;
  background: none;
  color: var(--text-secondary);
  font-size: 1.25rem;
  padding: 0.25rem;
}

.chat-header h3 {
  font-size: 0.9375rem;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
}

.messages-container::-webkit-scrollbar { width: 6px; }
.messages-container::-webkit-scrollbar-thumb { background: var(--bg-600); border-radius: 4px; }

.welcome-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  text-align: center;
  padding: 2rem;
}

.welcome-state .welcome-logo { font-size: 4rem; margin-bottom: 1rem; }
.welcome-state h2 { font-size: 1.5rem; color: var(--text-primary); margin-bottom: 0.5rem; }
.welcome-state p { max-width: 400px; line-height: 1.6; }

.message {
  display: flex;
  gap: 0.75rem;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
}

.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.875rem;
  flex-shrink: 0;
  margin-top: 2px;
}

.message.user .message-avatar {
  background: var(--blue-600);
}

.message.assistant .message-avatar {
  background: var(--bg-700);
}

.message-content {
  flex: 1;
  font-size: 0.9375rem;
  line-height: 1.7;
  color: var(--text-primary);
  min-width: 0;
}

.message-content p { margin-bottom: 0.75rem; }
.message-content p:last-child { margin-bottom: 0; }
.message-content strong { font-weight: 600; }
.message-content em { font-style: italic; }

.message-content ul, .message-content ol {
  padding-left: 1.5rem;
  margin-bottom: 0.75rem;
}

.message-content li { margin-bottom: 0.25rem; }

.message-content code {
  background: var(--bg-700);
  padding: 0.125rem 0.375rem;
  border-radius: 4px;
  font-size: 0.8125rem;
}

.typing-indicator {
  display: inline-flex;
  gap: 4px;
  padding: 4px 0;
}

.typing-indicator span {
  width: 6px;
  height: 6px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: typing 1.4s infinite;
}

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

@keyframes typing {
  0%, 60%, 100% { transform: translateY(0); opacity: 0.4; }
  30% { transform: translateY(-4px); opacity: 1; }
}

/* Input Area */
.chat-input-area {
  padding: 1rem 1.5rem 1.25rem;
  border-top: 1px solid var(--border);
}

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

.chat-input-wrapper textarea {
  flex: 1;
  padding: 0.75rem 1rem;
  background: var(--bg-800);
  border: 1px solid var(--border);
  border-radius: 12px;
  color: var(--text-primary);
  resize: none;
  outline: none;
  min-height: 44px;
  max-height: 200px;
  line-height: 1.5;
  transition: border-color 0.2s;
}

.chat-input-wrapper textarea:focus {
  border-color: var(--blue-600);
}

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

.send-btn {
  background: var(--blue-600);
  color: white;
  border-radius: 10px;
  width: 44px;
  height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.125rem;
  flex-shrink: 0;
}

.send-btn:hover:not(:disabled) { background: var(--blue-700); }
.send-btn:disabled { opacity: 0.4; }

/* Admin Page */
.admin-container {
  max-width: 960px;
  margin: 0 auto;
  padding: 2rem 1.5rem;
}

.admin-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 2rem;
}

.admin-header h1 {
  font-size: 1.5rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.admin-header .back-link { font-size: 0.875rem; }

.admin-section {
  background: var(--bg-800);
  border-radius: 12px;
  border: 1px solid var(--border);
  padding: 1.5rem;
  margin-bottom: 1.5rem;
}

.admin-section h2 {
  font-size: 1.125rem;
  margin-bottom: 1rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--border);
}

.admin-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  margin-bottom: 1rem;
}

.admin-form .form-group.full-width {
  grid-column: 1 / -1;
}

.user-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 0;
  border-bottom: 1px solid var(--border);
}

.user-row:last-child { border-bottom: none; }

.user-row .user-details {
  display: flex;
  flex-direction: column;
}

.user-row .user-email {
  font-size: 0.875rem;
  font-weight: 500;
}

.user-row .user-meta {
  font-size: 0.75rem;
  color: var(--text-muted);
  display: flex;
  gap: 0.75rem;
  margin-top: 0.125rem;
}

.user-row .user-actions {
  display: flex;
  gap: 0.5rem;
}

.user-row .user-actions button {
  font-size: 0.75rem;
  padding: 0.25rem 0.625rem;
  border-radius: 6px;
}

.badge {
  display: inline-block;
  font-size: 0.625rem;
  padding: 0.125rem 0.375rem;
  border-radius: 4px;
  font-weight: 600;
  text-transform: uppercase;
}

.badge-admin { background: var(--blue-600); color: white; }

.conv-view-list {
  list-style: none;
}

.conv-view-item {
  padding: 0.625rem 0;
  border-bottom: 1px solid var(--border);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

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

.conv-view-title {
  font-size: 0.875rem;
  cursor: pointer;
  color: var(--blue-500);
}

.conv-view-title:hover { text-decoration: underline; }

.conv-view-date {
  font-size: 0.75rem;
  color: var(--text-muted);
}

.admin-messages {
  max-height: 500px;
  overflow-y: auto;
  padding: 1rem 0;
}

/* 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: 1rem;
}

.modal {
  background: var(--bg-800);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 1.5rem;
  width: 100%;
  max-width: 440px;
}

.modal h3 {
  margin-bottom: 1rem;
}

.modal .modal-actions {
  display: flex;
  gap: 0.75rem;
  justify-content: flex-end;
  margin-top: 1.25rem;
}

.hidden { display: none !important; }

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

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

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

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

  .menu-toggle { display: block; }

  .admin-form {
    grid-template-columns: 1fr;
  }

  .user-row {
    flex-direction: column;
    align-items: flex-start;
    gap: 0.5rem;
  }
}
