/* Mobile-only overrides to keep desktop CSS clean */

/* Disable pinch zoom on mobile - only allow button-based zooming */
@media (max-width: 1024px) {
    #plot-svg .zoom-background,
    .zoom-background {
        touch-action: pan-y !important; /* Remove pinch-zoom on mobile */
    }
}

@media (max-width: 1024px) {
    .header h1 {
        font-size: 2rem;
    }

    .controls {
        flex-direction: row;
        flex-wrap: wrap;
        gap: 0.75rem;
        justify-content: center;
        align-items: center;
    }

    .zoom-controls {
        position: absolute;
        bottom: 1rem;
        left: 50%;
        right: auto;
        transform: translateX(-50%);
        width: min(92%, 340px);
        display: grid;
        grid-template-columns: repeat(3, minmax(60px, 1fr));
        grid-template-rows: repeat(2, 60px);
        grid-template-areas:
            "left right plus"
            "reset reset minus";
        gap: 0.5rem;
        pointer-events: auto;
    }

    .zoom-controls-floating {
        position: fixed !important;
        left: auto;
        right: 1rem;
        bottom: 1rem;
        transform: none;
        width: clamp(240px, 45vw, 340px);
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.25s ease;
        z-index: var(--z-zoom-controls-floating);
    }

    .zoom-controls-floating.is-visible {
        opacity: 1;
        pointer-events: auto;
    }

    #plot-container .zoom-controls .zoom-btn {
        width: 100%;
        height: 100%;
        min-width: 60px;
        min-height: 60px;
        font-size: 1.35rem;
        border-radius: 16px;
        background: var(--bg-secondary);
        border: 1px solid var(--border-color);
        color: var(--text-primary);
        box-shadow: 0 8px 18px rgba(0, 0, 0, 0.15);
        -webkit-appearance: none;
        appearance: none;
        margin: 0;
        padding: 0;
        touch-action: manipulation;
    }

    #plot-container .zoom-controls .zoom-btn {
        width: 100%;
        height: 100%;
        min-width: 60px;
        min-height: 60px;
        font-size: 1.35rem;
        border-radius: 16px;
        background: var(--bg-secondary);
        border: 1px solid var(--border-color);
        color: var(--text-primary);
        box-shadow: 0 8px 18px rgba(0, 0, 0, 0.15);
    }

    /* Compact tooltip on mobile */
    .tooltip.mobile-pinned {
        padding: 0.5rem;
    }
    /* Pinned mobile tooltip to avoid off-screen jumps */
    .tooltip.mobile-pinned {
        position: fixed;
        top: 12px;
        left: 50%;
        transform: translate(-50%, 0) !important;
        max-width: 100vw;
        pointer-events: auto;
        z-index: var(--z-tooltip-pinned); /* Above zoom controls */
    }
    .tooltip.mobile-pinned.visible {
        opacity: 1;
        transform: translate(-50%, 0) !important;
    }
    .tooltip-title {
        font-size: 0.95rem;
    }
    .tooltip-value,
    .tooltip-description,
    .tooltip-source {
        font-size: 0.8rem;
    }
    .tooltip-image {
        max-height: 140px;
    }
    
    /* Smaller tooltips in portrait mode */
    @media (orientation: portrait) {
        .tooltip.mobile-pinned {
            max-width: 125vw;
            padding: 0.4rem;
        }
        .tooltip-title {
            font-size: 0.9rem;
        }
        .tooltip-value,
        .tooltip-description,
        .tooltip-source {
            font-size: 0.75rem;
        }
        .tooltip-image {
            max-height: 100px;
        }

        /* More vertical space between header and buttons in portrait mode */
        #units-section .section-header,
        #items-section .section-header {
            margin-bottom: 1rem !important;
            padding-bottom: 0.75rem !important;
        }
        
        /* More space between header text and buttons when they wrap */
        #units-section .section-header-right,
        #items-section .section-header-right {
            margin-top: 0.75rem !important;
        }
    }
    
    /* Even smaller tooltips in landscape mode on mobile */
    @media (orientation: landscape) {
        .tooltip.mobile-pinned {
            max-width: 120vw;
            padding: 0.35rem;
        }
        .tooltip-title {
            font-size: 0.9rem;
        }
        .tooltip-value,
        .tooltip-description,
        .tooltip-source {
            font-size: 0.7rem;
        }
        
        /* Landscape: image and metadata on left, description on right */
        .tooltip.mobile-pinned .tooltip-content {
            display: grid;
            grid-template-columns: auto 1fr;
            grid-template-rows: auto auto auto 1fr;
            gap: 0.25rem 0.75rem;
            align-items: start;
        }
        
        /* Left column: image, then title, value, source stacked */
        .tooltip.mobile-pinned .tooltip-image {
            grid-column: 1;
            grid-row: 1;
            width: auto;
            max-width: 120px;
            max-height: 120px;
            min-width: 80px;
            object-fit: contain;
            margin-bottom: 0.25rem;
            margin-right: 0;
            display: block;
        }
        
        .tooltip.mobile-pinned .tooltip-title {
            grid-column: 1;
            grid-row: 2;
            margin-bottom: 0.2rem;
            margin-top: 0;
            max-width: 120px; /* Match image max-width so title wraps within image width */
            word-wrap: break-word;
            overflow-wrap: break-word;
            hyphens: auto;
        }
        
        .tooltip.mobile-pinned .tooltip-value {
            grid-column: 1;
            grid-row: 3;
            margin-bottom: 0.2rem;
            max-width: 120px; /* Match image max-width */
            word-wrap: break-word;
            overflow-wrap: break-word;
        }
        
        .tooltip.mobile-pinned .tooltip-source {
            grid-column: 1;
            grid-row: 4;
            margin-bottom: 0;
            margin-top: auto; /* Push source to bottom of its row */
            align-self: end; /* Align to bottom of the grid cell */
        }
        
        /* Description goes on the right, spans all rows */
        .tooltip.mobile-pinned .tooltip-description {
            grid-column: 2;
            grid-row: 1 / -1; /* Span from first to last row */
            margin-bottom: 0;
            align-self: start;
        }
        
        /* Reduce footer and unit description text size in landscape */
        .footer-attribution {
            font-size: 0.75rem;
        }
        .unit-description {
            font-size: 0.8rem;
        }
    }

    /* Slightly smaller axis labels to reduce overlap */
    .axis {
        font-size: 11px;
    }

    /* Let SVG grow; clip content to prevent axis labels from extending beyond container */
    .plot-container {
        height: auto;
        min-height: 520px;
        overflow: hidden; /* Changed from visible to hidden to clip axis labels */
    }

    /* Optional: keep controls accessible */
    /*
    .controls {
        position: sticky;
        top: 0;
        z-index: var(--z-sticky-header);
        background: var(--bg-primary);
        padding: 0.5rem 0.25rem;
        border-bottom: 1px solid var(--border-color);
    }
    */

    /* Smaller font for image upload buttons on mobile */
    .image-upload-controls * {
        font-size: 0.7rem !important;
    }
    .image-upload-controls .file-input-label.btn,
    .image-upload-controls label.btn,
    .image-upload-controls .btn-small,
    .image-upload-controls button.btn-small,
    .image-upload-controls button.btn.btn-small,
    .image-upload-controls button.btn.btn-small.btn-danger,
    .image-upload-controls button {
        font-size: 0.7rem !important;
        padding: 0.2rem 0.4rem !important;
    }
    /* Center text in Choose Image button on mobile */
    .image-upload-controls .file-input-label.btn,
    .image-upload-controls label.btn {
        text-align: center !important;
    }
}

@media (max-width: 480px) {
    .header {
        padding: 1.25rem 0;
        margin-bottom: 1.25rem;
    }

    .header h1 {
        font-size: 1.6rem;
    }

    .subtitle {
        font-size: 0.95rem;
        margin-bottom: 1rem;
    }

    .controls {
        width: 100%;
        flex-direction: row; /* ensure buttons don't stack */
        align-items: center;
        justify-content: center;
        gap: 0.5rem;
    }

    .control-group {
        width: 100%;
        justify-content: center;
    }

    #dimension-select,
    #unit-select {
        width: 100%;
        font-size: 1rem;
        padding: 0.65rem 0.9rem;
    }

    .notation-toggle {
        width: 75px; 
        height: 44px;
        font-size: 0.8rem;
        padding: 0 10px;
        -webkit-appearance: none;
        appearance: none;
        background-color: var(--bg-primary);
        border-radius: 8px;
        margin: 0;
    }
    
    .dark-mode-toggle,
    .music-toggle {
        width: 44px;
        height: 44px;
        font-size: 1.25rem;
        -webkit-appearance: none;
        appearance: none;
        background-color: var(--bg-primary); /* Force solid background */
        border-radius: 50%; /* Enforce roundness */
        margin: 0;
    }

    .plot-container {
        height: auto; /* allow SVG to dictate height */
        min-height: 10px;
        overflow: hidden; /* Changed from visible to hidden to clip axis labels */
    }

    .unit-selector-container {
        flex-direction: column;
        gap: 0.5rem;
    }

    .tooltip.mobile-pinned,

    /* Smaller axis font on very small screens */
    .axis {
        font-size: 10px;
    }

    /* Extra compact tooltip on very small screens */
    .tooltip-title {
        font-size: 0.95rem;
    }
    .tooltip-value,
    .tooltip-description,
    .tooltip-source {
        font-size: 0.8rem;
    }
    .tooltip-image {
        max-height: 120px;
    }

    .footer {
        padding: 1.25rem 0;
        margin-top: 2rem;
    }

    .footer-attribution {
        font-size: 0.85rem;
    }

    /* Smaller font for image upload buttons on mobile */
    .image-upload-controls * {
        font-size: 0.5rem !important;
    }
    .image-upload-controls .file-input-label.btn,
    .image-upload-controls label.btn,
    .image-upload-controls .btn-small,
    .image-upload-controls button.btn-small,
    .image-upload-controls button.btn.btn-small,
    .image-upload-controls button.btn.btn-small.btn-danger,
    .image-upload-controls button {
        font-size: 0.5rem !important;
        padding: 0.2rem 0.4rem !important;
    }
    /* Center text in Choose Image button on mobile */
    .image-upload-controls .file-input-label.btn,
    .image-upload-controls label.btn {
        text-align: center !important;
    }
}