:root {
  --tiptap-border-radius: 0.5em;
}

/* Book writing and reading (pages) */
:where(tiptap-editor.page__editor, tiptap-editor.page__reader) {
  font-size: 0.8em;

  .tiptap-content,
  .ProseMirror {
    font-family: var(--font-book);
    text-align: justify;
    text-justify: inter-word;
  }

  .ProseMirror {
    --paragraph-indent: 1.4em;

    p {
      text-indent: var(--paragraph-indent);
    }

    :where(blockquote, pre, ul, ol, hr, figure, table) + p {
      text-indent: 0;
    }

    :where(blockquote, li) p {
      text-indent: 0;
    }
  }
}

/* Continuous text: a comfortable column, on a phone as when sheets are turned off */
tiptap-editor.page__editor:not([paginating]) {
  color: var(--ink-1);
  font-size: 18px;
  inline-size: min(100%, 38em);
  line-height: 1.6;
  margin-inline: auto;

  .ProseMirror {
    min-block-size: 60dvh;
  }
}

/* Paginated sheets: the book's own typography, sized as in the PDF export */
tiptap-editor[paginating] {
  align-items: center;
  font-size: var(--page-font-size);

  .ProseMirror {
    --paragraph-indent: var(--page-paragraph-indent);

    background: var(--sheet);
    border: 0 !important;
    border-radius: var(--radius-sheet);
    box-shadow: var(--shadow-md);
    color: var(--ink-1);
    font-family: var(--page-font-family);
    line-height: var(--page-line-height);
    margin-inline: auto;
    text-align: var(--page-text-align);

    /* Between two sheets the ground shows through. It is a fixed gradient, so
       the same one painted here lines up with the rest of the page. */
    .rm-pagination-gap {
      background: var(--bg) fixed !important;
      border: 0 !important;
      box-shadow:
        inset 0 10px 12px -12px oklch(var(--shadow-color) / 0.35),
        inset 0 -1px 0 oklch(var(--shadow-color) / 0.05);
    }

    .rm-page-number {
      color: var(--ink-3);
    }

    p {
      margin-block: 0 var(--page-paragraph-spacing);
    }

    h1, h2, h3, h4, h5, h6 {
      line-height: normal;
      margin-block: 0 10pt;
      text-align: start;
    }

    h1 { font-size: 1.64em; }
    h2 { font-size: 1.36em; }
    h3 { font-size: 1.18em; }
    h4 { font-size: 1.09em; }
    h5, h6 { font-size: 1em; }

    .page__heading {
      font-size: 2em;
      font-weight: bold;
      line-height: normal;
      margin-block: 0 36pt;
      padding-block-start: 72pt; /* CHAPTER_HEADING_DROP in the PDF export */
      text-align: center;
      text-indent: 0;
    }

    /* The break stretches to the end of its sheet: no margin of its own */
    .page-break {
      margin-block: 0;
    }

    /* Same height as PAGE_FOOTER_HEIGHT in tiptap_editor.js */
    .rm-page-footer-content {
      block-size: 20px;
      line-height: 20px;
      overflow: hidden;
    }

    .rm-page-footer-right {
      float: none;
      font-family: var(--font-sans);
      font-size: 13px;
      inline-size: 100%;
      margin-inline: 0;
      text-align: center;
    }
  }
}

/* Editor Container */
:where(tiptap-editor) {
  display: flex;
  flex-direction: column;
  flex-grow: 1;

  &:invalid {
    border: var(--color-negative) 2px solid;
  }
}

/* Toolbar */
:where(tiptap-toolbar) {
  --hover-size: 0;

  border-radius: var(--tiptap-border-radius);
  display: inline-flex;
  gap: 0.1em;

  :is(button, label) {
    aspect-ratio: 6/5;
    align-items: center;
    appearance: none;
    background-color: transparent;
    border: none;
    border-radius: var(--tiptap-border-radius);
    cursor: pointer;
    display: inline-flex;
    flex-shrink: 0;
    inline-size: auto;
    justify-content: center;
    min-block-size: 2em;
    transition: background-color 300ms ease;

    :is(img, svg) {
      -webkit-touch-callout: none;
      block-size: 1em;
      fill: currentColor;
      inline-size: auto;
      user-select: none;
    }

    &:where(:focus-visible) {
      background-color: var(--color-bg);
    }

    @media (hover: hover) {
      &:hover {
        background-color: var(--color-bg);
      }
    }

    &:where(:active),
    &:where(.active) {
      background-color: var(--color-selected-dark);
    }
  }
}

/* Tiptap Content Area */
:where(.tiptap-content) {
  caret-color: var(--color-link);
  flex-grow: 1;
  min-block-size: 50dvh;
  text-align: start;

  &:focus,
  &:active {
    border: none;
    outline: none;
  }
}

/* ProseMirror Editor (Tiptap internal) */
:where(.ProseMirror) {
  outline: none;
  min-block-size: 100%;

  &:focus {
    outline: none;
  }

  /* Placeholder */
  p.is-editor-empty:first-child::before {
    color: var(--color-subtle);
    content: attr(data-placeholder);
    float: left;
    height: 0;
    pointer-events: none;
  }

  /* Typography */
  h1, h2, h3, h4, h5, h6 {
    font-weight: bold;
    line-height: 1.2;
    margin-block: 1em 0.5em;
  }

  h1 { font-size: 2em; }
  h2 { font-size: 1.5em; }
  h3 { font-size: 1.25em; }
  h4 { font-size: 1.1em; }
  h5 { font-size: 1em; }
  h6 { font-size: 0.9em; }

  p {
    margin-block: 0.5em;
  }

  /* Lists */
  ul, ol {
    padding-inline-start: 1.5em;
    margin-block: 0.5em;
  }

  ul {
    list-style-type: disc;
  }

  ol {
    list-style-type: decimal;
  }

  li {
    margin-block: 0.25em;
  }

  /* Blockquote */
  blockquote {
    border-inline-start: 3px solid var(--color-subtle);
    margin-inline: 0;
    padding-inline-start: 1em;
    color: var(--color-ink-subtle);
    font-style: italic;
  }

  /* Code */
  code {
    background-color: var(--color-subtle);
    border-radius: 0.25em;
    font-family: monospace;
    padding: 0.1em 0.3em;
  }

  pre {
    background-color: var(--color-subtle);
    border-radius: var(--tiptap-border-radius);
    font-family: monospace;
    margin-block: 0.5em;
    overflow-x: auto;
    padding: 1em;

    code {
      background: none;
      padding: 0;
    }
  }

  /* Links */
  a {
    color: var(--color-link);
    text-decoration: underline;
    cursor: pointer;
  }

  /* Images */
  img {
    max-inline-size: 100%;
    block-size: auto;
    border-radius: var(--tiptap-border-radius);
    margin-block: 0.5em;
  }

  /* Horizontal Rule */
  hr {
    border: none;
    border-block-start: 1px solid var(--color-subtle);
    margin-block: 1em;
  }

  /* Manual page break */
  .page-break {
    border-block-start: 1px dashed var(--color-subtle-dark);
    cursor: default;
    margin-block: 2em;
    position: relative;

    &::after {
      background-color: var(--color-bg);
      color: var(--color-subtle-dark);
      content: "Saut de page";
      font-family: var(--font-sans, system-ui);
      font-size: 0.75em;
      inset-block-start: 0;
      inset-inline-start: 50%;
      padding-inline: 1ch;
      position: absolute;
      text-indent: 0;
      transform: translate(-50%, -50%);
      white-space: nowrap;
    }

    &.ProseMirror-selectednode {
      border-color: var(--color-link);

      &::after {
        color: var(--color-link);
      }
    }
  }

  /* Strong and Emphasis */
  strong {
    font-weight: bold;
  }

  em {
    font-style: italic;
  }

  s {
    text-decoration: line-through;
  }
}

/* Over the bubble and the assistant, which may both be in the way */
.synonyms-menu {
  background-color: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-md);
  box-shadow: var(--shadow-lg);
  color: var(--ink-1);
  font-family: var(--font-sans);
  inline-size: min(32ch, calc(100vw - 2rem));
  padding: 0.75em;
  position: fixed;
  z-index: 40;
}

.synonyms-menu__header {
  margin-block-end: 0.5em;
}

.synonyms-menu__list {
  display: grid;
  gap: 0.25em;
}

.synonyms-menu__item {
  --btn-border-radius: 0.6em;
  --btn-padding: 0.45em 0.6em;
  --btn-border-size: 0;
  --hover-size: 0;

  justify-content: start;
  inline-size: 100%;
  text-align: start;
}
