$search-height: var(--size-item-xlarge);
$search-height-new: 52px;
$search-icon-size: 24px;
$search-icon-padding: 16px;
$search-icon-width: 2 * $search-icon-padding + $search-icon-size - 4px; // 52px
$search-button-width: var(--size-item-xlarge);
$glyph-forward: url('chrome://browser/skin/forward.svg');

.search-wrapper {
  padding: 0 0 var(--space-xxlarge);


  // Edge case for users who have pocket stories turned off
  .no-recommended-stories & {
    // Bug 1967304 - Large number (40px)
    padding-block-end: calc(var(--space-large) + var(--space-xlarge));
  }

  // Edge case for users who have only search enabled
  .only-search & {
    // Bug 1967304 - Large number (40px)
    padding-block-end: calc(var(--space-large) + var(--space-xlarge));
  }

  .search-inner-wrapper {
    cursor: default;
    display: flex;
    min-height: $search-height-new;
    margin: 0 auto;
    position: relative;
    width: 304px;

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

    @media (min-width: $break-point-large) {
      width: 510px;
    }

    @media (min-width: $break-point-widest) {
      width: 720px;
    }

    // Bug 1966715: Offset padding of the submit button so the reset button is visible
    &.no-handoff {
      input {
        // Bug 1967304 - calc ($var)
        padding-inline-end: calc($search-button-width + var(--space-medium)); // stylelint-disable-line declaration-property-value-disallowed-list
      }
    }

  }

  .search-handoff-button,
  input {
    background: var(--newtab-background-color-secondary) var(--newtab-search-icon) $search-icon-padding center no-repeat;
    background-size: $search-icon-size;
    // Bug 1967304 - $variable
    padding-inline-start: $search-icon-width; // stylelint-disable-line declaration-property-value-disallowed-list
    padding-inline-end: var(--space-small);
    padding-block: 0;
    width: 100%;
    box-shadow: $shadow-card;
    border: 1px solid transparent;
    border-radius: var(--border-radius-medium);
    color: var(--newtab-text-primary-color);
    -moz-context-properties: fill;
    fill: var(--newtab-text-secondary-color);

    &:dir(rtl) {
      background-position-x: right $search-icon-padding;
    }
  }

  .search-inner-wrapper:active input,
  input:focus {
    border: 1px solid var(--newtab-primary-action-background);
    outline: 0;
    box-shadow: $shadow-focus;
  }

  .search-button {
    background: $glyph-forward no-repeat center center;
    background-size: 16px 16px;
    border: 0;
    border-radius: 0 var(--border-radius-medium) var(--border-radius-medium) 0;
    -moz-context-properties: fill;
    fill: var(--newtab-text-secondary-color);
    height: 100%;
    inset-inline-end: 0;
    position: absolute;
    width: $search-button-width;

    &:focus,
    &:hover {
      background-color: var(--newtab-element-hover-color);
      cursor: pointer;
    }

    &:focus {
      outline: 0;
      box-shadow: $shadow-focus;
      border: 1px solid var(--newtab-primary-action-background);
      border-radius: 0 var(--border-radius-medium) var(--border-radius-medium) 0;
    }

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

    &:dir(rtl) {
      transform: scaleX(-1);
    }
  }

  &.fake-focus:not(.search.disabled) {
    .search-handoff-button {
      border: 1px solid var(--newtab-primary-action-background);
      box-shadow: $shadow-focus;
    }
  }

  .search-handoff-button {
    padding-inline-end: var(--space-large);
    color: var(--newtab-text-primary-color);
    fill: var(--newtab-text-secondary-color);
    -moz-context-properties: fill;

    .fake-caret {
      inset-block-start: var(--space-large);
      // Bug 1967304 - $variable
      inset-inline-start: $search-icon-width; // stylelint-disable-line declaration-property-value-disallowed-list

      &:dir(rtl) {
        background-position-x: right $search-icon-padding;
      }
    }
  }

  &.visible-logo {
    .logo-and-wordmark {
      .wordmark {
        fill: var(--newtab-wordmark-color);
      }
    }
  }
}

.shortcuts-refresh.has-recommended-stories {
  .outer-wrapper:not(.fixed-search) .search-wrapper {
    margin-block-end: var(--space-xlarge);
    padding: 0;
  }
}

.has-recommended-stories {
  .outer-wrapper:not(.fixed-search) .search-wrapper {
    margin-block-start: var(--space-large);
  }

  .logo-and-wordmark {
    position: absolute;
    inset-inline-start: var(--space-xxlarge);
    inset-block-start: var(--space-xxlarge);

    @media (min-width: $break-point-layout-variant) {
      inset-inline-start: var(--space-large);
    }

    @media (min-width: $break-point-large) {
      inset-inline-start: var(--space-xxlarge);
    }
  }

  &.no-search {
    .body-wrapper {
      margin-block-start: 0;

      @media (min-width: $break-point-large) {
        // This is to handle the gap left by search being removed.
        // Bug 1967304 - Large number (96px)
        margin-block-start: calc(var(--space-xlarge) * 4);
      }

      @media (min-width: $break-point-ultra-wide) {
        margin-block-start: 0;
      }
    }
  }

  // Bug 1961633 - Keep shortcuts/feed below the logo/weather widget (var b layout)
  &.no-search.has-sections-grid {
    .body-wrapper {
      // 96px is the margin necessary to maintain the same spacing with/without the search bar
      // Bug 1967304 - Large number (96px)
      margin-block-start: calc(var(--space-xlarge) * 4);
    }

    .logo-and-wordmark {
      inset-inline-start: var(--space-large);

      @media (min-width: $break-point-small) {
        inset-inline-start: var(--space-xxlarge);
      }

      @media (min-width: $break-point-layout-variant) {
        inset-inline-start: var(--space-large);
      }

      @media (min-width: $break-point-large) {
        inset-inline-start: var(--space-xxlarge);
      }
    }

  }

  .search-inner-wrapper {
    width: 200px;

    @media (min-width: $break-point-small) {
      width: 300px;
    }

    @media (min-width: $break-point-medium) {
      width: 350px;
    }

    @media (min-width: $break-point-large) {
      width: 410px;
    }

    @media (min-width: $break-point-widest) {
      width: 584px;
    }

    @media (min-width: $break-point-weather) {
      width: 720px;
    }
  }

  // Bug 1960519 - Custom override for mobile icon next to weather
  &.has-mobile-download-promo {
    .search-inner-wrapper {
      @media (min-width: $break-point-widest) {
        // Set to smaller breakpoint to fit weather
        width: 497px;
      }

      @media (min-width: $break-point-weather) {
        // Reset back to default length
        width: 720px;
      }
    }
  }

  .logo {
    width: 52px;
    height: 52px;
    background-size: 52px;
  }

  .wordmark {
    display: none;
    height: 52px;

    @media (min-width: $break-point-large) {
      display: block;
    }
  }
}

@media (height <= 700px) {
  .search-wrapper {
    padding: 0 0 var(--space-xxlarge);
  }
}

.search-handoff-button {
  background: var(--newtab-background-color-secondary) var(--newtab-search-icon) $search-icon-padding center no-repeat;
  background-size: $search-icon-size;
  border: solid 1px transparent;
  border-radius: var(--border-radius-small);
  box-shadow: $shadow-secondary, 0 0 0 1px $black-15;
  cursor: text;
  font-size: inherit;
  padding: 0;
  // Bug 1967304 - Large number (48px)
  padding-inline: calc(var(--space-xlarge) * 2);
  opacity: 1;
  width: 100%;

  &:dir(rtl) {
    background-position-x: right $search-icon-padding;
  }

  .fake-focus:not(.search-disabled) & {
    border: $input-border-active;
    box-shadow: var(--newtab-textbox-focus-boxshadow);

    .fake-caret {
      display: block;
    }
  }

  .search-disabled & {
    opacity: 0.5;
    box-shadow: none;
  }

  .fake-editable:focus {
    outline: none;
    caret-color: transparent;
  }

  .fake-editable {
    color: transparent;
    height: 100%;
    opacity: 0;
    position: absolute;
    inset: 0;
  }

  .fake-textbox {
    opacity: 0.54;
    text-align: start;
    -webkit-line-clamp: 1;
    overflow: hidden;
  }

  .fake-caret {
    /* To replicate the default caret blink rate of 567ms (https://searchfox.org/mozilla-central/source/widget/cocoa/nsLookAndFeel.mm#397):
     - Multiply the blink time by 2 to cover both the visible and hidden states.
     - Use steps(2, start) to divide the animation into 2 phases:
       1. First 567ms (Step 1) → Caret is visible
       2. Next 567ms (Step 2) → Caret is hidden
    This gives a sharp ON/OFF effect instead of a smooth transition. */

    animation: caret-animation var(--caret-blink-time, 1134ms) steps(2, start) var(--caret-blink-count, infinite);
    background: var(--newtab-text-primary-color);
    display: none;
    // Bug 1967304 - Large number (48px)
    inset-inline-start:calc(var(--space-xlarge) * 2);
    height: 17px;
    position: absolute;
    inset-block-start: var(--space-large);
    width: 1px;

    @keyframes caret-animation {
      to {
        visibility: hidden;
      }
    }
  }
}

@media (height > 700px) {
  .activity-stream .fixed-search {
    .search-wrapper {
      $search-height: $search-height-new;
      $search-icon-size: 24px;
      $search-icon-padding: 16px;
      $search-header-bar-height: 95px;

      border-bottom: solid 1px var(--newtab-border-color);
      padding: var(--space-xlarge) 0;
      background-color: var(--newtab-overlay-color);
      min-height: $search-header-bar-height;
      inset-inline-start: 0;
      position: fixed;
      inset-block-start: 0;
      width: 100%;
      z-index: 3;
      display: flex;
      align-items: center;

      .search-inner-wrapper {
        min-height: $search-height;
      }

      input {
        background-position-x: $search-icon-padding;
        background-size: $search-icon-size;

        &:dir(rtl) {
          background-position-x: right $search-icon-padding;
        }
      }

      .search-handoff-button .fake-caret {
        inset-block-start: var(--space-large);
      }

      .logo-and-wordmark {
        display: none;
      }
    }

    .search-handoff-button {
      background-position-x: var(--space-large);
      background-size: $search-icon-size;

      &:dir(rtl) {
        background-position-x: right var(--space-large);
      }

      .fake-caret {
        inset-block-start: var(--space-small);
      }
    }
  }
}

@at-root {

  // Adjust the style of the contentSearchUI-generated table
  .contentSearchSuggestionTable {
    border: 0;
    box-shadow: $context-menu-shadow;
    transform: translateY($textbox-shadow-size);
    background-color: var(--newtab-background-color);

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

    .contentSearchHeader,
    .contentSearchSettingsButton {
      border-color: var(--newtab-border-color);
    }

    .contentSearchSuggestionsList {
      color: var(--newtab-text-primary-color);
      border: 0;
    }

    .contentSearchOneOffsTable {
      border-top: solid 1px var(--newtab-border-color);
      background-color: var(--newtab-background-color);
    }

    .contentSearchSearchWithHeaderSearchText {
      color: var(--newtab-text-primary-color);
    }

    .contentSearchSuggestionRow {
      &.selected {
        background: var(--newtab-element-hover-color);
        color: var(--newtab-text-primary-color);

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

        .historyIcon {
          fill: var(--newtab-text-secondary-color);
        }
      }
    }

    .contentSearchOneOffItem {
      // Make the border slightly shorter by offsetting from the top and bottom
      $border-offset: 18%;

      background-image: none;
      border-image: linear-gradient(transparent $border-offset, var(--newtab-border-color) $border-offset, var(--newtab-border-color) 100% - $border-offset, transparent 100% - $border-offset) 1;
      border-inline-end: 1px solid;
      position: relative;

      &.selected {
        background: var(--newtab-element-hover-color);
      }

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

    .contentSearchSettingsButton {
      &:hover {
        background: var(--newtab-element-hover-color);
        color: var(--newtab-text-primary-color);
      }
    }
  }

  .contentSearchHeaderRow > td > img,
  .contentSearchSuggestionRow > td > .historyIcon {
    margin-inline-start: var(--space-small);
    margin-inline-end: var(--space-large);
  }
}
