@use "sass:color";

// ============================================================================
// VARIABLES & MIXINS
// ============================================================================

$primary-color: var(--action-primary-default);
$secondary-color: #834DD0;
$danger-color: #E94F37;
$success-color: #7AC74F;
$border-color: #D9C8F0;
$bg-light: #F3EDFA;
$bg-lighter: #F9F6FC;
$text-primary: #333333;
$text-secondary: #6A1B9A;
$shadow-sm: 0 2px 4px rgba(0, 0, 0, 0.1);
$shadow-md: 0 4px 6px rgba(0, 0, 0, 0.2);
$border-radius: 8px;

@mixin flex-center {
  display: flex;
  align-items: center;
  justify-content: center;
}

@mixin card-shadow {
  box-shadow: $shadow-sm;
  border-radius: $border-radius;
}

// ============================================================================
// MAIN LAYOUT
// ============================================================================

.drag-drop-container {
  display: flex;
  gap: 20px;
  width: 100%;
  height: 95%;
  position: relative;
}

// ============================================================================
// LEFT SIDEBAR - FIELD PALETTE
// ============================================================================

.field-palette {
  width: 255px;
  min-height: 500px;
  background-color: $primary-color;
  padding: 25px 12px;
  @include flex-center;
  align-items: flex-start;
  overflow-y: auto;
}

.field-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  width: 100%;
}

.field-item {
  @include flex-center;
  flex-direction: column;
  background-color: #ffffff;
  width: 96px;
  height: 80px;
  border-radius: 6px;
  box-shadow: $shadow-sm;
  cursor: grab;
  transition: transform 0.2s, box-shadow 0.2s;

  &:hover {
    transform: translateY(-2px);
    box-shadow: $shadow-md;
  }

  &:active {
    cursor: grabbing;
  }
}

.field-icon {
  width: 22px;
  height: 25px;
  color: $primary-color;
  margin-bottom: 4px;
}

.field-label {
  font-size: 14px;
  color: $text-primary;
  font-weight: 500;
  text-align: center;
}

// ============================================================================
// CANVAS AREA
// ============================================================================

.canvas-area {
  flex: 1;
  border: 1px solid #ccc;
  padding: 10px;
  border-radius: 4px;
  background-color: #f9f9f9;
  overflow: auto;
  min-height: 300px;
}

.drop-zone {
  padding: 10px;
  width: 100%;
  background-color: #fafafa;
  min-height: 100%;
}

.form-element {
  margin: 10px 0;
  position: relative;
  transition: background-color 0.2s;

  &:hover {
    background-color: rgba(131, 77, 208, 0.05);
    border-radius: 4px;
  }
}

.field-wrapper {
  display: flex;
  margin-bottom: 16px;
}

.field-row {
  display: flex;
  align-items: center;
  gap: 16px;
}

.empty-state {
  text-align: center;
  color: #999;
  font-size: 16px;
  padding: 40px;
}

// ============================================================================
// GRID DROP AREA
// ============================================================================

.grid-drop-area {
  flex: 1;
  border: 1px solid #ccc;
  padding: 10px;
  border-radius: 4px;
  background-color: #f9f9f9;
  min-height: 300px;
}

.grid-info-panel {
  @include flex-center;
  flex-direction: column;
  padding: 20px;
  background-color: #f9f9f9;
  border: 1px solid #ddd;
  border-radius: $border-radius;
  box-shadow: $shadow-sm;
  max-width: 400px;
  margin: auto;
}

.grid-title {
  font-size: 1.5rem;
  font-weight: 600;
  color: $text-primary;
  margin: 0 0 20px 0;
  text-align: center;
}

.selected-fields {
  width: 100%;
  margin-bottom: 20px;
}

.field-count {
  font-size: 1rem;
  color: #666;
  margin-bottom: 12px;
  text-align: center;
}

.field-list {
  list-style: none;
  padding: 0;
  margin: 0;

  &-item {
    padding: 8px 12px;
    margin-bottom: 8px;
    background-color: #fff;
    border: 1px solid #ddd;
    border-radius: 4px;
    cursor: pointer;
    transition: background-color 0.2s, transform 0.2s;

    &:hover {
      background-color: $bg-light;
      transform: translateX(4px);
    }
  }
}

.btn-create-grid {
  height: 40px;
  padding: 0 24px;
  font-size: 14px;
  font-weight: 500;
}

// ============================================================================
// CONTEXT MENU
// ============================================================================

.context-menu {
  position: fixed;
  background: #F4E7FF;
  box-shadow: $shadow-md;
  z-index: 1000;
  padding: 8px;
  border-radius: 8px;
  display: flex;
  gap: 12px;
}

.context-menu-btn {
  @include flex-center;
  gap: 6px;
  padding: 8px 12px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  font-size: 14px;
  transition: opacity 0.2s;

  &:hover {
    opacity: 0.8;
  }

  mat-icon {
    font-size: 20px;
    width: 20px;
    height: 20px;
  }

  &.btn-edit {
    background-color: $secondary-color;
    color: #fff;
  }

  &.btn-delete {
    background-color: #fff;
    color: $danger-color;
  }
}

// ============================================================================
// MODALS
// ============================================================================

.modal-overlay {
  display: none;
  position: fixed;
  z-index: 1000;
  padding-top: 25px;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
  overflow-y: auto;
}

.modal-content {
  background-color: #fefefe;
  margin: auto;
  padding: 24px;
  border: 1px solid #888;
  border-radius: 10px;
  width: 90%;
  max-width: 1300px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.3);
}

.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 2px solid $border-color;
}

.modal-title {
  color: $secondary-color;
  font-family: Helvetica, sans-serif;
  font-size: 18px;
  font-weight: bold;
  margin: 0;
}

.modal-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.btn {
  @include flex-center;
  gap: 6px;
  padding: 8px 16px;
  border: none;
  border-radius: $border-radius;
  font-family: Helvetica, sans-serif;
  font-size: 12px;
  font-weight: 400;
  cursor: pointer;
  transition: all 0.3s ease;
  box-shadow: $shadow-sm;

  mat-icon {
    width: 16px;
    height: 16px;
    font-size: 16px;
  }

  &-primary {
    background-color: $success-color;
    color: #fff;

    &:hover {
      opacity: 0.9;
      transform: translateY(-1px);
    }
  }

  &-secondary {
    background-color: #fff;
    color: $secondary-color;

    &:hover {
      background-color: $bg-light;
    }
  }

  &-close {
    @include flex-center;
    background: none;
    border: none;
    cursor: pointer;
    padding: 4px;
    color: $text-primary;
    transition: color 0.2s;

    &:hover {
      color: $danger-color;
    }

    mat-icon {
      font-size: 24px;
      width: 24px;
      height: 24px;
    }
  }
}

.modal-body {
  max-height: calc(100vh - 200px);
  overflow-y: auto;
}

// ============================================================================
// CONFIGURATION FORM
// ============================================================================

.config-form {
  width: 100%;
}

.form-layout {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 32px;

  @media (max-width: 1200px) {
    grid-template-columns: 1fr;
  }
}

.form-section {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.form-group {
  // background-color: $bg-light;
  padding: 12px;
  border-radius: 4px;
  border-bottom: 2px solid $text-secondary;
  transition: border-color 0.3s;

  &:focus-within {
    border-bottom-color: color.adjust($text-secondary, $lightness: -10%);
  }
}

.form-label {
  display: block;
  font-size: 12px;
  font-weight: 400;
  color: $text-secondary;
  margin-bottom: 4px;
}

.form-input {
  width: 100%;
  border: none;
  outline: none;
  background-color: transparent;
  font-size: 16px;
  font-weight: 500;
  color: $text-primary;
  padding: 4px 0;

  &::placeholder {
    color: #999;
  }
}

.form-group-checkbox {
  display: flex;
  align-items: center;
  gap: 8px;

  .form-label {
    margin: 0;
  }
}

.form-control {
  height: 36px;
  padding-top: 1.5px;
}

.file-input {
  border: none;
  border-bottom: 2px solid #ccc;
  outline: none;
  padding: 8px 0;
  font-size: 14px;
  color: $text-primary;
  transition: border-color 0.3s ease;

  &:focus {
    border-bottom-color: $text-secondary;
  }
}

.file-info {
  margin-top: 8px;
  font-size: 12px;
  color: #666;
}

// ============================================================================
// OPTIONS TABLE
// ============================================================================

.options-table {
  width: 100%;
  border-collapse: collapse;
  border-radius: 6px;
  overflow: hidden;
  box-shadow: $shadow-sm;

  thead {
    background-color: $text-secondary;
    color: white;

    th {
      padding: 12px;
      font-size: 14px;
      text-align: left;
      font-weight: bold;
    }
  }

  tbody {
    background-color: $bg-lighter;

    tr {
      border-bottom: 1px solid #e6e6e6;

      &:last-child {
        border-bottom: none;
      }
    }

    td {
      padding: 10px;
      font-size: 14px;
      text-align: left;
    }
  }
}

.option-input {
  width: 100%;
  padding: 6px;
  border: none;
  outline: none;
  background: transparent;
  font-size: 14px;
  color: $text-primary;
  border-bottom: 1px solid #ccc;
  transition: border-color 0.3s;

  &:focus {
    border-bottom-color: $text-secondary;
  }
}

.btn-add-row {
  color: white;
  font-size: 24px;
  cursor: pointer;
  transition: transform 0.2s;

  &:hover {
    transform: scale(1.1);
  }
}

.btn-delete-row {
  color: $danger-color;
  font-size: 20px;
  cursor: pointer;
  transition: transform 0.2s;

  &:hover {
    transform: scale(1.1);
  }
}

// ============================================================================
// PREVIEW SECTION
// ============================================================================

.preview-section {
  display: flex;
  flex-direction: column;
}

.preview-title {
  color: $secondary-color;
  font-family: Helvetica, sans-serif;
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 12px 0;
}

.preview-container {
  border: 2px solid $border-color;
  border-radius: $border-radius;
  padding: 20px;
  background-color: #fff;
  min-height: 160px;
  @include flex-center;
}

// ============================================================================
// ANGULAR MATERIAL OVERRIDES
// ============================================================================

::ng-deep {
  // Checkbox customization
  .mdc-checkbox__native-control:enabled:checked ~ .mdc-checkbox__background,
  .mdc-checkbox__native-control:enabled:indeterminate ~ .mdc-checkbox__background {
    border-color: $primary-color !important;
    background-color: $primary-color !important;
  }

  .mdc-checkbox:hover .mdc-checkbox__native-control:checked ~ .mdc-checkbox__ripple,
  .mdc-checkbox .mdc-checkbox__native-control:focus:checked ~ .mdc-checkbox__ripple,
  .mdc-checkbox:active .mdc-checkbox__native-control:checked ~ .mdc-checkbox__ripple {
    background-color: $primary-color !important;
  }

  .mdc-checkbox__native-control:disabled:checked ~ .mdc-checkbox__background,
  .mdc-checkbox__native-control:disabled:indeterminate ~ .mdc-checkbox__background {
    border-color: $primary-color !important;
    background-color: $primary-color !important;
  }

  .mdc-checkbox__background {
    border-color: var(--mat-sys-inverse-surface) !important;
  }
}

// ============================================================================
// CDK DRAG & DROP STYLES
// ============================================================================

.cdk-drag-preview {
  background-color: #e3f2fd;
  border-radius: 4px;
  box-shadow: $shadow-md;
  opacity: 0.8;
}

.cdk-drag-placeholder {
  opacity: 0.4;
}

.cdk-drag-animating {
  transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}

.cdk-drop-list-dragging .cdk-drag {
  transition: transform 250ms cubic-bezier(0, 0, 0.2, 1);
}

// ============================================================================
// UTILITIES
// ============================================================================

.input-label {
  font-weight: bold;
  margin-right: 12px;
  color: $text-primary;
}

// ============================================================================
// RESPONSIVE DESIGN
// ============================================================================

@media (max-width: 768px) {
  .drag-drop-container {
    flex-direction: column;
  }

  .field-palette {
    width: 100%;
    height: auto;
    padding: 15px;
  }

  .modal-content {
    width: 95%;
    padding: 16px;
  }

  .form-layout {
    grid-template-columns: 1fr;
  }
}

.add-field-btn:disabled {
  opacity: 0.5;              /* 👈 reduce opacity */
  cursor: not-allowed;       /* 👈 show disabled cursor */
  pointer-events: none;      /* 👈 prevent clicks */
}

// ============================================================================
// CONDITIONS PANEL (Phase 2c)
// ============================================================================

.conditions-section {
  border-top: 1px solid $border-color;
  margin-top: 12px;
  padding-top: 12px;

  .conditions-title {
    margin: 0 0 8px;
    color: $text-primary;
    font-size: 14px;
  }
}

.conditions-list {
  list-style: none;
  margin: 0 0 8px;
  padding: 0;
}

.condition-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 8px;
  border: 1px solid $border-color;
  border-radius: $border-radius;
  background: $bg-lighter;
  margin-bottom: 4px;
  font-size: 13px;

  .btn-delete-row {
    cursor: pointer;
    color: $danger-color;
    font-size: 16px;
  }
}

.conditions-empty {
  color: $text-secondary;
  font-size: 13px;
  margin: 0 0 8px;
}

.condition-builder {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;

  .form-input {
    flex: 1 1 auto;
    min-width: 120px;
  }
}

// ============================================================================
// PAGE TABS & VALIDATION (Phase 2b)
// ============================================================================

.page-tabs {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-bottom: 1px solid $border-color;
  background: $bg-lighter;
  flex-wrap: wrap;
}

.page-tab {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px 8px;
  border-radius: $border-radius;
  border: 1px solid $border-color;
  background: #fff;
  cursor: pointer;

  &.active {
    border-color: $primary-color;
    background: $bg-light;
  }

  .page-tab-name {
    border: none;
    background: transparent;
    font: inherit;
    width: 90px;
    color: $text-primary;
  }

  .page-tab-close {
    font-size: 16px;
    width: 16px;
    height: 16px;
    cursor: pointer;
    color: $text-secondary;
  }
}

.page-tab-add {
  @include flex-center;
  gap: 4px;
  border: 1px dashed $border-color;
  border-radius: $border-radius;
  background: transparent;
  padding: 4px 10px;
  cursor: pointer;
  color: $text-secondary;
}

.validation-badge {
  margin-left: auto;
  @include flex-center;
  gap: 4px;
  border: 1px solid $success-color;
  color: $success-color;
  border-radius: $border-radius;
  padding: 4px 10px;
  background: #fff;
  cursor: pointer;

  &.has-warnings {
    border-color: #E0A800;
    color: #E0A800;
  }

  &.has-errors {
    border-color: $danger-color;
    color: $danger-color;
  }
}

.validation-panel {
  margin: 0 12px 8px;
  padding: 10px 14px;
  border: 1px solid $border-color;
  border-radius: $border-radius;
  background: #fff;
  max-height: 200px;
  overflow-y: auto;

  h4 {
    margin: 0 0 6px;
    color: $text-primary;
  }

  .validation-ok {
    color: $success-color;
    margin: 0;
  }

  ul {
    margin: 0;
    padding-left: 18px;
  }

  li.error {
    color: $danger-color;
  }

  li.warning {
    color: #E0A800;
  }
}

.settings-badge {
  border-color: $border-color;
  color: $text-primary;
}

.form-settings-panel {
  max-height: 320px;

  h4:not(:first-child) {
    margin-top: 12px;
  }

  .form-settings-row {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 6px;

    label {
      width: 110px;
      flex-shrink: 0;
      color: $text-primary;
    }

    input[type="text"] {
      flex: 1;
      padding: 4px 8px;
      border: 1px solid $border-color;
      border-radius: $border-radius;
    }
  }

  .form-settings-checkboxes {
    display: flex;
    flex-wrap: wrap;
    gap: 12px;

    label {
      display: flex;
      align-items: center;
      gap: 4px;
      color: $text-primary;
    }
  }
}