@layer base, layout, components, syntax;

@layer layout {
  body {
    max-width: 80.5ch;
    padding: 2ch;
    text-align: left;
    hyphens: auto;
    margin-inline: auto;
    overflow-wrap: break-word;
    container-type: inline-size;
  }

  header {
    margin-bottom: 2rem;

    & nav {
      display: flex;
      gap: 2ch;
      align-items: baseline;
    }

    & a {
      font-style: normal;
      color: var(--text);
      text-decoration: none;
    }

    & a:hover {
      color: var(--text-soft);
      text-decoration: none;
    }

    & .title {
      font-size: 1.2em;
      margin-right: auto;
    }
  }

  footer {
    margin-top: 2rem;

    p {
      display: flex;
      column-gap: 3.5ch;
      justify-content: center;
      flex-wrap: wrap;
    }

    a {
      display: flex;
      align-items: center;
      gap: 0.5ch;
      color: var(--text);
      text-decoration: none;
      white-space: nowrap;

      svg {
        width: 1rem;
        height: 1rem;
      }
    }
  }
}

@layer base {
  :root {
    --line-height: 1.8;
    --border-radius: 0.4rem;
    --background: #fffff8;
    --emphasis: #333333;
    --muted: #dbdbd6;
    --text: #1a1a1a;
    --accent: #279af1;
    --text-soft: rgba(0, 0, 0, 0.8);
  }

  @media (prefers-color-scheme: light) {
    :root:has(#theme-toggle:checked) {
      --background: #181a1b;
      --text: #e8e6e3;
      --accent: #279af1;
      --emphasis: #f5f5f5;
      --muted: #2a2f36;
      --text-soft: rgba(255, 255, 255, 0.75);
    }
  }

  .theme-toggle::before {
    content: "Dark";
  }

  .theme-toggle:hover {
    cursor: pointer;
    color: var(--text-soft);
  }

  body:has(#theme-toggle:checked) .theme-toggle::before {
    content: "Light";
  }

  @media (prefers-color-scheme: dark) {
    :root {
      --background: #181a1b;
      --text: #e8e6e3;
      --accent: #279af1;
      --emphasis: #f5f5f5;
      --muted: #2a2f36;
      --text-soft: rgba(255, 255, 255, 0.75);
    }

    .theme-toggle::before {
      content: "Light";
    }

    body:has(#theme-toggle:checked) .theme-toggle::before {
      content: "Dark";
    }

    :root:has(#theme-toggle:checked) {
      --background: #fffff8;
      --text: #070600;
      --muted: #dbdbd6;
      --emphasis: #1a1a1a;
      --accent: #279af1;
      --text-soft: rgba(0, 0, 0, 0.8);
    }
  }

  html {
    box-sizing: border-box;
    font-family: "et-book", Georgia, serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
    background: var(--background);
    color: var(--text);
    font-size-adjust: ex-height 0.53;
    font-kerning: normal;
    font-variant-ligatures: common-ligatures contextual;
    font-feature-settings: "kern", "liga", "clig";
    -webkit-text-size-adjust: 100%;
    line-height: var(--line-height);
  }

  main {
    /* flex-direction: row; */
    /* display: flex; */
    /* align-items: flex-start; */

    a {
      color: inherit;
      text-decoration: none;
      text-decoration-thickness: 1px;
      text-underline-offset: 2px;
    }

    a:visited {
      color: var(--text);
      text-decoration-color: var(--text);
    }

    a:hover {
      background: var(--muted);
      transition-property: background;
      transition-duration: 0.2s;
      border-radius: 8px;
    }

    a::after {
      position: relative;
      content: "\FEFF*";
      margin-left: 0.10em;
      font-size: 90%;
      top: -0.10em;
      color: var(--accent);
      font-feature-settings: "caps";
      font-variant-numeric: normal;
    }

    a[role="doc-noteref"]::after {
      content: none;
    }

    a[role="doc-backlink"]::after {
      content: none;
    }

    :where(h1, h2, h3) a::after {
      content: none;
    }

    p .subtitle {
      font-style: italic;
      margin-top: 1rem;
      margin-bottom: 1rem;
      font-size: 1.8rem;
      display: block;
      line-height: 1;
    }

    ul li::marker {
      content: "\2023  ";
      color: inherit;
      font-size: 1.1em;
    }
  }

  code,
  pre {
    font-family: "Iosevka", monospace;
  }

  em {
    font-style: italic;
  }

  dt a {
    font-style: italic;
    color: var(--accent);
    text-decoration: none;
  }

  dd a {
    color: var(--text);
  }

  dt a:hover {
    text-decoration: underline;
  }

  .reading-time::before {
    content: " – ";
    margin: 0 0.5rem;
    color: inherit;
  }

  .reading-time {
    display: inline-flex;
    align-items: center;
  }

  article {
    text-align: left;
    counter-reset: sidenote-counter;
    gap: 0.5rem;

    > * {
      margin: 0;
    }

    > *,
    > section > *,
    figure.blockquote {
      max-width: 65ch;
    }

    header {
      margin-bottom: 0rem;
    }

    p {
      margin-bottom: 1.4rem;
      padding-right: 0;
    }

    section {
      margin-top: 2.5rem;
    }

    > section,
    > figure,
    > section > figure {
      max-width: 63ch;
    }

    :where(h2, h3) {
      margin-block-start: var(--line-height);
    }

    a:visited {
      color: var(--text);
      text-decoration-color: var(--text);
    }
  }

  article :where(h1, h2, h3) a {
    color: var(--accent);
    text-decoration: none;
  }

  article :where(h1, h2, h3) a:visited {
    color: var(--accent);
    text-decoration: none;
  }

  article :where(h1, h2, h3) a::after {
    content: none;
  }

  :where(sup, sub) {
    line-height: 0;
  }

  :where(time, h1, h2, h3, .reading-time) {
    font-family: "et-book", sans-serif;
    font-style: italic;
    font-weight: 400;
  }

  :where(h1, h2, h3) {
    color: var(--accent);
    line-height: var(--line-height);
  }

  h1 {
    font-size: 2rem;
  }

  h2 {
    font-size: 1.8rem;
  }

  h3 {
    font-size: 1.5rem;
  }

  hr {
    border: none;
    height: 0;
    overflow: visible;
    color: var(--text);
    height: var(--line-height);
  }

  hr::after {
    font-family: "Ornaments";
    content: "🙦  🙥";
    display: block;
    text-align: center;
  }

  section[role="doc-endnotes"] > hr {
    all: revert;
  }

  section[role="doc-endnotes"] > hr::after {
    content: none;
  }

  svg {
    fill: currentColor;
  }
}

@layer components {
  .internal-link {
    font-variant: small-caps;
  }

  a.internal-link::after {
    content: none;
  }

  .table-of-contents menu {
    display: none;
  }

  .table-of-contents {
    font-size: 0.92rem;

    h1 {
      font-size: 1.15em;
    }

    h2 {
      font-size: 1.12em;
    }

    & h2 {
      font-size: 1.12em;
      margin: 0;
    }

    h3 {
      font-size: 1em;
    }

    menu {
      font-size: inherit;

      li {
        list-style-type: none;
        text-indent: none;
        margin: 0;
        padding: 0;
        padding-inline: 0.4em;
        line-height: 1.2;

        a {
          color: inherit;
          text-decoration: none;
          display: inline-block;
          width: 100%;

          padding-top: 0.4em;
          padding-bottom: 0.4em;
        }

        &[data-level="3"] a {
          padding-left: 0.5em;
        }

        &[data-level="4"] a {
          padding-left: 1em;
        }

        &[data-level="5"] a {
          padding-left: 1.5em;
        }

        &[data-level="6"] a {
          padding-left: 2em;
        }
      }
    }
  }

  @media (min-width: 950px) {
    main {
      > .table-of-contents {
        min-width: 320px;
        padding: 1em 2em;
        flex-shrink: 1;

        menu {
          padding: 1em;
          display: inline-block;
          width: auto;
          border-radius: var(--border-radius);
          /* background: var(--page-html-bg); */
          border: 1px solid var(--emphasis);
        }
      }
    }
  }

  @media (min-width: 1300px) {
    .table-of-contents {
      position: sticky;
      min-width: 400px;
      top: 60px;

      max-height: calc(100vh - 100px);
      overflow-y: auto;
    }
  }

  .table-of-contents a::after {
    content: none;
  }

  .meta-links {
    display: block;
    text-align: center;
  }

  .meta-links a {
    display: inline;
  }

  .sidenote-content {
    float: right;
    clear: right;
    margin-right: -60%;
    width: 50%;
    margin-top: 0.3rem;
    margin-bottom: 0;
    font-size: 0.9rem;
    line-height: 1.3;
    vertical-align: baseline;
    position: relative;
  }

  .sidenote-number {
    counter-increment: sidenote-counter;
  }

  .sidenote-number:after {
    content: counter(sidenote-counter);
    font-size: 0.8rem;
    top: -0.5rem;
    left: 0.1rem;
    color: var(--accent);
  }

  .sidenote-content:before {
    content: counter(sidenote-counter) " ";
    color: var(--accent);
    font-size: 0.8rem;
    top: -0.5rem;
    position: relative;
    vertical-align: baseline;
  }

  label.sidenote-number {
    display: inline-block;
    max-height: 2rem;
  }

  .sidenote-number::after,
  .sidenote::before {
    position: relative;
    vertical-align: baseline;
  }

  input.margin-toggle {
    display: none;
  }

  @media (max-width: 760px) {
    body {
      width: 84%;
      padding-left: 8%;
      padding-right: 8%;
    }

    hr,
    section > p,
    section > footer,
    section > table {
      width: 100%;
    }

    label.margin-toggle:not(.sidenote-number) {
      display: inline;
    }

    label {
      cursor: pointer;
    }

    .sidenote-content {
      display: none;
    }

    .margin-toggle:checked + .sidenote-content {
      display: block;
      float: left;
      left: 1rem;
      clear: both;
      width: 95%;
      margin: 1rem 2.5%;
      position: relative;
    }

    figure {
      max-width: 90%;
    }
  }

  ::-webkit-scrollbar {
    width: 12px;
    height: 12px;
  }

  ::-webkit-scrollbar-track {
    background: transparent;
  }

  ::-webkit-scrollbar-thumb {
    background: transparent;
    border-radius: 6px;
  }

  *:hover::-webkit-scrollbar-thumb {
    background: rgba(0, 0, 0, 0.3);
  }

  *:hover::-webkit-scrollbar-thumb:hover {
    background: rgba(0, 0, 0, 0.5);
  }

  nav {
    display: flex;
    gap: 2ch;
    align-items: baseline;
    flex-wrap: wrap;
  }

  .progress-container {
    position: sticky;
    top: 0;
    left: 0;
    right: 0;
    /* I'm too lazy to fix the reason why the horizontal scroll bar appears. */
    width: 98vw;
    height: 5px;
    background: var(--background);
    z-index: 10;
    margin-left: calc(-49vw + 50%);
    margin-right: calc(-49vw + 50%);
  }

  @supports not (animation-timeline: scroll()) {
    .progress-container {
      display: none;
    }
  }

  .progress-bar {
    width: 0%;
    height: 100%;
    background: var(--accent);

    animation: progress linear both;
    animation-timeline: scroll(root);
  }

  @keyframes progress {
    0% {
      width: 0%;
    }

    100% {
      width: 100%;
    }
  }

  figure {
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
    -webkit-margin-start: 0;
    -webkit-margin-end: 0;
    margin: 0;
  }

  figure.code-block > pre > code {
    display: flex;
    flex-direction: column;
    overflow-x: auto;
    overflow-y: clip;
    counter-reset: line;
    line-height: 1.2;
  }

  figure.code-block > pre > code > .line {
    counter-increment: line;
  }

  figure.code-block > pre > code > .line:before {
    content: counter(line);
    display: inline-block;
    width: 3ch;
    padding-right: 0.5ch;
    margin-right: 1ch;
    text-align: right;
    opacity: 0.4;
    border-right: 1px solid var(--emphasis);
  }

  figure.blockquote {
    padding: 1em 1.25em;
    margin: 1.5em 0;
    background: var(--background);
    border-left: 3px solid var(--accent);

    box-shadow: 0 1px 0 rgba(0, 0, 0, 0.08), 0 4px 10px rgba(0, 0, 0, 0.06);
  }

  .post-list {
    margin-left: 0;
    list-style: none;
  }

  .post-list > li {
    margin-top: 1em;
  }

  .post-list h2 {
    margin-top: 0;
  }

  .post-list a {
    color: #279af1;
    text-decoration: none;
    display: block;
  }

  .post-list a:hover {
    color: #279af1;
  }

  .emphasis {
    color: var(--emphasis);
    font-style: italic;
  }

  .emphasis:hover {
    text-decoration: underline;
  }

  kbd {
    font-family: "Iosevka", monospace;
    font-variant-ligatures: none;
    line-height: 1;
  }

  kbd > kbd {
    display: inline-block;
    background: #e6e0c8;
    color: var(--text);

    border-top: 2px solid #ffffff;
    border-left: 2px solid #ffffff;
    border-right: 2px solid #7a7a7a;
    border-bottom: 2px solid #7a7a7a;

    border-radius: 0;
    padding: 0.25em 0.5em;
    margin: 0 0.15em;

    box-shadow: none;
    position: relative;
    top: -1px;
  }

  aside.admn {
    display: flex;
    flex-direction: row;
    align-items: center;
  }

  aside.admn > svg.icon {
    flex-shrink: 0;
    width: 4rem;
    height: 4rem;
    fill: var(--accent);
  }

  aside.admn > div {
    flex: 1;
    min-width: 0;

    padding-inline-start: 1ch;
    margin-inline-start: 1ch;
    border-inline-start: 1px solid var(--muted);

    & > :last-child {
      margin-bottom: 0;
    }
  }

  aside.block {
    border: 1px solid var(--muted);
    border-radius: 4px;
    border-color: var(--muted);
    padding: 1em;
    background: var(--background);
  }

  aside.block > :where(:last-child) {
    margin-bottom: 0;
  }

  aside.block > :where(.title) {
    font-family: "et-book", sans-serif;
    font-size: 1.5rem;
    color: var(--accent);
    text-align: center;
    margin-top: 0;
    margin-bottom: 0.5rem;
  }
}

@layer syntax {
  .hl-keyword,
  .hl-literal {
    color: var(--text);
    font-weight: bold;
  }

  .hl-type,
  .hl-constructor,
  .hl-type-builtin {
    color: #d8ab1d;
  }

  .hl-function,
  .hl-function-method,
  .hl-function-call {
    color: var(--accent);
    font-weight: bold;
  }

  .hl-title.class_ {
    color: #445588;
    font-weight: bold;
  }

  .hl-comment {
    color: #e06b28;
    font-style: italic;
  }

  .hl-function-macro,
  .hl-keyword-import {
    color: #6ca68c;
    font-weight: bold;
  }

  .hl-number,
  .hl-constant-builtin {
    color: #b070a8;
  }

  .hl-string {
    color: #8aa21f;
  }

  .hl-subst {
    color: var(--text-soft);
  }

  .hl-line {
    background-color: #d8ab1d4d;
    color: var(--text);
  }
}
