/*
 * /watchlist (watchlists/show, _lists, _columns, _map, _table): every company
 * a reader follows, as the strip's heat tiles by sector or as tables that
 * sort. The heading; the named lists as a tab bar, with New list, Rename and
 * Delete at its end; a bar with the search, Add companies and the export;
 * then the page's frame — the serif standfirst, a row of
 * choices (view, grouping, colour or columns), and one `.card` per group.
 *
 * In the table view a group folds on its own <details>; its summary is the
 * card's header row, so the fold reads as the section heading it is, with a
 * chevron at the far end that turns when it is shut. "All together" draws
 * one table and no fold. The tables are one form: a checkbox beside each
 * ticker, a "select all" over each group once the `selection` controller is
 * there, and a bar that stays at the foot of the window while the tables
 * scroll past, with the count and the one button that unfollows.
 */
@layer pages {
  .watchlist {
    display: flex;
    flex-direction: column;
    gap: var(--space-lg);
  }

  .watchlist__head {
    display: flex;
    flex-direction: column;
    gap: var(--space-xs);

    & > .heading {
      margin-block-end: 0;
    }
  }

  .watchlist__lists {
    display: flex;
    flex-wrap: wrap;
    align-items: flex-end;
    gap: var(--space-xs) var(--space-lg);
    border-block-end: var(--rule-width) solid var(--color-rule);
  }

  /* The tabs' own rule is the row's, so tools beside them sit on one line. */
  .watchlist__tabs {
    flex: 1 1 20rem;
    min-inline-size: 0;
    border-block-end: 0;
  }

  .watchlist__tab-count {
    color: var(--color-ink-muted);
    font-weight: var(--weight-regular);
    font-variant-numeric: tabular-nums lining-nums;
  }

  .watchlist__list-tools {
    display: flex;
    align-items: center;
    gap: var(--space-md);
    min-block-size: var(--target-min);
  }

  /* Not positioned: the panel hangs from the tools' end, under the row. */
  .watchlist__list-form > summary {
    list-style: none;
    cursor: pointer;

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

  /* Each panel hangs from what opens it: the list forms from the tools'
     end, Add companies from its own button. */
  .watchlist__list-tools,
  .watchlist__add {
    position: relative;
  }

  .watchlist__list-panel,
  .watchlist__add-panel {
    position: absolute;
    inset-block-start: calc(100% + var(--space-2xs));
    z-index: 60;
    display: grid;
    gap: var(--space-sm);
    inline-size: min(22rem, calc(100vw - 2 * var(--gutter)));
    padding: var(--space-md);
    border: var(--rule-width) solid var(--color-ink);
    background-color: var(--color-field);
    box-shadow: var(--shadow-overlay);
  }

  .watchlist__list-panel {
    inset-inline-end: 0;
    justify-items: start;
  }

  .watchlist__add-panel {
    inset-inline-start: 0;
  }

  .watchlist__list-delete {
    display: contents;
  }

  /* The search, Add companies and the export, on one line while they fit. */
  .watchlist__bar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-sm) var(--space-lg);
  }

  .watchlist__search {
    flex: 0 1 22rem;
    min-inline-size: min(100%, 14rem);
  }

  .watchlist__add-summary {
    list-style: none;

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

  .watchlist__note {
    margin: 0;
    color: var(--color-ink-muted);
    font-size: var(--text-ui-sm);
  }

  .watchlist__page {
    display: flex;
    flex-direction: column;
    gap: var(--space-lg);
  }

  .watchlist__standfirst {
    max-inline-size: var(--measure);
    margin: 0;
    color: var(--color-ink-soft);
    font-family: var(--font-prose);
    font-size: var(--text-lead);
    line-height: var(--leading-lead);
    font-variant-numeric: tabular-nums lining-nums;
  }

  .watchlist__empty {
    margin: 0;
    color: var(--color-ink-muted);
    font-size: var(--text-ui);
  }

  .watchlist__controls {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0 var(--space-2xl);
  }

  /* A choice whose options do not say what they choose: the map's colour,
     the table's columns. On a phone it wraps, its label first. */
  .watchlist__choice {
    display: inline-flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0 var(--space-sm);
    min-inline-size: 0;
    max-inline-size: 100%;

    & > .segmented {
      flex: 0 1 auto;
      min-inline-size: 0;
    }
  }

  /* The export sits at the bar's far end: the dashed link, as the strip has
     it, or the words and the lock. */
  .watchlist__export {
    display: inline-flex;
    align-items: center;
    gap: var(--space-2xs);
    margin-inline-start: auto;
    font-size: var(--text-ui-sm);
    white-space: nowrap;

    &:is(a) {
      text-decoration-style: dashed;
    }

    &:is(span) {
      color: var(--color-ink-muted);
    }
  }

  .watchlist__screens {
    display: inline-flex;
    margin-inline-start: auto;
    min-inline-size: 0;
    max-inline-size: 100%;
  }

  /* A select on the choices' row is as tall as the segmented control. */
  .watchlist__screen-select {
    inline-size: auto;
    max-inline-size: min(16rem, 100%);
    block-size: 2rem;
    padding-block: 0;
    font-size: var(--text-ui-sm);
  }

  .watchlist__tiles {
    display: flex;
    flex-wrap: wrap;
    gap: 1px;
  }

  .watchlist__fold-summary {
    list-style: none;

    &:is(summary) {
      cursor: pointer;
    }

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

  .watchlist__chevron {
    flex: none;
    align-self: center;
    margin-inline-start: auto;
    transition: rotate 150ms ease;

    .watchlist__fold:not([open]) & {
      rotate: -90deg;
    }
  }

  .watchlist__followed {
    color: var(--color-ink-muted);
  }

  .watchlist__selection-form {
    display: flex;
    flex-direction: column;
    gap: var(--space-lg);
  }

  .watchlist__select-all {
    font-size: var(--text-ui-sm);
  }

  /* The checkbox and the ticker share the pinned first cell, so ticking a
     row never scrolls away from the name it ticks. */
  .watchlist__pick {
    display: inline-flex;
    align-items: center;
    gap: var(--space-xs);

    & input {
      inline-size: 1rem;
      block-size: 1rem;
      margin: 0;
      accent-color: var(--color-accent);
      cursor: pointer;

      @media (pointer: coarse) {
        inline-size: 1.5rem;
        block-size: 1.5rem;
      }
    }
  }

  .watchlist__selection {
    position: sticky;
    inset-block-end: 0;
    z-index: 1;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: space-between;
    gap: var(--space-xs) var(--space-md);
    padding-block: var(--space-xs);
    border-block-start: var(--rule-heavy);
    background-color: var(--color-surface);
  }

  .watchlist__selection-actions {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: var(--space-xs) var(--space-md);
  }

  /* "Add to list": a button that opens, upward from the bar at the foot of
     the window, a menu of the reader's lists. */
  .watchlist__list-menu {
    position: relative;
  }

  .watchlist__list-menu-summary {
    gap: var(--space-2xs);
    list-style: none;

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

  .watchlist__list-menu-items {
    position: absolute;
    inset-block-end: calc(100% + var(--space-2xs));
    inset-inline-start: 0;
    z-index: 60;
    display: grid;
    min-inline-size: 12rem;
    max-block-size: 16rem;
    overflow-y: auto;
    padding: var(--space-2xs);
    border: var(--rule-width) solid var(--color-ink);
    background-color: var(--color-field);
    box-shadow: var(--shadow-overlay);
  }

  .watchlist__list-menu-item {
    min-block-size: 2.25rem;
    padding-inline: var(--space-sm);
    border: 0;
    background-color: transparent;
    color: var(--color-ink);
    font: inherit;
    font-size: var(--text-ui-sm);
    text-align: start;
    cursor: pointer;

    &:hover,
    &:focus-visible {
      background-color: var(--color-surface-raised);
    }

    @media (pointer: coarse) {
      min-block-size: var(--target-min);
    }
  }

  .watchlist__selection-count {
    color: var(--color-ink-muted);
    font-size: var(--text-ui-sm);
  }
}
