@model EditQuestionnaireViewModel

@{
    ViewData["Title"] = "Edit";
}

<style>
    /* Modern Design Enhancements */
    @@import url('https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700&display=swap');

    :root {
        --primary-color: #6366f1;
        --primary-light: #8b5cf6;
        --primary-dark: #4338ca;
        --success-color: #10b981;
        --danger-color: #ef4444;
        --warning-color: #f59e0b;
        --info-color: #06b6d4;
        --gray-50: #f8fafc;
        --gray-100: #f1f5f9;
        --gray-200: #e2e8f0;
        --gray-300: #cbd5e1;
        --gray-400: #94a3b8;
        --gray-500: #64748b;
        --gray-600: #475569;
        --gray-700: #334155;
        --gray-800: #1e293b;
        --gray-900: #0f172a;
        --shadow-sm: 0 1px 2px 0 rgb(0 0 0 / 0.05);
        --shadow-md: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
        --shadow-lg: 0 10px 15px -3px rgb(0 0 0 / 0.1), 0 4px 6px -4px rgb(0 0 0 / 0.1);
        --shadow-xl: 0 20px 25px -5px rgb(0 0 0 / 0.1), 0 8px 10px -6px rgb(0 0 0 / 0.1);
        --shadow-2xl: 0 25px 50px -12px rgb(0 0 0 / 0.25);
        --border-radius-sm: 8px;
        --border-radius-md: 12px;
        --border-radius-lg: 16px;
        --border-radius-xl: 20px;
    }

    * {
        font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    }

    body {
        background: linear-gradient(135deg, #f1f5f9 0%, #e2e8f0 100%);
        min-height: 100vh;
    }

    .container {
        max-width: 1200px;
    }

    /* Enhanced Card Design */
    .main-card {
        background: white;
        border-radius: var(--border-radius-xl);
        box-shadow: var(--shadow-2xl);
        border: 1px solid var(--gray-200);
        overflow: hidden;
        backdrop-filter: blur(10px);
    }

    .card-header-custom {
        background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-light) 100%);
        color: white;
        padding: 2.5rem;
        text-align: center;
        border: none;
        position: relative;
        overflow: hidden;
    }

        .card-header-custom::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 100 20"><defs><radialGradient id="a" cx="50%" cy="0%" r="100%"><stop offset="0%" stop-color="white" stop-opacity="0.1"/><stop offset="100%" stop-color="white" stop-opacity="0"/></radialGradient></defs><rect width="100" height="20" fill="url(%23a)"/></svg>');
            opacity: 0.3;
        }

        .card-header-custom h5 {
            font-size: 2rem;
            font-weight: 700;
            margin: 0;
            text-shadow: 0 2px 4px rgba(0,0,0,0.1);
            position: relative;
            z-index: 1;
        }

    .card-body-custom {
        padding: 3rem;
        background: var(--gray-50);
    }

    /* Form Enhancements */
    .form-group {
        margin-bottom: 2rem;
    }

    .form-control {
        border: 2px solid var(--gray-200);
        border-radius: var(--border-radius-md);
        padding: 1rem 1.25rem;
        font-size: 0.95rem;
        font-weight: 500;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        background: white;
        box-shadow: var(--shadow-sm);
        width: 100%;
        min-height: 50px;
    }

        .form-control:focus {
            border-color: var(--primary-color);
            background: white;
            box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1), var(--shadow-md);
            outline: none;
            transform: translateY(-1px);
        }

    /* Enhanced Select Styling */
    select.form-control {
        appearance: none;
        -webkit-appearance: none;
        -moz-appearance: none;
        background-color: white;
        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 0.75rem center;
        background-repeat: no-repeat;
        background-size: 1.25em 1.25em;
        padding-right: 2.5rem;
        cursor: pointer;
    }

        select.form-control:focus {
            background-color: white;
            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='%236366f1' stroke-linecap='round' stroke-linejoin='round' stroke-width='1.5' d='m6 8 4 4 4-4'/%3e%3c/svg%3e");
            background-position: right 0.75rem center;
            background-repeat: no-repeat;
            background-size: 1.25em 1.25em;
        }

        select.form-control option {
            padding: 0.75rem;
            font-weight: 500;
            color: var(--gray-900);
            background: white;
        }

    .control-label {
        font-weight: 600;
        color: var(--gray-700);
        margin-bottom: 0.75rem;
        font-size: 0.95rem;
        display: flex;
        align-items: center;
    }

    /* Button Enhancements */
    .btn {
        border-radius: var(--border-radius-md);
        font-weight: 600;
        padding: 0.75rem 1.5rem;
        font-size: 0.875rem;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        border: none;
        text-decoration: none;
        display: inline-flex;
        align-items: center;
        gap: 0.5rem;
        box-shadow: var(--shadow-sm);
        position: relative;
        overflow: hidden;
    }

        .btn::before {
            content: '';
            position: absolute;
            top: 0;
            left: -100%;
            width: 100%;
            height: 100%;
            background: linear-gradient(90deg, transparent, rgba(255,255,255,0.2), transparent);
            transition: left 0.5s;
        }

        .btn:hover::before {
            left: 100%;
        }

    .btn-primary {
        background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-light) 100%);
        color: white;
    }

        .btn-primary:hover {
            background: linear-gradient(135deg, var(--primary-dark) 0%, var(--primary-color) 100%);
            transform: translateY(-2px);
            box-shadow: var(--shadow-lg);
            color: white;
        }

    .btn-success {
        background: linear-gradient(135deg, var(--success-color) 0%, #059669 100%);
        color: white;
    }

        .btn-success:hover {
            background: linear-gradient(135deg, #059669 0%, #047857 100%);
            transform: translateY(-2px);
            box-shadow: var(--shadow-lg);
            color: white;
        }

    .btn-danger {
        background: linear-gradient(135deg, var(--danger-color) 0%, #dc2626 100%);
        color: white;
    }

        .btn-danger:hover {
            background: linear-gradient(135deg, #dc2626 0%, #b91c1c 100%);
            transform: translateY(-2px);
            box-shadow: var(--shadow-lg);
            color: white;
        }

    .btn-info {
        background: linear-gradient(135deg, var(--info-color) 0%, #0891b2 100%);
        color: white;
    }

        .btn-info:hover {
            background: linear-gradient(135deg, #0891b2 0%, #0e7490 100%);
            transform: translateY(-2px);
            box-shadow: var(--shadow-lg);
            color: white;
        }

    .btn-warning {
        background: linear-gradient(135deg, var(--warning-color) 0%, #d97706 100%);
        color: white;
    }

        .btn-warning:hover {
            background: linear-gradient(135deg, #d97706 0%, #b45309 100%);
            transform: translateY(-2px);
            box-shadow: var(--shadow-lg);
            color: white;
        }

    .btn-secondary {
        background: linear-gradient(135deg, var(--gray-500) 0%, var(--gray-600) 100%);
        color: white;
    }

        .btn-secondary:hover {
            background: linear-gradient(135deg, var(--gray-600) 0%, var(--gray-700) 100%);
            transform: translateY(-2px);
            box-shadow: var(--shadow-lg);
            color: white;
        }

    /* Bulk Selection Controls */
    .bulk-selection-controls {
        background: white;
        border-radius: var(--border-radius-lg);
        padding: 2rem;
        margin: 2rem 0;
        box-shadow: var(--shadow-lg);
        border: 1px solid var(--gray-200);
        display: none;
        transition: all 0.3s ease;
    }

        .bulk-selection-controls.show {
            display: block;
            animation: slideDown 0.3s ease;
        }

    @@keyframes slideDown {
        from

    {
        opacity: 0;
        transform: translateY(-20px);
    }

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

    }

    .selection-summary {
        background: linear-gradient(135deg, var(--info-color) 0%, #0891b2 100%);
        color: white;
        padding: 1rem 1.5rem;
        border-radius: var(--border-radius-md);
        margin-bottom: 1.5rem;
        font-weight: 600;
        display: flex;
        align-items: center;
        gap: 0.75rem;
    }

    /* Question Container Enhancements */
    #customCard {
        background: white;
        border: none;
        border-radius: var(--border-radius-lg);
        padding: 3rem;
        margin: 3rem 0;
        box-shadow: var(--shadow-xl);
        position: relative;
        overflow: hidden;
    }

        #customCard::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            right: 0;
            height: 4px;
            background: linear-gradient(90deg, var(--primary-color), var(--primary-light), var(--info-color));
        }

        #customCard h4 {
            font-size: 1.75rem;
            font-weight: 700;
            color: var(--gray-800);
            margin-bottom: 3rem;
            text-align: center;
        }

    .question {
        background: white;
        border-radius: var(--border-radius-lg);
        padding: 2.5rem;
        margin-bottom: 2.5rem;
        box-shadow: var(--shadow-lg);
        border: 1px solid var(--gray-200);
        transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
        position: relative;
        overflow: hidden;
    }

        .question::before {
            content: '';
            position: absolute;
            top: 0;
            left: 0;
            width: 4px;
            height: 100%;
            background: linear-gradient(135deg, var(--primary-color), var(--primary-light));
            transform: scaleY(0);
            transition: transform 0.3s ease;
            transform-origin: bottom;
        }

        .question:hover {
            box-shadow: var(--shadow-2xl);
            transform: translateY(-4px);
            border-color: var(--primary-color);
        }

            .question:hover::before {
                transform: scaleY(1);
            }

        .question.selected {
            border-color: var(--danger-color);
            background: linear-gradient(135deg, #fef2f2 0%, #fecaca 10%, white 100%);
            box-shadow: 0 0 0 3px rgba(239, 68, 68, 0.1), var(--shadow-lg);
        }

            .question.selected::before {
                background: linear-gradient(135deg, var(--danger-color), #dc2626);
                transform: scaleY(1);
            }

    /* Question Checkbox Styling */
    .question-checkbox {
        position: absolute;
        top: 1.5rem;
        right: 1.5rem;
        z-index: 10;
    }

    .custom-checkbox {
        width: 24px;
        height: 24px;
        border: 2px solid var(--gray-300);
        border-radius: 6px;
        background: white;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        transition: all 0.2s ease;
        box-shadow: var(--shadow-sm);
    }

        .custom-checkbox:hover {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
        }

        .custom-checkbox.checked {
            background: var(--danger-color);
            border-color: var(--danger-color);
            color: white;
        }

        .custom-checkbox input {
            display: none;
        }

    /* Master checkbox styling */
    .master-checkbox-container {
        background: white;
        border-radius: var(--border-radius-md);
        padding: 1.5rem;
        margin: 2rem 0;
        box-shadow: var(--shadow-md);
        border: 1px solid var(--gray-200);
        display: flex;
        align-items: center;
        gap: 1rem;
    }

    .master-checkbox {
        width: 28px;
        height: 28px;
        border: 2px solid var(--gray-300);
        border-radius: 8px;
        background: white;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        transition: all 0.2s ease;
        box-shadow: var(--shadow-sm);
    }

        .master-checkbox:hover {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
        }

        .master-checkbox.checked {
            background: var(--primary-color);
            border-color: var(--primary-color);
            color: white;
        }

        .master-checkbox input {
            display: none;
        }

    /* Question Separator */
    .question-separator {
        display: flex;
        align-items: center;
        margin-bottom: 2rem;
    }

    .line-container {
        flex: 1;
    }

    .line-start, .line-end {
        border: none;
        height: 3px;
        background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-light) 100%);
        margin: 0;
        border-radius: 2px;
    }

    .next-question-container {
        padding: 0 1.5rem;
    }

    .badge {
        background: linear-gradient(135deg, var(--primary-color) 0%, var(--primary-light) 100%);
        color: white;
        padding: 0.75rem 1.25rem;
        border-radius: var(--border-radius-md);
        font-weight: 600;
        font-size: 0.8rem;
        text-transform: uppercase;
        letter-spacing: 0.5px;
        box-shadow: var(--shadow-md);
    }

    /* Question Title Styling */
    .question-title {
        background: linear-gradient(135deg, var(--gray-50) 0%, var(--gray-100) 100%);
        padding: 2rem 2rem 2rem 3rem;
        border-radius: var(--border-radius-md);
        border: 1px solid var(--gray-200);
        margin-bottom: 1.5rem;
        transition: all 0.3s ease;
        position: relative;
    }

        .question-title:hover {
            background: linear-gradient(135deg, var(--gray-100) 0%, var(--gray-200) 100%);
            transform: translateY(-1px);
        }

    .item-question {
        font-size: 1.15rem;
        font-weight: 600;
        color: var(--gray-900);
        line-height: 1.6;
        display: block;
        word-wrap: break-word;
        margin-bottom: 0;
    }

    /* Answer Section Enhancements */
    .answers {
        background: linear-gradient(135deg, var(--gray-50) 0%, var(--gray-100) 100%);
        border-radius: var(--border-radius-md);
        padding: 2rem;
        margin: 1.5rem 0;
        border: 1px solid var(--gray-200);
    }

        .answers h6 {
            font-size: 1.1rem;
            font-weight: 600;
            color: var(--gray-800);
            margin-bottom: 1.5rem;
        }

    .answer, .answer-group {
        background: white;
        border-radius: var(--border-radius-md);
        padding: 1.5rem;
        margin-bottom: 1.25rem;
        border: 2px solid var(--gray-200);
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        box-shadow: var(--shadow-sm);
    }

        .answer:hover, .answer-group:hover {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 4px rgba(99, 102, 241, 0.1), var(--shadow-md);
            transform: translateY(-1px);
        }

    /* Other Option Styling */
    .other-option-group {
        background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
        border: 2px solid var(--warning-color);
        position: relative;
    }

        .other-option-group::before {
            content: "OTHER OPTION";
            position: absolute;
            top: -12px;
            left: 20px;
            background: var(--warning-color);
            color: white;
            padding: 0.375rem 1rem;
            border-radius: var(--border-radius-sm);
            font-size: 0.7rem;
            font-weight: 700;
            letter-spacing: 0.5px;
            box-shadow: var(--shadow-sm);
        }

    .other-option-badge {
        background: var(--warning-color);
        color: white;
        padding: 0.375rem 1rem;
        border-radius: var(--border-radius-sm);
        font-size: 0.75rem;
        font-weight: 600;
        margin-left: 0.75rem;
        box-shadow: var(--shadow-sm);
    }

    .other-option-info {
        background: linear-gradient(135deg, #dbeafe 0%, #bfdbfe 100%);
        border: 1px solid var(--info-color);
        border-radius: var(--border-radius-sm);
        padding: 1rem 1.25rem;
        margin-top: 1rem;
        font-size: 0.875rem;
        color: var(--gray-700);
        font-weight: 500;
    }

        .other-option-info i {
            color: var(--info-color);
            margin-right: 0.75rem;
        }

    /* Question Type Info */
    .question-type-info {
        background: var(--gray-100);
        border-radius: var(--border-radius-sm);
        padding: 0.75rem 1rem;
        margin-top: 0.5rem;
        border-left: 4px solid var(--info-color);
        font-size: 0.875rem;
        color: var(--gray-700);
    }

        .question-type-info.supports-other {
            background: linear-gradient(135deg, #dbeafe 0%, #bfdbfe 100%);
            border-left-color: var(--info-color);
        }

        .question-type-info.no-other {
            background: linear-gradient(135deg, #f3f4f6 0%, #e5e7eb 100%);
            border-left-color: var(--gray-400);
        }

    /* Enhanced Other Option Button - Conditional Styling */
    .addOtherOption {
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

        .addOtherOption.hidden {
            display: none !important;
        }

        .addOtherOption.visible {
            display: inline-flex !important;
        }

    /* Action Buttons */
    .action-buttons {
        display: flex;
        gap: 1rem;
        margin-top: 2rem;
        flex-wrap: wrap;
    }

        .action-buttons .btn {
            flex: 0 0 auto;
        }

    /* Add Question Button */
    #addNewQuestion {
        background: linear-gradient(135deg, var(--success-color) 0%, #059669 100%);
        color: white;
        padding: 1.25rem 2.5rem;
        font-size: 1.1rem;
        font-weight: 700;
        border-radius: var(--border-radius-lg);
        margin: 3rem 0;
        box-shadow: var(--shadow-lg);
        border: none;
        transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        position: relative;
        overflow: hidden;
    }

        #addNewQuestion:hover {
            transform: translateY(-3px);
            box-shadow: var(--shadow-2xl);
            background: linear-gradient(135deg, #059669 0%, #047857 100%);
        }

    /* Alert Enhancements */
    .alert {
        border-radius: var(--border-radius-md);
        padding: 1.25rem 1.5rem;
        margin: 1.5rem 0;
        border: none;
        font-weight: 500;
        box-shadow: var(--shadow-sm);
    }

    .alert-success {
        background: linear-gradient(135deg, #d1fae5 0%, #a7f3d0 100%);
        color: #065f46;
        border-left: 4px solid var(--success-color);
    }

    .alert-info {
        background: linear-gradient(135deg, #dbeafe 0%, #bfdbfe 100%);
        color: #0c4a6e;
        border-left: 4px solid var(--info-color);
    }

    .alert-warning {
        background: linear-gradient(135deg, #fef3c7 0%, #fde68a 100%);
        color: #92400e;
        border-left: 4px solid var(--warning-color);
    }

    /* Form Actions */
    .form-actions {
        background: white;
        padding: 2.5rem;
        border-radius: var(--border-radius-lg);
        margin-top: 3rem;
        display: flex;
        gap: 1.5rem;
        flex-wrap: wrap;
        align-items: center;
        box-shadow: var(--shadow-lg);
        border: 1px solid var(--gray-200);
    }

    /* Responsive Design */
    @@media (max-width: 768px) {
        .card-body-custom

    {
        padding: 2rem;
    }

    .question {
        padding: 2rem;
    }

    #customCard {
        padding: 2rem;
    }

    .action-buttons {
        flex-direction: column;
    }

    .form-actions {
        flex-direction: column;
        align-items: stretch;
    }

    .card-header-custom h5 {
        font-size: 1.5rem;
    }

    }

    /* Modal Enhancements */
    .modal-content {
        border-radius: var(--border-radius-lg);
        border: none;
        box-shadow: var(--shadow-2xl);
        overflow: hidden;
    }

    .modal-header {
        background: linear-gradient(135deg, var(--danger-color) 0%, #dc2626 100%);
        color: white;
        border-radius: var(--border-radius-lg) var(--border-radius-lg) 0 0;
        border: none;
        padding: 2rem 2.5rem;
    }

    .modal-body {
        padding: 2.5rem;
        font-size: 1.1rem;
        color: var(--gray-700);
    }

    .modal-footer {
        padding: 2rem 2.5rem;
        border: none;
        background: var(--gray-50);
    }

    /* Loading States */
    .btn.loading {
        opacity: 0.7;
        pointer-events: none;
    }

        .btn.loading::after {
            content: "";
            width: 18px;
            height: 18px;
            margin-left: 0.75rem;
            border: 2px solid transparent;
            border-top-color: currentColor;
            border-radius: 50%;
            animation: spin 1s linear infinite;
        }

    @@keyframes spin {
        0%

    {
        transform: rotate(0deg);
    }

    100% {
        transform: rotate(360deg);
    }

    }

    /* Animations */
    .fade-in {
        animation: fadeIn 0.4s cubic-bezier(0.4, 0, 0.2, 1);
    }

    @@keyframes fadeIn {
        from

    {
        opacity: 0;
        transform: translateY(20px);
    }

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

    }

    /* Enhanced Focus States */
    .btn:focus,
    .form-control:focus {
        outline: 2px solid var(--primary-color);
        outline-offset: 2px;
    }

    /* Smooth scrolling */
    html {
        scroll-behavior: smooth;
    }

    /* Enhanced Typography */
    h1, h2, h3, h4, h5, h6 {
        font-weight: 700;
        letter-spacing: -0.025em;
    }

    /* Icon styling */
    .bi {
        font-size: 1.1em;
    }
</style>

<div class="container mt-4">
    <div class="card main-card">
        <div class="card-header-custom">
            <h5><i class="bi bi-pencil-square me-2"></i>Edit Survey</h5>
        </div>
        <div class="card-body-custom">
            <form asp-action="Edit">
                <input type="hidden" asp-for="Id" />

                <!-- Basic Information -->
                <div class="row">
                    <div class="col-md-12">
                        <div class="form-group">
                            <label asp-for="Title" class="control-label">
                                <i class="bi bi-card-heading me-2"></i>Survey Title
                            </label>
                            <input asp-for="Title" class="form-control" placeholder="Enter survey title" />
                            <span asp-validation-for="Title" class="text-danger"></span>
                        </div>
                    </div>
                    <div class="col-md-12">
                        <div class="form-group">
                            <label asp-for="Description" class="control-label">
                                <i class="bi bi-card-text me-2"></i>Description
                            </label>
                            <textarea asp-for="Description" class="form-control" rows="4" placeholder="Enter survey description"></textarea>
                            <span asp-validation-for="Description" class="text-danger"></span>
                        </div>
                    </div>
                </div>

                <!-- Questions Section -->
                <div class="card" id="customCard">
                    <div class="text-center">
                        <h4><i class="bi bi-question-circle me-2"></i>Edit Questions or Add New Questions</h4>
                    </div>

                    <!-- Master Checkbox for Select All -->
                    <div class="master-checkbox-container">
                        <div class="master-checkbox" id="selectAllCheckbox">
                            <input type="checkbox" id="selectAllInput">
                            <i class="bi bi-check" style="display: none;"></i>
                        </div>
                        <label for="selectAllInput" class="mb-0 font-weight-bold">
                            <i class="bi bi-check-square me-2"></i>Select All Questions
                        </label>
                        <div class="ms-auto">
                            <span id="selectedCount" class="badge bg-primary">0 selected</span>
                        </div>
                    </div>

                    <!-- Bulk Selection Controls -->
                    <div class="bulk-selection-controls" id="bulkControls">
                        <div class="selection-summary">
                            <i class="bi bi-info-circle"></i>
                            <span id="bulkSelectionText">0 questions selected</span>
                        </div>
                        <div class="d-flex gap-3 flex-wrap">
                            <button type="button" class="btn btn-danger" id="deleteSelectedQuestions">
                                <i class="bi bi-trash-fill"></i> Delete Selected Questions
                            </button>
                            <button type="button" class="btn btn-secondary" id="deselectAll">
                                <i class="bi bi-x-circle"></i> Deselect All
                            </button>
                        </div>
                    </div>

                    <div id="questionsContainer">
                        @for (int i = 0; i < Model.Questions.Count; i++)
                        {
                            <div class="question fade-in" data-question-index="@i">
                                <!-- Question Checkbox -->
                                <div class="question-checkbox">
                                    <div class="custom-checkbox question-select-checkbox">
                                        <input type="checkbox" class="question-checkbox-input">
                                        <i class="bi bi-check" style="display: none;"></i>
                                    </div>
                                </div>

                                <div class="question-separator">
                                    <div class="line-container">
                                        <hr class="line-start">
                                    </div>
                                    <div class="next-question-container">
                                        <span class="badge">Question @(i + 1)</span>
                                    </div>
                                    <div class="line-container">
                                        <hr class="line-end">
                                    </div>
                                </div>

                                <input type="hidden" asp-for="Questions[i].Id" />

                                <div class="question-title">
                                    <div class="d-flex justify-content-between align-items-start">
                                        <div class="flex-grow-1">
                                            <span class="item-question">@Html.Raw(Model.Questions[i].Text)</span>
                                        </div>
                                        <div class="action-buttons">
                                            <button type="button" class="btn btn-primary btn-sm editQuestion">
                                                <i class="bi bi-pencil-square"></i> Edit
                                            </button>
                                            <button type="button" class="btn btn-danger btn-sm removeQuestion">
                                                <i class="bi bi-trash"></i> Remove
                                            </button>
                                        </div>
                                    </div>
                                </div>

                                <div class="question-details" style="display: none;">
                                    <div class="form-group">
                                        <label asp-for="Questions[i].Text" class="control-label">Question Text</label>
                                        <input asp-for="Questions[i].Text" class="form-control" placeholder="Enter question text" />
                                        <span asp-validation-for="Questions[i].Text" class="text-danger"></span>
                                    </div>
                                    <div class="form-group">
                                        <label asp-for="Questions[i].Type" class="control-label">Question Type</label>
                                        <select asp-for="Questions[i].Type" asp-items="@ViewBag.QuestionTypes" class="form-control question-type"></select>
                                        <span asp-validation-for="Questions[i].Type" class="text-danger"></span>

                                        <!-- Question Type Information -->
                                        <div class="question-type-info">
                                            <i class="bi bi-info-circle"></i>
                                            <span class="type-info-text">Select a question type to see available options</span>
                                        </div>
                                    </div>

                                    <div class="answers">
                                        <h6 class="mb-3"><i class="bi bi-list-ul me-2"></i>Answer Options</h6>
                                        @for (int j = 0; j < Model.Questions[i].Answers.Count; j++)
                                        {
                                            <div class="answer-group @(Model.Questions[i].Answers[j].IsOtherOption ? "other-option-group" : "")">
                                                <input type="hidden" asp-for="Questions[i].Answers[j].Id" />
                                                <input type="hidden" asp-for="Questions[i].Answers[j].IsOtherOption" />
                                                <div class="form-group mb-0">
                                                    <label asp-for="Questions[i].Answers[j].Text" class="control-label">
                                                        Answer @(j + 1)
                                                        @if (Model.Questions[i].Answers[j].IsOtherOption)
                                                        {
                                                            <span class="other-option-badge">Other Option</span>
                                                        }
                                                    </label>
                                                    <div class="d-flex gap-2">
                                                        <input asp-for="Questions[i].Answers[j].Text" class="form-control" placeholder="Enter answer text" />
                                                        <button type="button" class="btn btn-danger btn-sm removeAnswer">
                                                            <i class="bi bi-trash3"></i>
                                                        </button>
                                                    </div>
                                                    <span asp-validation-for="Questions[i].Answers[j].Text" class="text-danger"></span>
                                                    @if (Model.Questions[i].Answers[j].IsOtherOption)
                                                    {
                                                        <div class="other-option-info">
                                                            <i class="bi bi-info-circle"></i>
                                                            This option allows users to write their own custom response
                                                        </div>
                                                    }
                                                </div>
                                            </div>
                                        }

                                        <div class="d-flex gap-2 mt-3">
                                            <button type="button" class="btn btn-success btn-sm addAnswer">
                                                <i class="bi bi-plus-square"></i> Add Answer
                                            </button>
                                            <button type="button" class="btn btn-warning btn-sm addOtherOption">
                                                <i class="bi bi-pencil-square"></i> Add "Other" Option
                                            </button>
                                        </div>
                                    </div>

                                    <div class="action-buttons">
                                        <button type="button" class="btn btn-primary saveQuestion">
                                            <i class="bi bi-check-circle"></i> Save Changes
                                        </button>
                                        <button type="button" class="btn btn-secondary cancelEdit">
                                            <i class="bi bi-x-circle"></i> Cancel
                                        </button>
                                    </div>
                                </div>
                            </div>
                        }
                    </div>
                </div>

                <div class="text-center">
                    <button type="button" id="addNewQuestion">
                        <i class="bi bi-plus-circle-fill"></i> Add New Question
                    </button>
                </div>

                <div class="form-actions">
                    <button type="submit" class="btn btn-primary btn-lg">
                        <i class="bi bi-save"></i> Update Survey
                    </button>
                    <a asp-action="Index" class="btn btn-info btn-lg">
                        <i class="bi bi-arrow-left"></i> Back to List
                    </a>
                </div>
            </form>
        </div>
    </div>
</div>

<!-- Enhanced Confirmation Modal for Single Question -->
<div class="modal fade" id="confirmDeleteModal" tabindex="-1" aria-labelledby="confirmDeleteModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="confirmDeleteModalLabel">
                    <i class="bi bi-exclamation-triangle me-2"></i>Confirm Deletion
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <p class="mb-3">Are you sure you want to delete this question?</p>
                <div class="alert alert-warning">
                    <i class="bi bi-warning me-2"></i>
                    <strong>Warning:</strong> This action cannot be undone. All associated answers and responses will be permanently removed.
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                    <i class="bi bi-x"></i> Cancel
                </button>
                <button type="button" class="btn btn-danger confirm-delete">
                    <i class="bi bi-trash"></i> Delete Question
                </button>
            </div>
        </div>
    </div>
</div>

<!-- Enhanced Bulk Delete Confirmation Modal -->
<div class="modal fade" id="confirmBulkDeleteModal" tabindex="-1" aria-labelledby="confirmBulkDeleteModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="confirmBulkDeleteModalLabel">
                    <i class="bi bi-exclamation-triangle me-2"></i>Confirm Multiple Deletion
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                <div class="alert alert-danger">
                    <i class="bi bi-exclamation-triangle me-2"></i>
                    <strong>Critical Warning:</strong> You are about to delete <span id="deletionCount" class="fw-bold">0</span> question(s).
                </div>
                <p class="mb-3">This action will permanently remove:</p>
                <ul class="list-unstyled">
                    <li class="mb-2"><i class="bi bi-check text-danger me-2"></i><span id="questionCount">0</span> questions</li>
                    <li class="mb-2"><i class="bi bi-check text-danger me-2"></i>All associated answers</li>
                    <li class="mb-2"><i class="bi bi-check text-danger me-2"></i>All related user responses</li>
                </ul>
                <div class="alert alert-warning">
                    <i class="bi bi-info-circle me-2"></i>
                    <strong>Note:</strong> This action cannot be undone. Please ensure you want to proceed with this bulk deletion.
                </div>
                <div class="preview-container" id="deletionPreview">
                    <h6 class="fw-bold mb-3">Questions to be deleted:</h6>
                    <div class="preview-list" style="max-height: 200px; overflow-y: auto; background: #f8f9fa; padding: 1rem; border-radius: 8px;">
                        <!-- Preview questions will be populated here -->
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">
                    <i class="bi bi-x"></i> Cancel
                </button>
                <button type="button" class="btn btn-danger" id="confirmBulkDelete">
                    <i class="bi bi-trash"></i> Delete All Selected Questions
                </button>
            </div>
        </div>
    </div>
</div>

@section Scripts {
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ckeditor/4.11.4/ckeditor.js"></script>
    <script>
        CKEDITOR.replace("Description");
    </script>
    @{
        <partial name="_ValidationScriptsPartial" />
    }

    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <script>
        $(document).ready(function () {
            let questionToDelete = null;
            let selectedQuestions = [];

            // ===== QUESTION TYPES THAT SUPPORT OTHER OPTIONS =====
            const questionTypesWithOtherOption = [
                'CheckBox',
                'Multiple_choice',
                'Demographic'
            ];

            // ===== FUNCTION TO CHECK IF QUESTION TYPE SUPPORTS OTHER OPTIONS =====
            function supportsOtherOption(questionType) {
                return questionTypesWithOtherOption.includes(questionType);
            }

            // ===== FUNCTION TO UPDATE OTHER OPTION VISIBILITY =====
            function updateOtherOptionVisibility(questionContainer) {
                const questionTypeSelect = questionContainer.find('.question-type');
                const selectedType = questionTypeSelect.val();
                const addOtherBtn = questionContainer.find('.addOtherOption');
                const typeInfo = questionContainer.find('.question-type-info');
                const typeInfoText = typeInfo.find('.type-info-text');

                // Get the text of the selected option
                const selectedOptionText = questionTypeSelect.find('option:selected').text();

                if (supportsOtherOption(selectedType)) {
                    // Show "Add Other Option" button
                    addOtherBtn.removeClass('hidden').addClass('visible').show();
                    typeInfo.removeClass('no-other').addClass('supports-other');
                    typeInfoText.html(`<strong>${selectedOptionText}</strong> supports "Other" options - users can provide custom answers`);
                } else {
                    // Hide "Add Other Option" button
                    addOtherBtn.removeClass('visible').addClass('hidden').hide();
                    typeInfo.removeClass('supports-other').addClass('no-other');
                    typeInfoText.html(`<strong>${selectedOptionText}</strong> does not support "Other" options`);

                    // Remove any existing other options for this question
                    questionContainer.find('.other-option-group').remove();
                }
            }

            // ===== UPDATE EXISTING QUESTIONS ON PAGE LOAD =====
            $('.question').each(function() {
                updateOtherOptionVisibility($(this));
            });

            // ===== HANDLE QUESTION TYPE CHANGES FOR EXISTING QUESTIONS =====
            $(document).on("change", ".question-type", function () {
                const questionContainer = $(this).closest('.question');
                updateOtherOptionVisibility(questionContainer);
            });

            // Update selection UI
            function updateSelectionUI() {
                const selectedCount = selectedQuestions.length;
                $('#selectedCount').text(selectedCount + ' selected');
                $('#bulkSelectionText').text(selectedCount + ' question' + (selectedCount !== 1 ? 's' : '') + ' selected');

                if (selectedCount > 0) {
                    $('#bulkControls').addClass('show');
                } else {
                    $('#bulkControls').removeClass('show');
                }

                // Update master checkbox state
                const totalQuestions = $('.question-checkbox-input').length;
                const masterCheckbox = $('#selectAllCheckbox');
                const masterInput = $('#selectAllInput');

                if (selectedCount === 0) {
                    masterCheckbox.removeClass('checked');
                    masterInput.prop('checked', false);
                    masterCheckbox.find('i').hide();
                } else if (selectedCount === totalQuestions) {
                    masterCheckbox.addClass('checked');
                    masterInput.prop('checked', true);
                    masterCheckbox.find('i').show();
                } else {
                    masterCheckbox.removeClass('checked');
                    masterInput.prop('checked', false);
                    masterCheckbox.find('i').hide();
                }
            }

            // Handle individual question checkbox clicks
            $(document).on('change', '.question-checkbox-input', function() {
                const questionContainer = $(this).closest('.question');
                const questionIndex = questionContainer.data('question-index');
                const checkbox = $(this).closest('.custom-checkbox');

                if ($(this).is(':checked')) {
                    if (!selectedQuestions.includes(questionIndex)) {
                        selectedQuestions.push(questionIndex);
                        questionContainer.addClass('selected');
                        checkbox.addClass('checked');
                        checkbox.find('i').show();
                    }
                } else {
                    selectedQuestions = selectedQuestions.filter(q => q !== questionIndex);
                    questionContainer.removeClass('selected');
                    checkbox.removeClass('checked');
                    checkbox.find('i').hide();
                }

                updateSelectionUI();
            });

            // Handle checkbox visual clicks
            $(document).on('click', '.question-select-checkbox', function() {
                const input = $(this).find('input');
                input.prop('checked', !input.is(':checked')).trigger('change');
            });

            // Handle master checkbox
            $('#selectAllCheckbox').click(function() {
                const shouldSelectAll = selectedQuestions.length !== $('.question-checkbox-input').length;

                $('.question-checkbox-input').each(function() {
                    const questionContainer = $(this).closest('.question');
                    const questionIndex = questionContainer.data('question-index');
                    const checkbox = $(this).closest('.custom-checkbox');

                    $(this).prop('checked', shouldSelectAll);

                    if (shouldSelectAll) {
                        if (!selectedQuestions.includes(questionIndex)) {
                            selectedQuestions.push(questionIndex);
                        }
                        questionContainer.addClass('selected');
                        checkbox.addClass('checked');
                        checkbox.find('i').show();
                    } else {
                        selectedQuestions = [];
                        questionContainer.removeClass('selected');
                        checkbox.removeClass('checked');
                        checkbox.find('i').hide();
                    }
                });

                updateSelectionUI();
            });

            // Deselect all button
            $('#deselectAll').click(function() {
                selectedQuestions = [];
                $('.question-checkbox-input').prop('checked', false);
                $('.question').removeClass('selected');
                $('.custom-checkbox').removeClass('checked');
                $('.custom-checkbox i').hide();
                updateSelectionUI();
            });

            // Handle bulk delete button
            $('#deleteSelectedQuestions').click(function() {
                if (selectedQuestions.length === 0) {
                    alert('Please select at least one question to delete.');
                    return;
                }

                // Update modal content
                $('#deletionCount, #questionCount').text(selectedQuestions.length);

                // Populate preview
                const previewContainer = $('#deletionPreview .preview-list');
                previewContainer.empty();

                selectedQuestions.forEach(function(questionIndex) {
                    const questionContainer = $(`.question[data-question-index="${questionIndex}"]`);
                    const questionText = questionContainer.find('.item-question').text().trim();
                    const truncatedText = questionText.length > 80 ? questionText.substring(0, 80) + '...' : questionText;

                    previewContainer.append(`
                        <div class="mb-2 p-2 bg-white rounded border">
                            <strong>Question ${questionIndex + 1}:</strong> ${truncatedText}
                        </div>
                    `);
                });

                $('#confirmBulkDeleteModal').modal('show');
            });

            // Confirm bulk deletion
            $('#confirmBulkDelete').click(function() {
                const $btn = $(this);
                $btn.addClass('loading').prop('disabled', true);

                // Sort in descending order to avoid index issues when removing
                selectedQuestions.sort((a, b) => b - a);

                // Remove selected questions with animation
                let delay = 0;
                selectedQuestions.forEach(function(questionIndex) {
                    setTimeout(function() {
                        const questionContainer = $(`.question[data-question-index="${questionIndex}"]`);
                        questionContainer.fadeOut(300, function() {
                            $(this).remove();
                        });
                    }, delay);
                    delay += 100;
                });

                // Reset selection state and re-index form after all deletions
                setTimeout(function() {
                    selectedQuestions = [];
                    updateSelectionUI();

                    // Re-index all remaining questions and their form fields
                    reindexAllQuestions();

                    $('#confirmBulkDeleteModal').modal('hide');
                    $btn.removeClass('loading').prop('disabled', false);

                    // Show success message
                    const successMessage = $('<div class="alert alert-success fade-in" role="alert"><i class="bi bi-check-circle me-2"></i>Selected questions deleted successfully!</div>');
                    $('#customCard').prepend(successMessage);

                    setTimeout(function() {
                        successMessage.fadeOut(300, function() {
                            $(this).remove();
                        });
                    }, 3000);
                }, delay + 500);
            });

            // Function to update question numbers after deletion
            function updateQuestionNumbers() {
                $('.question').each(function(index) {
                    $(this).find('.badge').text('Question ' + (index + 1));
                    $(this).attr('data-question-index', index);
                });
            }

            // Function to properly re-index all form fields after question deletion
            function reindexAllQuestions() {
                $('.question').each(function(questionIndex) {
                    const questionContainer = $(this);

                    // Update data attribute and badge
                    questionContainer.attr('data-question-index', questionIndex);
                    questionContainer.find('.badge').text('Question ' + (questionIndex + 1));

                    // Update question form fields
                    questionContainer.find('input[name*="Questions["]').each(function() {
                        const name = $(this).attr('name');
                        if (name) {
                            const newName = name.replace(/Questions\[\d+\]/, `Questions[${questionIndex}]`);
                            $(this).attr('name', newName);
                        }
                    });

                    questionContainer.find('select[name*="Questions["]').each(function() {
                        const name = $(this).attr('name');
                        if (name) {
                            const newName = name.replace(/Questions\[\d+\]/, `Questions[${questionIndex}]`);
                            $(this).attr('name', newName);
                        }
                    });

                    questionContainer.find('textarea[name*="Questions["]').each(function() {
                        const name = $(this).attr('name');
                        if (name) {
                            const newName = name.replace(/Questions\[\d+\]/, `Questions[${questionIndex}]`);
                            $(this).attr('name', newName);
                        }
                    });

                    // Update answer form fields
                    questionContainer.find('.answer-group').each(function(answerIndex) {
                        $(this).find('input[name*="Answers["]').each(function() {
                            const name = $(this).attr('name');
                            if (name) {
                                const newName = name.replace(/Questions\[\d+\]\.Answers\[\d+\]/, `Questions[${questionIndex}].Answers[${answerIndex}]`);
                                $(this).attr('name', newName);
                            }
                        });

                        // Update answer labels
                        const label = $(this).find('label.control-label').first();
                        if (label.length) {
                            const labelText = label.text();
                            if (labelText.includes('Answer ')) {
                                const otherContent = label.find('.other-option-badge').length > 0 ?
                                    ' <span class="other-option-badge">Other Option</span>' : '';
                                label.html(`Answer ${answerIndex + 1}${otherContent}`);
                            }
                        }
                    });
                });
            }

            // Function to add a new regular answer
            $(document).on('click', '.addAnswer', function () {
                var questionContainer = $(this).closest('.question');
                var questionIndex = questionContainer.attr('data-question-index');
                var newAnswerIndex = questionContainer.find('.answers .answer-group').length;

                var answerHtml = `
                    <div class="answer-group fade-in">
                        <div class="form-group mb-0">
                            <label class="control-label">Answer ${newAnswerIndex + 1}</label>
                            <div class="d-flex gap-2">
                                <input type="text" name="Questions[${questionIndex}].Answers[${newAnswerIndex}].Text" class="form-control" placeholder="Enter answer text" />
                                <input type="hidden" name="Questions[${questionIndex}].Answers[${newAnswerIndex}].IsOtherOption" value="false" />
                                <button type="button" class="btn btn-danger btn-sm removeAnswer">
                                    <i class="bi bi-trash3"></i>
                                </button>
                            </div>
                        </div>
                    </div>`;

                $(this).closest('.d-flex').before(answerHtml);
            });

            // Function to add "Other" option - Enhanced with validation
            $(document).on('click', '.addOtherOption', function () {
                var questionContainer = $(this).closest('.question');
                var questionIndex = questionContainer.attr('data-question-index');
                var newAnswerIndex = questionContainer.find('.answers .answer-group').length;
                var questionTypeSelect = questionContainer.find('.question-type');
                var selectedType = questionTypeSelect.val();

                // Double-check if the question type supports other options
                if (!supportsOtherOption(selectedType)) {
                    const selectedOptionText = questionTypeSelect.find('option:selected').text();
                    alert(`"Other" options are not supported for ${selectedOptionText} question type.`);
                    return;
                }

                // Check if "Other" option already exists
                var existingOther = questionContainer.find('.other-option-group');
                if (existingOther.length > 0) {
                    alert('An "Other" option already exists for this question.');
                    return;
                }

                var otherOptionHtml = `
                    <div class="answer-group other-option-group fade-in">
                        <div class="form-group mb-0">
                            <label class="control-label">
                                Answer ${newAnswerIndex + 1}
                                <span class="other-option-badge">Other Option</span>
                            </label>
                            <div class="d-flex gap-2">
                                <input type="text" name="Questions[${questionIndex}].Answers[${newAnswerIndex}].Text" class="form-control" placeholder="Other (please specify)" value="Other (please specify)" />
                                <input type="hidden" name="Questions[${questionIndex}].Answers[${newAnswerIndex}].IsOtherOption" value="true" />
                                <button type="button" class="btn btn-warning btn-sm removeAnswer">
                                    <i class="bi bi-trash3"></i> Remove Other
                                </button>
                            </div>
                            <div class="other-option-info">
                                <i class="bi bi-info-circle"></i>
                                This option allows users to write their own custom response
                            </div>
                        </div>
                    </div>`;

                $(this).closest('.d-flex').before(otherOptionHtml);
            });

            // Function to remove an answer
            $(document).on('click', '.removeAnswer', function () {
                const questionContainer = $(this).closest('.question');
                $(this).closest('.answer-group').fadeOut(300, function() {
                    $(this).remove();
                    // Re-index answers in this question
                    const questionIndex = questionContainer.attr('data-question-index');
                    questionContainer.find('.answer-group').each(function(answerIndex) {
                        $(this).find('input[name*="Answers["]').each(function() {
                            const name = $(this).attr('name');
                            if (name) {
                                const newName = name.replace(/Questions\[\d+\]\.Answers\[\d+\]/, `Questions[${questionIndex}].Answers[${answerIndex}]`);
                                $(this).attr('name', newName);
                            }
                        });

                        // Update answer labels
                        const label = $(this).find('label.control-label').first();
                        if (label.length) {
                            const otherContent = label.find('.other-option-badge').length > 0 ?
                                ' <span class="other-option-badge">Other Option</span>' : '';
                            label.html(`Answer ${answerIndex + 1}${otherContent}`);
                        }
                    });
                });
            });

            // Function to toggle question edit mode
            $(document).on('click', '.editQuestion', function () {
                var questionContainer = $(this).closest('.question');
                questionContainer.find('.question-title').hide();
                questionContainer.find('.question-details').slideDown(300);

                // Update other option visibility when editing starts
                updateOtherOptionVisibility(questionContainer);
            });

            // Function to cancel edit
            $(document).on('click', '.cancelEdit', function () {
                var questionContainer = $(this).closest('.question');
                questionContainer.find('.question-details').slideUp(300);
                questionContainer.find('.question-title').show();
            });

            // Function to save question details
            $(document).on('click', '.saveQuestion', function () {
                var $btn = $(this);
                var questionContainer = $btn.closest('.question');

                // Add loading state
                $btn.addClass('loading').prop('disabled', true);

                setTimeout(function() {
                    var questionText = questionContainer.find('.question-details input[name$=".Text"]:first').val();

                    // Update the question title display with full text
                    questionContainer.find('.item-question').text(questionText);

                    questionContainer.find('.question-details').slideUp(300);
                    questionContainer.find('.question-title').show();

                    // Show success message
                    var successMessage = $('<div class="alert alert-success fade-in" role="alert"><i class="bi bi-check-circle me-2"></i>Question updated successfully!</div>');
                    questionContainer.prepend(successMessage);

                    setTimeout(function() {
                        successMessage.fadeOut(300, function() {
                            $(this).remove();
                        });
                    }, 3000);

                    // Remove loading state
                    $btn.removeClass('loading').prop('disabled', false);
                }, 500);
            });

            // Function to handle question deletion
            $(document).on('click', '.removeQuestion', function () {
                questionToDelete = $(this).closest('.question');
                $('#confirmDeleteModal').modal('show');
            });

            // Confirm deletion
            $('.confirm-delete').click(function () {
                if (questionToDelete) {
                    const questionIndex = questionToDelete.data('question-index');

                    // Remove from selection if it was selected
                    selectedQuestions = selectedQuestions.filter(q => q !== questionIndex);
                    updateSelectionUI();

                    questionToDelete.fadeOut(300, function() {
                        $(this).remove();
                        // Re-index all remaining questions and their form fields
                        reindexAllQuestions();
                    });
                    $('#confirmDeleteModal').modal('hide');
                    questionToDelete = null;
                }
            });

            // Function to add a new question
            $('#addNewQuestion').click(function () {
                var $btn = $(this);
                $btn.addClass('loading').prop('disabled', true);

                setTimeout(function() {
                    var newQuestionIndex = $('.question').length;

                    var questionHtml = `
                        <div class="question fade-in" data-question-index="${newQuestionIndex}">
                            <!-- Question Checkbox -->
                            <div class="question-checkbox">
                                <div class="custom-checkbox question-select-checkbox">
                                    <input type="checkbox" class="question-checkbox-input">
                                    <i class="bi bi-check" style="display: none;"></i>
                                </div>
                            </div>

                            <div class="question-separator">
                                <div class="line-container">
                                    <hr class="line-start">
                                </div>
                                <div class="next-question-container">
                                    <span class="badge">Question ${newQuestionIndex + 1}</span>
                                </div>
                                <div class="line-container">
                                    <hr class="line-end">
                                </div>
                            </div>

                            <div class="question-details">
                                <div class="form-group">
                                    <label class="control-label">Question Text</label>
                                    <input type="text" class="form-control" name="Questions[${newQuestionIndex}].Text" placeholder="Enter question text"/>
                                </div>
                                <div class="form-group">
                                    <label class="control-label">Question Type</label>
                                    <select class="form-control question-type" name="Questions[${newQuestionIndex}].Type">`;

                    // Add question type options dynamically
                    var questionTypes = @Html.Raw(Json.Serialize(Enum.GetNames(typeof(QuestionType))));
                    for (var i = 0; i < questionTypes.length; i++) {
                        questionHtml += `<option value="${questionTypes[i]}">${questionTypes[i]}</option>`;
                    }

                    questionHtml += `</select>

                                    <!-- Question Type Information -->
                                    <div class="question-type-info">
                                        <i class="bi bi-info-circle"></i>
                                        <span class="type-info-text">Select a question type to see available options</span>
                                    </div>
                                </div>

                                <div class="answers">
                                    <h6 class="mb-3"><i class="bi bi-list-ul me-2"></i>Answer Options</h6>
                                    <div class="answer-group">
                                        <div class="form-group mb-0">
                                            <label class="control-label">Answer 1</label>
                                            <div class="d-flex gap-2">
                                                <input type="text" class="form-control" name="Questions[${newQuestionIndex}].Answers[0].Text" placeholder="Enter answer text"/>
                                                <input type="hidden" name="Questions[${newQuestionIndex}].Answers[0].IsOtherOption" value="false"/>
                                                <button type="button" class="btn btn-danger btn-sm removeAnswer">
                                                    <i class="bi bi-trash3"></i>
                                                </button>
                                            </div>
                                        </div>
                                    </div>

                                    <div class="d-flex gap-2 mt-3">
                                        <button type="button" class="btn btn-success btn-sm addAnswer">
                                            <i class="bi bi-plus-square"></i> Add Answer
                                        </button>
                                        <button type="button" class="btn btn-warning btn-sm addOtherOption hidden">
                                            <i class="bi bi-pencil-square"></i> Add "Other" Option
                                        </button>
                                    </div>
                                </div>

                                <div class="action-buttons">
                                    <button type="button" class="btn btn-primary saveQuestion">
                                        <i class="bi bi-check-circle"></i> Save Question
                                    </button>
                                    <button type="button" class="btn btn-danger removeQuestion">
                                        <i class="bi bi-trash"></i> Remove Question
                                    </button>
                                </div>
                            </div>
                        </div>`;

                    const newQuestionElement = $(questionHtml);
                    $('#questionsContainer').append(newQuestionElement);

                    // Update other option visibility for the new question
                    updateOtherOptionVisibility(newQuestionElement);

                    // Scroll to new question
                    $('html, body').animate({
                        scrollTop: newQuestionElement.offset().top - 100
                    }, 500);

                    $btn.removeClass('loading').prop('disabled', false);
                }, 300);
            });

            // Initialize UI
            updateSelectionUI();
        });
    </script>
}