/* stylelint-disable length-zero-no-unit */

@use '../../mixins/media';

// ============================================================
// Variant: Portal — main-nav only, no title, no local-nav
// Variant: Jobs — main-nav + local-nav, no title
// ============================================================

:root:has(post-header [slot='main-nav']):not(:has(post-header [slot='title'])) {
  --post-global-header-expanded-height: 64px;
  --post-local-header-reduced-height: 0px;
  --post-local-header-expanded-min-height: var(--post-main-navigation-height, 0px);

  @include media.only(desktop) {
    --post-global-header-expanded-height: 72px;
    --post-global-header-reduced-height: 24px;
    --post-local-header-reduced-height: 56px;
    --post-main-navigation-height: var(--post-local-header-reduced-height);
    --post-logo-reduced-offset: 6px;
  }
}

post-header:has([slot='main-nav']):not(:has([slot='title'])) {
  // Hide local header on tablet & mobile (no title present)
  @include media.max(desktop) {
    --post-local-header-display: none;
  }

  // Portal: main-nav only
  &:not(:has([slot='local-nav'])) {
    --post-local-header-grid-template: 'main-nav' auto / 1fr;
  }

  // Jobs: main-nav + local-nav
  &:has([slot='local-nav']) {
    --post-local-header-grid-template: 'main-nav local-nav' auto / minmax(0, 1fr) auto;
  }
}

// ============================================================
// Variant: Microsite — main-nav + title, optional local-nav
// ============================================================

:root:has(post-header [slot='main-nav']):has(post-header [slot='title']) {
  --post-global-header-expanded-height: 64px;
  --post-local-header-reduced-height: var(--post-local-header-expanded-height);

  @include media.only(mobile) {
    --post-local-header-expanded-min-height: 48px;
  }

  @include media.only(tablet) {
    --post-local-header-expanded-min-height: 56px;
  }

  @include media.only(desktop) {
    --post-global-header-expanded-height: 72px;
    --post-local-header-expanded-min-height: 112px;
    --post-global-header-reduced-height: 24px;
    --post-main-navigation-height: 56px;
    --post-logo-reduced-offset: 6px;
  }
}

post-header:has([slot='main-nav']):has([slot='title']) {
  --post-local-header-align-items: end;

  // Microsite without local-nav
  &:not(:has([slot='local-nav'])) {
    --post-local-header-grid-template: 'title' 1fr 'main-nav' auto / 1fr;
  }

  // Microsite with local-nav
  &:has([slot='local-nav']) {
    --post-local-header-grid-template: 'title local-nav' 1fr 'main-nav main-nav' auto /
      minmax(0, 1fr) auto;
  }
}

// ============================================================
// Variant: One-pager — title only, no main-nav, optional local-nav
// ============================================================

:root:has(post-header):not(:has(post-header [slot='main-nav'])) {
  --post-global-header-expanded-height: 64px;
  --post-local-header-expanded-min-height: 56px;

  @include media.only(desktop) {
    --post-global-header-expanded-height: 72px;
    --post-local-header-expanded-min-height: 64px;
  }
}

// One-pager header: when there is no main-nav and no title,
// ensure the local header collapses to zero height
:root:has(post-header):not(:has(post-header [slot='main-nav'])):not(
  :has(post-header [slot='title'])
) {
  --post-local-header-expanded-min-height: 0px;
}

post-header:not(:has([slot='main-nav'])) {
  // Hide burger button (no main navigation)
  --post-burger-button-display: none;

  // Hide local header if there is no title either
  &:not(:has([slot='title'])) {
    --post-local-header-display: none;
  }

  // One-pager without local-nav
  &:has([slot='title']):not(:has([slot='local-nav'])):not(:has([slot='side-nav'])) {
    --post-local-header-grid-template: 'title' 1fr / 1fr;
  }

  // One-pager with local-nav
  &:has([slot='title']):has([slot='local-nav']):not(:has([slot='side-nav'])) {
    --post-local-header-grid-template: 'title local-nav' 1fr / minmax(0, 1fr) auto;
  }

  // Application header with side-nav, without local-nav
  &:has([slot='title']):has([slot='side-nav']):not(:has([slot='local-nav'])) {
    --post-local-header-grid-template: 'side-nav title' 1fr / auto minmax(0, 1fr);
  }

  // Application header with side-nav and local-nav
  &:has([slot='title']):has([slot='side-nav']):has([slot='local-nav']) {
    --post-local-header-grid-template: 'side-nav title local-nav' 1fr / auto minmax(0, 1fr) auto;
  }
}

// ===========================================================
// Application header: logo only, no global nav slots
// ============================================================

:root:has(post-header):not(:has(post-header > [slot='audience'])):not(
  :has(post-header > [slot='global-nav-primary'])
):not(:has(post-header > [slot='global-nav-secondary'])):not(
  :has(post-header > [slot='language-menu'])
):not(:has(post-header > [slot='post-login'])) {
  --post-global-header-expanded-height: var(--_post-initial-global-header-height);
  --post-global-header-reduced-height: 32px;

  @include media.only(desktop) {
    --post-logo-expanded-offset: 10px;
    --post-logo-reduced-offset: 10px;
  }
  @include media.only(tablet) {
    --post-logo-expanded-offset: 12px;
    --post-logo-reduced-offset: 12px;
  }
  @include media.only(mobile) {
    --post-logo-expanded-offset: 8px;
    --post-logo-reduced-offset: 8px;
  }
}

// ============================================================
// Shared / base custom properties
// ============================================================

:root:has(post-header) {
  // Opt out of scroll anchoring so the browser doesn't adjust scrollY when the
  // sticky header repositions itself on scroll. Removing this breaks shrink-on-scroll
  // in frameworks that flush layout synchronously between scroll events (e.g. Angular).
  overflow-anchor: none;

  // --- Expanded height ---
  --post-local-header-expanded-height: var(--post-local-header-expanded-min-height);
  --post-header-expanded-height: calc(
    var(--post-global-header-expanded-height) + var(--post-local-header-expanded-height)
  );

  // --- Reduced height ---
  --post-global-header-reduced-height: var(--post-global-header-expanded-height);
  --post-local-header-reduced-height: var(--post-local-header-expanded-height);
  --post-header-reduced-height: calc(
    var(--post-global-header-reduced-height) + var(--post-local-header-reduced-height)
  );

  // --- Total height (dynamic based on scroll position) ---
  --post-global-header-height: max(
    var(--post-global-header-expanded-height) - var(--post-header-scroll-top, 0px),
    var(--post-global-header-reduced-height)
  );
  --post-local-header-height: max(
    var(--post-local-header-expanded-height) - var(--post-header-scroll-top, 0px),
    var(--post-local-header-reduced-height)
  );
  --post-header-height: calc(var(--post-global-header-height) + var(--post-local-header-height));

  // --- Logo offset ---
  --post-logo-expanded-offset: 0px;
  --post-logo-reduced-offset: var(--post-logo-expanded-offset);

  // Override when mobile menu is open and page is scrolled
  &:has([data-menu-extended]) {
    --post-header-height: (
      var(--post-global-header-expanded-height) + var(--post-local-header-expanded-height)
    );
  }
}
