/* The rich text editor's chrome (Forms::RichTextComponent, rich_text_controller.js): the frame around the editing surface, the toolbar,
   the link form, the surface itself, the list of pictures under it. How RENDERED narratives look (the display component) is another
   stylesheet; this one is only what a person meets while writing. Tokens only and logical properties only (test/styles/tokens_test.rb,
   test/styles/components_test.rb), so the editor reads in light and dark, on the shell and the console, and right to left.

   Linked after 36-form-fields.css, whose rules for a field (label, help, error, control, option row) the editor reuses: it is a `.field`.

   With no script the field is a text area and nothing here shows: the toolbar, the surface and the pictures are all `hidden` in the
   markup. When the script has started the editor it sets `data-rich-text-state="ready"` on the field, and then (and only then) the
   textarea, which stays in the form as the control that is submitted, is taken out of sight.

   The rules the design keeps, each with its reason:

   * The frame is the control. It draws the edge `--control-line` (3:1, 1.4.11), thickens when the field is invalid and wears the ring of
     `--focus` two pixels out when the surface has the focus, as every other field does (36-form-fields.css). The toolbar sits inside it,
     on the sunken ground, so the buttons and the text are one thing.
   * The buttons are 40px for a pointer (24px is WCAG's floor) and `--target-min` on touch (2.5.8). A pressed toggle is a fill AND a thicker
     edge, so it is never color alone (1.4.1), and in forced colors the edge is what remains.
   * Nothing is sticky, so nothing covers the focus (2.4.11); the surface grows with what is written and scrolls inside itself past 70dvh.
   * ProseMirror's own base rules are here because the editor is started with `injectCSS: false` (a runtime <style> would be a problem
     for a future content security policy, and its colors would not be ours). The `white-space` one is required: the editor misreads the
     page without it. */

/* ── The field ───────────────────────────────────────────────────────────── */

.rich-editor {
  position: relative;
  min-inline-size: 0;
  container: rich-editor / inline-size;
}
/* No script: the textarea is the control and draws its own edge, so the frame draws nothing. */
.field-rich-text[data-rich-text-state="ready"] .rich-editor {
  color: var(--fg1);
  background: var(--surface-raised);
  border: 1px solid var(--control-line);
  border-radius: var(--radius-md);
}
.field-rich-text[data-rich-text-state="ready"] .rich-editor:hover {
  border-color: var(--fg3);
}
.field-rich-text[data-rich-text-state="ready"] .rich-editor:has(.rich-editor-content:focus) {
  border-color: var(--accent);
}
.field-rich-text[data-rich-text-state="ready"] .rich-editor:has(.rich-editor-content:focus-visible) {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}
.field-rich-text[data-rich-text-state="ready"][data-invalid] .rich-editor {
  border-color: var(--tone-bad-fg);
  box-shadow: inset 0 0 0 1px var(--tone-bad-fg);
}
/* A field that is only read has no way to change it: the edge goes quiet and the text stays at full strength (a disabled control is exempt from
   contrast, but the words in it are still the record). */
.field-rich-text[data-rich-text-state="ready"][data-readonly] .rich-editor,
.field-rich-text[data-rich-text-state="ready"][data-disabled] .rich-editor {
  background: var(--surface-sunken);
  border-color: var(--rule-strong);
}

/* The textarea, once the editor is on: still there, still the form's control, out of sight and out of the tab order (the script sets
   aria-hidden and tabindex). 36-form-fields.css gives it a minimum height, padding, an edge and `field-sizing: content`, at a specificity
   of (0,2,1); this is (0,3,1). */
.field-rich-text[data-rich-text-state="ready"] textarea.rich-editor-source {
  position: absolute;
  inline-size: 1px;
  block-size: 1px;
  min-block-size: 0;
  max-block-size: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  border: 0;
  clip-path: inset(50%);
  white-space: nowrap;
  resize: none;
  field-sizing: fixed;
}

/* Everything the script reveals is `hidden` until it does. A `display` on these classes must not undo the attribute. */
.rich-editor-toolbar[hidden],
.rich-editor-link[hidden],
.rich-editor-surface[hidden],
.rich-editor-pictures[hidden],
.rich-editor-notice[hidden],
.rich-editor-picture [hidden],
.rich-editor-link [hidden] {
  display: none;
}

/* ── The toolbar ─────────────────────────────────────────────────────────── */

.rich-editor-toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-1);
  min-inline-size: 0;
  padding: var(--space-1);
  background: var(--surface-sunken);
  border-block-end: 1px solid var(--rule);
  border-start-start-radius: calc(var(--radius-md) - 1px);
  border-start-end-radius: calc(var(--radius-md) - 1px);
}
/* A group is the buttons that go together (the marks, the headings, the lists, the link and the picture, undo and redo). It draws no box of its own: the buttons wrap
   one by one, so a phone gets as few rows as the room allows, and the room after the last button of a group is what sets the groups apart where the whole toolbar fits
   on one line (the frame is the container, so this follows the room the field has and not the window). */
.rich-editor-group {
  display: contents;
}
@container rich-editor (min-inline-size: 36rem) {
  .rich-editor-group > .rich-editor-button:last-child {
    margin-inline-end: var(--space-2);
  }
}
.rich-editor-button {
  display: inline-grid;
  place-items: center;
  min-inline-size: 2.5rem;
  min-block-size: 2.5rem;
  margin: 0;
  padding: 0;
  color: var(--fg1);
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  font: inherit;
  line-height: 1;
  cursor: pointer;
}
@media (pointer: coarse) {
  .rich-editor-button {
    min-inline-size: var(--target-min);
    min-block-size: var(--target-min);
  }
}
.rich-editor-button .icon {
  inline-size: 1.25rem;
  block-size: 1.25rem;
}
.rich-editor-button:hover:where(:not(:disabled, [aria-disabled="true"])) {
  background: var(--surface-raised);
  border-color: var(--control-line);
}
.rich-editor-button:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}
/* On: the fill, the accent edge, and a second pixel of edge, so the state is a shape as well as a color. */
.rich-editor-button[aria-pressed="true"] {
  background: var(--accent-soft);
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
}
/* Undo and redo with nothing to undo or redo keep the focus (they say aria-disabled, not disabled) and look unavailable. */
.rich-editor-button:disabled,
.rich-editor-button[aria-disabled="true"] {
  color: var(--fg-faint);
  cursor: default;
}

/* ── The link form ───────────────────────────────────────────────────────── */

.rich-editor-link {
  display: grid;
  gap: var(--space-2);
  min-inline-size: 0;
  padding: var(--space-3);
  background: var(--surface-raised);
  border-block-end: 1px solid var(--rule);
}
.rich-editor-link-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

/* ── The surface ─────────────────────────────────────────────────────────── */

.rich-editor-surface {
  position: relative;
  min-inline-size: 0;
}
.rich-editor-content {
  position: relative;
  min-inline-size: 0;
  min-block-size: calc(var(--rich-editor-rows, 7) * 1.5em + var(--space-4));
  max-block-size: 70dvh;
  padding-block: var(--space-3);
  padding-inline: var(--space-3);
  overflow-y: auto;
  color: var(--fg1);
  font-family: var(--font-sans);
  font-size: 1rem;
  line-height: 1.5;
  /* ProseMirror needs these: `pre-wrap` so spaces are kept, no ligatures so the cursor can be placed inside a pair of letters. */
  word-wrap: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  white-space: break-spaces;
  font-variant-ligatures: none;
  font-feature-settings: "liga" 0;
  border-end-start-radius: calc(var(--radius-md) - 1px);
  border-end-end-radius: calc(var(--radius-md) - 1px);
  /* The ring is on the frame (above): a ring on this box would sit inside the frame's edge. */
  outline: none;
}
.rich-editor-content > * + * {
  margin-block-start: var(--space-3);
}
.rich-editor-content h2 {
  font-size: var(--text-lg);
  font-weight: 700;
  line-height: 1.3;
}
.rich-editor-content h3 {
  font-size: var(--text-md);
  font-weight: 700;
  line-height: 1.35;
}
.rich-editor-content ul,
.rich-editor-content ol {
  padding-inline-start: 1.5rem;
}
.rich-editor-content ul {
  list-style: disc;
}
.rich-editor-content ol {
  list-style: decimal;
}
.rich-editor-content li > p {
  margin: 0;
}
.rich-editor-content li + li {
  margin-block-start: var(--space-1);
}
.rich-editor-content blockquote {
  padding-inline-start: var(--space-3);
  border-inline-start: 3px solid var(--rule-strong);
  color: var(--fg2);
}
.rich-editor-content a {
  color: var(--accent-text);
  text-decoration: underline;
  text-underline-offset: 0.15em;
}
/* Each block reads in the direction of its own first strong letter, so a line in Arabic or Hebrew inside an English page is not set backwards. */
.rich-editor-content p,
.rich-editor-content h2,
.rich-editor-content h3,
.rich-editor-content li {
  unicode-bidi: plaintext;
  text-align: start;
}
/* A picture in the text sits on its own line, as it does where it is read, never wider than the column and never a wall of it. The list under the
   editor is where it is described. */
.rich-editor-content img {
  display: block;
  max-inline-size: 100%;
  max-block-size: 20rem;
  block-size: auto;
  border-radius: var(--radius-sm);
}
/* A picture that has no description yet is drawn with a dashed edge, so the person can tell which one the list is asking about; a described picture has none. */
.rich-editor-content img[data-needs-description] {
  outline: 2px dashed var(--control-line);
  outline-offset: 2px;
}
.rich-editor-content img.ProseMirror-selectednode {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}
.rich-editor-content p.is-editor-empty:first-child::before {
  content: attr(data-placeholder);
  float: inline-start;
  block-size: 0;
  color: var(--fg-faint);
  pointer-events: none;
}
/* ProseMirror's own: the separator it puts after an inline picture, the selection it hides while a picture is selected, the cursor between blocks. */
.rich-editor-content img.ProseMirror-separator {
  display: inline !important;
  inline-size: 0 !important;
  block-size: 0 !important;
  margin: 0 !important;
  border: none !important;
}
.rich-editor-content [contenteditable="false"] {
  white-space: normal;
}
.rich-editor-content .ProseMirror-hideselection *::selection {
  background: transparent;
}
.rich-editor-content.ProseMirror-hideselection * {
  caret-color: transparent;
}
.rich-editor-content .ProseMirror-gapcursor {
  display: none;
  position: absolute;
  margin: 0;
  pointer-events: none;
}
.rich-editor-content .ProseMirror-gapcursor::after {
  content: "";
  display: block;
  position: absolute;
  inset-block-start: -2px;
  inline-size: 20px;
  border-block-start: 1px solid var(--fg1);
}
.rich-editor-content.ProseMirror-focused .ProseMirror-gapcursor {
  display: block;
}
@media (prefers-reduced-motion: no-preference) {
  .rich-editor-content .ProseMirror-gapcursor::after {
    animation: rich-editor-cursor-blink 1.1s steps(2, start) infinite;
  }
}
@keyframes rich-editor-cursor-blink {
  to {
    visibility: hidden;
  }
}
/* Where a dragged picture or a dropped file will land. */
.rich-editor-dropcursor {
  background: var(--accent);
}

/* ── The count ───────────────────────────────────────────────────────────── */

/* Over the limit it says so in words (the count line reads "5 characters over the limit") and in the bad tone; the tone is not the signal. */
.field-rich-text .field-count[data-over] {
  color: var(--tone-bad-fg);
  font-weight: 650;
}

/* ── What was not added, and the pictures ────────────────────────────────── */

.rich-editor-notice {
  margin-block-start: var(--space-1);
}
.rich-editor-pictures {
  display: grid;
  gap: var(--space-2);
  min-inline-size: 0;
  margin-block-start: var(--space-2);
}
.rich-editor-pictures-title {
  margin: 0;
  color: var(--fg1);
  font-size: var(--text-sm);
  font-weight: 650;
  line-height: 1.4;
}
.rich-editor-pictures-list {
  display: grid;
  gap: var(--space-3);
  margin: 0;
  padding: 0;
  list-style: none;
}
.rich-editor-picture {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  gap: var(--space-3);
  min-inline-size: 0;
  padding: var(--space-3);
  background: var(--surface-raised);
  border: 1px solid var(--rule);
  border-radius: var(--radius-md);
}
.rich-editor-picture[data-invalid] {
  border-color: var(--tone-bad-fg);
  box-shadow: inset 0 0 0 1px var(--tone-bad-fg);
}
.rich-editor-picture-thumb {
  flex: none;
  inline-size: 5rem;
  block-size: 5rem;
  object-fit: cover;
  background: var(--surface-sunken);
  border: 1px solid var(--rule);
  border-radius: var(--radius-md);
}
.rich-editor-picture-body {
  display: grid;
  flex: 1 1 14rem;
  gap: var(--space-2);
  justify-items: start;
  min-inline-size: 0;
}
.rich-editor-picture-body > * {
  min-inline-size: 0;
  max-inline-size: 100%;
}
.rich-editor-picture-file {
  display: grid;
  gap: 0.1rem;
  margin: 0;
  font-size: var(--text-sm);
  line-height: 1.4;
  overflow-wrap: anywhere;
}
.rich-editor-picture-name {
  color: var(--fg1);
  font-weight: 650;
}
.rich-editor-picture-size {
  color: var(--fg2);
}
.rich-editor-picture-name:empty,
.rich-editor-picture-size:empty {
  display: none;
}
.rich-editor-picture-describe {
  display: grid;
  gap: 0.3rem;
  inline-size: 100%;
}
.rich-editor-picture-none {
  inline-size: 100%;
}

/* Forced colors drop our fills and shadows: a pressed button keeps a thicker edge, and a picture that is selected or needs words keeps a line. */
@media (forced-colors: active) {
  .rich-editor-button[aria-pressed="true"] {
    border-width: 2px;
  }
  /* A button that says aria-disabled (undo and redo with nothing to do) is not the native attribute, so the system does not grey it: it does. */
  .rich-editor-button[aria-disabled="true"] {
    color: GrayText;
  }
  .field-rich-text[data-rich-text-state="ready"][data-invalid] .rich-editor,
  .rich-editor-picture[data-invalid] {
    border-width: 2px;
  }
  .rich-editor-content img.ProseMirror-selectednode {
    outline-color: Highlight;
  }
  .rich-editor-dropcursor {
    background: Highlight;
  }
}
