@layer components {
  /* A person's Markdown editor: a monospace text as tall as the window, its preview in the same place. */
  .document-editor { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
  .document-editor-top { display: flex; align-items: baseline; justify-content: space-between; flex-wrap: wrap; gap: 4px 12px; }
  .document-editor-title {
    margin: 0; font-family: var(--font-heading); font-size: 20px; font-weight: 600; line-height: 1.25;
    overflow-wrap: anywhere;
  }
  .document-editor--panel .document-editor-title { font-size: 17px; }
  .document-editor-base { font-size: 13px; color: var(--muted-foreground); }

  .document-editor-notice, .document-editor-offer {
    margin: 0; padding: 8px 12px; border: 1px solid var(--jx-wait); border-radius: var(--radius-md);
    background: var(--jx-wait-soft); font-size: 13px;
  }
  .document-editor-offer:not([hidden]) { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; }
  .document-editor-offer span { flex: 1 1 200px; }
  .document-editor-reload { margin: 0; }

  .document-editor-form { display: flex; flex-direction: column; gap: 12px; }
  .document-editor-tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); }
  .document-editor-tab {
    padding: 6px 12px; margin-bottom: -1px; border: 0; border-bottom: 2px solid transparent; background: none;
    font: inherit; font-size: 13px; font-weight: 500; color: var(--muted-foreground); cursor: pointer;
  }
  .document-editor-tab[aria-selected="true"] { color: var(--foreground); border-bottom-color: var(--foreground); }
  .document-editor-tab:focus-visible { outline: 2px solid var(--jx-accent); outline-offset: 2px; }

  textarea.document-editor-input {
    width: 100%; min-height: 320px; height: calc(100dvh - 360px); resize: vertical;
    font-family: var(--font-mono); font-size: 13px; line-height: 1.55; tab-size: 2;
  }
  /* Under the document's header, the text leaves room for « Enregistrer » without scrolling. */
  .document-view-edit textarea.document-editor-input { min-height: 200px; height: calc(100dvh - 580px); }
  .document-editor-preview {
    min-height: 320px; padding: 12px 16px; border: 1px solid var(--border); border-radius: var(--radius-md);
    background: var(--surface); overflow: auto;
  }
  .document-editor-summary { display: flex; flex-direction: column; gap: 6px; font-size: 13px; }
  .document-editor-summary label { font-weight: 500; }

  /* The visual editor takes the text field's place; the field stays what the form sends. */
  .visual-editor { display: none; }
  [data-visual-editor-mode="visual"] .visual-editor { display: flex; flex-direction: column; min-width: 0; }
  [data-visual-editor-mode="visual"] :is(.document-editor-tabs, .document-editor-form [role="tabpanel"]),
  [data-visual-editor-mode="loading"] :is(.document-editor-tabs, .document-editor-form [role="tabpanel"]) {
    display: none;
  }
  /* The text fills the box: a click anywhere under the last block lands in the editor. */
  .visual-editor-surface { display: flex; flex-direction: column; min-height: 320px; padding: 12px 0 0; }
  .visual-editor-surface .ProseMirror { flex: 1; padding-bottom: 12px; }
  .visual-editor-surface .ProseMirror:focus-visible { outline: none; }
  .visual-editor-surface[data-focus-by="keyboard"]:has(.ProseMirror:focus) { box-shadow: inset 2px 0 0 var(--jx-accent); }
  .visual-editor-hint:not([hidden]) { display: block; }
  /* « Revenir à l'éditeur visuel »: in raw Markdown only, from 1100 px. */
  .visual-editor-back { display: none; margin: 0 0 8px; }
  [data-visual-editor-mode="raw"] .visual-editor-back:not([hidden]) { display: block; }
  /* While the library loads (328 KB the first time), the page says so where the text will be. */
  .visual-editor-loading { display: none; }
  [data-visual-editor-mode="loading"] .visual-editor-loading {
    display: block; min-height: 320px; padding: 12px 0; color: var(--muted-foreground);
  }

  /* « Nouveau »: the title and the template, before the editor opens. */
  .document-new { display: flex; flex-direction: column; gap: 12px; max-width: 560px; }
  .document-new-title { margin: 0; font-family: var(--font-heading); font-size: 20px; font-weight: 600; }
  .document-new-place { margin: 0; font-size: 13px; color: var(--muted-foreground); }
  .document-new-form { display: flex; flex-direction: column; gap: 16px; }

  /* The phone's editor: a screen of the pile. These parts exist for it and stay hidden from 1100 px. */
  .document-editor-bar, .document-editor-hint, .document-editor-offer-short { display: none; }

  @media (max-width: 1099px) {
    /* The screen is as tall as what the reader sees, keyboard included; its text takes what the header leaves. */
    .app-main.app-main--fit {
      height: var(--vv-height, 100dvh); translate: 0 var(--vv-top);
      display: flex; flex-direction: column; min-height: 0; padding: 0; overflow: hidden;
    }
    /* Full width: the page's auto margins would size it to its widest content, a wide table's. */
    .app-main--fit > .page { flex: 1; min-height: 0; width: 100%; max-width: none; gap: 0; }
    .document-editor--page { flex: 1; min-height: 0; gap: 0; }
    .document-editor--page :is(.document-editor-top, .document-editor-summary, .document-editor-actions) { display: none; }

    .document-editor-bar {
      flex: none; display: flex; align-items: center; gap: 8px; min-height: calc(48px + var(--safe-top));
      padding: var(--safe-top) var(--gutter) 0; border-bottom: 1px solid var(--border); background: var(--background);
    }
    .document-editor-bar-cancel, .document-editor-bar-save {
      flex: none; position: relative; display: inline-flex; align-items: center; justify-content: center;
      min-width: var(--tap); min-height: var(--tap); padding: 0 8px; border: 0; background: none;
      font: inherit; font-size: var(--text-body); text-decoration: none; cursor: pointer;
    }
    .document-editor-bar-cancel { margin-left: -8px; color: var(--foreground); }
    .document-editor-bar-save { margin-right: -8px; color: var(--jx-accent); font-weight: 600; }
    .document-editor-bar-title { flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; text-align: center; }
    .document-editor-bar-name {
      max-width: 100%; margin: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
      font-size: var(--text-body); font-weight: 600; line-height: 1.3;
    }
    .document-editor-bar-base {
      max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
      color: var(--muted-foreground); font-size: 0.8125rem;
    }

    /* Two lines at 320 px rather than an ellipsis: what a save does must be readable. */
    .document-editor-hint {
      display: block; flex: none; margin: 0; padding: 6px var(--gutter);
      color: var(--muted-foreground); font-size: 0.8125rem;
    }
    .document-editor-notice, .document-editor-reload { flex: none; margin: 8px var(--gutter) 0; }
    .document-editor-offer { flex: none; margin: 8px var(--gutter) 0; }
    .document-editor-offer:not([hidden]) { flex-wrap: nowrap; }
    .document-editor-offer span { flex: 1 1 0; min-width: 0; }
    .document-editor-offer-long { display: none; }
    .document-editor-offer-short { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

    .document-editor--page .document-editor-form { flex: 1; min-height: 0; gap: 0; }
    .document-editor--page .document-editor-tabs {
      flex: none; display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 2px; margin: 8px var(--gutter);
      padding: 3px; border: 0; border-radius: 11px; background: var(--muted);
    }
    .document-editor--page .document-editor-tab {
      min-height: 34px; margin: 0; padding: 0 8px; border: 0; border-radius: 8px;
      font-size: var(--text-body); color: var(--muted-foreground);
    }
    .document-editor--page .document-editor-tab[aria-selected="true"] {
      background: var(--background); color: var(--foreground); font-weight: 600; box-shadow: var(--shadow-sm);
    }
    .document-editor--page [role="tabpanel"]:not([hidden]) { flex: 1; min-height: 0; display: flex; flex-direction: column; }
    .document-editor--page textarea.document-editor-input {
      flex: 1; height: auto; min-height: 0; padding: 8px var(--gutter); font-size: 1rem;
      border: 0; border-radius: 0; box-shadow: none; resize: none;
    }
    .document-editor--page .document-editor-preview { flex: 1; padding: 8px var(--gutter); border: 0; border-radius: 0; }
    .document-editor .sheet-foot .button { min-height: var(--tap); }
    .document-editor .sheet .field .input { min-height: var(--tap); }

    /* The visual editor on the page: no tabs, no text field, the text scrolls in its own box under the header. The
       selectors outweigh `[role="tabpanel"]:not([hidden])` above (0,3,0). */
    .document-editor--page:is([data-visual-editor-mode="visual"], [data-visual-editor-mode="loading"])
      .document-editor-form :is(.document-editor-tabs, [role="tabpanel"]) { display: none; }
    .document-editor--page[data-visual-editor-mode="visual"] .visual-editor {
      flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; overscroll-behavior: contain;
    }
    .document-editor--page .visual-editor-toolbar { display: none; }
    /* Under the last block, a touch still lands in the text. */
    .document-editor--page .visual-editor-surface { flex: 1 0 auto; min-height: 0; padding: 8px var(--gutter) 16px; }
    .document-editor--page .visual-editor-back { margin: 0 var(--gutter) 8px; }
    .document-editor--page[data-visual-editor-mode="loading"] .visual-editor-loading { padding: 12px var(--gutter); }
    /* Landscape with the keyboard up leaves a few lines: the text takes the line of the draft's note. */
    @media (orientation: landscape) {
      html[data-keyboard] .document-editor--page .document-editor-hint { display: none; }
      html[data-keyboard] .document-editor--page .visual-editor-surface { padding-top: 0; }
    }
    :root[data-keyboard]:has([data-visual-editor-mode="visual"].document-editor--page) .toasts {
      --toast-bottom: calc(var(--vv-bottom, 0px) + var(--keys-height) + 8px);
    }
  }
}
