//
// Copyright IBM Corp. 2016, 2018
//
// This source code is licensed under the Apache-2.0 license found in the
// LICENSE file in the root directory of this source tree.
//

//-----------------------------
// Modals
//-----------------------------

@import '../../globals/scss/vars';
@import '../../globals/scss/helper-mixins';
@import '../../globals/scss/layout';
@import '../../globals/scss/vendor/@carbon/elements/scss/import-once/import-once';
@import '../../globals/scss/css--reset';

@import '../button/button';

/// Modal styles
/// @access private
/// @group modal
@mixin modal {
  .#{$prefix}--modal {
    position: fixed;
    top: 0;
    left: 0;
    z-index: z('modal');
    display: block;
    align-items: center;
    justify-content: center;
    width: 100vw;
    height: 100vh;
    background-color: transparent;
    visibility: hidden;
    opacity: 0;
    transition: background-color 0ms motion(exit, expressive),
      opacity 0ms motion(exit, expressive),
      visibility 0ms linear 0ms,
      display 0ms linear;
    content: '';

    &.is-visible {
      background-color: $overlay-01;
      visibility: inherit;
      display: flex;
      opacity: 1;
      transition: background-color $duration--slow-02 motion(entrance, expressive),
        opacity $duration--moderate-02 motion(entrance, expressive),
        visibility 0ms linear,
        display 0ms linear;
    }

    .#{$prefix}--text-input,
    .#{$prefix}--text-area,
    .#{$prefix}--search-input,
    .#{$prefix}--select-input,
    .#{$prefix}--dropdown,
    .#{$prefix}--dropdown-list,
    .#{$prefix}--number input[type='number'],
    .#{$prefix}--date-picker__input {
      background-color: $field-02;
    }
  }

  .#{$prefix}--modal.is-visible .#{$prefix}--modal-container {
    transform: translate3d(0, 0, 0);
    transition: transform $duration--moderate-02 motion(entrance, expressive);
  }

  .#{$prefix}--modal-container {
    position: fixed;
    top: 0;
    display: grid;
    grid-template-rows: auto 1fr auto;
    grid-template-columns: auto;
    width: 100%;
    height: 100%;
    max-height: 100%;
    overflow: hidden;
    background-color: $ui-01;
    // make modal edge visible on high contrast themes (#3880)
    outline: 3px solid transparent;
    outline-offset: -3px;
    transform: translate3d(0, -24px, 0);
    transform-origin: top center;
    transition: transform $duration--moderate-02 motion(exit, expressive);

    @include carbon--breakpoint(md) {
      position: static;
      width: 84%;
      height: auto;
      max-height: 90%;

      .#{$prefix}--modal-header,
      .#{$prefix}--modal-content,
      .#{$prefix}--modal-content__regular-content {
        padding-right: 20%;
      }

      .#{$prefix}--modal-content--with-form {
        // Override for `.#{$prefix}--modal-content`
        padding-right: $spacing-05;
      }
    }

    @include carbon--breakpoint(lg) {
      width: 60%;
      max-height: 84%;
    }

    @include carbon--breakpoint(xlg) {
      width: 48%;
    }
  }

  .#{$prefix}--modal-header,
  .#{$prefix}--modal-content {
    padding-left: $spacing-05;
  }

  .#{$prefix}--modal-header,
  .#{$prefix}--modal-content,
  .#{$prefix}--modal-content__regular-content {
    padding-right: $spacing-05;
  }

  .#{$prefix}--modal-content--with-form {
    padding-right: $spacing-05;
  }

  .#{$prefix}--modal-container--xs {
    .#{$prefix}--modal-header {
      padding-right: $carbon--spacing-09;
    }

    .#{$prefix}--modal-content,
    .#{$prefix}--modal-content__regular-content,
    .#{$prefix}--modal-content--with-form {
      padding-right: $spacing-05;
    }

    @include carbon--breakpoint(md) {
      width: 48%;
    }

    @include carbon--breakpoint(lg) {
      width: 32%;
      max-height: 48%;
    }

    @include carbon--breakpoint(xlg) {
      width: 24%;
    }
  }

  .#{$prefix}--modal-container--sm {
    .#{$prefix}--modal-header {
      padding-right: $carbon--spacing-09;
    }

    .#{$prefix}--modal-content,
    .#{$prefix}--modal-content__regular-content,
    .#{$prefix}--modal-content--with-form {
      padding-right: $spacing-05;
    }

    @include carbon--breakpoint(md) {
      width: 60%;
    }

    @include carbon--breakpoint(lg) {
      width: 42%;
      max-height: 72%;
    }

    @include carbon--breakpoint(xlg) {
      width: 36%;

      .#{$prefix}--modal-header,
      .#{$prefix}--modal-content,
      .#{$prefix}--modal-content__regular-content {
        padding-right: 20%;
      }

      .#{$prefix}--modal-content--with-form {
        // Override for `.#{$prefix}--modal-content`
        padding-right: $spacing-05;
      }
    }
  }

  .#{$prefix}--modal-container--lg {
    .#{$prefix}--modal-header {
      padding-right: $carbon--spacing-09;
    }

    .#{$prefix}--modal-content,
    .#{$prefix}--modal-content__regular-content,
    .#{$prefix}--modal-content--with-form {
      padding-right: $spacing-05;
    }

    @include carbon--breakpoint(md) {
      width: 96%;

      .#{$prefix}--modal-header,
      .#{$prefix}--modal-content,
      .#{$prefix}--modal-content__regular-content {
        padding-right: 20%;
      }

      .#{$prefix}--modal-content--with-form {
        // Override for `.#{$prefix}--modal-content`
        padding-right: $spacing-05;
      }
    }

    @include carbon--breakpoint(lg) {
      width: 84%;
      max-height: 96%;
    }

    @include carbon--breakpoint(xlg) {
      width: 72%;
    }
  }

  .#{$prefix}--modal-header {
    grid-row: 1/1;
    grid-column: 1/-1;
    margin-bottom: $spacing-03;
    padding-top: $spacing-05;
    padding-right: $spacing-09;
  }

  .#{$prefix}--modal-header__label {
    @include type-style('label-01');

    margin-bottom: $spacing-02;
    color: $text-02;
  }

  .#{$prefix}--modal-header__heading {
    @include type-style('productive-heading-03');

    color: $text-01;
  }

  .#{$prefix}--modal-content {
    @include type-style('body-long-01');

    grid-row: 2/-2;
    grid-column: 1/-1;
    margin-bottom: $spacing-09;

    // Required to accommodate focus outline's negative offset:
    padding-top: $spacing-03;

    overflow-y: auto;
    color: $text-01;
    font-weight: 400;

    &:focus {
      @include focus-outline('outline');
    }

    > * {
      @include type-style('body-long-01');
    }
  }

  // Required so overflow-indicator disappears at end of content
  .#{$prefix}--modal-scroll-content > *:last-child {
    padding-bottom: $spacing-07;
  }

  .#{$prefix}--modal-content--overflow-indicator {
    position: absolute;
    bottom: $spacing-09;
    left: 0;
    grid-row: 2/-2;
    grid-column: 1/-1;
    width: 100%;
    height: rem(32px);
    // Safari interprets `transparent` differently, so make color token value transparent instead:
    background-image: linear-gradient(to bottom, rgba($ui-01, 0), $ui-01);
    content: '';
    pointer-events: none;
  }

  .#{$prefix}--modal-content:focus
    ~ .#{$prefix}--modal-content--overflow-indicator {
    width: calc(100% - 4px);
    margin: 0 2px 2px;
  }

  @media screen and (-ms-high-contrast: active) {
    .#{$prefix}--modal-scroll-content > *:last-child {
      padding-bottom: 0;
    }

    .#{$prefix}--modal-content--overflow-indicator {
      display: none;
    }
  }

  .#{$prefix}--modal-footer {
    display: flex;

    grid-row: -1/-1;
    grid-column: 1/-1;
    justify-content: flex-end;
    height: 4rem;
    margin-top: auto;

    .#{$prefix}--btn {
      flex: 0 1 50%;
      max-width: none;
      height: 4rem;
      margin: 0;
      padding-top: $spacing-05;
      padding-bottom: $spacing-07;
    }
  }

  .#{$prefix}--modal-close {
    position: absolute;
    top: 0;
    right: 0;
    width: 3rem;
    height: 3rem;
    padding: rem(12px);
    overflow: hidden;
    background-color: transparent;
    border: 2px solid transparent;
    cursor: pointer;
    transition: background-color $duration--fast-02 motion(standard, productive);

    &:hover {
      background-color: $hover-ui;
    }

    &:focus {
      border-color: $focus;
      outline: none;
    }
  }

  .#{$prefix}--modal-close::-moz-focus-inner {
    border: 0;
  }

  .#{$prefix}--modal-close__icon {
    width: rem(20px);
    height: rem(20px);
    fill: $icon-01;
  }

  .#{$prefix}--body--with-modal-open {
    overflow: hidden;
  }

  .#{$prefix}--body--with-modal-open .#{$prefix}--tooltip {
    z-index: z('modal');
  }
}

@include exports('modal') {
  @include modal;
}
