@layer components {
  /* Write's column (write/index): white at the sides of the text, --write-margin, where a selection can start, too
     (the text's editable part reaches into it, below). The column's own padding, so it is there at any width of the
     window and the columns beside it; more would need the text narrower or the white measured. No padding at the top
     or bottom: the editor's toolbar stays at the top. */
  .write-column {
    --write-margin: var(--space-4);

    padding-inline: var(--write-margin);
  }

  /* Write: the whole thesis in one editor, in reading width, like a document */
  .write-document {
    /* The editor's toolbar as high as the column headers next to it (36px toggles, 8px above and below, 1px line), so
       their lines meet */
    --write-toolbar-height: calc(2.25rem + 2 * var(--space-2) + 1px);

    /* The menu of a reference is placed in it (citation-menu controller) */
    position: relative;
    max-inline-size: 48rem;
    margin-inline: auto;
  }

  /* The way to the version history (write/index), a ghost button: right below the toolbar at the right, in the room
     the toolbar leaves for it (see lexxy-toolbar below), above the first heading and its switch. It scrolls away with
     the text, under the toolbar. */
  .write-history-link {
    position: absolute;
    /* A little further from the toolbar than from the heading below */
    inset-block-start: calc(var(--write-toolbar-height) + var(--space-1));
    inset-inline-end: 0;
    z-index: var(--z-write-overlay);
  }

  /* The version history (projects/versions/_history): a popup as high as the window lets it be, the versions on the
     left, the one chosen on the right; each scrolls on its own (overflow-y) in a row as high as the popup */
  .lightbox__panel:has(.version-history) {
    block-size: calc(100dvh - var(--space-4) * 2); /* the panel's max-height (lightbox.css), also for a short text */
  }

  .version-history {
    display: grid;
    grid-template-columns: 12rem minmax(0, 1fr); /* the list as wide as "about 10 hours ago" */
    grid-template-rows: minmax(0, 1fr);

    /* Narrow: the list above the text, at most about three versions high */
    @media (max-width: 48rem) {
      grid-template-columns: minmax(0, 1fr);
      grid-template-rows: fit-content(8rem) minmax(0, 1fr);
    }
  }

  /* A citation's source in the panel from the right (statements/sources/show): its title on one line, then who and
     when, then the PDF */
  .source-panel {
    display: flex;
    flex-direction: column;
    min-inline-size: 0;
  }

  .source-panel__header {
    align-items: baseline;

    /* A long title is cut off, not widening the panel */
    strong {
      min-inline-size: 0;
    }
  }

  .source-panel__short {
    flex-shrink: 0;
  }

  .source-panel__passage {
    padding: var(--space-3);
    border-inline-start: 3px solid var(--color-warning); /* a quote's bar, thicker than any border width */
    border-radius: var(--radius-md);
    background-color: var(--color-surface);
  }

  /* The header, then the PDF; right of it in the whole height the citation at its place in the text, or the form for
     a new one while that is open (statements/sources/show), as on the citations page, scrolling on its own. In a
     window too narrow for text, PDF and form side by side (lightbox.css), where the panel takes the whole width, below
     the PDF instead. */
  .source-panel__content {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-block-size: 0;

    @media (min-width: 80rem) {
      display: grid;
      grid-template: auto minmax(0, 1fr) / minmax(0, 1fr) auto;

      .source-panel__header {
        grid-column: 1 / -1;
      }
    }
  }

  .source-panel__form {
    flex: none;
    max-block-size: 50%;
    overflow-y: auto;
    padding: var(--space-3);
    border-block-start: var(--border);

    /* One at a time: the new citation's form in place of the citation's while open */
    &:has(#new_citation:not(.hidden)) #citation-passage {
      display: none;
    }

    @media (min-width: 80rem) {
      inline-size: 24rem; /* no step of the spacing scale is a column; wider, the PDF beside it can't be read */
      max-block-size: none;
      border-block-start: 0;
      border-inline-start: var(--border);
    }
  }

  /* The PDF takes the rest of the panel's height */
  .source-panel__pdf {
    display: flex;
    flex-direction: column;
    flex: 1;
    min-block-size: 0;

    .pdf-viewer {
      flex: 1;
      block-size: auto;
    }
  }

  /* The menu of the citation the cursor is in, placed below its reference by the citation-menu controller: its groups
     of entries (write/index) side by side, each a bar like the "Ask AI" button (.text-rewrite-button), with a blue
     border, its entries each an icon and a word. In a narrow text (a small window, the source panel open) a group goes
     on to a second line instead of being cut off; a group wider than the text, too, its entries below each other. */
  .citation-menu {
    position: absolute;
    /* Under the toolbar, as all that scrolls with the text (see lexxy-toolbar below) */
    z-index: var(--z-write-overlay);
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-1);
    /* As wide as its entries on one line, not only as the room right of its reference: the controller moves it left
       to stay inside the text, so it wraps only in a text narrower than it */
    inline-size: max-content;
    max-width: 100%;

    &[hidden] {
      display: none;
    }

    /* While the panel of Ask AI is open (also for "Check against source"), it has the menu's place: the menu waits
       hidden, placed at the citation the cursor is in, and shows again as soon as the panel closes. Hidden, not gone,
       so the controller can measure its width to keep it inside the text. */
    .write-document:has(> .text-rewrite:not([hidden])) & {
      visibility: hidden;
    }
  }

  /* The lines between its entries are its own color in the room between them: so they stand right side by side and
     below each other, where its entries wrap. Each entry fills its line (below), so the color shows nowhere else. */
  .citation-menu__group {
    display: flex;
    flex-wrap: wrap;
    gap: var(--border-width-1);
    max-width: 100%;
    overflow: hidden;
    border: var(--border-width-1) solid var(--color-blue-accent);
    border-radius: var(--radius-md);
    background-color: var(--color-border);
    box-shadow: var(--shadow-sm);

    /* No entry to show (the one for orange without orange in the wording) */
    &:not(:has(> :not([hidden]))) {
      display: none;
    }
  }

  /* As wide as its line in its group: wrapped, the blue of "Check against source" reaches the border, too. On one line
     the group is as wide as its entries, so they keep their width. */
  .citation-menu__item {
    display: inline-flex;
    flex-grow: 1;
    align-items: center;
    justify-content: center;
    gap: var(--space-1);
    padding: var(--space-1) var(--space-2);
    border: 0;
    background-color: var(--color-surface-raised);
    color: var(--color-blue-accent);
    font: inherit;
    font-size: var(--text-small);
    font-weight: var(--font-weight-medium);
    white-space: nowrap;
    cursor: pointer;

    &:hover {
      background-color: var(--color-surface-soft);
    }

    /* An entry the citation hasn't (citation-menu controller) */
    &[hidden] {
      display: none;
    }
  }

  .citation-menu__item--danger {
    color: var(--color-negative);
  }

  /* "Check against source", filled as .btn-primary: what to do about orange, set off from the entries one knows */
  .citation-menu__item--primary {
    background-color: var(--color-blue-accent);
    color: var(--color-ink-inverted);

    &:hover {
      /* As .btn-primary's hover (buttons.css) */
      background-color: color-mix(in srgb, var(--color-blue-accent) 85%, white);
    }
  }

  /* Saving failed (save controller): right below the toolbar, staying there as the text scrolls; how far down the
     save controller sets, from the toolbar's height */
  .write-notice {
    position: sticky;
    /* As high as the toolbar (see lexxy-toolbar below) */
    z-index: var(--z-write-toolbar);
    padding: var(--space-2) var(--space-3);
    border-radius: var(--radius-md);
    background-color: var(--yellow-highlight);
    border: var(--border-width-1) solid var(--color-warning);

    &[hidden] {
      display: none;
    }
  }
}

/* While a citation's source is open in the panel from the right, the outline and the citations make room for it and
   the text moves aside, so text and source stand next to each other. Outside the layers: the columns are laid out with
   the utility flex, whose layer comes last. */
body:has(#citation-source-panel:popover-open) {
  .workspace-column,
  .column-resize-handle {
    display: none;
  }

  .workspace-main {
    margin-inline-end: var(--drawer-width);
  }
}

/* Lexxy's styles are not in a layer, so they beat every layer: these overrides stay outside, too */

/* The editor blends into the page: no frame */
.write-document lexxy-editor {
  --lexxy-editor-padding: 0;
  --lexxy-editor-rows: 2lh;

  border: none;
  border-radius: 0;
  background-color: transparent;
}

/* The editable part reaches into the white beside the text, as far as the column's padding (.write-column), so a
   selection can start there, as in a word processor: the browser selects from the start or the end of the line. A
   selection that starts outside the editable part never gets into it. The text stays where it is, as wide as the
   form; the controllers that place something at its edges measure the text, not this box (plan-toggle, citation-drop).
   Next to a chapter's plan, the plan's own area takes it (below). */
.write-document .lexxy-editor__content {
  margin-inline: calc(-1 * var(--write-margin));
  padding-inline: var(--write-margin);
}

/* A citation Write put into the text as it loaded, not worked in yet (new-citations controller): a bar beside its block
   in the white next to the text, and "New" beside the bar, turned to read upwards from the block's top. In a plan,
   the card's own left edge is the bar, and "New" stands in the plan's room beside it. In the colour of the text, so it
   reads as a mark of the text, not of the app. Drawn, not part of the text: the attribute is on Lexical's element. */
.write-document .lexxy-editor__content [data-new-citation] {
  position: relative;

  &::before,
  &::after {
    position: absolute;
    pointer-events: none;
  }

  /* In a paragraph only beside the citation's lines, not the rest of it (new-citations controller, #span) */
  &::before {
    content: "";
    inset-block: var(--new-citation-top, 0) var(--new-citation-bottom, 0);
    inset-inline-start: -7.5px; /* the bar's middle 6px left of the text, so "New" has room beside it in the white */
    inline-size: 3px;
    border-radius: var(--radius-pill);
    background-color: var(--color-ink);
  }

  /* Placed with physical sides: turned by writing-mode, its own inline and block sides are turned, too */
  &::after {
    content: "New";
    top: var(--new-citation-top, 0);
    right: calc(100% + 7.5px + var(--space-1)); /* left of the bar */
    writing-mode: vertical-rl;
    transform: rotate(180deg);
    color: var(--color-ink);
    font-size: 0.625rem; /* 10px, below the scale: it has to fit in the white beside the text */
    font-weight: var(--font-weight-semibold);
    line-height: 1;
    letter-spacing: 0.04em;
    text-transform: uppercase;
  }

  /* In the language of the interface (html lang, layouts/application), not :lang(): inside the editor that is the
     thesis's */
  html[lang="de"] &::after {
    content: "Neu";
  }

  &.plan-card {
    box-shadow: inset 3px 0 0 var(--color-ink);

    &:has(action-text-attachment[content-type="application/vnd.actiontext.citation"]) {
      box-shadow: inset 3px 0 0 var(--color-ink), var(--shadow-sm);
    }

    &::before {
      content: none;
    }

    &::after {
      right: calc(100% + var(--space-1));
    }
  }
}

/* The headings are the outline of the thesis, numbered 1, 1.1, 1.1.1, 1.1.1.1 like in the outline column (outline
   controller, which counts the same way). Only those at the top level of the text, not in a table. The numbers aren't
   part of the text: they are drawn, not saved. The same in a version of the text (version history). */
:is(.write-document .lexxy-editor__content, .version-history .lexxy-content) {
  counter-reset: heading-1 heading-2 heading-3 heading-4;

  > :is(h1, h2, h3, h4) {
    /* A jump from the outline puts the heading below the toolbar that stays at the top, Lexxy's room between two
       blocks below it, so the block before ends right at the toolbar */
    scroll-margin-block-start: calc(var(--write-toolbar-height) + var(--lexxy-content-margin));
  }

  > h1 {
    counter-increment: heading-1;
    counter-set: heading-2 0 heading-3 0 heading-4 0;
  }

  > h2 {
    counter-increment: heading-2;
    counter-set: heading-3 0 heading-4 0;
  }

  > h3 {
    counter-increment: heading-3;
    counter-set: heading-4 0;
  }

  > h4 {
    counter-increment: heading-4;
  }

  > h1::before { content: counter(heading-1) "\2002"; }
  > h2::before { content: counter(heading-1) "." counter(heading-2) "\2002"; }
  > h3::before { content: counter(heading-1) "." counter(heading-2) "." counter(heading-3) "\2002"; }
  > h4::before { content: counter(heading-1) "." counter(heading-2) "." counter(heading-3) "." counter(heading-4) "\2002"; }

  /* Room on the right for the switch between plan and text (plan-toggle controller), which stands next to the editor
     at the end of the heading's first line */
  > :is(h1, h2, h3, h4) {
    padding-inline-end: 5rem;
  }
}

/* A chapter's plan, in the text in place of the chapter's text (chapter_plan.js), as in the Luftschloss: a dashed blue
   frame, an intro, the cards, a footer with the buttons. Its own text on grey cards, each citation on a white one: a
   card is white when a reference to a source is in it. Which texts show, with cards or without, depends on whether
   there are cards. Outside the layers: it stands in Lexxy's editor, whose styles (paragraphs, buttons) are in none. */
.write-document .chapter-plan {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  margin-block: 0 var(--space-4);
  padding: var(--space-3);
  border: 1.5px dashed var(--color-blue-accent); /* from the Luftschloss: between --border-width-1 and -2 */
  border-radius: var(--radius-lg);
  background-color: var(--color-surface);
  font-size: var(--text-small);
  line-height: 1.45;

  p {
    margin: 0;
  }

  &:has(.plan-card) .chapter-plan__without-cards,
  &:not(:has(.plan-card)) :is(.chapter-plan__with-cards, .chapter-plan__cards) {
    display: none;
  }
}

/* The margin next to a plan is the plan's: an area of it, unseen, behind it, reaching as far as the text's editable
   part (above). A click there is one in the plan, which puts the cursor into a card; in the editable part next to the
   plan, the browser found no place in it and put the cursor at the start of the heading. Behind the plan in a stacking
   context of its own, so its cards stay on top whatever Lexxy sets on the editor. */
.write-document .chapter-plan {
  position: relative;
  isolation: isolate;

  &::before {
    content: "";
    position: absolute;
    inset-block: 0;
    inset-inline: calc(-1.5px - var(--write-margin));
    z-index: -1;
  }
}

.write-document .chapter-plan__intro,
.write-document .chapter-plan__none {
  color: var(--color-muted);
  user-select: none;
}

/* Inline, so "Plan." stands on the same line as the sentence after it (as inline-flex, the icon's lower edge set
   the line, and the sentence sat lower) */
.write-document .chapter-plan__intro b {
  margin-inline-end: 2px; /* a hair more than the space after it, below the spacing scale */
  color: var(--color-ink);
  font-weight: var(--font-weight-semibold);

  .icon {
    margin-inline-end: var(--space-2);
  }
}

.write-document .chapter-plan__cards {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
}

.write-document .chapter-plan__foot {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
  padding-block-start: var(--space-1);
  user-select: none;
}

.write-document .chapter-plan__spacer {
  flex: 1;
}

/* The app's secondary button; Lexxy resets buttons in its editor */
.write-document .chapter-plan__button {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border: var(--border);
  border-radius: var(--radius-md);
  background-color: var(--color-surface-raised);
  color: var(--color-ink);
  font: inherit;
  font-size: var(--text-small);
  line-height: 1.3;
  white-space: nowrap;
  cursor: pointer;

  &:hover {
    background-color: var(--color-surface-soft);
  }
}

/* "Draft with AI": only with cards. While the AI drafts (chapter-draft controller), its button says so and nothing
   else in the plan can be used. What went wrong shows above the footer. */
.write-document .chapter-plan {
  &:not(:has(.plan-card)) [data-plan-action="draft"],
  &:not(.chapter-plan--drafting) .chapter-plan__draft-busy,
  &.chapter-plan--drafting .chapter-plan__draft-idle,
  &.chapter-plan--drafting [data-plan-action="draft"] > .icon {
    display: none;
  }

  /* While it drafts, the spinner in place of its icon (animations.css) */
  .chapter-plan__draft-busy {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
  }

  &.chapter-plan--drafting {
    cursor: progress;

    :is(.chapter-plan__cards, .chapter-plan__button) {
      pointer-events: none;
    }

    :is(.chapter-plan__cards, .chapter-plan__button:not([data-plan-action="draft"])) {
      opacity: 0.5;
    }
  }
}

.write-document .chapter-plan__message {
  &:not([data-message]) {
    display: none;
  }

  &::before {
    content: attr(data-message);
  }

  padding: var(--space-2) var(--space-3);
  border: var(--border-width-1) solid var(--color-negative);
  border-radius: var(--radius-md);
  background-color: var(--color-negative-soft);
  color: var(--color-negative);
  user-select: none;
}

/* A card: grip, content, remove button. Grey: own text; white with a frame and a shadow: a citation. Dragged by all
   but its text (chapter_plan.js): the hand over the grip, the frame and beside a short text, and its frame blue, faded
   in as an input's frame on focus (inputs.css). */
.write-document .plan-card {
  cursor: grab;
  transition: border-color 0.2s ease;
  display: grid;
  grid-template-columns: 26px minmax(0, 1fr) auto;
  align-items: start;
  gap: var(--space-1);
  padding: var(--space-2) var(--space-2) var(--space-2) var(--space-1);
  border: var(--border-width-1) solid transparent;
  border-radius: var(--radius-md);
  background-color: var(--color-surface-soft);
  color: var(--color-ink);
  font-size: var(--text-normal);
  line-height: 1.5;

  &:has(action-text-attachment[content-type="application/vnd.actiontext.citation"]) {
    border-color: var(--color-border);
    background-color: var(--color-surface-raised);
    box-shadow: var(--shadow-sm);
  }

  /* Its frame, not its shadow: the bar of a new citation is one (data-new-citation) */
  &:hover:not(:has(.plan-card__content:hover, .plan-card__remove:hover)) {
    border-color: var(--color-blue-accent);
  }
}

.write-document .plan-card--dragging {
  opacity: 0.4;
}

/* The whole height of the card, its icon at the first line */
.write-document .plan-card__grip {
  display: grid;
  place-items: start center;
  align-self: stretch;
  padding-block-start: 2px; /* nudged down to the first line of the text beside it */
  color: var(--color-muted);
  cursor: grab;
  user-select: none;
}

/* The text, as wide as its longest line: beside a short one the card is grabbed. At least a bit wide, to click into
   an empty card. */
.write-document .plan-card__content {
  justify-self: start;
  min-inline-size: 8rem;
  cursor: text;
}

/* Several paragraphs in a card (Enter): a little room between them */
.write-document .plan-card__content > * {
  margin: 0;

  & + * {
    margin-block-start: var(--space-2);
  }
}

.write-document .plan-card__remove {
  display: grid;
  place-items: center;
  inline-size: 28px;
  block-size: 28px;
  padding: 0;
  border: none;
  border-radius: var(--radius-sm);
  background: none;
  color: var(--color-muted);
  cursor: pointer;

  &:hover {
    background-color: var(--color-surface-soft);
    color: var(--color-ink);
  }
}

@layer components {
  /* The switches at the headings (plan-toggle controller): placed in this layer over the text, which takes
     the clicks everywhere else */
  .plan-toggles {
    position: absolute;
    inset: 0;
    pointer-events: none;
  }

  /* Plan or text: two icons on grey, the current one raised in white */
  .plan-toggle {
    position: absolute;
    display: inline-flex;
    gap: 2px; /* a thin rim around the buttons, as in .segmented-control */
    padding: 3px;
    border-radius: var(--radius-md);
    background-color: var(--color-surface-soft);
    pointer-events: auto;
    transform: translateY(-50%);
  }

  .plan-toggle__button {
    display: grid;
    place-items: center;
    inline-size: 30px;
    block-size: 26px;
    padding: 0;
    border: none;
    border-radius: calc(var(--radius-md) - 3px); /* inside the toggle's 3px rim */
    background: none;
    color: var(--color-muted);
    cursor: pointer;

    &:hover {
      color: var(--color-ink);
    }

    &[aria-pressed="true"] {
      background-color: var(--color-surface-raised);
      color: var(--color-blue-accent);
      box-shadow: var(--shadow-sm);
    }
  }
}

@layer components {
  /* "Ask AI" (text-rewrite controller): the small button below selected text, and the panel that opens in its place.
     Placed in the form by the controller, over the text, like the menu of a reference. Plain for now. */
  .text-rewrite-button {
    position: absolute;
    z-index: var(--z-write-overlay);
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    padding: var(--space-1) var(--space-2);
    border: var(--border-width-1) solid var(--color-blue-accent);
    border-radius: var(--radius-md);
    background-color: var(--color-surface-raised);
    color: var(--color-blue-accent);
    box-shadow: var(--shadow-sm);
    font: inherit;
    /* As large as the menu of a citation (.citation-menu__item), which looks the same: both are small and stand in the
       text */
    font-size: var(--text-small);
    font-weight: var(--font-weight-medium);
    cursor: pointer;

    &:hover {
      background-color: var(--color-surface-soft);
    }

    &[hidden] {
      display: none;
    }
  }

  /* As wide as the text; framed in blue like its button, so it reads as the place to ask the AI. At most 60% of the
     window high: a long text of the AI's scrolls within its box (below), Replace and the rest stay in view. */
  .text-rewrite {
    position: absolute;
    z-index: var(--z-write-overlay);
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    inline-size: 100%;
    max-block-size: 60vh;
    overflow-y: auto;
    padding: var(--space-2);
    border: var(--border-width-1) solid var(--color-blue-accent);
    border-radius: var(--radius-md);
    background-color: var(--color-surface-raised);
    box-shadow: var(--shadow-lg);
    font-size: var(--text-small);

    &[hidden] {
      display: none;
    }

    p {
      margin: 0;
    }
  }

  .text-rewrite__ask {
    display: flex;
    align-items: center;
    gap: var(--space-2);
    padding-inline-start: var(--space-1);
    color: var(--color-blue-accent);
  }

  .text-rewrite__input {
    flex: 1;
    min-inline-size: 0;
    padding: var(--space-1) 0;
    border: none;
    background: none;
    color: var(--color-ink);
    font: inherit;
    font-size: var(--text-normal);
    outline: none;
  }

  .text-rewrite__presets,
  .text-rewrite__actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--space-2);
  }

  /* A quick action under the pointer framed in blue, like the panel */
  .text-rewrite__presets .btn:hover {
    border-color: var(--color-blue-accent);
  }

  .text-rewrite__hint,
  .text-rewrite__unchanged {
    color: var(--color-muted);
  }

  /* In the margin left of the text, next to the selection, while the AI's text is shown (text-rewrite controller); blue
     like the selection and the panel. The same next to a passage "Show" marks (passages controller) and next to the
     citation the cursor is in, whose menu shows (citation-menu controller). */
  .text-rewrite-bar {
    position: absolute;
    inset-inline-start: calc(var(--write-margin) / -2); /* halfway into the white beside the text */
    inline-size: 3px;
    border-radius: var(--radius-pill);
    background-color: var(--color-blue-accent);
    pointer-events: none;

    &[hidden] {
      display: none;
    }
  }

  .text-rewrite__writing {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-2);
    color: var(--color-muted);
  }

  /* The AI's text, on the panel's white, set off from the old text above, which is light blue (the words it leaves out
     struck through there, below), by the panel's blue frame and its label. No background of its own: blue would be
     the old text's, grey hides the references' pills and means one's own text in a plan. */
  .text-rewrite__suggestion {
    display: flex;
    flex-direction: column;
    gap: var(--space-1);
    min-block-size: 0;
  }

  .text-rewrite__label {
    color: var(--color-blue-accent);
    font-size: var(--text-x-small);
    font-weight: var(--font-weight-medium);
  }

  /* As it would stand in the text: citations green with their reference as a pill */
  .text-rewrite__result {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    min-block-size: 0;
    overflow-y: auto;
    color: var(--color-ink);
    font-size: var(--text-normal);
    line-height: 1.5;
  }

  /* List items in their list, as in the text, a nested list indented (--indent, text-rewrite controller) */
  .text-rewrite__result :is(ul, ol) {
    margin: 0;
    padding-inline-start: calc((var(--indent, 0) + 1) * 1.5em);
  }

  .text-rewrite__block {
    color: var(--color-muted);
    font-weight: var(--font-weight-medium);
  }

  .text-rewrite__wording {
    background-color: var(--green-highlight);
    box-shadow: inset 0 -1px 0 var(--color-positive);
    color: inherit;
  }

  .text-rewrite__reference {
    padding: 0.1em 0.5em; /* in em, as the reference in the text (below) */
    border: var(--border);
    border-radius: 0.5em;
    background-color: var(--color-surface-soft);
    font-size: 0.95em;
  }

  /* A word the AI changed or added (text_rewrite.js, differences); the same in a correction of the Language tab
     (review.css, .review-quote) */
  .text-rewrite__changed {
    text-decoration: underline 2px var(--color-blue-accent);
    text-underline-offset: 3px;
  }

  .text-rewrite__discard {
    margin-inline-start: auto;
  }

  .text-rewrite__message {
    padding: var(--space-2) var(--space-3);
    border: var(--border-width-1) solid var(--color-negative);
    border-radius: var(--radius-md);
    background-color: var(--color-negative-soft);
    color: var(--color-negative);
  }

  /* What shows in each state of the panel (data-state, text-rewrite controller): asking (the line and the quick
     actions, which stay while something is typed, so the line doesn't jump), writing, the AI's text, nothing changed
     or a message, each with the line again; a selection too long, only that message */
  .text-rewrite:not([data-state="ask"], [data-state="unchanged"], [data-state="error"]) :is(.text-rewrite__ask, .text-rewrite__presets, .text-rewrite__hint),
  .text-rewrite:not([data-state="writing"]) .text-rewrite__writing,
  .text-rewrite:not([data-state="result"]) :is(.text-rewrite__suggestion, .text-rewrite__actions),
  .text-rewrite:not([data-state="unchanged"]) .text-rewrite__unchanged,
  .text-rewrite:not([data-state="error"], [data-state="too-long"]) .text-rewrite__message,
  /* "Check against source" (data-mode="check"): nothing to ask, nothing to try again; what is wrong only there */
  .text-rewrite[data-mode="check"] :is(.text-rewrite__ask, .text-rewrite__presets, .text-rewrite__hint, .text-rewrite__again),
  .text-rewrite:not([data-mode="check"]) .text-rewrite__note {
    display: none;
  }
}

/* The selection "Ask AI" works on, while its panel is open: in the color of a selection, but not part of the text.
   While the AI's text is shown, the words it leaves out struck through in it, where they stand (text-rewrite
   controller), as the panel underlines the new ones; the same in a correction of the Language tab (review.css,
   .review-quote). The same color for the green of the citation the cursor is in, whose menu shows (citation-menu
   controller; its orange stays orange), and for a passage "Show" marks (review.css). */
::highlight(text-rewrite),
::highlight(citation-active) {
  background-color: var(--color-selected);
}

::highlight(text-rewrite-removed) {
  color: var(--color-negative);
  text-decoration: line-through 1.5px;
}

/* Lexxy's toolbar stays at the very top while the text scrolls below it (the main column has no padding at the top on
   Write, write/index). Its background and the line below it go across the whole main column, not only the width of
   the text: painted beyond its box (border-image outset), which takes no room, adds no scrollbar and is cut off at
   the column's edges. Instead of Lexxy's own line.
   Layers on Write: what stands over the text and scrolls with it (Ask AI's button and panel, the menu of a reference)
   at --z-write-overlay, the toolbar and the notice below it at --z-write-toolbar. So these go under the toolbar when
   the text scrolls up, and the toolbar's dropdowns, inside it, open over them. The switches at the headings and the
   bar beside Ask AI's selection have none and stay below anyway. */
.write-document lexxy-toolbar {
  /* Its buttons like the app's icon buttons (.btn-action), as the column toggles beside it: 36px, the same corners.
     Through Lexxy's own variables, set only here: Lexxy rounds things in the text with its radius, too. */
  --lexxy-toolbar-button-size: 2.25rem;
  --lexxy-radius: var(--radius-md);

  position: sticky;
  inset-block-start: 0;
  z-index: var(--z-write-toolbar);
  block-size: var(--write-toolbar-height);
  align-items: center;
  /* Room to the text for the button to the version history (.write-history-link: 4px down, a .btn about 36px high),
     a little more below it */
  margin-block-end: calc(2.25rem + var(--space-2));
  border: none;
  border-image: linear-gradient(to top, var(--color-border) 1px, var(--color-surface-raised) 1px) fill 0 / 0 / 0 100vmax;
}

/* Citations in the text (citation_marks.js): the source's wording on green, what isn't the source's words as cited on
   yellow (stored as Lexxy's "orange", --highlight-bg-2), each with a thin line below. Lexxy's highlight colors are
   variables: here they take the app's, so what is saved stays as it is. */
.write-document {
  --highlight-bg-7: var(--green-highlight);
  --highlight-bg-2: var(--yellow-highlight);

  mark {
    /* Across a line break, each line gets its background and line */
    box-decoration-break: clone;
    -webkit-box-decoration-break: clone;
  }

  mark[style*="--highlight-bg-7"] {
    box-shadow: inset 0 -1px 0 var(--color-positive);
  }

  mark[style*="--highlight-bg-2"] {
    box-shadow: inset 0 -1px 0 var(--color-warning);
  }
}

/* The reference to a source in the text (statements/_attachment): a pill set off from the words around it, since it
   is one piece that can't be typed in, only selected or deleted as a whole. About as big as the text, with room
   around the words, so it is easy to tap on a tablet. Lexxy draws custom attachments with these variables; selected,
   ours is framed in blue, not filled (below). */
.write-document action-text-attachment[content-type="application/vnd.actiontext.citation"] {
  padding: 0.1em 0.5em; /* in em, as its radius and size: it grows with the text */
  border: var(--border);
  border-radius: 0.5em;
  font-size: 0.95em;
  /* As high as a line of text with the padding and border, so lines with a pill keep their spacing */
  line-height: 1.3;
  cursor: pointer;

  &:not(.node--selected) {
    --lexxy-attachment-bg-color: var(--color-surface-soft);
  }

  /* Selected itself, the same: Lexxy fills a selected custom attachment in its accent color, white text on blue, which
     was too much next to the filled "Check against source" of its menu (Niklas); the text in Lexxy's color for an
     attachment (lexxy-content.css) */
  &.node--selected {
    --lexxy-attachment-bg-color: var(--color-surface-soft);
    --lexxy-attachment-text-color: currentColor;
  }

  /* Under the pointer, selected itself, or while its citation's menu shows (citation-menu controller): framed in blue */
  &:is(:hover, .node--selected, [data-citation-active]) {
    border-color: var(--color-link);
  }

  /* While its citation's menu shows, light blue, as its wording (::highlight(citation-active) below) */
  &[data-citation-active] {
    --lexxy-attachment-bg-color: var(--color-selected);
  }
}

/* A selected reference has its own menu (citation-menu controller), not Lexxy's toolbar for attachments with its remove
   button: that would take only the reference, ours takes the whole citation */
.write-document:has(action-text-attachment[content-type="application/vnd.actiontext.citation"].node--selected) lexxy-attachment-toolbar {
  display: none;
}

@layer components {
  /* The citations in the right column, "No chapter" or by chapter (write/_open_citations); the cards are dragged into
     the text. A card: the grip, then its wording and in-text citation, like a plan's cards (.plan-card): less room on
     the left, so the grip takes little of the text's width. The whole card is dragged, the grip only says so. */
  .write-citation {
    display: grid;
    grid-template-columns: 26px minmax(0, 1fr);
    align-items: start;
    gap: var(--space-1);
    margin: 0;
    padding-inline-start: var(--space-1);
    cursor: grab;
  }

  .write-citation__grip {
    display: grid;
    place-items: center;
    padding-block-start: 2px; /* nudged down to the first line of the text beside it */
    color: var(--color-muted);
  }

  .write-citation__body {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    min-inline-size: 0;

    > * {
      margin: 0;
    }
  }

  /* The switches of the Language and Review tabs (write/_language, write/_review) */
  .write-citations__switch {
    gap: var(--space-2);
    align-self: flex-start;
  }

  /* A chapter folds: its heading with an arrow (right while folded, down while open) and how many it lists, then its
     cards.
     Laid out here, not with utilities like flex and gap-2: those come in a later layer and would show it again when it
     is to go (below). */
  /* Arrow and count at the heading's first line, also when a long one wraps in a narrow column. Grey under the pointer
     or the keyboard's focus, like the app's icon buttons (.btn-action), so it reads as something to click. */
  .write-citations__summary {
    display: flex;
    align-items: flex-start;
    gap: var(--space-1);
    padding: 0.375rem var(--space-2); /* 6px, between --space-1 and -2: either would move every row of the list */
    border-radius: var(--radius-md);
    cursor: pointer;
    user-select: none;
    transition: background-color 0.2s ease;

    &:where(:hover, :focus-visible) {
      background-color: var(--color-surface-soft);
    }
  }

  .write-citations__summary > .fold-chevron {
    margin-block-start: 1px; /* nudged down to the first line of the heading */
  }

  .write-citations__count {
    margin-inline-start: auto;
  }

  .write-citations__cards {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    margin-block-start: var(--space-2);
  }

  /* Only "No chapter" (the group without a chapter's id), without what is in the text (write/_open_citations), and not
     at all with nothing else; then the hint shows. It counts its open citations. */
  .write-citations {
    .write-citations__chapter[data-chapter-id],
    .write-citation[data-in-text],
    .write-citations__chapter:not(:has(.write-citation:not([data-in-text]))) {
      display: none;
    }
  }

  .write-citations:has(.write-citations__chapter:not([data-chapter-id]) .write-citation:not([data-in-text])) > .write-citations__empty {
    display: none;
  }

  /* Where a dragged citation would go: a strong line between two blocks of the text, with a dot at each end, easy to
     see under a finger on a tablet, too (citation-drop controller). It doesn't push the text aside. */
  .citation-drop-line {
    --dot: 10px;

    position: fixed;
    z-index: var(--z-1);
    block-size: 4px;
    margin-block-start: -2px; /* half its height: centered on the line between two blocks */
    border-radius: var(--radius-pill);
    background-color: var(--color-blue-accent);
    pointer-events: none;

    &::before,
    &::after {
      content: "";
      position: absolute;
      inset-block-start: 50%;
      inline-size: var(--dot);
      block-size: var(--dot);
      border-radius: 50%;
      background-color: var(--color-blue-accent);
      transform: translateY(-50%);
    }

    &::before {
      inset-inline-start: calc(var(--dot) / -2);
    }

    &::after {
      inset-inline-end: calc(var(--dot) / -2);
    }
  }

  /* The references below the text, in its width; the room below them, as the main column has no padding there on
     Write (write/index) */
  .write-references {
    max-inline-size: 48rem;
    margin-block-start: calc(2 * var(--space-4));
    margin-inline: auto;
    padding-block-end: var(--space-4);
  }

  /* An entry of the outline on Write (outline controller): a button, as the headings it leads to have no ids to link
     to; indented by the level of its heading */
  .outline-link {
    position: relative;
    inline-size: 100%;
    padding-block: var(--space-1);
    padding-inline: calc(var(--outline-depth, 0) * var(--space-2)) 0;
    border: none;
    background: none;
    color: var(--color-muted);
    text-align: start;
    transition: color 0.2s ease;
  }

  .outline-link:where(:hover, :focus-visible) {
    color: var(--color-link);
  }

  /* A chapter with a new citation not worked in yet (outline controller, new-citations controller): a dot after its
     title, in the colour of the bar beside the citation */
  .outline-link--new::after {
    content: "";
    flex: none;
    inline-size: 6px; /* a dot, below the spacing scale */
    block-size: 6px;
    margin-block-start: 0.5lh;
    translate: 0 -50%;
    border-radius: 50%;
    background-color: var(--color-ink);
  }

  /* The part of the text being read (scroll spy in the outline controller). Drawn a bit bolder instead of set in a
     bolder weight, which is wider and would rewrap long titles while scrolling. A mark at the column's left edge, next
     to the sidebar's line. */
  .outline-link[aria-current="location"] {
    color: var(--color-link);
    -webkit-text-stroke: 0.2px currentColor;

    &::before {
      content: "";
      position: absolute;
      inset-block: var(--space-1);
      inset-inline-start: calc(-1 * var(--space-2));
      inline-size: var(--border-width-2);
      background-color: var(--color-link);
    }
  }
}
