// Glossary of exceptions:
// [1] Some email clients don't support CSS variables: https://www.caniemail.com/search/?s=var
// [2] Only using !important overrides the default style because premailer doesn't prioritize inlining based on media queries.
//     A workaround would be to move the email specific media queries into a <style data-premailer="ignore"> block,
//     but it doesn't work yet due to a known premailer issue (https://github.com/premailer/premailer/issues/242).
// [3] Some email clients don't support grid and flex. A table layout is needed for a better compatibility.
// [4] Some email clients don't support pseudo-elements: https: //www.caniemail.com/search/?s=pseudo
// [5] Some email clients don't support spacing between grid items, flex items and columns: https: //www.caniemail.com/search/?s=gap
// [6] Some email clients strip out HTML5 semantic elements: https://www.caniemail.com/features/html-semantics/

$accent-color: map-get($colors, "pink"); // [1]

.email {
  @include font-size(2);
  padding: spacer(3);

  & > .main,
  & > .footer {
    max-width: $main-stack-width;

    table {
      // [5]
      gap: 0;
      tr:not(:last-child) {
        margin-bottom: spacer(4);
      }

      &.reset {
        display: revert;
        border-collapse: separate;
        border-spacing: 0;

        & > tbody,
        & > tfoot {
          & > * {
            display: revert;
            border: none;

            & > * {
              display: revert;
              border: none;
              padding: 0;
            }
          }
        }

        & > tbody > * {
          background-color: unset;
        }

        & > :last-child > :last-child > * {
          border: none;
        }

        & > tbody > :first-child {
          & > :first-child {
            border-radius: unset;
          }

          & > :last-child {
            border-top-right-radius: unset;
          }
        }
      }

      &.stats {
        // [3]
        tr {
          &:first-child {
            td {
              padding-bottom: spacer(5);
            }
          }

          td {
            padding: 0 spacer(3);
            text-align: center;

            &:first-child {
              padding-right: spacer(3);
            }

            &:last-child {
              padding-left: spacer(3);
            }

            > *:last-child {
              @include font-size(4);
            }
          }
        }
      }

      &.list {
        // [3]
        & > thead {
          display: revert;
        }

        tr {
          &:first-child {
            td {
              padding-top: 0;
            }
          }

          &:last-child {
            td {
              padding-bottom: 0;
            }
          }

          td,
          th {
            vertical-align: top;
            padding: spacer(2);

            &:first-child {
              padding-left: 0;
            }

            &:last-child {
              padding-right: 0;
            }

            img.avatar {
              max-width: revert;
              width: spacer(6);
              height: spacer(6);
            }
          }
        }
      }

      &.table-stack {
        // [3]
        &,
        & > * {
          display: revert;
        }

        & > thead,
        & > tbody,
        & > tfoot {
          &,
          & > *,
          & > * > * {
            display: revert;
            border: none;
          }
        }

        & > tbody,
        & > tfoot {
          & > * > * {
            border-top: $border;

            &:first-child {
              border-left: $border;
            }

            &:last-child {
              border-right: $border;
            }
          }

          &:last-child > tr:last-child > * {
            border-bottom: $border;
          }
        }

        & > tbody > :first-child {
          & > :first-child {
            border-top-left-radius: $table-border-radius;
          }

          & > :last-child {
            border-top-right-radius: $table-border-radius;
          }
        }

        & > :last-child > :last-child {
          & > :first-child {
            border-bottom-left-radius: $table-border-radius;
          }

          & > :last-child {
            border-bottom-right-radius: $table-border-radius;
          }
        }
      }

      &.two-product-card-table {
        // [3]
        width: 100%;

        // [5]
        & > tbody > tr > td {
          display: block;

          &:first-child {
            padding-bottom: spacer(4);
          }
        }

        @include breakpoint-up(sm) {
          // [2]
          & > tbody > tr > td {
            display: revert !important;
            width: 50% !important;

            &:first-child {
              padding-right: spacer(2) !important;
              padding-bottom: 0 !important;
            }

            &:last-child {
              padding-left: spacer(2) !important;
            }
          }
        }
      }
    }

    [role="alert"],
    [role="status"] {
      &::before {
        content: unset !important;
      }

      td {
        vertical-align: top;

        &:first-child {
          width: calc(size(1) + size(0.5));
          text-align: left;

          img {
            /* Some email clients don't support CSS variables: https://www.caniemail.com/search/?s=var */
            /* References line-height of font-size(2) */
            height: size(0.875 * 1.3);
            width: size(0.875 * 1.3);
          }
        }
      }

      &.promo {
        td {
          &:first-child {
            width: calc(size(2) + size(0.75));

            img {
              /* Some email clients don't support CSS variables: https://www.caniemail.com/search/?s=var */
              height: size(1.75);
              /* References line-height of font-size(3) */
              width: size(1.25 * 1.6);
            }
          }
        }
      }
    }
  }

  & > .main {
    border: $border;
    margin: spacer(4) auto;

    & > * {
      padding: spacer(3);
      border-bottom: $border;

      &:last-child {
        border-bottom: none;
      }

      > * {
        margin: spacer(4) 0 0;

        &:first-child {
          margin-top: 0;
        }
      }

      button,
      .button {
        /* Needed to expand button full width in emails */
        display: block;
        text-align: center;
        width: unset;

        /* Some email clients don't support CSS variables: https://www.caniemail.com/search/?s=var */
        background-color: transparent;
        color: #000000;

        &.primary {
          /* Some email clients don't support CSS variables: https://www.caniemail.com/search/?s=var */
          background-color: #000000;
          color: #ffffff;
        }
      }

      img {
        display: block;
        max-width: 100%;

        &.header-badge {
          margin: 0 auto;
          max-width: size(16);
        }
      }

      pre {
        code {
          display: block;
          padding: spacer(3);
          font-family: Courier;
          background-color: var(--body-bg);
          border-radius: nth($border-radii, 1);
          border: $border;
        }
      }

      ul,
      ol {
        padding: 0 0 0 spacer(4);
      }

      @include breakpoint-up(sm) {
        // [2]
        padding: spacer(5) !important;
      }
    }
  }

  & > .footer {
    margin: spacer(6) auto spacer(4);
    text-align: center;

    & > .paragraphs {
      display: block;

      & > :not(:last-child) {
        margin-bottom: spacer(3);
      }
    }

    & > .wordmark {
      display: inline-block;
      margin-top: spacer(6);
      text-decoration: none;

      img {
        vertical-align: text-bottom;
        margin-left: spacer(1);
      }
    }
  }

  .rich-text {
    button {
      /* Overrides default display: inline-grid which is unsupported in certain email clients */
      /* Ensures margins consistency between custom button and other elements */
      display: block;
      width: max-content;
    }
  }

  .item {
    border: $border;
    border-radius: border-radius(1);
    overflow: hidden;

    .product-checkout-cell {
      display: table;
      table-layout: fixed;
      width: 100%;
      border-bottom: $border;
      height: size(8);

      & > .figure {
        // [3]
        display: table-cell;
        background: url("~images/placeholders/product-cover.png");
        background-size: cover;
        width: size(8);
        border-right: $border;
      }

      & > .section {
        display: table-cell;
        padding: spacer(4);

        .content {
          display: table;
          table-layout: fixed;
          width: 100%;
          height: 100%;

          & > * {
            display: table-row;

            &:first-child {
              height: 100%;
            }

            & > * {
              display: block;
              @include text-singleline;
              // Gmail inserts `wbr`s to break long text into multiple lines, which can result in
              // multiple lines truncated with ellipses without this.
              max-height: size(nth($line-heights, 3));
            }

            h4 {
              font-weight: bold;
            }

            .footer {
              // Overwrites font-size(2) set globally on .email
              @include font-size(1);

              .user-avatar {
                display: inline;
                margin-right: spacer(2);
              }

              &.product-stats {
                & > * {
                  // To vertically align the icon and text
                  line-height: size(0.875);

                  margin-right: spacer(2);

                  img {
                    display: inline;
                    width: size(0.875);
                    height: size(0.875);
                    margin-right: spacer(1);
                  }
                }
              }
            }
          }
        }
      }
    }

    .details {
      padding: spacer(4);

      & > * {
        margin: spacer(4) 0 0;

        &:first-child {
          margin-top: 0;
        }
      }
    }
  }
}

// TODO: Remove this along with CreatorMailer.gumroad_day_fee_saved mailer once emails are sent
.announcement {
  .main {
    & > .header {
      padding: 0;

      @include breakpoint-up(sm) {
        // [2]
        padding: 0 !important;
      }
    }

    & > *:not(.header) {
      padding: spacer(6);

      @include breakpoint-up(sm) {
        // [2]
        padding: spacer(6) !important;
      }
    }

    .button {
      &.primary {
        /* Some email clients don't support CSS variables: https://www.caniemail.com/search/?s=var */
        background-color: #ff90e8;
        color: #000000;
      }
    }
  }
}

.post {
  .title {
    text-decoration: none;
  }

  .main,
  .footer {
    max-width: size(35);
  }

  .main {
    border: none;

    & > * {
      margin-bottom: spacer(5);
      padding: 0;
      border-bottom: none;

      > * {
        margin-top: spacer(5);
      }

      @include breakpoint-up(sm) {
        // [2]
        padding: 0 !important;
      }
    }
  }

  .footer {
    border-top: $border;
    padding-top: spacer(5);
  }
}

.product-card {
  @extend article;

  .carousel {
    background: none;
    background-size: unset;
    position: unset;
    padding-bottom: 0;

    .items {
      position: unset;

      & img {
        width: 100%;
      }
    }
  }

  // [6]
  .header {
    @extend header;
    display: block; // [5]

    & > :not(:last-child) {
      @include text-singleline;
      padding-bottom: spacer(2);
    }

    .rating {
      padding-top: spacer(2); // [5]
    }

    .user {
      display: inline;

      & > :last-child {
        padding-top: spacer(2); // [5]
      }

      .user-avatar {
        display: inline;
        margin-right: spacer(2); // [5]
      }
    }
  }

  .footer {
    @extend footer;
  }

  &.horizontal {
    // [3]
    display: table;
    table-layout: fixed;
    width: 100%;

    // [6]
    .figure,
    .section {
      display: table-cell;
      vertical-align: top;
    }

    // [3] (Using explicit width and inline-block as an approximation of the thumbnail grid layout)
    .figure {
      width: size(8);
    }
    .thumbnails {
      display: block;
      text-align: center;
      background-color: $accent-color; // [1]

      > * {
        display: inline-block;
        width: 100%;
      }

      > :not(:only-child) {
        // Slightly less than 50% to account for the space between thumbnails. Gmail mobile support for calc() is patchy: https://www.caniemail.com/features/css-unit-calc/
        width: 47%;
      }

      > :nth-child(n + 3) {
        margin-top: spacer(1);
      }
    }

    // Premailer only inlines mobile styles, so this overrides them to replicate the horizontal desktop layout
    .thumbnails {
      border-bottom: none;
      border-right: $border;
      border-radius: border-radius(1) 0 0 border-radius(1);
    }

    .header {
      border-bottom: none;
    }
  }
}

.price {
  display: inline-block;
  background: $accent-color url("~images/email/price_tag_arrow.png") no-repeat right / auto 100%; // [1] [4]
}

.review {
  > .content {
    > * {
      margin: spacer(2) 0 0;

      &:first-child {
        margin-top: 0;
      }
    }

    > .byline {
      > * {
        vertical-align: middle;
      }
      .user-avatar {
        display: inline;
      }
    }

    &.response {
      margin-left: spacer(5);
    }
  }
}

.receipt {
  .info-row {
    margin-bottom: spacer(2);
    overflow: hidden;

    &.with-top-margin {
      margin-top: spacer(1);
    }
  }

  .info-label {
    font-weight: bold;
    float: left;
  }

  .info-value {
    float: right;
  }
}
