@use '../../sass/variables' as *;
@use '../../sass/mixins.module.scss' as *;

.card-panel {
  --background-color: color-mix(in srgb, var(--md-sys-color-surface), var(--md-sys-color-surface-tint) 17%);

  transition: box-shadow .25s;
  padding: 24px;
  margin: $element-top-margin 0 $element-bottom-margin 0;
  border-radius: 12px;
  @extend .z-depth-1;
  background-color: var(--background-color);
}

// Full-background image cards. Keep the scrim behind interactive content and
// let unsized cards grow when text wraps instead of covering it with actions.
.card.card-image-alt {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: var(--card-image-alt-height, 186px);
  isolation: isolate;
  background-size: cover;
  background-position: var(--card-image-position, center);
  background-repeat: no-repeat;

  // Existing markup with an explicit overlay is supported without doubling it.
  &:not(:has(> .overlay))::before,
  > .overlay {
    content: '';
    position: absolute;
    inset: 0;
    z-index: -1;
    border-radius: inherit;
    pointer-events: none;
    background: var(--card-image-alt-overlay, linear-gradient(
      color-mix(in srgb, var(--md-sys-color-scrim) 30%, transparent),
      color-mix(in srgb, var(--md-sys-color-scrim) 30%, transparent) 60%,
      var(--md-sys-color-scrim) 160%));
  }

  &.gradient {
    &:not(:has(> .overlay))::before,
    > .overlay {
      background: var(--card-image-alt-overlay, linear-gradient(to bottom,
        transparent,
        color-mix(in srgb, var(--md-sys-color-scrim) 40%, transparent) 50%,
        color-mix(in srgb, var(--md-sys-color-scrim) 80%, transparent)));
    }

    > .card-content {
      display: flex;
      flex-direction: column;

      // Auto margins align short content at the bottom without hiding the
      // beginning of long content when a sized card needs to scroll.
      > * { flex-shrink: 0; }
      > :first-child { margin-top: auto; }
    }
  }

  > .card-content {
    position: relative;
    flex: 1 1 auto;
    min-height: 0;
    overflow-wrap: anywhere;
    color: var(--md-sys-color-surface-light);

    > .card-title { font-size: 1.5em; font-weight: 700; line-height: 1.3; margin-bottom: 4px; }

    :is(.btn.text, .btn-flat):not(:disabled):not(.disabled):not([disabled]) {
      color: inherit;
      background-color: transparent;

      &:hover, &:focus {
        background-color: color-mix(in srgb, currentColor 20%, transparent);
      }

      &:focus-visible { outline-color: currentColor; }
    }
  }

  > .card-action {
    position: relative;
    flex: 0 0 auto;
    margin-top: auto;
    padding: 16px 24px;
    color: var(--md-sys-color-on-surface);
    background-color: var(--background-color);
    border-radius: 0 0 12px 12px;
  }
}

.card {
  --background-color: color-mix(in srgb, var(--md-sys-color-surface), var(--md-sys-color-surface-tint) 17%);

  position: relative;
  //margin: $element-top-margin 0 $element-bottom-margin 0;
  background-color: var(--background-color);
  transition: box-shadow .25s;
  border-radius: 12px;
  @extend .z-depth-1;

  .card-title {
    font-size: 24px;
    font-weight: 300;
  }

  // Overlay labels without changing the image or sized card's layout.
  .card-badges {
    position: absolute;
    top: 12px;
    right: 12px;
    z-index: 1;
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-end;
    gap: 6px;
    max-width: calc(100% - 24px);
    pointer-events: none;

    &.left {
      left: 12px;
      right: auto;
      justify-content: flex-start;
    }

    > .badge {
      display: inline-flex;
      align-items: center;
      gap: 4px;
      float: none;
      min-width: 0;
      min-height: 24px;
      max-width: 100%;
      height: auto;
      margin: 0;
      padding: 4px 8px;
      border-radius: 6px;
      font-size: 12px;
      font-weight: 500;
      line-height: 1.4;
      overflow-wrap: anywhere;
      color: var(--card-badge-color, var(--md-sys-color-on-primary-container));
      background-color: var(--card-badge-background, var(--md-sys-color-primary-container));

      // Preserve the badge modifier within the card overlay.
      &.round { border-radius: 999px; }

      // Labels pass clicks through to images; badge links remain usable.
      &:is(a, button) { pointer-events: auto; }
    }
  }

  // Sized cards keep media and actions stable; the remaining space belongs to
  // scrollable content instead of an arbitrary percentage that clips the text.
  &.small, &.medium, &.large {
    display: flex;
    flex-direction: column;
    min-width: 0;

    > .card-image {
      flex: 0 1 var(--card-image-height, 180px);
      height: var(--card-image-height, 180px);
      min-height: 0;
      overflow: hidden;

      > img {
        width: 100%;
        height: 100%;
        object-fit: cover;
        object-position: var(--card-image-position, center);
      }
    }

    > .card-content, > .card-stacked > .card-content {
      flex: 1 1 0;
      min-height: 0;
      max-height: none;
      overflow: auto;
    }

    > .card-action, > .card-stacked > .card-action {
      position: relative;
      flex: 0 0 auto;
      margin-top: auto;
    }
  }

  &.small { height: 300px; }
  &.medium { height: 400px; }
  &.large { height: 500px; }

  // Horizontal Cards
  &.horizontal {
    display: flex;

    &.small, &.medium, &.large {
      flex-direction: row;

      > .card-image {
        flex: 0 0 40%;
        height: 100%;
        max-height: none;
        overflow: hidden;

        > img { width: 100%; height: 100%; object-fit: cover; }
      }

      > .card-stacked { min-width: 0; min-height: 0; }
    }

    .card-image {
      max-width: 50%;

      img {
        border-radius: 12px 0 0 12px;
        max-width: 100%;
        width: auto;
      }
    }

    .card-stacked {
      display: flex;
      flex-direction: column;
      flex: 1;
      position: relative;

      .card-content {
        flex-grow: 1;
      }
    }
  }

  /// Match the card corners when its image is the only section.
  > .card-image:only-child {
    border-radius: inherit;

    > img {
      border-radius: inherit;
    }

    /// Keep the title scrim inside the rounded bottom corners.
    > .card-title.gradient {
      border-bottom-left-radius: inherit;
      border-bottom-right-radius: inherit;
    }
  }

  // Sticky Action Section
  &.sticky-action {
    .card-action {
      z-index: 2;
    }

    .card-reveal {
      z-index: 1;
      padding-bottom: 64px;
    }
  }

  .card-image {
    position: relative;

    // Image background for content
    img {
      display: block;
      border-radius: 12px 12px 0 0;
      position: relative;
      left: 0;
      right: 0;
      top: 0;
      bottom: 0;
      width: 100%;
    }

    .card-title {
      /// Keep image overlay titles light in both themes.
      color: var(--md-sys-color-surface-light);
      position: absolute;
      bottom: 0;
      left: 0;
      max-width: 100%;
      padding: 24px;

      &.gradient {
        display: block;
        top: auto;
        width: 100%;
        padding-top: 64px;
        // The image scrim stays dark in either theme, so its text stays light.
        color: var(--md-sys-color-surface-light);
        background: linear-gradient(to bottom,
          transparent,
          color-mix(in srgb, var(--md-sys-color-scrim) 40%, transparent) 50%,
          color-mix(in srgb, var(--md-sys-color-scrim) 80%, transparent));
        pointer-events: none;

        // Let decorative titles pass clicks to the image while preserving
        // titles that are themselves links or card-reveal activators.
        &:is(a, button, .activator),
        a, button { pointer-events: auto; }
      }
    }

    .activator {
      position: absolute;
      left: 0;
      right: 0;
      top:0;
      bottom: 0;
      cursor: pointer;
    }

    img.activator {
      position: relative;
    }
  }

  .card-content {
    padding: 24px;
    border-radius: 0 0 12px 12px;

    p {
      margin: 0;
    }

    .card-title {
      display: block;
      line-height: 32px;
      margin-bottom: 8px;

      i {
        line-height: 32px;

        // @implement /Users/kzarshenas/Sites/RodeoFx/rodeo_toolkit_2/app/Front/style/scss/component_enhance.scss
        // Sits on the title's baseline with a small gap before the text,
        // instead of floating flush against it mid-line.
        vertical-align: text-bottom;
        margin-right: 6px;
      }

      &.activator {
        cursor: pointer;
      }
    }
  }

  .card-action {
    padding: 0 1.6rem;

    &:last-child {
      border-radius: 0 0 12px 12px;
    }
    // Replaced card links with buttons (Accessibility, @see https://github.com/materializecss/materialize/issues/565)
    a {
      @include btn(
        var(--btn-height),
        var(--btn-border-radius),
        var(--btn-padding),
        var(--btn-padding),
        var(--btn-font-size)
      );

      &:first-child {
        margin-left: -1.6rem;
      }

      &:last-child {
        margin-right: -1.6rem;
      }
    }

    .btn {
      &.filled,
      &.tonal,
      &.elevated,
      &.outlined {
        margin: 0 .26rem 1.6rem 0;
      }
    }
  }

  .card-reveal {
    padding: 24px;
    position: absolute;
    background-color: var(--md-sys-color-surface);
    width: 100%;
    overflow-y: auto;
    left: 0;
    top: 100%;
    height: 100%;
    z-index: 3;
    display: none;

    .card-title {
      cursor: pointer;
      display: block;
    }
  }
}
