/**
 * cart.css
 */

/* Component: Cart Toggle */
body > header .cart {
  display: contents;

  .cart-toggle {
    position: relative;
    grid-column: 4;
    grid-row: 1;
    justify-self: end;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    margin-left: -24px;
    padding: 0;
    border: none;
    background: transparent;
    transition: var(--global-transition);
    cursor: pointer;

    .cart-icon {
      display: block;
      width: 24px;
      height: 24px;
      background: var(--color-black-light);
      mask: url('../../images/icons/icon-basket-48x48.png') no-repeat center / contain;
      -webkit-mask: url('../../images/icons/icon-basket-48x48.png') no-repeat center / contain;
      transition: var(--global-transition);
    }

    &:hover .cart-icon,
    &:focus .cart-icon,
    &[aria-expanded="true"] .cart-icon {
      background: var(--color-black);
    }

    .cart-count {
      position: absolute;
      right: -6px;
      bottom: -6px;
      box-sizing: border-box;
      min-width: 16px;
      height: 16px;
      padding: 0 4px;
      border-radius: 8px;
      background: var(--color-primary);
      color: var(--color-white);
      font-size: var(--font-size-tiny);
      font-weight: 600;
      line-height: 16px;
      text-align: center;

      &.is-empty {
        display: none;
      }
    }
  }
}

/* Component: Cart Toggle Bump, shown when something was added to the cart */
@keyframes cart-bump {
  0% {
    transform: translateY(0);
  }

  30% {
    transform: translateY(-6px);
  }

  55% {
    transform: translateY(0);
  }

  75% {
    transform: translateY(-2px);
  }

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

body > header .cart-toggle.is-bumped {
  animation: cart-bump 0.5s ease-out;
}

/* Component: Cart Panel */
body > header .cart-panel {
  position: absolute;
  top: 100%;
  right: 0;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
  width: var(--cart-panel-width);
  min-width: 0;
  max-width: 100%;
  max-height: calc(100dvh - var(--header-offset) - 48px);
  margin-top: var(--mega-menu-gap);
  background-color: var(--color-white);
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.05);
  clip-path: inset(0 -48px -48px -48px);
  border-radius: var(--border-radius-default);
  opacity: 0;
  transform: translateY(4px);
  transition: opacity 0.2s ease-out, transform 0.2s ease-out, visibility 0s linear 0.2s;
  visibility: hidden;
  overflow: hidden;
  pointer-events: none;
  z-index: 1000;

  .cart-back {
    display: none;
  }
}

body > header .cart.is-open .cart-panel {
  opacity: 1;
  transform: translateY(0);
  transition-delay: 0s, 0s, 0s;
  visibility: visible;
  pointer-events: auto;
}

/**
 * Desktop: Cart Panel Position
 * Like the mega menu, the header container is the containing block, so the panel hangs from the right edge of it.
 */
@media (min-width: 1025px) {
  body > header .cart-panel {
    top: calc(50% + var(--menu-item-height) / 2);
  }

  /* Bridges the gap between the toggle and the panel, so the pointer does not leave the cart on the way. */
  body > header .cart.is-open::after {
    content: '';
    position: absolute;
    top: calc(50% + var(--menu-item-height) / 2);
    right: 0;
    width: var(--cart-panel-width);
    height: var(--mega-menu-gap);
  }
}

/* Tablet: Cart Panel, hangs under the header on the right */
@media (max-width: 1024px) {
  body > header {
    /* Nested under .cart like the base rule, so these overrides are not outranked by it. */
    .cart .cart-toggle {
      margin-left: 0;
    }

    .cart-panel {
      position: fixed;
      top: var(--header-offset);
      right: 0;
      left: auto;
      width: min(100vw, var(--cart-panel-width));
      max-width: 100vw;
      max-height: calc(100dvh - var(--header-offset));
      margin-top: 0;
    }
  }

  /* WordPress Admin Bar: Desktop/Tablet */
  body:has(#wpadminbar) header .cart-panel {
    top: calc(var(--header-offset) + var(--wpadminbar-height-desktop));
    max-height: calc(100dvh - var(--header-offset) - var(--wpadminbar-height-desktop));
  }
}

/* WP Mobile: Admin bar is taller */
@media screen and (max-width: 782px) {
  body:has(#wpadminbar) header .cart-panel {
    top: calc(var(--header-offset) + var(--wpadminbar-height-mobile));
    max-height: calc(100dvh - var(--header-offset) - var(--wpadminbar-height-mobile));
  }
}

/**
 * Mobile: Cart Layer
 * Works like a mega menu layer: a full height panel under the header that slides in from the right,
 * with a sticky back row at the top.
 */
@media (max-width: 767px) {
  body > header {
    /* Like the search toggle, a square as tall as the header. The badge sits at the bottom right of the 24px icon centred in it. */
    .cart .cart-toggle {
      grid-column: 5;
      justify-self: stretch;
      width: 100%;
      height: 100%;

      .cart-count {
        right: 10px;
        bottom: 10px;
      }
    }

    .cart-panel {
      top: var(--header-offset-mobile);
      left: 0;
      right: 0;
      width: 100%;
      max-width: none;
      height: calc(100dvh - var(--header-offset-mobile));
      max-height: none;
      border-top-left-radius: 0;
      border-top-right-radius: 0;
      opacity: 1;
      transform: translateX(100%);
      transition: transform 0.2s ease-out, visibility 0s linear 0.2s;

      .cart-back {
        display: flex;
        flex: none;
        box-sizing: border-box;
        min-height: 52px;
        padding: 0 16px;
        border-bottom: 1px solid var(--color-black-transparent);
        background: var(--color-white);
      }

      .cart-back-button {
        display: flex;
        align-items: center;
        gap: 12px;
        width: 100%;
        padding: 0;
        border: none;
        background: transparent;
        color: var(--color-black);
        font: inherit;
        font-size: var(--font-size-small);
        font-weight: 600;
        text-align: left;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        cursor: pointer;
      }
    }

    .cart.is-open .cart-panel {
      transform: translateX(0);
    }
  }

  body:has(#wpadminbar) > header .cart-panel {
    top: calc(var(--header-offset-mobile) + var(--wpadminbar-height-mobile));
    height: calc(100dvh - var(--header-offset-mobile) - var(--wpadminbar-height-mobile));
  }

  body:has(.cart.is-open) {
    overflow: hidden;
  }
}

/* Component: Cart Panel Body */
.cart-panel-body {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  min-height: 0;
  transition: opacity 0.2s ease-out;
}

.cart-panel.is-loading .cart-panel-body {
  opacity: 0.5;
  pointer-events: none;
}

.cart-items {
  flex: 1 1 auto;
  min-height: 0;
  margin: 0;
  padding: 0;
  list-style: none;
  overflow-y: auto;
  overscroll-behavior: contain;
}

.cart-items-row + .cart-items-row {
  border-top: 1px solid var(--color-white-dark);
}

.cart-empty {
  margin: 0;
  padding: 32px 16px;
  color: var(--color-grey-dark);
  font-size: var(--font-size-medium);
  text-align: center;
}

/* Component: Cart Footer */
.cart-footer {
  flex: none;
  padding: 12px 16px 16px;
  border-top: 1px solid var(--color-white-dark);
  background: var(--color-white);

  .cart-subtotal {
    display: flex;
    justify-content: space-between;
    margin-bottom: 16px;
    font-size: var(--font-size-medium);
    font-weight: 600;
  }

  .cart-actions {
    display: grid;
    gap: 8px;
  }

  .cart-action {
    display: flex;
    align-items: center;
    justify-content: center;
    box-sizing: border-box;
    min-height: 44px;
    padding: 0 16px;
    border: 1px solid var(--color-grey-light);
    border-radius: var(--border-radius-default);
    color: var(--color-black);
    font-family: var(--font-family-headings);
    font-size: var(--font-size-small);
    font-weight: 700;
    text-decoration: none;
    letter-spacing: 0.2px;
    transition: var(--global-transition);

    &:hover,
    &:focus-visible {
      background: var(--color-white-dark);
    }

    &.is-primary {
      border-color: var(--color-primary);
      background: var(--color-primary);
      color: var(--color-white);

      &:hover,
      &:focus-visible {
        border-color: var(--color-primary-dark);
        background: var(--color-primary-dark);
      }
    }
  }
}

/* Component: CartItemRow */
article.cart-item {
  display: grid;
  grid-template-columns: 56px minmax(0, 1fr) auto;
  gap: 16px;
  padding: 16px;

  .cart-item-thumbnail {
    margin: 0;
    aspect-ratio: 1;
    overflow: hidden;
    box-sizing: border-box;
    border: 1px solid var(--color-grey-light);
    border-radius: 4px;
    background: var(--color-white-dark);

    a,
    img {
      display: block;
      width: 100%;
      height: 100%;
    }

    img {
      object-fit: cover;
    }
  }

  .cart-item-content {
    display: flex;
    flex-direction: column;
    gap: 4px;
    min-width: 0;
  }

  .cart-item-title {
    margin: 0;
    font-size: var(--font-size-medium);
    font-weight: 600;
    line-height: 1.3;
    overflow-wrap: anywhere;

    a {
      display: -webkit-box;
      overflow: hidden;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
      line-clamp: 2;
      color: var(--color-black);
      text-decoration: none;

      &:hover,
      &:focus-visible {
        text-decoration: underline;
      }
    }
  }

  .cart-item-price {
    color: var(--color-grey-dark);
    font-size: var(--font-size-small);
  }

  .cart-item-quantity {
    display: inline-flex;
    align-items: center;
    align-self: flex-start;
    margin-top: 4px;
    border: 1px solid var(--color-grey-light);
    border-radius: var(--border-radius-default);

    &.is-static {
      padding: 0;
      border: none;
      color: var(--color-grey-dark);
      font-size: var(--font-size-small);
    }

    button {
      width: 28px;
      height: 28px;
      padding: 0;
      border: none;
      background: transparent;
      color: var(--color-black);
      font-size: var(--font-size-body);
      line-height: 1;
      cursor: pointer;
      transition: var(--global-transition);

      &:hover:not(:disabled),
      &:focus-visible {
        background: var(--color-white-dark);
      }

      &:disabled {
        color: var(--color-grey);
        cursor: not-allowed;
      }
    }

    input.cart-item-qty {
      width: 36px;
      height: 28px;
      padding: 0;
      border: none;
      border-radius: 0;
      background: transparent;
      font-size: var(--font-size-small);
      text-align: center;
      appearance: textfield;
      -moz-appearance: textfield;

      &::-webkit-inner-spin-button,
      &::-webkit-outer-spin-button {
        margin: 0;
        -webkit-appearance: none;
      }
    }
  }

  .cart-item-side {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: space-between;
    gap: 8px;
  }

  .cart-item-subtotal {
    font-size: var(--font-size-medium);
    font-weight: 600;
    white-space: nowrap;
  }

  .cart-item-remove {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 30px;
    height: 30px;
    padding: 0;
    border: none;
    background: transparent;
    cursor: pointer;

    .close-icon {
      display: block;
      width: 16px;
      height: 16px;
      background: var(--color-grey-dark);
      mask: url('../../images/icons/icon-close-48x48.png') no-repeat center / contain;
      -webkit-mask: url('../../images/icons/icon-close-48x48.png') no-repeat center / contain;
      transition: var(--global-transition);
    }

    &:hover .close-icon,
    &:focus-visible .close-icon {
      background: var(--color-black);
    }
  }
}

/* Tablet and desktop: the footer buttons sit side by side, on mobile the layer is wide enough to stack them */
@media (min-width: 768px) {
  .cart-footer .cart-actions {
    grid-template-columns: 1fr 1fr;
  }

  .cart-footer .cart-action {
    padding: 0 8px;
    text-align: center;
  }
}

/* Mobile: CartItemRow, dividers and touch targets like the menu rows */
@media (max-width: 767px) {
  article.cart-item {
    .cart-item-quantity button {
      width: 36px;
      height: 36px;
    }

    .cart-item-quantity input.cart-item-qty {
      height: 36px;
    }

    .cart-item-remove {
      width: 36px;
      height: 36px;
      margin: -6px -6px 0 0;
    }
  }

  .cart-footer {
    padding-bottom: max(16px, env(safe-area-inset-bottom));
  }
}

@media (prefers-reduced-motion: reduce) {
  body > header .cart-toggle.is-bumped {
    animation: none;
  }

  body > header .cart-panel,
  .cart-panel-body {
    transition: none;
  }
}
