/* OS 2026 Window System - Custom Design (Non-Apple) */

/* The Dialog/Modal Container */
.mm-os-modal {
    position: fixed;
    inset: 0;
    z-index: 10000;
    display: none;
    align-items: center;
    justify-content: center;
    background: transparent;
    pointer-events: none;
    padding: 20px;
    border: none;
    max-width: 100vw;
    max-height: 100dvh;
    width: 100vw;
    height: 100dvh;
    --mm-window-width: 90vw;
    --mm-window-max-width: 1400px;
    --mm-window-height: 85vh;
    --mm-window-max-height: 90vh;
    --mm-window-min-width: 320px;
    --mm-window-min-height: 300px;
    --mm-window-radius: 16px;
}

/* Modal Sizing System */
.mm-os-modal[data-mm-size="compact"] {
    --mm-window-width: min(92vw, 720px);
    --mm-window-max-width: 720px;
    --mm-window-height: auto;
    --mm-window-max-height: 85vh;
    --mm-window-min-height: 240px;
}

.mm-os-modal[data-mm-size="medium"] {
    --mm-window-width: min(92vw, 980px);
    --mm-window-max-width: 980px;
    --mm-window-height: min(85vh, 900px);
    --mm-window-max-height: min(85vh, 900px);
    --mm-window-min-height: 320px;
}

.mm-os-modal[data-mm-size="wide"] {
    --mm-window-width: min(94vw, 1280px);
    --mm-window-max-width: 1280px;
    --mm-window-height: min(85vh, 980px);
    --mm-window-max-height: min(85vh, 980px);
    --mm-window-min-height: 360px;
}

.mm-os-modal[data-mm-size="full"] {
    --mm-window-width: 100vw;
    --mm-window-max-width: 100vw;
    --mm-window-height: 100vh;
    --mm-window-max-height: 100vh;
    --mm-window-min-width: 0;
    --mm-window-min-height: 0;
    --mm-window-radius: 0px;
    padding: 0;
}

/* Show modal when open - use flex for centering */
.mm-os-modal[open] {
    display: flex;
    pointer-events: auto;
    /* Prevent background scroll bleeding through */
    overscroll-behavior: contain;
}

/* Backdrop - covers full screen with subtle scale for depth */
.mm-os-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.85);
    /* Blur removed for performance - prevents scroll jank */
    opacity: 0;
    transition:
        opacity 0.25s ease-out;
    z-index: 1;
    cursor: pointer;
    /* Prevent touch events from bleeding through to body */
    touch-action: none;
}

.mm-os-modal[open] .mm-os-backdrop {
    opacity: 1;
}

/* The Window Itself */
.mm-os-window {
    position: relative;
    margin: auto;
    width: var(--mm-window-width);
    height: var(--mm-window-height);
    max-width: var(--mm-window-max-width);
    max-height: var(--mm-window-max-height);
    min-width: var(--mm-window-min-width);
    min-height: var(--mm-window-min-height);
    background: rgba(18, 18, 18, 0.98);
    /* Blur removed for performance - prevents scroll jank */
    border-radius: var(--mm-window-radius);
    border: 1px solid rgba(255, 255, 255, 0.1);
    box-shadow:
        0 18px 40px rgba(0, 0, 0, 0.45),
        0 0 60px rgba(0, 0, 0, 0.16);
    opacity: 0;
    transform: scale(0.95) translateY(20px);
    transition:
        opacity 0.4s cubic-bezier(0.16, 1, 0.3, 1),
        transform 0.4s cubic-bezier(0.16, 1, 0.3, 1),
        width 0.3s ease,
        height 0.3s ease;
    overflow: hidden;
    display: flex;
    flex-direction: column;
    z-index: 10;
    /* GPU Performance Hints */
    will-change: transform, opacity;
    transform: translateZ(0) scale(0.95) translateY(20px);
    isolation: isolate;
    -webkit-backface-visibility: hidden;
    backface-visibility: hidden;
}

/* Bounce-in keyframe for premium feel */
@keyframes mm-window-bounce-in {
    0% {
        opacity: 0;
        transform: translateZ(0) scale(0.92) translateY(25px);
    }

    60% {
        opacity: 1;
        transform: translateZ(0) scale(1.02) translateY(-5px);
    }

    100% {
        opacity: 1;
        transform: translateZ(0) scale(1) translateY(0);
    }
}

.mm-os-modal[open] .mm-os-window {
    animation: mm-window-bounce-in 0.5s cubic-bezier(0.34, 1.56, 0.64, 1) forwards;
}

/* Disable blur inside modals for stable rendering */
.mm-os-modal .bg-glass,
.mm-os-modal .glass-panel,
.mm-os-modal .glass-panel-heavy,
.mm-os-modal [class*="backdrop-blur"] {
    -webkit-backdrop-filter: none !important;
    backdrop-filter: none !important;
}

/* Settings Modal (Footer) */
.mm-settings-modal {
    align-items: flex-end;
    padding: 24px 24px calc(env(safe-area-inset-bottom) + 24px);
    --mm-window-radius: 24px;
}

.mm-settings-modal .mm-os-window {
    margin-left: auto;
}

/* Legal Modals (Reading optimized) */
.mm-legal-modal {
    --mm-window-radius: 24px;
    --mm-window-height: min(85vh, 920px);
    --mm-window-max-height: min(85vh, 920px);
}

.mm-legal-modal .mm-os-content {
    padding: clamp(20px, 3vw, 56px);
}

/* Support Modal (Large Workspace) */
.mm-support-modal {
    --mm-window-width: min(95vw, 1600px);
    --mm-window-max-width: 1600px;
    --mm-window-height: min(92vh, 980px);
    --mm-window-max-height: min(92vh, 980px);
}

.mm-support-modal .mm-os-content {
    padding: 0;
    display: flex;
    flex-direction: column;
}

.mm-support-modal #mm-support-modal-content {
    flex: 1;
    display: flex;
    min-height: 0;
}

.mm-support-modal .mm-support-iframe {
    flex: 1;
    width: 100%;
    height: 100%;
    border: none;
    background: transparent;
}

/* Maximized State */
.mm-os-window.mm-maximized {
    width: 100vw !important;
    height: 100vh !important;
    max-width: none !important;
    border-radius: 0 !important;
}

/* Minimized State (for tab functionality) */
.mm-os-window.mm-minimized {
    opacity: 0;
    transform: scale(0.8) translateY(50px);
    pointer-events: none;
}

.mm-os-modal.mm-window-minimized {
    pointer-events: none;
}

.mm-os-modal.mm-window-minimized .mm-os-backdrop {
    opacity: 0;
}

/* Window Header / Titlebar */
.mm-os-header {
    height: 48px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 16px;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
    user-select: none;
    cursor: grab;
    background: rgba(0, 0, 0, 0.2);
}

.mm-os-header:active {
    cursor: grabbing;
}

/* ===== Unified Modal Close Button (THE ARCHITECT Style) ===== */
/* Use this instead of Traffic Lights for dialog modals */
.mm-modal-close {
    width: 44px;
    height: 44px;
    margin-left: auto;
    /* Push to right */
    border-radius: 12px;
    background: transparent;
    border: 1px solid transparent;
    color: var(--color-text-meta, rgba(255, 255, 255, 0.4));
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.2, 0.8, 0.2, 1);
    flex-shrink: 0;
}

.mm-modal-close:hover {
    background: rgba(var(--color-signal-rgb), 0.1);
    border-color: rgba(var(--color-signal-rgb), 0.3);
    color: var(--color-signal);
}

.mm-modal-close:focus-visible {
    outline: none;
    box-shadow: 0 0 0 2px rgba(var(--color-signal-rgb), 0.5);
    background: rgba(var(--color-signal-rgb), 0.1);
    color: var(--color-signal);
}

.mm-modal-close:active {
    transform: scale(0.95);
    background: rgba(var(--color-signal-rgb), 0.2);
}

/* Light mode */
.light .mm-modal-close {
    color: var(--color-text-meta, rgba(0, 0, 0, 0.4));
}

.light .mm-modal-close:hover {
    background: rgba(var(--color-signal-rgb), 0.08);
}

/* ===== Header with Icon (New Unified Style) ===== */
.mm-os-header-icon {
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: var(--color-signal);
    font-size: 1rem;
    flex-shrink: 0;
}

/* Title - Fluid scaling, never truncated */
.mm-os-title {
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
    font-size: clamp(10px, 2.5vw, 14px);
    font-weight: 600;
    color: rgba(255, 255, 255, 0.5);
    letter-spacing: 0.02em;
    pointer-events: none;
    font-family: var(--font-body), sans-serif;
    /* Prevent text cutoff */
    max-width: 60%;
    text-align: center;
    word-break: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
    -webkit-hyphens: auto;
    line-height: 1.3;
}

/* Content Area - Smooth Scrolling + Fluid Text */
.mm-os-content {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    position: relative;
    background: rgba(0, 0, 0, 0.15);
    scrollbar-gutter: stable;
    scroll-behavior: smooth;
    /* Prevent scroll chaining to body - critical for mobile */
    overscroll-behavior: contain;
    overscroll-behavior-y: contain;
    -webkit-overflow-scrolling: touch;
    /* Fluid text - never truncate */
    word-break: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
    -webkit-hyphens: auto;
}

/* Content headings - fluid scaling */
.mm-os-content h1,
.mm-os-content h2,
.mm-os-content h3 {
    word-break: break-word;
    overflow-wrap: break-word;
    hyphens: auto;
    -webkit-hyphens: auto;
}

.mm-os-content h1 {
    font-size: clamp(1.5rem, 5vw, 2.5rem);
}

.mm-os-content h2 {
    font-size: clamp(1.25rem, 4vw, 2rem);
}

.mm-os-content h3 {
    font-size: clamp(1rem, 3vw, 1.5rem);
}

/* ===== Always Visible Scrollbars (Desktop with Mouse) ===== */
@media (pointer: fine) {
    .mm-os-content {
        scrollbar-width: auto;
    }

    .mm-os-content::-webkit-scrollbar {
        width: 12px;
        display: block;
    }

    .mm-os-content::-webkit-scrollbar-track {
        background: rgba(255, 255, 255, 0.03);
        border-radius: 6px;
        margin: 4px;
    }

    .mm-os-content::-webkit-scrollbar-thumb {
        background: rgba(var(--color-signal-rgb), 0.25);
        border-radius: 6px;
        border: 3px solid transparent;
        background-clip: padding-box;
        min-height: 40px;
    }

    .mm-os-content::-webkit-scrollbar-thumb:hover {
        background: rgba(var(--color-signal-rgb), 0.4);
        border: 3px solid transparent;
        background-clip: padding-box;
    }

    .mm-os-content::-webkit-scrollbar-thumb:active {
        background: rgba(var(--color-signal-rgb), 0.55);
        border: 3px solid transparent;
        background-clip: padding-box;
    }
}

/* ===== Mobile Adjustments ===== */
@media (max-width: 768px) {
    .mm-os-modal {
        padding: 0;
        /* Lock modal position on mobile */
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        /* Prevent any scroll bleeding */
        overscroll-behavior: none;
        touch-action: pan-y pinch-zoom;
        /* Ensure no flex centering on mobile */
        align-items: stretch !important;
        justify-content: stretch !important;
    }

    /* Mobile backdrop: No blur for smooth scrolling performance */

    .mm-os-window {
        /* Use fixed positioning to prevent address bar issues */
        position: fixed !important;
        top: 0 !important;
        left: 0 !important;
        right: 0 !important;
        bottom: 0 !important;
        width: 100vw !important;
        height: 100dvh !important;
        height: 100vh !important; /* Fallback */
        /* True fullscreen */
        max-height: 100dvh !important;
        max-height: 100vh !important; /* Fallback */
        max-width: none !important;
        min-width: 0 !important;
        min-height: 0 !important;
        margin: 0 !important;
        border-radius: 0 !important;
        /* No radius for fullscreen */
        border: none !important;
        resize: none;
        display: flex !important;
        flex-direction: column !important;
        /* Remove any transforms that might cause positioning issues */
        transform: none !important;
    }

    /* Ensure open modal window is visible and positioned correctly */
    .mm-os-modal[open] .mm-os-window {
        opacity: 1 !important;
        transform: none !important;
    }

    /* Mobile fade-in - no transform to avoid positioning issues */
    @keyframes mm-window-fade-in {
        0% {
            opacity: 0;
        }
        100% {
            opacity: 1;
        }
    }

    .mm-os-modal[open] .mm-os-window {
        opacity: 1;
        /* Simple fade animation - no transform to prevent position jumps */
        animation: mm-window-fade-in 0.25s ease-out forwards;
    }

    /* Ensure content is reachable */
    .mm-os-modal {
        align-items: stretch;
        justify-content: stretch;
        padding: 0;
    }

    .mm-settings-modal {
        align-items: stretch;
        padding: 0;
        /* Override compact size variables for mobile fullscreen */
        --mm-window-width: 100vw;
        --mm-window-max-width: none;
        --mm-window-height: 100dvh;
        --mm-window-max-height: 100dvh;
    }

    .mm-os-header {
        height: auto;
        min-height: 56px;
        padding: calc(env(safe-area-inset-top, 0px) + 8px) 12px 8px;
        display: flex;
        align-items: center;
    }


    .mm-os-title {
        font-size: 11px;
    }

    /* Touch-friendly scrollbar */
    .mm-os-content::-webkit-scrollbar {
        width: 4px;
    }
}

@media (max-width: 768px) {
    .mm-os-content {
        padding-bottom: env(safe-area-inset-bottom, 0px);
    }

    /* Ensure all scrollable areas inside modals prevent body scroll */
    .mm-os-modal [class*="overflow-y-auto"],
    .mm-os-modal [class*="overflow-auto"] {
        overscroll-behavior: contain;
        overscroll-behavior-y: contain;
    }

    /* Fix for iOS Safari address bar issues */
    .mm-os-modal .mm-os-window {
        min-height: -webkit-fill-available;
    }
}

/* Mobile modal fine-tuning */
@media (max-width: 768px) {
    .mm-settings-modal .mm-os-window {
        /* Override CSS variables to prevent data-mm-size from constraining width */
        --mm-window-width: 100vw;
        --mm-window-max-width: none;
        --mm-window-height: 100dvh;
        --mm-window-max-height: 100dvh;
        /* Direct property overrides - use !important to override var() values */
        width: 100vw !important;
        height: 100dvh !important;
        max-width: none !important;
        max-height: 100dvh !important;
        min-width: 0 !important;
        margin: 0 !important;
        border-radius: 0 !important;
        border: none !important;
    }

    .mm-settings-modal .mm-os-content {
        padding: 16px;
    }

    .mm-legal-modal .mm-os-content {
        padding: 18px 16px calc(env(safe-area-inset-bottom) + 24px);
    }

    #mm-blog-scroll-container,
    #mm-wizard-content,
    #mm-shop-content {
        padding-bottom: calc(env(safe-area-inset-bottom) + 16px);
    }

    .mm-wizard-footer {
        padding-bottom: calc(env(safe-area-inset-bottom) + 16px);
    }

    /* Blog modal */
    #mm-blog-modal #mm-blog-filter-bar {
        padding: 12px;
    }


    #mm-blog-modal #mm-blog-categories {
        flex-wrap: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
    }

    #mm-blog-modal #mm-blog-categories::-webkit-scrollbar {
        display: none;
    }

    #mm-blog-modal .mm-cat-pill {
        padding: 6px 10px;
        font-size: 10px;
        white-space: nowrap;
    }

    #mm-blog-modal #mm-blog-list-view {
        padding: 16px 12px;
    }

    #mm-blog-modal #mm-blog-modal-grid {
        gap: 12px;
    }

    #mm-blog-modal #mm-blog-single-view article {
        padding: 20px 12px;
    }

    #mm-blog-modal #mm-blog-related {
        padding: 24px 12px;
    }

    #mm-blog-modal #mm-blog-share-bar {
        padding: 12px;
    }

    #mm-blog-modal #mm-blog-share-bar button {
        padding: 8px 10px;
        font-size: 12px;
    }

    #mm-blog-modal #mm-blog-scroll-top {
        width: 36px;
        height: 36px;
    }

    /* Shop modal */
    #mm-shop-modal .mm-os-header {
        padding: calc(env(safe-area-inset-top, 0px) + 6px) 10px 6px;
    }

    #mm-shop-modal .mm-modal-close {
        width: 40px;
        height: 40px;
    }

    #mm-shop-loading {
        backdrop-filter: none;
    }

    /* Support modal */
    #mm-support-app-modal .mm-os-header {
        padding: calc(env(safe-area-inset-top, 0px) + 6px) 12px 6px;
    }

    #mm-support-app-modal .mm-os-content {
        flex: 1;
        display: flex;
        flex-direction: column;
        min-height: 0;
        padding: 0;
    }

    #mm-support-app-modal #mm-support-modal-content {
        flex: 1;
        display: flex;
        min-height: 0;
    }

    #mm-support-modal-content iframe,
    .mm-support-modal .mm-support-iframe {
        width: 100%;
        height: 100%;
        flex: 1;
        border: 0;
        display: block;
    }

    /* Wizard modal */
    .mm-wizard-modal aside {
        padding: 12px;
    }

    .mm-wizard-modal #mm-wizard-content,
    .mm-wizard-modal .mm-os-content {
        padding: 16px 12px;
        /* Prevent scroll chaining to body */
        overscroll-behavior: contain;
        /* Fill available space on mobile */
        flex: 1;
        max-height: none !important;
    }

    .mm-wizard-modal .mm-wizard-actions {
        margin-top: 20px;
        gap: 10px;
    }

    .mm-wizard-stepper {
        padding: 12px;
        gap: 6px;
        /* Mobile: show as grid for better touch targets */
        display: grid;
        grid-template-columns: repeat(4, 1fr);
    }

    .mm-wizard-step {
        padding: 6px 10px;
        flex-direction: column;
        text-align: center;
        gap: 4px;
    }

    /* Show step labels on mobile */
    .mm-wizard-step-label {
        display: block !important;
        font-size: 9px;
        letter-spacing: 0.1em;
    }

    /* Hide step connectors on mobile */
    .mm-wizard-step-connector {
        display: none;
    }

    .mm-wizard-modal #mm-picked-total {
        font-size: 1.25rem;
    }

    .mm-wizard-modal aside .mt-auto {
        margin-top: 12px;
        padding-top: 12px;
    }

    /* Legal modals */
    .mm-legal-modal .mm-os-content {
        font-size: 14px;
        line-height: 1.7;
    }
}

/* ===== Close Animation ===== */
/* Desktop Default */
.mm-os-modal:not([open]) .mm-os-window {
    opacity: 0;
    transform: scale(0.95) translateY(20px);
    transition:
        opacity 0.25s cubic-bezier(0.4, 0, 1, 1),
        transform 0.25s cubic-bezier(0.4, 0, 1, 1);
}

/* Mobile Fade Out on Close */
@media (max-width: 768px) {
    .mm-os-modal:not([open]) .mm-os-window {
        opacity: 0;
        transform: none;
        transition: opacity 0.2s ease-out;
    }
}

.mm-os-modal:not([open]) .mm-os-backdrop {
    opacity: 0;
    transition: opacity 0.3s ease-out;
}

/* ===== Window Tab (Minimized in OS Bar) ===== */
.mm-window-tab {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    padding: 6px 12px;
    background: rgba(var(--color-signal-rgb), 0.1);
    border: 1px solid rgba(var(--color-signal-rgb), 0.2);
    border-radius: 8px;
    color: var(--color-text-dim);
    font-size: 11px;
    font-weight: 500;
    cursor: pointer;
    transition: all 0.2s ease;
    max-width: 150px;
    overflow: hidden;
}

.mm-window-tab:hover {
    background: rgba(var(--color-signal-rgb), 0.2);
    border-color: rgba(var(--color-signal-rgb), 0.4);
    color: var(--color-text);
}

.mm-window-tab .mm-tab-title {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.mm-window-tab .mm-tab-close {
    width: 14px;
    height: 14px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 3px;
    opacity: 0.5;
    transition: all 0.15s ease;
}

.mm-window-tab:hover .mm-tab-close {
    opacity: 1;
}

.mm-window-tab .mm-tab-close:hover {
    background: rgba(239, 68, 68, 0.3);
    color: rgb(239, 68, 68);
}

/* ===== Light Theme Support ===== */
.light .mm-os-window {
    background: rgba(255, 255, 255, 0.92);
    border-color: rgba(0, 0, 0, 0.1);
    box-shadow:
        0 25px 60px rgba(0, 0, 0, 0.15),
        0 0 100px rgba(0, 0, 0, 0.05);
}

.light .mm-os-backdrop {
    background: rgba(255, 255, 255, 0.5);
}

.light .mm-os-header {
    border-bottom: 1px solid rgba(0, 0, 0, 0.08);
    background: rgba(255, 255, 255, 0.3);
}

.light .mm-os-title {
    color: rgba(0, 0, 0, 0.5);
}

.light .mm-os-content {
    background: rgba(255, 255, 255, 0.3);
}

.light .mm-os-content::-webkit-scrollbar-track {
    background: rgba(0, 0, 0, 0.03);
}

.light .mm-os-content::-webkit-scrollbar-thumb {
    background: rgba(var(--color-signal-rgb), 0.3);
}

.light .mm-os-content::-webkit-scrollbar-thumb:hover {
    background: rgba(var(--color-signal-rgb), 0.45);
}

.light .mm-window-tab {
    background: rgba(var(--color-signal-rgb), 0.08);
    border-color: rgba(var(--color-signal-rgb), 0.15);
}

.light .mm-window-tab:hover {
    background: rgba(var(--color-signal-rgb), 0.15);
}

/* ===== Reduced Motion Support ===== */
@media (prefers-reduced-motion: reduce) {

    .mm-os-modal .mm-os-window,
    .mm-os-modal .mm-os-backdrop,
    .mm-os-modal:not([open]) .mm-os-window,
    .mm-os-modal:not([open]) .mm-os-backdrop,
    .mm-modal-close,
    .mm-window-tab {
        transition-duration: 0.01ms !important;
    }
}

.mm-reduce-motion .mm-os-modal .mm-os-window,
.mm-reduce-motion .mm-os-modal .mm-os-backdrop,
.mm-reduce-motion .mm-modal-close {
    transition-duration: 0.01ms !important;
}