.arraysubs-rule-workspace {
  display: grid;
  grid-template-columns: minmax(320px, 0.9fr) minmax(0, 1.3fr);
  gap: 24px;
  align-items: stretch;
  color: #1d2327;

  &__sidebar {
    min-width: 0;
  }
  &__guidance {
    .arraysubs-login-limit-global-settings .arraysubs-fb-alert-wrapper {
      grid-template-columns: minmax(0, 1fr);
    }
    .arraysubs-members-access__info-box,
    .arraysubs-member-styling__info-box {
      margin-bottom: 12px;
      padding: 14px 16px;
      border-radius: 8px;
      background: color-mix(in srgb, var(--wp-admin-theme-color) 4%, white);
      border-color: color-mix(
        in srgb,
        var(--wp-admin-theme-color) 18%,
        #dcdcde
      );
      font-size: 12px;

      h4 {
        margin: 0 0 8px;
        font-size: 12px;
      }
      ol,
      ul {
        margin-bottom: 0;
      }
      li {
        margin-bottom: 5px;
      }
    }
  }
  &__list-heading {
    margin: 22px 0 12px;
    h4 {
      display: flex;
      align-items: center;
      gap: 8px;
      margin: 0;
      font-size: 14px;
    }
    h4 span {
      display: inline-flex;
      align-items: center;
      justify-content: center;
      min-width: 24px;
      height: 24px;
      padding: 0 5px;
      box-sizing: border-box;
      border-radius: 6px;
      background: #f0f0f1;
      color: #50575e;
      font-size: 12px;
    }
    p {
      margin: 6px 0 0;
      font-size: 12px;
      color: #646970;
    }
  }
  &__list {
    display: flex;
    flex-direction: column;
    gap: 10px;
    overflow-anchor: none;
  }
  &__add.button {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    margin-top: 14px;
    border-radius: 6px;
  }
  &__no-rules {
    padding: 20px;
    margin: 0;
    border: 1px dashed #c3c4c7;
    border-radius: 8px;
    color: #646970;
  }
}

.arraysubs-rule-list-item {
  border: 1px solid #dcdcde;
  border-radius: 8px;
  background: white;
  overflow: hidden;
  transition:
    border-color 150ms,
    box-shadow 150ms;

  &--selected {
    border-color: var(--wp-admin-theme-color);
    .arraysubs-rule-list-item__footer {
      background: color-mix(in srgb, var(--wp-admin-theme-color) 5%, white);
    }
  }
  &--disabled .arraysubs-rule-list-item__name {
    color: #646970;
  }
  &__heading {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
  }
  &__number {
    font-size: 11px;
    color: #646970;
    flex-shrink: 0;
  }
  input#{&}__name {
    flex: 1;
    min-width: 0;
    width: 100%;
    padding: 3px 6px;
    font-size: 13px;
    font-weight: 600;
    border: 1px solid transparent;
    border-radius: 4px;
    box-shadow: none;
    background: transparent;
    color: #1d2327;
    &:hover {
      border-color: #dcdcde;
    }
    &:focus {
      border-color: var(--wp-admin-theme-color);
      box-shadow: 0 0 0 1px var(--wp-admin-theme-color);
      background: white;
    }
  }
  &__footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 6px 12px;
    border-top: 1px solid #f0f0f1;
    background: #fafafa;
  }
  &__select {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 4px 2px;
    border: 0;
    border-radius: 4px;
    background: transparent;
    color: var(--wp-admin-theme-color);
    font-size: 12px;
    font-weight: 500;
    cursor: pointer;
    &:hover {
      color: var(--wp-admin-theme-color-darker-20);
    }
    &:focus-visible {
      outline: 2px solid var(--wp-admin-theme-color);
      outline-offset: 2px;
    }
  }
  &__actions {
    display: flex;
    gap: 4px;
  }
  .arraysubs-rule-builder__rule-drag {
    width: 20px;
    height: 26px;
  }
  .arraysubs-rule-builder__action-btn {
    width: 27px;
    height: 27px;
    border-radius: 5px;
  }
  .arraysubs-rule-builder__rule-toggle
    input:focus-visible
    + .arraysubs-rule-builder__toggle-slider {
    outline: 2px solid var(--wp-admin-theme-color);
    outline-offset: 3px;
  }
}

// The canvas track. Grid stretch sizes it to the taller of the rule list and
// the canvas (measured height), and size containment keeps the canvas's scroll
// offset from ever growing that row. The track reaches past the workspace by
// the measured dock extension so the canvas stays docked at the end of the
// page, and stays click-through so it never covers the content that follows.
.arraysubs-rule-flow {
  position: relative;
  contain: size;
  margin-bottom: calc(-1 * var(--arraysubs-rule-dock-extend, 0px));
  overflow-anchor: none;
  scroll-margin-top: max(
    140px,
    calc(var(--arraysubs-rule-dock-top, 116px) + 8px)
  );
  min-width: 0;
  box-sizing: border-box;
  pointer-events: none;

  // In-flow offset the canvas scrolls from, re-based when the scroll
  // direction reverses. A spacer rather than a margin: sticky offsets keep the
  // margin box inside the track, which would stop the canvas moving over it.
  &::before {
    content: "";
    display: block;
    height: var(--arraysubs-rule-canvas-offset, 0px);
  }

  // Scroll-aware like the WordPress admin menu (useStickyRuleFlow): it sticks
  // below the header when it fits, otherwise it sticks by whichever edge the
  // scroll direction reveals. The browser pins it inside the inset band.
  &__content {
    position: sticky;
    min-height: calc(100vh - 240px);

    top: var(--arraysubs-rule-sticky-top, 116px);
    bottom: var(--arraysubs-rule-sticky-bottom, auto);
    box-sizing: border-box;
    pointer-events: auto;
    padding: 24px;
    // border: 1px solid #dcdcde;
    border-radius: 8px;
    background-color: #fff;
    background-image: radial-gradient(
      color-mix(in srgb, var(--wp-admin-theme-color) 14%, transparent) 0.8px,
      transparent 0.8px
    );
    background-size: 16px 16px;
  }

  &__header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 16px;
  }
  &__header h3 {
    margin: 4px 0 0;
    font-size: 16px;
    line-height: 1.4;
    overflow-wrap: anywhere;
  }
  &__eyebrow {
    color: #646970;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.6px;
  }
  &__close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    width: 30px;
    height: 30px;
    padding: 0;
    border: 1px solid #dcdcde;
    border-radius: 999px;
    background: white;
    color: #646970;
    cursor: pointer;
  }
  &__close:hover {
    border-color: var(--wp-admin-theme-color);
    color: var(--wp-admin-theme-color);
  }
  &__close:focus-visible,
  &__node:focus-visible {
    outline: 2px solid var(--wp-admin-theme-color);
    outline-offset: 3px;
  }
  &__instruction {
    margin: 8px 0 24px;
    color: #646970;
    font-size: 12px;
  }
  &__steps {
    display: flex;
    flex-direction: column;
    align-items: center;
    max-width: 530px;
    margin: 0 auto;
  }
  &__arrow {
    margin: 6px 0 2px;
    color: color-mix(in srgb, var(--wp-admin-theme-color) 55%, #8c8f94);
    flex-shrink: 0;
  }
  &__step {
    width: 100%;
  }
  &__step-label {
    display: block;
    margin-bottom: 9px;
    color: #646970;
    font-size: 11px;
    font-weight: 600;
    text-align: center;
  }
  &__node {
    display: flex;
    align-items: center;
    gap: 16px;
    width: 100%;
    min-height: 98px;
    padding: 18px;
    box-sizing: border-box;
    border: 1px solid #c3c4c7;
    border-radius: 8px;
    background: white;
    color: #1d2327;
    text-align: left;
    font-family: inherit;
    cursor: pointer;
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.025);
    transition:
      border-color 150ms,
      box-shadow 150ms;
    &:hover:not(:disabled) {
      border-color: var(--wp-admin-theme-color);
      box-shadow: 0 0 0 1px var(--wp-admin-theme-color);
    }
    &:disabled {
      opacity: 0.6;
      cursor: default;
    }
  }
  &__node-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 42px;
    height: 42px;
    flex-shrink: 0;
    border-radius: 8px;
    background: color-mix(in srgb, var(--wp-admin-theme-color) 8%, white);
    color: var(--wp-admin-theme-color);
  }
  &__node-content {
    display: flex;
    flex: 1;
    flex-direction: column;
    gap: 6px;
    min-width: 0;
  }
  &__node-content strong {
    font-size: 14px;
    line-height: 1.4;
    font-weight: 600;
  }
  &__node-content > span {
    font-size: 12px;
    color: #646970;
    line-height: 1.5;
    overflow-wrap: anywhere;
  }
  &__edit-icon {
    color: #8c8f94;
  }
  &__node:hover:not(:disabled) &__edit-icon {
    color: var(--wp-admin-theme-color);
  }
  &__step--schedule {
    margin-top: 24px;
    padding-top: 20px;
    border-top: 1px dashed #c3c4c7;
  }
  &__step--schedule &__step-label {
    text-align: left;
  }
  &__step--schedule &__node {
    min-height: 78px;
    padding: 14px 18px;
  }
  &__step--schedule &__node-icon {
    width: 36px;
    height: 36px;
  }
  &__end {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    margin-top: 22px;
    color: #8c8f94;
    font-size: 11px;
  }
  &__end > span {
    width: 6px;
    height: 6px;
    border: 1px solid #8c8f94;
    border-radius: 50%;
  }
  &__empty {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-height: 560px;
    max-width: 350px;
    margin: 0 auto;
    text-align: center;
  }
  &__empty-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 72px;
    height: 72px;
    border: 1px solid
      color-mix(in srgb, var(--wp-admin-theme-color) 20%, #dcdcde);
    border-radius: 16px;
    color: var(--wp-admin-theme-color);
    background: color-mix(in srgb, var(--wp-admin-theme-color) 6%, white);
  }
  &__empty h3 {
    margin: 20px 0 8px;
    font-size: 19px;
  }
  &__empty p {
    color: #646970;
    font-size: 13px;
    line-height: 1.7;
    margin: 0;
  }
  &__empty-preview {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 26px;
    opacity: 0.45;
  }
  &__empty-preview > span {
    width: 110px;
    height: 24px;
    border: 1px solid
      color-mix(in srgb, var(--wp-admin-theme-color) 30%, #dcdcde);
    border-radius: 5px;
    background: white;
  }
  &__empty-preview .arraysubs-rule-flow__arrow {
    height: 20px;
    margin: 2px;
  }
}

// The shared Modal supplies dismissal, focus management, and scroll locking.
// TinyMCE's menus and link dialog are mounted on body, outside the drawer.
body:has(.arraysubs-rule-drawer) {
  #mce-modal-block {
    z-index: 100001 !important;
  }
  .mce-window,
  .mce-floatpanel,
  .mce-tooltip {
    z-index: 100002 !important;
  }
  .mce-primary {
    background: var(--wp-admin-theme-color);
    border-color: var(--wp-admin-theme-color-darker-10);
  }
  .mce-primary:hover {
    background: var(--wp-admin-theme-color-darker-10);
  }
}
.arraysubs-modal-backdrop:has(.arraysubs-rule-drawer) {
  justify-content: flex-end;
  align-items: stretch;
  padding: 0;
}
.arraysubs-modal.arraysubs-rule-drawer {
  max-width: 680px;
  height: 100vh;
  height: 100dvh;
  max-height: 100dvh;
  border-radius: 12px 0 0 12px;
  transform: translateX(100%);
  .arraysubs-modal--visible & {
    transform: translateX(0);
  }
  .arraysubs-modal__header {
    padding: 20px 24px;
    border-bottom-color: #dcdcde;
  }
  .arraysubs-modal__title {
    font-size: 17px;
  }
  .arraysubs-modal__content {
    padding: 24px;
  }
  .arraysubs-rule-builder__section {
    margin: 0;
    border: 0;
    background: transparent;
  }
  .arraysubs-rule-builder__section-content {
    padding: 0;
    background: transparent;
  }
  .arraysubs-rule-field-row {
    flex-wrap: wrap;
    width: 100%;
    max-width: none;
    > .arraysubs-rule-field {
      flex: 1 1 14rem;
    }
  }
  .arraysubs-rule-field {
    min-width: 0;
    max-width: none;
  }
  .arraysubs-rule-field__input,
  .arraysubs-rule-field__select,
  .arraysubs-rule-field__multi-select,
  .arraysubs-rule-field__textarea,
  .arraysubs-rule-field__input-group,
  .arraysubs-ajax-select {
    width: 100%;
    min-width: 0;
    max-width: none;
    box-sizing: border-box;
  }
  .arraysubs-condition-row {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 32px;
    align-items: start;
    > :not(.arraysubs-condition-row__delete) {
      grid-column: 1 / -1;
      width: 100%;
      min-width: 0;
      max-width: none;
      box-sizing: border-box;
    }
    select.arraysubs-condition-row__type {
      grid-column: 1;
      grid-row: 1;
    }
    .arraysubs-condition-row__delete {
      grid-column: 2;
      grid-row: 1;
    }
    .arraysubs-condition-row__number-input {
      display: flex;
      align-items: center;
      gap: 6px;
      input {
        flex: 1;
        width: 100%;
        min-width: 0;
        max-width: none;
      }
    }
  }
  .arraysubs-schedule-fields__value,
  .arraysubs-schedule-fields__unit {
    flex: 1 1 8rem;
    width: auto;
    min-width: 0;
    max-width: none;
    box-sizing: border-box;
  }
  .arraysubs-schedule-fields__suffix {
    flex-basis: 100%;
  }

  .arraysubs-rule-drawer__context {
    margin-bottom: 24px;
    > span {
      display: block;
      font-size: 12px;
      font-weight: 600;
      color: var(--wp-admin-theme-color);
      overflow-wrap: anywhere;
    }
    p {
      margin: 7px 0 0;
      font-size: 13px;
      color: #646970;
    }
  }
  .arraysubs-rule-drawer__footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    margin-top: 28px;
    padding-top: 20px;
    border-top: 1px solid #dcdcde;
    p {
      margin: 0;
      max-width: 360px;
      font-size: 12px;
      color: #646970;
      line-height: 1.6;
    }
    .button {
      border-radius: 6px;
    }
  }
}

@media (max-width: 1100px) {
  .arraysubs-rule-workspace {
    gap: 16px;
    grid-template-columns: minmax(300px, 1fr) minmax(0, 1.15fr);
  }
  .arraysubs-rule-flow__content {
    padding: 20px 16px;
  }
  .arraysubs-rule-flow__node {
    gap: 12px;
    padding: 16px 12px;
  }
}
@media (max-width: 900px) {
  .arraysubs-rule-workspace {
    grid-template-columns: minmax(0, 1fr);
  }
  // Stacked: the canvas follows the rule list in normal flow.
  .arraysubs-rule-flow {
    contain: none;
    min-height: 0;
    margin-bottom: 0;
    pointer-events: auto;
  }
  .arraysubs-rule-flow::before {
    content: none;
  }
  .arraysubs-rule-flow__content {
    position: static;
    min-height: 420px;
  }
  .arraysubs-rule-flow__empty {
    min-height: 370px;
  }
}
@media (max-width: 480px) {
  .arraysubs-rule-list-item__heading {
    gap: 6px;
    padding: 10px 8px;
  }
  .arraysubs-rule-list-item__footer {
    padding: 6px 8px;
  }
  .arraysubs-rule-flow__content {
    padding: 20px 12px;
  }
  .arraysubs-rule-flow__node-content strong {
    font-size: 13px;
  }
  .arraysubs-rule-flow__node-icon {
    width: 32px;
    height: 32px;
  }
  .arraysubs-rule-flow__edit-icon {
    display: none;
  }
  .arraysubs-modal.arraysubs-rule-drawer {
    border-radius: 0;
  }
  .arraysubs-rule-drawer .arraysubs-modal__header,
  .arraysubs-rule-drawer .arraysubs-modal__content {
    padding: 16px;
  }
}
@media (prefers-reduced-motion: reduce) {
  .arraysubs-rule-list-item,
  .arraysubs-rule-flow__node,
  .arraysubs-modal.arraysubs-rule-drawer {
    transition: none;
  }
  .arraysubs-modal-backdrop:has(.arraysubs-rule-drawer) {
    transition: none;
  }
}
