.saas-app-peripheral {
  display: flex;
  flex-direction: column;
  gap: 20px;
  height: 100%;
  min-height: 0;
  padding: 24px;
  overflow: hidden;
  background: #fff;

  > .ant-spin-nested-loading {
    flex: 1;
    min-height: 0;

    > .ant-spin-container {
      display: flex;
      flex-direction: column;
      height: 100%;
      min-height: 0;
    }
  }

  &__header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 16px;
  }

  &__header-main {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    min-width: 0;
  }

  &__back {
    flex: 0 0 auto;
    margin-top: -1px;
  }

  &__spin {
    flex: 1;
    min-height: 0;
    overflow: auto;
  }

  &__title {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 24px;
    font-weight: 700;
    line-height: 32px;
    color: rgba(0, 0, 0, 0.88);

    .anticon {
      color: #1677ff;
    }
  }

  &__subtitle {
    margin-top: 4px;
    font-size: 14px;
    color: rgba(0, 0, 0, 0.45);
  }

  &__header-actions {
    display: flex;
    align-items: center;
    gap: 8px;
  }

  &__list {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: 16px;
    min-height: 0;
    overflow: auto;
  }

  &__card {
    display: flex;
    align-items: flex-start;
    gap: 16px;
    min-height: 98px;
    padding: 18px 20px;
    background: #f3f4f6;
    border-radius: 2px;
  }

  &__card-icon {
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    color: #faad14;
    border-radius: 4px;
    background: #fffbe6;
  }

  &__card-main {
    flex: 1;
    min-width: 0;
  }

  &__card-title-row {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
  }

  &__card-title {
    font-size: 15px;
    font-weight: 700;
    line-height: 22px;
    color: rgba(0, 0, 0, 0.88);
  }

  &__card-subtitle {
    margin-top: 4px;
    font-size: 14px;
    color: rgba(0, 0, 0, 0.55);
  }

  &__meta {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
    margin-top: 6px;
    font-size: 14px;
    color: rgba(0, 0, 0, 0.55);
  }

  &__tag {
    display: inline-flex;
    align-items: center;
    min-height: 20px;
    padding: 0 6px;
    font-size: 12px;
    color: rgba(0, 0, 0, 0.72);
    border: 1px solid #d9d9d9;
    border-radius: 4px;
    background: #fff;
  }

  &__card-actions {
    display: flex;
    flex: 0 0 auto;
    align-items: center;
    gap: 8px;
  }

  &__empty {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 260px;
  }

  &--phone {
    gap: 12px;
    height: 100dvh;
    padding: 12px 12px calc(16px + env(safe-area-inset-bottom));

    .saas-app-peripheral__header {
      flex-direction: column;
      gap: 12px;
    }

    .saas-app-peripheral__header-main {
      width: 100%;
    }

    .saas-app-peripheral__header-main > div {
      min-width: 0;
    }

    .saas-app-peripheral__title {
      font-size: 18px;
      line-height: 26px;
    }

    .saas-app-peripheral__subtitle {
      display: -webkit-box;
      overflow: hidden;
      -webkit-box-orient: vertical;
      -webkit-line-clamp: 2;
    }

    .saas-app-peripheral__header-actions,
    .saas-app-peripheral__card,
    .saas-app-peripheral__card-actions {
      width: 100%;
    }

    .saas-app-peripheral__header-actions {
      gap: 8px;
      justify-content: stretch;

      > button,
      > .ant-dropdown-trigger,
      > .pisell-lowcode-dropdown-trigger {
        flex: 1;
      }
    }

    .saas-app-peripheral__card {
      gap: 12px;
      flex-wrap: wrap;
      padding: 14px;
      border-radius: 8px;
    }

    .saas-app-peripheral__card-main {
      flex: 1 1 calc(100% - 52px);
    }

    .saas-app-peripheral__card-title-row {
      align-items: flex-start;
      justify-content: space-between;
    }

    .saas-app-peripheral__card-title {
      max-width: 100%;
    }

    .saas-app-peripheral__card-actions {
      gap: 8px;
      justify-content: stretch;
      padding-left: 52px;

      > button:first-child {
        flex: 1;
      }
    }

    .saas-app-peripheral__empty {
      min-height: 220px;
    }
  }
}

.saas-app-peripheral-remove-modal {
  &__desc {
    margin-bottom: 12px;
    color: rgba(0, 0, 0, 0.72);
  }

  &__option {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    width: 100%;
    padding: 16px;
    margin-top: 10px;
    color: rgba(0, 0, 0, 0.88);
    text-align: left;
    border: 1px solid #f0f0f0;
    border-radius: 8px;
    background: #fff;
    cursor: pointer;

    &:hover {
      border-color: #d9d9d9;
      background: #fafafa;
    }

    > .anticon {
      display: flex;
      flex: 0 0 auto;
      align-items: center;
      justify-content: center;
      width: 48px;
      height: 48px;
      margin-top: 2px;
      color: #d48806;
      border-radius: 10px;
      background: #fff7e6;
    }

    strong {
      display: block;
      font-size: 14px;
      line-height: 20px;
    }

    span {
      display: block;
      margin-top: 4px;
      font-size: 12px;
      line-height: 18px;
      color: rgba(0, 0, 0, 0.45);
    }

    &.is-danger {
      strong,
      > .anticon {
        color: #eb2f96;
      }

      > .anticon {
        background: #fff0f6;
      }
    }
  }
}

.saas-app-peripheral-physical-modal,
.saas-app-peripheral-usage-modal {
  &__list {
    display: flex;
    flex-direction: column;
    gap: 10px;
  }

  &__option {
    display: flex;
    align-items: flex-start;
    gap: 12px;
    width: 100%;
    padding: 14px;
    color: rgba(0, 0, 0, 0.88);
    text-align: left;
    border: 1px solid #f0f0f0;
    border-radius: 8px;
    background: #fff;
    cursor: pointer;

    &:hover {
      border-color: #d9d9d9;
      background: #fafafa;
    }

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

    > .anticon,
    > .pisell-lowcode-icon {
      display: flex;
      flex: 0 0 auto;
      align-items: center;
      justify-content: center;
      width: 40px;
      height: 40px;
      margin-top: 2px;
      color: #1677ff;
      border-radius: 8px;
      background: #e6f4ff;
    }

    strong {
      display: block;
      font-size: 14px;
      line-height: 20px;
    }

    span {
      display: block;
      margin-top: 4px;
      font-size: 12px;
      line-height: 18px;
      color: rgba(0, 0, 0, 0.45);
    }
  }
}

.saas-app-peripheral-edit-device-modal {
  &__row {
    display: flex;
    gap: 12px;
  }

  &__col {
    flex: 1;
    min-width: 0;
  }
}

.saas-app-peripheral-edit-device-modal--phone {
  width: 100%;
  max-width: 100%;

  .ant-modal-body,
  .pisell-lowcode-modal-body {
    max-height: calc(90dvh - 172px);
    overflow: auto;
  }

  .ant-modal-footer,
  .pisell-lowcode-modal-footer {
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }

  .saas-app-peripheral-edit-device-modal__row {
    flex-direction: column;
    gap: 0;
  }
}

.saas-app-peripheral-remove-modal--phone,
.saas-app-peripheral-confirm-modal--phone,
.saas-app-peripheral-physical-modal--phone,
.saas-app-peripheral-usage-modal--phone {
  width: 100%;
  max-width: 100%;

  .ant-modal-body,
  .pisell-lowcode-modal-body {
    max-height: calc(90dvh - 172px);
    overflow: auto;
  }

  .ant-modal-footer,
  .pisell-lowcode-modal-footer {
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }
}

.saas-app-peripheral-remove-modal--phone {
  .saas-app-peripheral-remove-modal__option {
    gap: 10px;
    padding: 14px;

    > .anticon {
      width: 40px;
      height: 40px;
    }
  }
}

.saas-app-peripheral-connect {
  &__modal-title {
    display: inline-flex;
    align-items: center;
    gap: 8px;
  }

  &__picker {
    display: grid;
    grid-template-columns: 260px 1fr;
    gap: 32px;

    &.is-new {
      grid-template-columns: 1fr;
    }
  }

  &__areas,
  &__targets,
  &__config {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  &__area-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
    overflow-y: auto;
    padding-right: 8px;
  }

  &__targets {
    min-width: 0;
  }

  &__device-list {
    overflow-y: auto;
    padding-right: 8px;
  }

  &__picker.is-existing {
    .saas-app-peripheral-connect__area-list,
    .saas-app-peripheral-connect__device-list {
      max-height: calc(80vh - 260px);
      overflow-y: auto;
    }
  }

  &__section-title {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    font-size: 16px;
    font-weight: 600;
    color: rgba(0, 0, 0, 0.88);
  }

  &__section-desc,
  &__help {
    font-size: 12px;
    color: rgba(0, 0, 0, 0.45);
  }

  &__area,
  &__device-card,
  &__model-card,
  &__option,
  &__summary-card {
    border: 1px solid #f0f0f0;
    border-radius: 8px;
    background: #fff;
  }

  &__area {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    min-height: 64px;
    padding: 12px;
    cursor: pointer;

    &.is-active {
      border-color: #1677ff;
      background: #e6f4ff;
    }
  }

  &__area-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    width: 100%;
  }

  &__current {
    margin-top: 4px;
    padding: 1px 6px;
    font-size: 11px;
    color: #1677ff;
    border-radius: 4px;
    background: #fff;
  }

  &__workarea-name {
    font-weight: 600;
  }

  &__filters,
  &__option-grid,
  &__summary {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
  }

  &__picker.is-new &__filters {
    grid-template-columns: 1fr auto auto;
  }

  &__picker.is-new &__model-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  &__model-grid,
  &__device-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
  }

  &__model-card,
  &__device-card {
    display: flex;
    gap: 12px;
    align-items: center;
    width: 100%;
    min-height: 76px;
    height: auto;
    padding: 12px;
    text-align: left;
    white-space: normal;
    cursor: pointer;

    &.is-disabled {
      color: rgba(0, 0, 0, 0.25);
      background: #fafafa;
      cursor: not-allowed;
      opacity: 0.55;
    }

    > div > span {
      display: block;
      color: rgba(0, 0, 0, 0.45);
    }
  }

  &__device-icon {
    display: inline-flex;
    flex: 0 0 auto;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border-radius: 8px;

    &.is-smart {
      background: #e6f4ff;
      color: #1677ff;
    }

    &.is-dumb {
      background: #fffbe6;
      color: #faad14;
    }
  }

  &__model-card {
    align-items: flex-start;
    flex-direction: column;
    min-height: 168px;
  }

  &__model-name {
    font-weight: 600;
  }

  &__summary {
    align-items: center;
    grid-template-columns: 1fr auto 1fr;

    > .anticon {
      justify-self: center;
    }
  }

  &__summary-card {
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: 88px;
    padding: 12px;
    background: #fafafa;

    &.is-smart {
      background: #f0f7ff;
      border-color: #d6e4ff;
    }

    &.is-dumb {
      background: #fffdf0;
      border-color: #ffe7ba;
    }

    > div > span {
      display: block;
      color: rgba(0, 0, 0, 0.45);
    }
  }

  &__empty {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 220px;
  }

  &__new-info {
    display: flex;
    flex-direction: column;
  }

  &__new-info .pisell-lowcode-select,
  &__new-info .ant-select,
  &__new-info .ant-input,
  &__new-info input {
    width: 100%;
  }

  &__option {
    display: flex;
    align-items: center;
    gap: 12px;
    min-height: 56px;
    width: 100%;
    padding: 12px;
    margin-inline-end: 0;
    cursor: pointer;

    > span:not(.ant-radio):not(.pisell-lowcode-radio) {
      display: flex;
      align-items: center;
      gap: 12px;
      width: 100%;
    }

    &.ant-radio-wrapper-checked,
    &.pisell-lowcode-radio-wrapper-checked {
      border-color: var(--primary-600, #7f56d9);
      background: var(--Primary-50, #f9f5ff);
    }
  }
}

.saas-app-peripheral-add-device {
  &__body {
    display: flex;
    flex-direction: column;
    gap: 16px;
    min-height: 0;
  }

  &__model-list {
    min-height: 0;
    overflow: auto;
  }

  &__filters {
    display: grid;
    grid-template-columns: 1fr 180px 180px;
    gap: 12px;
  }

  &__search {
    min-width: 0;
  }

  &__filter-select {
    width: 180px;
    flex: 0 0 180px;
  }

  &__model-group {
    & + & {
      margin-top: 20px;
    }
  }

  &__model-groups {
    display: flex;
    flex-direction: column;
    gap: 24px;
  }

  &__group-title {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 12px;
    font-size: 16px;
    font-weight: 600;
  }

  &__model-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 12px;
  }

  &__model-card {
    display: flex;
    flex-direction: column;
    gap: 6px;
    padding: 12px;
    border: 1px solid #f0f0f0;
    border-radius: 8px;
    background: #fff;
    cursor: pointer;
  }

  &__model-name {
    font-weight: 600;
    color: rgba(0, 0, 0, 0.88);
  }

  &__model-brand,
  &__model-desc {
    font-size: 12px;
    color: rgba(0, 0, 0, 0.45);
  }

  &__connection-list {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin-top: 8px;
  }

  &__connection-tag {
    display: inline-flex;
    align-items: center;
    height: 18px;
    padding: 0 6px;
    font-size: 11px;
    line-height: 16px;
    border: 1px solid #d9d9d9;
    border-radius: 4px;
    background: #fff;
  }
}

.saas-app-peripheral-add-device__body {
  max-height: calc(80dvh - 220px);
  height: 100%;
  min-height: 0;
}

.saas-app-peripheral-connect__modal--phone {
  width: 100%;
  max-width: 100%;

  .pisell-lowcode-modal-content {
    max-height: 92dvh;
  }

  .ant-modal-body,
  .pisell-lowcode-modal-body {
    max-height: calc(92dvh - 172px);
    overflow: auto;
  }

  .ant-modal-footer,
  .pisell-lowcode-modal-footer {
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }

  .saas-app-peripheral-connect__modal-title {
    display: flex;
    max-width: calc(100vw - 88px);
    padding-right: 8px;
    white-space: normal;

    span {
      min-width: 0;
      line-height: 24px;
      word-break: break-word;
    }
  }

  .saas-app-peripheral-connect__picker {
    display: flex;
    height: calc(92dvh - 220px);
    min-height: 0;
    flex-direction: column;
    gap: 16px;
    overflow: hidden;
  }

  .saas-app-peripheral-connect__picker.is-new {
    .saas-app-peripheral-connect__targets {
      overflow: hidden;
    }
  }

  .saas-app-peripheral-connect__areas,
  .saas-app-peripheral-connect__targets,
  .saas-app-peripheral-connect__config {
    min-height: 0;
  }

  .saas-app-peripheral-connect__targets {
    display: flex;
    flex: 1;
    flex-direction: column;
  }

  .saas-app-peripheral-connect__area-list {
    flex: 0 0 auto;
    max-height: 144px;
  }

  .saas-app-peripheral-connect__workarea-trigger {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    min-height: 44px;
    padding: 10px 12px;
    border: 1px solid #e5e6eb;
    border-radius: 10px;
    background: #f7f8fa;
    color: inherit;
    text-align: left;
  }

  .saas-app-peripheral-connect__device-list {
    flex: 1;
    min-height: 0;
  }

  .saas-app-peripheral-connect__picker.is-existing {
    .saas-app-peripheral-connect__area-list,
    .saas-app-peripheral-connect__device-list {
      max-height: none;
    }
  }

  .saas-app-peripheral-connect__filters,
  .saas-app-peripheral-connect__option-grid,
  .saas-app-peripheral-connect__summary,
  .saas-app-peripheral-connect__model-grid,
  .saas-app-peripheral-connect__device-grid {
    grid-template-columns: 1fr;
  }

  .saas-app-peripheral-connect__summary {
    > .anticon {
      transform: rotate(90deg);
    }
  }

  .saas-app-peripheral-connect__model-card {
    min-height: auto;
  }

  .saas-app-peripheral-add-device__body {
    max-height: calc(90dvh - 220px);
    height: 100%;
    min-height: 0;
  }

  .saas-app-peripheral-add-device__filters {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .saas-app-peripheral-add-device__search {
    grid-column: 1 / -1;
  }

  .saas-app-peripheral-add-device__filter-select {
    width: 100%;
    min-width: 0;
    flex: 1;
  }

  .saas-app-peripheral-add-device__model-list {
    flex: 1;
    min-height: 0;
  }

  .saas-app-peripheral-add-device__model-grid {
    grid-template-columns: 1fr;
  }
}

.saas-app-peripheral-workarea-picker-modal {
  width: 100%;
  max-width: 100%;

  .pisell-lowcode-modal-body,
  .ant-modal-body {
    max-height: 60dvh;
    overflow-y: auto;
  }

  .saas-app-peripheral-connect__area-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }
}
