/* Rich text editor and rendered rich text.
 *
 * Split out of quaeriva.css so the administration panel can load exactly these
 * rules without inheriting the public theme's shell, which would restyle the
 * whole panel. Both stylesheets link this file; the panel maps the design
 * tokens below onto its own --admin-* palette in quaeriva-admin.css.
 *
 * Tokens used here: --line, --surface, --surface-soft, --ink, --ink-muted,
 * --muted, --teal, --teal-dark, --radius-md, --color-primary,
 * --color-surface-soft, --color-border, --danger.
 */

/* Visual editor shell (quaeriva-editor.js, built from tools/editor).
 *
 * The surface itself carries .rich-text, so a heading, a quote and a link look
 * in the editor exactly as they will look once published -- that is the whole
 * point of the editor being visual, and it means the rules further down in this
 * file are the only definition of how content reads.
 */
.qe-wrap {
  position: relative;
  display: grid;
  gap: 0;
  min-inline-size: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--surface);
}
.qe-wrap:focus-within {
  border-color: var(--teal);
  outline: .1875rem solid color-mix(in srgb, var(--teal-dark) 35%, transparent);
  outline-offset: .1875rem;
}
/* The toolbar follows the writer down a long post. Without this the formatting
   controls scroll off the top and the caret is hundreds of pixels below them,
   so applying a heading means scrolling up, losing your place, and scrolling
   back. It only sticks while the editor itself is on screen. */
.qe-toolbar {
  position: sticky;
  inset-block-start: 0;
  z-index: 5;
  display: flex;
  flex-wrap: wrap;
  gap: .4rem;
  align-items: center;
  min-inline-size: 0;
  padding: .45rem;
  background: var(--surface-soft);
  border-block-end: 1px solid var(--line);
  border-start-start-radius: var(--radius-md);
  border-start-end-radius: var(--radius-md);
}

/* The wrapper must not create a scroll container of its own, or the sticky
   toolbar would stick to it instead of to the viewport. */
.qe-wrap {
  overflow: visible;
}

.qe-wrap.qe-invalid {
  border-color: var(--danger, #b42318);
}
.qe-wrap.qe-invalid .qe-status {
  color: var(--danger, #b42318);
  font-weight: 600;
}
.qe-group {
  display: inline-flex;
  /* A group is visual clustering, not a constraint: under a coarse pointer the
     block group is seven 44px targets, which is wider than a 375px phone, and
     refusing to break there pushed a button outside the editor. */
  flex-wrap: wrap;
  gap: .2rem;
  padding-inline-end: .4rem;
  border-inline-end: 1px solid var(--line);
}
.qe-group:last-of-type {
  padding-inline-end: 0;
  border-inline-end: 0;
}
.qe-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  inline-size: 2.25rem;
  block-size: 2.25rem;
  padding: 0;
  border: 1px solid transparent;
  border-radius: var(--radius-sm, .5rem);
  background: transparent;
  color: var(--ink-muted);
  cursor: pointer;
}
.qe-button svg { flex: 0 0 auto; }
.qe-button:hover:not(:disabled),
.qe-button:focus-visible {
  color: var(--ink);
  background: var(--surface);
  border-color: var(--line);
}
.qe-button.is-active {
  color: var(--color-primary, var(--teal));
  background: var(--color-surface-soft, var(--surface-soft));
  border-color: var(--color-primary, var(--teal));
}
.qe-button:disabled {
  opacity: .4;
  cursor: not-allowed;
}
.qe-surface {
  min-inline-size: 0;
}
.qe-surface .ProseMirror {
  min-block-size: 12rem;
  padding: .9rem 1rem;
  outline: 0;
  overflow-wrap: anywhere;
}
.qe-mode-comment .qe-surface .ProseMirror { min-block-size: 6rem; }
.qe-surface .ProseMirror > :first-child { margin-block-start: 0; }
.qe-surface .ProseMirror > :last-child { margin-block-end: 0; }
/* Placeholder text for an empty document, positioned by the extension rather
   than by :empty so it survives the trailing paragraph ProseMirror keeps. */
.qe-surface .ProseMirror p.is-editor-empty:first-child::before {
  content: attr(data-placeholder);
  float: inline-start;
  block-size: 0;
  color: var(--ink-muted);
  pointer-events: none;
}
/* A link has to be visible as a link while it is being written. The published
   page gets its link colour from the active theme, but the editing surface is
   not inside a theme, so without this a writer cannot see what they linked --
   which is most of the reason link editing felt unreliable before. */
.qe-surface .ProseMirror a {
  color: var(--color-primary, var(--teal));
  text-decoration: underline;
  text-underline-offset: .15em;
  cursor: text;
}
.qe-surface .ProseMirror img {
  max-inline-size: 100%;
  block-size: auto;
  border-radius: var(--radius-sm, .5rem);
}
.qe-surface .ProseMirror-selectednode {
  outline: 2px solid var(--color-primary, var(--teal));
  outline-offset: 2px;
}
/* The preview replaces the editing surface and announces itself. Rendering it
   underneath in the same typography made a short post look unchanged, so the
   button read as broken. */
.qe-preview-pane {
  background: var(--surface-soft);
  border-block-start: 1px solid var(--line);
}
.qe-preview-caption {
  margin: 0;
  padding: .5rem 1rem;
  border-block-end: 1px solid var(--line);
  color: var(--ink-muted);
  font-size: .78rem;
  font-weight: 600;
  letter-spacing: .02em;
  text-transform: uppercase;
}
.qe-preview {
  min-block-size: 8rem;
  padding: 1rem;
  background: var(--surface);
}
.qe-preview > :first-child { margin-block-start: 0; }
.qe-preview > :last-child { margin-block-end: 0; }
.qe-status {
  margin: 0;
  padding: .5rem 1rem;
  border-block-start: 1px solid var(--line);
  color: var(--ink-muted);
  font-size: .82rem;
}
textarea.qe-source-area {
  display: block;
  inline-size: 100%;
  min-block-size: 12rem;
  padding: .9rem 1rem;
  border: 0;
  border-radius: 0;
  background: var(--surface);
  color: var(--ink);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  resize: vertical;
}

/* One dialog for create, edit and remove, anchored inside the editor so the
   document selection is never handed to another element. */
.qe-link-dialog {
  position: absolute;
  inset-block-start: 50%;
  inset-inline-start: 50%;
  transform: translate(50%, -50%);
  inline-size: min(34rem, calc(100% - 2rem));
  z-index: 20;
  display: grid;
  gap: .65rem;
  padding: 1.15rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--surface);
  box-shadow: 0 1.5rem 4rem rgb(15 23 42 / 28%);
}
/* The dialog is centred by translating from the inline start, which flips with
   the writing direction; the sign has to flip with it. */
[dir="ltr"] .qe-link-dialog { transform: translate(-50%, -50%); }
.qe-link-label {
  margin: 0;
  color: var(--ink-muted);
  font-size: .82rem;
  font-weight: 600;
}
.qe-link-input {
  inline-size: 100%;
  min-inline-size: 0;
  min-block-size: 2.75rem;
  padding: .6rem .75rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm, .5rem);
  background: var(--surface);
  color: var(--ink);
  font-size: 1rem;
}
.qe-link-input:focus-visible {
  border-color: var(--color-primary, var(--teal));
  outline: .125rem solid color-mix(in srgb, var(--teal-dark) 35%, transparent);
  outline-offset: .0625rem;
}
.qe-link-error {
  margin: 0;
  color: var(--danger, #b42318);
  font-size: .82rem;
}
.qe-link-actions {
  display: flex;
  flex-wrap: wrap;
  gap: .5rem;
}
.qe-link-actions button {
  min-block-size: 2.75rem;
  padding-inline: 1.1rem;
  font-size: .95rem;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm, .5rem);
  background: var(--surface);
  color: var(--ink);
  cursor: pointer;
}
.qe-link-actions .qe-link-apply {
  border-color: var(--color-primary, var(--teal));
  background: var(--color-primary, var(--teal));
  color: var(--surface);
}
.qe-link-actions .qe-link-remove { color: var(--danger, #b42318); }

/* The touch floor the rest of the application uses, keyed to the pointer rather
   than to a width so a touch laptop is treated as touch and a narrow desktop
   window is not. The toolbar wraps, so a larger target costs no scrolling. */
@media (pointer: coarse) {
  .qe-button {
    inline-size: 2.75rem;
    block-size: 2.75rem;
  }
  .qe-link-actions button { min-block-size: 2.75rem; }
}


/* One typography definition for both the editing surface and the published
   page: the surface carries .rich-text, so what the writer sees while typing is
   set by the same rules that will render the post. */
.rich-text {
  overflow-wrap: anywhere;
  font-weight: 400;
  line-height: 1.7;
}
.rich-text ol,
.rich-text ul {
  margin-block: 1rem;
  padding-inline-start: 1.5rem;
}
.rich-text ol { list-style-type: decimal; }
.rich-text ul { list-style-type: disc; }
.rich-text li + li { margin-block-start: .45rem; }
/* Heading hierarchy.
 *
 * The level has to be readable at a glance, not deduced by holding two
 * headings side by side, so each one carries a colour and a marker of its own
 * as well as a size. That matters most in a long Arabic answer, where a reader
 * skims for the section they want and size alone reads as noise.
 *
 * Only --color-primary, --color-secondary and --color-text are used for text:
 * all three are contrast-gated against light and dark surfaces by
 * tests/Theme/DarkModeContrastRegression.php. --color-accent is decorative
 * here -- bars and dots only -- so it never has to meet a contrast floor.
 *
 * Logical properties throughout, because the same rules render right-to-left.
 */
.rich-text h2,
.rich-text h3,
.rich-text h4,
.rich-text h5,
.rich-text h6 {
  line-height: 1.4;
  overflow-wrap: anywhere;
  scroll-margin-block-start: 5rem;
}

.rich-text h2 {
  display: flex;
  align-items: center;
  gap: .55rem;
  margin-block: 1.75em .6em;
  padding-block-end: .35rem;
  border-block-end: 1px solid var(--color-border, var(--line, #dce7e3));
  color: var(--color-primary, #0e6f5c);
  font-size: clamp(1.2rem, 2.4vw, 1.45rem);
  font-weight: 800;
}
.rich-text h2::before {
  content: "";
  flex: 0 0 auto;
  align-self: stretch;
  inline-size: .28rem;
  min-block-size: 1.2em;
  border-radius: 999px;
  background: var(--color-primary, #0e6f5c);
}

.rich-text h3 {
  display: flex;
  align-items: baseline;
  gap: .5rem;
  margin-block: 1.5em .5em;
  color: var(--color-secondary, #3459e6);
  font-size: clamp(1.05rem, 2vw, 1.2rem);
  font-weight: 700;
}
.rich-text h3::before {
  content: "";
  flex: 0 0 auto;
  inline-size: .5rem;
  block-size: .5rem;
  border-radius: 50%;
  background: var(--color-accent, #c99a3b);
  transform: translateY(-.15em);
}

.rich-text h4 {
  margin-block: 1.3em .45em;
  color: var(--color-text, var(--ink, #173a36));
  font-size: 1.02rem;
  font-weight: 700;
}
.rich-text h4::after {
  content: "";
  display: block;
  inline-size: 2.2rem;
  block-size: 2px;
  margin-block-start: .3rem;
  border-radius: 999px;
  background: var(--color-accent, #c99a3b);
}

.rich-text h5,
.rich-text h6 {
  margin-block: 1.2em .4em;
  color: var(--color-muted, var(--ink-muted, #657975));
  font-size: .95rem;
  font-weight: 700;
}

/* The first heading of a body must not push the post away from its own title. */
.rich-text > :first-child {
  margin-block-start: 0;
}

/* A marker rendered inside a link or a small screen must never break the line
   box; the flex row does the alignment, so nothing else has to. */
@media (max-width: 30rem) {
  .rich-text h2 { gap: .4rem; }
  .rich-text h2::before { inline-size: .22rem; }
}
.rich-text blockquote {
  margin-block: 1rem;
  margin-inline: 0;
  padding-block: .75rem;
  padding-inline: 1rem;
  border-inline-start: 4px solid var(--color-primary, #3459e6);
  background: var(--color-surface-soft, #f5f7fb);
  border-radius: .65rem;
}
.rich-text pre {
  max-inline-size: 100%;
  overflow-x: auto;
  padding: 1rem;
  border-radius: .75rem;
  background: #101828;
  color: #f8fafc;
  direction: ltr;
  text-align: start;
}
.rich-text code {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
}
.rich-text :not(pre) > code {
  padding: .12em .35em;
  border-radius: .35rem;
  background: var(--color-surface-soft, #f5f7fb);
}
.rich-text a,
.comment-item a {
  overflow-wrap: anywhere;
}

/* Tables, in the editor and on the published page.
 *
 * The wrapper is what makes a wide table usable on a phone: the table scrolls
 * inside its own box instead of forcing the whole page sideways, which is the
 * rule the rest of the site follows for wide content. Borders are logical
 * properties so an Arabic table is not built the wrong way round. */
.rich-text-table-wrap,
.qe-surface .ProseMirror table {
  max-inline-size: 100%;
  margin-block: 1rem;
}
.rich-text-table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.rich-text-table,
.qe-surface .ProseMirror table {
  inline-size: 100%;
  border-collapse: collapse;
  font-size: .95em;
}
.rich-text-table th,
.rich-text-table td,
.qe-surface .ProseMirror th,
.qe-surface .ProseMirror td {
  padding: .55rem .7rem;
  border: 1px solid var(--color-border, var(--line));
  text-align: start;
  vertical-align: top;
  /* A cell with a long unbroken address must not stretch the table past the
     screen; it wraps instead. */
  overflow-wrap: anywhere;
}
.rich-text-table th,
.qe-surface .ProseMirror th {
  background: var(--color-surface-soft, var(--surface-soft));
  font-weight: 700;
}
.rich-text-table tbody tr:nth-child(even),
.qe-surface .ProseMirror tbody tr:nth-child(even) {
  background: color-mix(in srgb, var(--color-surface-soft, var(--surface-soft)) 45%, transparent);
}

/* The editing surface needs the cell boundaries to stay visible while empty,
   and a visible selection, or building a table is guesswork. */
.qe-surface .ProseMirror table {
  table-layout: fixed;
}
.qe-surface .ProseMirror th,
.qe-surface .ProseMirror td {
  min-inline-size: 4rem;
  position: relative;
}
.qe-surface .ProseMirror .selectedCell::after {
  content: '';
  position: absolute;
  inset: 0;
  background: color-mix(in srgb, var(--color-primary, var(--teal)) 18%, transparent);
  pointer-events: none;
}

/* Embedded content images. Markdown carries no dimensions, so the box is
   constrained here; the reserved min-height limits the shift while the image
   loads without pretending to know its aspect ratio. */
.rich-text-image {
    max-inline-size: 100%;
    block-size: auto;
    min-block-size: 2rem;
    margin-block: .75rem;
    border: 1px solid var(--color-border, var(--line));
    border-radius: var(--radius-md, .75rem);
    background: var(--color-surface-soft, var(--surface-soft));
}
