/*
  Rippli Japandi Design Tokens v1.0
  Token-first framework for Flask + Bootstrap 5 + D3/Cytoscape
  Muted sage palette with warm neutrals
*/

/* ---------------------------------------------------------------
   1) Base Palette (Japandi)
--------------------------------------------------------------- */
:root {
  /* Typography */
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
  --font-mono: "SF Mono", Monaco, "Cascadia Code", "Courier New", monospace;

  /* Warm neutrals */
  --neutral-0: hsl(0 0% 100%);
  --neutral-50: hsl(40 20% 98%);
  --neutral-100: hsl(35 15% 94%);
  --neutral-200: hsl(30 10% 88%);
  --neutral-300: hsl(30 8% 78%);
  --neutral-400: hsl(28 6% 60%);
  --neutral-500: hsl(26 5% 45%);
  --neutral-600: hsl(25 6% 35%);
  --neutral-700: hsl(24 8% 25%);
  --neutral-800: hsl(22 10% 18%);
  --neutral-900: hsl(20 12% 10%);

  /* Muted sage accent */
  --accent-50: hsl(135 30% 96%);
  --accent-100: hsl(135 25% 90%);
  --accent-200: hsl(135 22% 80%);
  --accent-300: hsl(135 20% 65%);
  --accent-400: hsl(135 18% 55%);
  --accent-500: hsl(135 18% 45%);
  --accent-600: hsl(135 20% 38%);
  --accent-700: hsl(135 25% 30%);
  --accent-800: hsl(135 28% 22%);
  --accent-900: hsl(135 30% 15%);

  /* Status colors */
  --success-500: hsl(155 35% 40%);
  --warning-500: hsl(43 85% 50%);
  --error-500: hsl(0 75% 50%);
}

/* ---------------------------------------------------------------
   2) Semantic Tokens (Light Theme Default)
--------------------------------------------------------------- */
:root {
  /* Surfaces & backgrounds */
  --color-bg: var(--neutral-0);
  --color-bg-muted: var(--neutral-50);
  --color-surface: var(--neutral-0);
  --color-surface-alt: var(--neutral-50);
  --color-surface-tertiary: var(--neutral-100);
  --color-navbar-bg: hsl(0 0% 98%);  /* #F9F9F9 equivalent in light mode */
  
  /* Text */
  --color-text: var(--neutral-900);
  --color-text-muted: var(--neutral-600);
  --color-text-tertiary: var(--neutral-400);
  --color-border: var(--neutral-200);
  
  /* Accent/Interactive */
  --color-accent: var(--accent-500);
  --color-accent-contrast: var(--neutral-0);
  --color-accent-hover: var(--accent-600);
  
  /* Legacy compatibility (maps old tokens to new) */
  --color-text-primary: var(--color-text);
  --color-text-secondary: var(--color-text-muted);
  --color-background-primary: var(--color-bg);
  --color-background-secondary: var(--color-bg-muted);
  --color-surface-primary: var(--color-surface);
  --color-surface-secondary: var(--color-surface-alt);
  --color-border-primary: var(--color-border);
  --color-border-secondary: var(--neutral-300);
  --color-interactive-primary: var(--color-accent);
  --color-interactive-primary-hover: var(--color-accent-hover);
  --color-interactive-hover: var(--color-accent-hover);
  --color-interactive-primary-subtle: color-mix(in srgb, var(--color-interactive-primary) 10%, transparent);
  
  /* Status (semantic) */
  --color-success: var(--success-500);
  --color-success-bg: hsl(155 35% 95%);
  --color-success-subtle: hsl(155 35% 95%);
  --color-warning: var(--warning-500);
  --color-warning-bg: hsl(43 85% 95%);
  --color-warning-subtle: hsl(43 85% 95%);
  --color-danger: var(--error-500);
  --color-danger-bg: hsl(0 75% 95%);
  --color-danger-subtle: hsl(0 75% 95%);
  --color-info: var(--neutral-500);
  --color-info-bg: var(--neutral-100);
  --color-muted: var(--neutral-400);
  --color-muted-bg: var(--neutral-50);
  
  /* Sentiment Colors (for gauges, visualizations) */
  --color-sentiment-negative: #960303;  /* Deep red — signals risk or pessimism */
  --color-sentiment-neutral: #E98C3D;   /* Orange — balanced or uncertain tone */
  --color-sentiment-positive: #3A8F5E;  /* Deep green — confident, bullish sentiment */

  /* Entity Highlight Colors */
  --color-entity-company: var(--color-interactive-primary);  /* Sage green for companies */
  --color-entity-company-subtle: var(--color-interactive-primary-subtle);
  --color-entity-person: #94b89d;  /* Sage green for people */
  --color-entity-person-subtle: color-mix(in srgb, var(--color-entity-person) 10%, transparent);

  /* Elevation/Shadows */
  --shadow-xs: 0 1px 2px rgba(0, 0, 0, .04), 0 1px 1px rgba(0, 0, 0, .03);
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, .06), 0 1px 1px rgba(0, 0, 0, .04);
  --shadow-md: 0 2px 6px rgba(0, 0, 0, .08), 0 1px 2px rgba(0, 0, 0, .06);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, .12), 0 2px 6px rgba(0, 0, 0, .08);
  --shadow-xl: 0 20px 25px -5px rgba(0, 0, 0, .1);
  --shadow-card: 0 2px 6px rgba(0, 0, 0, .05), 0 8px 16px rgba(0, 0, 0, .04);
  
  /* Overlays */
  --overlay-75: color-mix(in srgb, var(--neutral-900) 75%, transparent);
  --overlay-30: color-mix(in srgb, var(--neutral-900) 30%, transparent);
  
  /* Modal Overlays */
  --modal-backdrop: color-mix(in srgb, var(--neutral-900) 65%, transparent);
  --modal-backdrop-blur: 8px;

  /* Focus */
  --focus-ring: 2px solid color-mix(in oklab, var(--color-accent), white 20%);
  --focus-offset: 2px;

  /* Curved accent indicators (tabs, callouts) */
  --accent-line: color-mix(in oklab, var(--color-accent), white 15%);
  --accent-line-shadow: 0 -1px 4px color-mix(in oklab, var(--color-accent), transparent 70%);

  /* Signature gradients */
  --btn-accent-gradient: linear-gradient(
    to bottom,
    color-mix(in oklab, var(--color-accent) 96%, white 6%),
    color-mix(in oklab, var(--color-accent) 90%, black 4%)
  );
  --gradient-avatar: linear-gradient(
    145deg,
    color-mix(in oklab, var(--accent-400), white 10%) 0%,
    color-mix(in oklab, var(--accent-700), black 5%) 100%
  );

  /* Typography */
  --font-body: -apple-system, BlinkMacSystemFont, "SF Pro Display", "SF Pro Text", system-ui, "Segoe UI", Roboto, Arial, sans-serif;
  --font-serif: "Noto Serif JP", "Libre Baskerville", Georgia, serif;
  --font-size-xs: 0.75rem;
  --font-size-sm: 0.875rem;
  --font-size-base: 1rem;
  --font-size-lg: 1.125rem;
  --font-size-xlg: 1.25rem;
  --font-size-xl: 1.5rem;
  --font-size-2xl: 2rem;
  --font-size-3xl: 2.5rem;
  --font-weight-normal: 400;
  --font-weight-medium: 500;
  --font-weight-semibold: 600;
  --font-weight-bold: 700;
  --lh-body: 1.5;
  --lh-heading: 1.3;

  /* Spacing System */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --space-7: 48px;
  --space-8: 64px;
  --space-10: 80px;
  --space-12: 96px;
  --space-16: 128px;

  /* Border Radius */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 16px;
  --radius-xl: 24px;
  --radius-2xl: 32px;
  --radius-full: 50%;

  /* Motion */
  --motion-dur-quick: 120ms;
  --motion-dur-std: 200ms;
  --motion-dur-slow: 300ms;
  --motion-ease: cubic-bezier(.2, .8, .2, 1);
  --transition-base: all 0.2s cubic-bezier(.2, .8, .2, 1);
  --transition-standard: var(--transition-base); /* Alias for backward compatibility */

  /* Component-specific tokens */
  --card-background: var(--color-surface);
  --card-border: var(--color-border);
  --card-radius: var(--radius-lg);
  --button-radius: var(--radius-md);
  --btn-border-radius: var(--radius-md);
  --input-radius: var(--radius-sm);

  /* Layout Dimensions */
  --sidebar-width: 200px;
  --sidebar-collapsed-width: 60px;
  --header-height: 70px;

  /* Responsive Breakpoints */
  --breakpoint-xs: 480px;
  --breakpoint-sm: 640px;
  --breakpoint-md: 768px;
  --breakpoint-lg: 1024px;
  --breakpoint-xl: 1280px;

  /* Z-index Scale */
  --z-sidebar: 1000;
  --z-header: 1002;
  --z-dropdown: 1050;
  --z-suggestions: 1100;
  --z-modal-backdrop: 1199;
  --z-modal: 1200;
  --z-overlay: 1001;

  /* Data Visualization */
  --viz-node: var(--accent-400);
  --viz-node-highlight: var(--accent-600);
  --viz-edge: var(--color-border);
  --viz-label: var(--color-text);
  --viz-fill: color-mix(in oklab, var(--accent-400), transparent 85%);

  /* Sentiment Gauge */
  --gauge-bg: var(--color-bg-muted);
  --gauge-progress: var(--accent-400);
  
  /* AI Glow (for highlighted keywords/content) */
  --glow-color: hsl(135 65% 55% / 0.9);
  --glow-soft: hsl(135 65% 55% / 0.35);
}

/* ---------------------------------------------------------------
   3) Dark Theme
--------------------------------------------------------------- */
[data-theme="dark"] {
  /* Base overrides */
  --color-bg: var(--neutral-900);
  --color-bg-muted: hsl(20 8% 14%);
  --color-surface: hsl(20 8% 16%);
  --color-surface-alt: hsl(20 8% 19%);
  --color-surface-tertiary: hsl(20 8% 22%);
  --color-navbar-bg: hsl(20 8% 12%);
  --color-text: hsl(0 0% 96%);
  --color-text-muted: hsl(0 0% 75%);
  --color-text-tertiary: hsl(0 0% 60%);
  --color-border: hsl(20 8% 28%);
  --color-accent: var(--accent-300);
  --color-accent-hover: var(--accent-200);
  
  /* Modal backdrop for dark theme */
  --modal-backdrop: color-mix(in srgb, var(--neutral-900) 75%, transparent);
  
  /* Legacy compatibility */
  --color-text-primary: var(--color-text);
  --color-text-secondary: var(--color-text-muted);
  --color-background-primary: var(--color-bg);
  --color-background-secondary: var(--color-bg-muted);
  --color-surface-primary: var(--color-surface);
  --color-surface-secondary: var(--color-surface-alt);
  --color-border-primary: var(--color-border);
  --color-border-secondary: hsl(20 8% 35%);
  --color-interactive-primary: var(--color-accent);
  --color-interactive-primary-hover: var(--color-accent-hover);
  --color-interactive-hover: var(--color-accent-hover);
  --color-interactive-primary-subtle: color-mix(in srgb, var(--color-interactive-primary) 10%, transparent);
  
  /* Component-specific tokens for dark mode */
  --card-background: var(--color-surface);
  --card-border: var(--color-border);

  /* Shadows adjusted for dark */
  --shadow-card: 0 2px 8px rgba(0, 0, 0, .35);
  --shadow-md: 0 4px 12px rgba(0, 0, 0, .3);
  --shadow-lg: 0 12px 32px rgba(0, 0, 0, .4);

  /* Gradient adjustments */
  --gradient-avatar: linear-gradient(
    145deg,
    color-mix(in oklab, var(--accent-300), white 4%) 0%,
    color-mix(in oklab, var(--accent-800), black 10%) 100%
  );
  
  /* Status backgrounds for dark */
  --color-success-bg: hsl(155 35% 15%);
  --color-success-subtle: hsl(155 35% 15%);
  --color-warning-bg: hsl(43 85% 15%);
  --color-warning-subtle: hsl(43 85% 15%);
  --color-danger-bg: hsl(0 75% 15%);
  --color-danger-subtle: hsl(0 75% 15%);
  --color-info-bg: hsl(20 8% 20%);
  --color-muted-bg: var(--color-bg-muted);
}

/* ---------------------------------------------------------------
   4) Bootstrap Variable Mapping (Auto-theme Bootstrap)
--------------------------------------------------------------- */
:root {
  --bs-body-bg: var(--color-bg);
  --bs-body-color: var(--color-text);
  --bs-border-color: var(--color-border);
  --bs-tertiary-bg: var(--color-bg-muted);
  --bs-secondary-bg: var(--color-surface-alt);
  --bs-light-bg-subtle: var(--color-bg-muted);
  --bs-dark-bg-subtle: var(--color-surface-tertiary);
  --bs-primary: var(--color-accent);
  --bs-success: var(--success-500);
  --bs-warning: var(--warning-500);
  --bs-danger: var(--error-500);
  --bs-link-color: var(--color-accent);
  --bs-link-hover-color: var(--color-accent-hover);
}

[data-theme="dark"] {
  --bs-tertiary-bg: hsl(20 8% 14%);
  --bs-secondary-bg: hsl(20 8% 16%);
  --bs-light-bg-subtle: hsl(20 8% 16%);
  --bs-dark-bg-subtle: hsl(20 8% 12%);
}

/* Base Styles - Normalize page background */
html, body {
  background-color: var(--color-bg);
  color: var(--color-text);
}

/* Token-safe background utilities (replace Bootstrap bg- classes) */
.bg-page { background: var(--color-bg) !important; }
.bg-surface { background: var(--color-surface) !important; }
.bg-muted { background: var(--color-bg-muted) !important; }
.bg-navbar { background: var(--color-navbar-bg) !important; }

/* ---------------------------------------------------------------
   5) Component Classes (Japandi Styled)
--------------------------------------------------------------- */

/* Accent Gradient Button */
.btn-accent {
  background: var(--btn-accent-gradient);
  color: var(--color-accent-contrast);
  border: none;
  border-radius: 12px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .08);
  transition: filter var(--motion-dur-std) var(--motion-ease);
}
.btn-accent:hover {
  filter: brightness(0.98);
  color: var(--color-accent-contrast);
}

/* Japandi Card */
.card-jp {
  background: var(--color-surface);
  color: var(--color-text);
  border: 1px solid var(--color-border);
  border-radius: 16px;
  box-shadow: var(--shadow-card);
  transition: background var(--motion-dur-std), border-color var(--motion-dur-std);
}

/* Tabs with Curved Accent */
.tabs {
  display: flex;
  gap: .25rem;
  border-bottom: 1px solid var(--color-border);
}
.tab {
  position: relative;
  padding: .75rem 1.25rem;
  color: var(--color-text-muted);
  background: transparent;
  border: none;
  font-weight: 500;
  transition: color var(--motion-dur-std);
}
.tab.active {
  color: var(--color-text);
}
.tab.active::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 25%;
  width: 50%;
  height: 3px;
  border-radius: 2px 2px 0 0;
  background: var(--accent-line);
  box-shadow: var(--accent-line-shadow);
}

/* Executive Summary Callout */
.exec-summary {
  border-left: 3px solid var(--accent-line);
  border-radius: 16px;
  background: var(--color-surface);
  box-shadow: var(--shadow-card);
}

/* Sentiment Gauge (Conic) */
.sentiment-gauge {
  --score: 0;
  width: 140px;
  height: 140px;
  border-radius: 50%;
  background: conic-gradient(
    var(--gauge-progress) calc(var(--score) * 1%),
    var(--gauge-bg) 0
  );
  box-shadow: inset 0 0 6px rgba(0, 0, 0, .05);
}

/* Avatar Badge with Gradient */
.avatar-badge {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  background: var(--gradient-avatar);
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: inset 0 1px 2px rgba(255, 255, 255, .1), 0 1px 2px rgba(0, 0, 0, .06);
}
.avatar-initial {
  color: var(--color-bg);
  font-weight: 600;
  font-size: 1rem;
  letter-spacing: .03em;
}

/* Dropdown Menu Theming */
.dropdown-menu {
  --bs-dropdown-bg: var(--color-surface);
  --bs-dropdown-color: var(--color-text);
  --bs-dropdown-border-color: var(--color-border);
  --bs-dropdown-link-hover-bg: color-mix(in oklab, var(--color-accent), transparent 92%);
  --bs-dropdown-link-active-bg: color-mix(in oklab, var(--color-accent), transparent 86%);
  border-radius: 16px;
  box-shadow: var(--shadow-md);
}

/* Form Controls with Focus */
.form-control,
.form-select {
  background-color: var(--color-surface);
  color: var(--color-text);
  border-color: var(--color-border);
  transition: border-color var(--motion-dur-std), box-shadow var(--motion-dur-std);
}
.form-control:focus,
.form-select:focus {
  outline: var(--focus-ring);
  outline-offset: var(--focus-offset);
  border-color: color-mix(in oklab, var(--color-accent), white 20%);
  box-shadow: 0 0 0 3px color-mix(in oklab, var(--color-accent), white 75%);
  background-color: var(--color-surface);
  color: var(--color-text);
}

/* ---------------------------------------------------------------
   6) Body & Global Styles
--------------------------------------------------------------- */
body {
  font-family: var(--font-body);
  line-height: var(--lh-body);
  color: var(--color-text);
  background-color: var(--color-bg);
  transition: color var(--motion-dur-std) ease, background-color var(--motion-dur-std) ease;
}

/* Text Utilities */
.text-muted {
  color: var(--color-text-muted) !important;
}
.text-tertiary {
  color: var(--color-text-tertiary) !important;
}
.text-primary-enhanced {
  color: var(--color-text) !important;
}
.text-secondary-enhanced {
  color: var(--color-text-muted) !important;
}
.text-tertiary-enhanced {
  color: var(--color-text-tertiary) !important;
}

/* Badge Theme Utilities */
.badge-theme-primary {
  background-color: var(--color-accent);
  color: var(--color-accent-contrast);
}
.badge-theme-secondary {
  background-color: var(--color-surface-alt);
  color: var(--color-text);
}

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

/* ---------------------------------------------------------------
   Sonner Toast Tokenization
--------------------------------------------------------------- */
:where([data-sonner-toast][data-styled="true"]) {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  color: var(--color-text);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-md);
}

:where([data-rich-colors="true"][data-sonner-toast][data-type="success"]) {
  background: color-mix(in srgb, var(--color-success) 16%, var(--color-surface));
  border-color: color-mix(in srgb, var(--color-success) 35%, transparent);
}

:where([data-rich-colors="true"][data-sonner-toast][data-type="info"]) {
  background: color-mix(in srgb, hsl(210 92% 45%) 16%, var(--color-surface));
  border-color: color-mix(in srgb, hsl(210 92% 45%) 35%, transparent);
}

:where([data-rich-colors="true"][data-sonner-toast][data-type="warning"]) {
  background: color-mix(in srgb, var(--color-warning) 16%, var(--color-surface));
  border-color: color-mix(in srgb, var(--color-warning) 35%, transparent);
}

:where([data-rich-colors="true"][data-sonner-toast][data-type="error"]) {
  background: color-mix(in srgb, var(--color-danger) 16%, var(--color-surface));
  border-color: color-mix(in srgb, var(--color-danger) 35%, transparent);
}

/* ---------------------------------------------------------------
   AI Glow Animation (for highlighted content)
--------------------------------------------------------------- */
@media (prefers-reduced-motion: no-preference) {
  .ai-glow-in {
    animation: aiGlowIn 900ms cubic-bezier(.4,0,.2,1) both;
  }
}

@keyframes aiGlowIn {
  0% {
    opacity: 0;
    filter: blur(2px);
    text-shadow: 0 0 0 var(--glow-color);
  }
  60% {
    opacity: 1;
    filter: blur(0);
    text-shadow: 0 0 10px var(--glow-soft), 0 0 20px var(--glow-soft);
  }
  100% {
    text-shadow: 0 0 2px var(--glow-soft), 0 0 6px var(--glow-soft);
  }
}

/* ---------------------------------------------------------------
   Global Typography Application
--------------------------------------------------------------- */
body, html {
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

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

/* ---------------------------------------------------------------
   Theme Switching - Instant mode (no transitions during switch)
--------------------------------------------------------------- */
.theme-switching,
.theme-switching * {
  transition: none !important;
}
