/**
 * Tailwind Modal System - Standardized CSS with Sleek Animations
 * 
 * Usage:
 * - Add class 'modal-spark-backdrop' to backdrop div
 * - Add class 'modal-spark' to modal content div
 * - Use data-modal-open="modalId" to open
 * - Use data-modal-close="modalId" to close
 */

/* Modal Animation Keyframes */
@keyframes modal-backdrop-fade-in {
    from {
        opacity: 0;
    }

    to {
        opacity: 1;
    }
}

@keyframes modal-backdrop-fade-out {
    from {
        opacity: 1;
    }

    to {
        opacity: 0;
    }
}

@keyframes modal-slide-up {
    from {
        opacity: 0;
        transform: translateY(20px) scale(0.96);
    }

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

@keyframes modal-slide-down {
    from {
        opacity: 1;
        transform: translateY(0) scale(1);
    }

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

/* Modal Backdrop - Hidden by default */
/* CRITICAL: display:none by default, only show with .modal-open class */
.modal-spark-backdrop {
    position: fixed !important;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    -webkit-backdrop-filter: blur(4px);
    backdrop-filter: blur(4px);
    z-index: 9999;
    display: none !important;
    align-items: center;
    justify-content: center;
    padding: var(--spacing-4);
    opacity: 0;
}

/* Modal Backdrop - Opening state */
.modal-spark-backdrop.modal-open {
    display: flex !important;
    animation: modal-backdrop-fade-in 0.15s ease-out forwards;
}

/* Lighter in-page blocking campaigns should keep the underlying app visible. */
.modal-spark-backdrop.modal-campaign-page-backdrop {
    background: rgba(15, 23, 42, 0.16);
    -webkit-backdrop-filter: none;
    backdrop-filter: none;
}

/* Modal Backdrop - Closing state */
.modal-spark-backdrop.modal-closing {
    animation: modal-backdrop-fade-out 0.1s ease-in forwards;
}

/* Modal Content Container */
.modal-spark {
    background: #ffffff;
    border-radius: var(--radius-md);
    border: 1px solid #e2e8f0;
    width: 90%;
    max-width: 500px;
    max-height: 90vh;
    max-height: min(90vh, calc(100dvh - 2rem));
    overflow: hidden;
    display: flex;
    flex-direction: column;
    opacity: 0;
    transform: translateY(20px) scale(0.96);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    transition: none;
}

/* Modal Content - Opening state */
.modal-spark-backdrop.modal-open .modal-spark {
    animation: modal-slide-up 0.15s cubic-bezier(0.16, 1, 0.3, 1) forwards;
}

/* Modal Content - Closing state */
.modal-spark-backdrop.modal-closing .modal-spark {
    animation: modal-slide-down 0.1s ease-in forwards;
}

/* Modal Header */
.modal-spark-header {
    padding: 1.25rem 1.5rem;
    border-bottom: 1px solid #e2e8f0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background: #f8fafc;
}

.modal-spark-title {
    margin: 0;
    font-size: 1.125rem;
    font-weight: var(--font-weight-medium);
    color: #1e293b;
    display: flex;
    align-items: center;
    gap: var(--spacing-2);
}

.modal-spark-close {
    background: none;
    border: none;
    padding: 0.5rem;
    cursor: pointer;
    color: #64748b;
    font-size: 1.25rem;
    line-height: 1;
    transition: color 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-spark-close:hover {
    color: #1e293b;
}

/* Modal Body */
.modal-spark-body {
    padding: var(--spacing-6);
    overflow-y: auto;
    flex: 1;
    overflow-wrap: anywhere;
    word-break: break-word;
}

/* Modal Footer */
.modal-spark-footer {
    padding: 1rem 1.5rem;
    border-top: 1px solid #e2e8f0;
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.75rem;
    flex-wrap: wrap;
    background: #f8fafc;
}

/* Pulse animation for static modals */
@keyframes modal-pulse {

    0%,
    100% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.02);
    }
}

.modal-spark-pulse {
    animation: modal-pulse 0.3s ease-in-out;
}

/* Responsive adjustments */
@media (max-width: 640px) {
    .modal-spark-backdrop {
        padding: 0.5rem;
        align-items: center;
    }

    .modal-spark {
        width: 95%;
        max-width: none;
        max-height: calc(100dvh - 1rem);
        border-radius: 0.75rem;
    }

    .modal-spark-header {
        padding: var(--spacing-4);
        align-items: flex-start;
        gap: 0.5rem;
    }

    .modal-spark-title {
        font-size: 1rem;
        line-height: 1.35;
    }

    .modal-spark-body,
    .modal-spark-footer {
        padding: var(--spacing-4);
    }

    .modal-spark-footer {
        flex-direction: column-reverse;
        align-items: stretch;
        gap: var(--spacing-2);
    }

    .modal-spark-footer :is(button, a) {
        width: 100%;
        display: inline-flex;
        justify-content: center;
        text-align: center;
    }
}

/* Prevent body scroll when modal is open */
body.modal-open {
    overflow: hidden;
}

/* ============================================
   MODAL EXTENSIONS (migrated from inline styles)
   ============================================ */

.modal-section-label {
    font-size: 11px;
    font-weight: 700;
    color: #0078d4;
    text-transform: uppercase;
    letter-spacing: 1.5px;
    margin-bottom: 16px;
    display: block;
}

/* Campaign modals keep their own responsive rules so shared modal changes do not
   affect other flows. */
.modal-spark.modal-campaign-shell {
    width: min(100%, 42rem);
    max-width: min(42rem, calc(100vw - 1.5rem));
    max-height: min(90vh, calc(100dvh - 1.5rem));
    border-radius: 0.875rem;
}

.modal-campaign-form,
.modal-campaign-field,
.modal-campaign-input-wrap,
.modal-campaign-input-wrap > * {
    min-width: 0;
}

.modal-campaign-hero {
    height: var(--campaign-hero-height, 160px);
    background-size: cover;
    background-position: center;
    background-repeat: no-repeat;
    border-radius: 0.875rem 0.875rem 0 0;
    flex-shrink: 0;
}

.modal-campaign-body .modal-campaign-content {
    padding: 1.5rem;
}

.modal-campaign-body .modal-campaign-heading {
    margin-bottom: 1.5rem;
}

.modal-campaign-heading > * {
    overflow-wrap: anywhere;
    word-break: break-word;
}

.modal-campaign-body .modal-campaign-fields {
    gap: 1.25rem;
}

.modal-campaign-input-wrap textarea {
    resize: vertical;
}

.modal-campaign-input-wrap input[type="file"] {
    width: 100%;
    max-width: 100%;
}

.modal-campaign-field .modal-campaign-checkbox {
    align-items: flex-start;
    line-height: 1.4;
}

.modal-spark-footer.modal-campaign-footer {
    padding: 1rem 1.5rem;
}

.spark-loading-spinner {
    width: 48px;
    height: 48px;
    margin: 0 auto;
    border: 4px solid #f1f5f9;
    border-top: 4px solid #0078d4;
    border-radius: 50%;
    animation: sparkSpin 1s cubic-bezier(0.4, 0, 0.2, 1) infinite;
}

@keyframes sparkSpin {
    to {
        transform: rotate(360deg);
    }
}

@keyframes modalPulse {
    0% {
        transform: scale(1);
    }

    50% {
        transform: scale(1.02);
    }

    100% {
        transform: scale(1);
    }
}

.modal-spark-pulse {
    animation: modalPulse 0.3s ease-in-out;
}

/* Modal form input styles */
.modal-spark .input-spark,
.modal-spark .select-spark {
    width: 100%;
    height: 40px;
    border: 1px solid #e2e8f0;
    border-radius: 2px;
    padding: 0 16px;
    font-size: 14px;
    transition: border-color 0.2s;
}

.modal-spark .input-spark:focus {
    border-color: #0078d4;
    outline: none;
}

/* Commune sync announcement modal */
.commune-sync-modal {
    max-width: 680px;
    width: min(92vw, 680px);
    padding: 0;
    border-radius: 14px;
    border: 1px solid #dbe2ea;
    box-shadow: 0 18px 40px rgba(2, 6, 23, 0.28);
    max-height: calc(100dvh - 92px);
}

#communeSyncModal.modal-spark-backdrop {
    z-index: 20000 !important;
    align-items: center;
    justify-content: center;
    padding-top: 0.75rem;
    padding-bottom: 0.75rem;
}

.commune-sync-modal-hero {
    position: relative;
    min-height: clamp(104px, 17vh, 132px);
    background-size: cover;
    background-position: center center;
    background-repeat: no-repeat;
    border-bottom: 1px solid #0f172a;
}

.commune-sync-modal-hero-overlay {
    position: absolute;
    inset: 0;
    background:
        linear-gradient(180deg, rgba(2, 6, 23, 0.06) 0%, rgba(2, 6, 23, 0.22) 100%);
}

.commune-sync-modal-close {
    position: absolute;
    top: 14px;
    right: 14px;
    z-index: 2;
    width: 34px;
    height: 34px;
    color: rgba(255, 255, 255, 0.96);
    background: rgba(2, 6, 23, 0.5);
    border: 1px solid rgba(255, 255, 255, 0.28);
    border-radius: 9999px;
}

.commune-sync-modal-close:hover {
    color: #ffffff;
    background: rgba(2, 6, 23, 0.72);
    border-color: rgba(255, 255, 255, 0.45);
}

.commune-sync-modal-close:focus-visible {
    outline: 2px solid #ffffff;
    outline-offset: 2px;
}

.commune-sync-modal-hero-content {
    position: absolute;
    z-index: 1;
    left: 26px;
    right: 26px;
    bottom: 22px;
    color: #ffffff;
    max-width: 640px;
}

.commune-sync-modal-kicker {
    display: inline-block;
    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.12em;
    text-transform: uppercase;
    border: 1px solid rgba(255, 255, 255, 0.5);
    border-radius: 9999px;
    padding: 5px 12px;
    margin-bottom: 12px;
    background: rgba(2, 6, 23, 0.35);
}

.commune-sync-modal-hero-content h3 {
    margin: 0;
    font-size: clamp(1.55rem, 2.6vw, 2.1rem);
    line-height: 1.1;
    letter-spacing: -0.02em;
    font-weight: 800;
    color: #ffffff;
    text-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
}

.commune-sync-modal-hero-content p {
    margin: 10px 0 0;
    color: rgba(255, 255, 255, 0.92);
    font-size: 0.92rem;
    line-height: 1.45;
    max-width: 560px;
}

.commune-sync-modal-body {
    padding-top: 0.95rem;
    padding-bottom: 0.95rem;
}

.commune-sync-modal-body h4 {
    margin: 0 0 12px;
    color: #0f172a;
    font-size: 0.9rem;
    line-height: 1.34;
    font-weight: 700;
    letter-spacing: -0.01em;
}

.commune-sync-modal-body p {
    margin: 0 0 12px;
    color: #475569;
    line-height: 1.46;
    font-size: 0.82rem;
}

.commune-sync-modal-list {
    margin: 2px 0 12px;
    padding: 0;
    list-style: none;
    display: grid;
    gap: 8px;
}

.commune-sync-modal-list li {
    position: relative;
    padding-left: 15px;
    color: #1e293b;
    line-height: 1.42;
    font-size: 0.82rem;
}

.commune-sync-modal-list li::before {
    content: "";
    width: 8px;
    height: 8px;
    border-radius: 9999px;
    background: #84cc16;
    position: absolute;
    left: 0;
    top: 0.6em;
}

.commune-sync-modal-note {
    margin: 2px 0 0;
    padding: 9px 11px;
    border-left: 3px solid #84cc16;
    background: linear-gradient(180deg, #f8fafc 0%, #f1f5f9 100%);
    color: #334155;
    line-height: 1.4;
    font-size: 0.79rem;
}

.commune-sync-modal-footer {
    justify-content: space-between;
    align-items: center;
    gap: 12px;
    padding-top: 0.85rem;
    padding-bottom: 0.85rem;
    background: #f8fafc;
    border-top-color: #e2e8f0;
}

.commune-sync-modal-counter {
    color: #475569;
    font-size: 0.7rem;
    line-height: 1.35;
}

.commune-sync-modal-footer .btn-spark-primary {
    min-width: 108px;
    font-size: 0.82rem;
    padding: 0.5rem 0.9rem;
    font-weight: 700;
    letter-spacing: 0.01em;
}

.commune-sync-modal-counter strong {
    color: #0f172a;
}

@media (max-width: 640px) {
    .commune-sync-modal {
        width: 96vw;
        max-height: calc(100dvh - 86px);
    }

    #communeSyncModal.modal-spark-backdrop {
        align-items: center;
        justify-content: center;
        padding-top: 0.5rem;
        padding-bottom: 0.5rem;
    }

    .commune-sync-modal-hero {
        min-height: 92px;
    }

    .commune-sync-modal-body h4 {
        font-size: 0.86rem;
    }

    .commune-sync-modal-body p,
    .commune-sync-modal-list li {
        font-size: 0.79rem;
        line-height: 1.38;
    }

    .commune-sync-modal-note {
        font-size: 0.77rem;
        line-height: 1.36;
    }

    .commune-sync-modal-footer {
        justify-content: stretch;
        gap: 10px;
    }

    .commune-sync-modal-counter {
        text-align: center;
    }

    .modal-spark.modal-campaign-shell {
        width: 100%;
        max-width: calc(100vw - 1rem);
        max-height: calc(100dvh - 1rem);
        border-radius: 1rem;
    }

    .modal-campaign-hero {
        height: min(var(--campaign-hero-height, 160px), 9rem);
        border-radius: 1rem 1rem 0 0;
    }

    .modal-campaign-body .modal-campaign-content {
        padding: 1rem;
    }

    .modal-campaign-body .modal-campaign-heading {
        margin-bottom: 1rem;
    }

    .modal-campaign-heading h3 {
        font-size: 1rem;
        line-height: 1.4;
    }

    .modal-campaign-heading p {
        font-size: 0.8125rem;
        line-height: 1.5;
    }

    .modal-campaign-body .modal-campaign-fields {
        gap: 1rem;
    }

    .modal-spark-footer.modal-campaign-footer {
        padding: 1rem;
    }

    .modal-campaign-field .modal-campaign-checkbox {
        gap: 0.75rem;
    }
}
