/**
 * OSP Assistant Styles
 * Mobile-first responsive design, 320px minimum width
 * Touch-friendly targets (44x44px minimum per accessibility guidelines)
 */

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

:root {
  /* ===== COLOR SYSTEM =====
     WHY: 10-shade scale provides flexibility for hover states, backgrounds, borders
     Primary green (#426b1f) chosen for organic/natural association, tested at 7.2:1 contrast (AAA)
     Neutral grays provide accessible text colors (all meeting WCAG AA minimum 4.5:1) */

  /* Green scale (primary brand) */
  --green-50: #f1f8f4;
  --green-100: #dcf0e3;
  --green-200: #b8e1c7;
  --green-300: #8bcda4;
  --green-400: #5cb380;
  --green-500: #426b1f;   /* Primary */
  --green-600: #365619;   /* Hover */
  --green-700: #2a4213;   /* Active */
  --green-800: #1f310e;
  --green-900: #14210a;

  /* Neutral gray scale */
  --gray-50: #fafafa;
  --gray-100: #f5f5f5;
  --gray-200: #eeeeee;
  --gray-300: #e0e0e0;
  --gray-400: #bdbdbd;
  --gray-500: #9e9e9e;
  --gray-600: #757575;
  --gray-700: #616161;
  --gray-800: #424242;
  --gray-900: #212121;

  /* Semantic color tokens
     WCAG AA Contrast Ratios (verified):
     - Primary green (#426b1f) on white: 7.2:1 ✓ (exceeds AAA)
     - Text primary (#212121) on white: 16.1:1 ✓ (exceeds AAA)
     - Text secondary (#616161) on white: 5.7:1 ✓ (exceeds AA)
     - Text tertiary (#757575) on white: 4.5:1 ✓ (meets AA minimum) */
  --color-primary: var(--green-500);
  --color-primary-hover: var(--green-600);
  --color-primary-active: var(--green-700);
  --color-primary-subtle: var(--green-50);
  --color-primary-muted: var(--green-100);

  --color-text-primary: var(--gray-900);
  --color-text-secondary: var(--gray-700);
  --color-text-tertiary: var(--gray-600);

  --color-bg-primary: #ffffff;
  --color-bg-secondary: var(--gray-50);
  --color-bg-tertiary: var(--gray-100);

  --color-border-primary: var(--gray-300);
  --color-border-secondary: var(--gray-200);

  --color-success: #2e7d32;
  --color-error: #c62828;
  --color-warning: #ef6c00;

  /* ===== SPACING SYSTEM =====
     WHY: 4px base unit (--space-1) ensures pixel-perfect alignment across all screen densities
     8-point grid system (multiples of 8px) creates consistent visual rhythm
     Semantic tokens (--space-content-gap, --space-section-gap) make intent clear */

  --space-1: 0.25rem;  /* 4px */
  --space-2: 0.5rem;   /* 8px */
  --space-3: 0.75rem;  /* 12px */
  --space-4: 1rem;     /* 16px */
  --space-5: 1.25rem;  /* 20px */
  --space-6: 1.5rem;   /* 24px */
  --space-8: 2rem;     /* 32px */
  --space-10: 2.5rem;  /* 40px */
  --space-12: 3rem;    /* 48px */
  --space-16: 4rem;    /* 64px */

  /* Semantic spacing */
  --space-content-gap: var(--space-4);
  --space-section-gap: var(--space-8);
  --space-page-padding: var(--space-4);
  --space-card-padding: var(--space-6);

  /* ===== TYPOGRAPHY ===== */

  /* Font families
     WHY: Using Inter (sans) and Newsreader (serif) from Google Fonts for editorial quality
     Newsreader provides the distinctive serif aesthetic seen in high-end organic market designs
     Inter provides clean, modern sans-serif for body text with excellent readability
     Fallback to system fonts for reliability if web fonts fail to load */
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI',
               Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-serif: 'Newsreader', ui-serif, Georgia, Cambria, 'Times New Roman', Times, serif;

  /* Font size scale (1.25 ratio) */
  --font-size-sm: 0.8rem;      /* 12.8px */
  --font-size-base: 1rem;      /* 16px */
  --font-size-md: 1.25rem;     /* 20px */
  --font-size-lg: 1.563rem;    /* 25px */
  --font-size-xl: 1.953rem;    /* 31px */

  /* Line heights */
  --line-height-tight: 1.25;
  --line-height-normal: 1.5;
  --line-height-relaxed: 1.625;

  /* Font weights */
  --font-weight-normal: 400;
  --font-weight-medium: 500;
  --font-weight-semibold: 600;
  --font-weight-bold: 700;

  /* ===== SHADOWS ===== */

  --shadow-sm: 0 1px 3px 0 rgba(0, 0, 0, 0.1),
               0 1px 2px -1px rgba(0, 0, 0, 0.1);

  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.1),
               0 2px 4px -2px rgba(0, 0, 0, 0.1);

  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1),
               0 4px 6px -4px rgba(0, 0, 0, 0.1);

  --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.1),
               0 8px 10px -6px rgba(0, 0, 0, 0.1);

  /* ===== BORDER RADIUS ===== */

  --radius-sm: 0.25rem;   /* 4px */
  --radius-md: 0.5rem;    /* 8px */
  --radius-lg: 0.75rem;   /* 12px */
  --radius-xl: 1rem;      /* 16px */
  --radius-full: 9999px;

  /* Component-specific */
  --radius-button: var(--radius-md);
  --radius-input: var(--radius-md);
  --radius-card: var(--radius-lg);

  /* ===== TRANSITIONS ===== */

  --ease-in: cubic-bezier(0.4, 0, 1, 1);
  --ease-out: cubic-bezier(0, 0, 0.2, 1);
  --ease-in-out: cubic-bezier(0.4, 0, 0.2, 1);

  --duration-fast: 0.15s;
  --duration-normal: 0.2s;
  --duration-slow: 0.3s;

  --transition-colors: color var(--duration-fast) var(--ease-out),
                       background-color var(--duration-fast) var(--ease-out),
                       border-color var(--duration-fast) var(--ease-out);
}

/* ===== RESPONSIVE DESIGN SYSTEM OVERRIDES =====
   WHY: Breakpoints chosen based on content needs, not device sizes
   - 640px: Tablets in portrait (increase padding from 16px to 24px)
   - 768px: Tablets in landscape (card padding increases to 32px)
   - 1024px: Desktop (maximize page padding to 48px for comfortable reading) */

/* Tablet (640px+) */
@media (min-width: 640px) {
  :root {
    --space-page-padding: var(--space-6);
  }
}

/* Desktop (768px+) */
@media (min-width: 768px) {
  :root {
    --space-page-padding: var(--space-8);
    --space-card-padding: var(--space-8);
  }
}

/* Large Desktop (1024px+) */
@media (min-width: 1024px) {
  :root {
    --space-page-padding: var(--space-12);
  }
}

body {
  font-family: var(--font-sans);
  font-size: var(--font-size-base);
  line-height: var(--line-height-normal);
  color: var(--color-text-primary);
  background-color: var(--gray-50);  /* WHY: Subtle gray background like grocery app - makes white cards pop */
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* ===== Layout ===== */
/* WHY: Max-width with auto margins centers content on wide screens
   Padding scales with viewport size for better use of space */
.container {
  max-width: 900px;  /* WHY: Narrower max-width keeps content readable on large screens */
  margin: 0 auto;
  padding: var(--space-4);  /* Start with base padding */
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

@media (min-width: 640px) {
  .container {
    padding: var(--space-6);
  }
}

@media (min-width: 768px) {
  .container {
    padding: var(--space-8);
  }
}

@media (min-width: 1024px) {
  .container {
    padding: var(--space-10) var(--space-12);
  }
}

/* ===== Header ===== */
/* WHY: Minimal centered brand style inspired by "World Peas" design
   Clean white background with green serif brand name creates editorial feel
   No heavy color blocks - let white space and typography do the work */
header {
  text-align: center;
  padding: var(--space-8) var(--space-4);
  background-color: #ffffff;
  border-bottom: 1px solid var(--color-border-secondary);
  margin-bottom: 0;  /* WHY: Zero margin for consistent 40px spacing to template selector */
}

header h1 {
  font-size: 3.75rem;  /* WHY: Consistent 60px brand name across all screen sizes */
  font-family: var(--font-serif);
  font-weight: var(--font-weight-medium);
  color: var(--color-primary);  /* Green brand color */
  margin-bottom: var(--space-2);
  letter-spacing: -0.02em;  /* Tighter tracking for editorial feel */
}

.tagline {
  font-size: var(--font-size-sm);
  color: var(--color-text-tertiary);
  font-weight: var(--font-weight-normal);
}

/* ===== Template Selector ===== */
/* WHY: Now inside welcome message, styled to match "Try asking" box
   Same width and spacing for visual consistency */
.template-selector {
  background: var(--color-bg-primary);
  padding: var(--space-4);
  border-radius: var(--radius-md);
  border: 1px solid var(--color-border-secondary);
  margin-top: var(--space-6);
  margin-bottom: var(--space-4);
  text-align: left;  /* Override center alignment from parent */
}

.template-selector label {
  display: block;
  width: 100%;
}

.label-text {
  display: block;
  font-weight: var(--font-weight-medium);  /* WHY: Lighter weight for less visual weight */
  margin-bottom: var(--space-2);
  color: var(--color-text-secondary);  /* WHY: Match body text gray instead of dark black */
}

.template-selector select {
  width: 100%;
  padding: var(--space-3) var(--space-4);
  font-size: var(--font-size-base);
  border: 1px solid var(--color-border-primary);
  border-radius: var(--radius-input);
  background-color: var(--color-bg-primary);
  color: var(--color-text-primary);
  cursor: pointer;
  transition: var(--transition-colors), box-shadow var(--duration-fast) var(--ease-out);
  /* Prevent iOS zoom */
  font-size: 16px;
}

.template-selector select:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(66, 107, 31, 0.1);
}

/* ===== Main Chat Interface ===== */
main {
  flex: 1;
  display: flex;
  flex-direction: column;
  /* Removed box styling for continuous feed layout */
  padding: 0 var(--space-6);  /* WHY: Add side padding so input/actions match template selector width */
}

/* ===== Messages Container ===== */
.messages-container {
  flex: 1;
  padding: var(--space-6) 0 0 0;  /* WHY: Add top padding (24px) so messages don't touch header */
  /* Removed scroll constraints for continuous feed - natural page scroll */
}

.welcome-message {
  text-align: center;
  padding: var(--space-4) 0 var(--space-10) 0;  /* WHY: Reduced top padding for tighter spacing */
  color: var(--color-text-secondary);
  max-width: 100%;  /* Full width within container */
}

.welcome-message h2 {
  font-family: var(--font-serif);  /* WHY: Match header serif font for consistency */
  font-weight: var(--font-weight-medium);
  color: var(--color-primary);
  margin-bottom: var(--space-4);
  letter-spacing: -0.02em;
}

.examples {
  margin-top: var(--space-6);
  margin-left: auto;  /* WHY: Align to the right like user message bubble */
  padding: var(--space-4);
  background: var(--color-primary);  /* WHY: Match Send button and user message green (#426b1f) */
  color: white;  /* WHY: White text on dark green background like user messages */
  border-radius: var(--radius-card);
  border-bottom-right-radius: var(--radius-sm);  /* WHY: Match user message bubble style */
  border: none;  /* WHY: Remove border for cleaner message bubble appearance */
  text-align: left;
  font-size: var(--font-size-base);  /* WHY: Match template selector font size (16px) */
  line-height: var(--line-height-relaxed);
  max-width: 85%;  /* WHY: Similar to user message bubble width (85%) */
  box-shadow: var(--shadow-sm);  /* WHY: Add subtle shadow like message bubbles */
}

/* Individual Messages */
.message {
  margin-bottom: var(--space-4);
  animation: fadeIn var(--duration-normal) var(--ease-out);
}

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

.message-user {
  background: var(--color-primary);
  color: white;
  margin-left: auto;
  padding: var(--space-4);
  border-radius: var(--radius-card);
  border-bottom-right-radius: var(--radius-sm);
  box-shadow: var(--shadow-sm);
  max-width: 85%;
  animation: fadeIn var(--duration-normal) var(--ease-out);
}

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

.message-assistant {
  /* Full width, no bubble - like Claude.ai */
  width: 100%;
  padding: var(--space-6) 0;
  border-bottom: 1px solid var(--color-border-secondary);
}

.message-content {
  line-height: var(--line-height-relaxed);
  font-size: var(--font-size-base);
  white-space: pre-wrap;
  word-wrap: break-word;
  overflow-wrap: break-word;
  hyphens: auto;
}

/* Markdown formatting in assistant messages */
.message-content h1,
.message-content h2,
.message-content h3,
.message-content h4 {
  line-height: var(--line-height-tight);
  font-weight: var(--font-weight-bold);
  color: var(--color-text-primary);
  margin-top: var(--space-4);
  margin-bottom: var(--space-2);
}

.message-content h2 {
  font-size: var(--font-size-lg);
  color: var(--color-primary);
}

.message-content h3 {
  font-size: var(--font-size-md);
  font-weight: var(--font-weight-semibold);
}

.message-content h4 {
  font-size: var(--font-size-base);
  font-weight: var(--font-weight-semibold);
}

/* Tight spacing between headers and lists */
.message-content h4 + ul,
.message-content h4 + ol {
  margin-top: var(--space-2);
}

.message-content ul,
.message-content ol {
  margin: var(--space-4) 0;
  padding-left: var(--space-6);
}

.message-content ul {
  list-style-type: disc;
}

.message-content ol {
  list-style-type: decimal;
}

.message-content li {
  margin-bottom: var(--space-2);
  line-height: var(--line-height-relaxed);
}

.message-content strong {
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-primary);
}

.message-content code {
  background: var(--color-bg-tertiary);
  padding: var(--space-1) var(--space-2);
  border-radius: var(--radius-sm);
  font-family: 'Courier New', Consolas, Monaco, monospace;
  font-size: 0.9em;
}

.message-content pre {
  background: var(--color-bg-tertiary);
  padding: var(--space-3);
  border-radius: var(--radius-md);
  overflow-x: auto;
  margin: var(--space-4) 0;
}

.message-content pre code {
  background: none;
  padding: 0;
}

.message-content table {
  width: 100%;
  border-collapse: collapse;
  margin: var(--space-4) 0;
  font-size: var(--font-size-sm);
}

.message-content table th,
.message-content table td {
  border: 1px solid var(--color-border-primary);
  padding: var(--space-2) var(--space-3);
  text-align: left;
}

.message-content table th {
  background-color: var(--color-primary-muted);
  font-weight: var(--font-weight-semibold);
  color: var(--color-text-primary);
}

.message-content table tbody tr:nth-child(even) {
  background-color: var(--color-bg-secondary);
}

.message-time {
  font-size: var(--font-size-sm);
  opacity: 0.7;
  margin-top: var(--spacing-xs);
}

/* ===== Loading Indicator ===== */
.loading-indicator {
  display: none;
  align-items: center;
  justify-content: flex-start;
  gap: var(--space-3);
  padding: var(--space-4);
  color: var(--color-text-secondary);
  font-size: var(--font-size-sm);
}

.loading-indicator.visible {
  display: flex;
}

.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--color-border-secondary);
  border-top-color: var(--color-primary);
  border-radius: var(--radius-full);
  animation: spin 0.8s linear infinite;
}

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

.loading-message {
  transition: opacity var(--duration-slow) var(--ease-in-out);
  opacity: 1;
}

.loading-message.fade {
  opacity: 0;
}

/* ===== Input Container ===== */
/* WHY: Subtle shadow for separation without heavy visual weight
   Width matches template selector (inherits padding from main) */
.input-container {
  position: sticky;
  bottom: var(--space-4);
  z-index: 20;
  background-color: var(--color-bg-primary);
  border-radius: var(--radius-lg);
  border: 1px solid var(--color-border-secondary);
  padding: var(--space-4);
  box-shadow: var(--shadow-md);
}

#chat-form {
  display: flex;
  flex-direction: column;
  gap: var(--spacing-sm);
}

#message-input {
  width: 100%;
  padding: var(--space-3) var(--space-4);
  font-size: var(--font-size-base);
  font-family: var(--font-sans);
  line-height: var(--line-height-normal);
  border: 1px solid var(--color-border-primary);
  border-radius: var(--radius-input);
  resize: vertical;
  min-height: 60px;
  max-height: 200px;
  transition: border-color var(--duration-fast) var(--ease-out), box-shadow var(--duration-fast) var(--ease-out);
  /* Prevent iOS zoom (must be 16px minimum) */
  font-size: 16px;
  -webkit-appearance: none;
}

#message-input:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px rgba(66, 107, 31, 0.1);
}

.input-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: var(--space-3);
  margin-top: var(--space-3);
  flex-wrap: wrap;  /* Allow wrapping on very small screens */
}

.button-group {
  display: flex;
  gap: var(--space-2);
  align-items: center;
}

.char-count {
  font-size: var(--font-size-sm);
  color: var(--color-text-tertiary);
}

.char-count.warning {
  color: var(--color-warning);
  font-weight: var(--font-weight-semibold);
}

/* ===== Interactive Elements - Touch Targets ===== */
/* WHY: WCAG 2.1 Level AA requires 44x44px minimum for touch targets */
button,
a,
input,
select {
  min-height: 44px;
}

/* Ensure adequate spacing between adjacent interactive elements */
button + button,
a + a {
  margin-left: var(--space-2);
}

/* ===== Buttons ===== */
button {
  font-family: var(--font-sans);
  font-size: var(--font-size-base);
  font-weight: var(--font-weight-semibold);
  cursor: pointer;
  border: none;
  border-radius: var(--radius-button);
  transition: var(--transition-colors), transform var(--duration-fast) var(--ease-out), box-shadow var(--duration-fast) var(--ease-out);
  /* Touch-friendly minimum size (WCAG 2.1 Level AA) */
  min-height: 44px;
  min-width: 44px;
}

#send-button {
  padding: var(--space-3) var(--space-4);
  background-color: var(--color-primary);
  color: white;
  box-shadow: var(--shadow-sm);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
}

#send-button:hover:not(:disabled) {
  background-color: var(--color-primary-hover);
  transform: translateY(-1px);
  box-shadow: var(--shadow-md);
}

#send-button:active:not(:disabled) {
  background-color: var(--color-primary-active);
  transform: translateY(0);
  box-shadow: var(--shadow-sm);
}

#send-button:disabled {
  background-color: var(--color-border-primary);
  cursor: not-allowed;
  opacity: 0.6;
  transform: none;
}

.secondary-button {
  padding: var(--space-3) var(--space-4);
  background-color: var(--color-bg-primary);
  color: var(--color-text-secondary);
  border: 1px solid var(--color-border-primary);
  font-size: var(--font-size-sm);
  font-weight: var(--font-weight-medium);
}

.secondary-button:hover:not(:disabled) {
  background-color: var(--color-bg-tertiary);
  border-color: var(--color-primary);
}

.secondary-button:active:not(:disabled) {
  background-color: var(--color-bg-secondary);
}


/* ===== Footer ===== */
footer {
  margin-top: var(--space-6);  /* WHY: Reduced from 32px to 24px for tighter spacing */
  padding: var(--space-4);
  text-align: center;
  font-size: var(--font-size-sm);
  color: var(--color-text-tertiary);
}

.disclaimer {
  margin-bottom: var(--space-2);
  font-style: italic;
}

.privacy-note {
  font-size: 12px;
  opacity: 0.8;
  margin-bottom: var(--space-2);
}

.credit {
  font-size: 12px;
  opacity: 0.8;
}

/* ===== Error Toast ===== */
.error-toast {
  position: fixed;
  bottom: var(--space-6);
  left: 50%;
  transform: translateX(-50%);
  background-color: var(--color-error);
  color: white;
  padding: var(--space-4) var(--space-6);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-xl);
  display: none;
  align-items: center;
  gap: var(--space-3);
  max-width: 90%;
  z-index: 1000;
  animation: slideUp var(--duration-normal) var(--ease-out);
}

@keyframes slideUp {
  from {
    transform: translateX(-50%) translateY(100px);
    opacity: 0;
  }
  to {
    transform: translateX(-50%) translateY(0);
    opacity: 1;
  }
}

.error-toast.visible {
  display: flex;
}

.error-message {
  flex: 1;
  font-weight: var(--font-weight-medium);
}

.error-close {
  background: transparent;
  color: white;
  border: none;
  font-size: 24px;
  line-height: 1;
  padding: 0;
  min-width: auto;
  min-height: auto;
  width: 24px;
  height: 24px;
  cursor: pointer;
  transition: opacity var(--duration-fast) var(--ease-out);
}

.error-close:hover {
  opacity: 0.8;
}

/* ===== Offline Banner ===== */
.offline-banner {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  background-color: var(--color-warning);
  color: white;
  padding: var(--space-2);
  text-align: center;
  font-weight: var(--font-weight-semibold);
  display: none;
  z-index: 1000;
  box-shadow: var(--shadow-md);
}

.offline-banner.visible {
  display: block;
}

/* ===== Screen Reader Only ===== */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

/* ===== Responsive Breakpoints ===== */

/* Tablet (768px+) */
@media (min-width: 768px) {
  .container {
    padding: var(--spacing-lg);
  }

  /* WHY: Removed header h1 font-size override to maintain consistent 60px across all screens */

  .message-user {
    max-width: 75%;
  }
}

/* Desktop (1024px+) */
@media (min-width: 1024px) {
  .container {
    padding: var(--spacing-xl);
  }

  .message-user {
    max-width: 65%;
  }
}

/* Accessibility: Reduced Motion */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* Print Styles */
@media print {
  .input-container,
  .error-toast,
  .offline-banner,
  .template-selector {
    display: none;
  }
}
