:where([data-theme="tokyo_night"]) {
  .shiki span {
    color: var(--shiki-tokyo) !important;
  }
}

/* When the page is in night mode (html.night) but data-theme is not tokyo-night,
   prefer the dark shiki color. This avoids overriding the explicit tokyo-night
   theme which has its own color. */
:where(html.night):not([data-theme="tokyo_night"]) {
  .shiki span {
    color: var(--shiki-dark) !important;
  }
}

/* ===== MAIN CODE BLOCK FIGURE ===== */
figure.shiki {
  margin: 0 0 24px;
  border-radius: 8px !important;
  border: 1.5px solid var(--surface0) !important;
  overflow: auto;
  background: var(--base);
  color: var(--text);
}

.shiki-tools {
  display: flex;
  justify-content: space-between;
  min-height: 30px;
  padding: 0 15px;
  font-family: "Maple Mono NF CN", "Maple Mono CN", monospace;
  font-weight: bolder;
  user-select: none;
  overflow: hidden;
  position: relative;
  background: var(--base);
  color: var(--subtext1);

  >.left {
    gap: 5px;
    display: flex;
    align-items: center;
  }

  >.center {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-grow: 1;
    overflow: hidden;
    min-width: 0;
  }

  >.right {
    gap: 10px;
    display: flex;
    align-items: center;

    >i {
      cursor: pointer;
      transition:
        color 0.3s,
        transform 0.3s ease;
    }

    >i:hover {
      color: var(--blue);
    }
  }
}

.shiki-tools .expand {
  font-size: 1.2em;
  margin-left: -2px;
}

.shiki-tools .expand.closed {
  transform: rotate(90deg);
}

/* ===== COPY NOTICE ===== */
.shiki-tools>.right>.copy-notice {
  position: absolute;
  white-space: nowrap;
  opacity: 0;
  visibility: hidden;
  z-index: 1000;
  padding: 0 5px 0 0;
  transform: translateX(-110%);
  transition:
    opacity 0.2s ease,
    visibility 0.2s ease;

  &[style*="opacity: 1"] {
    opacity: 1;
    visibility: visible;
  }
}

/* ===== TRAFFIC LIGHTS ===== */
.traffic-lights {
  display: flex;
  align-items: center;
  padding-right: 8px;
  margin-left: -7px;
}

.traffic-light {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  margin-left: 8px;
  position: relative;

  &.red {
    background-color: var(--red, #f38ba8);
  }

  &.yellow {
    background-color: var(--yellow, #fab387);
  }

  &.green {
    background-color: var(--green, #a6e3a1);
  }
}

/* ===== EXPAND BUTTON ===== */
@keyframes code-expand-pulse {

  0%,
  100% {
    opacity: 0.6;
  }

  50% {
    opacity: 0.1;
  }
}

.code-expand-btn {
  position: absolute;
  bottom: 0;
  width: 100%;
  z-index: 1000;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 1em;
  cursor: pointer;

  >i {
    padding: 6px 0;
    animation: code-expand-pulse 1.2s infinite;
    transition: transform 0.3s ease;
  }

  &.expand-done>i {
    transform: rotate(180deg);
  }
}

/* ===== CODE COLLAPSE FUNCTIONALITY ===== */
figure.shiki[data-collapsible="true"] {
  position: relative;
}

figure.shiki[data-collapsible="true"]>pre.shiki {
  position: relative;
  overflow: hidden;
  transition: max-height 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
}

figure.shiki[data-collapsible="true"]:not(.expanded)>pre.shiki::after {
  content: "";
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 80px;
  pointer-events: none;
  z-index: 1;
  background: linear-gradient(transparent 0%,
      hsl(from var(--base) h s l / 0.8),
      30%,
      var(--base) 100%);
  opacity: 1;
}

figure.shiki[data-collapsible="true"].expanded>.code-expand-btn {
  transform: translateY(0);
}

/* ===== CODE BLOCK CONTENT ===== */
pre.shiki {
  background: var(--base);

  >code {
    text-align: left;
    counter-reset: step;
    counter-increment: step 0;
    padding: 0 !important;
    background: none !important;
    border: none !important;
    width: 100%;
    font-family:
      "Maple Mono CN", "Maple Mono NF CN", "SF Pro", "Fira Code",
      "JetbrainsMono Nerd Font", Menlo, "DejaVu Sans Mono", monospace;

    >span.line {
      display: block;
      width: fit-content;
      min-width: 100%;

      &::before {
        content: counter(step);
        counter-increment: step;
        width: 1.2rem;
        margin-right: 1.2rem;
        display: inline-block;
        text-align: right;
        color: rgba(115, 138, 148, 0.4);
      }
    }
  }
}

/* #region Transformer */
span.line.diff {
  &.remove {
    background-color: hsl(from var(--red) h s l / 0.2);

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

  &.add {
    background-color: hsl(from var(--green) h s l / 0.2);

    &:before {
      content: "+";
      color: var(--green);
    }
  }
}

span.line.highlighted {
  background-color: hsl(from var(--blue) h s l / 0.2);

  &.error {
    background-color: hsl(from var(--red) h s l / 0.2);
  }

  &.warning {
    background-color: hsl(from var(--yellow) h s l / 0.2);
  }
}

span.highlighted-word {
  background-color: hsl(from var(--yellow) h s l / 0.3);
  border-radius: 4px;
  margin: -1px -2px;
  padding: 1px 2px;
}

pre.shiki.has-focused>code {
  >.line {
    filter: blur(3px);
    opacity: 0.7;
    transition:
      filter 0.35s,
      opacity 0.35s;
  }

  >.line.focused,
  &:hover .line {
    filter: blur(0);
    opacity: 1;
  }
}

/* #endregion Transformer */