/**
 * GenForm Frontend Styles
 *
 * Public-facing form styling.
 * 
 * @package GenForm
 * @since 1.0.0
 */

@use 'abstracts' as *;

// ========================================
// Form Wrapper & Container
// ========================================

.gfm-wrapper {
  max-width: 100%;
  margin: 0 auto;
}

.gfm-form {
  background: $white;
  padding: 32px;
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  font-family: $font-main;
}

// Utility: hide elements with .gfm-hidden regardless of which stylesheet is loaded.
// (Previously only defined in admin.css — broke on the preview page where admin.css
// isn't enqueued, leaving the empty `.gfm-message` placeholder visible at the bottom.)
.gfm-hidden {
  display: none !important;
}

// Empty message placeholder — stays invisible until populated by JS.
.gfm-form .gfm-message:empty {
  display: none;
}

// ========================================
// Fields Container & Multi-Column Layout
// ========================================

.gfm-fields {
  display: flex;
  flex-wrap: wrap;
  gap: 20px;
}

.gfm-form-field {
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}

// Width utility classes for multi-column layouts.
.gfm-w-100 {
  width: 100%;
}

.gfm-w-75 {
  width: calc(75% - 10px);
}

.gfm-w-67 {
  width: calc(66.666% - 10px);
}

.gfm-w-50 {
  width: calc(50% - 10px);
}

.gfm-w-33 {
  width: calc(33.333% - 14px);
}

.gfm-w-25 {
  width: calc(25% - 15px);
}

// ========================================
// Form Labels
// ========================================

// Reset any admin label rules that might leak through when the form is
// rendered inside the admin preview. Frontend labels stay normal-case.
.gfm-form .gfm-label,
.gfm-label {
  display: block !important;
  margin-bottom: 8px !important;
  font-weight: 600 !important;
  font-size: 14px !important;
  color: $text-main !important;
  text-transform: none !important;
  letter-spacing: normal !important;
  line-height: 1.4 !important;
}

.gfm-required-mark {
  color: $danger;
  margin-left: 2px;
}

// ========================================
// Text Inputs & Textareas
// ========================================

// Wrapper around each input — must be full-width block so the input fills the field.
.gfm-form .gfm-input-control,
.gfm-input-control {
  display: block;
  width: 100%;
  box-sizing: border-box;
}

// Higher specificity wins over .genform-admin-wrap input[type="text"]
// when the form is rendered inside the admin preview.
.gfm-form .gfm-input,
.gfm-form .gfm-textarea,
.gfm-form .gfm-select,
.gfm-input,
.gfm-textarea,
.gfm-select {
  @include gfm-form-input-base;
  display: block;
  max-width: 100%;
}

.gfm-form .gfm-textarea,
.gfm-textarea {
  resize: vertical;
  min-height: 100px;
  height: auto;
  padding: 12px 16px;
  line-height: 1.5;
}

.gfm-form .gfm-select,
.gfm-select {
  appearance: none;
  -webkit-appearance: none;
  -moz-appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8' fill='none'><path d='M1 1.5L6 6.5L11 1.5' stroke='%236B7280' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/></svg>");
  background-repeat: no-repeat;
  background-position: right 16px center;
  padding-right: 40px;
  cursor: pointer;
  // Selects need an explicit width — without this, some browsers size them
  // to the longest option's text width even with width:100% on the mixin.
  width: 100%;
  min-width: 0;
}

// ========================================
// Checkboxes & Radio Buttons (Premium Design)
// ========================================

.gfm-options-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 4px;
}

.gfm-choice-label {
  display: flex;
  align-items: center;
  gap: 12px;
  cursor: pointer;
  user-select: none;
  transition: $transition-base;

  &:hover .gfm-input-choice {
    border-color: $primary;
    box-shadow: 0 0 0 4px rgba($primary, 0.08);
  }

  .gfm-choice-text {
    font-size: 14.5px;
    color: $text-main;
    font-weight: 500;
  }
}

.gfm-input-choice {
  appearance: none;
  -webkit-appearance: none;
  width: 20px;
  height: 20px;
  border: 2px solid $border-dark;
  background: $white;
  margin: 0;
  cursor: pointer;
  position: relative;
  transition: $transition-base;
  flex-shrink: 0;

  &[type="checkbox"] {
    border-radius: 6px;

    &::after {
      content: "";
      position: absolute;
      left: 5px;
      top: 1px;
      width: 6px;
      height: 11px;
      border: solid $white;
      border-width: 0 2.5px 2.5px 0;
      transform: rotate(45deg) scale(0);
      transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    }
  }

  &[type="radio"] {
    border-radius: 50%;

    &::after {
      content: "";
      position: absolute;
      left: 4px;
      top: 4px;
      width: 8px;
      height: 8px;
      border-radius: 50%;
      background: $white;
      transform: scale(0);
      transition: transform 0.2s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    }
  }

  &:checked {
    background: $primary;
    border-color: $primary;
    box-shadow: 0 0 0 4px rgba($primary, 0.1);

    &::after {
      transform: rotate(45deg) scale(1);
    }

    &[type="radio"]::after {
      transform: scale(1);
    }
  }

  &:focus-visible {
    outline: 2px solid $primary-alt;
    outline-offset: 2px;
  }
}

// ========================================
// Field Description / Help Text
// ========================================

.gfm-field-description {
  margin-top: 6px;
  font-size: 13px;
  color: $gfm-color-text-secondary;
  line-height: 1.4;
}

// ========================================
// Submit Button
// ========================================

.gfm-submit-wrap {
  width: 100%;
  margin-top: 24px;

  &.gfm-align-left   { text-align: left; }
  &.gfm-align-center { text-align: center; }
  &.gfm-align-right  { text-align: right; }
}

// Use selector specificity instead of !important to override admin styles
// when the form is rendered inside .genform-admin-wrap (preview page).
.gfm-form .gfm-submit,
.gfm-submit {
  @include gfm-btn-primary;
  position: relative;
}

.gfm-form .gfm-submit.gfm-btn-full,
.gfm-submit.gfm-btn-full {
  width: 100%;
}

// Spinner for loading state.
.gfm-spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2.5px solid rgba(255, 255, 255, 0.3);
  border-top-color: $white;
  border-radius: 50%;
  animation: gfm-spin 0.6s linear infinite;
  vertical-align: middle;
  margin-right: 8px;
}

@keyframes gfm-spin {
  to {
    transform: rotate(360deg);
  }
}

// ========================================
// Form Messages (Success/Error) with animation
// ========================================

.gfm-message {
  margin-top: 20px;
  padding: 16px;
  border-radius: 6px;
  font-size: 14px;
  animation: gfm-fade-in 0.3s ease;

  &.success {
    @include gfm-alert-success;
  }

  &.error {
    @include gfm-alert-error;
  }

  &.warning {
    @include gfm-alert($gfm-color-warning, $gfm-color-warning-text, $gfm-color-warning-border);
  }

  &.info {
    @include gfm-alert($gfm-color-info, $gfm-color-info-text, $gfm-color-info-border);
  }
}

@keyframes gfm-fade-in {
  from {
    opacity: 0;
    transform: translateY(8px);
  }

  to {
    opacity: 1;
    transform: translateY(0);
  }
}

// ========================================
// Loading State
// ========================================

.gfm-loading {
  position: relative;

  &::after {
    content: '';
    @include gfm-absolute-cover;
    background: rgba(255, 255, 255, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 8px;
  }
}

// ========================================
// Form Field Groups
// ========================================

.gfm-field-group {
  margin-bottom: 20px;
  padding-bottom: 20px;
  border-bottom: 1px solid #e5e5e5;

  &:last-child {
    border-bottom: none;
    margin-bottom: 0;
    padding-bottom: 0;
  }

  legend {
    display: block;
    margin-bottom: 16px;
    font-weight: 600;
    font-size: 14px;
    color: $gfm-color-text;
  }
}

.gfm-checkbox-group,
.gfm-radio-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

// ========================================
// Responsive Layout
// ========================================

@include gfm-mobile-only {
  .gfm-form {
    padding: 20px;
  }

  .gfm-submit {
    width: 100%;
  }

  .gfm-fields {
    gap: 12px;
  }

  // Stack all fields to full width on mobile.
  .gfm-form-field {
    width: 100% !important;
  }
}

// ========================================
// Accessibility Improvements
// ========================================

.gfm-form {

  input,
  select,
  textarea {
    &:focus {
      outline: 2px solid $gfm-color-brand;
      outline-offset: 2px;
    }
  }
}

// ========================================
// Error State Styling
// ========================================

.gfm-form-field {
  &.error {

    .gfm-input,
    .gfm-textarea,
    .gfm-select {
      border-color: $danger;
      background-color: rgba(214, 54, 56, 0.02);

      &:focus {
        @include gfm-focus-styles($danger);
      }
    }
  }

  .gfm-error-message {
    margin-top: 5px;
    font-size: 13px;
    color: $danger;
    display: flex;
    align-items: center;
    gap: 4px;
  }
}

// ========================================
// Success State Styling
// ========================================

.gfm-form-field {
  &.success {

    .gfm-input,
    .gfm-textarea,
    .gfm-select {
      border-color: $gfm-color-success-text;
      background-color: rgba(21, 87, 36, 0.02);
    }
  }
}