/* ============================================
   GLOBAL.CSS — Foundation Layer
   Crosra Research Analysis Tool
   
   Contains:
   - CSS Reset (modern normalize)
   - Design tokens (colors, spacing, typography)
   - Base element styles
   - Theme (light + dark mode)
   - Accessibility utilities
   
   Load order: global.css → components.css → layout.css → pages/*
   ============================================ */

/* ============================================
   1. DESIGN TOKENS — LIGHT MODE (default)
   ============================================ */
:root {
  /* ---- Brand Colors: Refined Green ---- */
  --primary-50: #f0f9f0;
  --primary-100: #dcf2dc;
  --primary-200: #bce5bc;
  --primary-300: #8ed08e;
  --primary-400: #5cb85c;
  --primary-500: #3a9d3a;
  --primary-600: #2d7a2d;
  --primary-700: #1f5c1f;
  --primary-800: #0f3d0f;
  --primary-900: #046307;  /* Signature deep green */
  --primary-950: #022a04;
  
  /* ---- Brand Colors: Refined Gold ---- */
  --accent-50: #fef9e7;
  --accent-100: #fdf0c4;
  --accent-200: #fbe08a;
  --accent-300: #f8cc4c;
  --accent-400: #f5b81a;
  --accent-500: #d4af37;  /* Signature gold */
  --accent-600: #b8941f;
  --accent-700: #8f7318;
  --accent-800: #6b5612;
  --accent-900: #4a3c0d;
  --accent-950: #2a2207;
  
  /* ---- Semantic aliases (primary usage) ---- */
  --primary: var(--primary-900);
  --primary-hover: var(--primary-800);
  --primary-active: var(--primary-700);
  --primary-light: var(--primary-100);
  --primary-lighter: var(--primary-50);
  --primary-dark: var(--primary-950);
  
  --accent: var(--accent-500);
  --accent-hover: var(--accent-600);
  --accent-active: var(--accent-700);
  --accent-light: var(--accent-100);
  --accent-lighter: var(--accent-50);
  --accent-dark: var(--accent-900);
  
  /* ---- Neutral Scale (Slate-based) ---- */
  --neutral-50: #f8fafc;
  --neutral-100: #f1f5f9;
  --neutral-200: #e2e8f0;
  --neutral-300: #cbd5e1;
  --neutral-400: #94a3b8;
  --neutral-500: #64748b;
  --neutral-600: #475569;
  --neutral-700: #334155;
  --neutral-800: #1e293b;
  --neutral-900: #0f172a;
  --neutral-950: #020617;
  
  /* ---- Semantic Colors ---- */
  --success: #10b981;
  --success-light: #d1fae5;
  --success-dark: #047857;
  
  --warning: #f59e0b;
  --warning-light: #fef3c7;
  --warning-dark: #b45309;
  
  --error: #ef4444;
  --error-light: #fee2e2;
  --error-dark: #b91c1c;
  
  --info: #3b82f6;
  --info-light: #dbeafe;
  --info-dark: #1d4ed8;
  
  /* ---- Surface Colors (light mode) ---- */
  --bg: #ffffff;
  --bg-subtle: var(--neutral-50);
  --bg-muted: var(--neutral-100);
  --bg-strong: var(--neutral-200);
  
  --surface: #ffffff;
  --surface-raised: #ffffff;
  --surface-sunken: var(--neutral-50);
  
  --border: var(--neutral-200);
  --border-strong: var(--neutral-300);
  --border-subtle: var(--neutral-100);
  
  /* ---- Text Colors ---- */
  --text: var(--neutral-900);
  --text-secondary: var(--neutral-600);
  --text-muted: var(--neutral-500);
  --text-subtle: var(--neutral-400);
  --text-inverse: #ffffff;
  --text-link: var(--primary-700);
  --text-link-hover: var(--primary-800);
  
  /* ---- Spacing (4px grid) ---- */
  --space-0: 0;
  --space-0-5: 2px;
  --space-1: 4px;
  --space-1-5: 6px;
  --space-2: 8px;
  --space-2-5: 10px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;
  --space-10: 40px;
  --space-12: 48px;
  --space-16: 64px;
  --space-20: 80px;
  --space-24: 96px;
  
  /* ---- Typography ---- */
  --font-sans: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --font-mono: 'SF Mono', 'Monaco', 'Inconsolata', 'Fira Code', 'Droid Sans Mono', 'Consolas', monospace;
  
  --text-xs: 0.75rem;     /* 12px */
  --text-sm: 0.875rem;    /* 14px */
  --text-base: 1rem;      /* 16px */
  --text-lg: 1.125rem;    /* 18px */
  --text-xl: 1.25rem;     /* 20px */
  --text-2xl: 1.5rem;     /* 24px */
  --text-3xl: 1.875rem;   /* 30px */
  --text-4xl: 2.25rem;    /* 36px */
  --text-5xl: 3rem;       /* 48px */
  
  --leading-tight: 1.25;
  --leading-normal: 1.5;
  --leading-relaxed: 1.625;
  
  --tracking-tight: -0.025em;
  --tracking-normal: 0;
  --tracking-wide: 0.025em;
  
  --font-normal: 400;
  --font-medium: 500;
  --font-semibold: 600;
  --font-bold: 700;
  
  /* ---- Border Radius (sharp, professional) ---- */
  --radius-none: 0;
  --radius-sm: 4px;
  --radius-md: 6px;
  --radius-lg: 8px;
  --radius-xl: 12px;
  --radius-2xl: 16px;
  --radius-full: 9999px;
  
  /* Backward compatibility */
  --radius: var(--radius-md);
  
  /* ---- Shadows (subtle, layered) ---- */
  --shadow-xs: 0 1px 2px rgba(15, 23, 42, 0.05);
  --shadow-sm: 0 1px 3px rgba(15, 23, 42, 0.08), 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 4px 6px -1px rgba(15, 23, 42, 0.08), 0 2px 4px -1px rgba(15, 23, 42, 0.04);
  --shadow-lg: 0 10px 15px -3px rgba(15, 23, 42, 0.08), 0 4px 6px -2px rgba(15, 23, 42, 0.04);
  --shadow-xl: 0 20px 25px -5px rgba(15, 23, 42, 0.10), 0 10px 10px -5px rgba(15, 23, 42, 0.04);
  --shadow-2xl: 0 25px 50px -12px rgba(15, 23, 42, 0.25);
  --shadow-inner: inset 0 2px 4px rgba(15, 23, 42, 0.06);
  
  /* ---- Transitions ---- */
  --transition-fast: 150ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-base: 200ms cubic-bezier(0.4, 0, 0.2, 1);
  --transition-slow: 300ms cubic-bezier(0.4, 0, 0.2, 1);
  
  /* Backward compatibility */
  --transition: var(--transition-base);
  
  /* ---- Z-index scale ---- */
  --z-0: 0;
  --z-10: 10;
  --z-20: 20;
  --z-30: 30;
  --z-40: 40;
  --z-50: 50;
  --z-dropdown: 100;
  --z-sticky: 200;
  --z-fixed: 300;
  --z-modal-backdrop: 400;
  --z-modal: 500;
  --z-popover: 600;
  --z-tooltip: 700;
  --z-toast: 800;
  
  /* ---- Breakpoints (for reference in JS) ---- */
  --bp-sm: 640px;
  --bp-md: 768px;
  --bp-lg: 1024px;
  --bp-xl: 1280px;
  --bp-2xl: 1536px;
  
  /* ---- Focus ring ---- */
  --focus-ring: 0 0 0 3px rgba(58, 157, 58, 0.35);
  --focus-ring-accent: 0 0 0 3px rgba(212, 175, 55, 0.4);
  --focus-ring-error: 0 0 0 3px rgba(239, 68, 68, 0.35);


      /* ============================================
       EXTENDED TOKENS (for SR pages compatibility)
       ============================================ */
    
    /* Extended backgrounds (3-tier surface system) */
    --bg-secondary: var(--bg-subtle);        /* Alias for consistency */
    --bg-tertiary: #f1f5f9;                  /* Slightly darker than subtle */
    
    /* Ultra-light status tints (for subtle backgrounds) */
    --success-lighter: #ecfdf5;              /* Very pale green */
    --warning-lighter: #fffbeb;              /* Very pale amber */
    --error-lighter: #fef2f2;                /* Very pale red */
    --info-lighter: #eff6ff;                 /* Very pale blue */
    --primary-lighter: #e8f5e9;              /* Very pale brand green */
    
    /* Extended accent */
    --accent-dark: #b5952f;                  /* Darker gold for contrast */
    --accent-light: #fff9e6;                 /* Pale gold tint */
    
    /* Risk of Bias traffic light scale */
    --rob-low: #10b981;                      /* Green - low concern */
    --rob-low-light: #d1fae5;
    --rob-some: #f59e0b;                     /* Amber - some concerns */
    --rob-some-light: #fef3c7;
    --rob-high: #ef4444;                     /* Red - high concern */
    --rob-high-light: #fee2e2;
    --rob-unclear: #94a3b8;                  /* Gray - unclear */
    --rob-unclear-light: #f1f5f9;


}

/* ============================================
   2. DESIGN TOKENS — DARK MODE
   ============================================ */
[data-theme="dark"] {
  /* Brand colors — lighter for dark backgrounds */
  --primary-50: #022a04;
  --primary-100: #0f3d0f;
  --primary-200: #1f5c1f;
  --primary-300: #2d7a2d;
  --primary-400: #3a9d3a;
  --primary-500: #5cb85c;
  --primary-600: #8ed08e;
  --primary-700: #bce5bc;
  --primary-800: #dcf2dc;
  --primary-900: #f0f9f0;
  --primary-950: #f8fcf8;
  
  --accent-50: #2a2207;
  --accent-100: #4a3c0d;
  --accent-200: #6b5612;
  --accent-300: #8f7318;
  --accent-400: #b8941f;
  --accent-500: #d4af37;  /* Signature gold — same */
  --accent-600: #f5b81a;
  --accent-700: #f8cc4c;
  --accent-800: #fbe08a;
  --accent-900: #fdf0c4;
  --accent-950: #fef9e7;
  
  /* Semantic aliases — auto-invert */
  --primary: var(--primary-400);
  --primary-hover: var(--primary-300);
  --primary-active: var(--primary-200);
  --primary-light: var(--primary-800);
  --primary-lighter: var(--primary-900);
  --primary-dark: var(--primary-50);
  
  --accent: var(--accent-500);
  --accent-hover: var(--accent-400);
  --accent-active: var(--accent-300);
  --accent-light: var(--accent-800);
  --accent-lighter: var(--accent-900);
  --accent-dark: var(--accent-50);
  
  /* Semantic colors — slightly adjusted for dark */
  --success: #34d399;
  --success-light: #064e3b;
  --success-dark: #6ee7b7;
  
  --warning: #fbbf24;
  --warning-light: #451a03;
  --warning-dark: #fcd34d;
  
  --error: #f87171;
  --error-light: #450a0a;
  --error-dark: #fca5a5;
  
  --info: #60a5fa;
  --info-light: #172554;
  --info-dark: #93c5fd;
  
  /* Surfaces — inverted */
  --bg: #0a0f1a;
  --bg-subtle: #0f172a;
  --bg-muted: #1e293b;
  --bg-strong: #334155;
  
  --surface: #111827;
  --surface-raised: #1f2937;
  --surface-sunken: #0a0f1a;
  
  --border: #1f2937;
  --border-strong: #334155;
  --border-subtle: #111827;
  
  /* Text — inverted */
  --text: #f1f5f9;
  --text-secondary: #cbd5e1;
  --text-muted: #94a3b8;
  --text-subtle: #64748b;
  --text-inverse: #0f172a;
  --text-link: var(--primary-400);
  --text-link-hover: var(--primary-300);
  
  /* Shadows — softer in dark mode */
  --shadow-xs: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-sm: 0 1px 3px rgba(0, 0, 0, 0.4), 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4), 0 2px 4px -1px rgba(0, 0, 0, 0.3);
  --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5), 0 4px 6px -2px rgba(0, 0, 0, 0.4);
  --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, 0.5), 0 10px 10px -5px rgba(0, 0, 0, 0.4);
  --shadow-2xl: 0 25px 50px -12px rgba(0, 0, 0, 0.7);
  --shadow-inner: inset 0 2px 4px rgba(0, 0, 0, 0.4);
  
  /* Focus ring — adjusted for dark */
  --focus-ring: 0 0 0 3px rgba(92, 184, 92, 0.5);
  --focus-ring-accent: 0 0 0 3px rgba(212, 175, 55, 0.5);
  --focus-ring-error: 0 0 0 3px rgba(248, 113, 113, 0.5);


      /* Extended backgrounds (dark mode) */
    --bg-secondary: var(--bg-subtle);
    --bg-tertiary: #1e293b;                  /* Darker than subtle */
    
    /* Ultra-light status tints (dark mode - use rgba for subtle glow) */
    --success-lighter: rgba(16, 185, 129, 0.10);
    --warning-lighter: rgba(245, 158, 11, 0.10);
    --error-lighter: rgba(239, 68, 68, 0.10);
    --info-lighter: rgba(59, 130, 246, 0.10);
    --primary-lighter: rgba(4, 99, 7, 0.15);
    
    /* Extended accent (dark mode) */
    --accent-dark: #e8c766;                  /* Lighter gold for dark bg */
    --accent-light: rgba(212, 175, 55, 0.15);
    
    /* Risk of Bias (dark mode - brighter for visibility) */
    --rob-low: #34d399;
    --rob-low-light: rgba(16, 185, 129, 0.18);
    --rob-some: #fbbf24;
    --rob-some-light: rgba(245, 158, 11, 0.18);
    --rob-high: #f87171;
    --rob-high-light: rgba(239, 68, 68, 0.18);
    --rob-unclear: #64748b;
    --rob-unclear-light: rgba(100, 116, 139, 0.18);

    
}

/* ============================================
   3. CSS RESET (Modern Normalize)
   ============================================ */
*, *::before, *::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html {
  -webkit-text-size-adjust: 100%;
  -moz-text-size-adjust: 100%;
  text-size-adjust: 100%;
  tab-size: 4;
  font-family: var(--font-sans);
  line-height: var(--leading-normal);
  scroll-behavior: smooth;
}

@media (prefers-reduced-motion: reduce) {
  html {
    scroll-behavior: auto;
  }
}

body {
  min-height: 100vh;
  font-family: var(--font-sans);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
  color: var(--text);
  background-color: var(--bg);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-rendering: optimizeLegibility;
}

/* Images & media */
img, picture, video, canvas, svg {
  display: block;
  max-width: 100%;
}

/* Forms inherit font */
input, button, textarea, select {
  font: inherit;
  color: inherit;
}

/* Remove default button styles */
button {
  background: none;
  border: none;
  cursor: pointer;
}

/* Remove list styles */
ul, ol {
  list-style: none;
}

/* Anchors */
a {
  color: var(--text-link);
  text-decoration: none;
  transition: color var(--transition-fast);
}

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

/* Hidden attribute */
[hidden] {
  display: none !important;
}

/* ============================================
   4. BASE TYPOGRAPHY
   ============================================ */
h1, h2, h3, h4, h5, h6 {
  font-weight: var(--font-semibold);
  line-height: var(--leading-tight);
  color: var(--text);
  letter-spacing: var(--tracking-tight);
  overflow-wrap: break-word;
}

h1 { font-size: var(--text-4xl); }
h2 { font-size: var(--text-3xl); }
h3 { font-size: var(--text-2xl); }
h4 { font-size: var(--text-xl); }
h5 { font-size: var(--text-lg); }
h6 { font-size: var(--text-base); }

p {
  overflow-wrap: break-word;
  line-height: var(--leading-relaxed);
}

small {
  font-size: var(--text-sm);
}

code, kbd, samp, pre {
  font-family: var(--font-mono);
  font-size: 0.9em;
}

pre {
  overflow-x: auto;
  padding: var(--space-4);
  background: var(--surface-sunken);
  border-radius: var(--radius-md);
  border: 1px solid var(--border);
}

code {
  padding: 2px 6px;
  background: var(--surface-sunken);
  border-radius: var(--radius-sm);
  color: var(--primary);
}

pre code {
  padding: 0;
  background: transparent;
  color: inherit;
}

strong, b {
  font-weight: var(--font-semibold);
}

/* ============================================
   5. FOCUS STATES (Accessibility)
   ============================================ */
:focus {
  outline: none;
}

:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* ============================================
   6. TEXT SELECTION
   ============================================ */
::selection {
  background-color: var(--primary-light);
  color: var(--primary-900);
}

[data-theme="dark"] ::selection {
  background-color: var(--primary-700);
  color: var(--primary-50);
}

/* ============================================
   7. SCROLLBAR (Webkit)
   ============================================ */
::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

::-webkit-scrollbar-track {
  background: var(--bg-subtle);
}

::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: var(--radius-full);
  border: 2px solid var(--bg-subtle);
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-subtle);
}

/* Firefox */
* {
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) var(--bg-subtle);
}

/* ============================================
   8. 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;
  }
}

/* ============================================
   9. THEME TRANSITION
   ============================================ */
body.theme-transitioning,
body.theme-transitioning *,
body.theme-transitioning *::before,
body.theme-transitioning *::after {
  transition: 
    background-color var(--transition-slow),
    border-color var(--transition-slow),
    color var(--transition-slow),
    box-shadow var(--transition-slow) !important;
}

/* ============================================
   10. UTILITY CLASSES
   ============================================ */
.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;
}

.truncate {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.no-select {
  user-select: none;
  -webkit-user-select: none;
}

.cursor-pointer { cursor: pointer; }
.cursor-not-allowed { cursor: not-allowed; }

/* Text colors */
.text-primary { color: var(--primary); }
.text-accent { color: var(--accent); }
.text-success { color: var(--success); }
.text-warning { color: var(--warning); }
.text-error { color: var(--error); }
.text-info { color: var(--info); }
.text-muted { color: var(--text-muted); }
.text-secondary { color: var(--text-secondary); }

/* Background colors */
.bg-primary { background-color: var(--primary); }
.bg-accent { background-color: var(--accent); }
.bg-success { background-color: var(--success); }
.bg-warning { background-color: var(--warning); }
.bg-error { background-color: var(--error); }
.bg-info { background-color: var(--info); }
.bg-surface { background-color: var(--surface); }
.bg-muted { background-color: var(--bg-muted); }

/* Font weights */
.font-normal { font-weight: var(--font-normal); }
.font-medium { font-weight: var(--font-medium); }
.font-semibold { font-weight: var(--font-semibold); }
.font-bold { font-weight: var(--font-bold); }

/* Text alignment */
.text-left { text-align: left; }
.text-center { text-align: center; }
.text-right { text-align: right; }

/* Display */
.block { display: block; }
.inline-block { display: inline-block; }
.inline { display: inline; }
.flex { display: flex; }
.inline-flex { display: inline-flex; }
.grid { display: grid; }
.hidden { display: none; }

/* Flex utilities */
.items-center { align-items: center; }
.items-start { align-items: flex-start; }
.items-end { align-items: flex-end; }
.justify-center { justify-content: center; }
.justify-between { justify-content: space-between; }
.justify-start { justify-content: flex-start; }
.justify-end { justify-content: flex-end; }
.flex-col { flex-direction: column; }
.flex-row { flex-direction: row; }
.flex-wrap { flex-wrap: wrap; }
.flex-1 { flex: 1 1 0%; }
.gap-1 { gap: var(--space-1); }
.gap-2 { gap: var(--space-2); }
.gap-3 { gap: var(--space-3); }
.gap-4 { gap: var(--space-4); }
.gap-6 { gap: var(--space-6); }
.gap-8 { gap: var(--space-8); }

/* Spacing utilities */
.p-0 { padding: 0; }
.p-2 { padding: var(--space-2); }
.p-3 { padding: var(--space-3); }
.p-4 { padding: var(--space-4); }
.p-6 { padding: var(--space-6); }
.p-8 { padding: var(--space-8); }

.m-0 { margin: 0; }
.m-2 { margin: var(--space-2); }
.m-3 { margin: var(--space-3); }
.m-4 { margin: var(--space-4); }
.m-6 { margin: var(--space-6); }
.m-8 { margin: var(--space-8); }

.mt-0 { margin-top: 0; }
.mt-2 { margin-top: var(--space-2); }
.mt-4 { margin-top: var(--space-4); }
.mt-6 { margin-top: var(--space-6); }
.mt-8 { margin-top: var(--space-8); }

.mb-0 { margin-bottom: 0; }
.mb-2 { margin-bottom: var(--space-2); }
.mb-4 { margin-bottom: var(--space-4); }
.mb-6 { margin-bottom: var(--space-6); }
.mb-8 { margin-bottom: var(--space-8); }

/* Width */
.w-full { width: 100%; }
.max-w-sm { max-width: 384px; }
.max-w-md { max-width: 512px; }
.max-w-lg { max-width: 640px; }
.max-w-xl { max-width: 768px; }
.max-w-2xl { max-width: 1024px; }
.max-w-3xl { max-width: 1280px; }
.max-w-4xl { max-width: 1536px; }
.mx-auto { margin-left: auto; margin-right: auto; }

/* Border radius */
.rounded-none { border-radius: var(--radius-none); }
.rounded-sm { border-radius: var(--radius-sm); }
.rounded-md { border-radius: var(--radius-md); }
.rounded-lg { border-radius: var(--radius-lg); }
.rounded-xl { border-radius: var(--radius-xl); }
.rounded-full { border-radius: var(--radius-full); }

/* Shadows */
.shadow-none { box-shadow: none; }
.shadow-xs { box-shadow: var(--shadow-xs); }
.shadow-sm { box-shadow: var(--shadow-sm); }
.shadow-md { box-shadow: var(--shadow-md); }
.shadow-lg { box-shadow: var(--shadow-lg); }
.shadow-xl { box-shadow: var(--shadow-xl); }

/* Overflow */
.overflow-hidden { overflow: hidden; }
.overflow-auto { overflow: auto; }
.overflow-x-auto { overflow-x: auto; }
.overflow-y-auto { overflow-y: auto; }

/* Position */
.relative { position: relative; }
.absolute { position: absolute; }
.fixed { position: fixed; }
.sticky { position: sticky; }

/* Z-index */
.z-0 { z-index: var(--z-0); }
.z-10 { z-index: var(--z-10); }
.z-20 { z-index: var(--z-20); }
.z-30 { z-index: var(--z-30); }
.z-40 { z-index: var(--z-40); }
.z-50 { z-index: var(--z-50); }