@layer base {
  body {
    display: grid;
    grid-template-rows: auto 1fr auto;
  }

  :where(#header) {
    position: relative;
    z-index: var(--z-nav);

    /* iOS 26+ blurs the top of home-screen apps unless it finds an opaque,
       sticky/fixed full-width bar at the top edge to extend instead. Only in
       the installed app — the browser tab keeps its static header. */
    @media (display-mode: standalone) {
      background-color: var(--color-backdrop);
      inset-block-start: 0;
      position: sticky;
    }
  }

  /* Three columns so the brand stays dead center whatever sits beside it. */
  .nav {
    align-items: center;
    block-size: var(--header-height);
    display: grid;
    grid-template-columns: 1fr auto 1fr;

    > :first-child {
      grid-column: 2;
    }

    > :last-child:not(:first-child) {
      grid-column: 3;
      justify-self: end;
    }

    /* Inside a workspace the menu takes the left edge instead, and leaves the
       sheet's positioning to #header. */
    > .resource-menu {
      grid-column: 1;
      justify-self: start;
      position: static;
    }
  }

  .topbar {
    align-items: center;
    display: flex;
    /* The title and its action cluster sit on one line when they fit and the
       actions drop to their own line when they don't — a phone can't hold
       "Reminders" plus four controls side by side. */
    flex-wrap: wrap;
    gap: var(--block-space-half) var(--inline-space);
    justify-content: space-between;
    margin-block-end: var(--block-space);
  }

  /* Index pages: the section name is the page's centerpiece, big and centered,
     with its actions tucked into the corner above it. */
  .topbar:has(> h1) {
    display: grid;
    grid-template-columns: 1fr auto 1fr;
    margin-block-end: var(--block-space-double);

    > h1 {
      font-size: var(--text-xx-large);
      font-weight: 800;
      grid-column: 2;
      grid-row: 1;
      letter-spacing: -0.02em;
      line-height: 1.1;
      text-align: center;
    }

    > :not(h1) {
      grid-column: 3;
      grid-row: 1;
      justify-self: end;
    }

    /* The section search mirrors the actions on the other side of the title. */
    > .section-search {
      grid-column: 1;
      justify-self: start;
    }

    /* Mid widths can't fit search, title and actions on one line: the title
       takes its own row and the search and actions share the row beneath. */
    @media (min-width: 640px) and (max-width: 1100px) {
      &:has(> .section-search) {
        grid-template-columns: 1fr auto;
        row-gap: var(--block-space);

        > h1 {
          grid-column: 1 / -1;
        }

        > .section-search {
          grid-column: 1;
          grid-row: 2;
        }

        > :not(h1, .section-search) {
          grid-column: 2;
          grid-row: 2;
        }
      }
    }

    @media (max-width: 639px) {
      grid-template-columns: 1fr;
      /* Title, actions and search each take a row; half a space crams them. */
      row-gap: var(--block-space);

      > h1 {
        grid-column: 1;
      }

      > :not(h1) {
        grid-column: 1;
        grid-row: 2;
        justify-self: center;
      }

      /* Plain icon buttons and text links have no padding of their own, so
         the cluster's gap is all that keeps them apart under a thumb. A
         crowded cluster (Reminders) wraps between controls on the narrowest
         phones rather than breaking a button's label in two. */
      > :not(h1, .section-search) {
        --column-gap: calc(var(--inline-space) * 1.5);
        flex-wrap: wrap;
        justify-content: center;

        > * {
          white-space: nowrap;
        }
      }

      > .section-search {
        grid-row: 3;
        justify-self: stretch;
      }
    }
  }

  /* A plain input with the magnifier as its submit button just to the right.
     Enter submits too. The input carries the width, so every section's box
     is the same length whatever buttons follow it. */
  .section-search {
    align-items: center;
    display: flex;
    gap: var(--inline-space-half);
    max-inline-size: 100%;

    @media (max-width: 639px) {
      inline-size: 100%;
    }
  }

  /* Pill-shaped and soft at rest: the hairline colour of the boxes, so it
     only stands out once the focus ring comes in. */
  .section-search__input {
    --input-border-color: var(--color-ink-lighter);
    --input-border-radius: 99rem;
    --input-padding: 0.5em 1em;
    --input-max-width: none;

    flex: 0 1 15rem;
    min-inline-size: 0;

    @media (max-width: 639px) {
      flex-grow: 1;
    }
  }

  /* A square exactly as tall as the input beside it: its line, its 0.5em
     block padding on each side and its 1px borders. */
  .section-search > .btn {
    --btn-border-color: var(--color-ink-lighter);
    --btn-size: calc(1lh + 1em + 2px);
  }

  /* An expanded option is in use: a blue dot on the button says the list
     is narrower than the search box alone suggests. */
  .section-search__options--active::before {
    background-color: var(--color-link);
    block-size: 0.5em;
    border-radius: 50%;
    box-shadow: 0 0 0 2px var(--color-canvas);
    content: "";
    inline-size: 0.5em;
    inset-block-start: 0.1em;
    inset-inline-end: 0.1em;
    position: absolute;
  }

  /* One line of tag chips above a section's content. */
  .tag-filter {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35em;
    margin-block-end: var(--block-space);
  }

  /* A subpage's section name, set exactly like the index's centered h1 so
     the section reads the same on every page beneath it; it links back. */
  .section-headline {
    font-size: var(--text-xx-large);
    font-weight: 800;
    letter-spacing: -0.02em;
    line-height: 1.1;
    margin-block: 0 var(--block-space-double);
    text-align: center;

    > .section-headline__link {
      color: var(--color-ink);
      text-decoration: none;

      @media (any-hover: hover) {
        &:hover {
          color: var(--color-link);
        }
      }
    }
  }

  /* A show page's own header, under the section headline: the record's kind
     and name on the left, its main actions (Edit, Delete) always on the
     right on the same line, dropping below the name only when space runs out. */
  .record-header {
    align-items: center;
    display: flex;
    flex-wrap: wrap;
    gap: var(--block-space-half) var(--inline-space);
    justify-content: space-between;
  }

  .record-header__actions {
    display: flex;
    flex-wrap: wrap;
    gap: var(--inline-space-half);
  }

  .nav__brand {
    color: var(--color-ink);
    font-size: var(--text-medium);
    font-weight: 800;
    letter-spacing: -0.01em;
    text-decoration: none;
  }

  .avatar--initials {
    align-items: center;
    background-color: var(--color-primary);
    block-size: 2rem;
    color: var(--color-white);
    display: inline-flex;
    font-size: 0.8rem;
    font-weight: 700;
    inline-size: 2rem;
    justify-content: center;
  }

  .avatar {
    aspect-ratio: 1;
    border-radius: 50%;
    object-fit: cover;
    vertical-align: middle;
  }

  .profile-menu {
    align-items: center;
    display: flex;
    position: relative;
  }

  .profile-menu > summary {
    list-style: none;

    &::-webkit-details-marker {
      display: none;
    }
  }

  .profile-menu__dropdown {
    --panel-padding-block: var(--block-space-half);
    --panel-padding-inline: var(--block-space-half);

    inline-size: max-content;
    min-inline-size: min(14rem, calc(100vw - var(--main-padding) * 2));
    max-inline-size: min(22rem, calc(100vw - var(--main-padding) * 2));
    inset-block-start: calc(100% + 0.5rem);
    inset-inline-end: 0;
    position: absolute;
    z-index: var(--z-popup);

    form {
      display: contents;
    }
  }

  /* Cargo on the left: a burger that folds into an X while the sheet is open. */
  .resource-menu__trigger {
    align-items: center;
    border-radius: 0.5em;
    cursor: pointer;
    display: flex;
    font-size: var(--text-large);
    font-weight: 900;
    gap: 0.45em;
    letter-spacing: -0.03em;
    margin-inline-start: -0.4em;
    padding: 0.15em 0.4em;

    @media (any-hover: hover) {
      &:hover {
        background-color: var(--color-ink-lightest);
      }
    }
  }

  .resource-menu__burger {
    block-size: 0.55em;
    inline-size: 0.8em;
    position: relative;

    &::before,
    &::after {
      background-color: currentColor;
      block-size: 0.12em;
      border-radius: 1em;
      content: "";
      inset-inline: 0;
      position: absolute;
      transition: inset-block-start 250ms var(--ease-out-expo), rotate 250ms var(--ease-out-expo);
    }

    &::before { inset-block-start: 0; }
    &::after  { inset-block-start: calc(100% - 0.12em); }

    .resource-menu[open] & {
      &::before,
      &::after {
        inset-block-start: calc(50% - 0.06em);
      }

      &::before { rotate: 45deg; }
      &::after  { rotate: -45deg; }
    }
  }

  .profile-menu__item {
    background: none;
    border: 0;
    border-radius: 0.4em;
    color: var(--color-ink);
    cursor: pointer;
    display: block;
    font-size: var(--text-small);
    font-weight: 500;
    inline-size: 100%;
    padding: 0.4em 0.5em;
    text-align: start;
    text-decoration: none;
    white-space: nowrap;

    @media (any-hover: hover) {
      &:hover {
        background-color: var(--color-ink-lightest);
      }
    }
  }

  /* The workspace sheet: Cargo washes a full-screen sheet out from the brand,
     one card per section with a + for a new record and its other views. */
  /* The sheet replaces the page's content area and nothing else: it hangs
     from the header (its containing block, so it scrolls with the page) with
     #main's width, rounding and padding, leaving the header and the profile
     menu in view. */
  .resource-sheet {
    animation: resource-sheet-in 450ms var(--ease-out-expo);
    background-color: var(--color-canvas);
    block-size: calc(100dvh - var(--header-height) - clamp(0px, 2vw, 2rem));
    border-radius: clamp(1rem, 3vw, 2rem);
    inline-size: calc(100% - 2 * clamp(0px, 2vw, 2rem));
    inset-block-start: 100%;
    inset-inline: 0;
    margin-inline: auto;
    max-inline-size: var(--main-width);
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-block: var(--block-space-double);
    padding-inline: clamp(var(--block-space), 4vw, calc(var(--block-space) * 3));
    position: absolute;
    z-index: var(--z-popup);

    /* The wash starts from the sheet's top-left corner, under the brand. */
    --sheet-origin: 2rem 0;
  }

  /* While the workspace sheet is open it is the page. The sheet is only one
     screen tall and hangs from the header, so on a long page (a person with a
     long timeline) scrolling would carry it away and reveal the records
     beneath. Lock the page scroll, as an open dialog does, and hide what's
     under the sheet so nothing peeks past its edges either. visibility (not
     display) keeps the page's height and scroll position for when it closes. */
  body:has(.resource-menu[open]) {
    overflow: hidden;

    > :is(#main, #footer) {
      visibility: hidden;
    }
  }

  .resource-sheet__inner {
    display: grid;
    gap: var(--block-space);
    margin-inline: auto;
    max-inline-size: 60rem;
  }

  .resource-sheet__home {
    align-items: center;
    border: var(--border);
    border-radius: 99em;
    display: inline-flex;
    font-size: var(--text-small);
    font-weight: 600;
    gap: 0.5em;
    justify-self: start;
    padding: 0.4em 1em;

    &, &:visited {
      color: var(--color-ink-dark);
    }

    @media (any-hover: hover) {
      &:hover {
        background-color: var(--color-ink-lightest);
        color: var(--color-ink);
      }
    }
  }

  .resource-sheet__home--active {
    &, &:visited {
      color: var(--color-ink);
    }
  }

  /* The tenant you're in, among the switcher pills — filled, not a link. */
  .resource-sheet__tenant--current {
    background-color: var(--color-ink-lightest);
    border-color: transparent;
    color: var(--color-ink);
  }

  .resource-sheet__grid {
    display: grid;
    gap: var(--block-space-half);
    grid-template-columns: repeat(auto-fill, minmax(min(100%, 17rem), 1fr));
  }

  /* Each section is one big tile in its own hue, clickable as a whole;
     scaffolded sections fall back to the link blue. */
  .section-card {
    --section-color: var(--color-link);

    background-color: color-mix(in oklch, var(--section-color) 12%, var(--color-canvas));
    border-radius: 1em;
    display: flex;
    flex-direction: column;
    justify-content: space-between;
    min-block-size: 9rem;
    padding: var(--block-space) calc(var(--block-space) * 1.25);
    transition: translate 200ms var(--ease-out-overshoot), background-color 200ms ease-out;

    &, &:visited {
      color: var(--color-ink);
    }

    > .icon {
      block-size: 2rem;
      color: var(--section-color);
      inline-size: 2rem;
    }

    @media (any-hover: hover) {
      &:hover {
        background-color: color-mix(in oklch, var(--section-color) 22%, var(--color-canvas));
        translate: 0 -3px;
      }
    }

    &[data-section="reminders"] { --section-color: oklch(var(--lch-yellow-dark)); }
    &[data-section="things"]    { --section-color: oklch(var(--lch-green-dark)); }
    &[data-section="accounts"]  { --section-color: oklch(var(--lch-purple-dark)); }
    &[data-section="recipes"]   { --section-color: oklch(var(--lch-red-dark)); }
    &[data-section="secrets"]   { --section-color: oklch(var(--lch-ink-darker)); }
  }

  .section-card--active {
    box-shadow: inset 0 0 0 2px var(--section-color);
  }

  .section-card__name {
    font-size: var(--text-large);
    font-weight: 800;
    letter-spacing: -0.02em;
  }

  /* Destructive menu actions (Delete): red text, light red hover. */
  .profile-menu__item--negative {
    color: var(--color-negative);

    @media (any-hover: hover) {
      &:hover {
        background-color: var(--color-negative-subtle);
      }
    }
  }

  /* The sheet: one raised, rounded surface holding the page, a step lighter
     than the backdrop in dark mode and white on off-white in light. */
  :where(#main) {
    background-color: var(--color-canvas);
    border-radius: clamp(1rem, 3vw, 2rem);
    inline-size: calc(100% - 2 * clamp(0px, 2vw, 2rem));
    margin-block-end: clamp(0px, 2vw, 2rem);
    max-inline-size: var(--main-width);
    /* A grid item's automatic minimum size is its content's min-content width,
       so a wide table would stretch #main past the viewport and the clip on
       <body> would eat the overhang — buttons and columns unreachable, with no
       scrollbar to reach them. Zero lets #main stay viewport-width and hands
       the overflow to the scroll container that owns it (.table-wrapper). */
    min-inline-size: 0;
    margin-inline: auto;
    padding-block: var(--block-space-double);
    padding-inline: clamp(var(--block-space), 4vw, calc(var(--block-space) * 3));
  }

  :where(#footer) {
    max-inline-size: 100%;
  }

  :is(#header, #footer) {
    @media print {
      display: none;
    }
  }

  .container {
    max-inline-size: var(--main-width);
    margin-inline: auto;
    padding-inline: var(--main-padding);
  }
}
