@layer reset, tokens, layout, components;

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

  body,
  h1,
  h2,
  h3,
  h4,
  p,
  figure,
  blockquote {
    margin: 0;
  }

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

@layer tokens {
  :root {
    color-scheme: dark;

    /* Quiet dark slate / soft low-glare stone gray (WCAG AA >= 4.5:1) */
    --bg: light-dark(#d5d3cd, #090b0e);
    --surface: light-dark(#cac8c1, #0e1116);
    --ink: light-dark(#22252a, #b8c0c8);
    --strong: light-dark(#131518, #d8dfe6);
    --muted: light-dark(#4a4d52, #7d8896);
    --line: light-dark(#a6a49d, #272e37);
    --line-soft: light-dark(#bab8b1, #1b2028);
    --line-strong: light-dark(#6f6e68, #56616f);
    --link: light-dark(#1c4478, #73a0d1);
    --visited: light-dark(#4e356b, #9e89be);
    --accent: light-dark(#943028, #cf6860);
    --selection-ink: light-dark(#ffffff, #090b0e);

    --font-sans:
      -apple-system, BlinkMacSystemFont, "Hiragino Kaku Gothic ProN",
      "Yu Gothic", Meiryo, sans-serif;
    --font-mono:
      ui-monospace, "SFMono-Regular", Menlo, Consolas, "Courier New", monospace;

    --col-max: 34rem;
  }

  :root:has(#theme:checked) {
    color-scheme: light;
  }

  @supports not (color: light-dark(red, blue)) {
    :root {
      --bg: #090b0e;
      --surface: #0e1116;
      --ink: #b8c0c8;
      --strong: #d8dfe6;
      --muted: #7d8896;
      --line: #272e37;
      --line-soft: #1b2028;
      --line-strong: #56616f;
      --link: #73a0d1;
      --visited: #9e89be;
      --accent: #cf6860;
      --selection-ink: #090b0e;
    }

    :root:has(#theme:checked) {
      --bg: #d5d3cd;
      --surface: #cac8c1;
      --ink: #22252a;
      --strong: #131518;
      --muted: #4a4d52;
      --line: #a6a49d;
      --line-soft: #bab8b1;
      --line-strong: #6f6e68;
      --link: #1c4478;
      --visited: #4e356b;
      --accent: #943028;
      --selection-ink: #ffffff;
    }
  }
}

@layer layout {
  html {
    scroll-behavior: smooth;

    @media (prefers-reduced-motion: reduce) {
      scroll-behavior: auto;
    }
  }

  body {
    min-block-size: 100svh;
    background-color: var(--bg);
    color: var(--ink);
    font-family: var(--font-sans);
    font-size: 0.8125rem;
    line-height: 1.85;
    text-wrap: pretty;
  }

  .entrance {
    min-block-size: 100svh;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding-block: 2.5rem 3.5rem;
    padding-inline: 1.25rem;
    position: relative;
  }

  .door {
    inline-size: min(100%, var(--col-max));
  }

  .content-shell {
    max-inline-size: var(--col-max);
    margin-inline: auto;
    padding-block: 4rem 5rem;
    padding-inline: 1.25rem;
    border-block-start: 1px dashed var(--line);

    @media (width < 32rem) {
      padding-block: 3rem 4rem;
    }
  }

  .article-shell {
    max-inline-size: 40rem;
    margin-inline: auto;
    padding-block: 2.5rem 5rem;
    padding-inline: 1.25rem;
  }
}

@layer components {
  .sr-only {
    position: absolute;
    inline-size: 1px;
    block-size: 1px;
    margin: 0;
    padding: 0;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  .theme-switch {
    position: relative;
  }

  .theme-toggle {
    position: absolute;
    inset: 0;
    inline-size: 1px;
    block-size: 1px;
    margin: 0;
    overflow: hidden;
    clip-path: inset(50%);
    white-space: nowrap;
  }

  .door-top,
  .article-top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
    font-family: var(--font-mono);
    font-size: 0.75rem;
  }

  .door-top {
    margin-block-end: 0.65rem;
  }

  .article-top {
    padding-block-end: 0.65rem;
    margin-block-end: 2.25rem;
    border-block-end: 1px dashed var(--line);
  }

  .top-left {
    display: flex;
    align-items: baseline;
    gap: 1rem;
  }

  .site-title {
    font-size: 0.8125rem;
    font-weight: 700;
    color: var(--strong);
    text-decoration: none;
    letter-spacing: 0.02em;
  }

  a.site-title {
    color: var(--strong);

    &:visited {
      color: var(--strong);
    }

    &:hover {
      color: var(--accent);
    }
  }

  .top-nav {
    display: flex;
    align-items: baseline;
    gap: 0.85rem;
    font-size: 0.71875rem;

    & a {
      text-decoration: none;

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

  .theme-label {
    display: inline-block;
    padding-block: 1px;
    padding-inline: 0.45rem;
    border: 1px solid var(--line-strong);
    background: var(--surface);
    color: var(--muted);
    font-family: var(--font-mono);
    font-size: 0.6875rem;
    line-height: 1.5;
    cursor: pointer;
    user-select: none;

    &:hover {
      color: var(--strong);
      border-color: var(--accent);
    }
  }

  .theme-glyph::before {
    content: "◐ ";
    content: "◐ " / "";
  }

  #theme:checked + .theme-label {
    color: var(--strong);

    & .theme-glyph::before {
      content: "◑ ";
      content: "◑ " / "";
    }
  }

  #theme:focus-visible + .theme-label {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
  }

  .door-photo {
    & img {
      inline-size: 100%;
      border: 1px solid var(--line);
    }
  }

  .sig-quote {
    margin-block: 1.15rem 0;
    padding-block: 0.5rem;
    padding-inline: 0.9rem;
    border-inline-start: 2px solid var(--muted);
    background: var(--surface);
    font-size: 0.78125rem;
    line-height: 1.7;

    & blockquote p {
      margin: 0;
      color: var(--strong);
    }

    & figcaption {
      margin-block-start: 0.25rem;
      font-family: var(--font-mono);
      font-size: 0.6875rem;
      color: var(--muted);
    }

    & cite {
      font-style: normal;
    }
  }

  .scroll-cue {
    position: absolute;
    inset-block-end: 0.75rem;
    padding-block: 0.5rem;
    padding-inline: 0.75rem;
    font-family: var(--font-mono);
    font-size: 0.6875rem;
    line-height: 1;
    color: var(--muted);
    text-decoration: none;

    &:visited {
      color: var(--muted);
    }

    &:hover {
      color: var(--strong);
    }
  }

  .content-shell section {
    margin-block-end: 3.25rem;

    &:last-of-type {
      margin-block-end: 0;
    }
  }

  h2 {
    font-family: var(--font-mono);
    font-size: 0.75rem;
    font-weight: 700;
    color: var(--strong);
    margin-block: 0 0.85rem;

    &::before {
      content: "# ";
      content: "# " / "";
      color: var(--muted);
      font-weight: 400;
    }
  }

  p {
    margin-block: 0 0.9rem;

    &:last-child {
      margin-block-end: 0;
    }
  }

  a {
    color: var(--link);
    text-underline-offset: 2px;

    &:visited {
      color: var(--visited);
    }

    &:hover {
      color: var(--accent);
    }

    &:focus-visible {
      outline: 2px solid var(--accent);
      outline-offset: 2px;
    }
  }

  .tree {
    list-style: none;
    margin: 0;
    padding: 0;
    font-size: 0.78125rem;

    & > li {
      position: relative;
      padding-inline-start: 0.9rem;
      margin-block-end: 0.65rem;

      &::before {
        content: "";
        position: absolute;
        inset-inline-start: 0.1rem;
        inset-block-start: 0.72em;
        inline-size: 4px;
        block-size: 4px;
        background: var(--muted);
      }

      &:last-child {
        margin-block-end: 0;
      }
    }

    & ul {
      list-style: none;
      margin-block: 0.15rem 0;
      padding-inline-start: 0.15rem;

      & li {
        position: relative;
        padding-inline-start: 1.15rem;

        &::before {
          content: "├ ";
          content: "├ " / "";
          position: absolute;
          inset-inline-start: 0;
          font-family: var(--font-mono);
          color: var(--muted);
        }

        &:last-child::before {
          content: "└ ";
          content: "└ " / "";
        }
      }
    }
  }

  .tree-key {
    color: var(--strong);
  }

  .link-list {
    list-style: none;
    margin: 0;
    padding: 0;
    font-family: var(--font-mono);
    font-size: 0.75rem;

    & li {
      display: flex;
      align-items: center;
      gap: 0.75rem;
      margin-block-end: 0.5rem;

      &:last-child {
        margin-block-end: 0;
      }
    }

    & a {
      display: inline-flex;
      align-items: center;
      gap: 0.5rem;
      text-decoration: none;

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

  .link-icon {
    flex: none;
    inline-size: 0.875rem;
    block-size: 0.875rem;
    color: var(--muted);

    a:hover & {
      color: var(--accent);
    }
  }

  .link-host {
    color: var(--muted);
    font-size: 0.6875rem;
  }

  /* Article page typography */
  article {
    & time {
      display: block;
      margin-block-end: 0.5rem;
      font-family: var(--font-mono);
      font-size: 0.6875rem;
      color: var(--muted);
    }

    & h1 {
      margin-block: 0 1.5rem;
      font-size: 1.15rem;
      font-weight: 700;
      color: var(--strong);
      line-height: 1.4;
    }

    & h2 {
      margin-block: 2rem 0.75rem;
      padding-block-end: 0.25rem;
      border-block-end: 1px dotted var(--line);
      font-size: 0.875rem;
    }

    & h3 {
      margin-block: 1.5rem 0.5rem;
      font-size: 0.8125rem;
      font-weight: 700;
      color: var(--strong);
    }

    & h4 {
      margin-block: 1rem 0.35rem;
      font-size: 0.78125rem;
      font-weight: 700;
      color: var(--muted);
    }

    & ul,
    & ol {
      margin-block: 0 1rem;
      padding-inline-start: 1.25rem;
    }

    & blockquote {
      margin-block: 0.75rem 1.25rem;
      padding-block: 0.4rem;
      padding-inline-start: 0.9rem;
      border-inline-start: 2px solid var(--line);
      color: var(--muted);
    }

    & img {
      margin-block: 1rem;
      border: 1px solid var(--line);
    }

    & pre {
      margin-block: 0.75rem 1.25rem;
      padding: 0.8rem 0.95rem;
      overflow-x: auto;
      border: 1px solid var(--line);
      background: var(--surface);
      font-family: var(--font-mono);
      font-size: 0.75rem;
      line-height: 1.6;

      & code {
        padding: 0;
        border: none;
        background: none;
      }
    }

    & code {
      padding-block: 0.1em;
      padding-inline: 0.3em;
      border: 1px solid var(--line-soft);
      background: var(--surface);
      font-family: var(--font-mono);
      font-size: 0.75rem;
    }
  }

  footer {
    margin-block-start: 4rem;
    padding-block-start: 1rem;
    border-block-start: 1px dashed var(--line);
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 0.75rem;
    font-family: var(--font-mono);
    font-size: 0.6875rem;
    color: var(--muted);
  }

  .banner {
    image-rendering: pixelated;
  }

  ::selection {
    background: var(--accent);
    color: var(--selection-ink);
  }
}
