@import '~@thumbtack/thumbprint-tokens/dist/scss/_index';
@import '~@thumbtack/thumbprint-scss/mixins';

$header-height-small: 56px;
$header-height-large: $tp-space__6;

// These values are duplicated in the JavaScript.
$transition-open-duration: $tp-duration__5;
$transition-close-duration: $tp-duration__4;

$easing: $tp-ease__in-out;

$width-narrow: 416px;
$width-medium: 632px;
$width-wide: 1400px;

$height-medium: 600px;
$height-tall: 900px;

$contents-padding-small: 20px;
$contents-padding-large: $tp-space__5;

.curtain {
    // ModalCurtain closing transition on small screens.
    transition: opacity 0ms $easing $transition-close-duration,
        visibility $transition-close-duration $easing;

    &Open {
        // ModalCurtain opening transition on small screen.
        transition: opacity 0ms $easing;
    }

    @include tp-respond-above($tp-breakpoint__small) {
        background-color: rgba(0, 0, 0, 0.8);

        // ModalCurtain closing on medium and large screens.
        transition: opacity $transition-close-duration $easing,
            visibility $transition-close-duration $easing;

        &Open {
            // ModalCurtain opening on medium and large screens.
            transition: opacity $transition-open-duration $easing;
        }
    }
}

.curtainInner {
    padding: 0;
    display: flex;
    flex-direction: column;
    flex: 1 0 auto; // Stretch to fit parent

    // Height/max-height are the same as .wrapper's height and max-height
    // They need to be explicitly specified in this node (i.e. .wrapper's containing block)
    // such that percentage-based height on .wrapper can be properly derived
    height: 100%;
    max-height: 100%;

    @include tp-respond-above($tp-breakpoint__small) {
        padding: $tp-space__5;

        height: auto;

        &ShouldPageScrollAboveSmall {
            max-height: none;
        }
    }
}

.wrapper {
    width: 100%;

    // At the small breakpoint, the modal is always 100% height and the page can not scroll.
    // The modal itself must handle any vertical overflow.
    height: 100%;
    display: flex;

    // Prevent the modal from growing taller than the page. This is primarly needed for modals
    // with sticky footers to not grow larger than the viewport when viewed above the small
    // breakpoint. We rely on `max-height` for that scenario so the modal is never taller than its
    // contents.
    max-height: 100%;

    transform: translate(0, 100%);
    // Wrapper close transition on small screens.
    transition: transform $transition-close-duration $easing;

    &Open {
        // Wrapper open transition on small screens.
        transition: transform $transition-open-duration $easing;
        transform: translate(0, 0);
    }

    @include tp-respond-above($tp-breakpoint__small) {
        // We don't force a `height: 100%` here because we don't want the modal to be taller than
        // its contents.
        height: auto;

        // If desired, we allow the modal to grow vertically beyond the height of the viewport. If
        // the modal is tall enough, this will make the page scroll. Without this, the modal won't
        // grow taller than the viewport and the modal contents itself will scroll.
        &ShouldPageScrollAboveSmall {
            max-height: none;
        }

        // Center the modal horizontally and vertically.
        margin: auto;
        transition: none;
        // Even if the curtain animates, the modal wrapper will appear and disappear immediately on
        // medium and large screens.
        opacity: 0;

        &Open {
            transition: none;
            opacity: 1;
        }
    }

    &Narrow {
        max-width: $width-narrow;

        // The `max-width` doesn't apply on small devices because we want the modal to take up the
        // entire screen.
        @include tp-respond-below($tp-breakpoint__small) {
            max-width: 100%;
        }
    }

    &Medium {
        max-width: $width-medium;

        // The `max-width` doesn't apply on small devices because we want the modal to take up the
        // entire screen.
        @include tp-respond-below($tp-breakpoint__small) {
            max-width: 100%;
        }
    }

    &Wide {
        max-width: $width-wide;

        // The `max-width` doesn't apply on small devices because we want the modal to take up the
        // entire screen.
        @include tp-respond-below($tp-breakpoint__small) {
            max-width: 100%;
        }
    }

    &HeightMedium {
        @include tp-respond-above($tp-breakpoint__small) {
            height: $height-medium;
        }
    }

    &HeightTall {
        @include tp-respond-above($tp-breakpoint__small) {
            height: $height-tall;
        }
    }
}

.container {
    background-color: $tp-color__white;
    position: relative;
    width: 100%;
    display: flex;
    flex-direction: column;
}

.contents {
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    // Momentum scrolling on iOS.
    -webkit-overflow-scrolling: touch;

    flex: 1 1 auto;
}

.contentsPadding {
    display: flex;
    flex-direction: column;
    flex: 1 0 auto;

    padding: $contents-padding-small $contents-padding-small $tp-space__5;

    @include tp-respond-above($tp-breakpoint__small) {
        &NotSticky {
            padding: 0 $contents-padding-large 40px;
        }
    }
}

.closeButton {
    background: $tp-color__white;
    display: flex;
    border-bottom: 1px solid $tp-color__gray;
    min-height: $header-height-small;
    align-items: flex-start;
    justify-content: flex-end;
    // The close button is at the bottom of the DOM so that it is not the first focused element
    // when the modal opens. We visually move it to the top with the flexbox `order` property.
    // This works better than absolute positioning because the browser will be able to properly
    // scroll to form fields when tabbing through a modal that is taller than the viewport.
    order: -1;

    @include tp-respond-above($tp-breakpoint__small) {
        // The close button should always have a line underneath if at the small breakpoint or if
        // the modal is sticky.
        &NotSticky {
            border-bottom: none;
            min-height: $header-height-large;
        }
    }

    &Icon {
        // Increase click target of the button to make it easier to tap.
        margin: $tp-space__3;
    }
}

.modalHeader {
    // This value works well even if `ModalContent` isn't used in the modal.
    margin-bottom: 12px;
}

.modalTitle {
    font-size: $tp-font__title__2__size;
    line-height: $tp-font__title__2__line-height;
    font-weight: $tp-font__title__2__weight;
}

.modalDescription {
    font-size: $tp-font__body__1__size;
    line-height: $tp-font__body__1__line-height;
    color: $tp-color__black-300;
}

.modalTitle + .modalDescription {
    margin-top: $tp-space__2;
}

.modalContent {
    // Add top padding, not bottom margin, because top padding is built into the `ModalFooter` due
    // to a top-border on mobile when sticky. Using padding prevents the margins from collapsing
    // with the margin from `ModalHeader`.
    padding-top: 12px;
}

// The double selectors in this file ensure that consumers using the `className` prop can't easily
// overwrite the styles that we want to enforce in the component.
.modalContentFullBleed.modalContentFullBleed {
    margin-left: -$contents-padding-small !important;
    margin-right: -$contents-padding-small !important;
    padding-left: $contents-padding-small !important;
    padding-right: $contents-padding-small !important;

    @include tp-respond-above($tp-breakpoint__small) {
        margin-left: -$contents-padding-large !important;
        margin-right: -$contents-padding-large !important;
        padding-left: $contents-padding-large !important;
        padding-right: $contents-padding-large !important;
    }
}

.modalFooterFluid {
    padding: $tp-space__4 0 0 0;

    @include tp-respond-above($tp-breakpoint__small) {
        padding: $tp-space__5 0 0 0;
    }
}
