/* Enhanced Forms CSS - Conditional Logic, Calculated Fields, and Dynamic Validation */

/* Base enhanced form styling */
.enhanced-dynamic-form {
    position: relative;
}

/* Field containers */
.form-field {
    transition: all 0.3s ease-in-out;
    position: relative;
}

.form-field.d-none {
    opacity: 0;
    max-height: 0;
    overflow: hidden;
    margin: 0;
    padding: 0;
}

/* Calculated fields styling */
.calculated-field {
    background-color: #f8f9fa;
    border-left: 4px solid #28a745;
    position: relative;
    font-weight: 600;
    color: #495057;
}

.calculated-field::after {
    content: "🧮";
    position: absolute;
    right: 10px;
    top: 50%;
    transform: translateY(-50%);
    font-size: 16px;
    opacity: 0.7;
    pointer-events: none;
}

.calculated-field:focus {
    background-color: #e8f5e8;
    border-left-color: #1e7e34;
}

/* Conditional field styling */
.form-field[data-conditional] {
    border-left: 3px solid transparent;
    padding-left: 15px;
}

.form-field[data-conditional]:not(.d-none) {
    border-left-color: #007bff;
    background-color: rgba(0, 123, 255, 0.05);
}

/* Dynamic validation styling */
.validation-message {
    font-size: 0.875rem;
    margin-top: 0.25rem;
    display: block;
}

.validation-message.invalid-feedback {
    color: #dc3545;
}

.form-control.is-invalid {
    border-color: #dc3545;
    box-shadow: 0 0 0 0.2rem rgba(220, 53, 69, 0.25);
}

.form-control.is-valid {
    border-color: #28a745;
    box-shadow: 0 0 0 0.2rem rgba(40, 167, 69, 0.25);
}

/* Loading states */
.enhanced-dynamic-form.loading {
    opacity: 0.7;
    pointer-events: none;
}

.enhanced-dynamic-form.loading::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(255, 255, 255, 0.8);
    display: flex;
    justify-content: center;
    align-items: center;
    z-index: 1000;
}

/* Required field indicators for conditional requirements */
.text-danger[data-conditional-required="true"] {
    animation: fadeIn 0.3s ease-in-out;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: scale(0.8);
    }

    to {
        opacity: 1;
        transform: scale(1);
    }
}

/* Disabled field styling */
.form-control:disabled,
.form-control.disabled {
    background-color: #e9ecef;
    opacity: 0.6;
    cursor: not-allowed;
}

/* Form field transitions */
.form-field {
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.form-field.show-animation {
    animation: slideIn 0.3s ease-out;
}

.form-field.hide-animation {
    animation: slideOut 0.3s ease-in;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateY(-10px);
        max-height: 0;
    }

    to {
        opacity: 1;
        transform: translateY(0);
        max-height: 1000px;
    }
}

@keyframes slideOut {
    from {
        opacity: 1;
        transform: translateY(0);
        max-height: 1000px;
    }

    to {
        opacity: 0;
        transform: translateY(-10px);
        max-height: 0;
    }
}

/* Enhanced field type specific styling */

/* Rating fields */
.rating-field .rating-star {
    font-size: 1.5rem;
    transition: color 0.2s ease;
    cursor: pointer;
    margin-right: 0.25rem;
}

.rating-field .rating-star:hover,
.rating-field .rating-star.active {
    color: #ffc107 !important;
}

/* Slider fields */
.slider-field-container {
    padding: 1rem;
    background-color: #f8f9fa;
    border-radius: 0.375rem;
    border: 1px solid #dee2e6;
}

.slider-field-container .form-range {
    margin-bottom: 0.5rem;
}

.slider-field-container .badge {
    font-size: 1rem;
    padding: 0.5rem 1rem;
}

/* Color fields */
.color-field-container {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.color-field-container .form-control-color {
    width: 60px;
    height: 40px;
    border-radius: 0.375rem;
    border: 2px solid #dee2e6;
    cursor: pointer;
}

/* JSON and Code fields */
.json-field-container,
.code-field-container {
    position: relative;
}

.json-editor,
.code-editor {
    font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', monospace;
    font-size: 0.875rem;
    background-color: #f8f9fa;
    border: 1px solid #dee2e6;
}

.json-editor:focus,
.code-editor:focus {
    background-color: #fff;
    border-color: #80bdff;
}

.json-validation-message {
    font-size: 0.75rem;
    color: #6c757d;
}

.json-validation-message.error {
    color: #dc3545;
}

/* Multi-select and checkbox groups */
.multi-select-container,
.checkbox-group,
.multiselect-checkbox-group {
    max-height: 200px;
    overflow-y: auto;
    border: 1px solid #dee2e6;
    border-radius: 0.375rem;
    padding: 0.5rem;
    background-color: #fff;
}

.multi-select-container .form-check,
.checkbox-group .form-check,
.multiselect-checkbox-group .form-check {
    margin-bottom: 0.5rem;
}

.multi-select-container .form-check:last-child,
.checkbox-group .form-check:last-child,
.multiselect-checkbox-group .form-check:last-child {
    margin-bottom: 0;
}

/* Date and time range fields */
.date-range-field,
.time-range-field {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.date-range-field .form-control,
.time-range-field .form-control {
    flex: 1;
}

/* Responsive design */
@media (max-width: 768px) {

    .date-range-field,
    .time-range-field {
        flex-direction: column;
        gap: 0.75rem;
    }

    .color-field-container {
        flex-direction: column;
        align-items: flex-start;
    }

    .slider-field-container {
        padding: 0.75rem;
    }
}

/* Form validation enhancement */
.enhanced-dynamic-form .was-validated .form-control:valid,
.enhanced-dynamic-form .form-control.is-valid {
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 8 8'%3e%3cpath fill='%2328a745' d='m2.3 6.73.4.38.87-.98.03-.04L6.02 2.7v-.06l-.34-.37c-.1.1-.2.22-.3.34L3.76 4.95l-.88-.84-.6.62z'/%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right calc(0.375em + 0.1875rem) center;
    background-size: calc(0.75em + 0.375rem) calc(0.75em + 0.375rem);
}

.enhanced-dynamic-form .was-validated .form-control:invalid,
.enhanced-dynamic-form .form-control.is-invalid {
    background-image: url("data:image/svg+xml,%3csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 12 12' width='12' height='12' fill='none' stroke='%23dc3545'%3e%3ccircle cx='6' cy='6' r='4.5'/%3e%3cpath d='m5.8 4.6 2.4 2.4M8.2 4.6l-2.4 2.4'/%3e%3c/svg%3e");
    background-repeat: no-repeat;
    background-position: right calc(0.375em + 0.1875rem) center;
    background-size: calc(0.75em + 0.375rem) calc(0.75em + 0.375rem);
}

/* Debug mode styling */
.enhanced-dynamic-form.debug-mode {
    border: 2px dashed #6f42c1;
    padding: 1rem;
    position: relative;
}

.enhanced-dynamic-form.debug-mode::before {
    content: "🔧 Enhanced Form Debug Mode";
    position: absolute;
    top: -12px;
    left: 10px;
    background: #6f42c1;
    color: white;
    padding: 0.25rem 0.5rem;
    border-radius: 0.25rem;
    font-size: 0.75rem;
    font-weight: bold;
}

/* Success states */
.form-field.success {
    border-left-color: #28a745;
    background-color: rgba(40, 167, 69, 0.05);
}

.form-field.success .form-label::after {
    content: " ✓";
    color: #28a745;
    font-weight: bold;
}

/* Error states */
.form-field.error {
    border-left-color: #dc3545;
    background-color: rgba(220, 53, 69, 0.05);
}

.form-field.error .form-label::after {
    content: " ⚠️";
    color: #dc3545;
}

/* Accessibility improvements */
.form-field[aria-hidden="true"] {
    display: none !important;
}

.enhanced-dynamic-form .form-control:focus {
    outline: 2px solid #007bff;
    outline-offset: 2px;
}

/* Print styles */
@media print {

    .enhanced-dynamic-form .calculated-field::after,
    .form-field .text-danger[data-conditional-required="true"] {
        display: none;
    }

    .enhanced-dynamic-form {
        background: white !important;
    }

    .form-field {
        page-break-inside: avoid;
    }
}

/* Multi-column field layout — CSS Grid wrapper from _form_styles.html.erb.
   When the wrapper holds fields, kill the per-row bottom margin so the
   grid's `gap` provides the spacing instead (avoids a double gap on
   wrapped rows). */
.form-field-grid > .field-group,
.form-field-grid > .form-field {
    margin-bottom: 0;
}

/* The builder canvas (#form-fields-container) shrinks its inter-card
   spacing — Bootstrap's mb-3 on each .form-field-item was tuned for
   single-column stacking; the grid handles spacing with `gap`. */
#form-fields-container.form-field-grid > .form-field-item {
    margin-bottom: 0;
}

/* Flatpickr — disabled cells. Stock flatpickr.css uses
   color: rgba(52,58,64,0.1) (10% opacity) which is too faint to read
   as "blocked" against the white calendar. Bump to a more visible
   muted grey while keeping the cursor cue. Subtle, not shouty. */
.flatpickr-day.flatpickr-disabled,
.flatpickr-day.flatpickr-disabled:hover {
    color: #adb5bd !important;
    cursor: not-allowed !important;
}