@import (inline) "../../../node_modules/prosemirror-view/style/prosemirror.css";
@import (inline) "../../../node_modules/prosemirror-gapcursor/style/gapcursor.css";
@import (inline) "../../../node_modules/@massifrg/prosemirror-tables-sections/style/tables.css";

.prosemirror, prose-mirror {
  display: flex;
  flex-direction: column;
  gap: 0.5rem 0; // Between menu and content
  min-height: var(--min-height);
  position: relative;

  // Toggle control button
  button.toggle {
    display: none;
    position: absolute;
    top: 0;
    right: 0;
    z-index: 1; // Above editor content
    &[disabled] {
      display: none;
    }
  }

  &:hover button.toggle:enabled { display: block; }

  // Active editors only
  .editor-container {
    flex: 1;
    position: relative;
    overflow: hidden;
    display: flex;
    flex-direction: column;
  }

  // Applies to both active and toggled inactive editors
  .editor-content {
    position: absolute;
    overflow: hidden auto; // Vertically scrolling
    padding-right: 0.5rem; // Scrollbar
    inset: 0;
    outline: 0 solid transparent;
    cursor: var(--cursor-text);
    user-select: text;
    &[contenteditable=""], &[contenteditable="true"] { cursor: auto; }
  }

  &.content-sized {
    flex: 1;
    .editor-content { position: unset; }
    .cm-scroller { position: unset; }
  }

  .menu-container {
    position: relative;
    flex: 0 0 var(--menu-height);
  }

  menu {
    display: flex;
    position: absolute;
    inset: 0;
    flex-wrap: nowrap;
    align-items: center;
    list-style: none;
    margin: 0;
    padding: var(--menu-padding, 8px);
    gap: 4px;
    background: var(--menu-background);
    border-radius: 6px;

    .source-code { margin-right: 4px; }

    .concurrent-users {
      display: none;
      justify-content: center;
      align-items: center;
      padding: 0 6px;
      background: var(--color-control-hover);
      border: 1px solid var(--color-shadow-highlight);
      border-radius: 3px;
      box-shadow: 0 0 4px var(--color-shadow-highlight);
      i { margin-right: 6px; }
    }

    button {
      --button-size: var(--menu-button-height);
      background: transparent;
      cursor: var(--cursor-pointer);
      border: none;
      border-radius: 4px;
      outline: none;
      i { margin: 0; }
      &.source-code-action { background: var(--color-control-hover); }
      &:focus { outline: none; }
      &:hover, &.active {
        box-shadow: none;
        background: var(--color-control-hover);
        color: var(--color-form-label-hover);
      }
    }

    li.text:has(> button[hidden]) { display: none; }

    .pm-dropdown {
      width: min-content;
      position: relative;
      display: flex;
      align-items: center;
      border-radius: 4px;
      > span {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        font-size: var(--font-size-12);
      }
      .html { display: none; }
      i.fa-chevron-down {
        font-size: var(--font-size-11);
      }
      ul { display: none; }
      &.format {
        justify-content: start;
        min-width: 90px;
      }
    }
  }

  /* ----------------------------------------- */
  /*  Collaborating                            */
  /* ----------------------------------------- */

  &.collaborating menu {
    .source-code-edit { display: none; }

    .concurrent-users {
      display: flex;
      height: var(--menu-height);

      .scene-player {
        width: 14px;
        height: 15px;
        border-radius: 50%;
        margin-right: -3px;
        line-height: var(--line-height-12);
        font-size: .625rem;
        text-align: center;
        color: black;
        font-weight: 700;

        &:last-child { margin: 0; }
      }
    }
  }

  &.collaborating:has(code-mirror[disabled]) menu [data-action=save] { display: none; }

  /* ----------------------------------------- */
  /*  Source Code Editing                      */
  /* ----------------------------------------- */

  &.editing-source {
    menu {
      .text { display: none; }
      .html { display: initial; }
      .right { margin-left: auto; }
      .right ~ .right { margin-left: unset; }
    }

    .editor-container { display: none }

    code-mirror.source-editor {
      border: 1px dashed var(--color-light-4);
      border-radius: 4px;
      margin: 2px;
    }
  }

  /* ----------------------------------------- */
  /*  Tables                                   */
  /* ----------------------------------------- */

  .tableWrapper {
    th, td {
      border-left: 1px solid black;
      border-right: 1px solid black;
    }
  }

  .ProseMirror .selectedCell::after {
    // Prevent select cell highlight appearing above menu dropdowns.
    z-index: unset;
  }
}

prose-mirror.inactive { overflow: hidden auto; }

/* ----------------------------------------- */
/*  Drop-downs                               */
/* ----------------------------------------- */

#prosemirror-dropdown {
  position: fixed;
  z-index: var(--z-index-tooltip);
  color: var(--button-text-color);

  .html { display: none; }

  ul {
    display: none;
    position: absolute;
    top: 0;
    left: 100%;
    min-width: 90px;
    padding: 6px;
    margin: 0;
    list-style: none;
    background: var(--dropdown-background);
    border-radius: 3px;
    border: 1px solid var(--dropdown-border);
    font-size: var(--font-size-12);
    line-height: var(--font-size-24);
    z-index: 1;
    box-shadow: 0 3px 6px rgb(0 0 0 / 45%);
  }

  > ul {
    display: block;

    &:not(:has(> li > ul)) {
      max-height: 400px;
      overflow: hidden auto;
    }
  }

  li {
    padding: 1px 6px;
    cursor: var(--cursor-pointer);
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-radius: 3px;
    position: relative;
    white-space: nowrap;

    i { font-size: var(--font-size-14); }

    &.divider {
      pointer-events: none;
      margin-bottom: 4px;
      border-bottom: 1px solid #999;
    }

    &.level1 { font-size: 2em; }
    &.level2 { font-size: 1.5em; }
    &.level3 { font-size: 1.25em; }

    &.level5 {
      font-size: .83em;
      font-weight: bold;
    }

    &.level6 {
      font-size: .67em;
      font-weight: bold;
    }

    &:hover {
      background: var(--color-control-hover);
      > ul { display: block; }
      > :is(span, i) { color: var(--color-form-label-hover); }
    }
  }
}

/* ----------------------------------------- */
/*  Insertion Dialogs                        */
/* ----------------------------------------- */

.prosemirror.menu-dialog {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: transparent;
  border: none;
  box-shadow: none;
  padding: 0;

  form {
    width: 460px;
    position: fixed;
    background: var(--dialog-background);
    border: 1px solid var(--dialog-border);
    border-radius: 4px;
    padding: 8px;
    box-shadow: 0 0 8px 0 var(--color-shadow-dark);
    gap: 8px;
  }
}

/* ----------------------------------------- */
/*  Tables                                   */
/* ----------------------------------------- */

// Reset some styles from tables plugin.
.ProseMirror table {
  > thead, > tfoot { border-left: unset; }
  > tbody:nth-child(even), > tbody:nth-child(odd) { border-left: unset; }
  th, td {
    > p:has(+ .column-resize-handle) { margin-bottom: 0; }
  }
}

/* ----------------------------------------- */
/*  Chat                                     */
/* ----------------------------------------- */

// TODO: Move into sidebar.less when prosemirror.less is moved out of compatibility layer.
#chat-notifications prose-mirror#chat-message {
  &:not(:focus-within) .menu-container { display: none; }
}

#chat-popout .prosemirror {
  gap: 0;
  menu { border-radius: 0; }
  .editor-container {
    border: none;
    border-top: var(--chat-input-border);
  }
}
