/* ============================================================
   vikram.codes — stylesheet
   Modern CSS: oklch · clamp · logical props · @layer · light-dark()
   ============================================================ */

/* ── Cascade layers: explicit order beats specificity wars ── */
@layer reset, base, layout, components;

/* ================================================================
   DESIGN TOKENS
   ================================================================ */
:root {
  /* Fonts */
  --font-heading: "Newsreader", serif;
  --font-body: "Instrument Sans", sans-serif;
  --font-mono: "IBM Plex Mono", monospace;

  /* Tell the browser which schemes we support — enables light-dark() */
  color-scheme: light dark;

  /* ── Colors via light-dark(): one declaration, both themes ── */
  --color-bg: light-dark(#faf7f2, #1e1e20);
  --color-text: light-dark(#141b2d, #e3e0d7);
  --color-accent: light-dark(
    oklch(50% 0.22 262),
    /* blue, light mode */ oklch(68% 0.18 255) /* softer blue, dark mode */
  );
  --color-border: light-dark(oklch(90% 0.008 260), oklch(26% 0.006 260));
  --color-code-bg: light-dark(#1e1e20, #2a2a2d);
  --color-code-text: #e1e1e3;
  --color-shadow: light-dark(oklch(0% 0 0 / 0.06), oklch(0% 0 0 / 0.25));

  /* ── Fluid spacing: scales smoothly between viewports ── */
  --space-xs: clamp(0.375rem, 1vw, 0.5rem);
  --space-sm: clamp(0.75rem, 2vw, 1rem);
  --space-md: clamp(1.25rem, 4vw, 2rem);
  --space-lg: clamp(2rem, 6vw, 4rem);

  /* ── Shared Constants ── */
  --radius-sm: 4px;
  --transition-speed: 0.2s;

  /* Layout */
  --content-width: 54rem;
  --nav-height: 4rem;

  /* TOC */
  --toc-width: 14rem;
  --toc-sticky-top: 20px;
}

/* Manual theme overrides still work for JS toggle buttons */
[data-theme="light"] {
  color-scheme: light;
}
[data-theme="dark"] {
  color-scheme: dark;
}

/* ================================================================
   RESET
   ================================================================ */
@layer reset {
  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

  /* Zero out margins/padding on block elements we always control.
     ul/ol are intentionally excluded — they need their indentation. */
  :is(h1, h2, h3, h4, h5, h6, p, blockquote, figure) {
    margin: 0;
    padding: 0;
  }

  img,
  video {
    max-inline-size: 100%;
    block-size: auto;
    display: block;
  }
}

/* ================================================================
   BASE
   ================================================================ */
@layer base {
  html {
    font-size: 100%;
    /* Prevent layout shift when a scrollbar appears/disappears */
    scrollbar-gutter: stable;
  }

  /* Respect user motion preferences */
  @media (prefers-reduced-motion: no-preference) {
    html {
      scroll-behavior: smooth;
    }
  }

  * {
    transition:
      background-color var(--transition-speed) ease,
      color var(--transition-speed) ease,
      border-color var(--transition-speed) ease,
      fill var(--transition-speed) ease,
      stroke var(--transition-speed) ease;
  }

  body {
    background-color: var(--color-bg);
    color: var(--color-text);
    font-family: var(--font-body);
    line-height: 1.6;
    display: flex;
    flex-direction: column;
    /* dvh accounts for mobile browser chrome; vh does not */
    min-block-size: 100dvh;
  }

  /* ── Fluid, balanced headings ── */
  :is(h1, h2, h3, h4, h5, h6) {
    font-family: var(--font-heading);
    font-weight: 600;
    line-height: 1.2;
    margin-block: var(--space-md) var(--space-sm);
    color: var(--color-text);
    /* Prevents ugly single-word final lines */
    text-wrap: balance;
  }

  h1 {
    font-size: clamp(2.25rem, 6vw, 3rem);
  }
  h2 {
    font-size: clamp(1.75rem, 4vw, 2.25rem);
  }
  h3 {
    font-size: clamp(1.375rem, 3vw, 1.75rem);
  }
  h4 {
    font-size: clamp(1.25rem, 2vw, 1.5rem);
  }
  h5 {
    font-size: 1.25rem;
  }
  h6 {
    font-size: 1rem;
  }

  /* Anchor links clear the nav area */
  :target {
    scroll-margin-block-start: var(--space-md);
  }

  a {
    color: var(--color-accent);
    text-decoration: none;
    transition: opacity var(--transition-speed);
  }

  a:hover {
    opacity: 0.75;
  }

  /* Accessibility: Focus states */
  a:focus-visible,
  button:focus-visible,
  .theme-toggle:focus-visible {
    outline: 2px solid var(--color-accent);
    outline-offset: 4px;
    border-radius: 2px;
  }

  :is(code, pre, .mono) {
    font-family: var(--font-mono);
    /* em instead of rem — scales with surrounding text size */
    font-size: 0.85em;
  }

  pre {
    padding: var(--space-sm) var(--space-md);
    border-radius: var(--radius-sm);
    /* Increased specificity via selector instead of !important */
    body & {
      background-color: var(--color-code-bg);
    }
    color: var(--color-code-text);
    overflow-x: auto;
    /* overflow-wrap replaces word-wrap; no vendor prefixes needed in 2025 */
    overflow-wrap: break-word;
    white-space: pre-wrap;
    margin-block: var(--space-sm);
  }

  /* Restore list indentation globally (ul/ol excluded from reset) */
  ul,
  ol {
    padding-inline-start: 1.5rem;
    margin-block: var(--space-sm);
  }
}

/* ================================================================
   LAYOUT
   ================================================================ */
@layer layout {
  main {
    flex: 1;
    width: 100%;

    /* Modern Grid Layout */
    display: grid;
    /*
       Grid Columns:
       1. Left Gutter / Sidebar Area (min 1rem)
       2. Main Content (max 54rem, centered)
       3. Right Gutter (min 1rem)
    */
    grid-template-columns:
      minmax(1rem, 1fr)
      min(100% - 2rem, var(--content-width))
      minmax(1rem, 1fr);

    align-items: start;
  }

  /* All direct children of main default to the center column */
  main > * {
    grid-column: 2;
  }

  /* Article post inherits the grid to place its children */
  .post {
    grid-column: 1 / -1;
    display: grid;
    /* Replicate the parent grid columns */
    grid-template-columns:
      minmax(1rem, 1fr)
      min(100% - 2rem, var(--content-width))
      minmax(1rem, 1fr);
  }

  .post > * {
    grid-column: 2;
  }

  /* ── Navigation ── */
  /* Used to be #nav-bar, now .nav-bar for lower specificity */
  .nav-bar {
    position: relative;
    display: flex;
    align-items: center;
    justify-content: space-between;
    block-size: var(--nav-height);
    padding-inline: 0.75rem;
    width: 100%;
    max-inline-size: var(--content-width);
    margin-inline: auto;
    background-color: var(--color-bg);
    font-family: var(--font-mono);
  }

  .nav-bar .nav-link {
    color: var(--color-text);
    text-transform: lowercase;
  }

  /* Fallback if HTML not updated with .nav-link class yet */
  .nav-bar a:not(.nav-logo) {
    color: var(--color-text);
    text-transform: lowercase;
  }

  .nav-logo {
    font-weight: 700;
    font-size: 1.1rem;
    color: var(--color-text);
  }

  .nav-links {
    margin-inline-start: auto;
    margin-inline-end: var(--space-sm);
  }

  .nav-links a {
    margin-inline-start: var(--space-sm);
  }

  .nav-bar a.active {
    color: var(--color-accent);
    font-weight: 600;
  }

  /* ── Post header ── */
  .post-header {
    margin-block-end: var(--space-md);
    text-align: center;
    position: relative;
  }

  .post-header h1 {
    font-size: clamp(2.75rem, 8vw, 4.2rem);
    margin-block-end: var(--space-xs);
  }

  /* ── Hero banner ── */
  .hero-wrapper {
    position: relative;
    /* Full-bleed using CSS Grid */
    grid-column: 1 / -1;
    width: 100%;
    margin-block-end: var(--space-md);
    overflow: hidden;
  }

  .hero-banner {
    width: 100%;
    /* clamp replaces the height + min-height pair; dvh fixes mobile chrome */
    block-size: clamp(300px, 60dvh, 700px);
    object-fit: cover;
    display: block;
  }

  .hero-overlay {
    position: absolute;
    /* inset shorthand + transform for centering */
    inset: 50% auto auto 50%;
    transform: translate(-50%, -50%);
    text-align: center;
    color: white;
    z-index: 10;
    /* min() keeps it from touching screen edges */
    width: min(90%, var(--content-width));

    background: oklch(0% 0 0 / 0.4);
    backdrop-filter: blur(15px);
    padding: var(--space-md);
    border-radius: 8px;
    border: 1px solid oklch(100% 0 0 / 0.1);
  }

  .hero-overlay h1 {
    color: white;
    text-shadow: 0 4px 12px oklch(0% 0 0 / 0.8);
    margin-block-start: 0;
    font-size: clamp(2.25rem, 8vw, 4.2rem);
  }

  /* ── Footer ── */
  footer {
    text-align: center;
    padding-block: var(--space-md);
    font-size: 0.9rem;
    opacity: 0.7;
    border-block-start: 1px solid var(--color-border);
    max-inline-size: var(--content-width);
    width: 100%;
    margin-inline: auto;
  }

  .footer-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-inline: 0.75rem;
    font-family: var(--font-mono);
  }

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

/* ================================================================
   COMPONENTS
   ================================================================ */
@layer components {
  /* ── Theme toggle ── */
  .theme-toggle {
    cursor: pointer;
    background: none;
    border: none;
    padding: 0;
    width: 24px;
    block-size: 24px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--color-text);
  }

  .icon {
    width: 16px;
    block-size: 16px;
    fill: currentColor;
  }

  .theme-toggle .icon {
    width: 100%;
    block-size: 100%;
  }

  /* ── Metadata (time, address) ── */
  :is(time, address) {
    font-family: var(--font-mono);
    font-size: 0.9rem;
    color: var(--color-text);
    opacity: 0.8;
    display: inline-block;
    margin-inline: 0.5rem;
  }

  .accent-data {
    color: var(--color-accent);
  }

  /* Remove blue from post header so it doesn't look like a link */
  .post-header .accent-data {
    color: var(--color-text);
  }

  /* Hero overlay overrides: ensure visibility on dark background */
  .hero-overlay :is(time, address) {
    color: oklch(100% 0 0 / 0.9);
    text-shadow: 0 2px 4px oklch(0% 0 0 / 0.8);
  }

  .hero-overlay .accent-data {
    color: #faf8f5;
  }

  /* ── TOC ── */
  .toc-container {
    background-color: var(--color-bg);
    border: 1px solid var(--color-border);
    padding: var(--space-sm);
    margin-block-end: var(--space-md);
    border-radius: var(--radius-sm);
    font-family: var(--font-mono);
    /* Default mobile placement: in flow (grid column 2) */
  }

  .toc-container h2 {
    font-family: var(--font-mono);
    font-size: 1rem;
    text-transform: uppercase;
    margin-block: 0 var(--space-xs);
  }

  .toc-container ul {
    list-style: none;
    padding-inline-start: 0;
    margin: 0;
  }

  .toc-container ul ul {
    padding-inline-start: var(--space-sm);
  }

  .toc-container a {
    display: block;
    padding-block: 0.2rem;
    padding-inline: 0.5rem;
    color: var(--color-text);
    font-size: 0.9rem;
    border-inline-start: 2px solid transparent;
  }

  .toc-container a.active {
    color: var(--color-accent);
    font-weight: 600;
    border-inline-start-color: var(--color-accent);
  }

  /* ── Post content ── */
  .post-content {
    font-size: 1.125rem;
  }

  .post-content img {
    max-inline-size: 100%;
    block-size: auto;
    border-radius: var(--radius-sm);
    cursor: zoom-in;
    margin-inline: auto;
  }

  .post-content figure {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-block: var(--space-md);
  }

  .post-content figcaption {
    margin-block-start: var(--space-xs);
    font-size: 0.875rem;
    opacity: 0.8;
    text-align: center;
  }

  .post-content p {
    margin-block-end: var(--space-sm);
  }

  .post-content li {
    margin-block-end: 0.25rem;
  }

  .post-content table {
    width: 100%;
    border-collapse: collapse;
    margin-block: var(--space-sm);
  }

  .post-content :is(th, td) {
    padding: var(--space-xs) var(--space-sm);
    border: 1px solid var(--color-border);
    text-align: start;
  }

  .post-content th {
    background-color: light-dark(oklch(0% 0 0 / 0.05), oklch(100% 0 0 / 0.05));
    font-weight: 600;
  }

  /* ── Accordions ── */
  details {
    margin-block-end: var(--space-sm);
    border-block-end: 1px solid var(--color-border);
  }

  summary {
    cursor: pointer;
    font-family: var(--font-mono);
    padding: var(--space-sm);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    list-style: none;
    font-weight: 500;
    transition: background-color var(--transition-speed) ease;
  }

  summary::-webkit-details-marker {
    display: none;
  }

  details[open] summary {
    border-block-end: 1px solid var(--color-border);
    border-end-start-radius: 0;
    border-end-end-radius: 0;
    background-color: light-dark(oklch(0% 0 0 / 0.02), oklch(100% 0 0 / 0.02));
  }

  .accordion-content {
    padding-block: var(--space-sm);
  }

  /* Opening animation via @starting-style */
  details[open] .accordion-content {
    opacity: 1;
    transform: translateY(0);
    transition:
      opacity 0.25s ease,
      transform 0.25s ease;
  }

  @starting-style {
    details[open] .accordion-content {
      opacity: 0;
      transform: translateY(-6px);
    }
  }

  /* ── Blockquotes ── */
  blockquote {
    margin-block: var(--space-md);
    border-inline-start: 4px solid var(--color-accent);
    background: oklch(50% 0 0 / 0.04);
    padding-block: var(--space-xs);
    padding-inline: var(--space-md) var(--space-sm);
    font-style: italic;
  }

  blockquote p:first-child {
    margin-block-start: 0;
  }
  blockquote p:last-child {
    margin-block-end: 0;
  }

  /* ── Tags ── */
  .post-tags {
    margin-block-start: var(--space-md);
    padding-block-start: var(--space-sm);
    border-block-start: 1px solid var(--color-border);
  }

  .tag {
    display: inline-block;
    font-family: var(--font-mono);
    margin-inline-end: var(--space-xs);
    font-size: 0.9rem;
  }

  /* ── Post lists ── */
  :is(.posts, .content-list) {
    list-style: none;
    padding: 0;
    margin-block: var(--space-md);
  }

  :is(.posts li, .title-list) {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
    padding-block: var(--space-sm);
    border-block-end: 1px solid var(--color-border);
  }

  :is(.posts li:last-child, .title-list:last-child) {
    border-block-end: none;
  }

  :is(.posts a, .content-link) {
    font-size: 1.25rem;
    font-weight: 500;
    font-family: var(--font-heading);
    color: var(--color-text);
    transition: color var(--transition-speed) ease;
  }

  :is(.posts a:hover, .content-link:hover) {
    color: var(--color-accent);
  }

  :is(.date, .content-meta) {
    font-family: var(--font-mono);
    font-size: 0.9rem;
    color: var(--color-text);
    opacity: 0.6;
    flex-shrink: 0;
    margin-inline-start: var(--space-sm);
  }

  /* ── Pagination ── */
  .pagination {
    margin-block-start: var(--space-lg);
    display: flex;
    justify-content: center;
  }

  .pagination-inner {
    display: flex;
    align-items: center;
    gap: var(--space-sm);
  }

  .page-item {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    block-size: 32px;
    border-radius: 50%;
    border: 1px solid var(--color-border);
    color: var(--color-text);
    transition:
      border-color var(--transition-speed) ease,
      color var(--transition-speed) ease;
  }

  .page-item:hover {
    border-color: var(--color-accent);
    color: var(--color-accent);
  }

  .page-current {
    font-family: var(--font-mono);
    font-size: 0.9rem;
  }

  /* ── Lightbox ── */
  .lightbox-overlay {
    position: fixed;
    inset: 0;
    background-color: oklch(0% 0 0 / 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1000;
    cursor: zoom-out;
    opacity: 0;
    transition: opacity 0.3s ease;
    pointer-events: none;
  }

  .lightbox-overlay.active {
    opacity: 1;
    pointer-events: auto;
  }

  .lightbox-image {
    max-inline-size: 90%;
    max-block-size: 90%;
    object-fit: contain;
    transform: scale(0.9);
    transition: transform 0.3s ease;
    border-radius: var(--radius-sm);
    box-shadow: 0 0 20px oklch(0% 0 0 / 0.5);
  }

  .lightbox-overlay.active .lightbox-image {
    transform: scale(1);
  }
}

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

/* TOC: sidebar layout on wide screens */
@media (width >= 1400px) {
  .toc-container {
    /* Place in grid column 1 */
    grid-column: 1;
    justify-self: end;

    /* Sticky behavior */
    position: sticky;
    top: var(--toc-sticky-top);

    /* Dimensions & Spacing */
    width: var(--toc-width);
    height: max-content;
    margin-right: var(--space-md);
    margin-top: var(--space-lg);
    margin-bottom: 0;

    /* Reset mobile styles */
    border: none;
    background: transparent;
    padding: 0;
    text-align: start;
  }

  .post-content {
    /* Normal alignment for content */
    margin-top: 0;
  }

  .toc-container h2 {
    font-size: 0.9rem;
    opacity: 0.8;
  }
}

/* clamp() on headings handles most fluid scaling,
   but the hero overlay still benefits from a nudge */
@media (width < 768px) {
  .hero-overlay {
    width: 95%;
    padding: var(--space-sm);
  }
}
