/**
 * Modal / Dialog
 *
 * Componente de janelas modais e diálogos
 * Baseado nas especificações do Figma - Design System SIDASP
 *
 * @version 1.0.0
 * @date 2025-10-31
 */

/* ============================================================
   BASE MODAL
   ============================================================ */

/**
 * Backdrop (fundo escurecido)
 */
.modal-backdrop {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100vh;
    margin: 0;
    padding: 20px;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
    z-index: 1050;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s ease;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

.modal-backdrop.active {
    opacity: 1;
    visibility: visible;
}

/**
 * Container do modal
 */
.modal {
    position: relative;
    background: var(--color-text-light);
    border-radius: 12px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    max-width: 500px;
    width: 100%;
    max-height: 90vh;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    transform: scale(0.9) translateY(20px);
    opacity: 0;
    transition: all 0.3s ease;
}

.modal-backdrop.active .modal {
    transform: scale(1) translateY(0);
    opacity: 1;
}

/**
 * Header do modal
 */
.modal-header {
    padding: 24px 24px 16px 24px;
    border-bottom: 1px solid var(--color-border-light);
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    flex-shrink: 0;
}

.modal-header-content {
    flex: 1;
}

.modal-title {
    font-size: 20px;
    font-weight: 600;
    color: var(--color-neutral-800);
    margin: 0;
    line-height: 1.4;
}

.modal-subtitle {
    font-size: 14px;
    color: var(--color-text-secondary);
    margin: 4px 0 0 0;
}

.modal-close {
    flex-shrink: 0;
    width: 32px;
    height: 32px;
    padding: 0;
    margin-left: 16px;
    background: transparent;
    border: none;
    border-radius: 6px;
    color: var(--color-text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s ease;
}

.modal-close:hover {
    background: rgba(0, 0, 0, 0.05);
    color: var(--color-neutral-800);
}

.modal-close:active {
    transform: scale(0.95);
}

.modal-close .material-icons {
    font-size: 20px;
}

/**
 * Body do modal
 */
.modal-body {
    padding: 24px;
    overflow-y: auto;
    flex: 1;
    color: var(--color-text-secondary);
    font-size: 14px;
    line-height: 1.6;
}

/**
 * Footer do modal
 */
.modal-footer {
    padding: 16px 24px 24px 24px;
    border-top: 1px solid var(--color-border-light);
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 12px;
    flex-shrink: 0;
}

.modal-footer-start {
    justify-content: flex-start;
}

.modal-footer-center {
    justify-content: center;
}

.modal-footer-between {
    justify-content: space-between;
}

/* ============================================================
   MODAL ALERT (Modais de Alerta)
   ============================================================ */

/**
 * Modal Alert - Estrutura específica para alertas
 */
.modal-alert {
    max-width: 440px;
    text-align: center;
}

.modal-alert .modal-header {
    border-bottom: none;
    padding: 32px 32px 0 32px;
    flex-direction: column;
    align-items: center;
}

.modal-alert .modal-close {
    position: absolute;
    top: 16px;
    right: 16px;
    margin: 0;
}

.modal-alert .modal-body {
    padding: 16px 32px 24px 32px;
    text-align: center;
}

.modal-alert .modal-footer {
    border-top: none;
    padding: 0 32px 32px 32px;
    justify-content: center;
    flex-direction: column;
    gap: 12px;
}

/**
 * Ícone do alert
 */
.modal-alert-icon {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin: 0 auto 20px auto;
    flex-shrink: 0;
}

.modal-alert-icon .material-icons {
    font-size: 32px;
}

/**
 * Título do alert
 */
.modal-alert .modal-title {
    font-size: 22px;
    font-weight: 600;
    color: var(--color-neutral-800);
    margin: 0 0 12px 0;
}

/**
 * Texto do alert
 */
.modal-alert-text {
    font-size: 14px;
    color: var(--color-text-secondary);
    line-height: 1.6;
    margin: 0;
}

/**
 * Botões do alert - largura total
 */
.modal-alert .modal-footer button {
    width: 100%;
    justify-content: center;
}

/* ============================================================
   VARIANTES DE ALERT
   ============================================================ */

/**
 * Alert Padrão / Info (Azul)
 */
.modal-alert-default .modal-alert-icon,
.modal-alert-info .modal-alert-icon {
    background: rgba(62, 104, 251, 0.1); /* RGBA of --color-primary (#3E68FB) */
    color: var(--color-primary);
}

/**
 * Alert Sucesso (Verde)
 */
.modal-alert-success .modal-alert-icon {
    background: var(--color-success-light);
    color: var(--color-success);
}

/**
 * Alert Atenção / Warning (Amarelo)
 */
.modal-alert-warning .modal-alert-icon {
    background: var(--color-warning-light);
    color: var(--color-warning);
}

/**
 * Alert Erro / Danger (Vermelho)
 */
.modal-alert-error .modal-alert-icon,
.modal-alert-danger .modal-alert-icon {
    background: var(--color-danger-light);
    color: var(--color-danger);
}

/* ============================================================
   MODAL SIZES
   ============================================================ */

.modal-sm {
    max-width: 400px;
}

.modal-md {
    max-width: 500px;
}

.modal-lg {
    max-width: 700px;
}

.modal-xl {
    max-width: 900px;
}

.modal-fullscreen {
    max-width: 100%;
    width: 100%;
    height: 100%;
    max-height: 100%;
    border-radius: 0;
}

/* ============================================================
   MODAL SEM HEADER/FOOTER
   ============================================================ */

.modal-no-header .modal-header {
    display: none;
}

.modal-no-footer .modal-footer {
    display: none;
}

.modal-no-padding .modal-body {
    padding: 0;
}

/* ============================================================
   MODAL COM SCROLL
   ============================================================ */

.modal-body-scroll {
    overflow-y: auto;
    max-height: 400px;
}

.modal-body-scroll::-webkit-scrollbar {
    width: 6px;
}

.modal-body-scroll::-webkit-scrollbar-track {
    background: var(--color-bg-secondary);
}

.modal-body-scroll::-webkit-scrollbar-thumb {
    background: var(--color-border);
    border-radius: 3px;
}

.modal-body-scroll::-webkit-scrollbar-thumb:hover {
    background: var(--color-text-muted);
}

/* ============================================================
   MODAL COM IMAGEM
   ============================================================ */

.modal-image {
    width: 100%;
    height: 200px;
    object-fit: cover;
    border-radius: 12px 12px 0 0;
}

.modal-with-image .modal-header {
    padding-top: 16px;
}

/* ============================================================
   MODAL CONFIRMAÇÃO
   ============================================================ */

.modal-confirm {
    max-width: 440px;
}

.modal-confirm .modal-footer {
    flex-direction: row;
}

.modal-confirm .modal-footer button {
    flex: 1;
}

/* ============================================================
   MODAL LOADING
   ============================================================ */

.modal-loading {
    position: relative;
    pointer-events: none;
}

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

.modal-loading-spinner {
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    width: 40px;
    height: 40px;
    border: 4px solid var(--color-border-light);
    border-top-color: var(--color-primary);
    border-radius: 50%;
    animation: modal-spinner 0.8s linear infinite;
    z-index: 1;
}

@keyframes modal-spinner {
    to {
        transform: translate(-50%, -50%) rotate(360deg);
    }
}

/* ============================================================
   MODAL DARK MODE
   ============================================================ */

.modal-dark {
    background: var(--color-bg-dark);
    color: var(--color-neutral-300);
}

.modal-dark .modal-header {
    border-bottom-color: var(--color-neutral-700);
}

.modal-dark .modal-title {
    color: var(--color-text-light);
}

.modal-dark .modal-subtitle,
.modal-dark .modal-body,
.modal-dark .modal-alert-text {
    color: var(--color-neutral-400);
}

.modal-dark .modal-footer {
    border-top-color: var(--color-neutral-700);
}

.modal-dark .modal-close {
    color: var(--color-neutral-400);
}

.modal-dark .modal-close:hover {
    background: rgba(255, 255, 255, 0.1);
    color: var(--color-text-light);
}

/* ============================================================
   ANIMAÇÕES ALTERNATIVAS
   ============================================================ */

.modal-fade-in .modal {
    animation: modal-fade-in 0.3s ease;
}

@keyframes modal-fade-in {
    from {
        opacity: 0;
        transform: scale(0.95);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.modal-slide-down .modal {
    animation: modal-slide-down 0.3s ease;
}

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

.modal-slide-up .modal {
    animation: modal-slide-up 0.3s ease;
}

@keyframes modal-slide-up {
    from {
        opacity: 0;
        transform: translateY(50px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* ============================================================
   MODAL STACK (Múltiplos Modais)
   ============================================================ */

.modal-backdrop[data-modal-level="2"] {
    z-index: 1060;
    background: rgba(0, 0, 0, 0.6);
}

.modal-backdrop[data-modal-level="3"] {
    z-index: 1070;
    background: rgba(0, 0, 0, 0.7);
}

/* ============================================================
   RESPONSIVO
   ============================================================ */

@media (max-width: 768px) {
    .modal {
        max-width: calc(100% - 32px);
        margin: 16px;
    }

    .modal-fullscreen-mobile {
        max-width: 100%;
        width: 100%;
        height: 100%;
        max-height: 100%;
        border-radius: 0;
        margin: 0;
    }

    .modal-header {
        padding: 20px 20px 12px 20px;
    }

    .modal-body {
        padding: 20px;
    }

    .modal-footer {
        padding: 12px 20px 20px 20px;
        flex-direction: column;
    }

    .modal-footer button {
        width: 100%;
    }

    .modal-alert .modal-header {
        padding: 24px 24px 0 24px;
    }

    .modal-alert .modal-body {
        padding: 16px 24px 20px 24px;
    }

    .modal-alert .modal-footer {
        padding: 0 24px 24px 24px;
    }

    .modal-alert-icon {
        width: 56px;
        height: 56px;
        margin-bottom: 16px;
    }

    .modal-alert-icon .material-icons {
        font-size: 28px;
    }

    .modal-alert .modal-title {
        font-size: 20px;
    }

    .modal-lg,
    .modal-xl {
        max-width: calc(100% - 32px);
    }
}

@media (max-width: 480px) {
    .modal-backdrop {
        padding: 0;
    }

    .modal {
        max-width: 100%;
        width: 100%;
        border-radius: 16px 16px 0 0;
        margin: auto 0 0 0;
        max-height: 95vh;
    }

    .modal-alert .modal-alert-icon {
        width: 48px;
        height: 48px;
    }

    .modal-alert .modal-alert-icon .material-icons {
        font-size: 24px;
    }
}

/* ============================================================
   ACESSIBILIDADE
   ============================================================ */

.modal-close:focus {
    outline: 2px solid var(--color-accent-300);
    outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
    .modal-backdrop,
    .modal,
    .modal-close {
        animation: none !important;
        transition: none !important;
    }

    .modal-backdrop.active .modal {
        transform: none;
    }
}

/* Trap focus dentro do modal */
.modal-backdrop.active {
    isolation: isolate;
}

/* ============================================================
   ESTADOS ESPECIAIS
   ============================================================ */

.modal-no-backdrop {
    background: transparent;
    backdrop-filter: none;
}

.modal-static .modal {
    animation: modal-shake 0.3s ease;
}

@keyframes modal-shake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-10px); }
    75% { transform: translateX(10px); }
}

/* ============================================================
   UTILITÁRIOS
   ============================================================ */

/* Previne scroll do body quando modal está aberto */
body.modal-open {
    overflow: hidden;
    position: relative;
}

/* Remove padding do body em mobile */
@media (max-width: 480px) {
    body.modal-open {
        position: fixed;
        width: 100%;
        top: 0;
        left: 0;
    }
}

/* ============================================================
   MODAL COM FORMULÁRIO
   ============================================================ */

.modal-form .modal-body {
    padding: 24px;
}

.modal-form .form-group {
    margin-bottom: 20px;
}

.modal-form .form-group:last-child {
    margin-bottom: 0;
}

/* ============================================================
   MODAL COM LISTA
   ============================================================ */

.modal-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.modal-list-item {
    padding: 12px 16px;
    border-bottom: 1px solid #E5E7EB;
    cursor: pointer;
    transition: background 0.2s ease;
}

.modal-list-item:hover {
    background: #F8F9FB;
}

.modal-list-item:last-child {
    border-bottom: none;
}

.modal-list-item.active {
    background: #EEF3FF;
    color: var(--color-accent-300);
}

/* ============================================================
   MODAL COM TABS
   ============================================================ */

.modal-tabs {
    display: flex;
    gap: 8px;
    padding: 16px 24px 0 24px;
    border-bottom: 1px solid #E5E7EB;
}

.modal-tab {
    padding: 12px 16px;
    background: transparent;
    border: none;
    color: #5A6478;
    font-size: 14px;
    font-weight: 500;
    cursor: pointer;
    border-bottom: 2px solid transparent;
    transition: all 0.2s ease;
}

.modal-tab:hover {
    color: var(--color-neutral-800);
}

.modal-tab.active {
    color: var(--color-accent-300);
    border-bottom-color: var(--color-accent-300);
}

/* ============================================================
   IMPRESSÃO
   ============================================================ */

@media print {
    .modal-backdrop {
        display: none;
    }
}
