/* ==========================================================================
   STUDY BUDDY - CSS STYLESHEET
   ========================================================================== */

/* Design Tokens & CSS Variables */
:root {
  /* Color Palette */
  --primary-hue: 245;
  --primary-50: #EEF2FF;
  --primary-100: #E0E7FF;
  --primary-200: #C7D2FE;
  --primary-300: #A5B4FC;
  --primary-400: #818CF8;
  --primary-500: #6366F1;
  --primary-600: #4F46E5;
  --primary-700: #4338CA;
  
  --accent-cyan: #06B6D4;
  --accent-purple: #8B5CF6;
  --accent-amber: #F59E0B;
  --accent-emerald: #10B981;
  --accent-rose: #F43F5E;

  /* Neutral Shades */
  --bg-main: #F8FAFC;
  --bg-card: #FFFFFF;
  --bg-sidebar: #F1F5F9;
  --border-color: #E2E8F0;
  --border-hover: #CBD5E1;
  
  --text-main: #0F172A;
  --text-muted: #64748B;
  --text-light: #94A3B8;

  /* Shadows */
  --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
  --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.08), 0 2px 4px -2px rgb(0 0 0 / 0.04);
  --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.05);
  --shadow-glow: 0 0 20px rgba(99, 102, 241, 0.25);

  /* Border Radius */
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 18px;
  --radius-full: 9999px;

  /* Transitions */
  --transition-fast: 0.15s ease;
  --transition-normal: 0.25s ease;
}

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

body {
  font-family: 'Plus Jakarta Sans', -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background-color: var(--bg-main);
  color: var(--text-main);
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  overflow: hidden;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
}

/* App Container */
.app-container {
  width: 100%;
  max-width: 1000px;
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: var(--bg-card);
  box-shadow: var(--shadow-lg);
  position: relative;
}

@media (min-width: 768px) {
  .app-container {
    height: 94vh;
    border-radius: var(--radius-lg);
    border: 1px solid var(--border-color);
    overflow: hidden;
  }
}

/* Header */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1rem 1.5rem;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border-color);
  z-index: 10;
}

.brand-section {
  display: flex;
  align-items: center;
  gap: 0.85rem;
}

.logo-avatar {
  position: relative;
  width: 44px;
  height: 44px;
  border-radius: var(--radius-md);
  background: linear-gradient(135deg, var(--primary-600), var(--accent-purple));
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.35rem;
  box-shadow: var(--shadow-glow);
}

.online-pulse {
  position: absolute;
  bottom: -2px;
  right: -2px;
  width: 12px;
  height: 12px;
  background-color: var(--accent-emerald);
  border: 2px solid white;
  border-radius: 50%;
}

.brand-info h1 {
  font-size: 1.15rem;
  font-weight: 800;
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--text-main);
}

.brand-info .badge {
  font-size: 0.65rem;
  font-weight: 700;
  padding: 0.15rem 0.5rem;
  background: var(--primary-50);
  color: var(--primary-600);
  border-radius: var(--radius-full);
  border: 1px solid var(--primary-200);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.status-text {
  font-size: 0.75rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 0.35rem;
}

.status-text .dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background-color: var(--accent-emerald);
  display: inline-block;
}

/* Header Buttons */
.header-actions {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.btn-secondary, .btn-outline, .btn-primary {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 0.9rem;
  font-size: 0.85rem;
  font-weight: 600;
  font-family: inherit;
  border-radius: var(--radius-md);
  cursor: pointer;
  transition: all var(--transition-fast);
  border: none;
}

.btn-secondary {
  background-color: var(--bg-main);
  color: var(--text-main);
  border: 1px solid var(--border-color);
  position: relative;
}

.btn-secondary:hover {
  background-color: var(--primary-50);
  border-color: var(--primary-300);
  color: var(--primary-700);
}

.btn-outline {
  background: transparent;
  color: var(--primary-600);
  border: 1px solid var(--primary-300);
}

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

.btn-primary {
  background: linear-gradient(135deg, var(--primary-600), var(--primary-500));
  color: white;
  box-shadow: 0 2px 4px rgba(79, 70, 229, 0.25);
}

.btn-primary:hover {
  opacity: 0.95;
  transform: translateY(-1px);
}

.status-indicator {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background-color: var(--text-light);
}

.status-indicator.active {
  background-color: var(--accent-emerald);
  box-shadow: 0 0 6px var(--accent-emerald);
}

.status-indicator.missing {
  background-color: var(--accent-amber);
  box-shadow: 0 0 6px var(--accent-amber);
}

/* Main Chat Messages Container */
.chat-main {
  flex: 1;
  overflow-y: auto;
  padding: 1.5rem;
  background: linear-gradient(180deg, #F8FAFC 0%, #FFFFFF 100%);
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  scroll-behavior: smooth;
}

.messages-container {
  display: flex;
  flex-direction: column;
  gap: 1.25rem;
  min-height: 100%;
}

/* Welcome Card */
.welcome-card {
  background: linear-gradient(135deg, #FFFFFF, #F1F5F9);
  border: 1px solid var(--border-color);
  border-radius: var(--radius-lg);
  padding: 1.75rem;
  text-align: center;
  box-shadow: var(--shadow-sm);
  margin-bottom: 0.5rem;
  animation: fadeIn 0.5s ease;
}

.welcome-icon-wrapper {
  width: 60px;
  height: 60px;
  margin: 0 auto 1rem;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary-100), var(--primary-200));
  color: var(--primary-600);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.75rem;
}

.welcome-card h2 {
  font-size: 1.35rem;
  font-weight: 800;
  margin-bottom: 0.5rem;
}

.welcome-card .highlight {
  background: linear-gradient(135deg, var(--primary-600), var(--accent-purple));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.welcome-card p {
  color: var(--text-muted);
  font-size: 0.9rem;
  max-width: 600px;
  margin: 0 auto 1.5rem;
}

.features-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 0.75rem;
  margin-bottom: 1.5rem;
}

.feature-item {
  background: white;
  padding: 0.75rem;
  border-radius: var(--radius-md);
  border: 1px solid var(--border-color);
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-main);
}

.feature-item i {
  color: var(--primary-600);
  font-size: 0.95rem;
}

/* Quick Questions */
.quick-questions-section {
  text-align: left;
  border-top: 1px dashed var(--border-color);
  padding-top: 1.25rem;
}

.quick-title {
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--text-main);
  margin-bottom: 0.75rem !important;
  display: flex;
  align-items: center;
  gap: 0.4rem;
}

.quick-title i {
  color: var(--accent-amber);
}

.quick-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
}

.quick-chip {
  background: white;
  border: 1px solid var(--border-color);
  padding: 0.5rem 0.85rem;
  border-radius: var(--radius-full);
  font-size: 0.8rem;
  font-weight: 500;
  font-family: inherit;
  color: var(--text-main);
  cursor: pointer;
  transition: all var(--transition-fast);
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.quick-chip i {
  color: var(--primary-500);
}

.quick-chip:hover {
  background-color: var(--primary-50);
  border-color: var(--primary-300);
  color: var(--primary-700);
  transform: translateY(-1px);
}

/* Chat Message Items */
.chat-row {
  display: flex;
  gap: 0.75rem;
  margin-bottom: 0.25rem;
  animation: slideUp 0.3s ease;
}

.chat-row.user-row {
  justify-content: flex-end;
}

.chat-row.bot-row {
  justify-content: flex-start;
}

.avatar {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  flex-shrink: 0;
  margin-top: 2px;
}

.user-avatar {
  background: linear-gradient(135deg, #3B82F6, #1D4ED8);
  color: white;
}

.bot-avatar {
  background: linear-gradient(135deg, var(--primary-600), var(--accent-purple));
  color: white;
  box-shadow: 0 2px 6px rgba(99, 102, 241, 0.3);
}

.message-bubble {
  max-width: 82%;
  padding: 0.85rem 1.15rem;
  border-radius: var(--radius-lg);
  font-size: 0.92rem;
  line-height: 1.6;
  position: relative;
  word-break: break-word;
}

.user-row .message-bubble {
  background: linear-gradient(135deg, var(--primary-600), var(--primary-500));
  color: white;
  border-bottom-right-radius: 4px;
  box-shadow: 0 2px 6px rgba(79, 70, 229, 0.2);
}

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

/* Formatting inside Bot Messages */
.message-bubble p {
  margin-bottom: 0.6rem;
}

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

.message-bubble ul, .message-bubble ol {
  margin-left: 1.25rem;
  margin-bottom: 0.6rem;
}

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

.message-bubble strong {
  font-weight: 700;
}

.message-bubble blockquote {
  border-left: 3px solid var(--primary-500);
  padding-left: 0.75rem;
  margin: 0.5rem 0;
  color: var(--text-muted);
  font-style: italic;
}

/* Code Formatting inside Bot Bubble */
.message-bubble pre {
  background-color: #0F172A;
  color: #F8FAFC;
  padding: 0.85rem;
  border-radius: var(--radius-md);
  overflow-x: auto;
  font-family: "Fira Code", Monaco, Consolas, monospace;
  font-size: 0.82rem;
  margin: 0.6rem 0;
  position: relative;
}

.message-bubble code {
  background-color: rgba(99, 102, 241, 0.1);
  color: var(--primary-700);
  padding: 0.15rem 0.35rem;
  border-radius: 4px;
  font-family: "Fira Code", Monaco, Consolas, monospace;
  font-size: 0.85em;
}

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

.code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #1E293B;
  color: #94A3B8;
  padding: 0.35rem 0.75rem;
  font-size: 0.72rem;
  font-weight: 600;
  border-top-left-radius: var(--radius-md);
  border-top-right-radius: var(--radius-md);
  margin-bottom: -0.6rem;
}

.btn-copy-code {
  background: transparent;
  border: none;
  color: #94A3B8;
  cursor: pointer;
  font-size: 0.75rem;
  display: flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
}

.btn-copy-code:hover {
  color: white;
  background-color: rgba(255, 255, 255, 0.1);
}

/* Thinking Indicator */
.thinking-wrapper {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  margin-bottom: 0.5rem;
  animation: fadeIn 0.2s ease;
}

.thinking-bubble {
  background: white;
  border: 1px solid var(--border-color);
  padding: 0.65rem 1rem;
  border-radius: var(--radius-lg);
  border-bottom-left-radius: 4px;
  box-shadow: var(--shadow-sm);
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.thinking-content {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  color: var(--text-muted);
  font-size: 0.85rem;
  font-weight: 500;
}

.thinking-spinner {
  width: 14px;
  height: 14px;
  border: 2px solid var(--primary-200);
  border-top-color: var(--primary-600);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

.pulse-dots {
  display: flex;
  gap: 4px;
}

.pulse-dots span {
  width: 6px;
  height: 6px;
  background-color: var(--primary-400);
  border-radius: 50%;
  animation: pulseDot 1.4s infinite ease-in-out both;
}

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

/* Chat Footer & Input Area */
.chat-footer {
  padding: 1rem 1.5rem;
  background-color: white;
  border-top: 1px solid var(--border-color);
}

.input-wrapper {
  display: flex;
  align-items: flex-end;
  gap: 0.6rem;
  background: var(--bg-main);
  border: 1.5px solid var(--border-color);
  border-radius: var(--radius-md);
  padding: 0.5rem 0.75rem;
  transition: border-color var(--transition-fast), box-shadow var(--transition-fast);
}

.input-wrapper:focus-within {
  border-color: var(--primary-500);
  box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.15);
  background-color: white;
}

#user-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  resize: none;
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--text-main);
  max-height: 140px;
  min-height: 24px;
  line-height: 1.5;
  padding: 0.2rem 0;
}

#user-input::placeholder {
  color: var(--text-light);
}

.btn-send {
  width: 38px;
  height: 38px;
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--primary-600), var(--primary-500));
  color: white;
  border: none;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  transition: all var(--transition-fast);
  flex-shrink: 0;
}

.btn-send:hover:not(:disabled) {
  opacity: 0.95;
  transform: scale(1.03);
}

.btn-send:disabled {
  background: var(--border-color);
  color: var(--text-light);
  cursor: not-allowed;
}

.input-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-top: 0.4rem;
  font-size: 0.72rem;
  color: var(--text-muted);
  padding: 0 0.25rem;
}

/* API Key Modal */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: rgba(15, 23, 42, 0.5);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 1rem;
  animation: fadeIn 0.2s ease;
}

.modal-card {
  background: white;
  width: 100%;
  max-width: 480px;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
  animation: slideUp 0.3s ease;
}

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

.modal-title {
  display: flex;
  align-items: center;
  gap: 0.6rem;
}

.modal-title i {
  font-size: 1.2rem;
  color: var(--primary-600);
}

.modal-title h3 {
  font-size: 1.05rem;
  font-weight: 700;
}

.btn-icon-close {
  background: transparent;
  border: none;
  font-size: 1.5rem;
  color: var(--text-muted);
  cursor: pointer;
  line-height: 1;
  padding: 0.2rem;
  border-radius: 4px;
}

.btn-icon-close:hover {
  color: var(--text-main);
  background-color: var(--bg-main);
}

.modal-body {
  padding: 1.5rem;
  font-size: 0.88rem;
  color: var(--text-muted);
}

.input-group {
  margin: 1rem 0;
}

.input-group label {
  display: block;
  font-weight: 600;
  color: var(--text-main);
  margin-bottom: 0.4rem;
  font-size: 0.83rem;
}

.input-password-wrapper {
  display: flex;
  border: 1px solid var(--border-color);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.input-password-wrapper input {
  flex: 1;
  padding: 0.65rem 0.85rem;
  border: none;
  outline: none;
  font-family: inherit;
  font-size: 0.88rem;
}

.btn-toggle-vis {
  background: var(--bg-main);
  border: none;
  padding: 0 0.85rem;
  color: var(--text-muted);
  cursor: pointer;
}

.key-help-box {
  background: var(--primary-50);
  border: 1px solid var(--primary-200);
  border-radius: var(--radius-md);
  padding: 0.85rem;
  display: flex;
  gap: 0.75rem;
  font-size: 0.8rem;
  color: var(--primary-700);
  margin-top: 1rem;
}

.key-help-box i {
  font-size: 1.1rem;
  margin-top: 2px;
}

.key-help-box a {
  color: var(--primary-700);
  font-weight: 700;
  text-decoration: underline;
}

.small-note {
  margin-top: 0.35rem;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.modal-footer {
  padding: 1rem 1.5rem;
  background-color: var(--bg-main);
  border-top: 1px solid var(--border-color);
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
}

.btn-danger-light {
  background: transparent;
  color: var(--accent-rose);
  border: 1px solid rgba(244, 63, 94, 0.3);
  padding: 0.5rem 0.9rem;
  font-weight: 600;
  border-radius: var(--radius-md);
  cursor: pointer;
}

.btn-danger-light:hover {
  background-color: rgba(244, 63, 94, 0.1);
}

/* Toast Notifications */
.toast-container {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 200;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  pointer-events: none;
}

.toast {
  pointer-events: auto;
  background: #0F172A;
  color: white;
  padding: 0.75rem 1.15rem;
  border-radius: var(--radius-md);
  font-size: 0.85rem;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  display: flex;
  align-items: center;
  gap: 0.6rem;
  animation: slideLeft 0.3s ease;
}

.toast.success i { color: var(--accent-emerald); }
.toast.warning i { color: var(--accent-amber); }
.toast.error i { color: var(--accent-rose); }

/* Keyframe Animations */
@keyframes fadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

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

@keyframes slideLeft {
  from { opacity: 0; transform: translateX(20px); }
  to { opacity: 1; transform: translateX(0); }
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

@keyframes pulseDot {
  0%, 80%, 100% { transform: scale(0); opacity: 0.3; }
  40% { transform: scale(1); opacity: 1; }
}

.hidden {
  display: none !important;
}

/* Responsive Rules */
@media (max-width: 640px) {
  .app-header {
    padding: 0.85rem 1rem;
  }
  
  .brand-info h1 {
    font-size: 1rem;
  }

  .btn-label {
    display: none;
  }

  .btn-secondary, .btn-outline {
    padding: 0.5rem;
  }

  .welcome-card {
    padding: 1.25rem 1rem;
  }

  .message-bubble {
    max-width: 90%;
  }

  .chat-footer {
    padding: 0.75rem 1rem;
  }
}
