/* =============================================================================
   CSS CUSTOM PROPERTIES (VARIABLES)
   ============================================================================= */
:root {
  /* Fonts - doubled deliberately: https://github.com/necolas/normalize.css/issues/519#issuecomment-197131966 */
  --font-mono: monospace, monospace;
  --font-fallback-mono: monospace;

  /* Colors */
  --color-text: #333;
  --color-link: #0066cc;
  --color-link-hover: #0052a3;
  --color-black: #000;
  --color-white: #fff;
  --color-gray-light: #f0f0f0;
  --color-gray-lighter: #f5f5f5;
  --color-gray-lightest: #f8f8f8;
  --color-gray: #666; /* Improved from #999 for better contrast */
  --color-gray-dark: #666;
  --color-gray-darker: #555;
  --color-darkgray: #545454;
  --color-border-light: #eee;
  --color-border: #e5e5e5;
  --color-border-quote: #e0e0e0;

  /* Note colors */
  --color-info: green;
  --color-info-bg: #d4f4d4;
  --color-warning: orange;
  --color-warning-bg: #ffd580;
  --color-alert: red;
  --color-alert-bg: #ffd5d5;

  /* Draft post styling */
  --color-draft: #cc0000; /* Red for draft posts */
  --color-draft-bg: #ffe6e6; /* Light red background */

  --max-width: 120ch;
}

/* =============================================================================
   RESET & BASE
   ============================================================================= */
* {
  box-sizing: border-box;
}

html {
  overflow-x: hidden;
  overflow-y: scroll; /* Always show scrollbar to prevent layout shift */
}

/* Skip to content link (accessible but hidden until focused) */
.skip-to-content {
  position: absolute;
  top: -40px;
  left: -100%;
  background: var(--color-black);
  color: var(--color-white);
  padding: 8px;
  text-decoration: none;
  z-index: 100;

  &:focus {
    top: 0;
    left: 0;
  }
}

html,
body {
  line-height: 1.65;
  color: var(--color-text);
}

body {
  font-family: var(--font-mono);
  display: flex;
  align-items: center;
  justify-content: center;
  max-width: var(--max-width);
  width: 100%;
  margin: 0 auto;
  flex-flow: column;
  padding: 0 1rem;
}

/* =============================================================================
   TYPOGRAPHY
   ============================================================================= */
h1 {
  margin-top: 0.5em;
  margin-bottom: 0.5em;
  font-weight: bold;
}

h1.title {
  border-bottom: 1px solid var(--color-black);
  margin-bottom: 0;
  margin-top: 0.5em;
  font-family: var(--font-mono);
  font-size: 150%;
  width: 100%;
}

h2 {
  font-size: 1.5em;
  line-height: 1.4;
  margin-top: 1.5em;
  margin-bottom: 0.6em;
  font-weight: bold;
}

h3 {
  font-size: 1.25em;
  line-height: 1.4;
  margin-top: 1.3em;
  margin-bottom: 0.5em;
  font-weight: 600;
}

h4 {
  font-size: 1.1em;
  line-height: 1.5;
  margin-top: 1.2em;
  margin-bottom: 0.4em;
  font-weight: 600;
}

h5,
h6 {
  font-size: 1em;
  line-height: 1.5;
  margin-top: 1em;
  margin-bottom: 0.3em;
  font-weight: 600;
  font-style: italic;
}

p {
  text-justify: inter-word;
  text-wrap: pretty;
  margin-bottom: 1.2em;
  text-align: justify;
}

main > article p {
  font-family: var(--font-mono);
  text-rendering: optimizeLegibility;
  line-height: 1.65;
  hyphens: auto;
  -webkit-hyphens: auto;
  -moz-hyphens: auto;
}

/* =============================================================================
   LINKS
   ============================================================================= */
a {
  color: var(--color-link);
  text-decoration-thickness: 1px;
  text-underline-offset: 0.2em;

  &:hover {
    color: var(--color-link-hover);
    text-decoration-thickness: 2px;
  }

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

button {
  transition: all 0.2s ease;
}

/* =============================================================================
   HEADER & NAVIGATION
   ============================================================================= */
header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  border: 5px solid var(--color-black);
  width: 100%;
  margin-top: 1em;
  font-family: var(--font-mono);
  font-weight: bold;
  padding: 0.5em;

  nav {
    display: flex;
    gap: 0.2em;

    a {
      padding: 0.1em 0.3em;
      color: inherit;
      text-decoration: none;

      &.active {
        background-color: var(--color-black);
        color: var(--color-white);
      }

      &:hover:not(.active) {
        text-decoration: underline;
      }
    }
  }

  a.branding {
    color: inherit;
    text-decoration: none;

    &:hover {
      text-decoration: underline;
    }
  }
}

/* =============================================================================
   MAIN CONTENT AREAS
   ============================================================================= */
main {
  width: 100%;
  max-width: 100%;

  /* Homepage list styling */
  .home-content > ul,
  article > ul:not(.toc-list):not(.posts) {
    list-style: none;
    padding-left: 0;
    margin: 1.5em 0;

    > li {
      position: relative;
      padding-left: 1.8em;
      margin-bottom: 0.6em;
      line-height: 1.5;

      &::before {
        content: "→";
        position: absolute;
        left: 0;
        color: var(--color-black);
        font-weight: bold;
        font-size: 1.1em;
      }

      /* Alternative markers to try:
               content: "▶";
               content: "►";
               content: "→";
               content: "⟶";
               content: "☛";
               content: "▪";
               content: "◆"; */
    }
  }
}

article {
  width: 100%;
  max-width: 100%;
  overflow-wrap: break-word;
  word-wrap: break-word;
  padding-top: 1em;
  padding-bottom: 2em;

  pre {
    overflow-x: auto;
    max-width: 100%;
  }

  img {
    max-width: 100%;
    height: auto;
  }

  ol > li {
    list-style: square;
  }

  /* Style Zola anchor links (now just a # from our template) */
  h2,
  h3,
  h4,
  h5,
  h6 {
    position: relative;

    .zola-anchor {
      position: absolute;
      left: -1.5em;
      text-decoration: none;
      color: var(--color-gray);
      font-weight: normal;
      opacity: 0;
      transition: opacity 0.2s;
    }

    &:hover .zola-anchor {
      opacity: 1;
    }
  }
}

/* =============================================================================
   TABLE OF CONTENTS
   ============================================================================= */
div.toc-container {
  ul.toc-list {
    padding-left: 0;
    margin: 0;
  }
  ul {
    margin: 0;
    padding-top: 0;
    list-style: none;

    a {
      color: var(--color-gray);
      text-decoration: none;
      font-size: 0.75em;
      line-height: 1.4;
      transition: all 0.2s ease;

      &:hover {
        color: var(--color-black);
        text-decoration: underline;
      }

      &.active {
        color: var(--color-black);
        font-weight: bold;
      }
    }

    /* Top link styling */
    li.toc-top {
      margin-bottom: 0.5em;
      padding-bottom: 0.5em;
      border-bottom: 1px solid var(--color-border-light);

      a {
        color: var(--color-black);
        font-weight: 600;
      }
    }

    /* Nested levels */
    ul {
      padding-left: 0.8em;
      margin-top: 0;
    }
  }
}

/* Mobile: Collapsible TOC */
@media only screen and (max-width: 1199px) {
  div.toc-container {
    border: 3px solid var(--color-black);
    margin: 1em 0;
    background: var(--color-white);

    /* Hide the Top link on mobile */
    li.toc-top {
      display: none;
    }

    /* Toggle button (hidden by default, shown with JS) */
    .toc-toggle {
      display: none;
      background: var(--color-black);
      color: var(--color-white);
      border: none;
      padding: 0.5em 1em;
      font-family: var(--font-mono);
      font-size: 0.75em;
      font-weight: bold;
      text-transform: uppercase;
      cursor: pointer;
      width: 100%;
      text-align: left;
      position: relative;

      &:hover {
        background: var(--color-gray-darker);
      }

      &:focus {
        outline: 3px solid var(--color-white);
        outline-offset: -3px;
      }

      /* Arrow indicator */
      &::after {
        content: "▼";
        position: absolute;
        right: 1em;
        transition: transform 0.3s ease;
      }

      &.collapsed::after {
        transform: rotate(-90deg);
      }
    }

    /* TOC content wrapper */
    .toc-content {
      padding: 0 1em 1em 1em;
      max-height: 50vh;
      overflow-y: auto;
    }

    /* When JS is available and TOC is collapsed */
    &.toc-collapsible {
      .toc-toggle {
        display: block;
      }

      &.toc-collapsed .toc-content {
        display: none;
      }
    }
  }
}

/* Desktop: Fixed sidebar TOC on the right */
@media only screen and (min-width: 1200px) {
  div.toc-container {
    position: fixed;
    left: calc(50% + 60ch + 0.5rem); /* Closer to the 80ch article */
    top: 7.5rem; /* Align with article content start */
    max-width: calc(50vw - 40ch - 1rem); /* Use available space */
    max-height: calc(100vh - 8rem);
    overflow-y: auto;
    padding: 0;
    border: none; /* Remove border on desktop */

    /* Hide toggle button on desktop */
    .toc-toggle {
      /* biome-ignore lint/complexity/noImportantStyles: Required to override inline styles from JavaScript */
      display: none !important;
    }

    /* Reset content wrapper on desktop */
    .toc-content {
      padding: 0;
      max-height: none;
      overflow-y: visible;
    }

    /* Prevent line wrapping in TOC links */
    a {
      white-space: nowrap;
    }
  }
}

/* =============================================================================
   LISTS
   ============================================================================= */

/* Post lists */
ul.posts {
  list-style: none;
  margin: 1em 0 0 0;
  padding: 0;
  font-family: var(--font-mono);
  line-height: 1.4;

  li {
    padding: 0.2em 0;
    display: flex;
    gap: 0.5em;
  }

  li.draft {
    border-left: 3px solid var(--color-draft);
    padding-left: 0.5em;
  }
}

/* Post type indicator */
.post-type {
  font-size: 0.75em;
  color: var(--color-gray);
  font-family: var(--font-mono);
  margin-left: 0.5em;
}

.list-tags {
  margin-top: 0.1em;

  .tag {
    font-size: 0.75em;
    margin-right: 0.3em;
  }
}

/* =============================================================================
   CODE
   ============================================================================= */
code {
  background-color: var(--color-gray-light);
  border: 1px solid var(--color-border);
  padding: 0.1em 0.3em;
  font-size: 0.9em;
  font-family: var(--font-mono);
  display: inline-block;
  line-height: 1.2;
  vertical-align: baseline;
}

pre {
  background-color: var(--color-white);
  border: 3px solid var(--color-black);
  padding: 0;
  overflow-x: auto;
  line-height: 1.4;
  position: relative;
  margin: 1.5em 0;

  code {
    background-color: transparent;
    border: none;
    padding: 0.8em 0.5em;
    font-size: 0.85em;
    display: block;
  }
}

/* Code header bar - contains language and copy button */
.code-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--color-black);
  color: var(--color-white);
  font-family: var(--font-mono);
  font-size: 0.7em;
  font-weight: bold;
  text-transform: uppercase;
  border-bottom: 3px solid var(--color-black);
}

/* Language label - left side of header */
.code-lang {
  padding: 0.3em 0.8em;
  pointer-events: none;
}

/* Copy button - right side of header */
.copy-btn {
  background: var(--color-black);
  border: none;
  border-left: 3px solid var(--color-white);
  color: var(--color-white);
  padding: 0.3em 0.8em;
  font-size: 1em;
  font-family: var(--font-mono);
  font-weight: bold;
  text-transform: uppercase;
  cursor: pointer;
  transition: all 0.2s ease;
  line-height: 1;

  &:hover {
    background-color: var(--color-white);
    color: var(--color-black);
  }

  &:focus {
    outline: 3px solid var(--color-white);
    outline-offset: -3px;
    background-color: var(--color-white);
    color: var(--color-black);
  }

  &:focus:not(:focus-visible) {
    outline: none;
  }

  &:focus-visible {
    outline: 3px solid var(--color-white);
    outline-offset: -3px;
    background-color: var(--color-white);
    color: var(--color-black);
  }

  &:active {
    transform: scale(0.95);
  }
}

/* =============================================================================
   TAGS & METADATA
   ============================================================================= */
.post-tag,
.tag {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 0.85em;
  border-radius: 3px;
  text-decoration: none;
  color: var(--color-darkgray);

  &:hover {
    background-color: var(--color-black);
    color: var(--color-white);
    text-decoration: none;
  }
}

.metadata {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  width: 100%;

  time {
    display: inline-block;
    font-family: var(--font-mono);
    font-size: 85%;
    white-space: nowrap;
    flex-shrink: 0;
  }

  .tags {
    display: flex;
    flex-wrap: wrap;
    gap: 0.3em;
    flex-grow: 1;
    justify-content: flex-end;
    min-width: 0; /* Allow tags to shrink when needed */
  }
}

/* =============================================================================
   DRAFT POSTS
   ============================================================================= */
.draft-label {
  background-color: var(--color-draft);
  color: var(--color-white);
  padding: 0.2em 0.5em;
  font-size: 0.7em;
  font-weight: bold;
  text-transform: uppercase;
  border-radius: 3px;
  float: right;
  margin-left: 0.5em;
  vertical-align: middle;
}

/* Make draft posts stand out even more */
article.draft {
  border: 2px dashed var(--color-draft);
  padding: 1em;
}

div.meta {
  font-size: 75%;
}

div.page-header {
  margin-bottom: 0;
}

/* =============================================================================
   FOOTNOTES
   ============================================================================= */
.footnote-definition {
  display: flex;
  margin: 0.5em 0;
  font-size: 0.9em;
  list-style: none;

  sup {
    min-width: 1.5em;
    margin-right: 0.5em;

    &::after {
      content: ":";
    }
  }

  p {
    margin: 0;
    display: inline;
  }
}

sup.footnote-reference {
  font-size: 0.8em;
  vertical-align: super;
}

/* =============================================================================
   BLOCKQUOTES
   ============================================================================= */
blockquote {
  border-left: 4px solid var(--color-border-quote);
  padding-left: 1em;
  margin-left: 0;
  margin-right: 0;
  font-style: italic;
  color: var(--color-gray-darker);
}

/* =============================================================================
   ASIDE NOTES
   ============================================================================= */
aside.note {
  margin-top: 0.5em;
  border-width: 3px;
  border-radius: 0; /* Remove rounded corners to fix background leak */
  border-style: solid;
  vertical-align: middle;
  position: relative;
  padding-left: 1.5em;
  overflow: hidden;
  display: flex;
  align-items: center;

  /* CSS variables for theming */
  --border: purple;
  --background: silver;
  --info: var(--color-info);
  --info_back: var(--color-info-bg);
  --warning: var(--color-warning);
  --warning_back: var(--color-warning-bg);
  --alert: var(--color-alert);
  --alert_back: var(--color-alert-bg);

  border-color: var(--border);
  background-color: var(--background);

  &::before {
    content: attr(data-title);
    position: absolute;
    left: 0;
    top: -3px; /* Extend to cover the border */
    bottom: -3px; /* Extend to cover the border */
    width: 1.8em;
    background-color: var(--border);
    writing-mode: vertical-rl;
    transform: rotate(180deg);
    color: var(--color-white);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.9em;
    letter-spacing: 0.2em;
    text-transform: uppercase;
    padding: 1em 0;
    box-sizing: border-box;
  }

  p {
    margin: 0;
    /* biome-ignore lint/complexity/noImportantStyles: Required to override paragraph spacing in notes */
    margin-top: 0 !important;
    padding: 1em 0.5em 1em 0.3em;
    font-size: smaller;
  }

  p.title,
  h2 {
    color: var(--color-white);
    writing-mode: vertical-rl;
    transform: rotate(180deg);
    margin: 0;
    background-color: var(--border);
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 1.8em;
    padding: 1em 0;
    font-weight: 100;
    font-family: var(--font-fallback-mono);
    font-size: 0.9em;
    line-height: 1.5;
    border-right: 3px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    letter-spacing: 0.2em;
    text-transform: uppercase;
  }

  h2::before {
    content: "";
  }
}

/* =============================================================================
   RESPONSIVE
   ============================================================================= */
@media only screen and (max-width: 120ch) {
  body {
    max-width: 100%;
    padding: 0 0.5rem;
  }

  /* Make code header stick when scrolling horizontally */
  .code-header {
    position: sticky;
    left: 0;
    right: 0;
    z-index: 1;
    font-size: 0.6em;
  }

  /* Smaller padding on mobile */
  .code-lang,
  .copy-btn {
    padding: 0.2em 0.6em;
  }

  h1.title {
    font-size: large;
  }
}

/* =============================================================================
   ERROR PAGES
   ============================================================================= */
.error-content {
  text-align: center;
  margin: 2em 0;

  p {
    font-size: 1.1em;
    margin-bottom: 1em;
  }
}
