.personalizeButtonWrapper {
  margin: 0;
  padding: 0;
  // z-index note: menu should cover fixed search bar and asrouter dev tools toggle button
  z-index: 5;
  position: fixed;
  inset-block-end: var(--space-xlarge);
  inset-inline-end: var(--space-xlarge);
}

.personalize-button {
  border: 0;
  border-radius: var(--border-radius-small);
  background-color: var(--button-background-color, rgba(21, 20, 26, 7%));
  box-shadow: 0 0.8px 1.6px 0 rgba(58, 57, 68, 20%);
  padding: calc(5 * var(--space-xxsmall));
  position: relative;
  background-size: var(--icon-size-medium);
  background-position: center;
  display: flex;
  align-items: center;
  overflow: hidden;
  transition: background-color 0.3s ease;

  .lightWallpaper &,
  .darkWallpaper & {
    background-color: var(--newtab-weather-background-color);

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

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

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

  &:focus-visible {
    @include ds-focus;

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

  &.personalize-animate-exit-done {
    visibility: hidden;
  }

  > div {
    width: var(--icon-size-medium);
    height: var(--icon-size-medium);
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;

    > img {
      -moz-context-properties: fill;
      display: inline-block;
      color: var(--icon-color);
      fill: currentColor;
      height: 100%;
      object-fit: contain;
      vertical-align: middle;
      width: 100%;
    }
  }

  > label {
    margin-inline-end: 0;
    opacity: 0;
    white-space: nowrap;
    overflow: hidden;
    max-width: 0;
    transition:
      opacity 0.3s ease,
      margin 0.3s ease,
      max-width 0.3s ease;
  }
}

.personalize-button:hover,
.personalize-button:focus-visible,
.personalize-button.activation-window-variant-a,
.personalize-button.activation-window-variant-b {
  > label {
    opacity: 1;
    margin-inline-end: var(--space-medium);
    // CSS animations can only transition between fixed values;
    // 30ch is enough to accommodate longest localized string (Bulgarian: 15ch)
    max-width: 30ch;
  }
}

.customize-menu-animate-wrapper {
  color: var(--newtab-text-primary-color);
  background-color: var(--newtab-background-color-secondary);
  width: 432px;
  height: 100%;
  // Becomes block after first open; never reset (see .customize-animate-enter/exit)
  display: none;
  position: fixed;
  inset-block: 0;
  inset-inline-end: 0;
  z-index: 1001;
  padding-block: 0 var(--space-large);
  padding-inline: var(--space-large);
  transform: translateX(435px);
  visibility: hidden;
  cursor: default;

  &.customize-animate-enter,
  &.customize-animate-exit,
  &.customize-animate-enter-done,
  &.customize-animate-exit-done {
    // display: none avoids initial load cost
    // Stays as display: block after initial load
    display: block;
  }

  @media (prefers-reduced-motion: no-preference) {
    // We need customize-animate-enter and customize-animate-exit to fix bug 1868232
    // These first 2 classes happen only while the element is animating.
    &.customize-animate-enter,
    &.customize-animate-exit,
    // We only add these so the css is visible for inspecting while not animating.
    // Otherwise it's difficult to see and inspect this css because the transition is so fast.
    &.customize-animate-enter-done,
    &.customize-animate-exit-done {
      transition: transform 250ms $customize-menu-slide-bezier, visibility 250ms;
    }
  }

  @media (forced-colors: active) {
    border-inline-start: solid 1px;
  }

  &:dir(rtl) {
    transform: translateX(-435px);
  }

  &.customize-animate-enter-done,
  &.customize-animate-enter-active {
    box-shadow: $shadow-large;
    visibility: visible;
    transform: translateX(0);
  }

  &.customize-animate-exit-active {
    box-shadow: $shadow-large;
  }
}

.customize-menu {
  overflow: auto;
  height: 100%;

  &.subpanel-open {
    overflow: clip;
  }

  .close-button-wrapper {
    position: sticky;
    inset-block-start: 0;
    padding-block-start: var(--space-large);
    background-color: var(--newtab-background-color-secondary);
    z-index: 1;

    moz-button {
      margin-inline-start: auto;
      margin-inline-end: var(--space-large);
      display: block;
    }
  }

  *:focus-visible {
    outline: var(--focus-outline);
  }
}

.grid-skip {
  display: contents;
}

.home-section {
  display: grid;
  grid-template-columns: 1fr;
  grid-template-rows: repeat(4, auto);
  grid-row-gap: var(--space-xxlarge);
  padding-block: var(--space-xsmall) var(--space-large);
  padding-inline: var(--space-large);

  .wallpapers-section h2 {
    font-size: inherit;
  }

  .widgets-section h2 {
    font-size: inherit;
    margin-block-start: 0;
    font-weight: var(--font-weight-heading);
  }

  .settings-widgets,
  .settings-toggles {
    display: flex;
    flex-direction: column;
    gap: var(--space-xlarge);
  }

  .settings-widgets {
    gap: var(--space-large);

    .divider {
      margin-block-start: var(--space-large);
    }
  }

  [slot='nested'] {
    margin-block-start: 0;
  }

  .section {
    .customize-menu-checkbox-label {
      font-size: inherit;
      margin-inline-start: var(--space-xsmall);
    }

    .check-wrapper {
      position: relative;
      display: flex;
    }

    .customize-menu-checkbox {
      margin-inline-start: var(--space-xxsmall);
      width: 16px;
      height: 16px;
      vertical-align: middle;
      border: $customize-menu-border-tint;
      box-sizing: border-box;
      border-radius: var(--border-radius-small);
      appearance: none;
      background-color: var(--newtab-element-secondary-color);
      flex-shrink: 0;

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

    .customize-menu-checkbox:checked {
      -moz-context-properties: fill;
      fill: var(--newtab-primary-element-text-color);
      background: url('chrome://global/skin/icons/check.svg') center no-repeat;
      background-color: var(--newtab-primary-action-background);

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

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

      @media (forced-colors: active) {
        fill: $black;
      }
    }

    .customize-menu-checkbox:active + .checkmark {
      fill: var(--newtab-element-secondary-color);
    }

    .selector {
      color: var(--newtab-text-primary-color);
      width: 118px;
      display: block;
      border: 1px solid var(--border-color);
      border-radius: var(--border-radius-small);
      appearance: none;
      padding-block: var(--space-small);
      padding-inline: var(--space-small) var(--space-medium);
      margin-inline-start: var(--space-xxsmall);
      margin-block-end: var(--space-xxsmall);
      -moz-context-properties: fill;
      fill: var(--newtab-text-primary-color);
      background: url('chrome://global/skin/icons/arrow-down-12.svg') right no-repeat;
      background-size: 8px;
      background-origin: content-box;
      background-color: var(--newtab-background-color-secondary);

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

      &:dir(rtl) {
        background-position-x: left;
      }
    }

    .more-info-top-wrapper,
    .more-info-pocket-wrapper {
      margin-inline-start: calc(var(--space-xxsmall) * -1);
      overflow: hidden;

      .more-information {
        position: relative;
        transition: margin-block-start 250ms $customize-menu-expand-bezier;
      }
    }

    .more-info-pocket-wrapper {
      .more-information {
        // Note: This is necessary so the follow/block topics panel can
        // be positioned absolutely across the entire Customize menu
        position: static;
      }

      .check-wrapper {
        margin-block-end: var(--space-small);
      }

      // Prevent focus outline clipping by allowing overflow
      &:has(moz-box-button:focus-within) {
        overflow: visible;
      }
    }

    .more-info-top-wrapper {
      .check-wrapper {
        margin-block-start: var(--space-large);
      }
    }
  }

  .sponsored-content-info {
    display: flex;
    gap: var(--space-small);
    font-size: var(--font-size-small);
    border-radius: var(--border-radius-medium);
    background-color: var(--newtab-element-secondary-color);
    padding: var(--space-small) var(--space-large);

    .icon-help {
      flex-shrink: 0;
      color: var(--color-accent-primary);
      height: 20px;
    }

    a {
      color: var(--newtab-primary-action-background);
    }
  }

  .divider {
    border-block-start: 1px var(--border-color) solid;
    margin: 0 calc(var(--space-large) * -1);
  }

  .external-link {
    -moz-context-properties: fill;
    background: url('chrome://global/skin/icons/settings.svg') left no-repeat;
    background-size: 16px;
    border: 1px solid transparent;
    border-radius: var(--border-radius-small);
    color: var(--link-color);
    cursor: pointer;
    fill: var(--link-color);
    font-size: var(--font-size-small);
    margin-block-end: var(--space-large);
    padding-inline-start: var(--space-xlarge);
    text-decoration: underline;

    &:hover {
      color: var(--link-color-hover);
      text-decoration: none;
    }

    &:active {
      color: var(--link-color-active);
    }

    @media (forced-colors: active) {
      padding: var(--space-small) var(--space-medium);
      padding-inline-start: var(--space-xlarge);
    }

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

  .external-link:hover {
    text-decoration: none;
  }
}

.external-link:focus-visible,
.close-button:focus-visible {
  border: 1px solid var(--newtab-primary-action-background);
  outline: 0;
  box-shadow: $shadow-focus;
}

// Make sure form elements under the "Shortcuts" subsection don't change position on focus
// by always having a 2px box shadow around them - one that blends in with the background.
.home-section .section {
  .customize-menu-checkbox, .selector {
    outline: 0;
    box-shadow: 0 0 0 2px light-dark(var(--newtab-background-color), var(--newtab-background-color-dark));
  }
}

// Move the Shortcuts dropdown slightly down so that the box shadow is not cut off at the top
// in active & focus states.
.home-section .section .selector {
  margin-block-start: var(--space-xxsmall)
}

// Improve accessibility of shortcuts dropdown and sponsored shortcuts checkbox
// by setting a bright 2px box shadow in active and focus states.
.home-section .section
{
  .customize-menu-checkbox, .selector {
    &:is(:focus-visible) {
      box-shadow: 0 0 0 2px var(--color-accent-primary);
      outline: var(--focus-outline); // For Windows high-contrast mode
    }

    &:is(:active) {
      box-shadow: 0 0 0 2px var(--color-accent-primary-active);
      outline: 0;
    }
  }
}

.sections-mgmt-panel {
  height: 100%;
  // Width of panel minus the margins
  inset-inline-start: var(--space-xlarge);
  position: absolute;
  inset-block-start: 0;
  width: 380px;
  z-index: 2;
  transform: translateX(100%);
  margin-block: var(--space-large) 0;
  padding: 0;
  background-color: var(--newtab-background-color-secondary);
  overflow-y: auto;

  &:dir(rtl) {
    transform: translateX(-100%);
  }

  @media (prefers-reduced-motion: no-preference) {
    &.sections-mgmt-panel-enter,
    &.sections-mgmt-panel-exit,
    &.sections-mgmt-panel-enter-done,
    &.sections-mgmt-panel-exit-done {
      transition: transform 300ms;
    }
  }

  &.sections-mgmt-panel-enter-done,
  &.sections-mgmt-panel-enter-active {
    transform: translateX(0);
  }

  // Typography
  h3 {
    font-size: inherit;
    margin-block: 0 var(--space-small);
  }

  // List
  .topic-list {
    list-style: none;
    display: flex;
    flex-direction: column;
    gap: var(--space-small);
    margin-block: 0 var(--space-xxlarge);
    padding-inline: 0;
    width: 100%;

    li {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
  }

  .topic-list-empty-state {
    display: block;
    margin-block-end: var(--space-xxlarge);
    color: var(--text-color-deemphasized);
  }

  // Buttons
  .arrow-button {
    background: url('chrome://global/skin/icons/arrow-left.svg') no-repeat left center;

    &:dir(rtl) {
      background: url('chrome://global/skin/icons/arrow-right.svg') no-repeat right center;
    }

    border: none;
    cursor: pointer;
    margin-block-end: var(--space-xlarge);
    padding-inline-start: var(--space-xlarge);
    -moz-context-properties: fill;
    fill: currentColor;
    min-height: var(--size-item-small);
    margin-inline-start: var(--space-xsmall);
    margin-block-start: var(--space-xsmall);

    h1 {
      font-size: var(--font-size-root);
      margin-block: 0;
      font-weight: var(--button-font-weight);
    }

  }

  // Follow / Unfollow and Block / Unblock Buttons
  .section-block,
  .section-follow {
    .section-button-blocked-text,
    .section-button-following-text {
      display: none;
    }

    .section-button-unblock-text,
    .section-button-unfollow-text {
      display: none;
    }

    &.following {
      .section-button-follow-text {
        display: none;
      }

      .section-button-following-text {
        display: block;
      }
    }

    &.following:not(:hover) {
      moz-button {
        --button-background-color-destructive: var(--button-background-color);
        --button-text-color-destructive: var(--button-text-color);
        --button-border-color-destructive: var(--button-border-color);
      }
    }

    &.following:hover {
      .section-button-following-text {
        display: none;
      }

      .section-button-unfollow-text {
        display: block;
      }
    }

    &.blocked {
      .section-button-block-text {
        display: none;
      }

      .section-button-blocked-text {
        display: block;
      }
    }

    &.blocked:hover {
      .section-button-blocked-text {
        display: none;
      }

      .section-button-unblock-text {
        display: block;
      }
    }
  }

}
