:host(:not(:is([hydrated], .hydrated))) {
  @apply not-hydrated;
}


mds-card-content,
mds-card-footer,
mds-card-header,
mds-card-media {
  &:not(:is([hydrated], .hydrated)) {
    @apply not-hydrated;
  }
}


/**
 * @prop --mds-card-background: Public. Sets the background color of the card and of its regions (`mds-card-header`, `mds-card-media`, `mds-card-content`, `mds-card-footer`)
 * @prop --mds-card-color: Public. Sets the text color of the card, inherited by its regions, `--magma-text-default` by default: set it together with `--mds-card-background`, so the text keeps reading on the surface
 * @prop --mds-card-gap: Public. Sets the space between the regions of the card, `--magma-rhythm-title-content` by default; the footer keeps at least `--magma-rhythm-content-actions` from the region before it
 * @prop --mds-card-padding: Public. Sets the inset of the card, shared by its regions, `--magma-rhythm-inset` by default; the media region reaches out of it to the edges it touches
 */

/* one inset shared by the regions, which pad nothing of their own: the title of a card stands
   nearer to its content than to the edge (#1189, rhythm/inset and rhythm/shared-inset) */

/* the card paints its own surface, so it sets the text of that surface too, the pair the contrast
   gate measures: its content no longer takes the colour of what the card stands on, as the ink of
   the band of an introduction (#1327, surfaces/intro-band). A colour set on the host from outside
   still wins over :host */

:host {
  --mds-card-gap: var(--magma-rhythm-title-content, 12px);
  --mds-card-padding: var(--magma-rhythm-inset, 24px);
  --mds-card-background: rgb(var(--magma-surface-raised, var(--surface-neutral-raised, 248 248 248)));
  --mds-card-color: rgb(var(--magma-text-default, var(--text-neutral-default, 5 5 5)));

  background-color: var(--mds-card-background);
  border-radius: var(--magma-radius-2xl, var(--radius-squircle-2xl, 40px));
  box-shadow: var(--shadow-md-sharp, 0 0 1px 1px rgb(0 0 0 / 0.06), 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -1px rgb(0 0 0 / 0.06));
  color: var(--mds-card-color, rgb(var(--magma-text-default, var(--text-neutral-default, 5 5 5))));
  container-name: mds-card;
  container-type: inline-size;
  corner-shape: var(--magma-corner-shape, squircle);
  display: grid;
  min-height: --spacing(400);
  overflow: hidden;
}

/* rhythm/media-bleed (#1189): the media reaches the edges of the card it touches, and stands
   --mds-card-gap from the other regions. The sides it touches follow the grid areas of the
   layout (vertical here, horizontal in the container query below), never the order of the slots:
   both edges of the inline axis always, the top when no header stands above it, the bottom when
   nothing stands below it */

.layout:not(.layout--disabled) ::slotted([slot='media']) {
  grid-area: media;
  margin-inline: calc(-1 * var(--mds-card-padding, var(--magma-rhythm-inset, 24px)));
}

.layout--cfm:not(.layout--disabled) ::slotted([slot='media']),
.layout--cm:not(.layout--disabled) ::slotted([slot='media']),
.layout--fm:not(.layout--disabled) ::slotted([slot='media']),
.layout--m:not(.layout--disabled) ::slotted([slot='media']) {
  margin-block-start: calc(-1 * var(--mds-card-padding, var(--magma-rhythm-inset, 24px)));
}

.layout--hm:not(.layout--disabled) ::slotted([slot='media']),
.layout--m:not(.layout--disabled) ::slotted([slot='media']) {
  margin-block-end: calc(-1 * var(--mds-card-padding, var(--magma-rhythm-inset, 24px)));
}

.layout:not(.layout--disabled) ::slotted([slot='header']) {
  grid-area: header;
}

.layout:not(.layout--disabled) ::slotted([slot='content']) {
  grid-area: content;
}

/* at the end of its area: without a content some layouts give the footer the row that
   stretches, and its actions rose under the header (#1040) */

.layout:not(.layout--disabled) ::slotted([slot='footer']) {
  align-self: end;
  grid-area: footer;
}

/* rhythm/content-actions (#1189): the row of actions acts on the whole card, so the footer stands
   from the region before it as far as the fields of a form from each other; a larger
   --mds-card-gap still wins */

.layout:not(.layout--disabled, .layout--f) ::slotted([slot='footer']) {
  margin-block-start: max(0px, var(--magma-rhythm-content-actions, 24px) - var(--mds-card-gap, var(--magma-rhythm-title-content, 12px)));
}

.layout {
  display: grid;
  gap: var(--mds-card-gap, var(--magma-rhythm-title-content, 12px));
  grid-template-columns: repeat(auto-fit, minmax(1px, 100%));
  padding: var(--mds-card-padding, var(--magma-rhythm-inset, 24px));
}

.layout--cfhm:not(.layout--disabled) {
  grid-template-areas:
    'header'
    'media'
    'content'
    'footer';
  grid-template-rows:
    min-content
    min-content
    1fr
    min-content;
}

.layout--chm:not(.layout--disabled) {
  grid-template-areas:
    'header'
    'media'
    'content';
  grid-template-rows:
    min-content
    min-content
    1fr;
}

.layout--cfm:not(.layout--disabled) {
  grid-template-areas:
    'media'
    'content'
    'footer';
  grid-template-rows:
    min-content
    1fr
    min-content;
}

.layout--fhm:not(.layout--disabled) {
  grid-template-areas:
    'header'
    'media'
    'footer';
  grid-template-rows:
    min-content
    1fr
    min-content;
}

.layout--cfh:not(.layout--disabled) {
  grid-template-areas:
    'header'
    'content'
    'footer';
  grid-template-rows:
    min-content
    1fr
    min-content;
}

.layout--hm:not(.layout--disabled) {
  grid-template-areas:
    'header'
    'media';
  grid-template-rows:
    min-content
    1fr;
}

.layout--fm:not(.layout--disabled) {
  grid-template-areas:
    'media'
    'footer';
  grid-template-rows:
    1fr
    min-content;
}

.layout--cm:not(.layout--disabled) {
  grid-template-areas:
    'media'
    'content';
  grid-template-rows:
    min-content
    1fr;
}

.layout--fh:not(.layout--disabled) {
  grid-template-areas:
    'header'
    'footer';
  grid-template-rows:
    min-content
    1fr;
}

.layout--ch:not(.layout--disabled) {
  grid-template-areas:
    'header'
    'content';
  grid-template-rows:
    min-content
    1fr;
}

.layout--cf:not(.layout--disabled) {
  grid-template-areas:
    'content'
    'footer';
  grid-template-rows:
    1fr
    min-content;
}

.layout--h:not(.layout--disabled) {
  grid-template-areas: 'header';
  grid-template-rows: 1fr;
}

.layout--m:not(.layout--disabled) {
  grid-template-areas: 'media';
  grid-template-rows: 1fr;
}

.layout--c:not(.layout--disabled) {
  grid-template-areas: 'content';
  grid-template-rows: 1fr;
}

.layout--f:not(.layout--disabled) {
  grid-template-areas: 'footer';
  grid-template-rows: 1fr;
}

@container mds-card (width > 30em) {
  .layout--cfhm:not(.layout--disabled) {
    grid-template-areas:
      'media header'
      'media content'
      'media footer';
    grid-template-columns: minmax(--spacing(6000), 1fr) 2fr;
    grid-template-rows:
      min-content
      1fr
      min-content;
  }

  .layout--chm:not(.layout--disabled) {
    grid-template-areas:
      'media header'
      'media content'
      'media content';
    grid-template-columns: minmax(--spacing(6000), 1fr) 2fr;
    grid-template-rows:
      min-content
      1fr
      1fr;
  }

  .layout--fhm:not(.layout--disabled) {
    grid-template-areas:
      'media header'
      'media footer';
    grid-template-columns: minmax(--spacing(6000), 1fr) 2fr;
    grid-template-rows:
      min-content
      1fr;
  }

  .layout--cfm:not(.layout--disabled) {
    grid-template-areas:
      'media content'
      'media footer';
    grid-template-columns: minmax(--spacing(6000), 1fr) 2fr;
    grid-template-rows:
      1fr
      min-content;
  }

  /* one row: a second, empty one put a gap under the content */

  .layout--cm:not(.layout--disabled) {
    grid-template-areas: 'media content';
    grid-template-columns: minmax(--spacing(6000), 1fr) 2fr;
    grid-template-rows: 1fr;
  }

  /* rhythm/media-bleed: in the start column the media touches the top, the bottom and the start */

  .layout--cfhm:not(.layout--disabled) ::slotted([slot='media']),
  .layout--chm:not(.layout--disabled) ::slotted([slot='media']),
  .layout--fhm:not(.layout--disabled) ::slotted([slot='media']),
  .layout--cfm:not(.layout--disabled) ::slotted([slot='media']),
  .layout--cm:not(.layout--disabled) ::slotted([slot='media']) {
    margin-block: calc(-1 * var(--mds-card-padding, var(--magma-rhythm-inset, 24px)));
    margin-inline: calc(-1 * var(--mds-card-padding, var(--magma-rhythm-inset, 24px))) 0;
  }
}

/* Theme preference. Background and elevation now come from the semantic surface
 * layer (--magma-surface-*, spec 6.1): it flips per mode at :root and inherits
 * into the shadow DOM, so this component needs no per-mode dark refinement. The
 * former dark override (a --tone-* background re-set and/or a fake outline / glow
 * that faked elevation on the old flat-black canvas) is intentionally gone. */

/* Contrast preference. The component reflects the resolved mode from <html> onto
 * its host as pref-contrast (see subscribePreference in @common/preference); these
 * plain shadow selectors refine for high contrast. `more` is the explicit choice;
 * `system` and no controller follow the OS via @media. Cross-browser, no :host-context. */

:host([pref-contrast='more']) {
  box-shadow: var(--shadow-ring-2, 0 0 0 2px rgb(var(--tone-neutral-01, 5 5 5) / 0.3)), var(--shadow-lg, 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1));
}

@media (prefers-contrast: more) {
  :host([pref-contrast='system']),
  :host(:not([pref-contrast])) {
    box-shadow: var(--shadow-ring-2, 0 0 0 2px rgb(var(--tone-neutral-01, 5 5 5) / 0.3)), var(--shadow-lg, 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1));
  }
}

