/* Modal CSS Reset - Inspired by Josh Comeau's Reset */
.modal-overlay,
.modal-overlay *,
.modal-overlay *::before,
.modal-overlay *::after {
    /* Box model reset */
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    border: 0;
    
    /* Font reset to system stack */
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji", sans-serif;
    font-size: 100%;
    font-weight: normal;
    line-height: 1.5;
    vertical-align: baseline;
    
    /* Font smoothing */
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    
    /* Text rendering optimization */
    text-rendering: optimizeLegibility;
    
    /* Reset text decoration and transform */
    text-decoration: none;
    text-transform: none;
    
    /* Reset list styles */
    list-style: none;
    
    /* Prevent text overflow issues */
    overflow-wrap: break-word;
    hyphens: auto;
}

/* Apply text-wrap to headings and paragraphs in modal */
.modal-overlay p {
    text-wrap: pretty;
}

.modal-overlay h1,
.modal-overlay h2,
.modal-overlay h3,
.modal-overlay h4,
.modal-overlay h5,
.modal-overlay h6 {
    text-wrap: balance;
}

/* Media elements reset */
.modal-overlay img,
.modal-overlay picture,
.modal-overlay video,
.modal-overlay canvas,
.modal-overlay svg {
    display: block;
    max-width: 100%;
    height: auto;
}

/* Form elements inherit font */
.modal-overlay input,
.modal-overlay button,
.modal-overlay textarea,
.modal-overlay select {
    font: inherit;
    color: inherit;
}

/* Respect motion preferences */
@media (prefers-reduced-motion: reduce) {
    .modal-overlay,
    .modal-overlay * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
        scroll-behavior: auto !important;
    }
}

/* Modern interpolate-size for smoother animations */
@media (prefers-reduced-motion: no-preference) {
    .modal-overlay {
        interpolate-size: allow-keywords;
    }
}

/* Modal Overlay */
.modal-overlay {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    background-color: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    z-index: 98;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    height: 100vh;
    height: 100dvh;
    
    /* Isolation for stacking context */
    isolation: isolate;
    
    /* Smooth fade-in animation */
    animation: modalFadeIn 0.2s ease-out;
    
    /* Ensure overlay resets */
    margin: 0;
    padding: 0;
    border: none;
    outline: none;
}

@keyframes modalFadeIn {
    from {
        opacity: 0;
    }
    to {
        opacity: 1;
    }
}

@keyframes modalSlideUp {
    from {
        transform: translateY(100%);
        opacity: 0;
    }
    to {
        transform: translateY(0);
        opacity: 1;
    }
}

@keyframes modalSlideIn {
    from {
        transform: translateY(20px) scale(0.95);
        opacity: 0;
    }
    to {
        transform: translateY(0) scale(1);
        opacity: 1;
    }
}

@media (min-width: 640px) {
    .modal-overlay {
        align-items: center;
        padding: 1rem;
    }
}

/* Modal Container */
.modal-container {
    position: relative;
    background-color: white;
    width: 100%;
    max-width: 32rem; /* 512px */
    border-top-left-radius: 1rem;
    border-top-right-radius: 1rem;
    display: flex;
    flex-direction: column;
    
    /* FIXED HEIGHT FOR ALL DEVICES - Prevents resizing */
    height: calc(100vh - 20px);
    height: calc(100dvh - 20px);
    
    /* Safe area padding for mobile devices */
    padding-top: env(safe-area-inset-top, 0);
    padding-right: env(safe-area-inset-right, 0);
    padding-bottom: env(safe-area-inset-bottom, 0);
    padding-left: env(safe-area-inset-left, 0);
    
    /* Smooth slide-up animation on mobile */
    animation: modalSlideUp 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);
    
    /* Subtle shadow for depth */
    box-shadow: 0 -10px 40px -10px rgba(0, 0, 0, 0.1);
    
    /* Container specific resets */
    border: none;
    outline: none;
    overflow: hidden;
    
    /* Prevent content from pushing container size */
    position: relative;
    contain: size layout style paint;
    
    /* Create new stacking context */
    isolation: isolate;
}

@media (min-width: 640px) {
    .modal-container {
        border-radius: 1rem;
        margin: 1rem;
        /* FIXED HEIGHT FOR DESKTOP TOO */
        height: 600px;
        /* Smooth scale animation on desktop */
        animation: modalSlideIn 0.25s cubic-bezier(0.25, 0.46, 0.45, 0.94);
        
        /* Enhanced shadow for desktop */
        box-shadow: 0 20px 60px -10px rgba(0, 0, 0, 0.2),
                    0 10px 30px -15px rgba(0, 0, 0, 0.1);
    }
}

@media (min-width: 1024px) {
    .modal-container {
        /* Slightly taller for larger screens */
        height: 700px;
    }
}

/* Modal Header */
.modal-header {
    font-size: 1rem;
    font-weight: 600;
    padding: 1.25rem 1.5rem;
    width: 100%;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #e5e7eb;
    
    /* Fixed height for header */
    min-height: 64px;
    max-height: 64px;
    flex-shrink: 0;
    
    /* Header styling */
    background-color: white;
    margin: 0;
    position: relative;
    z-index: 10;
}

/* Modal Title */
.modal-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: #111827;
    
    /* Title specific styles */
    font-size: 1rem;
    font-weight: 600;
    line-height: 1.75rem;
    letter-spacing: -0.025em;
    margin: 0;
    padding: 0;
    background: transparent;
    border: none;
    flex: 1;
    padding-right: 1rem;
    
    /* Apply text-wrap balance for better line breaks */
    text-wrap: balance;
}

/* Modal Close Button */
.modal-close-btn {
    font-size: 1.5rem;
    line-height: 1;
    color: #6b7280;
    background-color: transparent;
    border: none;
    cursor: pointer;
    padding: 0.5rem;
    margin: -0.5rem -0.5rem -0.5rem 0;
    appearance: none;
    outline: none;
    transition: all 0.15s ease;
    
    /* Hover area */
    border-radius: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2.5rem;
    height: 2.5rem;
    flex-shrink: 0;
    
    /* Inherit font for consistency */
    font: inherit;
}

.modal-close-btn:hover {
    color: #111827;
    background-color: #f3f4f6;
    transform: rotate(90deg);
}

.modal-close-btn:focus-visible {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
}

.modal-close-btn:active {
    background-color: #e5e7eb;
    transform: rotate(90deg) scale(0.95);
}

/* Modal Body */
.modal-body {
    flex: 1 1 auto;
    overflow-y: auto;
    overflow-x: hidden;
    min-height: 0;
    padding: 1.5rem;
    color: #374151;
    
    /* Smooth scrolling with motion preference check */
    scroll-behavior: smooth;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
    
    /* Body styling */
    background-color: white;
    margin: 0;
    border: none;
    position: relative;
    font-size: 1rem;
    line-height: 1.625;
}

/* Custom scrollbar styling */
.modal-body::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

.modal-body::-webkit-scrollbar-track {
    background: #f9fafb;
    border-radius: 3px;
}

.modal-body::-webkit-scrollbar-thumb {
    background: #d1d5db;
    border-radius: 3px;
    transition: background 0.2s ease;
}

.modal-body::-webkit-scrollbar-thumb:hover {
    background: #9ca3af;
}

/* Firefox scrollbar */
.modal-body {
    scrollbar-width: thin;
    scrollbar-color: #d1d5db #f9fafb;
}

/* Typography in modal body */
.modal-body h1,
.modal-body h2,
.modal-body h3,
.modal-body h4,
.modal-body h5,
.modal-body h6 {
    font-weight: 600;
    line-height: 1.25;
    margin: 0 0 0.75rem 0;
    padding: 0;
    color: #111827;
    letter-spacing: -0.025em;
    text-wrap: balance;
    overflow-wrap: break-word;
}

.modal-body h1 { font-size: 2rem; }
.modal-body h2 { font-size: 1.5rem; }
.modal-body h3 { font-size: 1.25rem; }
.modal-body h4 { font-size: 1.125rem; }
.modal-body h5 { font-size: 1rem; }
.modal-body h6 { font-size: 0.875rem; }

.modal-body p {
    margin: 0 0 1rem 0;
    padding: 0;
    line-height: 1.625;
    color: #374151;
    text-wrap: pretty;
    overflow-wrap: break-word;
}

.modal-body a {
    color: #3b82f6;
    text-decoration: none;
    background: transparent;
    transition: color 0.15s ease;
    border-bottom: 1px solid transparent;
    overflow-wrap: break-word;
}

.modal-body a:hover {
    color: #2563eb;
    border-bottom-color: #2563eb;
}

.modal-body ul,
.modal-body ol {
    margin: 0 0 1rem 1.5rem;
    padding: 0;
    color: #374151;
}

.modal-body ul {
    list-style-type: disc;
}

.modal-body ol {
    list-style-type: decimal;
}

.modal-body li {
    margin: 0 0 0.375rem 0;
    padding: 0;
    line-height: 1.625;
    overflow-wrap: break-word;
}

/* Code blocks and inline code */
.modal-body code {
    font-family: ui-monospace, SFMono-Regular, "SF Mono", Consolas, "Liberation Mono", Menlo, monospace;
    font-size: 0.875em;
    background-color: #f3f4f6;
    padding: 0.125rem 0.25rem;
    border-radius: 0.25rem;
    overflow-wrap: break-word;
}

.modal-body pre {
    font-family: ui-monospace, SFMono-Regular, "SF Mono", Consolas, "Liberation Mono", Menlo, monospace;
    font-size: 0.875rem;
    background-color: #f3f4f6;
    padding: 1rem;
    border-radius: 0.5rem;
    overflow-x: auto;
    margin: 0 0 1rem 0;
}

.modal-body pre code {
    background-color: transparent;
    padding: 0;
}

/* Modal Footer */
.modal-footer {
    border-top: 1px solid #e5e7eb;
    padding: 1rem 1.5rem;
    display: flex;
    justify-content: flex-end;
    gap: 0.75rem;
    
    /* Fixed height for footer */
    min-height: 72px;
    max-height: 72px;
    flex-shrink: 0;
    
    /* Footer styling */
    background-color: #fafafa;
    margin: 0;
    position: relative;
    z-index: 10;
}

/* For browsers that don't support gap property in flexbox */
.modal-footer > *:not(:last-child) {
    margin-right: 0.75rem;
}

/* Modal buttons */
.modal-footer button,
.modal-footer .btn {
    /* Inherit font from parent */
    font: inherit;
    font-size: 0.875rem;
    font-weight: 500;
    line-height: 1.25rem;
    padding: 0.625rem 1.25rem;
    border-radius: 0.5rem;
    border: 1px solid #d1d5db;
    background-color: white;
    color: #ffffff;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    text-align: center;
    text-decoration: none;
    vertical-align: middle;
    white-space: nowrap;
    appearance: none;
    transition: all 0.15s ease;
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
    
    /* Prevent text selection on double click */
    user-select: none;
}

.modal-footer button:hover,
.modal-footer .btn:hover {
    background-color: #f9fafb;
    border-color: #9ca3af;
    transform: translateY(-1px);
    box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);
}

.modal-footer button:focus-visible,
.modal-footer .btn:focus-visible {
    outline: 2px solid #3b82f6;
    outline-offset: 2px;
}

.modal-footer button:active,
.modal-footer .btn:active {
    transform: translateY(0);
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
}

/* Primary button variant */
.modal-footer button.primary,
.modal-footer .btn.primary {
    background-color: #3b82f6;
    border-color: #3b82f6;
    color: white;
}

.modal-footer button.primary:hover,
.modal-footer .btn.primary:hover {
    background-color: #2563eb;
    border-color: #2563eb;
}

.modal-footer button.primary:active,
.modal-footer .btn.primary:active {
    background-color: #1d4ed8;
    border-color: #1d4ed8;
}

/* Disabled state */
.modal-footer button:disabled,
.modal-footer .btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
}

.modal-footer button:disabled:hover,
.modal-footer .btn:disabled:hover {
    background-color: white;
    border-color: #d1d5db;
    transform: none;
    box-shadow: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
}

/* Body overflow hidden class */
.modal-body-overflow-hidden {
    /* Keep scrollbar visible but disable scrolling */
    overflow-y: scroll !important;
    position: fixed !important;
    width: 100% !important;
    
    /* Maintain current scroll position */
    top: var(--scroll-y, 0) !important;
    left: 0 !important;
    right: 0 !important;
}

/* Fix for iOS Safari bounce scrolling */
@supports (-webkit-touch-callout: none) {
    .modal-container {
        position: fixed;
    }
}

/* Internet Explorer 11 specific fixes */
@media screen and (-ms-high-contrast: active), (-ms-high-contrast: none) {
    .modal-overlay {
        display: flex;
    }
    
    .modal-container {
        display: flex;
    }
    
    .modal-header {
        display: flex;
    }
    
    .modal-footer {
        display: flex;
    }
}

/* Additional isolation from parent styles */
.modal-overlay::before,
.modal-overlay::after,
.modal-container::before,
.modal-container::after {
    content: none;
    display: none;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .modal-container {
        border: 2px solid currentColor;
    }
    
    .modal-header,
    .modal-footer {
        border-color: currentColor;
    }
    
    .modal-close-btn:focus-visible,
    .modal-footer button:focus-visible {
        outline-width: 3px;
    }
}