/* =============================================================================
   TYPOGRAPHY SYSTEM
   Comprehensive typographic design system with modular scale, fluid sizing,
   and accessibility-first approach
   ============================================================================= */

/* =============================================================================
   DESIGN TOKENS - TYPE SCALE
   Using 1.250 ratio (Major Third) for harmonious hierarchy
   Base: 18px (1rem at root)
   ============================================================================= */

:root {
  /* Modular Type Scale - 1.250 ratio (Major Third) */
  --text-xs: 0.694rem;      /* 12.5px - Fine print, small labels */
  --text-sm: 0.833rem;      /* 15px - Metadata, captions, timestamps */
  --text-base: 1rem;        /* 18px - Body text */
  --text-lg: 1.2rem;        /* 21.6px - Lead paragraphs, emphasis */
  --text-xl: 1.44rem;       /* 25.9px - Small headings, card titles */
  --text-2xl: 1.728rem;     /* 31.1px - H4 */
  --text-3xl: 2.074rem;     /* 37.3px - H3 */
  --text-4xl: 2.488rem;     /* 44.8px - H2 */
  --text-5xl: 2.986rem;     /* 53.7px - H1 */
  --text-6xl: 3.583rem;     /* 64.5px - Display headings */

  /* Font Weight Scale */
  --font-light: 300;
  --font-normal: 400;
  --font-medium: 500;
  --font-semibold: 600;
  --font-bold: 700;
  --font-extrabold: 800;

  /* Line Height Scale - Context-based */
  --leading-none: 1;
  --leading-tight: 1.25;       /* Headlines, display text */
  --leading-snug: 1.375;       /* Subheadings, UI elements */
  --leading-normal: 1.5;       /* Body text (WCAG minimum) */
  --leading-relaxed: 1.625;    /* Long-form content, articles */
  --leading-loose: 1.75;       /* Emphasis, pull quotes */

  /* Letter Spacing Scale */
  --tracking-tighter: -0.05em;   /* Large display text */
  --tracking-tight: -0.025em;    /* Headings */
  --tracking-normal: 0;          /* Body text */
  --tracking-wide: 0.025em;      /* Small text, labels */
  --tracking-wider: 0.05em;      /* ALL CAPS, badges */
  --tracking-widest: 0.1em;      /* Acronyms, emphasis */

  /* Measure (Line Length) */
  --measure-narrow: 45ch;        /* Short text, UI */
  --measure-base: 66ch;          /* Optimal reading */
  --measure-wide: 75ch;          /* Wide content */

  /* Font Stacks - Inherited from existing system */
  --font-serif: 'Crimson Text', 'Georgia', 'Times New Roman', serif;
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', 'SF Mono', 'Consolas', 'Monaco', 'Courier New', monospace;
}

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

/* Fluid base font size: 16px at 320px viewport to 18px at 1024px */
html {
  font-size: clamp(1rem, 0.875rem + 0.3125vw, 1.125rem);
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
  -moz-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  font-family: var(--font-sans);
  font-size: var(--text-base);
  font-weight: var(--font-normal);
  line-height: var(--leading-normal);
  letter-spacing: var(--tracking-normal);

  /* Font rendering optimization */
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;

  /* OpenType features */
  font-feature-settings:
    "kern" 1,    /* Kerning */
    "liga" 1,    /* Common ligatures */
    "calt" 1;    /* Contextual alternates */
}

/* =============================================================================
   HEADINGS - Fluid sizing with modular scale
   ============================================================================= */

h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-serif);
  font-weight: var(--font-bold);
  line-height: var(--leading-tight);
  letter-spacing: var(--tracking-tight);
  color: var(--color-ink);
  margin: 0;

  /* Text wrapping improvements */
  text-wrap: balance;
  overflow-wrap: break-word;
  word-break: break-word;

  /* OpenType features for headings */
  font-feature-settings:
    "kern" 1,
    "liga" 1,
    "dlig" 1;    /* Discretionary ligatures */
}

h1 {
  font-size: clamp(2rem, 1.5rem + 2.5vw, 3.5rem);
  line-height: 1.1;
  letter-spacing: var(--tracking-tighter);
  font-weight: var(--font-bold);
}

h2 {
  font-size: clamp(1.5rem, 1.25rem + 1.25vw, 2.5rem);
  line-height: 1.2;
  font-weight: var(--font-semibold);
}

h3 {
  font-size: clamp(1.25rem, 1.125rem + 0.625vw, 2rem);
  line-height: 1.3;
  font-weight: var(--font-semibold);
}

h4 {
  font-size: clamp(1.125rem, 1.0625rem + 0.3125vw, 1.5rem);
  line-height: 1.35;
  font-weight: var(--font-semibold);
}

h5 {
  font-size: var(--text-lg);
  line-height: var(--leading-snug);
  font-weight: var(--font-medium);
}

h6 {
  font-size: var(--text-base);
  line-height: var(--leading-snug);
  font-weight: var(--font-medium);
  text-transform: uppercase;
  letter-spacing: var(--tracking-wider);
}

/* =============================================================================
   PARAGRAPH & BODY TEXT
   ============================================================================= */

p {
  margin: 0 0 1.5em 0;
  max-width: var(--measure-base);
}

/* Lead paragraph */
.lead,
.post-description,
article > p:first-of-type:not(.no-lead) {
  font-size: var(--text-lg);
  line-height: var(--leading-relaxed);
  color: var(--color-ink);
}

/* Long-form content optimization */
article p {
  font-family: var(--font-serif);
  font-size: 1.1rem;
  line-height: var(--leading-relaxed);
  margin-bottom: 1.5em;
  color: var(--color-ink);

  /* Text wrapping improvements */
  text-wrap: pretty;
  hyphens: auto;
  -webkit-hyphens: auto;
  -moz-hyphens: auto;
  hyphenate-limit-chars: 6 3 2;
  word-spacing: 0.05em;
}

/* Paragraph following heading - reduce space */
h2 + p,
h3 + p,
h4 + p {
  margin-top: 0.75em;
}

/* =============================================================================
   EMPHASIS & INLINE ELEMENTS
   ============================================================================= */

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

em, i {
  font-style: italic;
}

small {
  font-size: var(--text-sm);
  line-height: var(--leading-normal);
}

mark {
  background-color: var(--color-accent-light);
  color: var(--color-ink);
  padding: 0.125em 0.25em;
  border-radius: 2px;
}

/* =============================================================================
   LINKS
   ============================================================================= */

a {
  color: var(--color-accent);
  text-decoration: underline;
  text-decoration-thickness: 1px;
  text-underline-offset: 2px;
  text-decoration-skip-ink: auto;
  transition: var(--transition);
}

a:hover {
  text-decoration-thickness: 2px;
  color: var(--color-accent-dark);
}

a:focus-visible {
  outline: 3px solid var(--color-accent);
  outline-offset: 2px;
  border-radius: 2px;
  text-decoration: none;
}

/* =============================================================================
   LISTS
   ============================================================================= */

ul, ol {
  margin: 0 0 1.5em 0;
  padding-left: 1.5em;
  max-width: var(--measure-base);
}

li {
  margin-bottom: 0.5em;
  line-height: var(--leading-relaxed);
}

li > ul,
li > ol {
  margin-top: 0.5em;
  margin-bottom: 0.5em;
}

/* Definition lists */
dt {
  font-weight: var(--font-semibold);
  margin-top: 1em;
}

dd {
  margin-left: 1.5em;
  margin-bottom: 0.5em;
}

/* =============================================================================
   BLOCKQUOTES
   ============================================================================= */

blockquote {
  position: relative;
  margin: 2em 0;
  padding: 1rem 1.5rem 1rem 2.5rem;
  color: var(--color-ink-light);
  border-left: 3px solid var(--color-accent);
  background-color: var(--color-accent-light);
  font-style: italic;
  border-radius: 0 var(--radius-md) var(--radius-md) 0;
  max-width: var(--measure-base);
}

blockquote p {
  font-family: var(--font-serif);
  line-height: var(--leading-relaxed);
}

blockquote cite {
  display: block;
  margin-top: 1em;
  font-size: var(--text-sm);
  font-style: normal;
  color: var(--color-ink-light);
}

/* Pull quotes - large, centered quotes */
blockquote.pullquote {
  font-size: 1.75rem;
  font-family: var(--font-serif);
  font-style: italic;
  font-weight: var(--font-semibold);
  text-align: center;
  border-left: none;
  background: none;
  padding: 2.5rem 4rem;
  margin: 4rem auto;
  color: var(--color-ink);
  position: relative;
  max-width: 90%;
  line-height: 1.4;
}

blockquote.pullquote::before,
blockquote.pullquote::after {
  content: """;
  font-size: 4rem;
  color: var(--color-accent-light);
  position: absolute;
  font-family: 'Georgia', serif;
  line-height: 1;
}

blockquote.pullquote::before {
  top: 0;
  left: 0;
}

blockquote.pullquote::after {
  content: """;
  bottom: -0.5rem;
  right: 0;
}

/* =============================================================================
   CODE & PREFORMATTED TEXT
   ============================================================================= */

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

  /* Disable ligatures in code */
  font-variant-ligatures: none;
  font-feature-settings:
    "calt" 0,
    "liga" 0;
}

/* Inline code */
code {
  font-size: 0.875em;
  padding: 0.125em 0.375em;
  background-color: var(--color-accent-light);
  border-radius: var(--radius-sm);
  letter-spacing: -0.01em;
  font-weight: var(--font-medium);
  color: var(--color-ink);
}

/* Code blocks */
pre {
  font-size: 0.85rem;
  line-height: var(--leading-relaxed);
  padding: var(--space-lg);
  margin: 1.5em 0;
  background-color: var(--color-accent-light);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-md);
  overflow-x: auto;
  tab-size: 2;
  max-width: 100%;
}

pre code {
  background: none;
  padding: 0;
  border-radius: 0;
  font-size: inherit;
}

/* Keyboard input */
kbd {
  font-size: 0.85em;
  padding: 0.125em 0.375em;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-paper);
  box-shadow:
    0 1px 0 var(--color-border),
    0 0 0 2px var(--color-paper) inset;
  font-weight: var(--font-medium);
}

/* =============================================================================
   TABLES
   ============================================================================= */

table {
  width: 100%;
  border-collapse: collapse;
  margin: 1.5em 0;
  font-size: 0.95rem;
  font-family: var(--font-sans);
  max-width: var(--measure-wide);
}

th, td {
  padding: 0.75rem;
  border: 1px solid var(--color-border);
  text-align: left;
  line-height: var(--leading-normal);
}

th {
  background-color: var(--color-accent-light);
  font-weight: var(--font-semibold);
  color: var(--color-ink);
}

/* Tabular numbers for data columns */
td {
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1;
}

/* =============================================================================
   TIME & METADATA
   ============================================================================= */

time,
.post-date,
.timestamp {
  font-family: var(--font-mono);
  font-size: var(--text-sm);
  color: var(--color-ink-light);
  font-weight: var(--font-medium);
  letter-spacing: var(--tracking-wide);

  /* Tabular numbers for dates */
  font-variant-numeric: tabular-nums;
  font-feature-settings: "tnum" 1, "zero" 1;
}

/* =============================================================================
   UTILITY CLASSES
   ============================================================================= */

/* Font families */
.font-sans { font-family: var(--font-sans); }
.font-serif { font-family: var(--font-serif); }
.font-mono { font-family: var(--font-mono); }

/* Font sizes */
.text-xs { font-size: var(--text-xs); }
.text-sm { font-size: var(--text-sm); }
.text-base { font-size: var(--text-base); }
.text-lg { font-size: var(--text-lg); }
.text-xl { font-size: var(--text-xl); }
.text-2xl { font-size: var(--text-2xl); }
.text-3xl { font-size: var(--text-3xl); }
.text-4xl { font-size: var(--text-4xl); }
.text-5xl { font-size: var(--text-5xl); }

/* Font weights */
.font-light { font-weight: var(--font-light); }
.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); }
.font-extrabold { font-weight: var(--font-extrabold); }

/* Line heights */
.leading-none { line-height: var(--leading-none); }
.leading-tight { line-height: var(--leading-tight); }
.leading-snug { line-height: var(--leading-snug); }
.leading-normal { line-height: var(--leading-normal); }
.leading-relaxed { line-height: var(--leading-relaxed); }
.leading-loose { line-height: var(--leading-loose); }

/* Letter spacing */
.tracking-tighter { letter-spacing: var(--tracking-tighter); }
.tracking-tight { letter-spacing: var(--tracking-tight); }
.tracking-normal { letter-spacing: var(--tracking-normal); }
.tracking-wide { letter-spacing: var(--tracking-wide); }
.tracking-wider { letter-spacing: var(--tracking-wider); }
.tracking-widest { letter-spacing: var(--tracking-widest); }

/* Text transforms */
.uppercase { text-transform: uppercase; }
.lowercase { text-transform: lowercase; }
.capitalize { text-transform: capitalize; }

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

/* =============================================================================
   ACCESSIBILITY ENHANCEMENTS
   ============================================================================= */

/* Improved focus indicators - WCAG 2.1 compliant */
*:focus-visible {
  outline: 3px solid var(--color-accent);
  outline-offset: 2px;
  border-radius: 2px;
}

/* Skip link */
.skip-link {
  position: absolute;
  top: -100px;
  left: 0;
  padding: var(--space-sm) var(--space-md);
  background-color: var(--color-accent);
  color: var(--color-paper);
  font-family: var(--font-sans);
  font-weight: var(--font-semibold);
  font-size: var(--text-base);
  z-index: 1000;
  transition: top 0.2s;
  text-decoration: none;
  border-radius: 0 0 var(--radius-sm) 0;
}

.skip-link:focus {
  top: 0;
  outline: none;
}

/* 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: 0;
}

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

@media (max-width: 768px) {
  /* Adjust spacing for smaller screens */
  p {
    margin-bottom: 1.25em;
  }

  article p {
    font-size: 1.05rem;
  }

  blockquote {
    padding: 0.75rem 1rem 0.75rem 1.5rem;
    margin: 1.5em 0;
  }

  blockquote.pullquote {
    font-size: 1.5rem;
    padding: 2rem 2rem;
    margin: 3rem auto;
  }

  pre {
    font-size: 0.8rem;
    padding: var(--space-md);
  }
}

@media (max-width: 640px) {
  /* Further adjustments for mobile */
  article p {
    font-size: 1rem;
    line-height: 1.7;
  }

  blockquote.pullquote {
    font-size: 1.25rem;
    padding: 1.5rem 1rem;
  }

  ul, ol {
    padding-left: 1.25em;
  }
}

/* =============================================================================
   REDUCED MOTION
   ============================================================================= */

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

  * {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

/* =============================================================================
   PRINT STYLES
   ============================================================================= */

@media print {
  html {
    font-size: 12pt;
  }

  body {
    color: black;
    background: white;
  }

  h1 {
    font-size: 24pt;
    page-break-after: avoid;
  }

  h2 {
    font-size: 18pt;
    page-break-after: avoid;
  }

  h3 {
    font-size: 14pt;
    page-break-after: avoid;
  }

  p, li {
    orphans: 3;
    widows: 3;
  }

  a {
    color: black;
    text-decoration: underline;
  }

  a[href^="http"]::after {
    content: " (" attr(href) ")";
    font-size: 10pt;
    color: #666;
  }

  pre, blockquote {
    page-break-inside: avoid;
  }
}

/* =============================================================================
   DARK MODE ADJUSTMENTS
   ============================================================================= */

@media (prefers-color-scheme: dark) {
  code {
    background-color: rgba(255, 255, 255, 0.07);
  }

  pre {
    background-color: rgba(255, 255, 255, 0.07);
  }

  blockquote {
    background-color: rgba(255, 255, 255, 0.05);
  }

  table th {
    background-color: rgba(255, 255, 255, 0.05);
  }

  kbd {
    background: var(--color-paper);
    border-color: rgba(255, 255, 255, 0.1);
  }
}

/* Theme-specific dark mode */
[data-theme="dark"] code {
  background-color: rgba(255, 255, 255, 0.07);
}

[data-theme="dark"] pre {
  background-color: rgba(255, 255, 255, 0.07);
}

[data-theme="dark"] blockquote {
  background-color: rgba(255, 255, 255, 0.05);
}

[data-theme="dark"] table th {
  background-color: rgba(255, 255, 255, 0.05);
}
