/**
 * KaTeX Unified Styles v1.0
 * Production-grade formula rendering styles for all MrCalculator calculators
 * Mobile-first, responsive, accessible
 *
 * Consolidates previous katex-simple.css and katex-responsive.css
 * Resolves overflow conflicts and provides consistent styling
 *
 * @author MrCalculator Team
 * @license MIT
 */

/* ===================================================================
   BASE FORMULA CONTAINERS
   ================================================================ */
.formula-container {
  background: linear-gradient(135deg, #f0f7ff 0%, #e0f2fe 100%);
  border: 2px solid #0ea5e9;
  border-radius: 16px;
  padding: 2rem;
  margin: 2.5rem 0;
  box-shadow: 0 8px 25px rgba(14, 165, 233, 0.15);
  width: 100%;
  box-sizing: border-box;
}

.formula-grid {
  display: grid !important;
  grid-template-columns: 1fr !important; /* Mobile first: 1 column */
  gap: 1.5rem !important;
  margin-top: 1.5rem;
  width: 100% !important;
  max-width: 100% !important;
}

/* FIXED: Max 2 columns on tablet and desktop - prevents formula cards from being too narrow */
@media (min-width: 768px) {
  .formula-grid {
    grid-template-columns: repeat(2, 1fr) !important; /* MAX 2 columns */
  }
}

@media (min-width: 1024px) {
  .formula-grid {
    grid-template-columns: repeat(2, 1fr) !important; /* STILL MAX 2 columns - not auto-fit */
  }
}

.formula-item {
  background: white;
  border: 1px solid #0ea5e9;
  border-radius: 12px;
  padding: 1.5rem;
  text-align: center;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
  width: 100% !important;
  max-width: 100% !important;
  box-sizing: border-box !important;
  overflow: visible; /* Allow content to be visible on desktop */
}

.formula-item:hover {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(14, 165, 233, 0.2);
}

.formula-math,
[data-katex] {
  margin: 1rem 0;
  padding: 0.5rem;
  text-align: center;
  background: rgba(255, 255, 255, 0.9);
  border-radius: 8px;
  border: 1px solid rgba(14, 165, 233, 0.2);
  max-width: 100%;
  position: relative;
  display: block;
  width: 100%;
  box-sizing: border-box;

  /* Desktop: no horizontal scroll */
  overflow: visible;
  -webkit-overflow-scrolling: touch;
}

/* ===================================================================
   KATEX CORE STYLES
   ================================================================ */
.katex {
  font-size: 1.4em;
  color: #1e293b;
  line-height: 1.5;
  white-space: normal;
  word-wrap: break-word;
  display: inline-block;
  max-width: 100%;
}

.katex-display {
  margin: 0.5rem 0;
  text-align: center;
  overflow: visible;
  padding: 0.5rem;
}

.katex-html {
  overflow: visible;
  max-width: 100%;
}

/* Inline formulas in text */
.katex:not(.katex-display) {
  font-size: 1em;
  vertical-align: middle;
}

/* ===================================================================
   RESPONSIVE BREAKPOINTS
   ================================================================ */

/* Desktop (>= 1024px) */
@media (min-width: 1024px) {
  .katex {
    font-size: 1.4em;
    white-space: nowrap;
  }

  /* FIXED: Allow horizontal scroll on desktop too for long formulas */
  .formula-math,
  [data-katex],
  .formula-display {
    padding: 1rem 0.5rem;
    overflow-x: auto !important;
    overflow-y: visible !important;
    max-width: 100% !important;
  }

  .katex-display {
    transform-origin: center center;
  }

  .katex-html {
    display: inline-block;
    min-width: min-content;
  }
}

/* Tablet (768px - 1023px) */
@media (min-width: 768px) and (max-width: 1023px) {
  .katex {
    font-size: 1.35em;
    white-space: nowrap;
  }

  /* FIXED: Allow horizontal scroll on tablet too */
  .formula-math,
  [data-katex],
  .formula-display {
    padding: 1rem 0.75rem;
    overflow-x: auto !important;
    overflow-y: visible !important;
    max-width: 100% !important;
  }

  .katex-display,
  .katex-html {
    display: inline-block;
    min-width: min-content;
  }
}

/* Mobile (<= 767px) - Touch-optimized */
@media (max-width: 767px) {
  .formula-container {
    padding: 0.5rem !important;
    margin: 1rem 0 !important;
    border-width: 1.5px;
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
    overflow: visible !important; /* Let formula-items handle their own overflow */
  }

  .formula-grid {
    grid-template-columns: 1fr !important;
    gap: 0.75rem !important;
    width: 100% !important;
  }

  .formula-item {
    padding: 0.5rem !important;
    width: 100% !important;
    max-width: 100% !important;
    overflow-x: auto !important; /* Allow horizontal scroll within each card */
    overflow-y: visible !important;
    -webkit-overflow-scrolling: touch !important;
    text-align: center !important;
  }

  /* CRITICAL: Single consistent font size for mobile formulas */
  .katex,
  .formula-grid .katex,
  .formula-item .katex,
  .formula-math .katex,
  [data-katex] .katex {
    font-size: 0.75em !important;
    line-height: 1.4 !important;
    white-space: nowrap !important; /* KaTeX MUST have nowrap */
  }

  .formula-label {
    font-size: 0.8rem !important;
    word-wrap: break-word !important;
    white-space: normal !important;
    text-align: center !important;
    margin-bottom: 0.5rem !important;
  }

  /* Text descriptions under formulas should wrap */
  .formula-item p,
  .formula-item .formula-description {
    white-space: normal !important;
    word-wrap: break-word !important;
    overflow-wrap: break-word !important;
    text-align: center !important;
    font-size: 0.85rem !important;
  }

  /* Enable horizontal scroll on formula containers */
  .formula-math,
  [data-katex],
  .formula-item {
    overflow-x: auto !important;
    overflow-y: visible !important;
    -webkit-overflow-scrolling: touch !important;
    padding: 0.5rem !important;
    max-width: 100% !important;
  }

  /* KaTeX internal elements must allow expansion for scroll */
  .katex-html {
    overflow: visible !important;
    display: inline-block !important;
    min-width: min-content !important;
  }

  /* Ensure inner KaTeX can be wider than container for smooth scroll */
  .formula-math > .katex,
  [data-katex] > .katex {
    display: inline-block;
    min-width: min-content;
  }

  /* Scroll hint gradient for overflowing content */
  .formula-math.has-overflow::after,
  [data-katex].has-overflow::after {
    content: '';
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    width: 20px;
    background: linear-gradient(to right, transparent, rgba(255,255,255,0.95));
    pointer-events: none;
    z-index: 1;
    opacity: 1;
    transition: opacity 0.3s ease;
  }

  /* Subtle scrollbar styling for mobile */
  .formula-math::-webkit-scrollbar,
  [data-katex]::-webkit-scrollbar {
    height: 4px;
  }

  .formula-math::-webkit-scrollbar-track,
  [data-katex]::-webkit-scrollbar-track {
    background: rgba(0,0,0,0.05);
    border-radius: 2px;
  }

  .formula-math::-webkit-scrollbar-thumb,
  [data-katex]::-webkit-scrollbar-thumb {
    background: rgba(0,0,0,0.2);
    border-radius: 2px;
  }
}

/* Small Mobile (<= 480px) */
@media (max-width: 480px) {
  .formula-container {
    padding: 1rem;
    border-width: 1px;
  }

  .formula-item {
    padding: 0.75rem;
  }

  .katex {
    font-size: 1.05em !important;
  }

  .formula-math,
  [data-katex] {
    padding: 0.5rem 0.25rem;
  }
}

/* Extra Small Mobile (<= 360px) */
@media (max-width: 360px) {
  .katex {
    font-size: 1em !important;
  }
}

/* ===================================================================
   FORMULA CONTENT STYLES
   ================================================================ */
.formula-label {
  color: #0c4a6e;
  font-size: 1rem;
  font-weight: 700;
  margin-bottom: 1rem;
  text-transform: uppercase;
  letter-spacing: 0.025em;
}

.formula-title {
  color: #0c4a6e;
  font-size: 1.375rem;
  font-weight: 800;
  margin-bottom: 1.5rem;
  text-align: center;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* ===================================================================
   FORMULA EXAMPLES & SPECIAL CASES
   ================================================================ */
.formula-example {
  background: white;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  padding: 1rem;
  margin: 1rem 0;
  text-align: center;
}

.formula-example .formula-math {
  margin: 0;
  background: transparent;
  border: none;
  padding: 0.5rem 0;
}

/* Formulas in tables */
td .katex,
th .katex {
  font-size: 0.9em;
}

/* ===================================================================
   ERROR STATES
   ================================================================ */
.formula-error {
  color: #dc2626;
  font-family: 'Courier New', monospace;
  font-size: 0.9em;
  padding: 0.5rem;
  background: #fef2f2;
  border: 1px solid #fecaca;
  border-radius: 4px;
  display: inline-block;
  max-width: 100%;
  word-break: break-all;
}

.katex-fallback {
  opacity: 0.9;
}

.katex-error {
  border: 1px dashed #dc2626;
  background: #fef2f2;
}

/* ===================================================================
   PRINT STYLES
   ================================================================ */
@media print {
  .formula-math,
  [data-katex] {
    overflow: visible !important;
    max-width: 100%;
  }

  .katex {
    font-size: 1em !important;
  }

  .formula-container {
    background: white;
    border: 1px solid #000;
    box-shadow: none;
  }

  /* Hide scroll hints in print */
  .formula-math::after,
  [data-katex]::after {
    display: none !important;
  }
}

/* ===================================================================
   ACCESSIBILITY
   ================================================================ */
@media (prefers-contrast: high) {
  .katex {
    font-weight: 500;
  }

  .formula-container {
    border-width: 3px;
  }

  .formula-item {
    border-width: 2px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .formula-item {
    transition: none;
  }

  .formula-math.has-overflow::after,
  [data-katex].has-overflow::after {
    transition: none;
  }
}

/* Focus styles for keyboard navigation */
.formula-math:focus,
[data-katex]:focus {
  outline: 2px solid #0ea5e9;
  outline-offset: 2px;
}

/* ===================================================================
   LOADING STATE
   ================================================================ */
[data-katex]:not(.katex-rendered) {
  color: #64748b;
  font-style: italic;
  min-height: 2em;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* ===================================================================
   SCROLLBAR STYLING
   ================================================================ */
/* FIXED: Show subtle scrollbars on ALL screen sizes when content overflows */
.formula-math::-webkit-scrollbar,
.formula-display::-webkit-scrollbar,
[data-katex]::-webkit-scrollbar {
  height: 6px;
}

.formula-math::-webkit-scrollbar-track,
.formula-display::-webkit-scrollbar-track,
[data-katex]::-webkit-scrollbar-track {
  background: rgba(0,0,0,0.05);
  border-radius: 3px;
}

.formula-math::-webkit-scrollbar-thumb,
.formula-display::-webkit-scrollbar-thumb,
[data-katex]::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,0.15);
  border-radius: 3px;
}

.formula-math::-webkit-scrollbar-thumb:hover,
.formula-display::-webkit-scrollbar-thumb:hover,
[data-katex]::-webkit-scrollbar-thumb:hover {
  background: rgba(0,0,0,0.25);
}

/* Firefox scrollbar styling */
.formula-math,
.formula-display,
[data-katex] {
  scrollbar-width: thin;
  scrollbar-color: rgba(0,0,0,0.15) rgba(0,0,0,0.05);
}

/* ===================================================================
   GLOBAL MOBILE TEXT FIX - CRITICAL
   Prevents content from overflowing and being cut off on mobile.
   This section MUST be at the end to override all other CSS.
   ================================================================ */
@media (max-width: 768px) {
  /* Force all containers to respect viewport width */
  .calculator-wrapper,
  .calculator-container,
  .educational-content,
  .content-section,
  .content-text,
  .advanced-content,
  .topic-card,
  .concept-card,
  .example-card,
  .tip-card,
  .mistake-card,
  .feature-card,
  .industry-card,
  .formula-example,
  .type-card,
  .step-card,
  .application-card,
  .method-card,
  .insight-card,
  .quick-tip,
  .pro-tip,
  .warning-box,
  .info-box,
  .success-box {
    max-width: 100% !important;
    width: 100% !important;
    box-sizing: border-box !important;
    overflow-x: hidden !important;
  }

  /* Force text elements to wrap properly - NOT cut off */
  /* CRITICAL: Do NOT include span or div - breaks KaTeX! */
  p, h1, h2, h3, h4, h5, h6, li,
  .subsection-title,
  .section-title,
  .card-title,
  .content-text p,
  .educational-content p,
  .content-section p {
    max-width: 100% !important;
    box-sizing: border-box !important;
    word-wrap: break-word !important;
    overflow-wrap: break-word !important;
    hyphens: auto !important;
    white-space: normal !important;
  }

  /* Protect ALL KaTeX elements - they MUST have nowrap */
  .katex,
  .katex *,
  .katex-html,
  .katex-html *,
  .katex-display,
  .katex-display * {
    white-space: nowrap !important;
  }

  /* But formulas should still allow horizontal scroll */
  .formula-math,
  [data-katex],
  .type-formula,
  .topic-formula,
  .concept-formula,
  .step-formula {
    overflow-x: auto !important;
    overflow-y: hidden !important;
    -webkit-overflow-scrolling: touch !important;
    max-width: 100% !important;
    box-sizing: border-box !important;
  }

  /* Allow KaTeX formulas to be wider than container for scrolling */
  .formula-math .katex,
  [data-katex] .katex,
  .type-formula .katex,
  .topic-formula .katex,
  .concept-formula .katex,
  .step-formula .katex {
    display: inline-block !important;
    min-width: min-content !important;
    white-space: nowrap !important;
  }

  /* Reduce padding to maximize content space on mobile */
  .content-section {
    padding: 0.75rem !important;
    margin: 0.5rem 0 !important;
  }

  /* Cards should have modest padding */
  .topic-card,
  .concept-card,
  .example-card,
  .tip-card,
  .mistake-card,
  .feature-card,
  .industry-card,
  .type-card,
  .step-card {
    padding: 1rem !important;
    margin: 0.5rem 0 !important;
  }
}

/* Extra small mobile - even tighter */
@media (max-width: 480px) {
  .content-section {
    padding: 0.5rem !important;
  }

  .topic-card,
  .concept-card,
  .example-card,
  .tip-card,
  .mistake-card,
  .feature-card,
  .industry-card,
  .type-card,
  .step-card {
    padding: 0.75rem !important;
  }
}

/* ===================================================================
   UNIVERSAL FORMULA GRID FIX - MATCHES ALL PREFIXED CLASSES

   ALWAYS SINGLE COLUMN - formulas need full width to display properly
   No 2-column layouts - they cause formulas to be cut off

   MUST BE AT END OF FILE to override component-specific styles
   ================================================================ */

/* Target ALL formula grids - ALWAYS single column */
[class*="-formula-grid"],
[class*="-formula-section"] .formula-grid,
.formula-grid {
  display: flex !important;
  flex-direction: column !important;
  gap: 1rem !important;
  width: 100% !important;
  max-width: 100% !important;
  box-sizing: border-box !important;
}

/* Target ALL formula cards with any prefix */
[class*="-formula-card"],
[class*="-formula-item"],
.formula-card,
.formula-item {
  background: white !important;
  border-radius: 12px !important;
  padding: 1.5rem 2rem !important;
  width: 100% !important;
  max-width: 100% !important;
  box-sizing: border-box !important;
  text-align: center !important;
}

/* The formula display INSIDE cards */
[class*="-formula-card"] [data-katex],
[class*="-formula-card"] .formula-display,
[class*="-formula-card"] .formula-math,
[class*="-formula-item"] [data-katex],
.formula-card [data-katex],
.formula-item [data-katex] {
  display: block !important;
  width: 100% !important;
  padding: 1rem 0.5rem !important;
  /* TALL ENOUGH - no vertical scroll ever */
  min-height: 5rem !important;
  /* Horizontal scroll only */
  overflow-x: auto !important;
  overflow-y: hidden !important;
  text-align: center !important;
}

/* KaTeX content - let it be natural width */
[class*="-formula-card"] .katex,
[class*="-formula-item"] .katex,
.formula-card .katex,
.formula-item .katex {
  display: inline-block !important;
  font-size: 1.1em !important;
}

/* Formula labels with any prefix */
[class*="-formula-label"],
[class*="-formula-badge"],
.formula-label {
  font-weight: 700 !important;
  font-size: 0.85rem !important;
  margin-bottom: 0.75rem !important;
  text-align: center !important;
  display: inline-block !important;
}

/* Formula containers with any prefix */
[class*="-formula-container"],
[class*="-formula-section"],
.formula-container {
  width: 100% !important;
  max-width: 100% !important;
  box-sizing: border-box !important;
  margin: 1.5rem 0 !important;
}

/* Scrollbar styling - subtle, only shows when needed */
[class*="-formula-card"] [data-katex]::-webkit-scrollbar,
[class*="-formula-item"] [data-katex]::-webkit-scrollbar {
  height: 4px;
}

[class*="-formula-card"] [data-katex]::-webkit-scrollbar-track,
[class*="-formula-item"] [data-katex]::-webkit-scrollbar-track {
  background: transparent;
}

[class*="-formula-card"] [data-katex]::-webkit-scrollbar-thumb,
[class*="-formula-item"] [data-katex]::-webkit-scrollbar-thumb {
  background: rgba(0,0,0,0.1);
  border-radius: 2px;
}

/* Mobile adjustments */
@media (max-width: 767px) {
  [class*="-formula-grid"],
  .formula-grid {
    gap: 0.75rem !important;
  }

  [class*="-formula-card"],
  [class*="-formula-item"],
  .formula-card,
  .formula-item {
    padding: 0.75rem !important;
  }

  /* Formula display - TALL on mobile too */
  [class*="-formula-card"] [data-katex],
  [class*="-formula-card"] .formula-display,
  [class*="-formula-item"] [data-katex],
  .formula-card [data-katex],
  .formula-item [data-katex] {
    min-height: 4rem !important;
    padding: 0.75rem 0.5rem !important;
    overflow-y: hidden !important;
  }

  /* Shrink KaTeX on mobile */
  [class*="-formula-card"] .katex,
  [class*="-formula-item"] .katex,
  .formula-card .katex,
  .formula-item .katex,
  .formula-math .katex,
  [data-katex] .katex {
    font-size: 0.8em !important;
  }
}

/* Extra small mobile */
@media (max-width: 400px) {
  [class*="-formula-card"] .katex,
  [class*="-formula-item"] .katex,
  .formula-card .katex,
  .formula-item .katex,
  .formula-math .katex,
  [data-katex] .katex {
    font-size: 0.65em !important;
  }
}
