/**
 * @file
 * Styles for form rendering and public form display.
 */

/* ============================================================================
   ISOLATION WRAPPER
   Prevents parent theme styles from interfering with form rendering

   NOTE: We use SELECTIVE resets instead of `all: initial` because
   `all: initial` breaks CSS custom property (variable) inheritance.
   CSS variables defined on this element would not cascade to children.
   ============================================================================ */
.afb-isolated-form {
  /* Selective reset - preserves CSS variable inheritance */
  margin: 0 !important;
  padding: 0 !important;
  border: none !important;
  background: transparent;
  text-decoration: none;
  vertical-align: baseline;

  /* Re-establish basic layout properties */
  display: block;
  box-sizing: border-box;

  /* Reset text properties */
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: #212529;
  text-align: left;

  /* Ensure proper stacking context */
  position: relative;
  z-index: 1;
}

/* Force box-sizing for all children */
.afb-isolated-form *,
.afb-isolated-form *::before,
.afb-isolated-form *::after {
  box-sizing: border-box;
}

/* Reset common Bootstrap/Drupal theme overrides */
.afb-isolated-form button,
.afb-isolated-form input,
.afb-isolated-form select,
.afb-isolated-form textarea {
  font-family: inherit;
  font-size: inherit;
  line-height: inherit;
  margin: 0;
  padding: 0;
  border: none;
  background: none;
  color: inherit;
}

/* Re-enable form element appearance */
.afb-isolated-form select {
  appearance: auto;
  -webkit-appearance: auto;
  -moz-appearance: auto;
}

/* ============================================================================
   FORM CONTAINER STYLES (Scoped within isolation wrapper)
   ============================================================================ */

/* Standardized wrapper - CSS variables are applied here as inline styles */
/* This class is used by Preview Modal, Paragraph Embed, and Standalone Embed */
.apex-form-wrapper.afb-isolated-form {
  display: block;
  max-width: var(--form-max-width, 800px);
  margin: var(--form-margin, 0 auto);
}

.afb-isolated-form .apex-form-render {
  max-width: var(--form-max-width, 800px);
  margin: 0 auto;
  padding: 20px;
  font-family: var(--form-font-family, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif) !important;
  font-size: var(--form-font-size, 16px) !important;
  line-height: var(--form-line-height, 1.5) !important;
  color: var(--form-text-color, #212529) !important;
}

.afb-isolated-form .form-header {
  margin-bottom: 30px;
  text-align: var(--form-heading-alignment, left);
}

.afb-isolated-form .form-header h2 {
  color: var(--form-heading-color, #212529) !important;
  margin: 0 0 10px 0;
  font-size: var(--form-heading-size, 28px) !important;
  font-weight: var(--form-heading-weight, 600);
  line-height: 1.2;
  text-align: inherit;
}

.afb-isolated-form .form-description {
  color: var(--form-text-color, #666) !important;
  font-size: var(--form-font-size, 1.1rem) !important;
  line-height: var(--form-line-height, 1.5) !important;
  margin: 0;
}

.afb-isolated-form .apex-dynamic-form {
  background: var(--form-bg, white);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  padding: var(--form-padding, 40px);
  border-radius: var(--form-border-radius, 12px);
  box-shadow: var(--form-shadow, 0 4px 20px rgba(0,0,0,0.1));
  border: var(--form-border-width, 1px) var(--form-border-style, solid) var(--form-border-color, #e9ecef);
  font-family: var(--form-font-family, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif);
  color: var(--form-text-color, inherit);
}

.afb-isolated-form .form-component {
  margin-bottom: 24px;
}

.afb-isolated-form .form-component:last-child {
  margin-bottom: 0;
}

.afb-isolated-form .form-component label {
  font-weight: var(--form-label-font-weight, 600) !important;
  margin-bottom: var(--form-label-spacing, 8px);
  display: block;
  color: var(--form-label-color, inherit) !important;
  font-size: var(--form-label-font-size, 0.95rem) !important;
  font-family: var(--form-font-family, inherit) !important;
}

.afb-isolated-form .form-component .required-indicator {
  color: var(--form-error-color, #e74c3c);
  margin-left: 4px;
  font-size: 1.1em;
}

.afb-isolated-form .form-component input,
.afb-isolated-form .form-component textarea,
.afb-isolated-form .form-component select,
.afb-isolated-form .form-control {
  width: 100% !important;
  padding: var(--form-input-padding, 12px 16px) !important;
  border: var(--form-input-border-width, 2px) var(--form-input-border-style, solid) var(--form-input-border-color, #e0e0e0) !important;
  border-radius: var(--form-input-border-radius, 8px) !important;
  font-size: var(--form-font-size, 16px) !important;
  font-family: var(--form-font-family, inherit) !important;
  transition: all 0.3s ease;
  background-color: var(--form-input-bg, rgba(255, 255, 255, 0.9)) !important;
  color: inherit !important;
}

.afb-isolated-form .form-component input:focus,
.afb-isolated-form .form-component textarea:focus,
.afb-isolated-form .form-component select:focus,
.afb-isolated-form .form-control:focus {
  outline: none !important;
  border-color: var(--form-input-focus-border, var(--form-color-primary, #007bff)) !important;
  box-shadow: var(--form-input-focus-shadow, 0 0 0 4px rgba(0,123,255,0.1)) !important;
  transform: translateY(-1px);
}

.afb-isolated-form .form-component input::placeholder,
.afb-isolated-form .form-component textarea::placeholder {
  color: var(--form-placeholder-color, #999);
  opacity: var(--form-input-placeholder-opacity, 1);
}

.afb-isolated-form .form-component textarea {
  resize: vertical;
  min-height: 100px;
}

.afb-isolated-form .form-component select {
  cursor: pointer;
  background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' fill='none' viewBox='0 0 20 20'%3e%3cpath stroke='%236b7280' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='m6 8 4 4 4-4'/%3e%3c/svg%3e");
  background-position: right 12px center;
  background-repeat: no-repeat;
  background-size: 16px;
  padding-right: 40px;
  appearance: none;
}

.afb-isolated-form .form-component .error {
  border-color: var(--form-error-color, #e74c3c) !important;
  box-shadow: 0 0 0 4px rgba(231,76,60,0.1) !important;
}

.afb-isolated-form .form-component .error-message {
  color: var(--form-error-color, #e74c3c);
  font-size: 0.875rem;
  margin-top: 6px;
  display: flex;
  align-items: center;
  gap: 6px;
}

.afb-isolated-form .form-component .error-message::before {
  content: "⚠";
  font-size: 1em;
}

/* Checkbox and Radio Groups */
.afb-isolated-form .checkbox-group,
.afb-isolated-form .radio-group {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: 8px;
}

.afb-isolated-form .checkbox-item,
.afb-isolated-form .radio-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  border: 2px solid #f0f0f0;
  border-radius: 8px;
  transition: all 0.3s ease;
  cursor: pointer;
}

.afb-isolated-form .checkbox-item:hover,
.afb-isolated-form .radio-item:hover {
  border-color: #007bff;
  background-color: #f8f9ff;
}

.afb-isolated-form .checkbox-item input[type="checkbox"],
.afb-isolated-form .radio-item input[type="radio"] {
  width: 18px;
  height: 18px;
  margin: 0;
  cursor: pointer;
}

.afb-isolated-form .checkbox-item label,
.afb-isolated-form .radio-item label {
  margin: 0;
  font-weight: 500;
  cursor: pointer;
  flex: 1;
}

.afb-isolated-form .checkbox-item input[type="checkbox"]:checked + label,
.afb-isolated-form .radio-item input[type="radio"]:checked + label {
  color: #007bff;
}

/* File Upload */
.afb-isolated-form .form-component input[type="file"] {
  padding: 16px;
  border: 2px dashed #ddd;
  border-radius: 8px;
  background-color: #fafafa;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s ease;
}

.afb-isolated-form .form-component input[type="file"]:hover {
  border-color: #007bff;
  background-color: #f8f9ff;
}

.afb-isolated-form .form-component input[type="file"]:focus {
  border-color: #007bff;
  box-shadow: 0 0 0 4px rgba(0,123,255,0.1);
}

/* Form Actions */
.afb-isolated-form .form-actions {
  text-align: center;
  margin-top: 40px;
  padding-top: 30px;
}

.afb-isolated-form .form-actions .btn {
  min-width: 200px;
  padding: var(--btn-padding, 16px 32px);
  font-size: var(--btn-font-size, var(--form-font-size, 1.1rem));
  font-weight: var(--btn-font-weight, 600);
  font-family: var(--form-font-family, inherit);
  border: none;
  border-radius: var(--btn-border-radius, 8px);
  cursor: pointer;
  transition: all 0.3s ease;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.afb-isolated-form .form-actions .btn-primary {
  background: var(--form-color-primary, linear-gradient(135deg, #007bff 0%, #0056b3 100%));
  color: white;
  box-shadow: 0 4px 12px rgba(0,123,255,0.3);
}

.afb-isolated-form .form-actions .btn-secondary {
  background: linear-gradient(135deg, #6c757d 0%, #545b62 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(108,117,125,0.3);
}

.afb-isolated-form .form-actions .btn-success {
  background: linear-gradient(135deg, #28a745 0%, #1e7e34 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(40,167,69,0.3);
}

.afb-isolated-form .form-actions .btn-danger {
  background: linear-gradient(135deg, #dc3545 0%, #bd2130 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(220,53,69,0.3);
}

.afb-isolated-form .form-actions .btn-warning {
  background: linear-gradient(135deg, #ffc107 0%, #d39e00 100%);
  color: #212529;
  box-shadow: 0 4px 12px rgba(255,193,7,0.3);
}

.afb-isolated-form .form-actions .btn-info {
  background: linear-gradient(135deg, #17a2b8 0%, #117a8b 100%);
  color: white;
  box-shadow: 0 4px 12px rgba(23,162,184,0.3);
}

.afb-isolated-form .form-actions .btn-primary:hover,
.afb-isolated-form .form-actions .btn-secondary:hover,
.afb-isolated-form .form-actions .btn-success:hover,
.afb-isolated-form .form-actions .btn-danger:hover,
.afb-isolated-form .form-actions .btn-warning:hover,
.afb-isolated-form .form-actions .btn-info:hover {
  transform: translateY(-2px);
  filter: brightness(1.1);
}

.afb-isolated-form .form-actions .btn-primary:active,
.afb-isolated-form .form-actions .btn-secondary:active,
.afb-isolated-form .form-actions .btn-success:active,
.afb-isolated-form .form-actions .btn-danger:active,
.afb-isolated-form .form-actions .btn-warning:active,
.afb-isolated-form .form-actions .btn-info:active {
  transform: translateY(0);
}

.afb-isolated-form .form-actions .btn:disabled {
  background: #6c757d;
  cursor: not-allowed;
  transform: none;
  box-shadow: none;
}

/* Form Messages */
.afb-isolated-form .form-messages {
  margin-top: 24px;
}

.afb-isolated-form .form-messages .alert {
  padding: 16px 20px;
  border-radius: 8px;
  display: none;
  align-items: center;
  gap: 12px;
  font-weight: 500;
}

.afb-isolated-form .form-messages .alert.show {
  display: flex;
}

.afb-isolated-form .form-messages .alert i {
  font-size: 1.2em;
}

.afb-isolated-form .alert-success {
  background-color: #d4edda;
  border: 2px solid #c3e6cb;
  color: #155724;
}

.afb-isolated-form .alert-danger {
  background-color: #f8d7da;
  border: 2px solid #f5c6cb;
  color: #721c24;
}

.afb-isolated-form .alert-info {
  background-color: #d1ecf1;
  border: 2px solid #bee5eb;
  color: #0c5460;
}

.afb-isolated-form .alert-warning {
  background-color: #fff3cd;
  border: 2px solid #ffeaa7;
  color: #856404;
}

/* Loading States */
.afb-isolated-form .form-loading {
  position: relative;
}

.afb-isolated-form .form-loading::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(255,255,255,0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
}

.afb-isolated-form .spinner {
  width: 32px;
  height: 32px;
  border: 3px solid #f3f3f3;
  border-top: 3px solid #007bff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

/* Progress Indicators */
.afb-isolated-form .form-progress {
  width: 100%;
  height: 6px;
  background-color: #e9ecef;
  border-radius: 3px;
  overflow: hidden;
  margin-bottom: 24px;
}

.afb-isolated-form .form-progress-bar {
  height: 100%;
  background: linear-gradient(90deg, #007bff 0%, #0056b3 100%);
  border-radius: 3px;
  transition: width 0.3s ease;
}

/* Layout Container Styles */
.afb-isolated-form .layout-container {
  display: flex;
  flex-wrap: wrap;
  width: 100%;
  margin-bottom: 24px;
  box-sizing: border-box;
}

/* Default gap between columns */
.afb-isolated-form .layout-container.gap-sm {
  gap: 10px;
}

.afb-isolated-form .layout-container.gap-md {
  gap: 15px;
}

.afb-isolated-form .layout-container.gap-lg {
  gap: 20px;
}

.afb-isolated-form .layout-container.gap-xl {
  gap: 30px;
}

/* Layout Column Styles */
.afb-isolated-form .layout-column {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
  box-sizing: border-box;
}

/* Column Width Classes */
.afb-isolated-form .layout-column.col-1 { flex: 0 0 8.333%; max-width: 8.333%; }
.afb-isolated-form .layout-column.col-2 { flex: 0 0 16.666%; max-width: 16.666%; }
.afb-isolated-form .layout-column.col-3 { flex: 0 0 25%; max-width: 25%; }
.afb-isolated-form .layout-column.col-4 { flex: 0 0 33.333%; max-width: 33.333%; }
.afb-isolated-form .layout-column.col-5 { flex: 0 0 41.666%; max-width: 41.666%; }
.afb-isolated-form .layout-column.col-6 { flex: 0 0 50%; max-width: 50%; }
.afb-isolated-form .layout-column.col-7 { flex: 0 0 58.333%; max-width: 58.333%; }
.afb-isolated-form .layout-column.col-8 { flex: 0 0 66.666%; max-width: 66.666%; }
.afb-isolated-form .layout-column.col-9 { flex: 0 0 75%; max-width: 75%; }
.afb-isolated-form .layout-column.col-10 { flex: 0 0 83.333%; max-width: 83.333%; }
.afb-isolated-form .layout-column.col-11 { flex: 0 0 91.666%; max-width: 91.666%; }
.afb-isolated-form .layout-column.col-12 { flex: 0 0 100%; max-width: 100%; }

/* Flex Alignment Utilities */
.afb-isolated-form .layout-column.align-start {
  align-items: flex-start;
}

.afb-isolated-form .layout-column.align-center {
  align-items: center;
}

.afb-isolated-form .layout-column.align-end {
  align-items: flex-end;
}

.afb-isolated-form .layout-column.align-stretch {
  align-items: stretch;
}

.afb-isolated-form .layout-column.justify-start {
  justify-content: flex-start;
}

.afb-isolated-form .layout-column.justify-center {
  justify-content: center;
}

.afb-isolated-form .layout-column.justify-end {
  justify-content: flex-end;
}

.afb-isolated-form .layout-column.justify-between {
  justify-content: space-between;
}

.afb-isolated-form .layout-column.justify-around {
  justify-content: space-around;
}

/* Nested Layout Support */
.afb-isolated-form .layout-column .layout-container {
  margin-bottom: 0;
}

/* Responsive Design */
@media (max-width: 768px) {
  .afb-isolated-form .apex-form-render {
    padding: 15px;
  }

  /* Stack layout columns on mobile */
  .afb-isolated-form .layout-container {
    flex-direction: column;
    gap: 20px !important;
  }

  .afb-isolated-form .layout-column {
    width: 100% !important;
    max-width: 100% !important;
    flex: none !important;
  }
  
  .afb-isolated-form .apex-dynamic-form {
    padding: 24px;
  }
  
  .afb-isolated-form .form-header h2 {
    font-size: 1.6rem;
  }
  
  .afb-isolated-form .form-component input,
  .afb-isolated-form .form-component textarea,
  .afb-isolated-form .form-component select {
    padding: 14px 16px;
    font-size: 16px; /* Prevents zoom on iOS */
  }
  
  .afb-isolated-form .form-actions .btn {
    width: 100%;
    margin-bottom: 12px;
    padding: 18px 32px;
  }
  
  .afb-isolated-form .checkbox-group,
  .afb-isolated-form .radio-group {
    gap: 8px;
  }
  
  .afb-isolated-form .checkbox-item,
  .afb-isolated-form .radio-item {
    padding: 16px 12px;
  }
}

@media (max-width: 480px) {
  .afb-isolated-form .apex-form-render {
    padding: 10px;
  }
  
  .afb-isolated-form .apex-dynamic-form {
    padding: 20px;
  }
  
  .afb-isolated-form .form-header {
    margin-bottom: 24px;
  }
  
  .afb-isolated-form .form-header h2 {
    font-size: 1.4rem;
  }
  
  .afb-isolated-form .form-description {
    font-size: 1rem;
  }
  
  .afb-isolated-form .form-component {
    margin-bottom: 20px;
  }
}

/* High Contrast Mode Support */
@media (prefers-contrast: high) {
  .afb-isolated-form .form-component input,
  .afb-isolated-form .form-component textarea,
  .afb-isolated-form .form-component select {
    border-width: 3px;
    border-color: #000;
  }
  
  .form-component input:focus,
  .form-component textarea:focus,
  .form-component select:focus {
    border-color: #0066cc;
    box-shadow: 0 0 0 3px #0066cc;
  }
}

/* Reduced Motion Support */
@media (prefers-reduced-motion: reduce) {
  .afb-isolated-form .form-component input,
  .afb-isolated-form .form-component textarea,
  .form-component select,
  .afb-isolated-form .checkbox-item,
  .radio-item,
  .afb-isolated-form .form-actions .btn {
    transition: none;
  }
  
  .spinner {
    animation: none;
  }
}

/* Dark Mode Support - DISABLED for embedded forms compatibility */
/* Forms must maintain consistent appearance across all client websites */
/* TODO: Implement form properties/styling feature in builder for user control */