@layer base, layout, components;

@layer base {
  :root {
    --font-symbol: "Iosevka";
    --title-font: "Cabin", "et-book", Georgia, serif;
    --regular-font: "et-book", Georgia, serif;
    --line-height: 1.8;
    --border-radius: 0.4rem;
    --background: #eceff4;
    --lang-bg: #fbfbf5;
    --emphasis: #181818;
    --term-fg: #eddbb2;
    --term-bg: #181818;
    --muted: #dbdbd6;
    --text: #3b4252;
    --accent: #75AADB;
    --code-tint: color-mix(in srgb, var(--accent) 15%, transparent);
    --figure-width: 69ch;
    --figure-block-margin: 1.8em;

    --col-toc: 17rem;
    --col-sidenote: 14rem;
    --col-gap: 2rem;
  }

  @media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
      --background: #181a1b;
      --lang-bg: #232628;
      --text: #dcd8d0;
      --accent: #FCBF49;
      --emphasis: #f5f5f5;
      --muted: #2a2f36;
    }
  }

  html {
    box-sizing: border-box;
    font-family: var(--regular-font);
    -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 {
    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);
      border-radius: 8px;
      transition-property: background;
      transition-duration: 0.2s;
    }

    a::after {
      content: "";
      mask-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='14' height='14' fill='none' viewBox='0 0 14 14'%3E%3Cpath stroke='%23000' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='M12.25 5.25v-3.5m0 0h-3.5m3.5 0L7 7M5.833 1.75H4.55c-.98 0-1.47 0-1.844.19a1.75 1.75 0 0 0-.765.766c-.191.374-.191.864-.191 1.844v4.9c0 .98 0 1.47.19 1.845.169.329.436.597.766.764.374.191.864.191 1.844.191h4.9c.98 0 1.47 0 1.845-.19a1.75 1.75 0 0 0 .764-.766c.191-.374.191-.864.191-1.844V8.167'/%3E%3C/svg%3E");
      display: inline-block;
      -webkit-transition: all 0.3s;
      -o-transition: all 0.3s;
      transition: all 0.3s;
      width: 0.875em;
      height: 0.875em;
      mask-size: contain;
      mask-repeat: no-repeat;
      mask-position: center center;
      margin-left: 0.2em;
      margin-right: 1px;
      transform: translateY(0.125em);
      background-color: var(--accent, #666);
    }

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

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

    :where(h1, h2, time, h3) {
      font-family: var(--title-font);
    }

    :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: var(--font-symbol), monospace;
  }

  p code {
    background: var(--code-tint);
  }

  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 {
    display: inline-flex;
    align-items: center;
  }

  .draft {
    position: absolute;
    top: 0;
    right: 0;
    overflow: hidden;
    width: 6rem;
    height: 6rem;
    pointer-events: none;
  }

  .draft span {
    position: absolute;
    display: block;
    width: 9rem;
    padding: 0.3rem 0;
    background: var(--accent);
    color: var(--background);
    font-size: 0.7rem;
    font-weight: 600;
    text-align: center;
    letter-spacing: 0.05em;
    top: 1.1rem;
    right: -2.1rem;
    transform: rotate(45deg);
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
    text-transform: uppercase;
  }

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

    > * {
      margin: 0;
    }

    > ol {
      margin: revert;
    }

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

    > figure {
      max-width: 72ch;
    }

    header {
      margin-bottom: 0rem;
    }

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

      & [data-kind="media"],
      & [data-kind="diagram"] {
        display: block;
        height: auto;
        max-width: var(--figure-width);
        width: 100%;
        margin-inline: auto;
        margin-block: var(--figure-block-margin);
        overflow: hidden;
      }

      section {
        margin-top: 2.5rem;
      }

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

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

    p:has([data-kind="media"]),
    p:has([data-kind="diagram"]) {
      max-width: unset;
    }

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

      max-width: var(--figure-width);
      display: flex;
      flex-direction: column;
      width: 90%;
      margin-inline: auto;
      margin-block: var(--figure-block-margin);

      img {
        width: 100%;
        height: auto;
        display: block;
      }

      &.with-caption {
        border: 1px solid var(--muted);
        font-family: var(--title-font);
        border-radius: 6.9px;
        box-sizing: border-box;
        padding: .8em;
      }

      &.blockquote {
        margin: 1.5em 0;
        margin-inline: auto;
        background: var(--lang-bg);
        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);
        font-weight: 200;
        font-size: 0.90em;
        font-style: italic;
      }

      &.code-block {
        padding: 1em 1.25em;
        background: var(--muted);
        box-shadow: 0 1px 0 rgba(0, 0, 0, 0.08), 0 4px 10px rgba(0, 0, 0, 0.06);
        background: var(--lang-bg);
        border-radius: 0.4rem;
        border: 2px solid var(--muted);
        position: relative;

        > pre > code {
          display: block;
          overflow-x: auto;
          overflow-y: clip;
          counter-reset: line;
          line-height: 1.2;
          font-size: 1rem;
        }

        > pre > code > .line:before {
          content: "";
          display: inline-block;
          opacity: 0;
        }

        > .language-tag {
          color: light-dark(#6f8399, #e7e7e7);
          position: absolute;
          top: 0.75rem;
          right: 0.75rem;
          padding: 0.2rem 0.6rem;
          z-index: 1;

          > .language-icon {
            width: 25px;
            height: 25px;
            display: block;
            fill: currentColor;
          }
        }
      }
    }

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

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

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

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

    time,
    .reading-time {
      color: var(--text);
    }

    :where(h1, h2, h3) {
      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: blue;
    }
  }
}

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

  header {
    /* border-bottom: 1px solid var(--muted); */
    max-width: 80.5ch;
    margin-inline: auto;
    margin-bottom: 2rem;

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

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

      & a:hover {
        text-decoration: none;
      }

      & .title {
        font-size: 1.0em;
        margin-right: auto;
        min-width: 0;
        overflow-wrap: break-word;
        white-space: normal;
      }
    }
  }

  footer {
    max-width: 80.5ch;
    margin-inline: auto;
    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;
      }
    }

    .copyr {
      font-size: 0.9rem;
      margin-top: 3rem;
      text-align: center;
      color: var(--emphasis);
    }
  }

  .normal-layout {
    max-width: 80.5ch;
    margin-inline: auto;
  }

  .post-layout {
    display: grid;
    grid-template-columns:
      minmax(0, var(--col-toc)) minmax(0, 72ch) var(--col-sidenote);
    grid-template-areas: "toc content sidenotes";
    gap: 0 var(--col-gap);
    align-items: start;
    max-width: calc(var(--col-toc) + 72ch + var(--col-sidenote) + var(--col-gap)
      * 2);

    margin-inline: auto;

    .card {
      background: var(--lang-bg);
      border: 2px dashed var(--accent);
      padding: 15px;
      box-shadow: 0 1px 0 rgba(0, 0, 0, 0.08), 0 4px 10px rgba(0, 0, 0, 0.06);
      max-width: var(--figure-width);
    }

    .draftwall {
      & h2 {
        text-align: center;
        margin-top: 5px;
      }

      margin: 30px auto;
    }

    .draftwall-after {
      filter: blur(5px);
    }
  }

  @media (min-width: 1101px) {
    article .sidenote-content {
      float: right;
      clear: right;
      margin-right: -60%;
      width: 50%;
      font-size: 0.8rem;
      line-height: 1.3;
      vertical-align: baseline;
      position: relative;
    }
  }

  @media (max-width: 1100px) {
    .post-layout {
      grid-template-columns: minmax(0, 72ch);
      grid-template-areas:
        "content"
        "sidenotes";
      max-width: 80.5ch;

      > aside.table-of-contents {
        display: none;
      }
    }

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

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

    label {
      cursor: pointer;
    }

    .sidenote-content {
      display: none;
    }

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

  aside.table-of-contents {
    top: 40px;
    font-size: 0.80rem;
    font-family: var(--title-font);
    grid-area: toc;
    position: sticky;
    width: 100%;
    max-height: calc(100vh - 80px);
    overflow-y: auto;

    h2 {
      font-size: inherit;
      text-transform: lowercase;
      margin-bottom: 0.7em;
      font-weight: 600;
    }

    .toc-header {
      margin: 0;
    }

    & a.toc-entry {
      padding: 0;

      &::after {
        content: none;
      }
    }

    & menu {
      font-size: inherit;
      border: none;
      padding: 0 0 0 1rem;

      a.toc-entry {
        display: inline-block;
        width: 100%;
        padding-top: 0.32rem;
        padding-bottom: 0.32rem;
      }

      a.toc-entry:hover {
        background: transparent;
        border-radius: 0;
        transition: none;

        color: var(--emphasis);
      }

      & ul {
        list-style: none;
        padding: 0;
        margin: 0;

        & ul {
          padding-left: 1rem;
        }

        & li {
          &::marker {
            content: none;
          }

          list-style-type: none;
          margin: 0;
          padding: 0;
          line-height: 1.35;

          border-right: 3px solid transparent;
          transition: border-color 0.12s ease;

          &:hover {
            border-right-color: var(--muted);
          }

          &.reading {
            font-weight: 600;
            border-right-color: var(--accent);

            > a.toc-entry {
              color: var(--accent);
            }
          }
        }
      }
    }
  }

  .post-layout > article {
    grid-area: content;
    min-width: 0;
  }
}

@layer components {
  .private {
    border-radius: 0.4rem;
    border: 2px solid var(--accent);
    padding: 1em 1.25em;
    color: var(--text);
    font-weight: bold;
  }

  .active {
    color: var(--text);
    display: flex;
  }

  .footnote {
    font-size: 0.8rem;

    hr {
      all: revert;
    }

    hr::after {
      content: none;
    }
  }

  a[href^="/"] {
    font-variant: small-caps;
  }

  header a[href^="/"] {
    font-variant: normal;
  }

  a.statement {
    font-variant: normal;
  }

  a.post_title,
  a.tag {
    font-variant: normal;
  }

  a[href^="/"]::after {
    content: none;
  }

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

  .meta-links a {
    display: inline;
  }

  .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);
    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;
  }

  ::-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: center;
    flex-wrap: wrap;
  }

  .meta-row {
    font-size: 0.8rem;
    font-family: var(--title-font);
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 2px;
    margin-top: 0.15rem;
  }

  .header-tags {
    margin-left: auto;
    font-size: inherit;

    a::before {
      content: "#";
    }

    a:hover {
      color: var(--accent);
      text-decoration: underline;
    }
  }

  .tags {
    color: var(--text);
    display: flex;
    margin-left: auto;
    gap: 6px;

    a::before {
      content: "#";
    }

    a:hover {
      color: var(--accent);
      text-decoration: underline;
    }
  }

  .tag {
    font-variant: normal;
    font-size: .8rem;
    color: var(--emphasis);
    padding: 2px 6px;
    border-radius: 0.4rem;
    text-transform: lowercase;
  }

  .post-list {
    margin-left: 0;
    list-style: none;
    max-width: 65ch;

    > li {
      padding: 1rem;
      border: 2px solid var(--muted);
      margin-bottom: 1rem;
      background: var(--lang-bg);
    }

    > li.latest-post {
      position: relative;
      overflow: visible;
      border-color: var(--accent);

      h3 {
        font-size: 1.6rem;
        font-weight: bold;
      }

      a::after {
        font-size: 1.1rem;
      }
    }

    > li::marker {
      content: none;
    }

    h3 {
      margin: 0;
    }

    a {
      text-decoration: none;
      display: block;
    }

    a:hover {
      color: var(--accent);
      background: none;
      transition: none;
    }
  }

  .abstract {
    font-size: 0.86rem;

    & a {
      font-variant: normal;

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

  @media (max-width: 700px) {
    .post-list {
      width: auto;
      max-width: none;
      position: static;
      max-width: none;
      margin-inline: -2ch;
      padding-inline: 0;

      > li,
       > li.latest-post {
        padding-left: 1ch;
        padding-left: 1ch;
      }
    }
  }

  .blogroll {
    max-width: 65ch;
    padding-left: 0.3rem;

    > li {
      margin-bottom: 1.3rem;
    }

    > li::marker {
      content: none;
    }

    h2 {
      margin: 0;
      font-size: 1.2rem;
      display: flex;
      align-items: baseline;
      gap: 0.5rem;
    }

    a {
      text-decoration: none;
    }

    h2 a {
      line-height: 1.25;
    }

    a:hover {
      background: none;
      transition: none;
    }
  }

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

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

  .info {
    background: var(--lang-bg);
    border: 1px solid var(--accent);
    padding-block: 0.2em;
    padding-inline: 0.9em;
    align-self: flex-start;
    margin: 0 0.15em;
  }

  .links {
    ul {
      padding-left: 0.5rem;
      margin-left: 0;
      list-style: none;
    }

    li {
      gap: 0.35rem;
      padding: 0.15rem 0;
      display: flex;
      align-items: center;
      padding-left: 0;
      margin-left: 0;
      list-style: none;
    }

    .link-label {
      flex: 1;
      min-width: 0;
    }

    ul li::marker {
      content: none;
    }
  }

  .link-favicon {
    display: block;
    justify-content: center;
    border-radius: 3px;
    flex-shrink: 0;
    object-fit: contain;
    opacity: 0.85;
    background: var(--background);
  }

  kbd {
    font-family: var(--font-symbol), 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;
    background: var(--background);
  }

  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: var(--regular-font);
    font-size: 1.5rem;
    color: var(--accent);
    text-align: center;
    margin-top: 0;
    margin-bottom: 0.5rem;
  }
}
