/*
 * Responsible for: the look of the folder-tab frame (screens/kit/tab-frame.ts): tabs like the tabs of paper folders on
 * one panel. The chosen tab shares the panel's surface and the panel's outline runs round tab and panel as one shape; the
 * other tab sits behind, darker. The frame marks the chosen tab `data-chosen`, whether a tab is a link or a switch, so one
 * rule serves both.
 *
 * Must never: give the tabs different sizes or positions (choosing a tab changes how it is painted, not where
 * anything is), let the aside move the tab row, animate anything but opacity and transform, or clip the view's content with the tab row.
 */

@layer kit {
  .ia-tabs {
    --ia-tab-h: calc(var(--tap) + var(--s-3));
    /* How far the chosen tab's foot flares out where it meets the panel's top edge. */
    --ia-flare: var(--r-md);
    min-width: 0;
  }

  /* A control of the chosen view that stands beside the tabs. On a phone it is the folder's first line, under the tab row, so the
     tabs never move when it comes or goes; on a laptop it is lifted to the row's end, where it takes no room at all. It is
     hidden, and takes none, where there is nothing to show. */
  .ia-tabs__aside {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    justify-content: flex-end;
    gap: var(--s-2) var(--s-3);
    padding: var(--s-3) var(--s-3) 0;
  }

  .ia-tabs__aside[hidden],
  .ia-tabs__aside:not(:has(> :not([hidden]))) {
    display: none;
  }

  .ia-tabs__row {
    position: relative;
    z-index: 1;
    display: flex;
    height: var(--ia-tab-h);
    /* The panel's top line runs through the row's last pixel, under the tabs. */
    margin-bottom: -1px;
  }

  .ia-tabs__tab {
    position: relative;
    isolation: isolate;
    display: flex;
    flex: 1 1 0;
    flex-direction: column;
    justify-content: center;
    gap: 1px;
    min-width: 0;
    height: var(--ia-tab-h);
    padding: 0 var(--s-2);
    border: 1px solid var(--rule);
    border-radius: var(--r-md) var(--r-md) 0 0;
    background: var(--sunken);
    color: var(--muted);
    font: inherit;
    text-align: start;
    text-decoration: none;
    cursor: pointer;
  }

  .ia-tabs__tab + .ia-tabs__tab {
    margin-left: -1px;
  }

  .ia-tabs__name {
    overflow: hidden;
    font: 600 var(--t-sm) / 1.2 var(--sans);
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .ia-tabs__note {
    font: 500 var(--t-xs) / 1.2 var(--sans);
  }

  /* The tab behind: a lighter wash comes up under the pointer, and the panel's line stays as its foot. */
  .ia-tabs__tab:not([data-chosen])::before {
    content: "";
    position: absolute;
    inset: 0;
    z-index: -1;
    border-radius: inherit;
    background: var(--panel);
    opacity: 0;
    transition: opacity var(--fast) var(--ease);
  }

  @media (hover: hover) {
    .ia-tabs__tab:not([data-chosen]):hover::before {
      opacity: 0.7;
    }

    .ia-tabs__tab:not([data-chosen]):hover {
      color: var(--ink);
    }
  }

  .ia-tabs__tab:not([data-chosen]):active::before {
    opacity: 1;
  }

  /* The chosen tab: the panel's surface, with no line under it, standing in front. */
  .ia-tabs__tab[data-chosen] {
    z-index: 2;
    /* No line under it; the missing pixel is given back as padding so the words sit where they do on the other tab. */
    border-bottom-width: 0;
    padding-bottom: 1px;
    background: var(--panel);
    color: var(--ink);
    box-shadow: inset 0 2px 0 var(--accent);
  }

  .ia-tabs__tab[data-chosen] .ia-tabs__note {
    color: var(--accent);
  }

  /* Its feet: a curve on each side, from the tab's edge down into the panel's top line. */
  .ia-tabs__tab[data-chosen]::before,
  .ia-tabs__tab[data-chosen]::after {
    content: "";
    position: absolute;
    bottom: 0;
    z-index: 1;
    width: calc(var(--ia-flare) + 1px);
    height: calc(var(--ia-flare) + 1px);
    pointer-events: none;
  }

  .ia-tabs__tab[data-chosen]::before {
    left: calc(-1 * (var(--ia-flare) + 1px));
    background: radial-gradient(circle at 0 0, transparent calc(var(--ia-flare) - 0.5px), var(--rule) var(--ia-flare), var(--rule) calc(var(--ia-flare) + 0.5px), var(--panel) calc(var(--ia-flare) + 1px));
  }

  .ia-tabs__tab[data-chosen]::after {
    right: calc(-1 * (var(--ia-flare) + 1px));
    background: radial-gradient(circle at 100% 0, transparent calc(var(--ia-flare) - 0.5px), var(--rule) var(--ia-flare), var(--rule) calc(var(--ia-flare) + 0.5px), var(--panel) calc(var(--ia-flare) + 1px));
  }

  /* The first tab stands on the panel's left edge and the last on a phone fills to the right edge: no foot there. */
  .ia-tabs__tab[data-chosen]:first-child::before,
  .ia-tabs__tab[data-chosen]:last-child::after {
    display: none;
  }

  .ia-tabs__panel {
    position: relative;
    min-width: 0;
    border: 1px solid var(--rule);
    border-radius: 0 0 var(--r-lg) var(--r-lg);
    background: var(--panel);
    box-shadow: var(--shadow-1);
  }

  .ia-tabs__view {
    min-width: 0;
    border-radius: inherit;
  }

  /* A laptop window: the tabs keep their natural width and the panel's top right corner is its own. */
  @media (min-width: 640px) {
    .ia-tabs__tab {
      flex: 0 0 auto;
      min-width: calc(var(--s-8) * 2.5);
      padding: 0 var(--s-5);
    }

    .ia-tabs__name {
      font-size: var(--t-md);
    }

    .ia-tabs__tab[data-chosen]:last-child::after {
      display: block;
    }

    .ia-tabs__panel {
      border-radius: 0 var(--r-lg) var(--r-lg) var(--r-lg);
    }
  }

  @media (min-width: 900px) {
    .ia-tabs__aside {
      position: absolute;
      right: 0;
      bottom: 100%;
      /* Above the row, which spans the whole width and would otherwise cover it and take its presses. */
      z-index: 2;
      flex-wrap: nowrap;
      height: var(--ia-tab-h);
      padding: 0;
    }
  }

  /* Filling the height it is given: the view scrolls inside the panel, and the page does not. */
  .ia-tabs--fill {
    display: flex;
    flex-direction: column;
    height: 100%;
    min-height: 0;
  }

  .ia-tabs--fill .ia-tabs__panel {
    display: flex;
    flex: 1 1 0;
    flex-direction: column;
    min-height: 0;
  }

  .ia-tabs--fill .ia-tabs__view {
    flex: 1 1 0;
    min-height: 0;
    overflow: auto;
    overscroll-behavior: contain;
  }

  .ia-tabs--fill .ia-tabs__view > * {
    min-height: 100%;
  }
}
