@layer components {
  /* layout.css lets the container's children sit in the body's rows; the strip can only stick inside a box that runs to the end of the page. A column, so #main's auto margins still centre a lone panel. Without the zero, the crumbs would widen the page on a phone. */
  #global-container {
    display: flex;
    flex-direction: column;
    min-inline-size: 0;
  }

  /* The last row keeps the page clear of the bottom bar. */
  body {
    grid-template-rows: 1fr auto var(--footer-height);

    &.public {
      grid-template-rows: 1fr auto;
    }
  }

  /* One panel in the canvas colour under the top bar, as wide as its page asks and centred between the gutters; below 800 it fills the window. The fill runs on past its end, under the bar to the bottom of the page. */
  .sheet {
    --color-page: var(--color-canvas);
    --sheet-size: var(--sheet-size-standard);

    display: flex;
    flex: 1 1 auto;
    flex-direction: column;
    min-block-size: 0;

    /* A page takes the standard sheet: a list, a site, a record, a form, the activity, the chat. Settings take the narrow one and a chooser the medium one. */
    &:has(> #main .page--narrow) {
      --sheet-size: var(--sheet-size-narrow);
    }

    &:has(> #main .page--medium) {
      --sheet-size: var(--sheet-size-medium);
    }

    /* A full page, a paper with its index beside it, takes the window between the gutters. */
    &:has(> #main .page--full) {
      --sheet-size: 100%;
    }

    /* On paper the page is plain: no fill, no padding and no width of the sheet's. */
    @media screen {
      border-image: conic-gradient(var(--color-canvas) 0 0) fill 0 / 0 / 0 0 100vmax;
      inline-size: min(100% - var(--sheet-gutter) * 2, var(--sheet-size));
      margin-inline: auto;

      /* Where the ground shows beside it, square corners and a soft wide shadow set it apart. */
      @media (width >= 800px) {
        box-shadow: 0 0 30px oklch(var(--lch-black) / 8%);
      }

      > #main {
        inline-size: auto;
        margin-inline: 0;
        max-inline-size: none;
        padding-block: var(--sheet-padding) calc(var(--sheet-padding-end) - var(--footer-height));
        padding-inline:
          calc(var(--sheet-padding) + var(--custom-safe-inset-left))
          calc(var(--sheet-padding) + var(--custom-safe-inset-right));
      }

      /* The chat holds the screen and keeps its thread close under the strip. */
      > #main:has(> .chat) {
        padding-block-start: var(--block-space-half);
      }
    }
  }

  /* Fizzy's #main centres its text; a page of ours reads from the start. A panel inside fills the page at every width. */
  .page {
    --panel-size: 100%;

    inline-size: 100%;
    margin-inline: auto;
    max-inline-size: var(--page-size, 40rem);
    text-align: start;
  }

  /* A page's name, large and heavy at the top of the sheet, with a lead line under it and the page's main actions in a row under that. A filter that follows sits close, as one more row of its controls. */
  .page-header {
    display: flex;
    flex-direction: column;
    gap: 2px;
    margin-block-end: var(--page-header-space);
    text-align: start;

    /* The filter keeps 2px over its field for the focus ring, also where a tray's list opens as a page in its frame. */
    &:has(+ .page > [data-controller~="quick-find"]:first-child, + .page > turbo-frame:first-child > [data-controller~="quick-find"]:first-child) {
      margin-block-end: calc(var(--control-gap) - 2px);
    }
  }

  /* On paper the name prints as any heading. */
  .page-header__title {
    margin: 0;
    overflow-wrap: anywhere;
    text-wrap: balance;

    @media screen {
      font-size: var(--text-page-title);
      font-weight: 800;
      letter-spacing: -0.02em;
      line-height: 1.15;
    }
  }

  /* A state beside the name, on its line while there is room and under it when there is not. */
  .page-header__heading {
    align-items: center;
    display: flex;
    flex-wrap: wrap;
    gap: var(--inline-space-half) var(--inline-space);
  }

  .page-header__status {
    flex: none;
    font-size: var(--text-control);
  }

  /* A lead reads at a row's size. */
  .page-header__lead {
    font-size: var(--text-row);
    margin: 0;
    text-wrap: pretty;
  }

  .page-header__actions {
    align-items: center;
    display: flex;
    flex-wrap: wrap;
    font-size: var(--text-control);
    gap: var(--control-gap);
    margin-block-start: calc(var(--control-gap) - 2px);
  }

  /* On the sheet a page fills it, the sheet having taken the width the page asks for. */
  .sheet .page {
    max-inline-size: none;
  }
}
