/**
 * MODALS & DIALOGS - PharmData Design System v2.0
 * Componentes de sobreposição com acessibilidade completa
 *
 * Inclui: Modal, Dialog de confirmação, Drawer/Sidebar, Lightbox
 * Todos com suporte a trap focus e navegação por teclado.
 */

/* ===================
   CSS CUSTOM PROPERTIES
   =================== */

:root {
    /* Overlay */
    --modal-overlay-bg: rgba(11, 45, 42, 0.6);
    --modal-overlay-blur: 4px;

    /* Modal container */
    --modal-bg: white;
    --modal-border-radius: 0.75rem;
    --modal-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.25);

    /* Sizing */
    --modal-width-sm: 24rem;
    --modal-width-md: 32rem;
    --modal-width-lg: 48rem;
    --modal-width-xl: 64rem;
    --modal-width-full: calc(100vw - 2rem);
    --modal-max-height: calc(100vh - 4rem);

    /* Padding */
    --modal-padding: 1.5rem;
    --modal-header-padding: 1.25rem 1.5rem;
    --modal-footer-padding: 1rem 1.5rem;

    /* Animation */
    --modal-transition: all 0.2s ease-out;

    /* Z-index stack */
    --modal-z-overlay: 1000;
    --modal-z-content: 1001;
}

/* Dark mode */
:root.dark-mode {
    --modal-overlay-bg: rgba(0, 0, 0, 0.7);
    --modal-bg: var(--arctic-mist, #1a2332);
    --modal-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
}

/* ===================
   OVERLAY / BACKDROP
   =================== */

.modal-overlay {
    position: fixed;
    inset: 0;
    z-index: var(--modal-z-overlay);
    background: var(--modal-overlay-bg);
    backdrop-filter: blur(var(--modal-overlay-blur));
    -webkit-backdrop-filter: blur(var(--modal-overlay-blur));
    opacity: 0;
    visibility: hidden;
    transition: var(--modal-transition);
}

.modal-overlay.active,
.modal-overlay[data-state="open"] {
    opacity: 1;
    visibility: visible;
}

/* ===================
   MODAL CONTAINER
   =================== */

.modal {
    position: fixed;
    inset: 0;
    z-index: var(--modal-z-content);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 1rem;
    opacity: 0;
    visibility: hidden;
    pointer-events: none;
    transition: var(--modal-transition);
}

.modal.active,
.modal[data-state="open"] {
    opacity: 1;
    visibility: visible;
    pointer-events: auto;
}

/* ===================
   MODAL CONTENT
   =================== */

.modal-content {
    position: relative;
    width: 100%;
    max-width: var(--modal-width-md);
    max-height: var(--modal-max-height);
    background: var(--modal-bg);
    border-radius: var(--modal-border-radius);
    box-shadow: var(--modal-shadow);
    display: flex;
    flex-direction: column;
    overflow: hidden;
    transform: scale(0.95) translateY(-10px);
    transition: var(--modal-transition);
}

.modal.active .modal-content,
.modal[data-state="open"] .modal-content {
    transform: scale(1) translateY(0);
}

/* Size variants */
.modal-sm .modal-content {
    max-width: var(--modal-width-sm);
}

.modal-lg .modal-content {
    max-width: var(--modal-width-lg);
}

.modal-xl .modal-content {
    max-width: var(--modal-width-xl);
}

.modal-full .modal-content {
    max-width: var(--modal-width-full);
    max-height: calc(100vh - 2rem);
    border-radius: 0.5rem;
}

/* ===================
   MODAL HEADER
   =================== */

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 1rem;
    padding: var(--modal-header-padding);
    border-bottom: 1px solid rgba(183, 228, 213, 0.2);
    flex-shrink: 0;
}

.modal-title {
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--graphite-depth, #1F2937);
    margin: 0;
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.modal-title i,
.modal-title svg {
    color: var(--teal-intense, #2AA198);
    font-size: 1.25rem;
}

/* Close button */
.modal-close {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 2rem;
    height: 2rem;
    border: none;
    background: transparent;
    color: var(--soft-steel, #64748B);
    border-radius: 0.375rem;
    cursor: pointer;
    transition: all 0.15s ease;
    flex-shrink: 0;
}

.modal-close:hover {
    background: rgba(183, 228, 213, 0.1);
    color: var(--graphite-depth, #1F2937);
}

.modal-close:focus-visible {
    outline: none;
    box-shadow: 0 0 0 2px var(--teal-intense, #2AA198);
}

/* ===================
   MODAL BODY
   =================== */

.modal-body {
    flex: 1;
    padding: var(--modal-padding);
    overflow-y: auto;
    overscroll-behavior: contain;
    color: var(--graphite-depth, #1F2937);
}

.modal-body p {
    margin: 0 0 1rem;
    line-height: 1.6;
}

.modal-body p:last-child {
    margin-bottom: 0;
}

/* ===================
   MODAL FOOTER
   =================== */

.modal-footer {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 0.75rem;
    padding: var(--modal-footer-padding);
    border-top: 1px solid rgba(183, 228, 213, 0.2);
    flex-shrink: 0;
    background: rgba(183, 228, 213, 0.03);
}

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

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

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

/* ===================
   DIALOG DE CONFIRMAÇÃO
   Modal simplificado para ações
   =================== */

.dialog {
    text-align: center;
}

.dialog .modal-content {
    max-width: 24rem;
}

.dialog .modal-body {
    padding: 2rem 1.5rem;
}

.dialog-icon {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 4rem;
    height: 4rem;
    border-radius: 50%;
    margin-bottom: 1rem;
    font-size: 1.75rem;
}

.dialog-icon-info {
    background: rgba(59, 130, 246, 0.1);
    color: var(--info, #3B82F6);
}

.dialog-icon-success {
    background: rgba(16, 185, 129, 0.1);
    color: var(--success, #10B981);
}

.dialog-icon-warning {
    background: rgba(245, 158, 11, 0.1);
    color: var(--warning, #F59E0B);
}

.dialog-icon-danger {
    background: rgba(239, 68, 68, 0.1);
    color: var(--error, #EF4444);
}

.dialog-title {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--graphite-depth, #1F2937);
    margin: 0 0 0.5rem;
}

.dialog-message {
    color: var(--soft-steel, #64748B);
    line-height: 1.5;
    margin: 0;
}

.dialog .modal-footer {
    justify-content: center;
    border-top: none;
    background: none;
    padding-top: 0;
}

/* ===================
   DRAWER / SIDEBAR
   Modal lateral
   =================== */

.drawer {
    padding: 0;
}

.drawer .modal-content {
    position: fixed;
    max-width: 20rem;
    max-height: 100vh;
    height: 100%;
    margin: 0;
    border-radius: 0;
}

/* Drawer Right (default) */
.drawer .modal-content,
.drawer-right .modal-content {
    right: 0;
    left: auto;
    transform: translateX(100%);
}

.drawer.active .modal-content,
.drawer-right.active .modal-content {
    transform: translateX(0);
}

/* Drawer Left */
.drawer-left .modal-content {
    left: 0;
    right: auto;
    transform: translateX(-100%);
}

.drawer-left.active .modal-content {
    transform: translateX(0);
}

/* Drawer sizes */
.drawer-sm .modal-content {
    max-width: 16rem;
}

.drawer-lg .modal-content {
    max-width: 28rem;
}

.drawer-xl .modal-content {
    max-width: 40rem;
}

/* ===================
   LIGHTBOX
   Para imagens/mídia
   =================== */

.lightbox {
    background: rgba(0, 0, 0, 0.9);
}

.lightbox .modal-content {
    background: transparent;
    box-shadow: none;
    max-width: 90vw;
    max-height: 90vh;
}

.lightbox-image {
    max-width: 100%;
    max-height: 80vh;
    object-fit: contain;
    border-radius: 0.5rem;
}

.lightbox-caption {
    text-align: center;
    color: white;
    padding: 1rem;
    font-size: 0.875rem;
}

.lightbox .modal-close {
    position: absolute;
    top: 1rem;
    right: 1rem;
    color: white;
    background: rgba(255, 255, 255, 0.1);
}

.lightbox .modal-close:hover {
    background: rgba(255, 255, 255, 0.2);
}

/* Navigation arrows */
.lightbox-nav {
    position: absolute;
    top: 50%;
    transform: translateY(-50%);
    width: 3rem;
    height: 3rem;
    border: none;
    background: rgba(255, 255, 255, 0.1);
    color: white;
    border-radius: 50%;
    cursor: pointer;
    transition: all 0.15s ease;
    display: flex;
    align-items: center;
    justify-content: center;
}

.lightbox-nav:hover {
    background: rgba(255, 255, 255, 0.2);
}

.lightbox-prev {
    left: 1rem;
}

.lightbox-next {
    right: 1rem;
}

/* ===================
   MODAL STACKING
   Múltiplos modais
   =================== */

.modal[data-stack="1"] { z-index: 1011; }
.modal[data-stack="2"] { z-index: 1021; }
.modal[data-stack="3"] { z-index: 1031; }

.modal-overlay[data-stack="1"] { z-index: 1010; }
.modal-overlay[data-stack="2"] { z-index: 1020; }
.modal-overlay[data-stack="3"] { z-index: 1030; }

/* ===================
   BODY SCROLL LOCK
   =================== */

body.modal-open {
    overflow: hidden;
    padding-right: var(--scrollbar-width, 0);
}

/* ===================
   ACCESSIBILITY
   =================== */

/* Focus trap indicator */
.modal-content:focus {
    outline: none;
}

.modal-content:focus-visible {
    outline: 2px solid var(--teal-intense, #2AA198);
    outline-offset: 2px;
}

/* Screen reader only close button label */
.modal-close .sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* High contrast */
@media (prefers-contrast: high) {
    .modal-content {
        border: 2px solid currentColor;
    }
}

/* Reduced motion */
@media (prefers-reduced-motion: reduce) {
    .modal,
    .modal-content,
    .modal-overlay {
        transition: none;
    }
}

/* ===================
   RESPONSIVE
   =================== */

@media (max-width: 640px) {
    .modal-content {
        max-width: 100%;
        max-height: calc(100vh - 2rem);
        margin: 1rem;
        border-radius: 0.75rem;
    }

    .modal-full .modal-content {
        margin: 0;
        max-height: 100vh;
        border-radius: 0;
    }

    .modal-header,
    .modal-body,
    .modal-footer {
        padding-left: 1rem;
        padding-right: 1rem;
    }

    .drawer .modal-content {
        max-width: 85vw;
    }

    .modal-footer {
        flex-direction: column;
    }

    .modal-footer > * {
        width: 100%;
    }
}
