/*
  tabs.css
  ---------------------------------------------------------------------
  A segmented content switcher - visually tab-like, built from radio
  inputs and :has(), zero JavaScript.

  ACCESSIBILITY NOTE: this is NOT ARIA tabs. Real ARIA tabs require
  role="tab"/aria-selected/role="tabpanel" and arrow-key navigation
  between tabs, none of which a radio group provides for free. Do NOT
  add role="tablist" to .tabs__list - that promises an interaction
  model this component doesn't implement, which is worse than no
  role at all. Left as native radio buttons, a screen reader announces
  exactly what's there: a set of options with a checked state - which
  is honest and reasonably usable. If true ARIA tabs are required,
  this is one of the few components where a small (~20-30 line) JS
  enhancement is the right call instead.

  WHAT CHANGED AND WHY
    The previous version matched tabs to panels by hardcoded ID:
    .tabs:has(#tab-1:checked) #panel-1, written out to #tab-6. That
    baked two limits into the framework stylesheet - a six-tab
    ceiling, and one global set of ID names, so a second tabset on the
    same page fought the first over #panel-1. Matching by POSITION
    instead removes both. No ID appears anywhere below.

    This is the same technique form-steps.css uses to match step
    indicators to panels. If you have read that file, you already know
    this one.

  GRACEFUL DEGRADATION
    The tab-switching logic is entirely :has()-driven, so it's gated
    behind @supports selector(:has(*)). Without it: every .tabs__panel
    shows at once as ordinary sequential content, and .tabs__list stays
    hidden rather than showing tab-like labels that would silently do
    nothing. A browser without :has() still gets every word of content.

  Requires: css/reset.css, css/core.css

  HTML STRUCTURE: see tabs.html

    <section class="tabs">
      <div class="tabs__list" role="radiogroup" aria-label="Product information">
        <input type="radio" name="product-tabs" id="pt-1" class="tabs__input" checked />
        <label for="pt-1" class="tabs__label">Overview</label>

        <input type="radio" name="product-tabs" id="pt-2" class="tabs__input" />
        <label for="pt-2" class="tabs__label">Details</label>
      </div>

      <div class="tabs__panels">
        <section class="tabs__panel stack">...</section>
        <section class="tabs__panel stack">...</section>
      </div>
    </section>

  The input IDs are yours to name - they only pair each input with its
  own label, and only have to be unique on the page. The panels need
  no IDs at all now. Two tabsets on one page just need different
  `name` values so the radio groups stay independent.

  TAB LIMIT
    Matching position in CSS means enumerating positions, so the rules
    below are written out to 10 tabs. Past that a tab strip is the
    wrong component anyway - reach for an accordion or a select.
  ---------------------------------------------------------------------
*/

@layer components {
  .tabs { --tab-indicator: var(--color-accent); }

  .tabs__list {
    display: flex;
    gap: 0;
    border-block-end: 1px solid var(--color-border);
    flex-wrap: wrap;
  }

  .tabs__input {
    position: absolute;
    opacity: 0;
    pointer-events: none;
  }

  .tabs__label {
    display: inline-block;
    padding: 0.75rem 1.1rem;
    cursor: pointer;
    color: var(--color-muted);
    border-block-end: 2px solid transparent;
    margin-block-end: -1px;
    font-weight: 600;
    transition: color var(--transition-fast), border-color var(--transition-fast);
  }

  .tabs__label:hover { color: var(--color-text); }

  .tabs__input:focus-visible + .tabs__label {
    outline: 3px solid var(--color-accent);
    outline-offset: 2px;
    border-radius: var(--radius);
  }

  .tabs__input:checked + .tabs__label {
    color: var(--color-text);
    border-block-end-color: var(--tab-indicator);
  }

  /* Baseline (no :has() support): every panel shows at once, as
     ordinary sequential content, and the tab strip stays hidden - a
     row of labels that LOOK interactive but silently do nothing would
     be worse than no tab strip at all. A browser without :has() still
     gets every word of content, just not the switching interaction. */
  .tabs__panel {
    display: block;
    padding-block-start: var(--space-6);
  }

  .tabs__list {
    display: none;
  }

  @supports selector(:has(*)) {
    .tabs__list {
      display: flex;
    }

    .tabs__panel {
      display: none;
    }

    /*
      The Nth checked input shows the Nth panel. :nth-of-type() on
      .tabs__input counts inputs only, so the interleaved labels don't
      shift the count; :nth-child() inside .tabs__panels is safe
      because that wrapper holds nothing but panels.

      Everything is scoped to .tabs, so each tabset answers only for
      its own inputs and its own panels.
    */
    .tabs:has(.tabs__list .tabs__input:nth-of-type(1):checked)  .tabs__panels > :nth-child(1),
    .tabs:has(.tabs__list .tabs__input:nth-of-type(2):checked)  .tabs__panels > :nth-child(2),
    .tabs:has(.tabs__list .tabs__input:nth-of-type(3):checked)  .tabs__panels > :nth-child(3),
    .tabs:has(.tabs__list .tabs__input:nth-of-type(4):checked)  .tabs__panels > :nth-child(4),
    .tabs:has(.tabs__list .tabs__input:nth-of-type(5):checked)  .tabs__panels > :nth-child(5),
    .tabs:has(.tabs__list .tabs__input:nth-of-type(6):checked)  .tabs__panels > :nth-child(6),
    .tabs:has(.tabs__list .tabs__input:nth-of-type(7):checked)  .tabs__panels > :nth-child(7),
    .tabs:has(.tabs__list .tabs__input:nth-of-type(8):checked)  .tabs__panels > :nth-child(8),
    .tabs:has(.tabs__list .tabs__input:nth-of-type(9):checked)  .tabs__panels > :nth-child(9),
    .tabs:has(.tabs__list .tabs__input:nth-of-type(10):checked) .tabs__panels > :nth-child(10),
    /* Nothing checked (an author forgot the `checked` attribute) - show
       the first panel rather than an empty component. */
    .tabs:not(:has(.tabs__input:checked)) .tabs__panels > :first-child {
      display: block;
    }
  }
}
