/*
 * Annotated code blocks ({% annotated_code %}).
 *
 * Base styles are the no-JS / narrow-screen fallback: an ordinary code block
 * followed by the notes as a list. js/annotated-code.js adds `.anncode--live`
 * to switch on the pinned, scroll-driven side-by-side layout.
 */

.anncode {
  --anncode-notes-width: 16rem;
  --anncode-gap: 2rem;
  --anncode-stage-height: min(82vh, 46rem);
  --anncode-accent: #c2453c;
  --anncode-line-height: 1.5;

  margin: 2rem 0;
  padding: 0;
}

.anncode__pane {
  background-color: #f9f9f9;
  border-radius: 4px;
  margin-bottom: 1rem;
  min-width: 0;
  overflow: hidden;
  position: relative;
}

.anncode__caption {
  border-bottom: 1px solid #ececec;
  color: #8a8a8a;
  font-family: Menlo, Monaco, "Courier New", monospace;
  font-size: 0.7rem;
  padding: 0.4rem 0.8rem;
}

.anncode__scroll {
  overflow: auto;
  position: relative;
}

.anncode__pane pre {
  background: none;
  font-size: 0.7rem;
  line-height: var(--anncode-line-height);
  margin: 0;
  /* Grow to the longest line so the right padding lands after it, rather
     than the line overflowing past the padding - otherwise the padding never
     counts towards the scroll width the live-mode fit check relies on. */
  min-width: 100%;
  padding: 0.75rem 0.8rem 0.75rem 2.9rem;
  white-space: pre;
  width: max-content;
  word-break: normal;
  word-wrap: normal;
}

.anncode__line {
  display: block;
  min-height: calc(1em * var(--anncode-line-height));
  position: relative;
}

.anncode__line::before {
  color: #c4c4c4;
  content: attr(data-line);
  left: -2.2rem;
  pointer-events: none;
  position: absolute;
  text-align: right;
  -webkit-user-select: none;
  user-select: none;
  transition: color 0.2s ease;
  width: 1.6rem;
}

.anncode:not(.anncode--live) .anncode__line.is-circled {
  background-color: rgba(194, 69, 60, 0.07);
  box-shadow: -0.6rem 0 0 rgba(194, 69, 60, 0.07), 0.6rem 0 0 rgba(194, 69, 60, 0.07);
}

.anncode__line.is-circled::before {
  color: var(--anncode-accent);
}

/* The inner span hugs the text, so the overlay can circle the code rather
   than the full width of the scroll pane. */
.anncode__text {
  display: inline-block;
}

.anncode__notes {
  list-style: none;
  margin: 0;
  padding: 0;
}

.anncode__note {
  display: flex;
  gap: 0.6rem;
  margin-bottom: 0.9rem;
}

.anncode__marker {
  align-items: center;
  background: #fff;
  border: 1px solid #d8d8d8;
  border-radius: 50%;
  color: #8a8a8a;
  cursor: pointer;
  display: flex;
  flex: 0 0 auto;
  font: inherit;
  font-size: 0.75rem;
  height: 1.5rem;
  justify-content: center;
  margin-top: 0.1rem;
  padding: 0;
  transition: background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease;
  width: 1.5rem;
}

.anncode__body {
  min-width: 0;
}

.anncode__body > :last-child {
  margin-bottom: 0;
}

.anncode__ref {
  color: #a0a0a0;
  font-family: Menlo, Monaco, "Courier New", monospace;
  font-size: 0.65rem;
  margin-top: 0.25rem;
}

.anncode__overlay {
  display: none;
}

.anncode__marker:hover {
  border-color: var(--anncode-accent);
  color: var(--anncode-accent);
}

/*
 * Live mode: stage pinned to the viewport, notes beside the code, and the
 * overlay drawing from the active note to the lines it talks about.
 */

.anncode--live .anncode__stage {
  align-items: stretch;
  display: flex;
  gap: var(--anncode-gap);
  height: var(--anncode-stage-height);
  position: sticky;
  top: calc((100vh - var(--anncode-stage-height)) / 2);
}

/* Sized to the longest line rather than stretched, so a very wide column
   doesn't leave a slab of empty background between code and notes. */
.anncode--live .anncode__pane {
  display: flex;
  flex: 0 1 auto;
  flex-direction: column;
  margin-bottom: 0;
  max-width: 100%;
}

.anncode--live .anncode__scroll {
  flex: 1 1 auto;
  overflow-x: auto;
  overflow-y: hidden;
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 14px, #000 calc(100% - 14px), transparent 100%);
  mask-image: linear-gradient(to bottom, transparent 0, #000 14px, #000 calc(100% - 14px), transparent 100%);
}

.anncode--live .anncode__notes {
  align-self: center;
  flex: 0 0 var(--anncode-notes-width);
  max-height: 100%;
  overflow: hidden;
}

.anncode--live .anncode__note {
  font-size: 0.85rem;
  opacity: 0.32;
  transition: opacity 0.3s ease;
}

/* The note is emphasised exactly as much as its annotation is drawn. */
.anncode--live .anncode__note.is-active {
  opacity: calc(0.32 + 0.68 * var(--anncode-fade, 1));
  transition: none;
}

.anncode--live .anncode__note.is-active .anncode__marker {
  background: var(--anncode-accent);
  border-color: var(--anncode-accent);
  color: #fff;
}

/* Above the overlay, so the arrow appears to start from under the number. */
.anncode--live .anncode__marker {
  position: relative;
  z-index: 3;
}

/* Line numbers fade along with the overlay they belong to. */
.anncode--live .anncode__line.is-circled::before {
  color: color-mix(in srgb, var(--anncode-accent) calc(var(--anncode-fade, 1) * 100%), #c4c4c4);
  transition: none;
}

.anncode--live .anncode__overlay {
  display: block;
  height: 100%;
  left: 0;
  overflow: visible;
  pointer-events: none;
  position: absolute;
  top: 0;
  width: 100%;
  z-index: 2;
}

@media (min-width: 82em) {
  .anncode--live .anncode__pane pre {
    font-size: clamp(0.6rem, 0.88vw, 0.7rem);
  }
}

@media (prefers-reduced-motion: reduce) {
  .anncode__line::before,
  .anncode__note {
    transition: none;
  }
}
