/* Note-like paper colors shared by rendered Markdown and the Markdown editor */
:root {
  --app-note-background: light-dark(#faf3e3, #221d17);
  --app-note-border: light-dark(#e6d9bd, #3d352a);
  /* Code blocks are lighter (light) or darker (dark) than the note so they stand out. */
  --app-code-background: light-dark(#fffefb, #110e0b);
}

/* Rendered Markdown (see MarkdownRenderer), set apart from the page as a note */
.markdown {
  margin-bottom: var(--pico-spacing);
  padding: 1rem 1.25rem;
  border: 1px solid var(--app-note-border);
  border-radius: var(--pico-border-radius);
  background: var(--app-note-background);
  overflow-wrap: anywhere;

  /* The last content element; the copy button <template> comes after it. */
  > :nth-last-child(1 of :not(template)) {
    margin-bottom: 0;
  }

  /* Keep headings in the note smaller than the page headings. */
  h1 { font-size: 1.5rem; }
  h2 { font-size: 1.3rem; }
  h3 { font-size: 1.15rem; }

  pre {
    overflow-x: auto;
    border: 1px solid var(--app-note-border);
    border-radius: var(--pico-border-radius);
    background-color: var(--app-code-background);
  }

  pre > code {
    background: none;
  }

  /* Rouge sets the text colors on <pre class="highlight">; the background comes from the note theme. */
  pre.highlight {
    background-color: var(--app-code-background);

    > code {
      color: inherit;
    }

    .w {
      background: none;
    }
  }

  /* Tables shrink to their content and scroll when wider than the page. */
  table {
    display: block;
    width: max-content;
    max-width: 100%;
    overflow-x: auto;
  }

  pre.mermaid {
    border: none;
    background: transparent;
    text-align: center;
  }

  /* Header anchors generated by Commonmarker are empty links. */
  a.anchor {
    display: none;
  }

  /* Task lists */
  li:has(> input[type="checkbox"]) {
    list-style: none;
  }

  .footnotes {
    margin-top: var(--pico-spacing);
    padding-top: 0.5rem;
    border-top: 1px solid var(--pico-muted-border-color);
    font-size: 0.875em;
  }
}

/* Markdown source editor in forms */
textarea.markdown-editor {
  border-color: var(--app-note-border);
  background-color: var(--app-note-background);
  font-family: var(--pico-font-family-monospace);
  font-size: 0.875rem;
  line-height: 1.6;
}

/* Code blocks with a copy button (see code_copy_controller.js) */
.markdown .code-block {
  position: relative;
  margin-bottom: var(--pico-spacing);

  > pre {
    margin-bottom: 0;
  }

  .code-copy {
    position: absolute;
    top: 0.375rem;
    right: 0.375rem;
    width: auto;
    margin: 0;
    padding: 0.25rem 0.375rem;
    line-height: 1;
    background-color: var(--app-code-background);
    opacity: 0;
    transition: opacity var(--pico-transition);

    > svg:last-child {
      display: none;
    }

    &.copied {
      color: var(--pico-ins-color);
      border-color: var(--pico-ins-color);

      > svg:first-child {
        display: none;
      }

      > svg:last-child {
        display: inline-block;
      }
    }
  }

  &:hover .code-copy,
  .code-copy:focus-visible,
  .code-copy.copied {
    opacity: 1;
  }

  /* No hover on touch screens, so keep the button visible. */
  @media (hover: none) {
    .code-copy {
      opacity: 1;
    }
  }
}
