/*
 * The app shell: a pastel masthead holding the top bar and the page header, a wave into the canvas, then the page.
 * Pages fill it through content_for (see app/views/layouts/application.html.erb).
 */
@layer layouts {
  .masthead { background: var(--band); }
  /* The wave hangs a pixel past the band, so a fractional height never shows a line of band under it. */
  .masthead > .wave { margin-bottom: -1px; }

  .masthead__inner {
    display: grid;
    gap: var(--space-10);
    max-width: var(--content-width);
    margin-inline: auto;
    padding: var(--space-4) var(--gutter) var(--space-4);
  }

  .top-bar { --shelf-row: 3.5rem; --shelf-height: var(--shelf-row); --joint: var(--space-3); position: relative; display: flex; gap: var(--space-2); align-items: center; }

  /* Sections: the current one, when it has a shelf, is a white folder tab joined to it, a white bar of that section's
     pages under the top bar. Hovering or focusing another section previews its shelf there. */
  .top-bar:has(.sections__item--current > .shelf) { margin-bottom: var(--shelf-height); }
  .sections { display: flex; align-self: stretch; gap: var(--space-1); margin: 0; padding: 0; list-style: none; }
  .sections__item { display: flex; }

  /* Taller than the avatar, so the avatar keeps clear of the shelf below it. */
  .sections__tab {
    position: relative;
    display: inline-flex;
    align-items: center;
    min-height: calc(var(--control-height) + var(--space-4));
    padding: 0 var(--space-4);
    border-radius: var(--radius-md);
    color: inherit;
    font-weight: var(--weight-medium);
    text-decoration: none;
    white-space: nowrap;
    transition: background var(--duration-fast) var(--ease);

    &:hover { background: var(--fill-hover); }
    &:focus-visible { outline-offset: -3px; }
    &[aria-current] { font-weight: var(--weight-bold); }
  }
  /* A section without a shelf, such as Team, is a white squircle when current. */
  .sections__item--current:not(:has(> .shelf)) > .sections__tab { background: var(--color-surface); }

  .shelf {
    --fill: var(--color-ink-8);
    --fill-hover: var(--color-ink-8);

    position: absolute;
    top: 100%;
    inset-inline: 0;
    z-index: 1;
    display: flex;
    gap: var(--space-1);
    align-items: center;
    height: var(--shelf-row);
    padding-inline: var(--space-2);
    overflow-x: auto;
    scrollbar-width: none;
    border-radius: var(--radius-lg);
    background: var(--color-surface);
    visibility: hidden;

    .nav-item[aria-current] { background: var(--band); }
  }

  .shelf__end { margin-left: auto; }

  /* On an event the shelf has two rows: the event, with the open tab's actions, over its tabs. */
  .top-bar:has(.sections__item--current .shelf__head) { --shelf-height: var(--shelf-head); }
  .top-bar:has(.sections__item--current .shelf__tabs) { --shelf-height: calc(var(--shelf-head) + var(--shelf-row) - var(--space-2)); }
  .top-bar { --shelf-head: 4.5rem; }
  .shelf:has(.shelf__head) { flex-direction: column; align-items: stretch; gap: 0; height: var(--shelf-height); overflow: visible; }
  .shelf__head {
    display: flex;
    gap: var(--space-3);
    align-items: center;
    min-width: 0;
    height: var(--shelf-head);
    padding: var(--space-2) var(--space-2) 0 var(--space-4);
  }
  /* The name, its tag and the tab's actions share one height, the small control's. */
  .shelf__head { --shelf-control: var(--control-height-sm); }
  .shelf__name {
    min-width: 0;
    font-size: var(--text-xl);
    font-weight: var(--weight-bold);
    line-height: var(--shelf-control);
    letter-spacing: var(--tracking-title);

    a { display: flex; gap: var(--space-1); align-items: center; height: var(--shelf-control); color: inherit; text-decoration: none; }
    a:hover span { text-decoration: underline; }
    span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
    svg { flex: none; }
  }
  .shelf__head .tag { flex: none; min-height: var(--shelf-control); }
  .shelf__actions .button { min-height: var(--shelf-control); padding-inline: var(--space-4); }
  .shelf__actions svg { display: none; }
  .shelf__actions { display: flex; flex: none; gap: var(--space-2); align-items: center; margin-left: auto; }
  .shelf__tabs { display: flex; gap: var(--space-1); align-items: center; height: calc(var(--shelf-row) - var(--space-2)); overflow-x: auto; scrollbar-width: none; }

  /* The open tab: white, square at the bottom, with concave corners into the shelf. */
  .sections__item--current:has(> .shelf), .top-bar:has(.sections__item--current > .shelf) .sections__item:has(> .shelf):is(:hover, :focus-within) {
    > .shelf { visibility: visible; }
    > .sections__tab { border-radius: var(--radius-md) var(--radius-md) 0 0; background: var(--color-surface); }
    > .sections__tab::before, > .sections__tab::after { content: ""; position: absolute; bottom: 0; width: var(--joint); height: var(--joint); corner-shape: round; }
    > .sections__tab::before { right: 100%; background: radial-gradient(circle at 0 0, transparent var(--joint), var(--color-surface) calc(var(--joint) + 0.5px)); }
    > .sections__tab::after { left: 100%; background: radial-gradient(circle at 100% 0, transparent var(--joint), var(--color-surface) calc(var(--joint) + 0.5px)); }
  }

  .top-bar:has(.sections__item:not(.sections__item--current):is(:hover, :focus-within) > .shelf) .sections__item--current {
    > .shelf { visibility: hidden; }
    > .sections__tab { border-radius: var(--radius-md); background: transparent; }
    > .sections__tab::before, > .sections__tab::after { content: none; }
  }
  .sections__item:not(.sections__item--current) > .shelf { z-index: 2; }

  /* The avatar sits on the band beside a white shelf, so it takes a tint of the band. */
  .top-bar__end .avatar, .button--tint { background: var(--color-ink-8); &:hover { background: var(--color-ink-12); } }
  .button--tint { color: var(--color-ink); }
  .top-bar__switch::after { content: "▾"; margin-left: var(--space-2); font-size: 0.8em; }

  .top-bar__workspace {
    display: flex;
    gap: var(--space-3);
    align-items: center;
    margin-right: var(--space-4);
    font-size: var(--text-lg);
    font-weight: var(--weight-bold);
    letter-spacing: var(--tracking-title);
    text-decoration: none;

    img { width: 2.5rem; height: 2.5rem; }
  }

  .top-bar__end { display: flex; gap: var(--space-3); align-items: center; margin-left: auto; }

  .page-header { display: grid; gap: var(--space-5); }
  .page-header__row { display: flex; flex-wrap: wrap; gap: var(--space-4) var(--space-6); align-items: end; justify-content: space-between; }
  .page-header__text { display: grid; gap: var(--space-2); }
  .page-header__breadcrumb { font-weight: var(--weight-medium); text-decoration: none; }
  .page-header__breadcrumb:hover { text-decoration: underline; }
  .page-header__subline { font-size: var(--text-lg); }
  .page-header__actions { display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: center; }
  .page-header__subline .tag { vertical-align: 0.1em; }

  .page-header__title {
    font-size: var(--text-display);
    line-height: var(--leading-tight);
    letter-spacing: var(--tracking-display);
    overflow-wrap: anywhere;
  }

  .page {
    display: grid;
    gap: var(--space-10);
    max-width: var(--content-width);
    margin-inline: auto;
    padding: var(--space-6) var(--gutter) var(--space-16);
  }

  /* A task page: a close button for the top bar, then its header and form centred in one column. */
  /* Full page, the close button sits on the band; in the modal it sits on the modal, outside the part that scrolls. */
  .focus .masthead { position: relative; }

  .focus, .modal {
    .focus__close { position: absolute; top: var(--gutter); right: var(--gutter); }
    .masthead__inner { padding-top: var(--space-12); }
    .page-header, .page-header__text { justify-items: center; text-align: center; }
    .page-header__subline { max-width: var(--form-width); text-wrap: balance; }
    .page { max-width: calc(var(--form-width) + 2 * var(--space-6) + 2 * var(--gutter)); }
    .page > .cluster { justify-content: center; }
    .page:has(> .plans) { max-width: var(--content-width); }
  }

  /* In the modal the header is short and its title the size of a panel's, so the form starts sooner. */
  .modal {
    .masthead__inner { padding-block: var(--space-10) var(--space-3); }
    .page-header__title { font-size: var(--text-3xl); }
    .page-header__subline { font-size: var(--text-md); }
    .page { padding-top: var(--space-4); }
  }

  /* A panel's frame reloaded as a full page sits on the page's own edge. */
  .page > .panel__body { height: auto; padding: 0; overflow: visible; }

  /* A square filter button; a dot says some filter is on. */
  .page__filter { position: relative; }
  .page__filter-dot { position: absolute; top: var(--space-2); right: var(--space-2); width: 0.5rem; height: 0.5rem; border-radius: 50%; background: var(--color-button); }
  /* Where the filters redraw only the list, the dot follows the sheet's own controls instead: any box ticked, or another sort. */
  form:has(.panel .picker input:checked, .panel option:checked:not([value=""])) .page__filter-dot[hidden] { display: block; }

  .section { display: grid; gap: var(--space-3); align-content: start; }
  .section__title { font-size: var(--text-xl); letter-spacing: var(--tracking-title); }
  .section__head { display: flex; gap: var(--space-4); align-items: center; justify-content: space-between; }

  /* Stacks and clusters for page content */
  .stack { display: grid; gap: var(--space-4); align-content: start; }
  .stack--tight { gap: var(--space-1); }
  .cluster { display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: center; }
  .split { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 22rem), 1fr)); gap: var(--space-6); }
  .prose { max-width: 40rem; }

  /* Phones only: the menu button and, in its menu, the sections and New event. */
  .top-bar__menu, .page__actions { display: none; }

  /* Phones: one row on the band (the workspace, or on an event its name and the tab's actions; then the menu), over the
     open section's pages in a white strip from edge to edge. The sections, New event and the account are in the menu. */
  @media (max-width: 767px) {
    .masthead__inner { gap: var(--space-8); }
    .top-bar, .top-bar:has(.sections__item--current > .shelf) { flex-wrap: wrap; row-gap: var(--space-4); margin-bottom: 0; }
    .top-bar__workspace { font-size: var(--text-md); img { width: 2rem; height: 2rem; } }
    .top-bar:has(.shelf__head) .top-bar__workspace { display: none; }
    .top-bar__end { order: 2; gap: var(--space-2); > :not(.top-bar__menu) { display: none; } }
    .top-bar__menu { display: inline-flex; width: var(--control-height-sm); min-height: var(--control-height-sm); }

    .sections, .sections__item--current, .shelf:has(.shelf__head) { display: contents; }
    .sections__item:not(.sections__item--current), .sections__tab { display: none; }
    .shelf, .shelf__tabs {
      position: static;
      flex: 1 0 100%;
      order: 3;
      height: var(--shelf-row);
      margin-inline: calc(-1 * var(--gutter));
      padding-inline: calc(var(--gutter) - var(--space-3));
      border-radius: 0;
      background: var(--color-surface);
      visibility: visible;
    }
    .shelf .nav-item, .shelf__tabs .nav-item { padding: 0 var(--space-3); }

    /* The band holds only the event's name; its tag and the tab's actions open the page. */
    .shelf__head { flex: 1; order: 1; height: auto; padding: 0; > .tag, .shelf__actions { display: none; } }
    .shelf__name { font-size: var(--text-lg); }
    /* The tab's actions, small, close above what they act on. */
    .page__actions { display: flex; flex-wrap: wrap; gap: var(--space-2); align-items: center; margin-bottom: calc(var(--space-6) - var(--space-10)); }
    /* The tab's own actions as full-height controls, the size of the fields they sit with; Search and filter, square, at the end. */
    .page__actions { flex-wrap: nowrap; .page__filter { margin-left: auto; } }
    .page__actions .button--secondary:not(.page__filter) { display: none; }
    .page__actions:not(:has(.button:not(.button--secondary), .page__filter)) { display: none; }

    /* With nothing under it, the strip meets the page: no band below it, no wave. */
    .masthead:not(:has(.page-header)):has(.shelf__tabs) { > .wave { display: none; } .masthead__inner { padding-bottom: 0; } }
    .page-header__title { font-size: clamp(2.5rem, 12vw, 3.5rem); }
  }
}
