/* =========================================
   5. 모달 시스템 (모바일 최적화 버전)
   ========================================= */

.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    backdrop-filter: blur(4px);
    z-index: var(--z-modal);
    padding: 0; /* ✅ 상하좌우 여백 제거 */
    overflow-y: auto;
    align-items: flex-start; /* ✅ center → flex-start (상단 정렬) */
    justify-content: center;
}

/* ✅ 모달 중첩 방지 - 특정 조건에서만 1개 존재 */
.modal + .modal {
    display: none !important;
}

.modal.show {
    display: flex;
    animation: fadeIn 0.2s ease-out;
}

.modal-content {
    background: white;
    border-radius: 0 0 var(--radius-2xl) var(--radius-2xl); /* ✅ 상단은 0, 하단만 둥글게 */
    box-shadow: var(--shadow-2xl);
    max-width: 600px;
    min-width: 320px;
    width: 100%;
    max-height: 100vh; /* ✅ 상하 여백 없이 전체 높이 */
    margin-top: 0; /* ✅ 상단 여백 제거 */
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: slideUp 0.3s ease-out;
    position: relative;
}

/* ✅ 모달 안에 모달 콘텐츠가 중첩되는 것 방지 */
.modal-content .modal-content {
    all: unset;
    display: block;
}

.modal-content.medium {
    max-width: 600px;
}

.modal-content.large {
    max-width: 900px;
}

.modal-content.small {
    max-width: 400px;
}

/* ✅ 마스터 캘린더 일정 모달 전용 (넓은 가로) */
.modal-content.xlarge {
    max-width: 1100px;
}

.modal-content.full {
    max-width: 95vw;
    max-height: 95vh;
}

/* ========== 모달 헤더 ========== */
.modal-header {
    padding: var(--spacing-6);
    border-bottom: 2px solid var(--gray-100);
    display: flex;
    justify-content: space-between;
    align-items: center;
    position: sticky;
    top: 0;
    background: white;
    z-index: 10;
    border-radius: 0; /* ✅ 상단 radius 제거 */
}

.modal-header h2,
.modal-header h3 {
    font-size: var(--text-2xl);
    font-weight: var(--font-bold);
    color: var(--gray-900);
    margin: 0;
    flex: 1;
}

.modal-close,
.btn-close {
    width: 40px;
    height: 40px;
    min-width: 40px;
    border-radius: var(--radius-full);
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--gray-100);
    color: var(--gray-600);
    font-size: var(--text-xl);
    cursor: pointer;
    transition: all var(--transition-fast);
    border: none;
    flex-shrink: 0;
}

.modal-close:hover,
.btn-close:hover {
    background: var(--gray-200);
    color: var(--gray-900);
}

/* ========== 모달 바디 ========== */
.modal-body {
    padding: var(--spacing-6);
    overflow-y: auto;
    overflow-x: hidden;
    max-height: calc(100vh - 140px); /* ✅ 헤더 높이만 빼기 */
}

/* ========== 모달 푸터 ========== */
.modal-footer {
    padding: var(--spacing-6);
    border-top: 1px solid var(--gray-200);
    display: flex;
    justify-content: flex-end;
    gap: var(--spacing-3);
    position: sticky;
    bottom: 0;
    background: white;
    border-radius: 0 0 var(--radius-2xl) var(--radius-2xl);
}

/* ========== 폼 모달 전용 ========== */
.form-row {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: var(--spacing-4);
    margin-bottom: var(--spacing-4);
}

.form-row .form-group {
    margin-bottom: 0;
}

.form-row.full {
    grid-template-columns: 1fr;
}

/* 3열 그리드 */
.form-row.cols-3 {
    grid-template-columns: repeat(3, 1fr);
}

/* ========== 모달 변형 ========== */

/* 확인 모달 */
.modal-confirm .modal-content {
    max-width: 450px;
}

.modal-confirm .modal-body {
    text-align: center;
    padding: var(--spacing-8);
}

.modal-confirm .modal-icon {
    font-size: 48px;
    margin-bottom: var(--spacing-4);
}

.modal-confirm .modal-icon.warning {
    color: var(--warning-500);
}

.modal-confirm .modal-icon.danger {
    color: var(--danger-500);
}

.modal-confirm .modal-icon.success {
    color: var(--success-500);
}

.modal-confirm .modal-icon.info {
    color: var(--info-500);
}

.modal-confirm .modal-message {
    font-size: var(--text-lg);
    color: var(--gray-700);
    margin-bottom: var(--spacing-6);
}

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

/* 사이드 모달 */
.modal-side {
    padding: 0;
    align-items: stretch;
    justify-content: flex-end;
}

.modal-side .modal-content {
    max-width: 500px;
    max-height: 100vh;
    height: 100vh;
    border-radius: 0;
    animation: slideInRight 0.3s ease-out;
}

/* ========== 애니메이션 정의 ========== */
@keyframes fadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes slideUp {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes slideInRight {
    from {
        transform: translateX(100%);
    }
    to {
        transform: translateX(0);
    }
}

/* ========== 모달 오버레이 클릭 처리 ========== */
.modal[data-backdrop="static"] {
    cursor: default;
}

/* ========== 스크롤 잠금 ========== */
body.modal-open {
    overflow: hidden;
}

/* ========== 모달 안의 스크롤 영역 ========== */
.modal-scroll {
    max-height: 400px;
    overflow-y: auto;
    padding: var(--spacing-4);
    border: 1px solid var(--gray-200);
    border-radius: var(--radius-lg);
}

/* ========== 모달 로딩 상태 ========== */
.modal-loading {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(255, 255, 255, 0.9);
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-2xl);
    z-index: 20;
}

.modal-loading .spinner {
    width: 40px;
    height: 40px;
    border-width: 4px;
}

/* ========================================
   모바일 최적화 (태블릿 - 1024px 이하)
   ======================================== */
@media (max-width: 1024px) {
    .modal-content {
        max-width: 90%;
    }
    
    .modal-content.large {
        max-width: 90%;
    }
    
    .modal-content.xlarge {
        max-width: 95%;
    }
    
    .form-row.cols-3 {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* ========================================
   모바일 최적화 (모바일 - 768px 이하)
   ======================================== */
@media (max-width: 768px) {
    .modal {
        padding: 0; /* ✅ 좌우 여백 제거 */
        align-items: flex-end;
    }
    
    /* ✅ xlarge 모달은 PC 스타일 유지 (마스터캘린더 등) */
    .modal.modal-xlarge {
        align-items: flex-start;
        padding: 1rem;
        overflow-y: auto;
    }
    
    .modal-content {
        max-width: 100%; /* ✅ 가로 전체 */
        width: 100%;
        max-height: 92vh;
        border-radius: var(--radius-2xl) var(--radius-2xl) 0 0;
        animation: slideUpMobile 0.3s ease-out;
    }
    
    /* ✅ xlarge 모달은 PC 스타일 유지 */
    .modal-content.xlarge {
        max-width: 95vw;
        width: 95vw;
        max-height: 90vh;
        border-radius: var(--radius-2xl);
        margin: 0 auto;
        animation: slideUp 0.3s ease-out;
    }
    
    .modal-content.xlarge .modal-header {
        border-radius: var(--radius-2xl) var(--radius-2xl) 0 0;
    }
    
    .modal-content.xlarge .modal-body {
        max-height: calc(90vh - 80px);
        padding: 1rem;
        overflow-y: auto;
        overflow-x: auto; /* ✅ 가로 스크롤 추가 */
        -webkit-overflow-scrolling: touch;
    }
    
    /* ✅ xlarge 모달 내부 콘텐츠 최소 너비 보장 */
    .modal-content.xlarge .modal-body > div {
        min-width: 700px;
    }
    
    @keyframes slideUpMobile {
        from {
            transform: translateY(100%);
        }
        to {
            transform: translateY(0);
        }
    }
    
    .modal-header {
        padding: 1.25rem 1rem;
        border-radius: var(--radius-2xl) var(--radius-2xl) 0 0;
    }
    
    .modal-header h2,
    .modal-header h3 {
        font-size: 1.25rem;
        line-height: 1.4;
    }
    
    .modal-close,
    .btn-close {
        width: 36px;
        height: 36px;
        min-width: 36px;
        font-size: 1.25rem;
    }
    
    .modal-body {
        padding: 1.25rem 1rem;
        max-height: calc(92vh - 120px); /* ✅ 모바일 헤더 + 여유 */
    }
    
    .modal-footer {
        padding: 1rem;
        flex-direction: column-reverse;
        gap: 0.75rem;
    }
    
    .modal-footer .btn,
    .modal-footer .btn-primary,
    .modal-footer .btn-secondary,
    .modal-footer button {
        width: 100%;
        padding: 0.875rem 1rem;
        font-size: 1rem;
        min-height: 48px;
    }
    
    .form-row {
        grid-template-columns: 1fr;
        gap: 1.5rem;
        margin-bottom: 1.5rem;
    }
    
    .form-row.cols-3 {
        grid-template-columns: 1fr;
    }
    
    /* 확인 모달 모바일 */
    .modal-confirm .modal-content {
        max-width: 100%;
    }
    
    .modal-confirm .modal-body {
        padding: 2rem 1.5rem;
    }
    
    .modal-confirm .modal-icon {
        font-size: 56px;
    }
    
    .modal-confirm .modal-message {
        font-size: 1.125rem;
        line-height: 1.6;
    }
    
    /* 사이드 모달 모바일 */
    .modal-side {
        justify-content: stretch;
        align-items: flex-end;
    }
    
    .modal-side .modal-content {
        max-width: 100%;
        width: 100%;
        border-radius: var(--radius-2xl) var(--radius-2xl) 0 0;
        animation: slideUpMobile 0.3s ease-out;
    }
    
    /* 모달 스크롤 영역 */
    .modal-scroll {
        max-height: 300px;
    }
}

/* ========================================
   모바일 최적화 (작은 모바일 - 320px 이하)
   ======================================== */
@media (max-width: 320px) {
    .modal-content {
        max-height: 95vh;
    }
    
    /* ✅ xlarge 모달은 PC 스타일 유지 */
    .modal-content.xlarge {
        max-width: 98vw;
        width: 98vw;
        max-height: 95vh;
    }
    
    .modal-header {
        padding: 1rem 0.875rem;
    }
    
    .modal-header h2,
    .modal-header h3 {
        font-size: 1.125rem;
        line-height: 1.4;
    }
    
    .modal-close,
    .btn-close {
        width: 32px;
        height: 32px;
        min-width: 32px;
        font-size: 1.125rem;
    }
    
    .modal-body {
        padding: 1rem 0.875rem;
        max-height: calc(95vh - 110px); /* ✅ 작은 모바일 헤더 + 여유 */
    }
    
    .modal-footer {
        padding: 0.875rem;
        gap: 0.625rem;
    }
    
    .modal-footer .btn,
    .modal-footer .btn-primary,
    .modal-footer .btn-secondary,
    .modal-footer button {
        padding: 0.75rem 0.875rem;
        font-size: 0.9375rem;
        min-height: 44px;
    }
    
    .form-row {
        gap: 1.25rem;
    }
    
    /* 확인 모달 */
    .modal-confirm .modal-body {
        padding: 1.5rem 1rem;
    }
    
    .modal-confirm .modal-icon {
        font-size: 48px;
        margin-bottom: 0.75rem;
    }
    
    .modal-confirm .modal-message {
        font-size: 1rem;
        margin-bottom: 1.25rem;
    }
    
    /* 모달 스크롤 영역 */
    .modal-scroll {
        max-height: 250px;
        padding: 0.75rem;
    }
}

/* ========================================
   터치 디바이스 최적화
   ======================================== */
@media (hover: none) and (pointer: coarse) {
    /* 터치 타겟 최소화 */
    .modal-close,
    .btn-close {
        min-width: 44px;
        min-height: 44px;
        width: 44px;
        height: 44px;
    }
    
    .modal-footer .btn,
    .modal-footer button {
        min-height: 48px;
        padding: 0.875rem 1.25rem;
        font-size: 1rem;
    }
    
    /* 스크롤 개선 */
    .modal-body,
    .modal-scroll {
        -webkit-overflow-scrolling: touch;
        overscroll-behavior: contain;
    }
    
    /* 입력 필드 확대 */
    .modal input,
    .modal select,
    .modal textarea {
        min-height: 44px;
        font-size: 16px; /* iOS 자동 줌 방지 */
    }
}

/* ========================================
   가로 모드 최적화
   ======================================== */
@media (max-height: 600px) and (orientation: landscape) {
    .modal-content {
        max-height: 95vh;
    }
    
    .modal-header {
        padding: 0.75rem 1rem;
    }
    
    .modal-header h2,
    .modal-header h3 {
        font-size: 1.125rem;
    }
    
    .modal-body {
        padding: 1rem;
        max-height: calc(95vh - 100px); /* ✅ 가로 모드 최적화 */
    }
    
    .modal-footer {
        padding: 0.75rem 1rem;
    }
    
    .modal-confirm .modal-body {
        padding: 1.5rem 1rem;
    }
    
    .modal-confirm .modal-icon {
        font-size: 40px;
        margin-bottom: 0.5rem;
    }
}

/* ========================================
   접근성 개선
   ======================================== */
@media (prefers-reduced-motion: reduce) {
    .modal.show,
    .modal-content {
        animation: none;
    }
}

/* 고대비 모드 */
@media (prefers-contrast: high) {
    .modal {
        background: rgba(0, 0, 0, 0.8);
    }
    
    .modal-header {
        border-bottom-width: 3px;
    }
    
    .modal-footer {
        border-top-width: 2px;
    }
}
