:root {
  /* Fonts */
  --font-heading: 'Merriweather', serif;
  --font-body: 'Inter', sans-serif;
  --font-mono: 'Fira Code', monospace;

  /* Base Sizes */
  --base-font-size: 16px; /* Adjust as needed */
  --base-line-height: 1.7;
  --base-spacing-unit: calc(var(--base-line-height) * 1rem); /* ~27.2px for 16px base */

  /* Colors */
  --color-bg: #ffffff;
  --color-text: hsl(210, 10%, 15%);
  --color-muted: #6c757d;
  --color-border: #dee2e6;
  /* Primary link color changed to old-patine brown */
  --color-link: #5a381e;
  --color-link-hover: #3e220f;
  --color-code-bg: #f8f9fa;
  --color-code-text: #212529;
  --color-blockquote-bg: #f8f9fa;
  --color-blockquote-border: #ced4da;

  /* Layout */
  --content-max-width: 800px;
}

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

/* Base styles & Vertical Rhythm */
html {
  /* Base font size: desktop default */
  font-size: var(--base-font-size);
}

body {
  display: grid;
  grid-template-rows: auto 1fr auto;
  grid-template-areas:
    "header"
    "main"
    "footer";
  min-height: 100vh;
  font-family: var(--font-body);
  line-height: var(--base-line-height);
  color: var(--color-text);
  background-color: #f9f9f9; /* Slightly off-white background */
  /* Improve text density with subtle letter-spacing */
  letter-spacing: 0.02em;
  /* Enable ligatures, oldstyle numbers, text smoothing */
  font-variant-ligatures: common-ligatures discretionary-ligatures contextual;
  font-variant-numeric: oldstyle-nums proportional-nums;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  /* Widow & orphan control */
  widows: 2;
  orphans: 2;
}
/* Hyphenation and word wrapping for long text */
p, ul, ol {
  hyphens: auto;
  overflow-wrap: break-word;
  word-break: break-word;
}

/* Layout Areas */
.site-header {
  grid-area: header;
  background-color: var(--color-bg);
  border-bottom: 1px solid var(--color-border);
  padding: var(--base-spacing-unit) calc(var(--base-spacing-unit) / 2);
  text-align: center;
}
/* Header link styling: replace default blue with dark brown for aged-paper vibe */
.site-header a {
  color: #5a381e;
  text-decoration: none;
}
.site-header a:hover {
  color: #3e220f;
  text-decoration: underline;
}
.site-header h1 {
  margin-top: 0;
  margin-bottom: calc(var(--base-spacing-unit) / 4);
  font-size: 1.8rem; /* Adjust */
}
.site-header p {
  margin-bottom: 0;
  color: var(--color-muted);
  font-size: 0.9rem;
}

.site-main {
  grid-area: main;
  /* Optimal measure: limit lines to ~80 characters */
  max-width: min(80ch, var(--content-max-width));
  margin: 0 auto;
  padding: var(--base-spacing-unit) calc(var(--base-spacing-unit) / 2);
  width: 100%;
}

.site-footer {
  grid-area: footer;
  background-color: var(--color-bg);
  border-top: 1px solid var(--color-border);
  padding: calc(var(--base-spacing-unit) / 2);
  text-align: center;
  color: var(--color-muted);
  font-size: 0.85rem;
}

/* Typography Scale & Rhythm */
h1, h2, h3, h4, h5, h6 {
  font-family: var(--font-heading);
  margin-top: calc(var(--base-spacing-unit) * 1.5);
  margin-bottom: var(--base-spacing-unit);
  line-height: 1.3;
  font-weight: 700;
  /* Slight letter-spacing for headings */
  letter-spacing: 0.02em;
}

h1 { font-size: clamp(2rem, 5vw, 3rem); }
h2 { font-size: clamp(1.75rem, 4vw, 2.5rem); }
h3 { font-size: clamp(1.5rem, 3vw, 2rem); }
h4 { font-size: clamp(1.25rem, 2.5vw, 1.75rem); }
h5 { font-size: clamp(1.125rem, 2vw, 1.5rem); }
h6 { font-size: clamp(0.875rem, 1.5vw, 1.25rem); font-weight: 600; }

p,
ul,
ol,
blockquote,
pre,
table,
.codehilite {
  margin-bottom: var(--base-spacing-unit);
}

/* Ensure first element doesn't have top margin */
.site-main > :first-child,
article.post-content > :first-child {
  margin-top: 0;
}

/* Links */
a {
  color: var(--color-link);
  text-decoration: none;
}
a:hover {
  color: var(--color-link-hover);
  text-decoration: underline;
}

/* Lists */
ul,
ol {
  padding-left: calc(var(--base-spacing-unit) * 1); /* Indentation based on rhythm */
}
li {
  margin-bottom: calc(var(--base-spacing-unit) / 4);
}

/* Code Styling */
/* Inline code */
code {
  font-family: var(--font-mono);
  background-color: var(--color-code-bg);
  color: var(--color-code-text);
  padding: 0.2em 0.4em;
  border-radius: 3px;
  font-size: 0.9em;
  word-wrap: break-word;
}

/* Code blocks (codehilite container only) */
.codehilite {
  font-family: var(--font-mono);
  /* Subtle grainy yellow background */
  background-color: #fffbe7;
  background-image:
    repeating-linear-gradient(45deg, rgba(0,0,0,0.02) 0 1px, transparent 1px 2px),
    repeating-linear-gradient(-45deg, rgba(0,0,0,0.02) 0 1px, transparent 1px 2px);
  background-size: 3px 3px;
  padding: var(--base-spacing-unit);
  /* Wrap long lines to container width */
  white-space: pre-wrap;
  word-break: break-word;
  /* Remove horizontal scroll */
  overflow-x: hidden;
  border-radius: 5px;
  font-size: 0.9em;
  line-height: var(--base-line-height); /* Maintain rhythm inside code */
  border: 1px solid var(--color-border);
  /* Differentiate code blocks visually */
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.04);
  position: relative; /* For copy button positioning */
}
/* Ensure nested <pre> inside codehilite wraps lines */
.codehilite pre {
  white-space: pre-wrap !important;
  word-break: break-word;
}

/* Copy button inside code blocks */
/* Copy button inside code blocks */
/* Copy button inside code blocks */
/* Copy button inside code blocks */
.codehilite .copy-btn {
  position: absolute;
  top: 0.5rem;
  right: 0.5rem;
  background: rgba(0, 0, 0, 0.05);
  color: #000;
  border: none;
  border-radius: 3px;
  padding: 0.2em 0.6em;
  font-size: 0.75em;
  cursor: pointer;
  transition: background-color 0.2s ease, transform 0.1s ease;
  z-index: 10;
}
/* Show button when hovering over codehilite container */
/* On hover, make the button fully visible */
/* On container hover, darken button slightly */
.codehilite:hover .copy-btn {
  background: rgba(0, 0, 0, 0.1);
}
/* Button hover state */
/* On button hover, darken further */
.codehilite .copy-btn:hover {
  background: rgba(0, 0, 0, 0.15);
}
/* Button copied state */
/* Copied state: static gray */
.codehilite .copy-btn.copied {
  background: rgba(0, 0, 0, 0.15);
}

/* On button press, simulate depth */
.codehilite .copy-btn:active {
  background: rgba(0, 0, 0, 0.2);
  transform: translateY(1px);
}
/* Reset styles for code inside pre/codehilite */
pre code,
.codehilite pre code {
  background: none;
  padding: 0;
  font-size: inherit; /* Inherit from parent pre */
  line-height: inherit;
  color: inherit;
  border-radius: 0;
}
.codehilite pre {
  margin: 0; /* Remove default margin */
  padding: 0; /* Remove default padding */
  background: none;
  border: none;
  overflow: visible; /* Let outer container handle scroll */
}

/* Images */
img {
  max-width: 100%;
  height: auto;
  display: block;
  margin-bottom: var(--base-spacing-unit);
  border-radius: 4px; /* Optional: slightly rounded corners */
}

/* Tables */
table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: var(--base-spacing-unit);
  font-size: 0.95em;
}
th,
td {
  border: 1px solid var(--color-border);
  padding: calc(var(--base-spacing-unit) / 3);
  text-align: left;
  vertical-align: top;
}
th {
  background-color: var(--color-code-bg); /* Use code bg for consistency */
  font-weight: 600;
}

/* Blockquotes */
blockquote {
  /* Accent blockquote with link color and refined background */
  border-left: 0.25em solid var(--color-link);
  padding: calc(var(--base-spacing-unit) / 1.5) var(--base-spacing-unit);
  background-color: #f0f4f8;
  color: var(--color-muted);
  margin: 0 0 var(--base-spacing-unit);
  font-style: italic;
}
blockquote p:last-child {
  margin-bottom: 0; /* Remove margin from last paragraph inside quote */
}

/* Post Specific Styles */
.post-content h1 {
  margin-bottom: calc(var(--base-spacing-unit) / 2); /* Less space after main title */
}
.post-meta {
  color: var(--color-muted);
  font-size: 0.9rem;
  margin-bottom: var(--base-spacing-unit);
  margin-top: calc(-1 * var(--base-spacing-unit) / 2); /* Pull up slightly */
}
.post-meta a {
    color: var(--color-muted);
    text-decoration: underline;
}
.post-meta a:hover {
    color: var(--color-text);
}


/* Add styles for codehilite syntax classes if needed, e.g.: */
/* .codehilite .k { color: #0077aa; font-weight: bold; } */
/* .codehilite .s { color: #dd1144; } */
/* .codehilite .c { color: #999988; font-style: italic; } */
/* ... etc. ... */

/* Responsive adjustments */
@media (max-width: 600px) {
  /* Responsive typography: larger base font on mobile (book-like) */
  html {
    font-size: clamp(16px, 6vw, 16px);
  }
  /* Enhance leading and letter-spacing for mobile readability */
  body {
    line-height: 1.8; /* Increase leading for large text */
    letter-spacing: 0.03em; /* Maintain adequate letter spacing */
  }
  /* Ensure adequate padding and spacing */
  .site-main {
    /* Increase side padding for mobile readability */
    padding: calc(var(--base-spacing-unit) * 1.5);
  }
  /* Slightly larger paragraph spacing for mobile */
  p, ul, ol, blockquote {
    margin-bottom: calc(var(--base-spacing-unit) * 1.2);
  }
  /* Adjust headings for mobile scale */
  h1 { font-size: 2rem; }
  h2 { font-size: 1.75rem; }
  h3 { font-size: 1.5rem; }
}
