@use 'sass:math';

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

$top-sites-size: $grid-unit-small;
$top-sites-border-radius: var(--border-radius-medium);
$top-sites-icon-border-radius: var(--border-radius-small);
$rich-icon-size: 96px;
$default-icon-wrapper-size: 40px;
$default-icon-size: 40px;
$default-icon-offset: 6px;
$half-base-gutter: math.div($base-gutter, 2);
$hover-transition-duration: 150ms;
$letter-fallback-color: $white;
$calculated-max-width-medium: $break-point-medium + $card-width;
$calculated-max-width-large: $break-point-large + $card-width;
$calculated-max-width-twice-large: $break-point-large + 2 * $card-width;
$calculated-max-width-widest: $break-point-widest + $card-width;
$calculated-max-width-twice-widest: $break-point-widest + 2 * $card-width;

.top-sites-list {
  &:not(.dnd-active) {
    .top-site-outer:is(.active, :focus, :hover) {
      @include wallpaper-contrast-fix;

      // Ideally this would just match button --button-background-color-hover.
      // However, --button-background-color-hover uses currentColor.
      // We cannot change the shortcut's color to match,
      // so we just use --button-text-color-hover directly.
      background: color-mix(in srgb, var(--button-text-color-hover) 14%, transparent);

      .tile {
        box-shadow: 0 2px 8px 0 var(--newtab-section-card-box-shadow-color);
      }

      .icon-pin-small {
        opacity: 1;
      }

      .lightWallpaper &,
      .darkWallpaper & {
        background: color-mix(in srgb, var(--button-text-color-hover) 14%, transparent);

        @media (prefers-contrast) {
          background-color: var(--background-color-box);
        }
      }
    }
  }

  list-style: none;
  margin: 0 calc(var(--space-large) * -1);
  padding: 0;
  display: grid;
  grid-template-columns: repeat(3, auto);
  justify-content: center; // Center shortcuts when fewer than max number of sites per row
  row-gap: var(--space-small);

  @media (min-width: $break-point-layout-variant) {
    grid-template-columns: repeat(6, auto);
  }

  @media (min-width: $break-point-widest) {
    grid-template-columns: repeat(8, auto);
  }

  a {
    text-decoration: none;
  }

  // Two columns
  @media (max-width: $break-point-medium) {
    > :nth-child(2n+1) {
      @include context-menu-open-middle;
    }

    > :nth-child(2n) {
      @include context-menu-open-left;
    }
  }

  // Four columns
  @media (min-width: $break-point-medium) and (max-width: $break-point-large) {
    :nth-child(4n) {
      @include context-menu-open-left;
    }
  }

  @media (min-width: $break-point-medium) and (max-width: $calculated-max-width-medium) {
    :nth-child(4n+3) {
      @include context-menu-open-left;
    }
  }

  // Six columns
  @media (min-width: $break-point-large) and (max-width: $calculated-max-width-twice-large) {
    :nth-child(6n) {
      @include context-menu-open-left;
    }
  }

  @media (min-width: $break-point-large) and (max-width: $calculated-max-width-large) {
    :nth-child(6n+5) {
      @include context-menu-open-left;
    }
  }

  // Eight columns
  @media (min-width: $break-point-widest) and (max-width: $calculated-max-width-twice-widest) {
    :nth-child(8n) {
      @include context-menu-open-left;
    }
  }

  @media (min-width: $break-point-widest) and (max-width: $calculated-max-width-widest) {
    :nth-child(8n+7) {
      @include context-menu-open-left;
    }
  }

  .hide-for-narrow {
    display: none;
  }

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

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

  @media (min-width: $break-point-widest) {
    .hide-for-narrow {
      display: inline-block;
    }
  }
}

.discovery-stream.ds-layout.ds-layout-topsites {
  display: flex;
  justify-content: center;

  .top-sites-list {
    padding-block-start: 0;
  }
}

// container for drop zone
.top-site-outer {
  width: 120px;
  margin-block-end: 0;
  border-radius: var(--border-radius-large);
  display: inline-block;

  .top-site-inner {
    position: relative;

    > a {
      padding: var(--space-large) var(--space-large) var(--space-xsmall);
      color: inherit;
      display: block;
      outline: none;
    }
  }

  &:hover, &:focus {
    .context-menu-button {
      opacity: 1;
      transition: opacity 200ms;

      @include wallpaper-contrast-fix;
    }

    .tile {
      background-color: var(--newtab-background-color-secondary);
    }

    .icon-pin-small {
      opacity: 1;
    }
  }

  &:not(:hover, :active, .active) {
    @include wallpaper-contrast-fix;
  }

  &.active .context-menu-button {
    opacity: 1;
    background-color: var(--newtab-button-active-background);
  }

  .tile {
    background: var(--newtab-background-card);
    border-radius: var(--border-radius-large);
    box-shadow: $shadow-card;
    height: calc(var(--size-item-large) * 2);
    width: calc(var(--size-item-large) * 2);
    margin: 0 auto;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    text-transform: uppercase;
    font-size: var(--font-size-xxlarge);
    color: var(--newtab-contextual-text-secondary-color);
    cursor: pointer;

    .icon-wrapper {
      border-radius: var(--border-radius-small);
      width: $default-icon-wrapper-size;
      height: $default-icon-wrapper-size;
      display: flex;
      align-items: center;
      justify-content: center;
      overflow: hidden;
      position: relative;

      &.letter-fallback {
        border-radius: var(--border-radius-circle);

        &::before {
          content: attr(data-fallback);
          font-size: var(--font-size-xlarge);
          transform: none;
          position: static;
        }
      }
    }

    .top-site-icon {
      background-color: transparent;
      border-radius: var(--border-radius-medium);
      background-position: center;
      background-repeat: no-repeat;
      position: absolute;
    }
  }

  .context-menu-button {
    background-image: url('chrome://global/skin/icons/more.svg');
    border: 0;
    border-radius: var(--border-radius-small);
    cursor: pointer;
    fill: var(--newtab-button-text);
    -moz-context-properties: fill;
    height: var(--size-item-medium);
    width: var(--size-item-medium);
    inset-inline-end: 0;
    inset-block-start: var(--space-medium);
    position: absolute;
    opacity: 0;

    &:hover {
      background-color: var(--newtab-button-hover-background);

      &:active {
        background-color: var(--newtab-button-active-background);
      }
    }

    &:focus-visible {
      background-color: var(--newtab-button-focus-background);
      outline: var(--focus-outline); // Backup for when wallpaper is not set
      opacity: 1;

      .lightWallpaper &,
      .darkWallpaper & {
        &::after {
          content: '';
          position: absolute;
          inset: 0;
          border-radius: var(--border-radius-small);
          outline: var(--focus-outline);
          color-scheme: initial; // Override wallpaper-contrast-fix for focus outline color
        }
      }
    }
  }

  .context-menu {
    inset-block-start: calc(var(--space-large) + var(--space-xlarge));
  }

  .icon-pin-small {
    inset-block-start: var(--space-small);
    inset-inline-start: var(--space-small);
    opacity: 0;
    position: absolute;
  }

  .top-site-button {
    padding: var(--space-large) var(--space-large) var(--space-small);

    &:focus + div .context-menu-button {
      opacity: 1;
    }

    &:focus .tile {
      .lightWallpaper &,
      .darkWallpaper & {
        outline: var(--focus-outline);
        color-scheme: initial;
      }
    }
  }

  .title {
    color: var(--newtab-contextual-text-primary-color);
    padding-block-start: var(--space-small);
    font: caption;
    text-align: center;
    position: relative;

    .icon {
      margin-inline-end: var(--space-xxsmall);
      fill: currentColor;
    }

    .sponsored-label {
      color: var(--newtab-contextual-text-secondary-color);
      font-size: var(--font-size-xsmall);
    }

    &:not(.sponsored) .sponsored-label {
      display: none;
    }

    .search-topsite {
      margin-inline-end: var(--space-xxsmall);
      fill: currentColor;
      background-size: var(--icon-size-xsmall);
      height: var(--icon-size-xsmall);
      width: var(--icon-size-xsmall);
      background-position: center;
      background-repeat: no-repeat;
      background-color: transparent;
      position: static;
      display: inline-block;
      color: var(--icon-color);
      box-shadow: none;
    }
  }

  .rich-icon {
    background-size: cover;
    height: 100%;
    width: 100%;
    position: absolute;
    inset-inline-start: 0;
    inset-block-start: 0;
  }

  .default-icon,
  .search-topsite {
    background-size: $default-icon-size;
    height: $default-icon-wrapper-size;
    width: $default-icon-wrapper-size;
    align-items: center;
    display: flex;
    justify-content: center;
    font-size: var(--font-size-xlarge);

    &[data-fallback]::before {
      content: attr(data-fallback);
    }
  }

  .search-topsite {
    background-image: url('chrome://global/skin/icons/search-glass.svg');
    background-size: 16px;
    background-color: var(--newtab-primary-action-background);
    border-radius: var(--border-radius-circle);
    -moz-context-properties: fill;
    fill: var(--newtab-primary-element-text-color);
    box-shadow: $shadow-card;
    transition: background-size $hover-transition-duration,
      bottom $hover-transition-duration,
      inset-inline-end $hover-transition-duration,
      height $hover-transition-duration,
      width $hover-transition-duration;
    height: 32px;
    width: 32px;
    // Bug 1967304 - $variable
    inset-block-end: -$default-icon-offset; // stylelint-disable-line declaration-property-value-disallowed-list
    inset-inline-end: -$default-icon-offset; // stylelint-disable-line declaration-property-value-disallowed-list
  }

  &.placeholder {
    .tile {
      box-shadow: $shadow-card;
      cursor: default;
    }
  }

  &.add-button {
    .tile {
      background-color: var(--button-background-color);

      .icon-wrapper {
        background-image: url('chrome://global/skin/icons/plus-20.svg');
        background-size: cover;
        background-repeat: no-repeat;
        height: 20px;
        width: 20px;
        fill: var(--icon-color);
        -moz-context-properties: fill;
      }
    }

    &:hover {
      .tile {
        background-color: var(--newtab-background-color-secondary);
        color-scheme: initial;
      }
    }
  }

  &.search-shortcut {
    .rich-icon {
      background-color: $white;
    }
  }

  .edit-button {
    background-image: url('chrome://global/skin/icons/edit.svg');
  }

  &.dragged {
    .tile {
      *,
      &::before {
        display: none;
      }
    }

    .title {
      visibility: hidden;
    }
  }
}

.edit-topsites-wrapper {
  .top-site-inner > .top-site-button > .tile {
    border: 1px solid var(--newtab-border-color);
  }

  .modal {
    box-shadow: $shadow-secondary;
    inset-inline-start: 0;
    margin: 0 auto;
    max-height: calc(100% - 40px);
    position: fixed;
    inset-inline-end: 0;
    inset-block-start: var(--space-xxlarge);
    width: $wrapper-default-width;

    @media (min-width: $break-point-medium) {
      width: $wrapper-max-width-medium;
    }

    @media (min-width: $break-point-large) {
      width: $wrapper-max-width-large;
    }
  }
}

.topsite-form {
  $form-width: 300px;
  $form-spacing: 32px;

  .section-title {
    font-size: inherit;
    margin: 0 0 var(--space-large);
  }

  .form-input-container {
    max-width: $form-width + 3 * $form-spacing + $rich-icon-size;
    margin: 0 auto;
    padding: var(--space-xxlarge);

    .top-site-outer {
      pointer-events: none;
    }
  }

  .search-shortcuts-container {
    max-width: 700px;
    margin: 0 auto;
    padding: var(--space-xxlarge);

    > div {
      margin-inline-end: calc(var(--space-xxlarge) * -1);
    }

    .top-site-outer {
      margin-inline-start: 0;
      margin-inline-end: var(--space-xxlarge);
    }
  }

  .top-site-outer {
    padding: 0;
    margin: var(--space-xlarge) 0 0;
    margin-inline-start: var(--space-xxlarge);
  }

  .fields-and-preview {
    display: flex;
  }

  label {
    font-size: $section-title-font-size;
  }

  .form-wrapper {
    width: 100%;

    .field {
      position: relative;

      .icon-clear-input {
        position: absolute;
        transform: translateY(-50%);
        inset-block-start: 50%;
        inset-inline-end: var(--space-small);
      }
    }

    .url {
      input:dir(ltr) {
        padding-inline-end: var(--space-xxlarge);
      }

      input:dir(rtl) {
        padding-inline-start: var(--space-xxlarge);

        &:not(:placeholder-shown) {
          direction: ltr;
          text-align: right;
        }
      }
    }

    .enable-custom-image-input {
      display: inline-block;
      font-size: var(--font-size-small);
      margin-block-start: var(--space-xsmall);
      cursor: pointer;
    }

    .custom-image-input-container {
      margin-block-start: var(--space-xsmall);

      .loading-container {
        width: 16px;
        height: 16px;
        overflow: hidden;
        position: absolute;
        transform: translateY(-50%);
        inset-block-start: 50%;
        inset-inline-end: var(--space-small);
      }

      // This animation is derived from Firefox's tab loading animation
      // See https://searchfox.org/mozilla-central/rev/b29daa46443b30612415c35be0a3c9c13b9dc5f6/browser/themes/shared/tabs.inc.css#208-216
      .loading-animation {
        @keyframes tab-throbber-animation {
          100% { transform: translateX(-960px); }
        }

        @keyframes tab-throbber-animation-rtl {
          100% { transform: translateX(960px); }
        }

        width: 960px;
        height: 16px;
        -moz-context-properties: fill;
        fill: var(--newtab-primary-action-background);
        background-image: url('chrome://browser/skin/tabbrowser/loading.svg');
        animation: tab-throbber-animation 1.05s steps(60) infinite;

        &:dir(rtl) {
          animation-name: tab-throbber-animation-rtl;
        }
      }
    }

    input {
      &[type='text'] {
        background-color: var(--newtab-background-color-secondary);
        border: $input-border;
        margin: var(--space-small) 0;
        padding: 0 var(--space-small);
        height: 32px;
        width: 100%;
        font-size: inherit;

        &[disabled] {
          border: $input-border;
          box-shadow: none;
          opacity: 0.4;
        }
      }
    }

    .invalid {
      input {
        &[type='text'] {
          border: $input-error-border;
          box-shadow: $input-error-boxshadow;
        }
      }
    }

    .error-tooltip {
      animation: fade-up-tt 450ms;
      background: var(--newtab-status-error);
      border-radius: var(--border-radius-small);
      color: $white;
      inset-inline-start: var(--space-xxsmall);
      padding: var(--space-xsmall) var(--space-medium);
      position: absolute;
      inset-block-start: var(--space-xxlarge);
      z-index: 1;

      // tooltip caret
      &::before {
        background: var(--newtab-status-error);
        inset-block-end: calc(var(--space-small) * -1);
        content: '.';
        height: 16px;
        inset-inline-start: var(--space-medium);
        position: absolute;
        text-indent: -999px;
        inset-block-start: calc(var(--space-small) * -1);
        transform: rotate(45deg);
        white-space: nowrap;
        width: 16px;
        z-index: -1;
      }
    }
  }

  .actions {
    justify-content: flex-end;

    button {
      margin-inline-start: var(--space-small);
      margin-inline-end: 0;
    }
  }

  @media (max-width: $break-point-medium) {
    .fields-and-preview {
      flex-direction: column;

      .top-site-outer {
        margin-inline-start: 0;
      }
    }
  }

  // prevent text selection of keyword label when clicking to select
  .title {
    user-select: none;
  }

  // CSS styled checkbox
  [type='checkbox']:not(:checked),
  [type='checkbox']:checked {
    inset-inline-start: -9999px; // stylelint-disable-line declaration-property-value-disallowed-list
    position: absolute;
  }

  [type='checkbox']:not(:checked) + label,
  [type='checkbox']:checked + label {
    cursor: pointer;
    display: block;
    position: relative;
  }

  $checkbox-offset: calc(var(--space-small) * -1);

  [type='checkbox']:not(:checked) + label::before,
  [type='checkbox']:checked + label::before {
    background: var(--newtab-background-color);
    border: $input-border;
    border-radius: var(--border-radius-small);
    content: '';
    height: 21px;
    inset-inline-start: calc(var(--space-small) * -1);
    position: absolute;
    inset-block-start: calc(var(--space-small) * -1);
    width: 21px;
    z-index: 1;

    [dir='rtl'] & {
      inset-inline-start: auto;
      inset-inline-end: calc(var(--space-small) * -1);
    }
  }

  // checkmark
  [type='checkbox']:not(:checked) + label::after,
  [type='checkbox']:checked + label::after {
    background: url('chrome://global/skin/icons/check.svg') no-repeat center center;
    content: '';
    height: 21px;
    inset-inline-start: calc(var(--space-small) * -1);
    position: absolute;
    inset-block-start: calc(var(--space-small) * -1);
    width: 21px;
    -moz-context-properties: fill;
    fill: var(--newtab-primary-action-background);
    z-index: 2;

    [dir='rtl'] & {
      inset-inline-start: auto;
      inset-inline-end: calc(var(--space-small) * -1);
    }
  }

  // when selected, highlight the tile
  [type='checkbox']:checked + label {
    .tile {
      box-shadow: $shadow-focus;
    }
  }

  // checkmark changes
  [type='checkbox']:not(:checked) + label::after {
    opacity: 0;
  }

  [type='checkbox']:checked + label::after {
    opacity: 1;
  }

  // accessibility
  [type='checkbox']:checked:focus + label::before,
  [type='checkbox']:not(:checked):focus + label::before {
    border: 1px dotted var(--newtab-primary-action-background);
  }
}

.top-sites-list,
.fields-and-preview {
  .top-site-outer {
    .top-site-button {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
    }

    .title {
      .title-label {
        display: block;
        overflow: hidden;
        text-overflow: ellipsis;
        -webkit-line-clamp: 2;
        white-space: wrap;
        word-break: break-word;
        min-height: 2lh;
        width: 100px;
        padding: 0 var(--space-xsmall);
      }

      &.sponsored .title-label {
        -webkit-line-clamp: 1;
        min-height: 1lh;
      }
    }
  }
}

// used for tooltips below form element
@keyframes fade-up-tt {
  0% {
    opacity: 0;
    transform: translateY(15px);
  }

  100% {
    opacity: 1;
    transform: translateY(0);
  }
}

// used for TopSites impression wrapper
.topsite-impression-observer {
  position: absolute;
  inset-block-start: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
