/**
 * Article Styles CSS - Comprehensive styling for both editor and exported HTML
 * 
 * 🚨 IMPORTANT: This file contains styles for READER-FACING article components
 * 
 * RULE: Any component visible in published articles MUST have its styles here.
 * See: .cursor/rules/code-style/article-styles-placement.mdc
 * 
 * This file provides:
 * - Typography variable mapping for all HTML elements
 * - Color palette integration via CSS variables
 * - Consistent spacing and layout rules
 * - Responsive design for exported articles
 * - Reader-facing component styles (tables, captions, containers, etc.)
 * 
 * Used by both the TipTap editor and exported HTML files
 * 
 * Selector Pattern:
 * [data-typography] .component,           // Editor with typography system
 * .j-article-studio-output-wrapper .component,  // Exported HTML
 * .ProseMirror .component                 // TipTap editor (all modes)
 */

/* Typography Role Mapping - Core variable-to-element mapping */
[data-typography] h1,
.j-article-studio-output-wrapper h1.j-article-title {
  font-family: var(--title-font, inherit);
  font-size: var(--title-size, inherit);
  line-height: var(--title-lh, inherit);
  font-weight: var(--title-weight, inherit);
  font-style: var(--title-style, inherit);
}

/* H2 = Heading (primary section) */
[data-typography] h2,
.j-article-studio-output-wrapper h2 {
  font-family: var(--heading-font, inherit);
  font-size: var(--heading-size, inherit);
  line-height: var(--heading-lh, inherit);
  font-weight: var(--heading-weight, inherit);
  font-style: var(--heading-style, inherit);
}

/* H3 = Subheading (subsection) */
[data-typography] h3,
.j-article-studio-output-wrapper h3 {
  font-family: var(--subheading-font, inherit);
  font-size: var(--subheading-size, inherit);
  line-height: var(--subheading-lh, inherit);
  font-weight: var(--subheading-weight, inherit);
  font-style: var(--subheading-style, inherit);
}

/* H4 = Section header / Minor heading */
[data-typography] h4,
.j-article-studio-output-wrapper h4 {
  font-family: var(--section-font, inherit);
  font-size: var(--section-size, inherit);
  line-height: var(--section-lh, inherit);
  font-weight: var(--section-weight, inherit);
  font-style: var(--section-style, inherit);
  text-transform: var(--section-case, inherit);
  letter-spacing: var(--section-tracking, inherit);
}

/* Subtitle / Dek lives under the title, not as a heading */
[data-typography] p.dek,
.j-article-studio-output-wrapper p.dek {
  font-family: var(--subtitle-font, inherit);
  font-size: var(--subtitle-size, inherit);
  line-height: var(--subtitle-lh, inherit);
  font-weight: var(--subtitle-weight, inherit);
  font-style: var(--subtitle-style, inherit);
  margin-bottom: 16px; /* keep the rhythm */
}

[data-typography] p,
.j-article-studio-output-wrapper p {
  font-family: var(--body-font, inherit);
  font-size: var(--body-size, inherit);
  line-height: var(--body-lh, inherit);
  font-weight: var(--body-weight, inherit);
  font-style: var(--body-style, inherit);
}

[data-typography] blockquote,
[data-typography] .quote,
.j-article-studio-output-wrapper blockquote,
.j-article-studio-output-wrapper .quote {
  font-family: var(--quote-font, inherit);
  font-size: var(--quote-size, inherit);
  line-height: var(--quote-lh, inherit);
  font-weight: var(--quote-weight, inherit);
  font-style: var(--quote-style, inherit);
}

[data-typography] figcaption,
[data-typography] .caption,
.j-article-studio-output-wrapper figcaption,
.j-article-studio-output-wrapper .caption {
  font-family: var(--caption-font, inherit);
  font-size: var(--caption-size, inherit);
  line-height: var(--caption-lh, inherit);
  font-weight: var(--caption-weight, inherit);
  font-style: var(--caption-style, inherit);
}

/* Color Variable Integration */
/* Base color + background come from the palette */
[data-palette][data-typography],
.j-article-studio-output-wrapper {
  background-color: var(--color-bg);
  color: var(--text-body); /* default running text color */
}

/* Headings should be stronger than running text */
[data-palette] h1,
[data-palette] h2,
[data-palette] h3,
[data-palette] h4,
[data-palette] h5,
.j-article-studio-output-wrapper h1,
.j-article-studio-output-wrapper h2,
.j-article-studio-output-wrapper h3,
.j-article-studio-output-wrapper h4,
.j-article-studio-output-wrapper h5 {
  color: var(--text-foreground);
}

/* Muted/meta */
[data-palette] figcaption,
[data-palette] .text-muted,
.j-article-studio-output-wrapper figcaption,
.j-article-studio-output-wrapper .text-muted {
  color: var(--text-muted);
}

/* Links */
[data-palette] a,
.j-article-studio-output-wrapper a { 
  color: var(--color-primary); 
}
[data-palette] a:hover,
.j-article-studio-output-wrapper a:hover { 
  color: var(--color-secondary); 
}

[data-palette] .text-body,
.j-article-studio-output-wrapper .text-body {
  color: var(--text-body, inherit);
}

[data-palette] .text-muted,
.j-article-studio-output-wrapper .text-muted {
  color: var(--text-muted, inherit);
}

[data-palette] .bg-primary,
.j-article-studio-output-wrapper .bg-primary {
  background-color: var(--color-primary, inherit);
}

[data-palette] .bg-secondary,
.j-article-studio-output-wrapper .bg-secondary {
  background-color: var(--color-secondary, inherit);
}

[data-palette] .border,
[data-palette] .border-border,
.j-article-studio-output-wrapper .border,
.j-article-studio-output-wrapper .border-border {
  border-color: var(--color-border, inherit);
}

/* Element Spacing Rules - Applied regardless of typography styles */
[data-typography] h2,
.j-article-studio-output-wrapper h2 {
  margin-top: 32px;
  margin-bottom: 8px;
}

[data-typography] h3,
.j-article-studio-output-wrapper h3 {
  margin-top: 24px;
  margin-bottom: 8px;
}

[data-typography] h4,
.j-article-studio-output-wrapper h4 {
  margin-top: 16px;
  margin-bottom: 8px;
}

[data-typography] p,
.j-article-studio-output-wrapper p {
  margin-top: 0;
  margin-bottom: 16px;
}

[data-typography] ul,
[data-typography] ol,
.j-article-studio-output-wrapper ul,
.j-article-studio-output-wrapper ol {
  font-family: var(--body-font, 'Inter', sans-serif);
  font-size: var(--body-size, 16px);
  line-height: var(--body-lh, 28px);
  margin: 16px 0;
  padding-left: 24px;
}

[data-typography] li,
.j-article-studio-output-wrapper li {
  margin-bottom: 8px;
}

/* Make list markers inherit text color */
[data-typography] li::marker,
.j-article-studio-output-wrapper li::marker,
.ProseMirror li::marker {
  color: inherit;
}

/* Task list checkboxes inherit text color */
[data-typography] ul[data-type="taskList"] li input[type="checkbox"],
.j-article-studio-output-wrapper ul[data-type="taskList"] li input[type="checkbox"],
.ProseMirror ul[data-type="taskList"] li input[type="checkbox"] {
  accent-color: currentColor;
}

[data-typography] blockquote,
.j-article-studio-output-wrapper blockquote {
  margin-top: 24px;
  margin-bottom: 24px;
  padding-left: 16px;
  border-left: 4px solid var(--blockquote-border-color, #148fff);
  font-style: italic;
}

/* Ensure blockquote border is visible even without palette */
[data-typography]:not([data-palette]) blockquote {
  border-left-color: #148fff; /* Original blue color when no palette is applied */
}

[data-typography] figure,
.j-article-studio-output-wrapper figure {
  margin-top: 24px;
  margin-bottom: 24px;
  text-align: center;
}

/* =============================================================================
   MEDIA IMAGE CAPTION STYLES
   ============================================================================= */

/* Base caption styles - applies to all captions */
[data-typography] .mi-caption,
.j-article-studio-output-wrapper .mi-caption,
.ProseMirror .mi-caption {
  margin: 0;
  padding: 0;
  font-size: 14px;
  line-height: 1.4;
  color: inherit;
  word-wrap: break-word;
  overflow-wrap: break-word;
  hyphens: none;
  white-space: normal;
  box-sizing: border-box;
}

/* Caption below image - default positioning */
[data-typography] .mi-caption--below,
.j-article-studio-output-wrapper .mi-caption--below,
.ProseMirror .mi-caption--below {
  display: block;
  margin-top: 8px;
  padding: 8px 0;
  text-align: left;
  width: 100%;
  max-width: 100%;
  word-wrap: break-word;
  overflow-wrap: break-word;
  white-space: normal;
  box-sizing: border-box;
}

/* Caption overlay on image */
[data-typography] .mi-caption--on,
.j-article-studio-output-wrapper .mi-caption--on,
.ProseMirror .mi-caption--on {
  position: absolute;
  padding: 8px 12px;
  background: rgba(0, 0, 0, 0.7);
  color: white;
  border-radius: 4px;
  font-size: 13px;
  word-wrap: break-word;
  overflow-wrap: break-word;
  white-space: normal;
  box-sizing: border-box;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.8);
}

/* Caption content wrapping */
[data-typography] .mi-caption--below span,
[data-typography] .mi-caption--on span,
.j-article-studio-output-wrapper .mi-caption--below span,
.j-article-studio-output-wrapper .mi-caption--on span,
.ProseMirror .mi-caption--below span,
.ProseMirror .mi-caption--on span {
  display: inline;
  overflow-wrap: break-word;
  white-space: normal;
}

/* Prevent link styles from affecting captions when figure is wrapped in anchor tag */
[data-typography] a .mi-caption,
[data-typography] a .mi-caption--below,
[data-typography] a .mi-caption--on,
.j-article-studio-output-wrapper a .mi-caption,
.j-article-studio-output-wrapper a .mi-caption--below,
.j-article-studio-output-wrapper a .mi-caption--on,
.ProseMirror a .mi-caption,
.ProseMirror a .mi-caption--below,
.ProseMirror a .mi-caption--on {
  color: inherit !important;
  text-decoration: none !important;
}

/* Ensure caption spans inside links also don't inherit link styles */
[data-typography] a .mi-caption span,
[data-typography] a .mi-caption--below span,
[data-typography] a .mi-caption--on span,
.j-article-studio-output-wrapper a .mi-caption span,
.j-article-studio-output-wrapper a .mi-caption--below span,
.j-article-studio-output-wrapper a .mi-caption--on span,
.ProseMirror a .mi-caption span,
.ProseMirror a .mi-caption--below span,
.ProseMirror a .mi-caption--on span {
  color: inherit !important;
  text-decoration: none !important;
}

[data-typography] hr,
.j-article-studio-output-wrapper hr {
  border: none;
  border-top: 1px solid var(--color-border, #ddd);
  margin-top: 24px;
  margin-bottom: 24px;
}

/* Subtitle (dek under title) */
[data-typography] .subtitle,
[data-typography] .dek,
.j-article-studio-output-wrapper .subtitle,
.j-article-studio-output-wrapper .dek {
  margin-bottom: 16px;
}

/* =============================================================================
   EXPORTED ARTICLE STYLES
   Additional styling for exported HTML articles
   ============================================================================= */

/* Enhanced Typography Hierarchy for Exports is unified above via combined selectors */

/* List Styling - Unified above with [data-typography] selectors */

/* Link Styling */
[data-typography] a,
.j-article-studio-output-wrapper a {
  color: var(--color-primary, #0066cc);
  text-decoration: underline;
}

[data-typography] a:hover,
.j-article-studio-output-wrapper a:hover {
  color: var(--color-secondary, #004499);
}

/* Image and Figure Styling */
[data-typography] img,
.j-article-studio-output-wrapper img {
  max-width: 100%;
  height: auto;
  display: block;
  margin: 0 auto;
}

/* figure handled above with spacing rule */

/* Horizontal Rule handled above with spacing and border */

/* Code and Preformatted Text */
[data-typography] code,
.j-article-studio-output-wrapper code {
  font-family: 'Monaco', 'Menlo', 'Consolas', monospace;
  font-size: 0.9em;
  background-color: var(--color-surface, #f5f5f5);
  padding: 2px 4px;
  border-radius: 3px;
}

[data-typography] pre,
.j-article-studio-output-wrapper pre {
  font-family: 'Monaco', 'Menlo', 'Consolas', monospace;
  background-color: var(--color-surface, #f5f5f5);
  padding: 16px;
  border-radius: 8px;
  overflow-x: auto;
  margin: 16px 0;
}

[data-typography] pre code,
.j-article-studio-output-wrapper pre code {
  background: none;
  padding: 0;
}

/* Text Highlighting - Use palette secondary color as default for better contrast */
[data-palette] mark,
[data-palette] .highlight,
.j-article-studio-output-wrapper mark,
.j-article-studio-output-wrapper .highlight {
  background-color: var(--color-secondary, #ffeb3b);
  color: var(--text-foreground, inherit);
  padding: 0 2px;
  border-radius: 2px;
}

/* Highlights with explicit color attribute override the theme default */
/* TipTap inline styles take precedence over CSS variables */

/* Table Wrapper - TipTap creates a wrapper div around tables */
[data-typography] .tableWrapper,
.j-article-studio-output-wrapper .tableWrapper,
.ProseMirror .tableWrapper {
  width: 100% !important;
  max-width: 100% !important;
  overflow-x: auto;
}

/* Table Styling - Always 100% width with percentage-based columns */
[data-typography] table,
.j-article-studio-output-wrapper table,
.ProseMirror table {
  width: 100% !important;
  max-width: 100% !important;
  border-collapse: collapse;
  margin: 16px 0;
  font-family: var(--body-font, 'Inter', sans-serif);
  font-size: var(--body-size, 16px);
  table-layout: fixed;
}

/* Table cells - default border and background styling */
[data-typography] th,
[data-typography] td,
.j-article-studio-output-wrapper th,
.j-article-studio-output-wrapper td,
.ProseMirror th,
.ProseMirror td {
  padding: 8px 12px;
  text-align: left;
  border: 1px solid var(--color-border, #e5e7eb);
  vertical-align: middle;
}

/* Header cells get bolder font weight and background */
[data-typography] th,
.j-article-studio-output-wrapper th,
.ProseMirror th {
  font-weight: var(--heading-weight, 600);
  background-color: var(--color-surface, #f9fafb);
}

/* Palette-aware table styling */
[data-palette] th,
[data-palette] td {
  border-color: var(--color-border, #e5e7eb);
}

[data-palette] th {
  background-color: var(--color-surface, rgba(0, 0, 0, 0.03));
}

/* Table with borders removed - applies border: none to th/td cells */
[data-typography] table[data-border-width="0"] th,
[data-typography] table[data-border-width="0"] td,
.j-article-studio-output-wrapper table[data-border-width="0"] th,
.j-article-studio-output-wrapper table[data-border-width="0"] td,
.ProseMirror table[data-border-width="0"] th,
.ProseMirror table[data-border-width="0"] td,
[data-typography] table[data-borders-hidden="true"] th,
[data-typography] table[data-borders-hidden="true"] td,
.j-article-studio-output-wrapper table[data-borders-hidden="true"] th,
.j-article-studio-output-wrapper table[data-borders-hidden="true"] td,
.ProseMirror table[data-borders-hidden="true"] th,
.ProseMirror table[data-borders-hidden="true"] td {
  border: none !important;
}

/* Inline styles from TableExtension override defaults when present */

/* =============================================================================
   CONTENT CONTAINER STYLES
   Styled content boxes that wrap multiple elements
   ============================================================================= */

/* Base content container styling */
[data-typography] .content-container,
.j-article-studio-output-wrapper .content-container,
.ProseMirror .content-container {
  position: relative;
  margin: 16px 0;
  min-height: 24px;
}

/* Remove bottom margin from last child elements inside content container */
[data-typography] .content-container > *:last-child,
[data-typography] .content-container .content-container__content > *:last-child,
.j-article-studio-output-wrapper .content-container > *:last-child,
.j-article-studio-output-wrapper .content-container .content-container__content > *:last-child,
.ProseMirror .content-container > *:last-child,
.ProseMirror .content-container .content-container__content > *:last-child {
  margin-bottom: 0 !important;
}

/* Specifically target paragraphs as last child */
[data-typography] .content-container p:last-child,
[data-typography] .content-container .content-container__content > p:last-child,
.j-article-studio-output-wrapper .content-container p:last-child,
.j-article-studio-output-wrapper .content-container .content-container__content > p:last-child,
.ProseMirror .content-container p:last-child,
.ProseMirror .content-container .content-container__content > p:last-child {
  margin-bottom: 0 !important;
}

/* Remove top margin from first child elements */
[data-typography] .content-container > *:first-child,
[data-typography] .content-container .content-container__content > *:first-child,
.j-article-studio-output-wrapper .content-container > *:first-child,
.j-article-studio-output-wrapper .content-container .content-container__content > *:first-child,
.ProseMirror .content-container > *:first-child,
.ProseMirror .content-container .content-container__content > *:first-child {
  margin-top: 0 !important;
}

/* Headings as first child - remove top margin */
[data-typography] .content-container h1:first-child,
[data-typography] .content-container h2:first-child,
[data-typography] .content-container h3:first-child,
[data-typography] .content-container h4:first-child,
[data-typography] .content-container h5:first-child,
[data-typography] .content-container h6:first-child,
.j-article-studio-output-wrapper .content-container h1:first-child,
.j-article-studio-output-wrapper .content-container h2:first-child,
.j-article-studio-output-wrapper .content-container h3:first-child,
.j-article-studio-output-wrapper .content-container h4:first-child,
.j-article-studio-output-wrapper .content-container h5:first-child,
.j-article-studio-output-wrapper .content-container h6:first-child,
.ProseMirror .content-container h1:first-child,
.ProseMirror .content-container h2:first-child,
.ProseMirror .content-container h3:first-child,
.ProseMirror .content-container h4:first-child,
.ProseMirror .content-container h5:first-child,
.ProseMirror .content-container h6:first-child {
  margin-top: 0 !important;
}

/* Headings inside content container */
[data-typography] .content-container h1:last-child,
[data-typography] .content-container h2:last-child,
[data-typography] .content-container h3:last-child,
[data-typography] .content-container h4:last-child,
[data-typography] .content-container h5:last-child,
[data-typography] .content-container h6:last-child,
.j-article-studio-output-wrapper .content-container h1:last-child,
.j-article-studio-output-wrapper .content-container h2:last-child,
.j-article-studio-output-wrapper .content-container h3:last-child,
.j-article-studio-output-wrapper .content-container h4:last-child,
.j-article-studio-output-wrapper .content-container h5:last-child,
.j-article-studio-output-wrapper .content-container h6:last-child,
.ProseMirror .content-container h1:last-child,
.ProseMirror .content-container h2:last-child,
.ProseMirror .content-container h3:last-child,
.ProseMirror .content-container h4:last-child,
.ProseMirror .content-container h5:last-child,
.ProseMirror .content-container h6:last-child {
  margin-bottom: 0 !important;
}

/* Lists inside content container */
[data-typography] .content-container ul:last-child,
[data-typography] .content-container ol:last-child,
.j-article-studio-output-wrapper .content-container ul:last-child,
.j-article-studio-output-wrapper .content-container ol:last-child,
.ProseMirror .content-container ul:last-child,
.ProseMirror .content-container ol:last-child {
  margin-bottom: 0 !important;
}

/* Responsive Design */
@media (max-width: 768px) {
  .j-article-studio-output-wrapper {
    padding: 15px;
    max-width: 100%;
  }
  
  .j-article-studio-output-wrapper h1 {
    font-size: calc(var(--title-size, 36px) * 0.8);
    line-height: calc(var(--title-lh, 40px) * 0.8);
  }
  
  .j-article-studio-output-wrapper h2 {
    font-size: calc(var(--heading-size, 20px) * 0.9);
  }
  
  .j-article-studio-output-wrapper p.dek {
    font-size: calc(var(--subtitle-size, 24px) * 0.85);
    line-height: calc(var(--subtitle-lh, 32px) * 0.85);
  }
  
  /* Caption styles for tablet/mobile */
  [data-typography] .mi-caption,
  .j-article-studio-output-wrapper .mi-caption,
  .ProseMirror .mi-caption {
    font-size: 13px;
  }
  
  [data-typography] .mi-caption--on,
  .j-article-studio-output-wrapper .mi-caption--on,
  .ProseMirror .mi-caption--on {
    font-size: 12px;
    padding: 6px 10px;
  }
}

@media (max-width: 480px) {
  .j-article-studio-output-wrapper {
    padding: 12px;
  }
  
  .j-article-studio-output-wrapper ul,
  .j-article-studio-output-wrapper ol {
    padding-left: 20px;
  }
  
  .j-article-studio-output-wrapper blockquote {
    padding-left: 16px;
    margin: 16px 0;
  }
  
  .j-article-studio-output-wrapper table {
    font-size: 14px;
  }
  
  .j-article-studio-output-wrapper th,
  .j-article-studio-output-wrapper td {
    padding: 6px 8px;
  }
}

/* Print Styles */
@media print {
  .j-article-studio-output-wrapper {
    max-width: none;
    padding: 0;
    color: #000;
    background: #fff;
  }
  
  .j-article-studio-output-wrapper a {
    color: #000;
    text-decoration: none;
  }
  
  .j-article-studio-output-wrapper a[href]:after {
    content: " (" attr(href) ")";
    font-size: 0.8em;
    color: #666;
  }
  
  .j-article-studio-output-wrapper blockquote {
    border-left-color: #000;
  }
  
  .j-article-studio-output-wrapper pre,
  .j-article-studio-output-wrapper code {
    background-color: #f5f5f5;
  }
  
  /* Caption styles for print */
  [data-typography] .mi-caption--on,
  .j-article-studio-output-wrapper .mi-caption--on,
  .ProseMirror .mi-caption--on {
    background: transparent !important;
    color: black !important;
    border: 1px solid #ccc;
    text-shadow: none;
  }
}

/* Dark Mode Support (when CSS variables are set for dark theme) */
@media (prefers-color-scheme: dark) {
  .j-article-studio-output-wrapper {
    background-color: var(--color-bg, #1a1a1a);
    color: var(--text-foreground, #e0e0e0);
  }
  
  .j-article-studio-output-wrapper a {
    color: var(--color-primary, #4dabf7);
  }
  
  .j-article-studio-output-wrapper code,
  .j-article-studio-output-wrapper pre {
    background-color: var(--color-surface, #2d2d2d);
  }
  
  .j-article-studio-output-wrapper hr {
    border-top-color: var(--color-border, #444);
  }
  
  /* Table styles (border, background) are handled by inline styles */
  
  /* Caption styles for dark mode */
  [data-typography] .mi-caption--on,
  .j-article-studio-output-wrapper .mi-caption--on,
  .ProseMirror .mi-caption--on {
    background: black !important;
    color: white !important;
    border: 2px solid white;
  }
}
