@layer base, layout, components;

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

    --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-weight: 500; */
    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;
    }

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

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

    h1 {
      font-family: "Libre Bodoni", serif;
      font-weight: 700;
      letter-spacing: -0.02em;
    }

    :where(h2, h3) {
      font-family: var(--regular-font);
      font-style: italic;
      font-weight: 700;
    }

    .post-title {
      font-family: "Libre Bodoni", serif;
      font-style: normal;
    }

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

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

  code,
  pre {
    font-family: var(--code-font), monospace;
  }

  p code {
    background: var(--code-tint);
    border-radius: min(calc(var(--radius,.5em) * .6), .35em);
    padding: .125em .3em;
    /* font-size: .85em; */
  }

  .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: 80.5ch;
    }

    header {
      margin-bottom: 0;
    }

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

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

      a {
        text-decoration: underline;
        text-decoration-color: var(--muted);
        font-weight: 700;
        text-decoration-thickness: 1px;
        text-underline-offset: 2px;
        transition: text-decoration-color 0.5s;
      }
    }

    p:has([data-kind="media"]) {
      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 {
        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: 500;
        font-style: italic;
      }

      &.code-block {
        padding: 1em 1.25em;
        background: var(--lang-bg);
        box-shadow: 0 1px 0 rgba(0, 0, 0, 0.08), 0 4px 10px rgba(0, 0, 0, 0.06);
        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;
          }
        }
      }
    }

    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;
      overflow: visible;
      color: var(--text);
      height: var(--line-height);
    }

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

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

  .skip-link {
    position: absolute;
    top: -100%;
    left: 1ch;
    padding: 0.5em 1em;
    background: var(--text);
    color: var(--background);
    z-index: 1000;
    font-family: var(--title-font);
    border-radius: var(--border-radius);
  }

  .skip-link:focus {
    top: 1ch;
  }

  header {
    margin-bottom: 2rem;

    .site-name {
      font-family: Cabin;
      text-transform: uppercase;
      font-weight: 700;
      font-size: 0.8em;
    }

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

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

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

      & .title {
        display: flex;
        align-items: center;
        gap: 0.6ch;
        font-size: 1.0em;
        margin-right: auto;
        min-width: 0;
        transition: color 0.2s;

        .logo {
          display: flex;
          align-items: center;
          justify-content: center;
          width: 2rem;
          height: 2rem;
          flex-shrink: 0;
          border-radius: 50%;
          background: var(--muted);

          svg {
            width: 60%;
            height: 60%;
            fill: currentColor;
          }
        }

        .site-name {
          line-height: 1.2;
          white-space: nowrap;
        }

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

  footer {
    margin-top: 3rem;
    font-size: 0.8rem;

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

    .footer-links {
      font-size: 1rem;
    }

    a {
      display: flex;
      align-items: center;
      gap: 0.5ch;
      color: color-mix(in srgb, var(--text) 85%, var(--background));
      text-decoration: none;
      white-space: nowrap;

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

      svg {
        width: 0.9em;
        height: 0.9em;
      }
    }

    .copyr {
      margin-top: 2.5rem;
      text-align: center;
      color: color-mix(in srgb, var(--text) 85%, var(--background));
    }
  }

  .normal-layout {
    a {
      text-decoration: underline;
      text-decoration-color: var(--muted);
      font-weight: 700;
      text-decoration-thickness: 1px;
      text-underline-offset: 2px;
      transition: text-decoration-color 0.5s;
    }

    max-width: 80.5ch;
    margin-inline: auto;
  }

  .post-layout {
    display: grid;
    grid-template-columns: minmax(0, 72ch) var(--col-sidenote);
    grid-template-areas: "content sidenotes";
    gap: 0 var(--col-gap);
    align-items: start;
    max-width: calc(72ch + var(--col-sidenote) + var(--col-gap));
    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 {
      margin: 30px auto;

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

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

  @media (min-width: 1101px) {
    article .sidenote-content {
      float: right;
      clear: right;
      margin-right: -60%;
      width: 50%;
      font-size: 0.9rem;
      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;
    }

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

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

  details.post-toc {
    max-width: 72ch;
    margin-block: 2rem;
    border: 2px solid var(--muted);
    border-radius: var(--border-radius);
    background: var(--lang-bg);
    box-shadow: 0 1px 0 rgba(0, 0, 0, 0.08), 0 4px 10px rgba(0, 0, 0, 0.06);

    summary {
      cursor: pointer;
      padding: 0.7em 1em;
      font-family: var(--title-font);
      font-weight: 700;
      list-style: none;

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

      &::before {
        content: "";
        display: inline-block;
        width: 0.5em;
        height: 0.5em;
        margin-right: 0.6em;
        border-right: 2px solid var(--accent);
        border-bottom: 2px solid var(--accent);
        transform: rotate(-45deg);
        transition: transform 0.15s ease;
      }
    }

    &[open] summary::before {
      transform: rotate(45deg);
    }

    menu {
      margin: 0;
      padding: 0 0 1em 1.8em;
      font-size: 0.9rem;

      & a.toc-entry {
        color: var(--text);
        text-decoration: none;

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

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

      ul {
        padding-left: 1rem;
      }

      li {
        line-height: 1.35;
        margin: 0;
        padding: 0;
      }
    }
  }
}

@layer components {
  .meta-links {
    display: block;
    text-align: center;

    a {
      display: inline;
    }
  }

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

  .sidenote-number:after {
    content: counter(sidenote-counter);
    position: relative;
    vertical-align: baseline;
    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;
  }

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

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

  .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-bottom: 2rem;
      margin-bottom: 2rem;
      border-bottom: 1px solid var(--muted);
    }

    > li:last-child {
      border-bottom: none;
      margin-bottom: 0;
      padding-bottom: 0;
    }

    > li.latest-post {
      h3 a {
        font-size: 1.55rem;
      }
    }

    h3 {
      margin: 0 0 0.5rem;

      a {
        font-size: 1.35rem;
        font-weight: 600;
      }
    }

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

      &::after {
        content: none;
      }
    }

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

    .meta-row {
      margin-top: 0;
      color: color-mix(in srgb, var(--text) 85%, var(--background));
      font-weight: 700;
    }

    .tags {
      display: flex;
      align-items: center;
      gap: 0.6rem;
      margin-top: 0.5rem;
    }

    .tag {
      padding: 0;
      border-radius: 0;
      border: none;
      color: color-mix(in srgb, var(--text) 85%, var(--background));
      font-size: 0.75rem;
      font-weight: 700;
      text-transform: lowercase;

      &::before {
        content: "#";
        color: var(--accent);
      }
    }

    li::marker {
      content: 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 {
      max-width: none;
      margin-inline: -2ch;
      padding-inline: 0;
    }
  }

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

  .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;
      margin-left: 0;
    }

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

  .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(--code-font), 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;
  }
}
