@use 'sass:math';

/* stylelint-disable max-nesting-depth */

.card-outer {
  @include context-menu-button;

  background: var(--newtab-background-color-secondary);
  border-radius: var(--border-radius-medium);
  display: inline-block;
  height: $card-height;
  margin-inline-end: var(--space-xxlarge);
  position: relative;
  width: 100%;

  &:is(:focus):not(.placeholder) {
    @include ds-focus;

    transition: none;
  }

  &:hover {
    box-shadow: none;
    transition: none;
  }

  &.placeholder {
    background: transparent;

    .card-preview-image-outer,
    .card-context {
      display: none;
    }
  }

  .card {
    border-radius: var(--border-radius-medium);
    box-shadow: $shadow-card;
    height: 100%;
  }

  > a {
    color: inherit;
    display: block;
    height: 100%;
    outline: none;
    position: absolute;
    width: 100%;

    &:is(:focus) {
      .card {
        @include ds-focus;
      }
    }

    &:is(.active, :focus) {
      .card {
        @include fade-in-card;
      }

      .card-title {
        color: var(--newtab-primary-action-background);
      }
    }
  }

  &:is(:hover, :focus, .active):not(.placeholder) {
    @include context-menu-button-hover;

    outline: none;

    .card-title {
      color: var(--newtab-primary-action-background);
    }

    .alternate ~ .card-host-name {
      display: none;
    }

    .card-host-name.alternate {
      display: block;
    }
  }

  .card-preview-image-outer {
    background-color: var(--newtab-element-secondary-color);
    border-radius: var(--border-radius-medium) var(--border-radius-medium) 0 0;
    height: $card-preview-image-height;
    overflow: hidden;
    position: relative;

    &::after {
      border-bottom: 1px solid var(--newtab-card-hairline-color);
      inset-block-end: 0;
      content: '';
      position: absolute;
      width: 100%;
    }

    .card-preview-image {
      background-position: center;
      background-repeat: no-repeat;
      background-size: cover;
      height: 100%;
      opacity: 0;
      transition: opacity 1s $photon-easing;
      width: 100%;

      &.loaded {
        opacity: 1;
      }
    }
  }

  .card-details {
    padding: var(--space-large) var(--space-large) var(--space-medium);
  }

  .card-text {
    max-height: 4 * $card-text-line-height + $card-title-margin;
    overflow: hidden;

    &.no-host-name,
    &.no-context {
      max-height: 5 * $card-text-line-height + $card-title-margin;
    }

    &.no-host-name.no-context {
      max-height: 6 * $card-text-line-height + $card-title-margin;
    }

    &:not(.no-description) .card-title {
      max-height: 3 * $card-text-line-height;
      overflow: hidden;
    }
  }

  .card-host-name {
    color: var(--newtab-text-secondary-color);
    font-size: var(--font-size-small);
    overflow: hidden;
    padding-block-end: var(--space-xsmall);
    text-overflow: ellipsis;
    text-transform: uppercase;
    white-space: nowrap;
  }

  .card-host-name.alternate { display: none; }

  .card-title {
    font-size: inherit;
    font-weight: var(--heading-font-weight);
    line-height: $card-text-line-height;
    margin: 0 0 var(--space-xxsmall);
    word-wrap: break-word;
  }

  .card-description {
    font-size: var(--font-size-small);
    line-height: $card-text-line-height;
    margin: 0;
    overflow: hidden;
    word-wrap: break-word;
  }

  .card-context {
    inset-block-end: 0;
    color: var(--newtab-text-secondary-color);
    display: flex;
    font-size: var(--font-size-small);
    inset-inline-start: 0;
    padding: var(--space-small) var(--space-large);
    position: absolute;
  }

  .card-context-icon {
    fill: var(--newtab-text-secondary-color);
    height: 22px;
    margin-inline-end: var(--space-xsmall);
  }

  .card-context-label {
    flex-grow: 1;
    line-height: 22px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}

.normal-cards {
  .card-outer {
    // Wide layout styles
    @media (min-width: $break-point-widest) {
      $line-height: 23px;

      height: $card-height-large;

      .card-preview-image-outer {
        height: $card-preview-image-height-large;
      }

      .card-details {
        padding: var(--space-medium) var(--space-large);
      }

      .card-text {
        max-height: 6 * $line-height + $card-title-margin;
      }

      .card-host-name {
        font-size: var(--font-size-small);
        padding-block-end: var(--space-xsmall);
      }

      .card-title {
        font-size: var(--font-size-large);
        line-height: $line-height;
        margin-block-end: 0;
      }

      .card-text:not(.no-description) {
        .card-title {
          max-height: 3 * $line-height;
        }
      }

      .card-description {
        font-size: inherit;
        line-height: $line-height;
      }

      .card-context {
        inset-block-end: var(--space-xsmall);
        font-size: inherit;
        line-height: $line-height;
      }
    }
  }
}

.compact-cards {
  $card-detail-vertical-spacing: 12px;
  $card-title-font-size: var(--font-size-small);

  .card-outer {
    height: $card-height-compact;

    .card-preview-image-outer {
      height: $card-preview-image-height-compact;
    }

    .card-details {
      padding: var(--space-medium) var(--space-large);
    }

    .card-host-name {
      line-height: 10px;
    }

    .card-text {
      .card-title,
      &:not(.no-description) .card-title {
        font-size: $card-title-font-size;
        line-height: $card-title-font-size + 1;
        max-height: $card-title-font-size + 5;
        overflow: hidden;
        padding: 0 0 var(--space-xsmall);
        text-overflow: ellipsis;
        white-space: nowrap;
      }
    }

    .card-description {
      display: none;
    }

    .card-context {
      $icon-size: 16px;
      $container-size: 32px;

      background-color: var(--newtab-background-color-secondary);
      border-radius: var(--border-radius-medium);
      clip-path: inset(-1px -1px $container-size - ($card-height-compact - $card-preview-image-height-compact - 2 * $card-detail-vertical-spacing));
      height: $container-size;
      width: $container-size;
      padding: var(--space-small);
      // The -1 at the end is so both opacity borders don't overlap, which causes bug 1629483
      // Bug 1967304 - Large number // 91px
      inset-block-start: $card-preview-image-height-compact - math.div($container-size, 2) - 1; // stylelint-disable-line declaration-property-value-disallowed-list
      inset-inline-end: var(--space-medium);
      inset-inline-start: auto;

      &::after {
        border: 1px solid var(--newtab-card-hairline-color);
        border-bottom: 0;
        border-radius: var(--border-radius-medium);
        content: '';
        position: absolute;
        height: math.div($container-size + 2, 2);
        width: $container-size + 2;
        inset-block-start: calc(var(--space-xxsmall) * -1);
        inset-inline-start: calc(var(--space-xxsmall) * -1);
      }

      .card-context-icon {
        margin-inline-end: 0;
        height: $icon-size;
        width: $icon-size;

        &.icon-bookmark-added {
          fill: $bookmark-icon-fill;
        }

        &.icon-download {
          fill: $download-icon-fill;
        }

        &.icon-pocket {
          fill: $pocket-icon-fill;
        }
      }

      .card-context-label {
        display: none;
      }
    }
  }

  @media not all and (min-width: $break-point-widest) {
    .hide-for-narrow {
      display: none;
    }
  }
}
