.ProseMirror-container {
  margin: 0;
  overscroll-behavior: contain;
  overflow-anchor: none;
  flex-direction: column;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  height: 100%;
}

.ProseMirror {
  outline: 0;
  padding: 0 0.625rem;

  > div:first-child,
  > details:first-child {
    margin-top: 0.5rem;

    // This is hacky, but helps having the leading gapcursor at the right position
    &.ProseMirror-gapcursor {
      position: relative;
      display: block;
    }
  }

  h1,
  h2,
  h3,
  h4,
  h5,
  h6 {
    margin: 30px 0 10px;

    &:first-child {
      margin-top: 10px;
    }
  }

  h1 {
    font-size: var(--font-up-3-rem);
  }

  h2 {
    font-size: var(--font-up-2-rem);
  }

  h3 {
    font-size: var(--font-up-1-rem);
  }

  img[data-placeholder="true"] {
    animation: placeholder 1.5s infinite;

    @keyframes placeholder {
      0% {
        opacity: 0.6;
      }

      50% {
        opacity: 0.4;
      }

      100% {
        opacity: 0.6;
      }
    }
  }

  ul,
  ol {
    padding-left: 1.25em;

    &[data-tight="true"] > li > p {
      margin-top: 0;
      margin-bottom: 0;
    }
  }

  p {
    line-height: 1.5;

    &:first-child {
      margin-top: 0.59rem;
    }
  }

  p[data-placeholder] {
    overflow: hidden;

    &::before {
      pointer-events: none;
      position: absolute;
      padding-top: 2px;
      padding-right: 0.5rem;
      content: attr(data-placeholder);
      color: var(--primary-400);
      line-height: 1.1;
    }
  }

  del {
    background-color: var(--danger-low);
  }

  ins {
    background-color: var(--success-low);
  }

  mark {
    background-color: var(--highlight);
  }

  td {
    padding: 3px 3px 3px 0.5em;
  }

  th {
    padding-bottom: 2px;
    font-weight: bold;
    color: var(--primary);
  }

  a:not([contenteditable="false"] a) {
    cursor: inherit;
  }

  kbd {
    // I believe this shouldn't be `inline-flex` in posts either (test with emojis before/after text to see why),
    // but overriding just for the editor for now
    display: inline;
    padding-top: 0.2rem;
  }

  .onebox-wrapper {
    white-space: normal;
  }

  .code-language-select {
    position: absolute;
    right: 0.25rem;
    top: -0.6rem;
    border: 1px solid var(--content-border-color);
    border-radius: var(--d-border-radius);
    background-color: var(--primary-very-low);
    color: var(--primary-medium);
    font-size: var(--font-down-1-rem);
  }

  pre {
    position: relative;
  }

  .html-block {
    border: 1px dashed var(--primary-low-mid);

    &::after {
      position: absolute;
      right: 0;
      top: 0;
      content: "HTML";
      font-size: var(--font-down-2-rem);
      color: var(--primary-low-mid);
      z-index: 1;
    }
  }

  .onebox-loading {
    border-radius: var(--d-border-radius);
    padding: 0 0.125rem;
    background: linear-gradient(
      90deg,
      var(--primary-very-low) 0%,
      var(--primary-low) 50%,
      var(--primary-very-low) 100%
    );
    background-size: 200% 100%;
    animation: loading-pulse 1.5s ease-in-out infinite;
  }

  @keyframes loading-pulse {
    0% {
      background-position: 200% 0;
    }

    100% {
      background-position: -200% 0;
    }
  }
}

.fk-d-menu[data-identifier="composer-link-toolbar"],
.fk-d-menu[data-identifier="composer-image-toolbar"],
.fk-d-menu[data-identifier="composer-image-alt-text"] {
  user-select: none;
  z-index: z("composer", "dropdown");
  animation: fade-in ease 0.25s 1 forwards !important;

  .fk-d-menu__inner-content {
    overflow: visible;
  }

  .toolbar__button {
    background-color: var(--secondary);
    color: var(--primary-medium);
    border-radius: var(--d-border-radius);

    .d-icon {
      color: var(--primary-medium);
    }

    &:hover:not(:disabled) {
      color: var(--primary);

      .d-icon {
        color: var(--primary);
      }
    }

    &:active {
      background: var(--secondary);
    }

    &:focus-visible {
      @include default-focus;
    }

    &.--active {
      background: var(--d-selected);
    }
  }

  .composer-link-toolbar__visit {
    .d-button-label {
      max-width: 12rem;
    }
  }
}

.composer-link-toolbar__visit {
  min-width: 0;
  align-items: center;

  // visit link has a right-side icon
  flex-direction: row-reverse;

  .d-icon {
    // overrides the default from d-button.scss
    margin-right: 0;
  }

  .d-button-label {
    font-size: var(--font-down-1-rem);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  &:not(:disabled) .d-button-label {
    margin-right: 0.5rem;
  }
}

.fk-d-menu[data-identifier="composer-image-alt-text"] {
  width: calc(100% - 1rem);
  min-width: calc(max(100% - 1rem, 7.25rem));
  background-color: rgb(var(--secondary-rgb), 0.65);
  backdrop-filter: blur(4px);
  color: var(--primary);
  isolation: auto;
  border-radius: var(--d-border-radius);
  font-size: var(--font-down-1);
  box-shadow: var(--shadow-dropdown);

  .fk-d-menu__inner-content {
    width: 100%;
    box-sizing: border-box;
    border: none;
    box-shadow: none;
    background-color: transparent;
    min-width: 100%;
    padding: 0.25rem 0.5rem;
  }

  &:focus-within {
    box-shadow: 0 0 0 2px var(--tertiary-high);
  }

  &:has(.image-alt-text-input.--expanded) {
    min-width: calc(max(100% - 1rem, 15rem));
  }
  transition: min-width 0.15s ease;
}

.composer-image-node {
  display: inline-block;
  white-space: normal;
  position: relative;

  // This makes the editor less pixel perfect with cooked, but it helps with some
  // hard positions (like between images), and makes the blinking cursor more visible
  // The vertical offset increases the drop area between image paragraphs
  margin: -2px 1px;
  padding: 2px 1px;

  img {
    display: inline-block;
    height: auto;
    max-width: 100%;
    border-radius: var(--d-border-radius);
    transition:
      width 0.2s ease,
      height 0.2s ease;

    &:not(.ProseMirror-selectednode):hover {
      cursor: pointer;
    }

    &.ProseMirror-selectednode {
      user-select: none;
    }
  }
}

.image-alt-text-input {
  min-width: 100%;
  display: flex;
  position: relative;
  transition: height 0.15s ease;
  height: 1.5em;

  @media (prefers-reduced-motion: reduce) {
    transition: none;
  }

  &.--expanded {
    height: 4.25em;
  }

  &__field {
    flex: 1;
    border: none;
    outline: none;
    padding: 0;
    background: transparent;
    resize: none;
    margin: 0;
    scrollbar-color: rgb(var(--primary-rgb), 0.3) transparent;
    scrollbar-width: thin;
    overflow-y: auto;
    scrollbar-gutter: stable;
    line-height: var(--line-height-large);

    &::placeholder {
      color: var(--primary);
    }

    &:focus {
      outline: none;
    }
  }

  &__display {
    cursor: pointer;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    flex: 1;
  }
}

/*********************************************************
 Section below from prosemirror-view/style/prosemirror.css
 ********************************************************/

// stylelint-disable-next-line no-duplicate-selectors
.ProseMirror {
  word-wrap: break-word;
  white-space: break-spaces;
}

// stylelint-disable-next-line no-duplicate-selectors
.ProseMirror pre {
  white-space: pre-wrap;
}

.ProseMirror li {
  position: relative;
}

.ProseMirror-hideselection *::selection {
  background: transparent;
}

.ProseMirror-hideselection {
  caret-color: transparent;
}

/* See https://github.com/ProseMirror/prosemirror/issues/1421#issuecomment-1759320191 */
.ProseMirror [draggable][contenteditable="false"] {
  user-select: text;
}

.ProseMirror-selectednode {
  outline: 2px solid var(--tertiary-high);
  border-radius: var(--d-border-radius);
}

/* Make sure li selections wrap around markers */
li.ProseMirror-selectednode {
  outline: none;
}

li.ProseMirror-selectednode::after {
  content: "";
  position: absolute;
  left: -32px;
  right: -2px;
  top: -2px;
  bottom: -2px;
  border: 2px solid #8cf;
  pointer-events: none;
}

/* Protect against generic img rules */
.ProseMirror-separator {
  display: inline !important;
  border: none !important;
  margin: 0 !important;
}

/************************************************************
 Section below from prosemirror-gapcursor/style/gapcursor.css
 ***********************************************************/

.ProseMirror-gapcursor {
  display: none;
  pointer-events: none;
  position: sticky;
}

.ProseMirror-gapcursor::after {
  content: "";
  display: block;
  position: absolute;
  top: -2px;
  width: 20px;
  border-top: 1px solid var(--primary);
  animation: ProseMirror-cursor-blink 1.1s steps(2, start) infinite;
}

@keyframes ProseMirror-cursor-blink {
  to {
    visibility: hidden;
  }
}

.ProseMirror-focused .ProseMirror-gapcursor {
  display: block;
}

/************************************************************
 Section below from prosemirror-codemark/src/codemark.css
 ***********************************************************/
@keyframes fake-cursor-blink {
  49% {
    border-color: unset;
  }

  50% {
    border-color: transparent;
  }

  99% {
    border-color: transparent;
  }
}

.no-cursor {
  caret-color: transparent;
}

div:focus .fake-cursor,
span:focus .fake-cursor {
  margin-right: -1px;
  border-left-width: 1px;
  border-left-style: solid;
  animation: fake-cursor-blink 1s;
  animation-iteration-count: infinite;
  position: relative;
  z-index: 1;
}
