// An interactive card's link or button, when it is the card's only content. The inactive
// spotlight's border is rendered after the children, so it doesn't count.
@card-container-only-action: ~":is(a[href], button:enabled):first-child:not(:has(~ :not(.wds-card-container__border)))";

// The hover and pressed states, as mixins so CardWidget can apply them to the card when only its
// title is hovered or pressed. Applied to the card's root.
.wds-card-container-hover-state() {
  // The active spotlight keeps its resting shadow, and its hairline border strengthens.
  &.wds-card-container--active {
    --CardContainer-border-color: color-mix(in srgb, var(--color-border-neutral) 75%, transparent);
  }

  // The inactive spotlight's fill takes its hover value, at the same 25% strength as at rest.
  &.wds-card-container--inactive {
    --CardContainer-background-color: color-mix(
      in srgb,
      var(--color-background-neutral-hover) 25%,
      transparent
    );
  }
}

.wds-card-container-pressed-state() {
  // The active spotlight keeps its resting shadow, and its border is at full strength.
  &.wds-card-container--active {
    --CardContainer-border-color: var(--color-border-neutral);
  }

  // The inactive spotlight's fill takes its pressed value, at the same 25% strength as at rest.
  &.wds-card-container--inactive {
    --CardContainer-background-color: color-mix(
      in srgb,
      var(--color-background-neutral-active) 25%,
      transparent
    );
  }
}

.wds-card-container {
  // Resolved at the nearest theme root, below.
  --CardContainer-shadow: var(--CardContainer-theme-shadow, none);
  --CardContainer-background-color: var(--color-background-screen);
  --CardContainer-border-color: var(--color-border-neutral);
  --CardContainer-border-radius: var(--radius-large);
  --CardContainer-padding-block: var(--size-8);
  --CardContainer-padding-inline: var(--size-8);

  background-color: var(--CardContainer-background-color);
  border: 1px solid var(--CardContainer-border-color);
  border-radius: var(--CardContainer-border-radius);
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
  padding: var(--CardContainer-padding-block) var(--CardContainer-padding-inline);
  width: 100%;

  @media (--screen-md) {
    --CardContainer-padding-block: var(--size-16);
    --CardContainer-padding-inline: var(--size-16);
  }

  // Set through the padding variables, so the `inactive` spotlight's padding still composes.
  // Length zeroes, as the inactive spotlight adds `1px` to these in `calc()`, which a unitless `0`
  // would invalidate.
  &--padding-none {
    --CardContainer-padding-block: 0px;
    --CardContainer-padding-inline: 0px;

    // Full-bleed content has to follow the card's rounded corners. `overflow` leaves the card's
    // own shadow alone, which `clip-path` would cut off.
    overflow: clip;
  }

  &--active {
    --CardContainer-background-color: var(--color-background-elevated);
    // The raised surface's shadow already separates it, so its border is a softer hairline.
    --CardContainer-border-color: color-mix(in srgb, var(--color-border-neutral) 25%, transparent);

    box-shadow: var(--CardContainer-shadow);
  }

  &--inactive {
    --CardContainer-background-color: color-mix(
      in srgb,
      var(--color-background-neutral) 25%,
      transparent
    );

    // The dashed stroke is drawn inside the card instead of on a border, so the stroke isn't
    // clipped with `padding="none"`. The padding takes the border's 1px, so content stays put.
    border-width: 0;
    padding: calc(var(--CardContainer-padding-block) + 1px)
      calc(var(--CardContainer-padding-inline) + 1px);
    position: relative;
  }

  &--interactive {
    // Only when the stretched link applies, so other cards keep their containing block. Spelled
    // out, as `:has()` can't nest the only-action selector's own `:has()`.
    &:has(> :is(a[href], button:enabled):first-child):not(
        :has(> * + :not(.wds-card-container__border))
      ) {
      position: relative;
    }

    // When a link or button is the card's only content, it stretches over the padding, so the
    // whole card hovers and clicks as it. Rounded like the card, so its corners outside the card's
    // radius don't click.
    > @{card-container-only-action}::before {
      border-radius: var(--CardContainer-border-radius);
      content: "";
      inset: 0;
      position: absolute;
    }

    // Without padding, the card clips at its edges, so the only link or button's focus ring is
    // drawn inside it, rounded like the card.
    &.wds-card-container--padding-none > @{card-container-only-action} {
      --ring-outline-offset: calc(-1 * var(--ring-outline-width));

      border-radius: var(--CardContainer-border-radius);
    }
  }

  // An `interactive` card follows its direct child link or button, such as a link that fills the
  // card, so it only looks clickable where it can be clicked. Keyboard focus on the child shows the
  // hover state too. A card without a spotlight has no hover or pressed state.
  &--interactive:has(
      > a[href]:hover,
      > button:enabled:hover,
      > a[href]:focus-visible,
      > button:enabled:focus-visible
    ) {
    .wds-card-container-hover-state();
  }

  // Pressed state, while the same direct child is pressed. After the hover rule, so pressed wins
  // while the pointer is down.
  &--interactive:has(> a[href]:active, > button:enabled:active) {
    .wds-card-container-pressed-state();
  }

  // Same treatment as ListItem's inactive spotlight: CSS `dashed` borders have a browser-chosen
  // dash length, so the 6px/6px dashes are an SVG stroke along the card's edge.
  &__border {
    // A size container, so the stroke's corner radius can be capped by the card's height. An SVG
    // `rect` clamps `rx` and `ry` separately, which turns the corners of a card shorter than twice
    // the radius into ellipses, where `border-radius` keeps them round.
    container-type: size;
    inset: 0;
    pointer-events: none;
    position: absolute;

    svg {
      display: block;
      height: 100%;
      width: 100%;
    }

    rect {
      --CardContainer-border-dash-size: calc(var(--size-12) * 0.5);
      --CardContainer-border-stroke-radius: calc(
        min(var(--CardContainer-border-radius), 50cqh, 50cqw) - 0.5px
      );

      fill: none;
      height: calc(100% - 1px);
      rx: var(--CardContainer-border-stroke-radius);
      ry: var(--CardContainer-border-stroke-radius);
      stroke: var(--color-border-neutral);
      stroke-dasharray: var(--CardContainer-border-dash-size) var(--CardContainer-border-dash-size);
      width: calc(100% - 1px);
      x: 0.5px;
      y: 0.5px;
    }
  }
}

/**
 * The active spotlight's resting shadow, only designed for the light themes. Set on every theme
 * root, as a nested ThemeProvider renders its own, so a card follows its nearest theme: a dark or
 * green provider inside a light one turns the shadow off.
 *
 * There is no shadow or elevation token in @transferwise/neptune-tokens, so the value is
 * component-scoped for now, the way PromoCard scopes `--Card-box-shadow`. It is the "Card Active
 * shadow" effect style from Figma: `#0e0f0c` at 4%, kept literal so it doesn't drift with a
 * theme's tokens. Collapses to a token reference once one is minted.
 */
.np-theme-personal {
  --CardContainer-theme-shadow: 0 2px 12px rgb(14 15 12 / 4%), 0 2px 32px rgb(14 15 12 / 4%);

  &:is(
    .np-theme-personal--dark,
    .np-theme-personal--bright-green,
    .np-theme-personal--forest-green,
    .np-theme-business--dark,
    .np-theme-business--bright-green,
    .np-theme-business--forest-green,
    .np-theme-platform--forest-green
  ) {
    --CardContainer-theme-shadow: none;
  }
}
