/* Modal CSS Reset - Inspired by Josh Comeau's Reset */
.modal-overlay,
.modal-overlay *,
.modal-overlay *::before,
.modal-overlay *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
    border: 0;

    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;

    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;

    text-decoration: none;
    text-transform: none;
    list-style: none;

    overflow-wrap: break-word;
    hyphens: auto;
}

.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;
}

.modal-overlay img,
.modal-overlay picture,
.modal-overlay video,
.modal-overlay canvas,
.modal-overlay svg {
    display: block;
    max-width: 100%;
    height: auto;
}

.modal-overlay input,
.modal-overlay button,
.modal-overlay textarea,
.modal-overlay select {
    font: inherit;
    color: inherit;
}

@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;
    }
}

@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;
    z-index: 98;
    display: flex;
    align-items: flex-end;
    justify-content: center;
    width: 100vw;
    height: 100vh;
    height: 100dvh;

    /* Transparent by default - backdrop is handled by ::before */
    background-color: transparent;

    isolation: isolate;
    animation: modalFadeIn 0.2s ease-out;

    margin: 0;
    padding: 0;
    border: none;
    outline: none;

    /* Block pointer events on overlay, allow on modal content */
    pointer-events: none;

    /* Prevent any scrolling or bounce on overlay */
    overflow: hidden;
    overscroll-behavior: none;
    -webkit-overflow-scrolling: auto;
}

/* Mobile: Use visualViewport for Android address bar handling */
@media (max-width: 639px) {
    .modal-overlay {
        top: var(--vv-top, 0px) !important;
        bottom: 0 !important;
        height: auto !important;
        padding-bottom: max(var(--kb, 0px), env(safe-area-inset-bottom, 0px)) !important;
    }
}

/* Fixed white backdrop on mobile - always covers entire window */
.modal-overlay::before {
    content: '';
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    background-color: white;
    z-index: -1;
    transition: background-color 0.3s ease;
}

/* Desktop: Center modal with dark backdrop */
@media (min-width: 640px) {
    .modal-overlay {
        align-items: center;
        padding: 1rem;
    }

    .modal-overlay::before {
        background-color: rgba(0, 0, 0, 0.5);
        backdrop-filter: blur(0.25rem);
        -webkit-backdrop-filter: blur(0.25rem);
    }
}

@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(1.25rem) scale(0.95);
        opacity: 0;
    }
    to {
        transform: translateY(0) scale(1);
        opacity: 1;
    }
}

/* Modal Container */
.modal-container {
    position: relative;
    background-color: white;
    width: 100%;
    display: flex;
    flex-direction: column;

    /* Mobile: Full screen, no rounded corners */
    max-width: 100%;
    border-radius: 0;
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;

    /* Use CSS custom property for dynamic height with keyboard */
    height: var(--vvh, 100vh);
    max-height: var(--vvh, 100vh);

    animation: modalSlideUp 0.3s cubic-bezier(0.25, 0.46, 0.45, 0.94);

    border: none;
    outline: none;
    overflow: hidden;

    /* contain: size removed - conflicts with height: auto on desktop */
    contain: layout style paint;
    isolation: isolate;

    /* Re-enable pointer events on modal content */
    pointer-events: auto;
}

/* iOS-only: Safe area padding and fixed positioning for bounce prevention */
@supports (-webkit-touch-callout: none) {
    .modal-container {
        position: fixed;
        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);
    }
}

/* Desktop: Centered modal with constrained size */
@media (min-width: 640px) {
    .modal-container {
        border-radius: 1rem;
        max-width: 32rem; /* 512px */
        height: auto;
        max-height: calc(100vh - 2rem);
        max-height: calc(100dvh - 2rem);

        animation: modalSlideIn 0.25s cubic-bezier(0.25, 0.46, 0.45, 0.94);

        box-shadow: 0 1.25rem 3.75rem -0.625rem rgba(0, 0, 0, 0.2),
                    0 0.625rem 1.875rem -0.9375rem rgba(0, 0, 0, 0.1);
    }
}

@media (min-width: 1024px) {
    .modal-container {
        max-height: calc(100vh - 4rem);
        max-height: calc(100dvh - 4rem);
    }
}

/* Keyboard visible state */
.modal-container.keyboard-visible {
    /* No transform needed - height transitions are smooth */
}

/* 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;

    min-height: 4rem;
    max-height: 4rem;
    flex-shrink: 0;

    background-color: white;
    margin: 0;
    position: relative;
    z-index: 10;
}

.modal-title {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    color: #111827;

    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;

    text-wrap: balance;
}

.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;

    border-radius: 0.5rem;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 2.5rem;
    height: 2.5rem;
    flex-shrink: 0;

    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;

    /* Let tua-body-scroll-lock handle scroll behavior */
    scroll-behavior: auto !important;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;

    background-color: white;
    margin: 0;
    border: none;
    position: relative;
    font-size: 1rem;
    line-height: 1.625;

    /* Smooth scrolling experience */
    scrollbar-gutter: stable;
}

/* Custom scrollbar styling */
.modal-body::-webkit-scrollbar {
    width: 0.375rem;
    height: 0.375rem;
}

.modal-body::-webkit-scrollbar-track {
    background: #f9fafb;
    border-radius: 0.1875rem;
}

.modal-body::-webkit-scrollbar-thumb {
    background: #d1d5db;
    border-radius: 0.1875rem;
    transition: background 0.2s ease;
}

.modal-body::-webkit-scrollbar-thumb:hover {
    background: #9ca3af;
}

.modal-body {
    scrollbar-width: thin;
    scrollbar-color: #d1d5db #f9fafb;
}

/* Typography in modal body - all in rem */
.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;
}

.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;

    min-height: 4.5rem;
    max-height: 4.5rem;
    flex-shrink: 0;

    background-color: #fafafa;
    margin: 0;
    position: relative;
    z-index: 10;
}

/* For browsers that don't support gap property */
.modal-footer > *:not(:last-child) {
    margin-right: 0.75rem;
}

/* Modal buttons - all rem units */
.modal-footer button,
.modal-footer .btn {
    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: #374151;
    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 0.0625rem 0.125rem 0 rgba(0, 0, 0, 0.05);

    user-select: none;
}

.modal-footer button:hover,
.modal-footer .btn:hover {
    background-color: #f9fafb;
    border-color: #9ca3af;
    transform: translateY(-0.0625rem);
    box-shadow: 0 0.125rem 0.25rem 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 0.0625rem 0.125rem 0 rgba(0, 0, 0, 0.05);
}

.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;
}

.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 0.0625rem 0.125rem 0 rgba(0, 0, 0, 0.05);
}

/* 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;
    }
}
