@use '../../../../shared/styles/variables' as *;

.device-permissions {
  justify-content: space-between;
}

.device-permissions__header {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 12px;
  padding-bottom: 4px;
  border-bottom: 1px solid $gray20;

  h3 {
    line-height: 1;
    margin: 0;
    text-align: left;
    color: $color-text-heading;
    font-size: $font-size-18;
    font-weight: 600;
  }
}

.device-permissions__title {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 7px;
}

.device-permissions__help {
  position: relative;
  width: 18px;
  height: 18px;

  display: inline-flex;
  align-items: center;
  justify-content: center;

  color: $color-text-muted;
  cursor: help;
  outline: none;
  transition: color 0.18s ease;

  svg {
    width: 16px;
    height: 16px;
    stroke-width: 2.2;
    transition: .2s ease;
  }

  &:hover,
  &:focus-visible {
    color: $blue;
  }

  &:hover .device-permissions__help-tooltip,
  &:focus-visible .device-permissions__help-tooltip {
    opacity: 1;
    visibility: visible;
    transform: translateY(8px);
  }
}

.device-permissions__help-tooltip {
  position: absolute;
  z-index: 30;
  top: 100%;
  left: -34px;

  width: 400px;
  padding: 11px;
  border-radius: 12px;
  //background: rgba($color-surface-rgb, 0.94);
  backdrop-filter: blur(18px);
  box-shadow:
    0 14px 30px rgba($color-shadow-rgb, 0.16),
    inset 0 0 0 1px rgba($color-primary-rgb, 0.08);

  display: flex;
  flex-direction: column;
  gap: 12px;

  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translateY(2px);
  transition:
    opacity 0.18s ease,
    visibility 0.18s ease,
    transform 0.18s ease;

  &::before {
    content: '';
    position: absolute;
    left: 38px;
    bottom: 100%;
    width: 9px;
    height: 9px;
    background: rgba($color-surface-rgb, 0.94);
    box-shadow: -1px -1px 0 rgba($color-primary-rgb, 0.08);
    transform: translateY(5px) rotate(45deg);
  }
}

.device-permissions__help-row {
  display: grid;
  grid-template-columns: 22px 1fr;
  gap: 9px;
  align-items: center;

  >svg {
    margin-top: 1px;
    width: 20px;
    height: 20px;
    color: $blue;
  }

  span {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    text-align: left;
    gap: 2px;
  }

  b {
    color: $color-text-heading;
    font-size: $font-size-14;
    font-weight: 700;
    line-height: 1.2;
  }

  small {
    color: $color-text-muted;
    font-size: $font-size-13;
    font-weight: 500;
    line-height: 1.28;
  }
}

.device-permissions__header-actions {
  position: relative;
  width: 82px;
  height: 36px;
  flex: 0 0 82px;
}

.device-permissions__icon-btn {
  position: absolute;
  top: 0;
  right: 0;

  width: 34px;
  height: 34px;
  padding: 0;
  border: none;
  border-radius: 12px;
  background: $color-bg;

  display: inline-flex;
  align-items: center;
  justify-content: center;

  color: $gray50;
  cursor: pointer;
  transition:
    background-color 0.18s ease,
    color 0.18s ease,
    opacity 0.18s ease,
    transform 0.18s ease,
    box-shadow 0.18s ease;

  &:hover:not(:disabled) {
    background: $gray20;
    color: $color-text;
    //transform: scale(1.05);
  }

  &:disabled {
    opacity: 0.45;
    cursor: default;
  }
}

.device-permissions__icon-btn--edit {
  z-index: 2;
  background: $blue20;
  color: $blue;

  &:hover:not(:disabled) {
    background: $blue;
    color: $color-text-inverse;
    box-shadow: 0 8px 20px rgba($color-primary-rgb, 0.18);
  }
}

.device-permissions__icon-btn--reset {
  z-index: 1;
  opacity: 0;
  pointer-events: none;
  transform: translateX(0) scale(0.92);
  transition:
    background-color 0.18s ease,
    color 0.18s ease,
    opacity 0.18s ease,
    transform 0.28s cubic-bezier(0.34, 1.42, 0.64, 1);
}

.device-permissions.is-editing {
  .device-permissions__icon-btn--reset {
    opacity: 1;
    pointer-events: auto;
    transform: translateX(-46px) scale(1);

    &:disabled {
      opacity: 0.45;
      pointer-events: none;
    }
  }
}

.device-permissions:not(.is-policy-editing) {
  .device-permission {
    opacity: 0.72;
  }

  .device-permission__switch {
    cursor: not-allowed;
  }
}

.device-permissions__list {
  display: flex;
  flex-direction: column;
}

.device-permission__controls {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

.device-permission__policy-lock {
  position: relative;
  z-index: 3;
  width: 34px;
  height: 34px;
  flex: 0 0 34px;
  border-radius: 9px;
  background: $color-surface-hover;

  display: inline-flex;
  align-items: center;
  justify-content: center;

  color: $color-text-muted;
  cursor: help;
  outline: none;
  transition:
    background-color 0.18s ease,
    color 0.18s ease,
    transform 0.18s ease;

  svg {
    width: 18px !important;
    height: 18px !important;
    padding: 0 !important;
    border-radius: 0 !important;
    background: $transparent !important;
    color: currentColor;
    stroke-width: 2.35;
  }

  &:hover,
  &:focus-visible {
    z-index: 120;
    color: $blue;
    background: $blue20;
    transform: translateY(-1px);
  }

  &:hover .device-permission__policy-lock-tooltip,
  &:focus-visible .device-permission__policy-lock-tooltip {
    opacity: 1;
    visibility: visible;
    transform: translate(-50%, -8px);
  }
}

.device-permission__policy-lock-tooltip {
  position: absolute;
  z-index: 110;
  left: 50%;
  bottom: calc(100% + 4px);

  width: 260px;
  padding: 9px 11px;
  border-radius: 10px;
  //background: rgba($color-surface-rgb, 0.94);
  backdrop-filter: blur(18px);
  box-shadow:
    0 12px 28px rgba($color-shadow-rgb, 0.14),
    inset 0 0 0 1px rgba($color-primary-rgb, 0.08);

  color: $color-text-heading;
  font-size: $font-size-13;
  font-weight: 500;
  line-height: 1.32;
  text-align: left;

  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translate(-50%, -2px);
  transition:
    opacity 0.18s ease,
    visibility 0.18s ease,
    transform 0.18s ease;

  &::after {
    content: '';
    position: absolute;
    top: 100%;
    left: 50%;
    width: 9px;
    height: 9px;
    background: rgba($color-surface-rgb, 0.94);
    box-shadow: 1px 1px 0 rgba($color-primary-rgb, 0.08);
    transform: translate(-50%, -5px) rotate(45deg);
  }
}

.device-permission__switch {
  position: relative;

  width: 118px;
  height: 28px;
  flex: 0 0 118px;
  padding: 3px;
  border: none;
  outline: none;

  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  align-items: stretch;

  border-radius: 999px;
  background: $color-bg;
  box-shadow: inset 0 0 0 1px rgba($gray50-rgb, 0.08);

  cursor: pointer;
  user-select: none;

  transition:
    background-color 0.28s ease,
    box-shadow 0.28s ease,
    transform 0.18s ease;

  &--component {
    width: 78px;
    flex-basis: 78px;
    grid-template-columns: repeat(2, minmax(0, 1fr));

    .device-permission__switch-thumb {
      width: calc((100% - 6px) / 2) !important;
    }
  }

  .device-permission__switch-thumb {
    position: absolute;
    top: 3px;
    left: 3px;

    width: calc((100% - 6px) / 3);
    height: calc(100% - 6px);
    border-radius: 100px;
    background: $color-surface;

    box-shadow:
      0 2px 5px rgba($color-shadow-rgb, 0.18),
      0 1px 1px rgba($color-shadow-rgb, 0.08);

    transition:
      transform 0.28s cubic-bezier(0.34, 1.56, 0.64, 1),
      box-shadow 0.28s ease,
      width 0.18s ease;
  }

  .device-permission__switch-option {
    position: relative;
    z-index: 1;

    min-width: 0;
    padding: 0;
    border: none;
    background: $transparent;

    display: inline-flex;
    align-items: center;
    justify-content: center;

    color: $gray50;
    cursor: pointer;
    outline: none;
    -webkit-tap-highlight-color: $transparent;

    &:focus-visible {
      border-radius: 999px;
      //box-shadow: 0 0 0 2px rgba($color-primary-rgb, 0.22);
    }
  }

  .device-permission__switch-option[aria-disabled='true'] {
    cursor: not-allowed;
  }

  .device-permission__switch-label {
    display: inline-flex;
    align-items: center;
    justify-content: center;

    transition:
      color 0.2s ease,
      opacity 0.2s ease;

    svg {
      width: 14px;
      height: 14px;
      stroke-width: 2.35;
      background-color: transparent;
    }
  }

  &.is-unspecified {
    box-shadow: inset 0 0 0 1px rgba($color-primary-rgb, 0.1);

    .device-permission__switch-thumb {
      transform: translateX(100%);
      background: $blue20;
      box-shadow:
        0 3px 7px -4px rgba($color-primary-rgb),
        0 1px 1px rgba($color-shadow-rgb, 0.08);
    }

    .device-permission__switch-option.is-active .device-permission__switch-label {
      color: $blue;
    }
  }

  &.is-enabled,
  &.is-allowed {
    background: rgba($color-primary-rgb, 0.1);
    box-shadow: inset 0 0 0 1px rgba($color-primary-rgb, 0.12);

    .device-permission__switch-thumb {
      background: $blue;
      box-shadow:
        0 3px 7px rgba($color-primary-rgb, 0.24),
        0 1px 1px rgba($color-shadow-rgb, 0.08);
    }

    .device-permission__switch-option.is-active .device-permission__switch-label {
      color: $color-text-inverse;
      svg{
        color: $white;
      }
    }
  }

  &.is-enabled {
    .device-permission__switch-thumb {
      transform: translateX(200%);
    }
  }

  &.is-allowed {
    .device-permission__switch-thumb {
      transform: translateX(100%);
    }
  }

  &.is-disabled,
  &.is-disallowed {
    .device-permission__switch-option.is-active .device-permission__switch-label {
      color: $color-text;
    }
  }

  &:hover:not(.is-disabled) {
    //box-shadow:
      //inset 0 0 0 1px rgba($gray50-rgb, 0.12),
      //0 4px 10px rgba($color-shadow-rgb, 0.06);
  }

  &:active:not(.is-disabled) {
    transform: scale(0.96);
  }

  &:focus-within {
    box-shadow:
      //0 0 0 3px rgba($color-primary-rgb, 0.18),
      inset 0 0 0 1px rgba($color-primary-rgb, 0.18);
  }

  &.is-disabled {
    cursor: not-allowed;
  }
}

.device-permission {
  transition:
    opacity 0.2s ease,
    filter 0.2s ease;

  &.is-policy-locked {
    cursor: not-allowed;

    >svg,
    .device-permission__label>span:first-child,
    .device-permission__switch {
      opacity: 0.52;
    }

    .device-permission__switch {
      cursor: not-allowed;
      box-shadow: inset 0 0 0 1px rgba($gray50-rgb, 0.08);
    }
  }
}

.device-permissions:not(.is-policy-editing) {
  .device-permission.is-policy-locked {
    opacity: 1;
  }
}

.device-permissions__error {
  margin-top: 10px;
  padding: 6px 8px;
  border-radius: 9px;
  background: rgba($color-danger-rgb, 0.1);

  color: $red;
  font-size: $font-size-13;
  font-weight: 600;
  line-height: 1.25;
  text-align: left;
}

[data-theme='dark'] {
  .device-permission__switch:not(.is-enabled):not(.is-allowed):not(.is-unspecified) {
    svg {
      color: $blue !important;
      stroke-width: 3px;

      path {
        box-shadow: 0 0 1px 0px $blue;
      }
    }

    .device-permission__switch-thumb {
      background: $white;
    }
  }

  .device-permission__switch {

    &.is-disabled,
    &.is-disallowed {
      .device-permission__switch-option.is-active .device-permission__switch-label {
        color: $neutral-900;
      }
    }

    &:hover:not(.is-disabled) {
      //box-shadow:
        //inset 0 0 0 1px rgba($gray50-rgb, 0.16),
        //0 4px 10px rgba($color-shadow-rgb, 0.08);
    }
  }

  .device-permissions__icon-btn--reset {
    background: $color-surface-hover;
    color: $color-text-muted;
  }

  .device-permissions__icon-btn:not(.device-permissions__icon-btn--edit):hover:not(:disabled) {
    background: $color-surface-hover;
    color: $color-text-heading;
  }

  .device-permissions__icon-btn--reset:hover:not(:disabled) {
    background: $color-surface-active;
    color: $color-text-heading;
  }

  .device-permissions__icon-btn--edit:hover:not(:disabled) {
    background: $blue;
    color: $color-text-inverse;
    box-shadow: 0 8px 20px rgba($color-primary-rgb, 0.18);
  }
}
