/* Animation */
@keyframes fade-in-up {
  0% {
    transform: translateY(2rem);
    opacity: 0;
  }
  100% {
    transform: translateY(0);
    opacity: 1;
  }
}
.animate {
  opacity: 0;
  animation: 300ms fade-in-up;
  animation-fill-mode: forwards;
}
@media (prefers-reduced-motion) {
  .animate {
    opacity: 0;
    animation: 100ms fade-in-up;
    animation-fill-mode: forwards;
  }
}
#content-header {
  animation-delay: 50ms;
}
#content {
  animation-delay: 100ms;
}
#sidebar {
  animation-delay: 150ms;
}

/* Katex */
span.katex-display {
  max-width: 100%;
  overflow: auto hidden;
  padding: 0.5rem;
}
.katex-display > .katex {
  width: max-content;
  min-width: 100%;
}
.katex-html {
  padding: 3px;
}
.katex-html .base {
  margin-block: 0;
  margin-inline: auto;
}
.katex-html .tag {
  position: relative !important;
  display: inline-block;
  padding-inline-start: 0.5rem;
}

/* Shiki */
.astro-code {
  margin-top: 0.5rem;
  margin-bottom: 0.5rem;
  border-radius: 0.5rem;
  border: 1px solid hsl(var(--border));
  background-color: hsl(var(--code-bg) / var(--un-bg-opacity, 1)) !important;
  color: hsl(var(--code-fg));

  /* Code container */
  & pre {
    margin: 0;
    padding-inline: 0;
    background: none;
    overflow-x: scroll;
    color: inherit;

    & code {
      display: block;
      width: fit-content;
      min-width: 100%;
      min-height: 1.5rem;
      counter-reset: step;
      counter-increment: step 0;
      &::before {
        position: absolute;
        inset-inline-start: 0;
        width: 2.75rem;
        top: 0.8571429em;
        bottom: 0;
        content: '';
        display: block;
        background-color: hsl(var(--code-bg) / var(--un-bg-opacity, 1));
        z-index: 1;
      }
      & .line {
        padding-inline-end: 3rem;
        &::before {
          position: sticky;
          content: counter(step);
          counter-increment: step;
          color: hsl(var(--muted-foreground) / 0.7);
          background-color: hsl(var(--code-bg) / var(--un-bg-opacity, 1));
          inset-inline-start: 0;
          justify-content: end;
          display: inline-flex;
          width: 2rem;
          padding-inline-end: 0.75rem;
          box-sizing: content-box;
          z-index: 2;
        }
        &:last-child:empty,
        &:last-child:has(> span:empty:only-child) {
          display: none;
        }
      }
    }
  }

  /* Shiki transformer */
  &:has(code) {
    position: relative;
  }
  &:has(div.title) {
    padding-top: 2rem;
    & code::before {
      top: calc(2rem + 0.8571429em);
    }
  }
  & .language {
    transition: opacity 0.3s;
    position: absolute;
    top: 0.75rem;
    right: 0;
  }
  & button.copy {
    transition: opacity 0.3s;
    opacity: 0;
    position: absolute;
    top: 0.75rem;
    right: 0.75rem;
    user-select: none;

    & .success::before {
      content: 'Copied!';
      position: absolute;
      right: 100%;
      top: 0;
      bottom: 0;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 0.75rem;
      padding: 0.5rem;
      margin-right: 0.5rem;
      border-radius: 0.25rem;
      color: hsl(var(--muted-foreground) / var(--un-text-opacity, 1));
      background-color: hsl(var(--primary-foreground) / var(--un-bg-opacity, 1));
      border: 1px solid hsl(var(--border) / var(--un-border-opacity, 1));
    }
    &.copied {
      & .success {
        display: block;
      }

      & .ready {
        display: none;
      }
    }
  }
  &:hover {
    button.copy {
      opacity: 1;
    }
    .language {
      opacity: 0;
    }
  }

  & .highlighted,
  & .diff {
    min-width: 100%;
    display: inline-block;
  }
  & .highlighted,
  & .highlighted::before {
    background-color: rgba(149, 166, 183, 0.32) !important;
  }

  & .diff {
    &.remove {
      background-color: rgba(244, 63, 94, 0.16);
      &::before {
        background-color: rgba(244, 63, 94, 0.16);
        content: '-';
      }
    }
    &.add {
      background-color: rgba(16, 185, 129, 0.16);
      &::before {
        background-color: rgba(16, 185, 129, 0.16);
        content: '+';
      }
    }
  }
}
/* Long code blocks are enhanced on the client; without JS all code stays visible. */
.astro-code[data-code-collapsible] {
  scroll-margin-top: 5rem;

  & > .code-collapse-viewport {
    position: relative;
    overflow: hidden;
    height: var(--code-preview-height);
    border-radius: inherit;
    transition: height 0.3s ease;

    &::after {
      content: '';
      position: absolute;
      inset: auto 0 0;
      height: 5rem;
      background: linear-gradient(transparent, hsl(var(--code-bg)));
      pointer-events: none;
      opacity: 1;
      transition: opacity 0.25s ease;
      z-index: 3;
    }
  }

  /* The viewport is now the containing block, including for titled snippets. */
  & pre code::before {
    top: 0.8571429em;
  }

  & > .code-collapse-toggle {
    position: relative;
    display: block;
    width: calc(100% - 1rem);
    min-height: 2.75rem;
    margin: 0 0.5rem 0.5rem;
    border: 1px solid hsl(var(--border));
    border-radius: calc(var(--radius) - 2px);
    background: hsl(var(--foreground) / 0.04);
    color: hsl(var(--muted-foreground));
    font: inherit;
    cursor: pointer;
    transition: background-color 0.2s ease;

    &:hover {
      background: hsl(var(--foreground) / 0.08);
    }

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

  & .code-collapse-state {
    position: absolute;
    inset: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    transition:
      opacity 0.25s ease,
      filter 0.25s ease,
      transform 0.25s ease;

    & svg {
      width: 1.25rem;
      height: 1.25rem;
    }
  }

  & .code-collapse-hide {
    opacity: 0;
    filter: blur(4px);
    transform: scale(0.6);
  }

  &[data-code-expanded='true'] {
    & > .code-collapse-viewport {
      height: var(--code-full-height);

      &::after {
        opacity: 0;
      }
    }

    & .code-collapse-show {
      opacity: 0;
      filter: blur(4px);
      transform: scale(0.6);
    }

    & .code-collapse-hide {
      opacity: 1;
      filter: blur(0);
      transform: scale(1);
    }
  }
}

@media (prefers-reduced-motion: reduce) {
  .astro-code[data-code-collapsible] {
    & > .code-collapse-viewport,
    & > .code-collapse-viewport::after,
    & > .code-collapse-toggle,
    & .code-collapse-state {
      transition: none;
    }
  }
}

.dark {
  .astro-code pre span {
    color: var(--shiki-dark) !important;
  }
  :not(pre) > code {
    color: hsl(var(--foreground));
  }
  :not(.astro-code) > code span {
    color: var(--shiki-dark, hsl(var(--foreground))) !important;
  }
}

/* Scroll bar */
:root {
  --scrollbar-width: 6px;
  --scrollbar-bg: #e0e0e0;
  --scrollbar-thumb-bg: #c0c0c0;
}
html.dark {
  --scrollbar-bg: #2e2e2e;
  --scrollbar-thumb-bg: #4e4e4e;
}
body::-webkit-scrollbar {
  height: 0;
}
::-webkit-scrollbar {
  width: 6px !important;
  height: 6px !important;
  background: #0000;
}
::-webkit-scrollbar-thumb {
  background-color: var(--scrollbar-thumb-bg);
  border-radius: 999px;
  border: 2px solid var(--scrollbar-thumb-bg);
  opacity: 0.5;
}
::-webkit-scrollbar-corner {
  background: transparent;
}
::-webkit-scrollbar-track {
  background: transparent;
}
