// =============================================================================
// Effect primitives
// Glass is a spectrum: 3 named levels, each with a fallback.
// =============================================================================

@use 'sass:map';

// ---------------------------------------------------------------- elevation --
/// Two-layer shadow: contact + ambient. The colour comes from the theme so the
/// light variant can use a slate shadow instead of pure black.
$elevation: (
  1: (
    0 1px 2px rgb(var(--ui-shadow-rgb) / 0.24),
  ),
  2: (
    0 1px 2px rgb(var(--ui-shadow-rgb) / 0.24),
    0 8px 24px -16px rgb(var(--ui-shadow-rgb) / 0.72),
  ),
  3: (
    0 2px 4px rgb(var(--ui-shadow-rgb) / 0.28),
    0 16px 48px -20px rgb(var(--ui-shadow-rgb) / 0.8),
  ),
) !default;

/// Applies a step from the elevation scale.
///
/// @param {Number} $level - 1, 2 or 3.
@mixin elevation($level: 2) {
  @if not map.has-key($elevation, $level) {
    @error 'Unknown elevation level: #{$level}';
  }

  box-shadow: map.get($elevation, $level);
}

// -------------------------------------------------------------------- glass --
/// Frosted surface.
///
/// The blur is painted on a `::before` layer, never on the element itself.
/// `backdrop-filter` turns the element into a stacking context and a containing
/// block for `position: fixed` / `position: absolute` descendants. Bootstrap's
/// `.dropdown-menu` (`z-index: 1000`), `.offcanvas` and `.tooltip` all rely on
/// escaping that box, so blurring the element directly traps them and breaks
/// the paint order.
///
/// The element must be a positioning context (`relative`, `sticky` or `fixed`).
/// `.navbar` already is; the `.glass*` utilities set it explicitly.
///
/// @param {String} $level - `subtle`, `default` or `strong`.
/// @param {String} $token - Token name, without the `--ui-` prefix.
/// @param {Boolean} $layer - `true` paints the blur on `::before` (default).
///   Pass `false` for leaf elements such as `.modal-backdrop`, where the extra
///   layer is dead weight.
@mixin glass($level: 'default', $token: 'surface', $layer: true) {
  $config: (
    'subtle': (
      alpha: 0.42,
      blur: 12px,
    ),
    'default': (
      alpha: 0.62,
      blur: 20px,
    ),
    'strong': (
      alpha: 0.78,
      blur: 28px,
    ),
  );

  @if not map.has-key($config, $level) {
    @error 'Unknown glass level: #{$level}';
  }

  $alpha: map.get(map.get($config, $level), alpha);
  $blur: map.get(map.get($config, $level), blur);

  // Leaf element: no descendants to trap, paint in place.
  @if not $layer {
    background: rgb(var(--ui-#{$token}-rgb) / #{$alpha});
    backdrop-filter: blur($blur) saturate(var(--ui-glass-saturate));
    -webkit-backdrop-filter: blur($blur) saturate(var(--ui-glass-saturate));

    @supports not (backdrop-filter: blur(1px)) {
      background: rgb(var(--ui-#{$token}-rgb) / 0.98);
    }
  }

  // Container: paint the blur on a pseudo-element so the element itself stays
  // out of the stacking context / containing block business.
  background: transparent;

  &::before {
    position: absolute;
    inset: 0;
    z-index: -1;
    pointer-events: none;
    content: '';
    background: rgb(var(--ui-#{$token}-rgb) / #{$alpha});
    border-radius: inherit;
    backdrop-filter: blur($blur) saturate(var(--ui-glass-saturate));
    -webkit-backdrop-filter: blur($blur) saturate(var(--ui-glass-saturate));

    @supports not (backdrop-filter: blur(1px)) {
      background: rgb(var(--ui-#{$token}-rgb) / 0.98);
    }
  }
}

// ------------------------------------------------------------------ surface --
/// Opaque surface. Used by components that must not be transparent (modals,
/// dropdowns) but still need the border and radius.
///
/// @param {Number} $alpha - Background opacity.
@mixin surface($alpha: 0.98) {
  background: rgb(var(--ui-surface-rgb) / #{$alpha});
  border: 1px solid var(--ui-border);
  border-radius: var(--bs-border-radius);
}

// ----------------------------------------------------------------- hairline --
/// 1px light line on the top edge. Fakes a lit bevel.
@mixin hairline-top($opacity: 1) {
  box-shadow: inset 0 1px 0 rgb(var(--ui-heading-rgb) / #{0.06 * $opacity});
}

/// Full inner border, brighter at the top. Requires `position: relative`.
@mixin bevel($radius: inherit) {
  &::after {
    position: absolute;
    inset: 0;
    pointer-events: none;
    content: '';
    border-radius: $radius;
    box-shadow: inset 0 1px 0 var(--ui-hairline-strong);
  }
}

// -------------------------------------------------------------------- focus --
@mixin focus-ring($color: 'accent', $width: 3px) {
  outline: none;
  box-shadow: 0 0 0 $width rgb(var(--ui-#{$color}-rgb) / 0.24);
}

// ------------------------------------------------------------------ motion --
@mixin transition($properties...) {
  transition-duration: var(--ui-duration);
  transition-timing-function: var(--ui-ease);
  transition-property: $properties;

  @media (prefers-reduced-motion: reduce) {
    transition-duration: 1ms;
  }
}
