/* ============================================================================
   base.css — the design system + all shared components.

   Everything visual is driven by CSS custom properties, so the whole look
   (colour, type, spacing) can be re-skinned by editing the tokens in :root
   without touching a single layout. Section-specific bits live in
   articles.css / blog.css / book.css; syntax colours in chroma.css.

   Editorial theme: serif reading column on warm paper, a clean sans for UI,
   one restrained accent per content shape, full light + dark support.
   ========================================================================== */

/* --- Design tokens: light (default) --------------------------------------- */
:root {
  /* Palette — warm paper, warm ink */
  --bg:        #faf8f2;
  --surface:   #f2eee2;   /* asides, code, quiet panels */
  --surface-2: #fffdf7;   /* cards that should read as "raised" off the paper */
  --fg:        #262219;
  --fg-strong: #16130c;
  --muted:     #6d6656;
  --line:      #e4ddcc;
  --line-soft: #efe9db;

  /* Accent family — one hue per content shape, all muted for print-like calm */
  --accent-rust:  #9c4a24;
  --accent-blue:  #35548a;
  --accent-teal:  #2c6b5c;
  --accent-brown: #855321;
  --accent: var(--accent-rust);          /* default; overridden per shape */
  --accent-ink: color-mix(in srgb, var(--accent) 70%, var(--fg));

  /* Type */
  --font-serif: "Iowan Old Style", "Palatino Linotype", Palatino, "Book Antiqua",
                Charter, Georgia, Cambria, "Times New Roman", serif;
  --font-ui:    system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
                Helvetica, Arial, sans-serif;
  --font-mono:  ui-monospace, "SF Mono", "Cascadia Code", Menlo, Consolas,
                "Liberation Mono", monospace;

  /* Rhythm & shape */
  --measure: 40rem;   /* comfortable serif reading width (~68ch)  */
  --wrap:    46rem;   /* general single-column pages              */
  --wide:    64rem;   /* content + side aside                     */
  --radius:  8px;
  --radius-lg: 14px;
  --shadow:  0 1px 2px rgba(30, 24, 12, 0.04), 0 4px 16px rgba(30, 24, 12, 0.05);

  color-scheme: light;
}

/* --- Design tokens: dark -------------------------------------------------- */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:        #1b1a16;
    --surface:   #24221b;
    --surface-2: #26241d;
    --fg:        #e7e2d4;
    --fg-strong: #f6f1e4;
    --muted:     #a49c8a;
    --line:      #34312a;
    --line-soft: #2c2a23;

    --accent-rust:  #e79c78;
    --accent-blue:  #8fb4ec;
    --accent-teal:  #79c3ad;
    --accent-brown: #d3a869;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 18px rgba(0, 0, 0, 0.35);
    color-scheme: dark;
  }
}
/* Explicit toggle wins over the OS preference in both directions. */
:root[data-theme="dark"] {
  --bg:        #1b1a16;
  --surface:   #24221b;
  --surface-2: #26241d;
  --fg:        #e7e2d4;
  --fg-strong: #f6f1e4;
  --muted:     #a49c8a;
  --line:      #34312a;
  --line-soft: #2c2a23;
  --accent-rust:  #e79c78;
  --accent-blue:  #8fb4ec;
  --accent-teal:  #79c3ad;
  --accent-brown: #d3a869;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 18px rgba(0, 0, 0, 0.35);
  color-scheme: dark;
}

/* --- Reset-ish ------------------------------------------------------------ */
*, *::before, *::after { box-sizing: border-box; }
html { font-size: 112.5%; -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) { html { scroll-behavior: auto; } }

body {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 1rem;
  line-height: 1.72;
  color: var(--fg);
  background: var(--bg);
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}
main { flex: 1 0 auto; }

.muted { color: var(--muted); }
.wrap  { max-width: var(--wrap); margin: 0; padding: 3rem 1.5rem; }

/* --- Per-shape accent ----------------------------------------------------- */
body.shape-default  { --accent: var(--accent-rust); }
body.shape-articles { --accent: var(--accent-blue); }
body.shape-blog     { --accent: var(--accent-teal); }
body.shape-book     { --accent: var(--accent-brown); }

/* Section paper tint: warm for the topic-organised Writing, cool for the
   chronological blog. color-mix keeps it relative to --bg, so it adapts to dark. */
body.shape-articles { background: color-mix(in srgb, var(--bg) 92%, #b8722f); }
body.shape-blog     { background: color-mix(in srgb, var(--bg) 93%, var(--accent-teal)); }

/* --- Typography ----------------------------------------------------------- */
h1, h2, h3, h4 {
  font-family: var(--font-serif);
  color: var(--fg-strong);
  line-height: 1.18;
  font-weight: 700;
  letter-spacing: -0.01em;
}
h1 { font-size: clamp(1.9rem, 1.4rem + 2.2vw, 2.7rem); margin: 0 0 0.6rem; }
h2 { font-size: 1.5rem; margin: 2.2rem 0 0.7rem; }
h3 { font-size: 1.2rem; margin: 1.8rem 0 0.5rem; }
p  { margin: 0 0 1.1rem; }

a { color: var(--accent-ink); text-underline-offset: 0.14em; text-decoration-thickness: 0.06em; }
a:hover { color: var(--accent); }

hr { border: 0; border-top: 1px solid var(--line); margin: 2.5rem 0; }
small { font-size: 0.82rem; }

/* --- Prose (rendered .Content) ------------------------------------------- */
.prose { font-size: 1.06rem; }
.prose > :first-child { margin-top: 0; }
.prose h2 { padding-top: 0.2rem; }
.prose a { text-decoration: underline; text-decoration-color: color-mix(in srgb, var(--accent) 45%, transparent); }
.prose a:hover { text-decoration-color: var(--accent); }
.prose ul, .prose ol { padding-left: 1.3rem; margin: 0 0 1.1rem; }
.prose li { margin: 0.3rem 0; }
.prose img { max-width: 100%; height: auto; border-radius: var(--radius); }
.prose blockquote {
  margin: 1.4rem 0; padding: 0.3rem 1.2rem;
  border-left: 3px solid var(--accent);
  color: var(--muted); font-style: italic;
}
.prose blockquote p:last-child { margin-bottom: 0; }
.prose table {
  border-collapse: collapse; width: 100%; margin: 1.4rem 0;
  font-family: var(--font-ui); font-size: 0.92rem;
}
.prose th, .prose td { border: 1px solid var(--line); padding: 0.5rem 0.7rem; text-align: left; }
.prose th { background: var(--surface); font-weight: 600; }

/* Code — monospace, tinted panels; token colours come from chroma.css */
code, kbd, samp { font-family: var(--font-mono); font-size: 0.88em; }
.prose :not(pre) > code {
  background: var(--surface); border: 1px solid var(--line-soft);
  padding: 0.08em 0.36em; border-radius: 5px;
}
.prose pre {
  background: var(--surface); border: 1px solid var(--line);
  border-radius: var(--radius); padding: 0.9rem 1.1rem;
  overflow-x: auto; margin: 1.3rem 0; line-height: 1.55;
}
.prose pre code { background: none; border: 0; padding: 0; font-size: 0.85rem; }

/* --- Top navigation ------------------------------------------------------- */
.topnav {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 1.5rem;
  padding: 0.7rem clamp(1rem, 4vw, 2rem);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(8px);
  -webkit-backdrop-filter: saturate(1.4) blur(8px);
  border-bottom: 3px solid var(--accent);
  font-family: var(--font-ui);
}
.topnav .brand {
  font-weight: 700; font-size: 1.02rem; letter-spacing: -0.01em;
  color: var(--fg-strong); text-decoration: none; white-space: nowrap;
}
.topnav .brand:hover { color: var(--accent); }
.topnav .nav-links { display: flex; gap: 0.3rem; flex-wrap: wrap; }
.topnav .nav-links a {
  text-decoration: none; color: var(--muted);
  font-size: 0.92rem; padding: 0.3rem 0.6rem; border-radius: 6px;
}
.topnav .nav-links a:hover { color: var(--fg-strong); background: var(--surface); }
.topnav .nav-links a.active { color: var(--accent); font-weight: 600; }

/* Copyright footer — page chrome, aligned with the nav's horizontal padding. */
.site-footer {
  margin-top: 3rem;
  padding: 1.5rem clamp(1rem, 4vw, 2rem);
  border-top: 1px solid var(--line);
  color: var(--muted); font-size: 0.85rem; font-family: var(--font-ui);
}
.site-footer p { margin: 0; }

/* --- Chips (categories / tags) ------------------------------------------- */
.chip {
  display: inline-block;
  font-family: var(--font-ui); font-size: 0.76rem; line-height: 1.4;
  padding: 0.16rem 0.6rem; border: 1px solid var(--line); border-radius: 999px;
  text-decoration: none; color: var(--muted); background: var(--surface);
}
.chip:hover { border-color: var(--accent); color: var(--accent); }
.chip.cat { border-color: color-mix(in srgb, var(--accent) 55%, var(--line)); color: var(--accent); }

/* --- Backlinks panel ------------------------------------------------------ */
.backlinks {
  border: 1px solid var(--line); border-left: 3px solid var(--accent);
  border-radius: var(--radius); padding: 0.9rem 1.1rem;
  background: var(--surface); font-family: var(--font-ui); font-size: 0.9rem;
}
.backlinks h3 {
  margin: 0 0 0.55rem; font-family: var(--font-ui);
  font-size: 0.72rem; text-transform: uppercase; letter-spacing: 0.07em;
  color: var(--muted); font-weight: 700;
}
.backlinks ul { list-style: none; padding: 0; margin: 0; }
.backlinks li { margin: 0.4rem 0; line-height: 1.4; }
.backlinks a { text-decoration: none; color: var(--accent-ink); }
.backlinks a:hover { color: var(--accent); text-decoration: underline; }
.backlinks-sub {
  margin: 0.9rem 0 0.35rem; padding-top: 0.75rem;
  border-top: 1px solid var(--line);
  font-family: var(--font-ui); font-size: 0.85rem; font-weight: 600;
  letter-spacing: 0; color: var(--fg-strong);
}
/* First group sits directly under the heading — no divider needed there. */
.backlinks h3 + .backlinks-sub { margin-top: 0; padding-top: 0; border-top: 0; }

/* --- Section cards (home) ------------------------------------------------- */
.shape-cards {
  display: grid; gap: 1rem;
  grid-template-columns: repeat(auto-fit, minmax(15rem, 1fr));
  margin-top: 1.5rem;
}
.shape-card {
  display: block; padding: 1.2rem 1.3rem;
  border: 1px solid var(--line); border-radius: var(--radius-lg);
  background: var(--surface-2); text-decoration: none; color: inherit;
  transition: border-color .15s, transform .15s;
}
.shape-card:hover { border-color: var(--accent); transform: translateY(-2px); }
.shape-card h2 { margin: 0 0 0.35rem; font-size: 1.2rem; }
.shape-card p { margin: 0; color: var(--muted); font-size: 0.95rem; font-family: var(--font-ui); }

/* --- Home page ------------------------------------------------------------ */
.home { max-width: var(--wide); margin: 0; padding: 3rem 1.5rem; }
.home-intro { max-width: var(--measure); }
.home-intro h1 { font-size: clamp(2.1rem, 1.5rem + 3vw, 3.1rem); }
.home-intro .lede { font-size: 1.12rem; }

.home-cols {
  display: grid; gap: 2.5rem 3rem;
  grid-template-columns: 1.3fr 1fr; margin: 3rem 0 1rem;
}
.home-cols h2 {
  font-family: var(--font-ui); font-size: 0.8rem; font-weight: 700;
  text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted);
  border-bottom: 1px solid var(--line); padding-bottom: 0.5rem; margin: 0 0 0.9rem;
}

.post-list { list-style: none; padding: 0; margin: 0; }
.post-list li { margin: 0.7rem 0; display: flex; gap: 0.9rem; align-items: baseline; }
.post-list time {
  color: var(--muted); font-family: var(--font-ui); font-size: 0.78rem;
  white-space: nowrap; flex: 0 0 4.2rem;
}
.post-list a { text-decoration: none; font-size: 1.02rem; }
.post-list a:hover { text-decoration: underline; }
.more { margin-top: 1.1rem; font-family: var(--font-ui); font-size: 0.9rem; }
.more a { text-decoration: none; font-weight: 600; }

.cat-list { list-style: none; padding: 0; margin: 0;
  display: flex; flex-direction: column; gap: 0.15rem; }
.cat-list li { margin: 0; }
.cat-list a { text-decoration: none; font-weight: 600; }
.cat-list a:hover { text-decoration: underline; }
.cat-list .muted { font-family: var(--font-ui); font-size: 0.82rem; }

/* --- Responsive ----------------------------------------------------------- */
@media (max-width: 44rem) {
  .home-cols { grid-template-columns: 1fr; gap: 1.8rem; }
  .topnav { gap: 0.8rem; }
  .topnav .brand { font-size: 0.98rem; }
}

/* --- Accessibility -------------------------------------------------------- */
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 3px; }
.skip-link {
  position: absolute; left: -9999px; top: 0;
  background: var(--accent); color: #fff; padding: 0.5rem 1rem; z-index: 100;
  font-family: var(--font-ui); text-decoration: none;
}
.skip-link:focus { left: 0.5rem; top: 0.5rem; border-radius: 6px; }
