/* ==========================================================================
   Study Buddy - Design System & Stylesheet
   ========================================================================== */

/* Design Tokens */
:root {
  --font-primary: 'Outfit', 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  
  /* Color Palette */
  --bg-main: #0b0f19;
  --bg-card: #151c2c;
  --bg-input: #1e273e;
  --bg-input-focus: #25314d;
  
  --primary: #6366f1;
  --primary-hover: #4f46e5;
  --primary-glow: rgba(99, 102, 241, 0.25);
  
  --accent-cyan: #06b6d4;
  --accent-purple: #a855f7;
  --accent-amber: #f59e0b;
  
  --text-main: #f8fafc;
  --text-muted: #94a3b8;
  --text-dim: #64748b;
  
  --border-light: rgba(255, 255, 255, 0.08);
  --border-active: rgba(99, 102, 241, 0.5);
  
  --user-bubble-bg: linear-gradient(135deg, #6366f1 0%, #4f46e5 100%);
  --ai-bubble-bg: #1e293b;
  
  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;
  --radius-full: 9999px;
  
  --shadow-sm: 0 2px 8px rgba(0, 0, 0, 0.2);
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.3);
  --shadow-glow: 0 0 25px rgba(99, 102, 241, 0.15);
  
  --transition-fast: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
  --transition-smooth: 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

/* Reset & Global */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html, body {
  height: 100%;
  font-family: var(--font-primary);
  background-color: var(--bg-main);
  color: var(--text-main);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}

/* Layout Container */
.app-container {
  display: flex;
  flex-direction: column;
  height: 100vh;
  max-width: 1100px;
  margin: 0 auto;
  position: relative;
  background-color: var(--bg-main);
  border-left: 1px solid var(--border-light);
  border-right: 1px solid var(--border-light);
}

/* API Key Alert Banner */
.api-key-banner {
  background: linear-gradient(90deg, #7c2d12 0%, #991b1b 100%);
  color: #fef2f2;
  padding: 10px 20px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  z-index: 100;
  animation: slideDown 0.3s ease-out;
}

.api-key-banner.hidden {
  display: none;
}

.banner-content {
  display: flex;
  align-items: center;
  gap: 12px;
  max-width: 1000px;
  margin: 0 auto;
  font-size: 0.9rem;
}

.banner-icon {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.banner-text {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.banner-text code {
  background: rgba(0, 0, 0, 0.3);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: monospace;
  font-size: 0.85rem;
}

.btn-api-link {
  background-color: #fff;
  color: #991b1b;
  padding: 6px 14px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 0.85rem;
  text-decoration: none;
  transition: var(--transition-fast);
}

.btn-api-link:hover {
  background-color: #fecaca;
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
  background: rgba(21, 28, 44, 0.85);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border-light);
  z-index: 10;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 14px;
}

.logo-badge {
  width: 44px;
  height: 44px;
  border-radius: 12px;
  background: linear-gradient(135deg, var(--primary) 0%, var(--accent-purple) 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  box-shadow: var(--shadow-glow);
}

.logo-icon {
  width: 24px;
  height: 24px;
}

.header-title h1 {
  font-size: 1.25rem;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
  letter-spacing: -0.01em;
}

.ai-tag {
  font-size: 0.68rem;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: var(--radius-full);
  background: rgba(6, 182, 212, 0.15);
  color: var(--accent-cyan);
  border: 1px solid rgba(6, 182, 212, 0.3);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.status-indicator {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.status-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: #10b981;
  box-shadow: 0 0 10px #10b981;

  animation: pulseDot 2s infinite;
}

/* Secondary Button (New Chat) */
.btn-secondary {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  border-radius: var(--radius-md);
  background: var(--bg-input);
  border: 1px solid var(--border-light);
  color: var(--text-main);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
  transition: var(--transition-fast);
}

.btn-secondary:hover {
  background: var(--bg-input-focus);
  border-color: var(--border-active);
  transform: translateY(-1px);
}

.btn-secondary svg {
  width: 18px;
  height: 18px;
}

/* Main Chat Area */
.chat-viewport {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  position: relative;
}

/* Custom Scrollbar */
.chat-viewport::-webkit-scrollbar {
  width: 6px;
}

.chat-viewport::-webkit-scrollbar-track {
  background: transparent;
}

.chat-viewport::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.15);
  border-radius: var(--radius-full);
}

.chat-viewport::-webkit-scrollbar-thumb:hover {
  background: rgba(255, 255, 255, 0.3);
}

/* Welcome View */
.welcome-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  margin: auto 0;
  padding: 20px;
  max-width: 680px;
  align-self: center;
  animation: fadeIn 0.4s ease-out;
}

.welcome-container.hidden {
  display: none;
}

.avatar-large {
  width: 72px;
  height: 72px;
  border-radius: 20px;
  background: linear-gradient(135deg, rgba(99, 102, 241, 0.2) 0%, rgba(6, 182, 212, 0.2) 100%);
  border: 1px solid var(--border-active);
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--accent-cyan);
  margin-bottom: 20px;
  box-shadow: var(--shadow-glow);
}

.avatar-large svg {
  width: 36px;
  height: 36px;
}

.welcome-container h2 {
  font-size: 1.75rem;
  font-weight: 700;
  margin-bottom: 10px;
  background: linear-gradient(135deg, #ffffff 0%, var(--text-muted) 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.welcome-desc {
  font-size: 1rem;
  color: var(--text-muted);
  line-height: 1.6;
  margin-bottom: 32px;
}

.quick-questions-section {
  width: 100%;
}

.quick-title {
  font-size: 0.85rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  margin-bottom: 14px;
  font-weight: 600;
}

.quick-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 12px;
  width: 100%;
}

.quick-card {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: var(--bg-card);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  color: var(--text-main);
  text-align: left;
  cursor: pointer;
  transition: var(--transition-fast);
  font-family: var(--font-primary);
}

.quick-card:hover {
  background: var(--bg-input);
  border-color: var(--border-active);
  transform: translateY(-2px);
  box-shadow: var(--shadow-sm);
}

.quick-icon {
  font-size: 1.3rem;
  flex-shrink: 0;
}

.quick-text {
  font-size: 0.9rem;
  font-weight: 500;
  line-height: 1.4;
  color: var(--text-main);
}

/* Chat Messages */
.chat-messages {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.message-row {
  display: flex;
  gap: 12px;
  max-width: 85%;
  animation: messageSlideIn 0.3s ease-out;
}

.message-row.user-row {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.message-row.ai-row {
  align-self: flex-start;
}

.avatar-small {
  width: 36px;
  height: 36px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  flex-shrink: 0;
}

.user-avatar {
  background: linear-gradient(135deg, #3b82f6 0%, #1d4ed8 100%);
  color: #fff;
}

.ai-avatar {
  background: linear-gradient(135deg, var(--primary) 0%, var(--accent-purple) 100%);
  color: #fff;
  box-shadow: var(--shadow-glow);
}

.message-bubble {
  padding: 14px 18px;
  border-radius: var(--radius-lg);
  font-size: 0.95rem;
  line-height: 1.6;
  position: relative;
  word-wrap: break-word;
}

.user-row .message-bubble {
  background: var(--user-bubble-bg);
  color: #ffffff;
  border-bottom-right-radius: 4px;
  box-shadow: var(--shadow-sm);
}

.ai-row .message-bubble {
  background: var(--ai-bubble-bg);
  color: var(--text-main);
  border: 1px solid var(--border-light);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow-sm);
}

/* Formatted Content inside Bubbles */
.message-bubble p {
  margin-bottom: 8px;
}

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

.message-bubble strong {
  color: #ffffff;
  font-weight: 600;
}

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

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

.message-bubble code {
  background: rgba(0, 0, 0, 0.4);
  color: var(--accent-cyan);
  padding: 2px 6px;
  border-radius: 4px;
  font-family: 'Consolas', 'Courier New', monospace;
  font-size: 0.88rem;
}

.message-bubble pre {
  background: #0f172a;
  border: 1px solid var(--border-light);
  padding: 12px;
  border-radius: var(--radius-sm);
  overflow-x: auto;
  margin: 10px 0;
}

.message-bubble pre code {
  background: transparent;
  padding: 0;
  color: #e2e8f0;
}

/* Error Bubble */
.message-row.error-row .message-bubble {
  background: rgba(153, 27, 27, 0.2);
  border: 1px solid rgba(239, 68, 68, 0.4);
  color: #fecaca;
}

/* Loading Indicator */
.loading-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  align-self: flex-start;
  margin-top: 10px;
  animation: fadeIn 0.2s ease-in;
}

.loading-bar.hidden {
  display: none;
}

.loading-content {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--ai-bubble-bg);
  border: 1px solid var(--border-light);
  padding: 10px 16px;
  border-radius: var(--radius-lg);
  border-bottom-left-radius: 4px;
}

.typing-dots {
  display: flex;
  align-items: center;
  gap: 4px;
}

.typing-dots span {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: var(--accent-cyan);
  animation: bounceDot 1.4s infinite ease-in-out both;
}

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

.loading-text {
  font-size: 0.85rem;
  color: var(--text-muted);
  font-style: italic;
}

/* Footer / Input Area */
.app-footer {
  padding: 16px 24px;
  background: rgba(21, 28, 44, 0.95);
  backdrop-filter: blur(12px);
  border-top: 1px solid var(--border-light);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.input-form {
  width: 100%;
}

.input-container {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--bg-input);
  border: 1px solid var(--border-light);
  border-radius: var(--radius-md);
  padding: 8px 12px 8px 16px;
  transition: var(--transition-fast);
}

.input-container:focus-within {
  border-color: var(--primary);
  background: var(--bg-input-focus);
  box-shadow: var(--shadow-glow);
}

textarea {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  color: var(--text-main);
  font-family: var(--font-primary);
  font-size: 0.95rem;
  resize: none;
  max-height: 140px;
  line-height: 1.5;
  padding: 4px 0;
}

textarea::placeholder {
  color: var(--text-dim);
}

.btn-send {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-hover) 100%);
  border: none;
  color: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: var(--transition-fast);
  flex-shrink: 0;
}

.btn-send:hover {
  transform: scale(1.05);
  box-shadow: var(--shadow-glow);
}

.btn-send:disabled {
  opacity: 0.4;
  cursor: not-allowed;
  transform: none;
}

.btn-send svg {
  width: 18px;
  height: 18px;
}

.footer-disclaimer {
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-dim);
}

/* Animations */
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-100%); }
  to { opacity: 1; transform: translateY(0); }
}

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

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

@keyframes bounceDot {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1.0); }
}

/* Responsive Media Queries */
@media (max-width: 640px) {
  .app-container {
    border: none;
  }

  .app-header {
    padding: 12px 16px;
  }

  .header-title h1 {
    font-size: 1.05rem;
  }

  .chat-viewport {
    padding: 16px;
  }

  .message-row {
    max-width: 92%;
  }

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

  .app-footer {
    padding: 12px 16px;
  }

  .banner-content {
    flex-direction: column;
    align-items: flex-start;
  }
}
