$gridstack-columns: 12;
$gridstack-rows: 200;
$gridstack-line-height: $box-padding;
$gridstack-vertical-margin: $box-padding;
$gridstack-min-height: 400px;

:root .grid-stack-item > .ui-resizable-handle {
  filter: none;
}

.grid-stack {
  $block: "grid-stack";

  @for $i from 1 through 100 {
    &[data-gs-current-height='#{$i}']{height: $i*$gridstack-line-height};
  }
  &[data-gs-current-height='NaN']{
    height: $gridstack-min-height;
  }

  position: relative;
  margin: 0 (-$gap);
  min-height: $gridstack-min-height;
  padding-bottom: 200px;

  &.#{$block}-rtl {
    direction: ltr;

    & > .#{$block}-item {
      direction: rtl;
    }
  }

  &-item-handle {
    position: absolute;
    top: 1px;
    left: $gap+1px;
    right: $gap+1px;
    padding: 0.2*$gap;
    border-bottom: 1px solid $color-border;
    background: $color-border-minor;
    text-align: center;
    line-height: 0;
    opacity: 0;
    z-index: 1;

    &:before {
      content: "";
      width: 26px;
      border-bottom: 2px dotted darken($color-border,10%);
      height: 2px;
      border-top: 2px dotted darken($color-border,10%);
      display: inline-block;
    }

    &:hover{
      cursor: move;
    }
  }
  &-placeholder{
    &:before{
      display: none;
    }
  }
  &-item-wrapper {
    position: absolute;
    min-width: 10%;
    padding: 0;
    overflow: hidden;
    @extend %compositor-layer;
    @extend %pseudo-before;

    @for $i from 1 through $gridstack-columns {
      &[data-gs-width='#{$i}'] { width: (100% / $gridstack-columns) * $i; }
      &[data-gs-x='#{$i}'] { left: (100% / $gridstack-columns) * $i; }
      &[data-gs-min-width='#{$i}'] { min-width: (100% / $gridstack-columns) * $i; }
      &[data-gs-max-width='#{$i}'] { max-width: (100% / $gridstack-columns) * $i; }
    }

    @for $i from 1 through $gridstack-rows {
      &[data-gs-height='#{$i}']{height: $i*$gridstack-line-height - $gridstack-vertical-margin};
      &[data-gs-y='#{$i}']{top: $i*$gridstack-line-height};
      &[data-gs-min-height='#{$i}']{min-height: $i*$gridstack-line-height - $gridstack-vertical-margin};
      &[data-gs-max-height='#{$i}']{max-height: $i*$gridstack-line-height - $gridstack-vertical-margin};
    }

    &[data-gs-width='NaN']{ // not nice - hardcoded but no clue how to do that better
      width: 25%;
      //height: 380px !important;
    }


    &:before{
      position: absolute;
      top: 0 !important;
      bottom: 0 !important;
      right: $gap;
      left: $gap;
      background: $color-foreground;
      border: 1px solid $color-border;
      z-index: -1;
      content: "" !important;
    }

    & > .box {
      position: absolute;
      top: 0;
      bottom: 0;
      left: $gap;
      right: $gap;
      margin-bottom: 0;
      //overflow-x: hidden;
      //overflow-y: auto;

      & > .module-content {
        position: absolute;
        top: 3*$box-margin;
        left: 0;
        right: 0;
        bottom: 0;
        margin: 0;
        overflow: hidden;
      }
    }

    & > .item-content {
      position: absolute;
      top: 0;
      left: $gap;
      right: $gap;
      bottom: 0;
      width: auto;
      margin: 0;
      z-index: 0 !important;
      overflow-x: hidden;
      overflow-y: auto;
    }

    & > .ui-resizable-handle {
      position: absolute;
      font-size: 0.1px;
      display: block;
      -ms-touch-action: none;
      touch-action: none;
      z-index: 1!important;
    }

    &.ui-resizable-disabled,
    &.ui-resizable-autohide {
      & > .ui-resizable-handle { display: none }
    }

    &.ui-draggable-dragging,
    &.ui-draggable-resizing {
      & > .ui-resizable-handle { z-index: 100;border: 1px solid red }

      & > .grid-stack-item-content {
        box-shadow: 1px 4px 6px rgba(0, 0, 0, 0.2);
        opacity: 0.8;
      }
    }

    & > .ui-resizable-se,
    & > .ui-resizable-sw {
      background-image: url(data:image/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0iaXNvLTg4NTktMSI/Pgo8IS0tIEdlbmVyYXRvcjogQWRvYmUgSWxsdXN0cmF0b3IgMTYuMC4wLCBTVkcgRXhwb3J0IFBsdWctSW4gLiBTVkcgVmVyc2lvbjogNi4wMCBCdWlsZCAwKSAgLS0+CjwhRE9DVFlQRSBzdmcgUFVCTElDICItLy9XM0MvL0RURCBTVkcgMS4xLy9FTiIgImh0dHA6Ly93d3cudzMub3JnL0dyYXBoaWNzL1NWRy8xLjE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sMTIuODQ3LDUuNDI0YzQuOTUsMCw5LjIzNC0xLjgwNywxMi44NDktNS40MjQgICBsNzMuMDg3LTczLjA4OGMzLjYxMy0zLjYyLDUuNDIxLTcuOTAxLDUuNDIxLTEyLjg0N2MwLTQuOTQ4LTEuODA4LTkuMjMyLTUuNDIxLTEyLjg1NCAgIEMzMzguMTQyLDQwMy44MDIsMzMzLjg1Nyw0MDEuOTk0LDMyOC45MDYsNDAxLjk5NHoiIGZpbGw9IiM2NjY2NjYiLz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8Zz4KPC9nPgo8L3N2Zz4K);
      background-repeat: no-repeat;
      background-position: center;
    }

    & > .ui-resizable-se { @extend %rotate-45ccw }
    & > .ui-resizable-sw { @extend %rotate-45cw }

    & > [class^="ui-resizable-"] {
      width: 2*$gap;
      height: 2*$gap;
    }

    @each $dir in nw, n, ne, e, w, sw, s, se {
      & > .ui-resizable-#{$dir} {
        cursor: #{$dir}-resize;

        @if $dir == 'nw' { top:    $gap; left:  2*$gap }
        @if $dir == 'ne' { top:    $gap; right: 2*$gap }
        @if $dir == 'sw' { bottom: $gap; left:  2*$gap }
        @if $dir == 'se' { bottom: $gap; right: 2*$gap }

        @if $dir == 'n'  { left: 0; right:  0; margin: auto; top:    2*$gap }
        @if $dir == 'e'  { top:  0; bottom: 0; margin: auto; right:  2*$gap }
        @if $dir == 's'  { left: 0; right:  0; margin: auto; bottom: 2*$gap }
        @if $dir == 'w'  { top:  0; bottom: 0; margin: auto; left:   2*$gap }
      }
    }

    & > .ui.resizable-nw {
      cursor: nw-resize;
      left: 2*$gap;
      top: 2*$gap;
    }

    &:hover {
      .grid-stack-item-handle {
        opacity: 1;
        .locked &{
          display: none;
        }
      }
    }
  }

  &-placeholder {
    .placeholder-content {
      position: absolute;
      top: 0;
      left: $gap;
      right: $gap;
      bottom: 0;
      width: auto;
      margin: 0;
      border: 1px dashed $color-important;
      z-index: 0 !important;
      text-align: center;
    }
  }

  &.grid-stack-animate {
    &, .grid-stack-item {
      @include transition(unquote("left 0.3s, top 0.3s, height 0.3s, width 0.3s"));
    }

    .grid-stack-item {
      &.ui-draggable-dragging,
      &.ui-resizable-resizing
      &.grid-stack-placeholder {
        @include transition(unquote("left 0s, top 0s, height 0s, width 0s"));
      }
    }
  }
}

@include respond-to(max-small-screen){
  .easy-page-module, .grid-stack-item-wrapper {
    position: relative;
    width: auto;
    left: 0;
    top: auto;
    margin-bottom: 10px;
    height: auto;
    min-height: auto;
  }

  .easy-page-module .ui-resizable-handle {
    display: none;
  }

  .grid-stack {
    height: auto;
  }
}
