// ============================================================
// Tabs Component
// .cyber-tabs (tab strip) + .cyber-tab (one tab) + panels.
// <div class="cyber-tabs" role="tablist" aria-label="Sections">
//   <button class="cyber-tab" role="tab" aria-selected="true" aria-controls="p1">One</button>
//   <button class="cyber-tab" role="tab" aria-selected="false" tabindex="-1">Two</button>
// </div>
// <div class="cyber-tabs__panel" role="tabpanel" id="p1" tabindex="0">…</div>
// The selected tab is `aria-selected="true"` or `.cyber-tab--active`.
// A11y: one Tab stop for the strip, arrow keys move between tabs (roving
// tabindex), panels not shown carry the `hidden` attribute.
// ============================================================

@use "kit";

// Reduced-motion overrides, shared by the media query and data-motion="reduce"
@mixin -reduced-motion {
  .cyber-tab,
  .cyber-tab::after {
    transition: none;
  }

  .cyber-tabs__panel--active,
  .cyber-tabs__panel[role="tabpanel"] {
    animation: none;
  }
}

@layer components {
  .cyber-tabs {
    display: flex;
    flex-wrap: wrap;
    gap: 2px;
    border-bottom: 1px solid var(--color-border-default);

    // Tab list wrapper, when the strip and its panels share one parent
    &__list {
      display: flex;
      flex-wrap: wrap;
      gap: 2px;
      margin: 0;
      padding: 0;
      list-style: none;
    }

    // Single row that scrolls sideways
    &--scrollable {
      @include kit.scrollbar(4px);

      flex-wrap: nowrap;
      overflow-x: auto;

      .cyber-tabs__list {
        flex-wrap: nowrap;
      }
    }

    // Small screens scroll instead of wrapping into a wall of tabs
    @media (width <= 768px) {
      @include kit.scrollbar(4px);

      flex-wrap: nowrap;
      overflow-x: auto;

      .cyber-tabs__list {
        flex-wrap: nowrap;
      }
    }

    &__panels {
      padding: var(--space-md) 0;
    }

    // Panels show when active, or when they are a visible `role="tabpanel"`
    &__panel {
      display: none;

      &--active,
      &[role="tabpanel"]:not([hidden]) {
        display: block;
        animation: cyber-tab-in var(--duration-normal) var(--ease-out-expo);
      }

      &:focus-visible {
        @include kit.focus-ring;
      }
    }

    // --------------------------------------------------------
    // Layout variants
    // --------------------------------------------------------

    &--vertical {
      flex-flow: column nowrap;
      border-bottom: 0;
      overflow-x: visible;
      border-inline-end: 1px solid var(--color-border-default);

      .cyber-tabs__list {
        flex-direction: column;
      }

      .cyber-tab {
        justify-content: flex-start;

        // Marker runs down the trailing edge instead of along the bottom
        &::after {
          width: 3px;
          height: auto;
          transform: scaleY(0);
          inset: 0 0 0 auto;
        }

        &:dir(rtl)::after {
          inset: 0 auto 0 0;
        }
      }
    }

    // Separate cut-corner pills instead of a joined strip
    &--pills {
      gap: var(--space-2xs);
      border-bottom: 0;

      .cyber-tabs__list {
        gap: var(--space-2xs);
      }

      .cyber-tab {
        --frame-cut: 8px;
        --frame-border-color: var(--color-border-strong, var(--cyber-chrome-600));
        --frame-bg: var(--color-bg-secondary);

        @include kit.cut-frame;

        background: transparent;

        @include kit.hover {
          --frame-border-color: var(--cyber-chrome-400);

          background: transparent;
        }

        &::after {
          display: none;
        }

        &--active,
        &[aria-selected="true"] {
          --frame-border-color: var(--tab-color);
          --frame-bg: color-mix(in srgb, var(--tab-color) 14%, var(--color-bg-secondary));

          background: transparent;
        }
      }
    }

    &--fullwidth {
      .cyber-tabs__list {
        width: 100%;
      }

      .cyber-tab {
        flex: 1 0 auto;
      }
    }
  }

  // --------------------------------------------------------
  // One tab
  // --------------------------------------------------------

  .cyber-tab {
    --tab-color: var(--color-accent, var(--cyber-cyan-500));

    @include kit.label-text(var(--text-sm), 0.06em);

    position: relative;
    display: inline-flex;
    flex: none;
    gap: var(--space-2xs);
    align-items: center;
    justify-content: center;
    min-height: var(--control-height-md, 2.75rem);
    padding: var(--space-3xs) var(--space-md);
    border: 0;
    text-decoration: none;
    white-space: nowrap;
    color: var(--color-text-secondary);
    background: var(--color-bg-secondary);
    transition:
      color var(--duration-fast) ease,
      background-color var(--duration-fast) ease;
    cursor: pointer;

    @include kit.hover {
      color: var(--color-text-primary);
      background: var(--color-bg-tertiary);
    }

    // Glowing marker, grows from the centre when the tab is selected
    &::after {
      content: "";
      position: absolute;
      height: 3px;
      background: var(--tab-color);
      box-shadow: 0 0 0.6rem var(--tab-color);
      transform: scaleX(0);
      transition: transform var(--duration-normal) var(--ease-out-expo);
      inset: auto 0 0;
    }

    &:focus-visible {
      @include kit.focus-ring(var(--tab-color), -3px);

      z-index: 1;
      color: var(--color-text-primary);
    }

    // Listed with :hover so a selected tab keeps its colour under the pointer
    &--active,
    &--active:hover,
    &[aria-selected="true"],
    &[aria-selected="true"]:hover {
      color: var(--tab-color);
      background: var(--color-bg-tertiary);

      &::after {
        transform: none;
      }
    }

    &:disabled,
    &[aria-disabled="true"],
    &--disabled {
      opacity: 50%;
      cursor: not-allowed;
      pointer-events: none;
    }

    svg,
    &__icon {
      flex: none;
      width: 1em;
      height: 1em;
    }

    // Count next to the label
    &__badge {
      min-width: 1.25rem;
      padding: 0 0.3rem;
      border: 1px solid color-mix(in srgb, var(--tab-color) 70%, transparent);
      font-family: var(--font-mono);
      font-size: kit.$text-xs;
      line-height: 1.5;
      letter-spacing: 0;
      text-align: center;
      color: var(--tab-color);
      background: color-mix(in srgb, var(--tab-color) 14%, transparent);
    }

    &--magenta {
      --tab-color: var(--cyber-magenta-500);
    }

    &--yellow {
      --tab-color: var(--cyber-yellow-500);
    }

    &--green {
      --tab-color: var(--cyber-green-500);
    }
  }

  @keyframes cyber-tab-in {
    from {
      opacity: 0%;
      transform: translateY(4px);
    }
  }

  @media (prefers-reduced-motion: reduce) {
    @include -reduced-motion;
  }

  [data-motion="reduce"] {
    @include -reduced-motion;
  }

  @media (prefers-contrast: more) {
    .cyber-tabs {
      border-color: var(--cyber-chrome-400);
    }

    .cyber-tab {
      color: var(--color-text-primary);
    }
  }

  // Selection must not rest on colour alone when colours are forced
  @media (forced-colors: active) {
    .cyber-tab {
      border: 1px solid ButtonBorder;

      &--active,
      &[aria-selected="true"] {
        border-color: Highlight;
        border-bottom-width: 4px;
      }
    }
  }
}
