/* Frontend CSS */
.formly {
    --ff-surface: #ffffff;
    --ff-border: #d7dde6;
    --ff-border-strong: #2563eb;
    --ff-text: #0f172a;
    --ff-text-muted: #475569;
    --ff-danger: #b91c1c;
    --ff-danger-soft: #fef2f2;
    --ff-danger-border: #fecaca;
    --ff-success: #166534;
    --ff-success-soft: #ecfdf3;
    --ff-success-border: #86efac;
    --ff-warning: #92400e;
    --ff-warning-soft: #fffbeb;
    --ff-warning-border: #fcd34d;
    --ff-grid-gap: 16px;
    --ff-font: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    display: block;
    width: 100%;
    max-width: 100%;
    margin: 0 auto;
    padding: 24px;
    border: 1px solid var(--ff-border);
    border-radius: 16px;
    background: var(--ff-surface);
    box-shadow: none;
    box-sizing: border-box;
    font-family: var(--ff-font);
}

.formly.ff-adopt-theme-fonts {
    --ff-font: inherit;
    font-family: inherit;
}

.formly.ff-adopt-theme-fonts,
.formly.ff-adopt-theme-fonts button,
.formly.ff-adopt-theme-fonts input,
.formly.ff-adopt-theme-fonts select,
.formly.ff-adopt-theme-fonts textarea,
.formly.ff-adopt-theme-fonts .formly-control,
.formly.ff-adopt-theme-fonts .formly-submit,
.formly.ff-adopt-theme-fonts .formly-step-button,
.formly.ff-adopt-theme-fonts .formly-control--file::file-selector-button {
    font-family: inherit;
}

.formly .ff-form-rows,
.formly .ff-form-step-grid {
    display: flex;
    flex-direction: column;
    gap: var(--ff-grid-gap);
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

.formly .ff-form-step {
    display: block;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

.formly .ff-form-row {
    display: grid !important;
    gap: var(--ff-grid-gap);
    align-items: stretch;
    justify-items: stretch;
    width: 100% !important;
    max-width: 100% !important;
    box-sizing: border-box;
    grid-template-columns: minmax(0, 1fr);
}

.formly .ff-form-row[data-layout-columns="2"] {
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
}

.formly .ff-form-row[data-layout-columns="3"] {
    grid-template-columns: repeat(3, minmax(0, 1fr)) !important;
}

.formly .ff-form-row[data-layout-columns="4"] {
    grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
}

/* Legacy flat flex layout fallback (older markup only). */
form.ff-form:not(.formly) {
    --ff-grid-columns: 12;
    --ff-grid-gap: 16px;
    display: flex;
    flex-wrap: wrap;
    gap: var(--ff-grid-gap);
    align-items: flex-start;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

.formly.ff-form {
    display: block;
}

.ff-field,
.formly-field {
    --ff-col-span: 12;
    width: 100%;
    min-width: 0;
    max-width: 100%;
    box-sizing: border-box;
}

.formly .ff-form-row > .ff-field,
.formly .ff-form-row > .formly-field {
    width: 100% !important;
    max-width: none !important;
    min-width: 0 !important;
    flex: none;
    justify-self: stretch;
    align-self: stretch;
}

.formly .ff-form-row > .ff-field.is-full-row,
.formly .ff-form-row > .formly-field.is-full-row,
.formly .ff-form-row > .ff-field.ff-col-12,
.formly .ff-form-row > .formly-field.ff-col-12 {
    grid-column: 1 / -1;
}

.ff-col-1 { --ff-col-span: 1; }
.ff-col-2 { --ff-col-span: 2; }
.ff-col-3 { --ff-col-span: 3; }
.ff-col-4 { --ff-col-span: 4; }
.ff-col-5 { --ff-col-span: 5; }
.ff-col-6 { --ff-col-span: 6; }
.ff-col-7 { --ff-col-span: 7; }
.ff-col-8 { --ff-col-span: 8; }
.ff-col-9 { --ff-col-span: 9; }
.ff-col-10 { --ff-col-span: 10; }
.ff-col-11 { --ff-col-span: 11; }
.ff-col-12 { --ff-col-span: 12; }

.ff-form-progress,
.ff-form-step,
.formly-response {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

.ff-form > .ff-form-progress,
.ff-form > .ff-form-step,
.ff-form > .formly-response {
    flex: 0 0 100%;
}

.ff-form-progress {
    display: flex;
    flex-wrap: wrap;
    gap: 0.85rem 1rem;
    margin-bottom: 0.25rem;
    padding-bottom: 1rem;
    border-bottom: 1px solid #e2e8f0;
}

.ff-form-progress-step {
    display: inline-flex;
    align-items: center;
    gap: 0.55rem;
    min-width: 0;
}

.ff-form-progress-dot {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 2rem;
    border: 1px solid var(--ff-border);
    border-radius: 999px;
    background: #f8fafc;
    color: var(--ff-text-muted);
    font-size: 0.88rem;
    font-weight: 700;
    line-height: 1;
}

.ff-form-progress-dot.is-active,
.ff-form-progress-dot.is-complete {
    border-color: #0f172a;
    color: #0f172a;
}

.ff-form-progress-dot.is-complete {
    background: #eef2ff;
}

.ff-form-progress-label {
    color: var(--ff-text-muted);
    font-size: 0.92rem;
    font-weight: 600;
    line-height: 1.4;
}

.ff-form-step {
    width: 100%;
}

.ff-step-hidden {
    display: none !important;
}

.ff-form-step-header {
    margin-bottom: 1rem;
}

.ff-form-step-kicker {
    margin-bottom: 0.35rem;
    color: var(--ff-text-muted);
    font-size: 0.8rem;
    font-weight: 700;
    letter-spacing: 0.04em;
    text-transform: uppercase;
}

.ff-form-step-title {
    margin: 0;
    color: var(--ff-text);
    font-size: 1.2rem;
    font-weight: 700;
    line-height: 1.3;
}

.ff-form-step-grid + .formly-captcha,
.ff-form-step-grid + .formly-actions {
    margin-top: 1rem;
}

.formly-field > label {
    display: flex;
    align-items: center;
    gap: 0.35rem;
    margin-bottom: 0.45rem;
    color: var(--ff-text);
    font-size: 0.95rem;
    font-weight: 600;
    line-height: 1.4;
}

.ff-screen-reader-text {
    position: absolute !important;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.ff-condition-hidden {
    display: none !important;
}

.formly-required {
    color: #dc2626;
}

.formly .formly-control,
.formly .formly-field input:not([type="checkbox"]):not([type="radio"]),
.formly .formly-field textarea,
.formly .formly-field select {
    display: block;
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0;
    min-height: 44px;
    padding: 0.65rem 0.85rem;
    border: 1px solid var(--ff-border);
    border-radius: 10px;
    background-color: #ffffff;
    color: var(--ff-text);
    font-family: inherit;
    font-size: 0.98rem;
    line-height: 1.45;
    box-sizing: border-box;
    transition: border-color 0.2s ease, background-color 0.2s ease;
}

.formly-field select {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
    padding-right: 2.4rem;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8' fill='none'%3E%3Cpath d='M1.5 1.75L6 6.25L10.5 1.75' stroke='%2364748b' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    background-position: right 0.85rem center;
    background-size: 12px 8px;
}

.formly-field textarea {
    min-height: 120px;
    resize: vertical;
}

.formly-control--file {
    padding: 0.55rem 0.65rem;
    cursor: pointer;
}

.formly-control--file::file-selector-button {
    margin-right: 0.85rem;
    padding: 0.5rem 0.85rem;
    border: 1px solid var(--ff-border);
    border-radius: 8px;
    background: #f8fafc;
    color: var(--ff-text);
    font: inherit;
    cursor: pointer;
}

.formly-field-hint {
    margin-top: 0.45rem;
    color: var(--ff-text-muted);
    font-size: 0.84rem;
    line-height: 1.45;
}

.formly-control::placeholder,
.formly-field input::placeholder,
.formly-field textarea::placeholder {
    color: #94a3b8;
}

.formly-control:focus,
.formly-field input:not([type="checkbox"]):not([type="radio"]):focus,
.formly-field textarea:focus,
.formly-field select:focus {
    outline: none;
    border-color: var(--ff-border-strong);
    box-shadow: none;
}

.formly-field.has-error > label {
    color: var(--ff-danger);
}

.formly-control.is-invalid,
.formly-field input:not([type="checkbox"]):not([type="radio"]).is-invalid,
.formly-field textarea.is-invalid,
.formly-field select.is-invalid {
    border-color: #dc2626;
    background: #fffafb;
    box-shadow: none;
}

.formly-choice-group {
    display: grid;
    gap: 0.65rem;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

.formly-choice-group--classic {
    padding: 0;
    border: 0;
    background: transparent;
}

.formly-choice-group--modern {
    padding: 0;
    border: 0;
    background: transparent;
    gap: 0.55rem;
}

.formly-choice-group--vertical {
    grid-template-columns: minmax(0, 1fr);
}

.formly-choice-group--horizontal {
    grid-template-columns: repeat(auto-fit, minmax(min(100%, 9.5rem), 1fr));
    align-items: start;
}

.formly-choice {
    display: flex;
    align-items: flex-start;
    gap: 0.65rem;
    margin: 0;
    min-width: 0;
    color: var(--ff-text);
    font-size: 0.95rem;
    font-weight: 500;
    line-height: 1.4;
    cursor: pointer;
}

.formly-choice-group--classic .formly-choice {
    padding: 0.15rem 0;
}

.formly-choice-group--modern .formly-choice {
    padding: 0.75rem 0.9rem;
    border: 1px solid var(--ff-border);
    border-radius: 10px;
    background: #ffffff;
    transition: border-color 0.15s ease, background-color 0.15s ease, box-shadow 0.15s ease;
}

.formly-choice-group--modern .formly-choice:hover {
    border-color: #94a3b8;
    background: #f8fafc;
}

.formly-choice-group--modern .formly-choice:has(input:checked) {
    border-color: var(--ff-border-strong);
    background: #eff6ff;
    box-shadow: 0 0 0 1px var(--ff-border-strong);
}

.formly-choice-group--modern .formly-choice:focus-within {
    border-color: var(--ff-border-strong);
    box-shadow: 0 0 0 1px var(--ff-border-strong);
}

.formly-choice span {
    min-width: 0;
    overflow-wrap: anywhere;
}

.formly-choice input[type="checkbox"],
.formly-choice input[type="radio"] {
    width: 1rem;
    height: 1rem;
    margin: 0.15rem 0 0;
    flex-shrink: 0;
    accent-color: #2563eb;
}

.formly-field.has-error .formly-choice-group--modern .formly-choice {
    border-color: #dc2626;
}

.formly-field.has-error .formly-choice-group--modern .formly-choice:has(input:checked) {
    background: #fffafb;
    box-shadow: 0 0 0 1px #dc2626;
}

@media (max-width: 640px) {
    .formly-choice-group--horizontal {
        grid-template-columns: minmax(0, 1fr);
    }
}

.formly-field-error {
    min-height: 1.2rem;
    margin-top: 0.4rem;
    color: var(--ff-danger);
    font-size: 0.84rem;
    font-weight: 500;
    line-height: 1.4;
}

.formly-captcha,
.formly-actions,
.formly-response,
.formly-honeypot {
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
}

.ff-form > .formly-captcha,
.ff-form > .formly-actions,
.ff-form > .formly-response,
.ff-form > .formly-honeypot {
    flex: 0 0 100%;
}

.formly-captcha {
    margin-top: 0.75rem;
    margin-bottom: 1.25rem;
}

.formly-captcha-card {
    padding: 16px;
    border: 1px solid var(--ff-border);
    border-radius: 12px;
    background: #f8fafc;
    box-shadow: none;
}

.formly-captcha-card--math {
    display: grid;
    gap: 0.75rem;
}

.formly-captcha-card--image {
    display: grid;
    gap: 0.85rem;
}

.formly-captcha-card--recaptcha {
    display: grid;
    gap: 0.85rem;
}

.formly-captcha-card.has-error {
    border-color: #dc2626;
}

.formly-captcha-badge {
    display: inline-flex;
    align-items: center;
    width: fit-content;
    padding: 0.25rem 0.6rem;
    border-radius: 999px;
    background: #e2e8f0;
    color: #334155;
    font-size: 0.76rem;
    font-weight: 700;
    letter-spacing: 0.03em;
    text-transform: uppercase;
}

.formly-captcha-question {
    color: var(--ff-text);
    font-size: 1.05rem;
    font-weight: 700;
}

.formly-captcha-help {
    margin: 0;
    color: var(--ff-text-muted);
    font-size: 0.92rem;
}

.formly-captcha-image-row {
    display: flex;
    flex-wrap: wrap;
    gap: 0.85rem;
    align-items: center;
}

.formly-captcha-image {
    display: block;
    width: 170px;
    max-width: 100%;
    height: 60px;
    border: 1px solid var(--ff-border);
    border-radius: 10px;
    background: #ffffff;
}

.formly-captcha-image--fallback {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    color: var(--ff-text-muted);
    font-size: 0.85rem;
    font-weight: 600;
    background: #f8fafc;
}

.formly-captcha-refresh {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2.25rem;
    height: 2.25rem;
    padding: 0;
    border: 0;
    border-radius: 999px;
    background: transparent;
    color: var(--ff-text);
    font-size: 1.2rem;
    font-weight: 700;
    line-height: 1;
    cursor: pointer;
    transition: color 0.2s ease, background-color 0.2s ease, transform 0.2s ease;
}

.formly-captcha-refresh:hover:not(:disabled) {
    color: var(--ff-border-strong);
    background: rgba(37, 99, 235, 0.08);
}

.formly-captcha-refresh:focus-visible {
    outline: none;
    color: var(--ff-border-strong);
    background: rgba(37, 99, 235, 0.08);
}

.formly-captcha-refresh:disabled {
    cursor: wait;
    opacity: 0.65;
}

.formly-captcha-refresh.is-loading {
    transform: rotate(90deg);
}

.formly-captcha-input--image {
    letter-spacing: 0.18em;
    text-transform: uppercase;
}

.formly-captcha-error {
    min-height: 1.2rem;
    color: var(--ff-danger);
    font-size: 0.84rem;
    font-weight: 500;
    line-height: 1.4;
}

.ff-recaptcha-v2-widget {
    min-height: 78px;
}

.ff-recaptcha-v3-widget {
    min-height: 0;
}

.formly-field--captcha {
    margin-bottom: 0;
}

.formly-honeypot {
    display: none !important;
    visibility: hidden !important;
    height: 0 !important;
    width: 0 !important;
    margin: 0 !important;
    padding: 0 !important;
}

.formly-actions {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    flex-wrap: wrap;
    gap: 1rem;
    margin-top: 1.25rem;
}

.formly-captcha + .formly-actions {
    margin-top: 0.25rem;
}

.ff-form-step-actions {
    justify-content: space-between;
}

.formly-step-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 140px;
    min-height: 44px;
    padding: 0.7rem 1.05rem;
    border: 1px solid var(--ff-border);
    border-radius: 10px;
    background: #ffffff;
    color: var(--ff-text);
    font-family: inherit;
    font-size: 0.95rem;
    font-weight: 600;
    line-height: 1.2;
    cursor: pointer;
    transition: border-color 0.2s ease, background-color 0.2s ease, color 0.2s ease;
}

.formly-step-button:hover:not(:disabled) {
    border-color: #94a3b8;
    background: #f8fafc;
}

.formly-step-button:focus-visible {
    outline: none;
    border-color: var(--ff-border-strong);
}

.formly-step-button:disabled {
    cursor: wait;
    opacity: 0.7;
}

.formly-step-button--primary {
    margin-left: auto;
}

.formly-submit {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 0.65rem;
    min-width: 180px;
    min-height: 44px;
    padding: 0.7rem 1.2rem;
    border: 1px solid #0f172a;
    border-radius: 10px;
    background: #0f172a;
    color: #ffffff;
    font-family: inherit;
    font-size: 0.96rem;
    font-weight: 600;
    line-height: 1.2;
    cursor: pointer;
    box-shadow: none;
    transition: background-color 0.2s ease, border-color 0.2s ease, color 0.2s ease;
}

.formly-submit:hover:not(:disabled) {
    background: #1e293b;
    border-color: #1e293b;
}

.formly-submit:focus-visible {
    outline: none;
    border-color: #2563eb;
    box-shadow: none;
}

.formly-submit:disabled {
    cursor: wait;
    opacity: 0.7;
}

.formly-submit-spinner {
    display: none;
    width: 1rem;
    height: 1rem;
    border: 2px solid rgba(255, 255, 255, 0.35);
    border-top-color: #ffffff;
    border-radius: 999px;
    animation: ff-spin 0.8s linear infinite;
}

.formly-submit.is-loading .formly-submit-spinner {
    display: inline-block;
}

.formly-response {
    margin-top: 1rem;
    padding: 0.9rem 1rem;
    border: 1px solid transparent;
    border-radius: 10px;
    font-weight: 600;
    line-height: 1.5;
    transition: opacity 0.2s ease, transform 0.2s ease;
}

.formly-response:focus {
    outline: none;
}

.formly-response.is-visible {
    opacity: 1;
    transform: translateY(0);
}

.formly-response.success,
.formly-response--success {
    background: var(--ff-success-soft);
    color: var(--ff-success);
    border-color: var(--ff-success-border);
}

.formly-response.error,
.formly-response--error {
    background: var(--ff-danger-soft);
    color: var(--ff-danger);
    border-color: var(--ff-danger-border);
}

.formly-response.warning,
.formly-response--warning {
    background: var(--ff-warning-soft);
    color: var(--ff-warning);
    border-color: var(--ff-warning-border);
}

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

@media (max-width: 782px) {
    .formly .ff-form-row[data-layout-columns="2"],
    .formly .ff-form-row[data-layout-columns="3"],
    .formly .ff-form-row[data-layout-columns="4"] {
        grid-template-columns: minmax(0, 1fr) !important;
    }

    .formly .ff-form-row > .ff-field,
    .formly .ff-form-row > .formly-field {
        width: 100% !important;
        --ff-col-span: 12;
    }

    .formly {
        padding: 18px;
    }

    .ff-form-progress {
        gap: 0.75rem;
    }

    .ff-form-progress-step {
        width: 100%;
    }
}

@media (max-width: 640px) {
    .formly-actions.ff-form-step-actions {
        flex-direction: column;
        align-items: stretch;
    }

    .ff-form-step-actions {
        justify-content: flex-start;
    }

    .ff-form-step-actions .formly-step-button {
        width: 100%;
    }

    .formly-step-button--primary {
        margin-left: 0;
    }
}
