/* Trix ships one fixed light look: its toolbar icons are black SVGs drawn as
   background images, and every border, fill and dialog colour is a literal.
   On a dark page the icons sat near-black on near-black and the link dialog
   opened as a white slab with white text in its field.

   This is the Trix half of what lexxy-theme.css does for Lexxy. Trix has no
   palette of custom properties to re-point, so the rules below restate its
   colours against our own tokens instead. Every rule is scoped to `.dark`, so
   light mode keeps the stock stylesheet untouched, and because `.dark` and
   `[data-theme]` both live on <html> the tokens resolve against whichever
   festival theme is active.

   It is linked straight after trix.css in layouts/avr/application.html.erb,
   as lexxy-theme.css is after lexxy.css. Today each rule also out-ranks the
   stock one through its extra `.dark`, so the order is a second line of
   defence, not the mechanism: it is what keeps a rule written at Trix's own
   specificity winning. spec/lint/trix_theme_spec.rb pins the order, the
   `.dark` scope, the tokens and the contrast.

   Two choices that look odd until you measure them:

   The lines use `--color-text-muted`, not `--color-border-input`. A control's
   boundary needs 3:1 against what it sits on (WCAG 1.4.11) and the border
   tokens are tuned for hairlines inside a filled field: 1.42:1 on the surface
   under Lavender in dark mode, 1.94:1 under Classic. The toolbar has no fill
   of its own, so its outline is all that says where a button is.

   The pressed button is filled with `--color-text-secondary` and keeps Trix's
   black glyph. A text token has to be light in dark mode in every theme or
   nothing would be readable, so black on it is legible by construction. A
   brand stop makes no such promise: Aero inverts its ramp in dark mode and
   Lavender moves only two stops of it. */

/* ──── Toolbar buttons ──── */
.dark trix-toolbar .trix-button-group {
  border-color: var(--color-text-muted);
}

.dark trix-toolbar .trix-button {
  border-bottom-color: transparent;
  color: var(--color-text-secondary);
}

.dark trix-toolbar .trix-button:not(:first-child) {
  border-left-color: var(--color-text-muted);
}

/* The glyphs are data-URI background images on the button's ::before, so the
   only colour they can be given is the one a filter makes of them. The filter
   sits on the pseudo-element, which holds nothing but the glyph: the button's
   own border, fill and focus ring are not inverted with it. */
.dark trix-toolbar .trix-button--icon::before {
  filter: invert(1);
}

.dark trix-toolbar .trix-button:not(:disabled, .trix-active):hover {
  background: var(--color-hover);
}

.dark trix-toolbar .trix-button--icon:not(:disabled, .trix-active):hover::before {
  opacity: 1;
}

.dark trix-toolbar .trix-button.trix-active {
  background: var(--color-text-secondary);
  color: var(--color-page);
}

.dark trix-toolbar .trix-button--icon.trix-active::before {
  filter: none;
}

/* Undo and redo with nothing to undo. Stock Trix fades these to 12.5%, which
   on a dark surface is the fault this file exists to fix. */
.dark trix-toolbar .trix-button:disabled {
  color: color-mix(in oklab, var(--color-text-secondary) 30%, transparent);
}

.dark trix-toolbar .trix-button--icon:disabled::before {
  opacity: 0.3;
}

/* Trix sets `outline: none` on its buttons and on the dialog field. The ring
   is drawn inside the control because `.trix-button-row` scrolls sideways, and
   a scrolling box clips anything drawn outside it. */
.dark trix-toolbar .trix-button:focus-visible,
.dark trix-toolbar .trix-input--dialog:focus-visible {
  outline: 2px solid var(--color-ring);
  outline-offset: -2px;
}

.dark trix-toolbar .trix-button.trix-active:focus-visible {
  outline-color: var(--color-page);
}

/* ──── Link dialog ──── */
.dark trix-toolbar .trix-dialog {
  background: var(--color-surface);
  border: 1px solid var(--color-text-muted);
  border-top-width: 2px;
  box-shadow: none;
}

.dark trix-toolbar .trix-input--dialog {
  background-color: var(--color-surface);
  border-color: var(--color-text-muted);
  color: var(--color-text-primary);
}

.dark trix-toolbar .trix-input--dialog::placeholder {
  color: var(--color-text-placeholder);
}

/* A refused link. This one is not in trix.css: Trix's JavaScript injects a
   <style> of its own that paints the field pale pink, at a specificity the
   rule above does not beat, and our light text on pale pink cannot be read.
   The selector is Trix's own with `.dark` in front. The outline takes the
   error TEXT token because `--color-error-border` is a dark red in dark mode
   and would vanish into the fill. */
.dark trix-toolbar [data-trix-dialog] [data-trix-validate]:invalid {
  background-color: var(--color-error-bg);
  border-color: var(--color-error-text);
}

/* Trix puts the keyboard back in the field as it refuses the link, and the
   focus ring is drawn over the border. So at the moment it matters the ring
   is the only edge anyone sees, and it has to be the one that says refused. */
.dark trix-toolbar [data-trix-dialog] [data-trix-validate]:invalid:focus-visible {
  outline-color: var(--color-error-text);
}

/* ──── Editor box ──── */
.dark trix-editor {
  border-color: var(--color-text-muted);
}

/* :focus-within as well: typing an attachment's caption puts focus inside the
   editor and not on it, and rhino-editor.css answers that with a light-mode
   blue that is lost on a dark surface. */
.dark trix-editor:focus,
.dark trix-editor:focus-within {
  border-color: var(--color-ring);
  box-shadow: 0 0 0 1px var(--color-ring);
}

/* ──── Forced colours ──── */
/* A high-contrast setting replaces the pressed fill with its own dark one, and
   a black glyph on that is an empty box, on the one button that is switched
   on. Lighten it like the others and mark the button with the system's own
   highlight, since the fill no longer says which is pressed. */
@media (forced-colors: active) {
  .dark trix-toolbar .trix-button--icon.trix-active::before {
    filter: invert(1);
  }

  .dark trix-toolbar .trix-button.trix-active {
    outline: 2px solid Highlight;
    outline-offset: -2px;
  }
}
