@import (reference) "../CardContainer/CardContainer.less";

// Above this header width a long title wraps beside the action corner. At or below it, the title
// only shares a line with the actions while it fits on one line, so it keeps room to read.
@wds-card-widget-actions-stack-width: 360px;

.wds-card-widget {
  --CardWidget-inset: var(--size-8);
  --CardWidget-header-spacing: var(--size-8);
  --CardWidget-actions-gap: var(--size-8);

  // The header's title row is at least as tall as the 32px action buttons, so cards side by side
  // line up their titles and content whether or not their headers have actions, or have stacked
  // them. The actions wrap under the title when they don't fit beside it.
  &__header {
    align-items: center;
    column-gap: var(--size-24);
    container-type: inline-size;
    display: flex;
    flex-wrap: wrap;
    padding-inline-start: var(--CardWidget-inset);
    row-gap: var(--size-8);
  }

  // Fills the rest of the card's height, so content can be laid out against its bottom edge, such
  // as a footer pushed down with `margin-top: auto`. Children stack like the card's own.
  &__content {
    display: flex;
    flex: 1 0 auto;
    flex-direction: column;
  }

  // On the content rather than the header, as an inactive card's decorative border comes after the
  // header, so a header-only widget's header isn't its last child.
  &__header + &__content {
    margin-top: var(--CardWidget-header-spacing);
  }

  &__title {
    align-items: center;
    color: var(--color-content-secondary);
    display: grid;
    flex: 1 1 0;
    margin: 0;
    min-height: var(--size-32);

    @container (width <= @wds-card-widget-actions-stack-width) {
      flex-basis: auto;
    }
  }

  // The title is a `Link`, but keeps the title's typography and colour rather than the link
  // text style, and flows inline so the chevron can follow its last word.
  &__title-link.np-link {
    color: inherit;
    display: inline;
    font: inherit;
    // The title is a grid, which would stretch the link to the full width up to the actions, so
    // its hover and click area would reach past the text.
    justify-self: start;
    letter-spacing: inherit;
    // `Link` renders a button when there is no `href`, which would otherwise centre the title.
    text-align: start;
    text-decoration: none;

    &:hover,
    &:focus-visible {
      text-decoration: underline;
    }

    // Inline, so the icons follow the title's last word: the chevron, and the new-tab icon `Link`
    // adds for `target="_blank"`, which `Link` itself lays out as a flex box. Nested so it
    // outweighs `Link`'s own `.np-link .tw-icon`, which loads later.
    .tw-icon {
      display: inline-block;
      vertical-align: text-bottom;
    }

    .wds-card-widget__title-chevron {
      white-space: nowrap;

      .tw-icon {
        margin: 0;
      }
    }
  }

  // Beside the title, the growing title pushes the actions to the inline end. When they wrap onto
  // their own row under the title, they start at the inline start, under it.
  &__actions {
    // Stays in the top corner when a long title wraps onto more lines.
    align-self: start;
    align-items: center;
    display: flex;
    flex: none;
    gap: var(--CardWidget-actions-gap);
  }

  // With a header, the content is inset further from both inline edges.
  &--with-header &__content {
    padding-inline: var(--CardWidget-inset);
  }

  // The card's hover and pressed states follow the title link alone, as it leads to the widget's
  // own screen. Hovering or pressing the rest of the card, or its actions, leaves it at rest.
  // Keyboard focus on the link shows the hover state too, so keyboard users see the card react.
  &:has(.wds-card-widget__title-link:hover, .wds-card-widget__title-link:focus-visible) {
    .wds-card-container-hover-state();
  }

  // After the hover rule, so pressed wins while the pointer is down.
  &:has(.wds-card-widget__title-link:active) {
    .wds-card-container-pressed-state();
  }
}
