/* ═══════════════════════════════════════════════════════════
   D. NAKHLA — Design System
   Dark terminal aesthetic. Mr. Robot meets Devs meets arXiv.
   ═══════════════════════════════════════════════════════════ */

/* ── Custom Properties ─────────────────────────────────── */
:root {
  /* Backgrounds */
  --bg-deep:      #06090d;
  --bg-base:      #0a0e14;
  --bg-surface:   #111820;
  --bg-elevated:  #1a2230;
  --bg-hover:     #1f2937;

  /* Text */
  --text-primary:   #e0e6ed;
  --text-secondary: #6b7a8d;
  --text-tertiary:  #3d4f63;
  --text-inverse:   #0a0e14;

  /* Accent — Sienna */
  --accent:         #c4896a;
  --accent-bright:  #e67e5f;
  --accent-dim:     #9b442a;
  --accent-glow:    rgba(196, 137, 106, 0.08);

  /* Teal — Terminal / Code */
  --teal:           #7fdbca;
  --teal-dim:       #4a9e8e;

  /* Borders */
  --border-ghost:   rgba(196, 137, 106, 0.10);
  --border-subtle:  rgba(196, 137, 106, 0.18);
  --border-active:  rgba(196, 137, 106, 0.35);

  /* Grid overlay */
  --grid-line:      rgba(196, 137, 106, 0.04);

  /* Typography */
  --font-headline:  'Space Grotesk', system-ui, sans-serif;
  --font-body:      'Inter', system-ui, sans-serif;
  --font-mono:      'JetBrains Mono', 'Fira Code', monospace;

  /* Spacing */
  --space-xs: 0.25rem;
  --space-sm: 0.5rem;
  --space-md: 1rem;
  --space-lg: 1.5rem;
  --space-xl: 2rem;
  --space-2xl: 3rem;
  --space-3xl: 4rem;
  --space-4xl: 6rem;
  --space-5xl: 8rem;

  /* Layout */
  --max-width: 1280px;
  --content-width: 72ch;
}

/* ── Reset & Base ──────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  scroll-behavior: smooth;
}

body {
  background: var(--bg-base);
  color: var(--text-primary);
  font-family: var(--font-body);
  font-weight: 400;
  line-height: 1.7;
  overflow-x: hidden;
  min-height: 100vh;
}

/* Subtle grid background */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  background-image:
    linear-gradient(var(--grid-line) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-line) 1px, transparent 1px);
  background-size: 80px 80px;
  pointer-events: none;
  z-index: 0;
}

/* ── Typography ────────────────────────────────────────── */
h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-headline);
  font-weight: 700;
  line-height: 1.1;
  letter-spacing: -0.03em;
  color: var(--text-primary);
}

h1 { font-size: clamp(2.5rem, 6vw, 4.5rem); }
h2 { font-size: clamp(1.5rem, 3vw, 2.25rem); }
h3 { font-size: clamp(1.125rem, 2vw, 1.5rem); }

p { margin-bottom: var(--space-lg); }

a {
  color: var(--accent);
  text-decoration: none;
  transition: color 200ms ease;
}
a:hover { color: var(--accent-bright); }

.eyebrow {
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  font-weight: 500;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--accent);
}

.section-label {
  font-family: var(--font-headline);
  font-size: 0.625rem;
  font-weight: 700;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  color: var(--text-tertiary);
}

.mono {
  font-family: var(--font-mono);
  font-size: 0.875rem;
  color: var(--teal);
}

/* ── Layout ────────────────────────────────────────────── */
.container {
  position: relative;
  z-index: 1;
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 0 var(--space-xl);
}

@media (max-width: 640px) {
  .container { padding: 0 var(--space-md); }
}

.section {
  padding: var(--space-5xl) 0;
}

.section + .section {
  border-top: 0.5px solid var(--border-ghost);
}

.grid {
  display: grid;
  gap: var(--space-xl);
}

.grid-2 { grid-template-columns: repeat(2, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.grid-12 { grid-template-columns: repeat(12, 1fr); }

@media (max-width: 1024px) {
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 768px) {
  .grid-2, .grid-3, .grid-4, .grid-12 {
    grid-template-columns: 1fr;
  }
}

/* ── Navigation ────────────────────────────────────────── */
.nav {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 100;
  background: rgba(10, 14, 20, 0.92);
  backdrop-filter: blur(12px);
  border-bottom: 0.5px solid var(--border-ghost);
}

.nav-inner {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: var(--space-md) var(--space-xl);
  display: flex;
  justify-content: space-between;
  align-items: center;
}

@media (max-width: 640px) {
  .nav-inner { padding: var(--space-md); }
}

.nav-logo {
  font-family: var(--font-headline);
  font-size: 1.25rem;
  font-weight: 700;
  letter-spacing: -0.04em;
  color: var(--text-primary);
  text-decoration: none;
}
.nav-logo:hover { color: var(--text-primary); }

.nav-links {
  display: flex;
  gap: var(--space-2xl);
  list-style: none;
}

.nav-links a {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 400;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-secondary);
  text-decoration: none;
  transition: color 200ms ease;
  position: relative;
}

.nav-links a:hover,
.nav-links a.active {
  color: var(--accent);
}

.nav-links a.active::after {
  content: '';
  position: absolute;
  bottom: -6px;
  left: 0;
  right: 0;
  height: 1px;
  background: var(--accent);
}

/* Mobile menu */
.nav-toggle {
  display: none;
  background: none;
  border: none;
  color: var(--text-primary);
  cursor: pointer;
  font-size: 1.5rem;
  padding: var(--space-sm);
}

@media (max-width: 768px) {
  .nav-links { display: none; }
  .nav-toggle { display: block; }
  .nav-links.open {
    display: flex;
    flex-direction: column;
    position: fixed;
    inset: 0;
    background: rgba(10, 14, 20, 0.98);
    backdrop-filter: blur(16px);
    justify-content: center;
    align-items: center;
    gap: var(--space-2xl);
    z-index: 200;
  }
  .nav-links.open a {
    font-size: 1.25rem;
  }
  .nav-close {
    position: absolute;
    top: var(--space-md);
    right: var(--space-xl);
    background: none;
    border: none;
    color: var(--text-primary);
    font-size: 1.75rem;
    cursor: pointer;
  }
}

/* ── Cards ─────────────────────────────────────────────── */
.card {
  background: var(--bg-surface);
  border: 0.5px solid var(--border-ghost);
  padding: var(--space-2xl);
  transition: border-color 200ms ease, background-color 200ms ease;
}

.card:hover {
  border-color: var(--border-subtle);
  background: var(--bg-elevated);
}

.card-flat {
  border: 0.5px solid var(--border-ghost);
  padding: var(--space-2xl);
}

/* ── Stat Blocks ───────────────────────────────────────── */
.stat-block {
  padding: var(--space-xl) var(--space-2xl);
  border: 0.5px solid var(--border-ghost);
  text-align: center;
}

.stat-value {
  font-family: var(--font-headline);
  font-size: clamp(1.75rem, 3vw, 2.5rem);
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.03em;
  line-height: 1;
}

.stat-label {
  font-family: var(--font-mono);
  font-size: 0.625rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--text-tertiary);
  margin-top: var(--space-sm);
}

/* ── Badges ────────────────────────────────────────────── */
.badge {
  display: inline-block;
  padding: 0.15rem 0.6rem;
  font-family: var(--font-mono);
  font-size: 0.5625rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.14em;
}

.badge-published {
  background: rgba(127, 219, 202, 0.15);
  color: var(--teal);
  border: 0.5px solid rgba(127, 219, 202, 0.25);
}

.badge-dataset {
  background: rgba(196, 137, 106, 0.15);
  color: var(--accent);
  border: 0.5px solid rgba(196, 137, 106, 0.25);
}

.badge-topic {
  background: var(--bg-elevated);
  color: var(--text-secondary);
  border: 0.5px solid var(--border-ghost);
}

/* ── Blockquote ────────────────────────────────────────── */
.editorial-quote {
  border-left: 2px solid var(--accent);
  padding-left: var(--space-2xl);
  margin: var(--space-4xl) 0;
}

.editorial-quote blockquote {
  font-family: var(--font-headline);
  font-size: clamp(1.5rem, 3.5vw, 2.75rem);
  font-weight: 500;
  line-height: 1.25;
  color: var(--text-primary);
}

.editorial-quote cite {
  display: block;
  margin-top: var(--space-lg);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-style: normal;
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: var(--text-tertiary);
}

/* ── Links / CTAs ──────────────────────────────────────── */
.cta-link {
  display: inline-flex;
  align-items: center;
  gap: var(--space-sm);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  font-weight: 500;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--accent);
  text-decoration: none;
  border-bottom: 0.5px solid var(--accent-dim);
  padding-bottom: 2px;
  transition: color 200ms, border-color 200ms;
}

.cta-link:hover {
  color: var(--accent-bright);
  border-color: var(--accent-bright);
}

.cta-link svg {
  width: 14px;
  height: 14px;
  transition: transform 200ms;
}

.cta-link:hover svg {
  transform: translateX(3px);
}

/* ── Footer ────────────────────────────────────────────── */
.footer {
  border-top: 0.5px solid var(--border-ghost);
  padding: var(--space-4xl) 0;
  position: relative;
  z-index: 1;
}

.footer-inner {
  max-width: var(--max-width);
  margin: 0 auto;
  padding: 0 var(--space-xl);
  display: flex;
  flex-direction: column;
  gap: var(--space-2xl);
}

@media (min-width: 768px) {
  .footer-inner {
    flex-direction: row;
    justify-content: space-between;
    align-items: flex-start;
  }
}

.footer-brand {
  font-family: var(--font-headline);
  font-size: 0.875rem;
  font-weight: 700;
  color: var(--text-primary);
}

.footer-tagline {
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  color: var(--text-tertiary);
  margin-top: var(--space-xs);
  letter-spacing: 0.05em;
}

.footer-links {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-lg);
  list-style: none;
}

.footer-links a {
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--text-tertiary);
  text-decoration: none;
  transition: color 200ms;
}

.footer-links a:hover {
  color: var(--accent);
}

.footer-copy {
  font-family: var(--font-mono);
  font-size: 0.625rem;
  letter-spacing: 0.1em;
  color: var(--text-tertiary);
  opacity: 0.6;
}

/* ── Prose (article body text) ─────────────────────────── */
.prose {
  max-width: var(--content-width);
  color: var(--text-secondary);
  font-size: 1.0625rem;
  line-height: 1.8;
}

.prose p { margin-bottom: var(--space-lg); }

.prose strong {
  color: var(--text-primary);
  font-weight: 600;
}

.prose a {
  color: var(--accent);
  text-decoration: underline;
  text-decoration-thickness: 0.5px;
  text-underline-offset: 3px;
}

.prose a:hover { color: var(--accent-bright); }

/* ── SVG Diagrams ──────────────────────────────────────── */
.diagram-container {
  background: var(--bg-deep);
  border: 0.5px solid var(--border-subtle);
  padding: var(--space-2xl);
  margin: var(--space-2xl) 0;
  overflow-x: auto;
}

.diagram-container svg {
  width: 100%;
  height: auto;
  display: block;
}

.diagram-caption {
  font-family: var(--font-mono);
  font-size: 0.6875rem;
  color: var(--text-tertiary);
  text-align: center;
  margin-top: var(--space-md);
  letter-spacing: 0.05em;
}

/* ── Utility ───────────────────────────────────────────── */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  border: 0;
}

.text-accent { color: var(--accent); }
.text-teal { color: var(--teal); }
.text-secondary { color: var(--text-secondary); }
.text-tertiary { color: var(--text-tertiary); }

.mt-0 { margin-top: 0; }
.mt-xs { margin-top: var(--space-xs); }
.mt-sm { margin-top: var(--space-sm); }
.mt-md { margin-top: var(--space-md); }
.mt-lg { margin-top: var(--space-lg); }
.mt-xl { margin-top: var(--space-xl); }
.mt-2xl { margin-top: var(--space-2xl); }
.mt-3xl { margin-top: var(--space-3xl); }
.mt-4xl { margin-top: var(--space-4xl); }

.mb-0 { margin-bottom: 0; }
.mb-sm { margin-bottom: var(--space-sm); }
.mb-md { margin-bottom: var(--space-md); }
.mb-lg { margin-bottom: var(--space-lg); }
.mb-xl { margin-bottom: var(--space-xl); }
.mb-2xl { margin-bottom: var(--space-2xl); }

.flex { display: flex; }
.flex-col { flex-direction: column; }
.flex-wrap { flex-wrap: wrap; }
.items-center { align-items: center; }
.items-start { align-items: flex-start; }
.items-baseline { align-items: baseline; }
.justify-between { justify-content: space-between; }
.gap-sm { gap: var(--space-sm); }
.gap-md { gap: var(--space-md); }
.gap-lg { gap: var(--space-lg); }
.gap-xl { gap: var(--space-xl); }
.gap-2xl { gap: var(--space-2xl); }

/* ── Span helpers for grid-12 ──────────────────────────── */
.col-span-3 { grid-column: span 3; }
.col-span-4 { grid-column: span 4; }
.col-span-5 { grid-column: span 5; }
.col-span-6 { grid-column: span 6; }
.col-span-7 { grid-column: span 7; }
.col-span-8 { grid-column: span 8; }
.col-span-9 { grid-column: span 9; }
.col-span-12 { grid-column: span 12; }

@media (max-width: 768px) {
  [class*="col-span-"] { grid-column: 1 / -1; }
}

/* ── Scanline effect (subtle) ──────────────────────────── */
.scanline-overlay::after {
  content: '';
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 2px,
    rgba(0, 0, 0, 0.03) 2px,
    rgba(0, 0, 0, 0.03) 4px
  );
  pointer-events: none;
  z-index: 2;
}

/* ── Glow accent for hero ──────────────────────────────── */
.glow-spot {
  position: absolute;
  width: 400px;
  height: 400px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(196, 137, 106, 0.06) 0%, transparent 70%);
  pointer-events: none;
}

/* ── Pipeline steps ────────────────────────────────────── */
.pipeline-step {
  padding: var(--space-xl);
  background: var(--bg-surface);
  border: 0.5px solid var(--border-ghost);
  position: relative;
}

.pipeline-step::before {
  content: attr(data-step);
  font-family: var(--font-mono);
  font-size: 0.625rem;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--accent);
  margin-bottom: var(--space-sm);
  display: block;
}

/* ── Responsive images ─────────────────────────────────── */
img { max-width: 100%; height: auto; }

/* ── Focus styles ──────────────────────────────────────── */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ── Selection ─────────────────────────────────────────── */
::selection {
  background: rgba(196, 137, 106, 0.3);
  color: var(--text-primary);
}

/* ── Touch targets ─────────────────────────────────────── */
@media (max-width: 768px) {
  a, button { min-height: 44px; }
}

/* ── Mobile responsive fixes ──────────────────────────── */

/* Global safety net — prevent horizontal scroll on all mobile */
@media (max-width: 768px) {
  html, body {
    max-width: 100vw;
    overflow-x: hidden;
  }

  /* Root cause #1: .copy / .prose / .prose-editorial max-width in ch units exceeds phone viewports */
  /* !important needed to override inline <style> blocks on 30+ article pages and style="" attributes */
  .copy,
  .prose,
  .prose-editorial {
    max-width: 100% !important;
    overflow-x: hidden;
    min-width: 0;
  }

  /* Catch-all: any element with inline style max-width in ch units */
  [style*="max-width"][style*="ch"] {
    max-width: 100% !important;
  }

  /* Root cause #2: Code blocks overflow (up to 434px past viewport on iPhone SE) */
  .copy pre,
  .prose pre,
  .prose-editorial pre,
  pre {
    max-width: calc(100vw - 2rem) !important;
    overflow-x: auto;
  }

  .copy code,
  .prose code,
  .prose-editorial code {
    word-break: break-word;
  }

  /* Root cause #3: Tables overflow (up to 375px on comparison tables) */
  .copy table,
  .prose table,
  .prose-editorial table,
  table {
    display: block;
    max-width: calc(100vw - 2rem) !important;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
}

/* Nav overflow fix — prevent horizontal scroll on mobile */
@media (max-width: 768px) {
  .nav {
    max-width: 100vw;
    overflow-x: hidden;
  }

  /* Hamburger touch target */
  .nav-toggle {
    min-width: 44px;
    min-height: 44px;
  }

  /* Footer link touch targets */
  .footer-links a {
    min-height: 44px;
    padding: 12px 8px;
    display: inline-flex;
    align-items: center;
  }

  /* Accessible minimum font size for decorative text */
  .section-label,
  .stat-label,
  .pipeline-step::before,
  .footer-copy {
    font-size: max(0.75rem, 12px);
  }
}

/* Override inline grid-template-columns on featured cards at mobile */
@media (max-width: 768px) {
  /* Featured article card (writing/index.html) — inline 2-col grid */
  .card[style*="grid-template-columns"] {
    grid-template-columns: 1fr !important;
  }

  /* Infrastructure section — inline grid-column: span 2 on grid-4 children */
  .grid-4 > [style*="grid-column"] {
    grid-column: span 1 !important;
  }

  /* Stat blocks: fix borders when stacked vertically */
  .stat-block[style*="border-left"] {
    border-left: 0.5px solid var(--border-ghost) !important;
  }
  .stat-block + .stat-block {
    border-top: none !important;
  }

  /* Cards in grid-2/grid-3: normalize borders for single-column */
  .grid-2 > .card[style*="border-right: none"],
  .grid-3 > .card-flat[style*="border-right: none"],
  .grid-3 > .card[style*="border-right: none"] {
    border-right: 0.5px solid var(--border-ghost) !important;
  }
  .grid-2 > .card[style*="border-top: none"],
  .grid-3 > .card-flat[style*="border-top: none"] {
    border-top: 0.5px solid var(--border-ghost) !important;
  }

  /* Tailwind shim: grid-cols-2 used in article pages */
  .grid-cols-2 {
    grid-template-columns: 1fr !important;
  }

  /* Tighter editorial quotes on mobile */
  .editorial-quote {
    padding-left: var(--space-lg);
    margin: var(--space-2xl) 0;
  }
  .editorial-quote blockquote {
    font-size: clamp(1.2rem, 5vw, 2rem);
  }

  /* Tighter section spacing on mobile */
  .section {
    padding: var(--space-3xl) 0;
  }

  /* Ensure flex-wrap on all flex containers with inline gaps */
  .flex[style*="gap"] {
    flex-wrap: wrap;
  }
}

/* Extra small screens (iPhone SE / small Android) */
@media (max-width: 390px) {
  .container {
    padding: 0 var(--space-sm) !important;
  }

  .nav-inner {
    padding: var(--space-sm) !important;
  }

  /* Smaller headings for tiny screens */
  h1 {
    font-size: clamp(1.75rem, 8vw, 2.5rem) !important;
  }

  /* Stat values tighter */
  .stat-value {
    font-size: clamp(1.5rem, 6vw, 2rem);
  }

  /* Cards need less padding */
  .card, .card-flat {
    padding: var(--space-lg) !important;
  }

  /* Footer adjustments */
  .footer-inner {
    padding: 0 var(--space-sm);
  }
}

/* ── Print ─────────────────────────────────────────────── */
@media print {
  body { background: white; color: black; }
  body::before { display: none; }
  .nav, .footer { display: none; }
}
