$select-checkbox-border-radius: 2px !default;
$select-checkbox-border-width: 2px !default;
$select-border-width-default: 1px !default;
$select-checkbox-width: rem(1.4) !default;
$select-option-height: 48px !default;
$select-option-padding: 16px !default;
$select-container-padding: 16px !default;
$select-container-transition-duration: 350ms !default;
$select-value-padding-top: 2px;
$select-value-padding-bottom: 1px;

$select-max-visible-options: 5 !default;

$input-alignment: ($input-padding-top + $input-padding-bottom)
                   - ($select-value-padding-top + $select-value-padding-bottom);
$md-inline-alignment: ($input-container-vertical-margin + $input-container-padding)
                      + ($input-padding-top + $input-padding-bottom)
                      - ($select-value-padding-top + $select-value-padding-bottom);

// Fixes the animations with the floating label when select is inside an input container
md-input-container {
  &:not([md-no-float]) {
    .md-select-placeholder span:first-child {
      transition: transform $swift-ease-out-duration $swift-ease-out-timing-function;
      @include rtl(transform-origin, left top, right top);
    }
  }
  &.md-input-focused {
    &:not([md-no-float]) {
      md-select:not([placeholder]) .md-select-placeholder span:first-child {
        transform: translate(-2px, -22px) scale(0.75);
      }
    }
  }
}

.md-select-menu-container {
  position: fixed;
  left: 0;
  top: 0;
  z-index: $z-index-select;
  opacity: 0;
  display: none;

  // Fix 1px alignment issue to line up with text inputs (and spec)
  transform: translateY(-1px);

  // Don't let the user select a new choice while it's animating
  &:not(.md-clickable) {
    pointer-events: none;
  }

  md-progress-circular {
    display: table;
    margin: 3*$baseline-grid auto !important;
  }

  // enter: md-select scales in, then options fade in.
  &.md-active {
    display: block;
    opacity: 1;
    md-select-menu {
      transition: $swift-ease-out;
      transition-duration: 150ms;
      > * {
        opacity: 1;
        transition: $swift-ease-in;
        transition-duration: 150ms;
        transition-delay: 100ms;
      }
    }
  }

  // leave: the container fades out
  &.md-leave {
    opacity: 0;
    transition: $swift-ease-in;
    transition-duration: 250ms;
  }
}

.md-inline-form md-select {
  margin-top: $md-inline-alignment;
}

md-input-container {
  > md-select,
  .md-inline-form & > md-select {
    margin-top: $input-alignment;
  }
  > md-select {
    order: 2;
  }
}

// Show the asterisk on the placeholder if the element is required
//
// NOTE: When the input has a value and uses a floating label, the floating label will show the
// asterisk denoting that it is required
md-input-container:not(.md-input-has-value) {
  md-select[required]:not(.md-no-asterisk), md-select.ng-required:not(.md-no-asterisk) {
    .md-select-value span:first-child:after {
      content: ' *';
      font-size: 13px;
      vertical-align: top;
    }
  }
}

md-input-container.md-input-invalid {
  md-select {
    .md-select-value {
      border-bottom-style: solid;
      padding-bottom: 1px;
    }
  }
}

md-select {
  display: flex;

  &[required], &.ng-required {
    &.ng-empty.ng-invalid:not(.md-no-asterisk) {
      .md-select-value span:first-child:after {
        content: ' *';
        font-size: 13px;
        vertical-align: top;
      }
    }
  }

  &[disabled] .md-select-value {
    // This background-position was taken from the styling of disabled md-inputs.
    // The negative border width offsets the dotted "border" so it's placed in the same place as the
    // solid one before it.
    background-position: bottom $select-border-width-default * -1 left 0;
    // This background-size is coordinated with a linear-gradient set in select-theme.scss
    // to create a dotted line under the input.
    background-size: 4px 1px;
    background-repeat: repeat-x;
    // Add to padding-bottom to keep dotted line aligned with other bottom borders
    // Sub from padding-top to keep height consistent
    // Translate text 1px up to keep in alignment
    padding-bottom: $select-value-padding-bottom + 1;
    padding-top: $select-value-padding-top - 1;
    transform: translateY(1px);
  }

  &:focus {
    outline: none;
  }
  &[disabled]:hover {
    cursor: default;
  }
  &:not([disabled]) {
    &:hover {
      cursor: pointer
    }
    &:focus {
      .md-select-value {
        border-bottom-style: solid;
        border-bottom-width: $select-border-width-default + 1px;
        padding-bottom: $select-value-padding-bottom - 1px;
      }
    }
  }
}

md-input-container md-select {
  &:not([disabled]) {
    &:focus {
      .md-select-value {
        border-bottom-width: $input-border-width-focused;
      }
    }
  }
  &[disabled] {
    .md-select-value {
      // This background-position was taken from and matches the styling of disabled md-inputs.
      // The negative border width offsets the dotted "border" so it's placed in the same place as
      // the solid one before it.
      background-position: bottom $input-border-width-default * -1 left 0;
    }
  }
  .md-select-value {
    min-height: ($input-line-height + $input-padding-top * 2) - $input-border-width-focused - $input-border-width-default * 2;
    border-bottom-width: $input-border-width-default;
    padding-bottom: $input-border-width-focused - $input-border-width-default;
    &.md-select-placeholder {
      @include rtl(padding-left, 0, $input-container-padding);
      @include rtl(padding-right, $input-container-padding, 0);
    }
  }
}

.md-select-value {
  display: flex;
  align-items: center;
  padding-top: $select-value-padding-top;
  padding-bottom: $select-value-padding-bottom;
  @include rtl(padding-left, 0, $input-container-padding);
  @include rtl(padding-right, $input-container-padding, 0);
  border-bottom-width: $select-border-width-default;
  border-bottom-style: solid;
  background-color: rgba(0,0,0,0);
  position: relative;
  box-sizing: content-box;
  min-width: 11 * $baseline-grid;
  min-height: 26px;
  margin-bottom: auto;
  -ms-flex-item-align: start; // workaround for margin-bottom: auto
  flex-grow: 1;

  > span:not(.md-select-icon) {
    max-width: 100%;
    flex: 1 1 auto;
    text-overflow: ellipsis;
    white-space: nowrap;
    overflow: hidden;

    .md-text {
      display: inline;
    }
  }

  .md-select-icon {
    display: block;
    @include rtl(align-items, flex-end, flex-start);
    @include rtl(text-align, right, left);
    width: 3 * $baseline-grid;
    transform: translateY(-2px);
    font-size: 1.2rem;
  }

  .md-select-icon:after {
    display: block;
    content: '\25BC';
    position: relative;
    top: 2px;
    @include rtl(right, -4px, auto);
    @include rtl(left, auto, -4px);
    speak: none;
    font-size: 13px;
    transform: scaleY(0.5);
  }

  &.md-select-placeholder {
    display: flex;
    order: 1;
    pointer-events: none;
    -webkit-font-smoothing: antialiased;
    z-index: 1;
  }
}

md-select-menu {
  display: flex;
  flex-direction: column;
  &.md-reverse {
    flex-direction: column-reverse;
  }

  &:not(.md-overflow) {
    md-content {
      padding-top: $baseline-grid;
      padding-bottom: $baseline-grid;
    }
  }

  box-shadow: $whiteframe-shadow-1dp;
  max-height: ($select-option-height * $select-max-visible-options) + 2 * $baseline-grid;
  min-height: $select-option-height;
  overflow-y: hidden;

  @include rtl(transform-origin, left top, right top);

  transform: scale(1.0);

  md-content {
    min-width: 136px;
    min-height: $select-option-height;
    max-height: ($select-option-height * $select-max-visible-options) + 2 * $baseline-grid;
    overflow-y: auto;
  }
  > * {
    opacity: 0;
  }
}

md-option {
  cursor: pointer;
  position: relative;
  display: flex;
  align-items: center;
  width: auto;
  transition: background 0.15s linear;

  &[disabled] {
    cursor: default;
  }

  &:focus {
    outline: none;
  }

  .md-text {
    @include not-selectable();
    width: auto;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  padding: 0 $select-option-padding 0 $select-option-padding;
  height: $select-option-height;
}

md-optgroup {
  display: block;
  label {
    display: block;
    font-size: rem(1.4);
    text-transform: uppercase;
    padding: $baseline-grid * 2;
    font-weight: 500;
  }
  md-option {
    padding-left: $select-option-padding * 2;
    padding-right: $select-option-padding * 2;
  }
}

@media screen and (-ms-high-contrast: active) {
  .md-select-backdrop {
    background-color: transparent;
  }
  md-select-menu {
    border: 1px solid #fff;
  }
}

md-select-menu[multiple] {
  md-option.md-checkbox-enabled {
    @include rtl(padding-left, $select-option-padding * 2.5, $select-option-padding);
    @include rtl(padding-right, $select-option-padding, $select-option-padding * 2.5);

    @include checkbox-container('[selected]');

    .md-container {
      @include rtl(margin-left, $select-option-padding * math.div(2, 3), auto);
      @include rtl(margin-right, auto, $select-option-padding * math.div(2, 3));
    }
  }
}
