/**
 * RestrictionRuleBuilder Component Styles
 *
 * Styles matching the actual JSX class names used in the component
 *
 * @package ArraySubs
 */

// Variables
$rule-border-color: #c3c4c7;
$rule-bg-color: #ffffff;
$rule-hover-bg: #f6f7f7;
$rule-disabled-opacity: 0.6;
$condition-bg: #f6f7f7;
$condition-group-bg: #e8e8e8;
$action-bg: #ecfdf5;
$target-bg: #eff6ff;
$primary-color: #2271b1;
$danger-color: #d63638;
$success-color: #00a32a;
$warning-color: #dba617;
$text-color: #1e1e1e;
$text-muted: #646970;

.arraysubs-rule-builder,
.arraysubs-condition-builder,
.arraysubs-condition-row,
.arraysubs-ajax-select,
.arraysubs-schedule-fields {
  svg {
    display: block;
    flex: 0 0 auto;
  }
}

// ============================================
// Main Rule Builder
// ============================================
.arraysubs-rule-builder {
  display: flex;
  flex-direction: column;
  gap: 16px;

  // Rules Container
  &__rules {
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  // Individual Rule
  &__rule {
    background: $rule-bg-color;
    border: 1px solid $rule-border-color;
    border-radius: 4px;
    overflow: hidden;
    transition:
      border-color 0.15s,
      box-shadow 0.15s;

    &--disabled {
      opacity: $rule-disabled-opacity;

      .arraysubs-rule-builder__rule-body {
        pointer-events: none;
      }
    }

    &--collapsed {
      .arraysubs-rule-builder__rule-header {
        border-bottom: none;
      }
    }

    &--drag-over {
      border-color: $primary-color;
      box-shadow: 0 0 0 2px rgba($primary-color, 0.2);
    }
  }

  // Rule Header
  &__rule-header {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 12px 16px;
    background: $rule-hover-bg;
    border-bottom: 1px solid $rule-border-color;
    cursor: default;
  }

  // Clickable area between rule-info and actions for collapsing
  &__rule-collapse-area {
    flex: 1;
    height: 100%;
    min-height: 32px;
    cursor: pointer;
  }

  // Collapse button
  &__collapse-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    padding: 0;
    border: none;
    border-radius: 4px;
    background: transparent;
    color: $text-muted;
    cursor: pointer;
    flex-shrink: 0;
    transition: all 0.15s;

    &:hover {
      background: rgba(0, 0, 0, 0.05);
      color: $text-color;
    }
  }

  &__collapse-icon {
    transition: transform 0.2s ease;

    &--expanded {
      transform: rotate(90deg);
    }
  }

  &__rule-drag {
    color: $text-muted;
    cursor: grab;
    flex-shrink: 0;

    &:active {
      cursor: grabbing;
    }
  }

  // Toggle Switch
  &__rule-toggle {
    position: relative;
    display: inline-flex;
    align-items: center;
    flex-shrink: 0;

    input[type="checkbox"] {
      width: 36px;
      height: 20px;
      margin: 0;
      cursor: pointer;
      opacity: 0;
      position: absolute;
    }
  }

  &__toggle-slider {
    display: block;
    width: 36px;
    height: 20px;
    background-color: #8c8f94;
    border-radius: 20px;
    cursor: pointer;
    transition: 0.2s;
    pointer-events: none;
    position: relative;

    &::before {
      content: "";
      position: absolute;
      height: 14px;
      width: 14px;
      left: 3px;
      bottom: 3px;
      background-color: white;
      border-radius: 50%;
      transition: 0.2s;
    }
  }

  &__rule-toggle input:checked + &__toggle-slider {
    background-color: $success-color;

    &::before {
      transform: translateX(16px);
    }
  }

  // Rule Info (Name + Number)
  &__rule-info {
    display: flex;
    align-items: center;
    gap: 10px;
    flex-shrink: 0;
    min-width: 0;
  }

  &__rule-name {
    flex: 0 0 auto;
    width: 200px;
    padding: 6px 10px;
    border: 1px solid transparent;
    border-radius: 4px;
    background: transparent;
    font-size: 14px;
    font-weight: 500;
    color: $text-color;

    &:hover,
    &:focus {
      border-color: $rule-border-color;
      background: $rule-bg-color;
      outline: none;
    }

    &:focus {
      border-color: $primary-color;
      box-shadow: 0 0 0 1px $primary-color;
    }
  }

  &__rule-number {
    font-size: 12px;
    color: $text-muted;
    white-space: nowrap;
  }

  // Rule Actions
  &__rule-actions {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
  }

  &__action-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    padding: 0;
    background: none;
    border: 1px solid #ddd;
    border-radius: 4px;
    color: #2271b1;
    cursor: pointer;
    transition:
      background 0.15s ease,
      border-color 0.15s ease,
      color 0.15s ease;

    svg {
      flex-shrink: 0;
    }

    &:hover {
      background: #f0f6fc;
      border-color: #2271b1;
    }

    &:disabled {
      opacity: 0.5;
      cursor: not-allowed;
      pointer-events: none;
    }

    &--delete {
      color: $danger-color;

      &:hover {
        background: #fee;
        border-color: $danger-color;
        color: $danger-color;
      }
    }
  }

  // Rule Body
  &__rule-body {
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 16px;
  }

  // Sections (Target, Conditions, Actions)
  &__section {
    border-radius: 4px;
    border: 1px solid $rule-border-color;
  }

  &__section-header {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    background: $condition-bg;
    border-bottom: 1px solid $rule-border-color;
    border-top-right-radius: 4px;
    border-top-left-radius: 4px;
  }

  &__section-label {
    font-weight: 600;
  }

  &__section-hint {
    font-weight: 400;
    color: $text-muted;
    text-transform: none;
    letter-spacing: normal;
  }

  &__section-content {
    padding: 14px;
    border-bottom-left-radius: 4px;
    border-bottom-right-radius: 4px;
    background: $rule-bg-color;
  }

  // Target Section
  &__section--target {
    .arraysubs-rule-builder__section-header {
      background: $target-bg;
      color: #1e40af;
      border-color: #bfdbfe;
    }

    border-color: #bfdbfe;
  }

  // Conditions Section (IF)
  &__section--conditions {
    .arraysubs-rule-builder__section-header {
      background: $condition-bg;
      color: $text-color;
    }
  }

  // Actions Section (THEN)
  &__section--actions {
    .arraysubs-rule-builder__section-header {
      background: $action-bg;
      color: #047857;
      border-color: #a7f3d0;
    }

    border-color: #a7f3d0;
  }

  // Schedule Section (Content Dripping)
  &__section--schedule {
    .arraysubs-rule-builder__section-header {
      background: #fffbeb;
      color: #92400e;
      border-color: #fde68a;
    }

    border-color: #fde68a;
  }

  // Empty State
  &__empty {
    padding: 40px 20px;
    text-align: center;
    background: $rule-bg-color;
    border: 2px dashed $rule-border-color;
    border-radius: 4px;
    color: $text-muted;

    p {
      margin: 0;
    }
  }

  // Add Rule Button
  &__add {
    .button {
      display: inline-flex;
      align-items: center;
      gap: 6px;
      padding: 8px 16px;
      border: 2px dashed $primary-color;
      border-radius: 4px;
      background: transparent;
      color: $primary-color;
      font-size: 13px;
      font-weight: 500;
      cursor: pointer;
      transition: all 0.2s;

      &:hover {
        background: rgba($primary-color, 0.05);
        border-style: solid;
      }

      svg {
        flex-shrink: 0;
      }
    }
  }
}

// ============================================
// Condition Builder
// ============================================
.arraysubs-condition-builder {
  display: flex;
  flex-direction: column;
  gap: 12px;

  // Empty State
  &__empty {
    padding: 20px;
    text-align: center;
    color: $text-muted;
    font-size: 13px;
    background: $condition-bg;
    border-radius: 4px;

    p {
      margin: 0;
    }
  }

  // Logic Toggle Header
  &__header {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    color: $text-muted;
  }

  &__logic-toggle {
    display: inline-flex;
    border: 1px solid $rule-border-color;
    border-radius: 4px;
    overflow: hidden;

    button {
      padding: 4px 12px;
      border: none;
      background: $rule-bg-color;
      font-size: 12px;
      font-weight: 500;
      color: $text-muted;
      cursor: pointer;
      transition: all 0.15s;

      &:first-child {
        border-right: 1px solid $rule-border-color;
      }

      &:hover {
        background: $rule-hover-bg;
      }

      &.active {
        background: $primary-color;
        color: white;
      }
    }
  }

  // Conditions List
  &__list {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  &__item {
    display: flex;
    flex-direction: column;
    gap: 8px;
  }

  // Separator (AND/OR between conditions)
  &__separator {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    margin-left: 1rem;
    padding: 4px 0;

    span {
      padding: 2px 10px;
      font-size: 10px;
      font-weight: 600;
      text-transform: uppercase;
      letter-spacing: 0.05em;
      color: $text-muted;
      background: $condition-bg;
      border-radius: 4px;
      border: 1px solid $rule-border-color;
    }
  }

  // Nested Group (one level only)
  &__group {
    padding: 12px;
    background: $condition-group-bg;
    border-radius: 4px;
    margin: 4px 0;
    border: 1px solid $rule-border-color;

    .arraysubs-condition-builder {
      gap: 8px;
    }

    .arraysubs-condition-row {
      background: $rule-bg-color;
    }
  }

  &__group-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 10px;
    padding-bottom: 8px;
    border-bottom: 1px solid $rule-border-color;

    span {
      font-size: 11px;
      font-weight: 600;
      color: $text-muted;
      text-transform: uppercase;
      letter-spacing: 0.05em;
    }

    button {
      display: flex;
      align-items: center;
      justify-content: center;
      width: 24px;
      height: 24px;
      padding: 0;
      border: none;
      border-radius: 4px;
      background: transparent;
      color: $text-muted;
      cursor: pointer;

      &:hover {
        background: rgba($danger-color, 0.1);
        color: $danger-color;
      }
    }
  }

  // Actions (Add Condition, Add Group)
  &__actions {
    display: flex;
    gap: 8px;
    margin-top: 4px;

    button {
      display: inline-flex;
      align-items: center;
      gap: 4px;
      padding: 6px 12px;
      border: 1px dashed $rule-border-color;
      border-radius: 4px;
      background: transparent;
      font-size: 12px;
      color: $text-muted;
      cursor: pointer;
      transition: all 0.15s;

      &:hover {
        border-color: $primary-color;
        color: $primary-color;
        background: rgba($primary-color, 0.02);
      }
    }
  }
}

// ============================================
// Condition Row - Inline Layout
// ============================================
.arraysubs-condition-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px;
  background: $condition-bg;
  border-radius: 4px;
  border: 1px solid transparent;
  flex-wrap: wrap;

  &:hover {
    border-color: $rule-border-color;
  }

  // Type Selector (class is applied directly to a <select>).
  // Tag the selector with `select` to outrank WP 7.0's `.wp-core-ui select`
  // rule, which forces 40px min-height on every admin select.
  select#{&}__type {
    flex-shrink: 0;
    min-width: 200px;
    width: auto;
    padding: 0 24px 0 10px;
    min-height: 32px;
    line-height: 1.5;
    border: 1px solid $rule-border-color;
    border-radius: 4px;
    background: $rule-bg-color;
    font-size: 13px;
    cursor: pointer;

    &:focus {
      outline: none;
      border-color: $primary-color;
      box-shadow: 0 0 0 1px $primary-color;
    }
  }

  // Fields Container - inline row
  &__fields {
    display: flex;
    flex-wrap: wrap;
    gap: 8px;
    flex: 1;
    align-items: center;
  }

  // Field Input Styles - common for all inline inputs.
  // min-height pinned to 32px so the row aligns with the 32px delete
  // button — WP 7.0 raised the native input/select min-height to 40px.
  // Tagging selectors with `select`/`input` is needed to outrank WP's
  // `.wp-core-ui select` / `input[type="text"]` rules.
  select#{&}__select,
  input#{&}__input {
    padding: 0 10px;
    min-height: 32px;
    line-height: 1.5;
    border: 1px solid $rule-border-color;
    border-radius: 4px;
    background: $rule-bg-color;
    font-size: 13px;

    &:focus {
      outline: none;
      border-color: $primary-color;
      box-shadow: 0 0 0 1px $primary-color;
    }
  }

  select#{&}__select {
    cursor: pointer;
    min-width: 100px;
    padding-right: 24px;
  }

  &__input {
    width: 100px;

    &--wide {
      width: 100%;
      min-width: 150px;
    }
  }

  // Number Input with Prefix/Suffix
  &__number-wrapper {
    display: flex;
    align-items: center;
    gap: 6px;
  }

  &__prefix,
  &__suffix {
    font-size: 13px;
    color: $text-muted;
    font-weight: 500;
  }

  // Delete Button
  &__delete {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    padding: 0;
    border: none;
    border-radius: 4px;
    background: transparent;
    color: $text-muted;
    cursor: pointer;
    margin-top: 0;
    align-self: center;
    transition: all 0.15s;

    &:hover {
      background: rgba($danger-color, 0.1);
      color: $danger-color;
    }
  }
}

// ============================================
// Ajax Select
// ============================================
.arraysubs-ajax-select {
  position: relative;
  width: 100%;
  min-width: 210px;

  &--open {
    .arraysubs-ajax-select__dropdown {
      display: block;
    }

    .arraysubs-ajax-select__chevron {
      transform: rotate(180deg);
    }
  }

  // Trigger
  &__trigger {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 4px 10px;
    min-height: 32px;
    box-sizing: border-box;
    border: 1px solid $rule-border-color;
    border-radius: 4px;
    background: $rule-bg-color;
    cursor: pointer;
    gap: 8px;

    &:hover {
      border-color: darken($rule-border-color, 10%);
    }
  }

  &__value {
    flex: 1;
    overflow: hidden;
    min-height: 22px;
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 4px;
  }

  &__placeholder {
    color: $text-muted;
    font-size: 13px;
  }

  &__single-value {
    font-size: 13px;
    color: $text-color;
  }

  // Tags (Multiple Selection)
  &__tags {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
  }

  &__tag {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 2px 8px;
    background: $primary-color;
    color: white;
    border-radius: 3px;
    font-size: 12px;
    max-width: 250px;

    span {
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
  }

  &__tag-remove {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    border: none;
    background: transparent;
    color: rgba(white, 0.8);
    cursor: pointer;
    flex-shrink: 0;

    &:hover {
      color: white;
    }
  }

  // Indicators
  &__indicators {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
  }

  &__clear {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 2px;
    border: none;
    background: transparent;
    color: $text-muted;
    cursor: pointer;

    &:hover {
      color: $text-color;
    }
  }

  &__chevron {
    color: $text-muted;
    transition: transform 0.2s;
  }

  // Dropdown
  &__dropdown {
    display: none;
    position: absolute;
    top: 100%;
    left: 0;
    right: 0;
    z-index: 100;
    margin-top: 4px;
    background: $rule-bg-color;
    border: 1px solid $rule-border-color;
    border-radius: 4px;
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
  }

  // Search
  &__search {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    border-bottom: 1px solid $rule-border-color;
  }

  &__search-icon {
    color: $text-muted;
    flex-shrink: 0;
  }

  &__search-input {
    flex: 1;
    border: none;
    background: transparent;
    font-size: 13px;
    outline: none;

    &::placeholder {
      color: $text-muted;
    }
  }

  // Options
  &__options {
    max-height: 200px;
    overflow-y: auto;
  }

  &__option {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 12px;
    font-size: 13px;
    cursor: pointer;

    &:hover {
      background: $rule-hover-bg;
    }

    &--selected {
      background: rgba($primary-color, 0.08);
      color: $primary-color;
    }
  }

  &__checkbox {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 16px;
    height: 16px;
    border: 1px solid $rule-border-color;
    border-radius: 3px;
    font-size: 10px;
    flex-shrink: 0;
  }

  &__option--selected &__checkbox {
    background: $primary-color;
    border-color: $primary-color;
    color: white;
  }

  // States
  &__loading,
  &__empty,
  &__error {
    padding: 20px;
    text-align: center;
    font-size: 13px;
    color: $text-muted;
  }

  &__loading {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
  }

  &__spinner {
    animation: arraysubs-spin 1s linear infinite;
  }

  &__error {
    color: $danger-color;
  }
}

// Animation
@keyframes arraysubs-spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}

// ============================================
// Schedule Fields (Content Dripping)
// ============================================
.arraysubs-schedule-fields {
  &__toggle {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 13px;
    font-weight: 500;
    color: $text-color;
    cursor: pointer;

    input[type="checkbox"] {
      margin: 0;
      cursor: pointer;
    }
  }

  &__config {
    margin-top: 12px;
    padding: 12px;
    background: #fffbeb;
    border: 1px solid #fde68a;
    border-radius: 4px;
  }

  &__row {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
  }

  &__label {
    font-size: 13px;
    font-weight: 500;
    color: $text-color;
  }

  &__value {
    width: 70px;
    padding: 6px 8px;
    border: 1px solid $rule-border-color;
    border-radius: 4px;
    background: $rule-bg-color;
    font-size: 13px;
    text-align: center;

    &:focus {
      outline: none;
      border-color: $primary-color;
      box-shadow: 0 0 0 1px $primary-color;
    }
  }

  &__unit {
    padding: 6px 8px;
    border: 1px solid $rule-border-color;
    border-radius: 4px;
    background: $rule-bg-color;
    font-size: 13px;
    cursor: pointer;

    &:focus {
      outline: none;
      border-color: $primary-color;
      box-shadow: 0 0 0 1px $primary-color;
    }
  }

  &__suffix {
    font-size: 13px;
    color: $text-muted;
  }

  &__help {
    margin: 8px 0 0;
    font-size: 12px;
    color: $text-muted;
    line-height: 1.5;
  }
}

// ============================================
// Responsive
// ============================================
@media (max-width: 782px) {
  .arraysubs-rule-builder {
    &__rule-header {
      flex-wrap: wrap;
      gap: 8px;
    }

    &__rule-info {
      flex-direction: column;
      align-items: flex-start;
      gap: 4px;
    }

    &__rule-name {
      width: 100%;
    }

    &__rule-actions {
      width: 100%;
      justify-content: flex-end;
    }
  }

  .arraysubs-condition-row {
    flex-direction: column;
    gap: 12px;

    &__type {
      width: 100%;
      min-width: auto;
    }

    &__fields {
      width: 100%;
    }

    &__delete {
      align-self: flex-end;
    }
  }

  .arraysubs-ajax-select {
    min-width: auto;
    width: 100%;
  }
}
