/* Responsive Design for Math Worksheet Generator */
/* Breakpoints: Mobile: 768px, Tablet: 1024px, Desktop: 1200px+ */

/* Tablet Styles (768px - 1024px) */
@media screen and (max-width: 1024px) {
    .app-main {
        grid-template-columns: 1fr;
        gap: 1.5rem;
        padding: 1.5rem;
    }
    
    .preview-container {
        height: 500px;
        min-height: 400px;
    }
    
    .app-header {
        padding: 1rem 1.5rem;
    }
    
    .app-header h1 {
        font-size: 1.3rem;
    }
    
    /* Ensure buttons are touch-friendly on tablets */
    .primary-button, .secondary-button {
        min-height: 44px;
        padding: 0.875rem 1.5rem;
    }
}

/* Mobile Styles (up to 768px) */
@media screen and (max-width: 768px) {
    /* Header optimization for mobile */
    .app-header {
        padding: 1rem;
        flex-direction: column;
        gap: 1rem;
        text-align: center;
    }
    
    .app-header h1 {
        font-size: 1.2rem;
        line-height: 1.4;
    }
    
    .header-actions {
        width: 100%;
        justify-content: center;
    }
    
    /* Main layout optimization */
    .app-main {
        padding: 1rem;
        gap: 1rem;
        display: flex;
        flex-direction: column;
    }
    
    .config-panel, .preview-panel {
        padding: 1rem;
        width: 100%;
    }
    
    .config-panel h2, .preview-panel h2 {
        font-size: 1.1rem;
        margin-bottom: 1rem;
    }
    
    /* Preview container optimization */
    .preview-container {
        height: 400px;
        min-height: 300px;
        touch-action: pan-y pinch-zoom;
    }
    
    .preview-canvas {
        touch-action: pan-y pinch-zoom;
    }
    
    /* Touch-friendly buttons - minimum 48x48px touch target */
    .primary-button, .secondary-button {
        padding: 1rem 1.5rem;
        font-size: 1.1rem;
        min-height: 48px;
        min-width: 48px;
        width: 100%;
        touch-action: manipulation;
    }
    
    /* Touch-friendly form controls */
    .config-select {
        padding: 1rem;
        font-size: 1.1rem;
        min-height: 48px;
        touch-action: manipulation;
    }
    
    .radio-label {
        padding: 0.75rem;
        min-height: 48px;
        display: flex;
        align-items: center;
        cursor: pointer;
        touch-action: manipulation;
    }
    
    .radio-label input[type="radio"] {
        transform: scale(1.3);
        margin-right: 0.75rem;
        min-width: 24px;
        min-height: 24px;
    }
    
    .file-input {
        padding: 1rem;
        font-size: 1rem;
        min-height: 48px;
        touch-action: manipulation;
    }
    
    /* Improved spacing for mobile */
    .config-group {
        margin-bottom: 1.25rem;
    }
    
    .config-group label {
        font-size: 1rem;
        margin-bottom: 0.75rem;
    }
    
    .action-buttons {
        margin-top: 1.5rem;
        gap: 0.75rem;
        display: flex;
        flex-direction: column;
    }
    
    /* Message overlays for mobile */
    .error-content, .success-content {
        padding: 1.5rem;
        margin: 1rem;
        width: calc(100% - 2rem);
        max-width: 90vw;
    }
    
    .error-content h3, .success-content h3 {
        font-size: 1.2rem;
    }
    
    .error-content p, .success-content p {
        font-size: 1rem;
        line-height: 1.5;
    }
    
    .loading-overlay {
        font-size: 1.1rem;
    }
    
    .loading-spinner {
        width: 50px;
        height: 50px;
        border-width: 5px;
    }
    
    /* Install button optimization */
    .install-button {
        min-height: 44px;
        padding: 0.75rem 1rem;
        font-size: 1rem;
    }
}

/* Small Mobile Styles (up to 480px) */
@media screen and (max-width: 480px) {
    .app-header {
        padding: 0.75rem;
    }
    
    .app-header h1 {
        font-size: 1.1rem;
        line-height: 1.3;
    }
    
    .app-main {
        padding: 0.75rem;
        gap: 0.75rem;
    }
    
    .config-panel, .preview-panel {
        padding: 0.75rem;
        border-radius: 6px;
    }
    
    .config-panel h2, .preview-panel h2 {
        font-size: 1rem;
    }
    
    .preview-container {
        height: 300px;
        min-height: 250px;
    }
    
    .install-button {
        padding: 0.5rem 0.75rem;
        font-size: 0.9rem;
        min-height: 40px;
    }
    
    /* Stack radio buttons vertically on very small screens */
    .radio-group {
        gap: 0.5rem;
    }
    
    .radio-label {
        padding: 0.625rem;
        font-size: 0.95rem;
        min-height: 44px;
    }
    
    /* Adjust button sizes for small screens while maintaining touch targets */
    .primary-button, .secondary-button {
        padding: 0.875rem 1.25rem;
        font-size: 1rem;
        min-height: 48px;
    }
    
    .config-select {
        font-size: 1rem;
        padding: 0.875rem;
    }
    
    .error-content, .success-content {
        padding: 1.25rem;
        font-size: 0.95rem;
        margin: 0.75rem;
    }
    
    .error-close, .success-close {
        min-height: 44px;
        padding: 0.75rem 1.25rem;
    }
}

/* Landscape Mobile Styles */
@media screen and (max-width: 768px) and (orientation: landscape) {
    .app-main {
        grid-template-columns: 1fr 1.5fr;
        gap: 1rem;
        display: grid;
    }
    
    .preview-container {
        height: 350px;
        min-height: 300px;
    }
    
    .app-header {
        padding: 0.75rem 1rem;
        flex-direction: row;
        justify-content: space-between;
    }
    
    .app-header h1 {
        text-align: left;
    }
    
    .config-panel {
        max-height: 400px;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
    }
    
    /* Optimize button layout for landscape */
    .action-buttons {
        flex-direction: row;
        gap: 0.5rem;
    }
    
    .primary-button, .secondary-button {
        flex: 1;
        min-width: 120px;
    }
}

/* Extra small landscape devices */
@media screen and (max-width: 480px) and (orientation: landscape) {
    .app-header {
        padding: 0.5rem 1rem;
    }
    
    .app-header h1 {
        font-size: 1rem;
    }
    
    .config-panel {
        max-height: 300px;
    }
    
    .preview-container {
        height: 280px;
    }
}

/* High DPI Displays */
@media screen and (-webkit-min-device-pixel-ratio: 2),
       screen and (min-resolution: 192dpi) {
    .preview-canvas {
        image-rendering: -webkit-optimize-contrast;
        image-rendering: crisp-edges;
    }
    
    /* Ensure sharp text rendering on high DPI displays */
    body {
        -webkit-font-smoothing: antialiased;
        -moz-osx-font-smoothing: grayscale;
    }
}

/* Touch device optimizations */
@media (hover: none) and (pointer: coarse) {
    /* Increase touch targets for touch-only devices */
    .primary-button, .secondary-button,
    .config-select, .radio-label, .file-input {
        min-height: 48px;
    }
    
    /* Remove hover effects on touch devices */
    .primary-button:hover, .secondary-button:hover,
    .radio-label:hover, .file-input:hover {
        transform: none;
    }
    
    /* Add active states for better touch feedback */
    .primary-button:active {
        transform: scale(0.98);
        opacity: 0.9;
    }
    
    .secondary-button:active {
        transform: scale(0.98);
        opacity: 0.9;
    }
    
    .radio-label:active {
        background-color: #e3f2fd;
    }
}

/* Dark Mode Support */
@media (prefers-color-scheme: dark) {
    body {
        background-color: #121212;
        color: #e0e0e0;
    }
    
    .config-panel, .preview-panel {
        background: #1e1e1e;
        border: 1px solid #333;
    }
    
    .config-select {
        background: #2a2a2a;
        border-color: #444;
        color: #e0e0e0;
    }
    
    .config-select:focus {
        border-color: #2196f3;
    }
    
    .radio-label:hover {
        background-color: #2a2a2a;
    }
    
    .radio-label:active {
        background-color: #333;
    }
    
    .file-input {
        background: #2a2a2a;
        border-color: #444;
        color: #e0e0e0;
    }
    
    .preview-container {
        border-color: #444;
        background: #1e1e1e;
    }
    
    .preview-placeholder {
        color: #666;
    }
    
    .error-content, .success-content {
        background: #1e1e1e;
        color: #e0e0e0;
        border: 1px solid #333;
    }
    
    .loading-overlay {
        background: rgba(0, 0, 0, 0.8);
    }
}

/* Safe area support for notched devices (iPhone X+, etc.) */
@supports (padding: max(0px)) {
    .app-header {
        padding-left: max(1rem, env(safe-area-inset-left));
        padding-right: max(1rem, env(safe-area-inset-right));
        padding-top: max(1rem, env(safe-area-inset-top));
    }
    
    .app-main {
        padding-left: max(1rem, env(safe-area-inset-left));
        padding-right: max(1rem, env(safe-area-inset-right));
        padding-bottom: max(1rem, env(safe-area-inset-bottom));
    }
    
    @media screen and (max-width: 768px) {
        .app-header {
            padding-left: max(1rem, env(safe-area-inset-left));
            padding-right: max(1rem, env(safe-area-inset-right));
        }
        
        .app-main {
            padding-left: max(1rem, env(safe-area-inset-left));
            padding-right: max(1rem, env(safe-area-inset-right));
            padding-bottom: max(1rem, env(safe-area-inset-bottom));
        }
    }
}

/* Reduced Motion Support */
@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    
    .loading-spinner {
        animation: none;
        border: 4px solid #ccc;
        border-top: 4px solid #2196f3;
    }
}

/* Print Styles for Mobile */
@media print {
    .app-main {
        grid-template-columns: 1fr !important;
        padding: 0 !important;
    }
    
    .preview-container {
        height: auto !important;
        border: none !important;
    }
    
    .preview-canvas {
        width: 100% !important;
        height: auto !important;
    }
}

/* Focus Visible for Better Accessibility */
@supports selector(:focus-visible) {
    button:focus {
        outline: none;
    }
    
    button:focus-visible {
        outline: 2px solid #2196f3;
        outline-offset: 2px;
    }
}

/* Container Queries Support (Future Enhancement) */
@supports (container-type: inline-size) {
    .config-panel {
        container-type: inline-size;
    }
    
    @container (max-width: 300px) {
        .action-buttons {
            flex-direction: column;
        }
        
        .radio-group {
            flex-direction: column;
        }
    }
}

/* Orientation-specific body classes */
body.portrait .preview-container {
    /* Portrait-specific adjustments */
}

body.landscape .preview-container {
    /* Landscape-specific adjustments */
}

/* Mobile-specific zoom container */
.preview-container.zoomed {
    overflow: auto;
    -webkit-overflow-scrolling: touch;
}

.preview-container.zoomed .preview-canvas {
    max-width: none;
    max-height: none;
}

/* Fullscreen mode support */
.preview-container:fullscreen {
    background: #000;
    display: flex;
    align-items: center;
    justify-content: center;
}

.preview-container:fullscreen .preview-canvas {
    max-width: 100vw;
    max-height: 100vh;
}

/* iOS Safari specific fixes */
@supports (-webkit-touch-callout: none) {
    /* iOS-specific styles */
    .preview-container {
        -webkit-overflow-scrolling: touch;
    }
    
    .primary-button, .secondary-button {
        -webkit-appearance: none;
        appearance: none;
    }
    
    input[type="file"] {
        -webkit-appearance: none;
        appearance: none;
    }
}

/* Android Chrome specific fixes */
@media screen and (max-width: 768px) {
    /* Prevent zoom on input focus */
    input, select, textarea {
        font-size: 16px !important;
    }
}