@use '../variables' as v;
@use '../mixins' as m;

// ─────────────────────────────────────────────
// Form group
// ─────────────────────────────────────────────
.form-group {

  // margin-bottom: 18px;

  &:last-child { margin-bottom: 0; }
}

.form-row {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;

  @include m.max-sm { grid-template-columns: 1fr; }
}

// ─────────────────────────────────────────────
// Label
// ─────────────────────────────────────────────
.form-label {
  display: block;
  font-size: v.$font-size-sm;
  font-weight: 500;
  color: var(--text-primary) !important;
  margin-bottom: 4px;

  .required {
    color: v.$danger;
    margin-left: 3px;
  }
}

.form-hint {
  font-size: v.$font-size-xs;
  color: var(--text-muted);
  margin-top: 5px;
  line-height: 1.5;
}

.form-error {
  font-size: v.$font-size-xs;
  color: v.$danger;
  margin-top: 5px;
  display: flex;
  align-items: center;
  gap: 4px;

  svg { width: 13px; height: 13px; }
}

// ─────────────────────────────────────────────
// Input
// ─────────────────────────────────────────────
.form-input {
  display: block;
  width: 100%;
  height: 38px;
  padding: 0 12px;
  border: 1px solid var(--border-color);
  border-radius: v.$radius;
  font-size: v.$font-size-sm;
  font-family: v.$font-family;
  color: var(--text-primary);
  background: var(--bg-surface);
  transition: border-color v.$transition-base, box-shadow v.$transition-base;

  &::placeholder { color: var(--text-muted); }

  &:focus {
    outline: none;
    border-color: var(--active-text);
    @include m.focus-ring;
  }

  &.has-error {
    border-color: v.$danger-border;
    background: v.$danger-light;
  }

  &:disabled {
    background: var(--bg-hover);
    color: var(--text-muted);
    cursor: not-allowed;
  }
}

// Textarea
.form-textarea {
  @extend .form-input;
  height: auto;
  padding: 10px 12px;
  resize: vertical;
  min-height: 90px;
  line-height: 1.5;
}

// Select
.form-select {
  @extend .form-input;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 20 20' fill='%239ca3af'%3E%3Cpath fill-rule='evenodd' d='M5.23 7.21a.75.75 0 011.06.02L10 11.17l3.71-3.94a.75.75 0 111.08 1.04l-4.25 4.5a.75.75 0 01-1.08 0l-4.25-4.5a.75.75 0 01.02-1.06z' clip-rule='evenodd'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  background-size: 16px;
  padding-right: 34px;
  cursor: pointer;
}

// Input with icon
.input-with-icon {
  position: relative;

  .input-icon {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-muted);
    pointer-events: none;

    svg { width: 16px; height: 16px; }

    &--left  { left: 11px;  + .form-input { padding-left: 34px; } }
    &--right { right: 11px; }
  }
}

// ─────────────────────────────────────────────
// Checkbox & Radio
// ─────────────────────────────────────────────
.form-check {
  display: flex;
  align-items: flex-start;
  gap: 9px;
  cursor: pointer;
  font-size: v.$font-size-sm;
  color: var(--text-secondary);
  line-height: 1.4;
}

input[type="checkbox"],
.form-checkbox,
.form-check input[type="checkbox"] {
  appearance: none !important;
  -webkit-appearance: none !important;
  -moz-appearance: none !important;
  width: 18px;
  height: 18px;
  border: 1.5px solid var(--border-color, v.$gray-300);
  border-radius: 5px;
  outline: none !important;
  box-shadow: v.$shadow-xs;
  background-color: var(--bg-surface, #ffffff);
  display: inline-grid;
  place-content: center;
  flex-shrink: 0;
  cursor: pointer;
  margin: 0;
  vertical-align: middle;
  transition: border-color v.$transition-base, background-color v.$transition-base, box-shadow v.$transition-base;

  &::before {
    content: "";
    width: 10px;
    height: 10px;
    transform: scale(0);
    transition: transform 0.12s cubic-bezier(0.16, 1, 0.3, 1);
    background-color: #ffffff;
    clip-path: polygon(14% 44%, 0% 58%, 38% 96%, 100% 18%, 84% 4%, 38% 66%);
  }

  &:hover:not(:disabled) {
    border-color: v.$info-border;
  }

  &:checked {
    border-color: v.$primary !important;
    background-color: v.$primary !important;
    box-shadow: 0 1px 3px rgba(66, 133, 244, 0.3);

    &::before {
      transform: scale(1);
    }
  }

  &:focus-visible {
    border-color: v.$primary;
    box-shadow: 0 0 0 3px rgba(66, 133, 244, 0.2);
  }

  &:disabled {
    background-color: var(--bg-hover);
    border-color: var(--border-color);
    cursor: not-allowed;
    opacity: 0.6;
  }
}

input[type="radio"],
.form-check input[type="radio"] {
  appearance: none !important;
  -webkit-appearance: none !important;
  -moz-appearance: none !important;
  width: 16px;
  height: 16px;
  border: 2px solid var(--border-color);
  border-radius: 50%;
  outline: none !important;
  box-shadow: none !important;
  background-color: var(--bg-surface);
  display: inline-grid;
  place-content: center;
  flex-shrink: 0;
  cursor: pointer;
  transition: border-color 0.15s, background-color 0.15s;

  &::before {
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    transform: scale(0);
    transition: transform 0.15s;
    background-color: var(--bg-surface);
  }

  &:checked {
    border-color: v.$primary !important;
    background-color: v.$primary !important;

    &::before {
      transform: scale(1);
    }
  }

  &:disabled {
    background-color: var(--bg-hover-strong);
    border-color: var(--border-color);
    cursor: not-allowed;
  }
}

// Toggle switch
.form-toggle {
  position: relative;
  display: inline-block;
  width: 38px;
  height: 22px;

  input {
    opacity: 0;
    width: 0;
    height: 0;
    position: absolute;

    &:checked + .toggle-track {
      background: v.$primary;

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

    &:focus + .toggle-track { @include m.focus-ring; }
  }

  .toggle-track {
    position: absolute;
    inset: 0;
    background: var(--bg-hover-strong);
    border-radius: v.$radius-full;
    transition: background v.$transition-base;
    cursor: pointer;

    &::after {
      content: '';
      position: absolute;
      left: 3px;
      top: 3px;
      width: 16px;
      height: 16px;
      background: var(--text-primary);
      border-radius: v.$radius-full;
      transition: transform v.$transition-base;
      box-shadow: v.$shadow-xs;
    }
  }
}

// ─────────────────────────────────────────────
// File upload zone
// ─────────────────────────────────────────────
.upload-zone {
  border: 2px dashed var(--border-color);
  border-radius: v.$radius-lg;
  padding: 40px 24px;
  text-align: center;
  background: var(--bg-page);
  cursor: pointer;
  transition: border-color v.$transition-base, background v.$transition-base;

  &:hover,
  &.is-dragover {
    border-color: var(--active-text);
    background: var(--active-bg);
  }

  &-icon {
    @include m.flex-center;
    width: 52px;
    height: 52px;
    background: var(--bg-surface);
    border: 1px solid var(--border-color);
    border-radius: v.$radius-lg;
    margin: 0 auto 16px;

    svg { width: 24px; height: 24px; color: var(--text-muted); }
  }

  &-title {
    font-size: v.$font-size-base;
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 6px;

    span { color: v.$primary; }
  }

  &-sub {
    font-size: v.$font-size-xs;
    color: var(--text-muted);
  }
}

// ─────────────────────────────────────────────
// Color picker swatch
// ─────────────────────────────────────────────
.color-swatches {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.color-swatch {
  width: 26px;
  height: 26px;
  border-radius: v.$radius-full;
  border: 2px solid transparent;
  cursor: pointer;
  transition: transform v.$transition-base;

  &:hover     { transform: scale(1.15); }
  &.is-active { border-color: v.$gray-800; }
}
