/* ========================================================================== */
/* RESPONSIVE DESIGN - Optimized for all screen sizes                        */
/* Version: 3.0.0 - Rebuilt for better density and utilization              */
/* ========================================================================== */

/* ========================================================================== */
/* ULTRA-WIDE SCREENS (1920px and up) - Maximum utilization                  */
/* ========================================================================== */

@media (min-width: 1920px) {
    .container {
        max-width: 1800px;
    }
    
    .metrics-grid {
        grid-template-columns: repeat(6, 1fr); /* More columns on ultra-wide */
    }
    
    .data-table {
        font-size: 0.9rem;
    }
    
    .tab-panel {
        padding: 1.5rem;
    }
    
    .form-row {
        grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    }
}

/* ========================================================================== */
/* LARGE SCREENS (1200px to 1919px) - Desktop standard                       */
/* ========================================================================== */

@media (min-width: 1200px) and (max-width: 1919px) {
    .container {
        max-width: 1600px;
    }
    
    .metrics-grid {
        grid-template-columns: repeat(5, 1fr);
    }
}

/* ========================================================================== */
/* MEDIUM SCREENS (992px to 1199px) - Small desktop / large tablet           */
/* ========================================================================== */

@media (max-width: 1199px) {
    .container {
        max-width: 960px;
        padding: 0 1rem;
    }
    
    .metrics-grid {
        grid-template-columns: repeat(4, 1fr);
    }
    
    .targets-sidebar {
        width: 300px;
    }
    
    .targets-sidebar.open + .targets-floating-toggle {
        right: 315px;
    }
}

/* ========================================================================== */
/* SMALL SCREENS (768px to 991px) - Tablet                                   */
/* ========================================================================== */

@media (max-width: 991px) {
    .container {
        max-width: 720px;
        padding: 0 1rem;
    }
    
    .header {
        padding: 0.65rem 0;
    }
    
    .layout {
        padding-top: 60px;
    }
    
    .logo {
        height: 38px;
    }
    
    .title-section h1 {
        font-size: 1.35rem;
    }
    
    .subtitle {
        font-size: 0.75rem;
    }
    
    .header-content {
        flex-wrap: wrap;
    }
    
    .header-actions {
        width: 100%;
        justify-content: space-between;
    }
    
    .metrics-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 0.75rem;
    }
    
    .form-row {
        grid-template-columns: repeat(2, 1fr);
    }
    
    .panel-header {
        flex-wrap: wrap;
        gap: 0.65rem;
    }
    
    .panel-actions {
        width: 100%;
    }
    
    .targets-sidebar {
        width: 280px;
        top: 60px;
        height: calc(100vh - 60px);
    }
    
    .targets-header {
        min-height: 55px;
        padding: 0.85rem 1rem;
    }
    
    .targets-floating-toggle {
        min-width: 100px;
        height: 40px;
        font-size: 0.8rem;
    }
    
    .targets-sidebar.open + .targets-floating-toggle {
        right: 295px;
    }
}

/* ========================================================================== */
/* MOBILE SCREENS (576px to 767px) - Large phone                             */
/* ========================================================================== */

@media (max-width: 767px) {
    .container {
        padding: 0 0.75rem;
    }
    
    .header {
        padding: 0.65rem 0;
        position: fixed;
        backdrop-filter: blur(12px);
        -webkit-backdrop-filter: blur(12px);
        box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
    }
    
    .layout {
        padding-top: 55px;
    }
    
    .logo {
        height: 36px;
    }
    
    .title-section h1 {
        font-size: 1.25rem;
    }
    
    .subtitle {
        font-size: 0.7rem;
    }
    
    .header-actions {
        gap: 0.4rem;
    }
    
    .user-box {
        font-size: 0.75rem;
    }
    
    .tab-nav {
        scrollbar-width: none; /* Hide scrollbar on mobile */
    }
    
    .tab-nav::-webkit-scrollbar {
        display: none;
    }
    
    .tab-btn {
        padding: 0.65rem 1rem;
        font-size: 0.8rem;
        min-width: 110px;
        gap: 0.3rem;
    }
    
    .tab-btn i {
        font-size: 0.8rem;
    }
    
    .tab-panel {
        padding: 1rem;
        min-height: calc(100vh - 150px);
    }
    
    .panel-header {
        flex-direction: column;
        align-items: flex-start;
        margin-bottom: 1rem;
        padding-bottom: 0.65rem;
    }
    
    .panel-header h2 {
        font-size: 1.1rem;
    }
    
    .panel-actions {
        width: 100%;
        flex-direction: column;
        gap: 0.4rem;
    }
    
    .panel-actions .btn {
        width: 100%;
        padding: 0.55rem 0.85rem;
        font-size: 0.8rem;
    }
    
    .metrics-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 0.75rem;
    }
    
    .metric-card {
        padding: 1rem 0.85rem;
        border-radius: 10px;
    }
    
    .metric-card:hover {
        transform: translateY(-2px);
    }
    
    /* INDVD Results Responsive - Medium Screens */
    .indvd-graphs-grid {
        grid-template-columns: 1fr;
        gap: 1.25rem;
    }
    
    .indvd-details-grid {
        grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
        gap: 1.25rem;
    }
    
    .metric-value {
        font-size: 1.3rem;
    }
    
    .metric-label {
        font-size: 0.7rem;
    }
    
    .data-table {
        font-size: 0.75rem;
        min-width: 600px;
    }
    
    .data-table th {
        padding: 0.5rem 0.65rem;
        font-size: 0.7rem;
    }
    
    .data-table td {
        padding: 0.45rem 0.65rem;
    }
    
    .data-table-container {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        border-radius: 6px;
        margin-bottom: 1rem;
    }
    
    .btn {
        padding: 0.55rem 0.85rem;
        font-size: 0.8rem;
        min-height: 42px; /* Touch target size */
    }
    
    .btn i {
        font-size: 0.75rem;
    }
    
    .form-control {
        padding: 0.55rem 0.65rem;
        font-size: 0.8rem;
        min-height: 42px; /* Touch target size */
    }
    
    .form-group {
        margin-bottom: 0.85rem;
    }
    
    .form-group label {
        font-size: 0.8rem;
        margin-bottom: 0.3rem;
    }
    
    .form-row {
        grid-template-columns: 1fr;
        gap: 0.65rem;
    }
    
    .formulation-controls {
        padding: 0.85rem;
        margin-bottom: 1rem;
    }
    
    .formulation-row {
        grid-template-columns: 1fr 1fr 40px;
        gap: 0.65rem;
        padding: 0.75rem;
        margin-bottom: 0.65rem;
    }
    
    .formulation-row .form-group label {
        font-size: 0.75rem;
    }
    
    .formulation-row .form-control {
        font-size: 0.8rem;
        padding: 0.45rem 0.55rem;
    }
    
    .formulation-row .btn {
        width: 36px;
        height: 36px;
        padding: 0.35rem;
        min-height: auto;
    }
    
    .modal-content {
        margin: 1rem;
        max-height: calc(100vh - 2rem);
        width: calc(100vw - 2rem);
    }
    
    .modal-header,
    .modal-body,
    .modal-footer {
        padding: 1rem;
    }
    
    .modal-header h3 {
        font-size: 1.05rem;
    }
    
    .results-section {
        padding: 1rem;
        margin-top: 1.25rem;
    }
    
    .results-section h3 {
        font-size: 1.05rem;
        margin-bottom: 0.85rem;
    }
    
    /* Targets sidebar on mobile */
    .targets-sidebar {
        width: 100%;
        max-width: 300px;
        top: 55px;
        height: calc(100vh - 55px);
    }
    
    .targets-header {
        padding: 0.85rem 1rem;
        min-height: 50px;
    }
    
    .targets-header h3 {
        font-size: 0.95rem;
    }
    
    .targets-content {
        padding: 1rem;
    }
    
    .targets-section {
        margin-bottom: 1.25rem;
    }
    
    .targets-section h4 {
        font-size: 0.75rem;
        margin-bottom: 0.75rem;
    }
    
    .targets-section .form-group {
        margin-bottom: 0.75rem;
    }
    
    .targets-section label {
        font-size: 0.75rem;
    }
    
    .targets-section .form-control {
        padding: 0.4rem 0.55rem;
        font-size: 0.75rem;
        min-height: 38px;
    }
    
    .targets-actions {
        padding: 0.85rem 1rem;
        gap: 0.55rem;
    }
    
    .btn-full {
        padding: 0.55rem 0.85rem;
        font-size: 0.75rem;
    }
    
    .btn-full i {
        font-size: 0.7rem;
    }
    
    .targets-floating-toggle {
        right: 0.75rem;
        min-width: 90px;
        height: 38px;
        font-size: 0.75rem;
        padding: 0 0.65rem;
    }
    
    .targets-floating-toggle i {
        font-size: 0.85rem;
    }
    
    .targets-floating-toggle .toggle-text {
        font-size: 0.75rem;
    }
    
    .targets-sidebar.open + .targets-floating-toggle {
        right: calc(100% - 90px);
    }
    
    .ingredient-list {
        max-height: 250px;
        padding: 0.65rem;
    }
    
    .ingredient-list label {
        padding: 0.45rem;
        font-size: 0.8rem;
    }
    
    .bounds-editor {
        gap: 0.65rem;
    }
    
    .bounds-row {
        grid-template-columns: 2fr 1fr 1fr;
        gap: 0.55rem;
        padding: 0.55rem;
    }
    
    .bounds-row label {
        font-size: 0.8rem;
    }
    
    .bounds-row input {
        padding: 0.4rem 0.55rem;
        font-size: 0.8rem;
    }
    
    .progress-steps {
        flex-direction: column;
        align-items: flex-start;
        gap: 0.35rem;
    }
    
    .step {
        font-size: 0.75rem;
        padding: 0.35rem 0.65rem;
    }
    
    .file-upload-area {
        padding: 2rem 1rem;
    }
    
    .file-upload-area i {
        font-size: 2rem;
        margin-bottom: 0.65rem;
    }
    
    .file-upload-area p {
        font-size: 0.85rem;
    }
    
    .info-box {
        padding: 0.55rem;
        font-size: 0.75rem;
    }
    
    .success-message {
        padding: 0.75rem 1rem;
        font-size: 0.8rem;
    }
}

/* ========================================================================== */
/* EXTRA SMALL SCREENS (less than 576px) - Small phone                       */
/* ========================================================================== */

@media (max-width: 575px) {
    .container {
        padding: 0 0.65rem;
    }
    
    .header-content {
        gap: 0.65rem;
    }
    
    .logo-section {
        gap: 0.5rem;
    }
    
    .logo {
        height: 32px;
    }
    
    .title-section h1 {
        font-size: 1.1rem;
    }
    
    .subtitle {
        display: none; /* Hide subtitle on very small screens */
    }
    
    .header-actions {
        font-size: 0.7rem;
    }
    
    .btn {
        padding: 0.5rem 0.75rem;
        font-size: 0.75rem;
        gap: 0.3rem;
        min-height: 40px;
    }
    
    .btn i {
        font-size: 0.7rem;
    }
    
    .tab-nav {
        flex-direction: column;
    }
    
    .tab-btn {
        border-bottom: 1px solid #e5e7eb;
        border-radius: 0;
        padding: 0.875rem 1rem;
        justify-content: flex-start;
        font-size: 0.875rem;
        transition: all 0.2s ease;
    }
    
    .tab-btn:last-child {
        border-bottom: none;
    }
    
    .tab-btn.active {
        border-bottom-color: #2563eb;
        border-left: 4px solid #2563eb;
        background: linear-gradient(135deg, #eff6ff 0%, #dbeafe 100%);
        font-weight: 600;
    }
    
    .tab-btn.active::before {
        display: none;
    }
    
    .tab-panel {
        padding: 0.85rem;
    }
    
    .panel-header h2 {
        font-size: 1rem;
    }
    
    .metrics-grid {
        grid-template-columns: 1fr;
        gap: 0.55rem;
    }
    
    .metric-card {
        padding: 0.75rem;
    }
    
    .metric-value {
        font-size: 1.2rem;
    }
    
    .metric-label {
        font-size: 0.7rem;
    }
    
    .data-table {
        min-width: 550px;
        font-size: 0.7rem;
    }
    
    .data-table th {
        padding: 0.45rem 0.5rem;
        font-size: 0.65rem;
    }
    
    .data-table td {
        padding: 0.4rem 0.5rem;
    }
    
    .formulation-row {
        grid-template-columns: 1fr;
        gap: 0.55rem;
        padding: 0.65rem;
    }
    
    .formulation-row .btn {
        width: 100%;
        height: auto;
    }
    
    /* INDVD Results Responsive */
    .indvd-key-result {
        padding: 1.5rem 1rem;
        margin: 1rem 0;
    }
    
    .indvd-percentage-large {
        font-size: 3rem;
    }
    
    .indvd-label-large {
        font-size: 1rem;
    }
    
    .indvd-digestibility-badge {
        font-size: 0.8rem;
        padding: 0.4rem 1rem;
    }
    
    .indvd-graphs-grid {
        grid-template-columns: 1fr;
        gap: 1rem;
    }
    
    .indvd-graph-card {
        padding: 1rem;
    }
    
    .indvd-donut-container,
    .indvd-bar-container {
        min-height: 280px;
    }
    
    .indvd-details-grid {
        grid-template-columns: 1fr;
        gap: 1rem;
    }
    
    .detail-section {
        padding: 1rem;
    }
    
    .detail-row {
        padding: 0.5rem;
        flex-wrap: wrap;
    }
    
    .detail-label {
        font-size: 0.8rem;
        margin-bottom: 0.25rem;
    }
    
    .detail-value {
        font-size: 0.85rem;
        padding: 0.5rem;
    }
    
    .targets-sidebar {
        width: 100%;
    }
    
    .targets-header {
        padding: 0.75rem 0.85rem;
    }
    
    .targets-header h3 {
        font-size: 0.9rem;
    }
    
    .targets-content {
        padding: 0.85rem;
    }
    
    .targets-floating-toggle {
        right: 0.5rem;
        min-width: 80px;
        height: 36px;
        font-size: 0.7rem;
        padding: 0 0.55rem;
    }
    
    .targets-floating-toggle .toggle-text {
        font-size: 0.7rem;
    }
    
    .targets-sidebar.open + .targets-floating-toggle {
        right: calc(100% - 80px);
    }
    
    .modal-content {
        margin: 0.75rem;
        width: calc(100vw - 1.5rem);
    }
    
    .modal-header,
    .modal-body,
    .modal-footer {
        padding: 0.85rem;
    }
    
    .bounds-row {
        grid-template-columns: 1fr;
    }
}

/* ========================================================================== */
/* LANDSCAPE ORIENTATION - Mobile                                            */
/* ========================================================================== */

@media (max-width: 767px) and (orientation: landscape) {
    .layout {
        padding-top: 55px;
    }
    
    .metrics-grid {
        grid-template-columns: repeat(3, 1fr);
    }
    
    .targets-sidebar {
        width: 280px;
        height: calc(100vh - 55px);
    }
    
    .targets-sidebar.open + .targets-floating-toggle {
        right: 295px;
    }
}

/* ========================================================================== */
/* HIGH DPI DISPLAYS                                                         */
/* ========================================================================== */

@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .logo {
        image-rendering: -webkit-optimize-contrast;
        image-rendering: crisp-edges;
    }
}

/* ========================================================================== */
/* REDUCED MOTION                                                            */
/* ========================================================================== */

@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: 0.01ms !important;
    }
    
    .loading-spinner i {
        animation: none;
    }
    
    .btn:hover,
    .metric-card:hover,
    .targets-toggle i,
    .targets-floating-toggle i {
        transform: none !important;
    }
}

/* ========================================================================== */
/* PRINT OPTIMIZATIONS                                                       */
/* ========================================================================== */

@media print {
    @page {
        margin: 0.5in;
        size: A4;
    }
    
    body {
        font-size: 11pt;
        line-height: 1.4;
    }
    
    .header {
        background: white !important;
        color: black !important;
        box-shadow: none !important;
        border-bottom: 2px solid #000;
        position: static !important;
    }
    
    .layout {
        padding-top: 0 !important;
    }
    
    .tab-nav,
    .targets-sidebar,
    .targets-floating-toggle,
    .modal,
    .loading-overlay {
        display: none !important;
    }
    
    .content {
        box-shadow: none !important;
        border: 1px solid #000;
    }
    
    .tab-panel {
        display: block !important;
        page-break-inside: avoid;
        padding: 0.5in;
    }
    
    .data-table {
        font-size: 9pt;
        border-collapse: collapse;
    }
    
    .data-table th,
    .data-table td {
        border: 1px solid #000;
        padding: 3pt 5pt;
    }
    
    .data-table th {
        background: #f0f0f0 !important;
        font-weight: bold;
    }
    
    .metrics-grid {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 8pt;
        margin: 10pt 0;
    }
    
    .metric-card {
        border: 1px solid #000;
        padding: 8pt;
        text-align: center;
    }
    
    .btn,
    .panel-actions {
        display: none !important;
    }
    
    .data-table-container,
    .metrics-grid {
        page-break-inside: avoid;
    }
    
    .panel-header {
        border-bottom: 2px solid #000;
        padding-bottom: 0.25in;
    }
}

/* ========================================================================== */
/* HIGH CONTRAST MODE                                                        */
/* ========================================================================== */

@media (prefers-contrast: high) {
    .btn-primary {
        background: #0000ff !important;
        color: #ffffff !important;
        border: 2px solid #000000 !important;
    }
    
    .btn-secondary {
        background: #808080 !important;
        color: #ffffff !important;
        border: 2px solid #000000 !important;
    }
    
    .form-control {
        border: 2px solid #000000 !important;
    }
    
    .data-table th {
        background: #000000 !important;
        color: #ffffff !important;
    }
    
    .data-table td {
        border: 1px solid #000000 !important;
    }
}

/* ========================================================================== */
/* TOUCH OPTIMIZATIONS                                                       */
/* ========================================================================== */

@media (max-width: 768px) and (hover: none) and (pointer: coarse) {
    /* Improve touch targets */
    button,
    .btn,
    .tab-btn,
    input[type="checkbox"] {
        min-height: 42px;
        min-width: 42px;
    }
    
    /* Prevent zoom on input focus - iOS Safari fix */
    input,
    select,
    textarea {
        font-size: 16px !important;
    }
    
    /* Add touch feedback */
    .btn:active,
    .tab-btn:active {
        transform: scale(0.97) !important;
        transition: transform 0.1s ease;
    }
    
    /* Improve scrolling */
    .data-table-container,
    .targets-content,
    .tab-panel {
        -webkit-overflow-scrolling: touch;
        scroll-behavior: smooth;
    }
    
    /* Better tap highlights */
    button,
    a,
    input[type="button"],
    input[type="submit"] {
        -webkit-tap-highlight-color: rgba(37, 99, 235, 0.15);
    }
}

/* ========================================================================== */
/* IOS SPECIFIC FIXES                                                        */
/* ========================================================================== */

@supports (-webkit-touch-callout: none) {
    /* Fix iOS input zoom */
    input,
    select,
    textarea {
        font-size: max(16px, 1em);
    }
    
    /* Fix iOS sticky positioning */
    .header {
        position: -webkit-sticky;
        position: fixed;
    }
    
    /* Fix iOS bottom safe area */
    .tab-content {
        padding-bottom: env(safe-area-inset-bottom);
    }
    
    .targets-sidebar {
        padding-bottom: env(safe-area-inset-bottom);
    }
}

/* ========================================================================== */
/* FOCUS INDICATORS - Keyboard Navigation                                    */
/* ========================================================================== */

.btn:focus-visible,
.form-control:focus-visible,
.tab-btn:focus-visible {
    outline: 2px solid #3b82f6 !important;
    outline-offset: 2px;
}

/* ========================================================================== */
/* SKIP LINKS - Screen Reader Support                                        */
/* ========================================================================== */

.skip-link {
    position: absolute;
    top: -40px;
    left: 6px;
    background: #000;
    color: #fff;
    padding: 8px;
    text-decoration: none;
    z-index: 10000;
    border-radius: 4px;
}

.skip-link:focus {
    top: 6px;
}

/* ========================================================================== */
/* MOBILE/DESKTOP VISIBILITY TOGGLES                                         */
/* ========================================================================== */

.desktop-only {
    display: inline;
}

@media (max-width: 767px) {
    .mobile-only {
        display: inline-block !important;
    }
    
    .desktop-only {
        display: none !important;
    }
}

/* ========================================================================== */
/* END OF RESPONSIVE STYLESHEET                                              */
/* ========================================================================== */
