/* ============================================================
   Twilight of the Legible — Editorial Typography
   ============================================================ */

/* --- Fonts --- */
@import url('https://fonts.googleapis.com/css2?family=Literata:ital,opsz,wght@0,7..72,300..700;1,7..72,300..700&display=swap');

/* --- Custom Properties (light mode defaults) --- */
:root {
  --color-bg: #faf8f5;
  --color-text: #2c2c2c;
  --color-text-muted: #5a5a5a;
  --color-link: #4a4a6a;
  --color-link-hover: #2a2a4a;
  --color-border: #d4d0ca;
  --color-border-light: #e8e4de;
  --color-blockquote-border: #b0a99a;
  --color-blockquote-bg: transparent;
  --color-header-bg: transparent;
  --color-footer-text: #6b6b6b;
  --color-code-bg: #f3f1ed;

  --font-body: 'Literata', 'Source Serif 4', 'Georgia', 'Times New Roman', serif;
  --font-ui: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;

  --measure: 65ch;
  --font-size-base: 19px;
  --line-height-base: 1.65;
  --spacing-unit: 1.5rem;
}

/* --- Dark Mode --- */
[data-theme='dark'] {
  --color-bg: #1a1a1e;
  --color-text: #d8d4cf;
  --color-text-muted: #9e9a93;
  --color-link: #a8a0c8;
  --color-link-hover: #c8c0e0;
  --color-border: #3a3a3e;
  --color-border-light: #2e2e32;
  --color-blockquote-border: #5a574f;
  --color-blockquote-bg: transparent;
  --color-header-bg: transparent;
  --color-footer-text: #8a877f;
  --color-code-bg: #232328;
}

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

html {
  font-size: var(--font-size-base);
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font-body);
  line-height: var(--line-height-base);
  color: var(--color-text);
  background-color: var(--color-bg);
  margin: 0;
  padding: 0;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

/* --- Layout --- */
main {
  max-width: var(--measure);
  margin: 0 auto;
  padding: var(--spacing-unit) 1.25rem;
}

/* --- Skip Link --- */
.skip-link {
  position: absolute;
  top: -100%;
  left: 0;
  z-index: 100;
  padding: 0.5rem 1rem;
  background: var(--color-bg);
  color: var(--color-link);
  text-decoration: underline;
  font-size: 0.9rem;
}

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

/* --- Header --- */
header[role="banner"] {
  border-bottom: 1px solid var(--color-border-light);
  padding: 0.75rem 1.25rem;
}

header[role="banner"] nav {
  max-width: var(--measure);
  margin: 0 auto;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.site-title {
  font-family: var(--font-body);
  font-size: 1rem;
  font-weight: 400;
  color: var(--color-text);
  text-decoration: none;
  letter-spacing: 0.01em;
}

.site-title:hover {
  color: var(--color-link);
}

#theme-toggle {
  font-family: var(--font-ui);
  font-size: 1.1rem;
  background: none;
  border: none;
  cursor: pointer;
  color: var(--color-text-muted);
  padding: 0.25rem 0.5rem;
  border-radius: 4px;
  line-height: 1;
}

#theme-toggle:hover {
  color: var(--color-text);
  background-color: var(--color-border-light);
}

#theme-toggle:focus-visible {
  outline: 2px solid var(--color-link);
  outline-offset: 2px;
}

/* --- Footer --- */
footer[role="contentinfo"] {
  max-width: var(--measure);
  margin: 0 auto;
  padding: 2rem 1.25rem;
  border-top: 1px solid var(--color-border-light);
  font-size: 0.8rem;
  color: var(--color-footer-text);
}

footer p {
  margin: 0.25rem 0;
}

.colophon {
  font-style: italic;
}

/* --- Headings --- */
h1, h2, h3, h4 {
  font-family: var(--font-body);
  font-weight: 600;
  line-height: 1.25;
  color: var(--color-text);
  margin-top: 2em;
  margin-bottom: 0.5em;
}

h1 {
  font-size: 2rem;
  font-weight: 700;
  margin-top: 0;
  letter-spacing: -0.01em;
}

h2 {
  font-size: 1.4rem;
}

h3 {
  font-size: 1.15rem;
}

h4 {
  font-size: 1rem;
  font-weight: 600;
}

/* --- Prose --- */
p {
  margin: 0 0 var(--spacing-unit) 0;
}

blockquote {
  margin: var(--spacing-unit) 0;
  padding: 0.25rem 0 0.25rem 1.5rem;
  border-left: 3px solid var(--color-blockquote-border);
  color: var(--color-text-muted);
  font-style: italic;
}

blockquote p:last-child {
  margin-bottom: 0;
}

hr {
  border: none;
  border-top: 1px solid var(--color-border);
  margin: var(--spacing-unit) 0;
}

strong {
  font-weight: 600;
}

ul, ol {
  margin: 0 0 var(--spacing-unit) 0;
  padding-left: 1.5rem;
}

li {
  margin-bottom: 0.35rem;
}

li > ul, li > ol {
  margin-top: 0.35rem;
  margin-bottom: 0;
}

/* --- Links --- */
a {
  color: var(--color-link);
  text-decoration: underline;
  text-decoration-color: var(--color-border);
  text-underline-offset: 2px;
  transition: color 0.15s ease, text-decoration-color 0.15s ease;
}

a:hover {
  color: var(--color-link-hover);
  text-decoration-color: var(--color-link-hover);
}

a:visited {
  color: var(--color-link);
}

a:focus-visible {
  outline: 2px solid var(--color-link);
  outline-offset: 2px;
  border-radius: 2px;
}

/* --- Essay Header --- */
.essay-header {
  margin-bottom: 0;
}

.essay-header h1 {
  margin-bottom: 0.3em;
}

.subtitle {
  font-size: 1.15rem;
  font-weight: 350;
  font-style: italic;
  color: var(--color-text-muted);
  margin: 0 0 0.5em 0;
  line-height: 1.4;
}

.byline {
  font-size: 0.8rem;
  font-variant: small-caps;
  letter-spacing: 0.03em;
  color: var(--color-text-muted);
  margin: 0 0 0.5em 0;
  line-height: 1.5;
}

/* --- Essay Body --- */
.essay-body {
  margin-top: var(--spacing-unit);
}

.essay-body h2 {
  font-size: 1.3rem;
  margin-top: 2.5em;
}

.essay-body h3 {
  font-size: 1.1rem;
  margin-top: 2em;
}

/* --- Essay Navigation --- */
.essay-nav {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-top: 3rem;
  padding-top: 1.5rem;
  border-top: 1px solid var(--color-border-light);
  font-size: 0.85rem;
  gap: 1rem;
}

.essay-nav a {
  color: var(--color-text-muted);
  text-decoration: none;
  max-width: 45%;
}

.essay-nav a:hover {
  color: var(--color-link-hover);
}

.essay-nav-prev {
  text-align: left;
}

.essay-nav-next {
  text-align: right;
  margin-left: auto;
}

/* --- TOC (Home Page) --- */
.toc-part {
  margin-bottom: 2.5rem;
}

.toc-part h2 {
  font-size: 1.2rem;
  font-weight: 600;
  margin-bottom: 0.75rem;
  color: var(--color-text);
  border-bottom: 1px solid var(--color-border-light);
  padding-bottom: 0.4rem;
}

.toc-essays {
  list-style: none;
  padding-left: 0;
  margin: 0;
}

.toc-essays li {
  padding: 0.4rem 0;
  margin-bottom: 0;
  line-height: 1.45;
}

.toc-essays a {
  font-weight: 500;
  text-decoration: none;
  color: var(--color-text);
}

.toc-essays a:hover {
  color: var(--color-link);
  text-decoration: underline;
  text-decoration-color: var(--color-border);
}

.toc-subtitle {
  display: block;
  font-size: 0.85rem;
  font-style: italic;
  color: var(--color-text-muted);
  margin-top: 0.1rem;
}

.toc-byline {
  display: block;
  font-size: 0.75rem;
  font-variant: small-caps;
  letter-spacing: 0.02em;
  color: var(--color-footer-text);
  margin-top: 0.15rem;
}

/* --- Tables --- */
.table-wrapper {
  overflow-x: auto;
  margin: var(--spacing-unit) 0;
  -webkit-overflow-scrolling: touch;
}

table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.9rem;
  line-height: 1.5;
}

thead th {
  font-weight: 600;
  text-align: left;
  background-color: var(--color-border-light);
  border-bottom: 2px solid var(--color-border);
  padding: 0.6rem 0.75rem;
}

tbody td {
  padding: 0.5rem 0.75rem;
  border-bottom: 1px solid var(--color-border-light);
  vertical-align: top;
}

tbody tr:last-child td {
  border-bottom: none;
}

/* --- Code Blocks --- */
pre {
  background-color: var(--color-code-bg);
  border: 1px solid var(--color-border-light);
  border-radius: 4px;
  padding: 1rem 1.25rem;
  overflow-x: auto;
  margin: var(--spacing-unit) 0;
  font-size: 0.85rem;
  line-height: 1.55;
  -webkit-overflow-scrolling: touch;
}

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

/* --- Inline Code --- */
code {
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  font-size: 0.85em;
  background-color: var(--color-code-bg);
  padding: 0.15em 0.35em;
  border-radius: 3px;
  border: 1px solid var(--color-border-light);
}

/* --- Responsive --- */
@media (max-width: 600px) {
  :root {
    --font-size-base: 17px;
  }

  h1 {
    font-size: 1.6rem;
  }

  h2 {
    font-size: 1.2rem;
  }

  .essay-nav {
    flex-direction: column;
    gap: 0.75rem;
  }

  .essay-nav a {
    max-width: 100%;
  }

  .essay-nav-next {
    text-align: left;
    margin-left: 0;
  }
}

/* --- Print --- */
@media print {
  /* Force black on white */
  :root {
    --color-bg: #fff;
    --color-text: #000;
    --color-text-muted: #333;
    --color-link: #000;
    --color-link-hover: #000;
    --color-border: #999;
    --color-border-light: #ccc;
    --color-blockquote-border: #999;
    --color-footer-text: #666;
    --color-code-bg: #f5f5f5;
  }

  [data-theme='dark'] {
    --color-bg: #fff;
    --color-text: #000;
    --color-text-muted: #333;
    --color-link: #000;
    --color-link-hover: #000;
    --color-border: #999;
    --color-border-light: #ccc;
    --color-blockquote-border: #999;
    --color-footer-text: #666;
    --color-code-bg: #f5f5f5;
  }

  body {
    font-size: 12pt;
    line-height: 1.5;
    background: #fff;
    color: #000;
  }

  /* Hide navigation chrome */
  header[role="banner"],
  #theme-toggle,
  .essay-nav,
  footer[role="contentinfo"] {
    display: none !important;
  }

  /* Adjust layout for paper */
  main {
    max-width: 100%;
    margin: 0;
    padding: 0;
  }

  /* Page margins */
  @page {
    margin: 2cm 2.5cm;
  }

  /* Avoid widows/orphans */
  p, blockquote, li {
    orphans: 3;
    widows: 3;
  }

  h1, h2, h3, h4 {
    page-break-after: avoid;
    break-after: avoid;
  }

  /* Keep headings with following content */
  h1, h2, h3, h4, blockquote, pre, table {
    page-break-inside: avoid;
    break-inside: avoid;
  }

  /* Show URLs after links */
  a[href]::after {
    content: " (" attr(href) ")";
    font-size: 0.85em;
    color: #666;
    font-style: italic;
  }

  /* Don't show URL for internal/anchor links or site title */
  a[href^="#"]::after,
  a[href^="/"]::after,
  .site-title::after {
    content: none;
  }

  /* Clean up link styles for print */
  a {
    color: #000;
    text-decoration: underline;
    text-decoration-color: #999;
  }

  /* Tables: ensure borders print */
  thead th {
    background-color: #eee !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  /* Code blocks: subtle background for print */
  pre {
    border: 1px solid #ccc;
    background-color: #f5f5f5 !important;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
    white-space: pre-wrap;
    word-wrap: break-word;
  }
}
