/* ===== Responsive layout - mobile portrait (< 640px) ===== */
@media (max-width: 639px) {
    /* ===== Click-to-toggle - centered popup ===== */
    /* Hide the checkbox */
    .section-toggle {
        display: none;
    }
    
    /* Content area hidden by default, pops up centered */
    .content-toggle {
        display: none;
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 85%;
        max-width: 400px;
        max-height: 80vh;
        background: rgba(255, 255, 255, 0.98);
        z-index: 1000;
        padding: 30px 20px;
        padding-top: 50px;
        overflow-y: auto;
        box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
        pointer-events: none;
        box-sizing: border-box;
        border-radius: 16px;
        animation: fadeInScale 0.3s ease;
    }
    
    /* Show the centered popup when selected */
    #box-toggle:checked ~ .box-info .content-toggle,
    #product-toggle:checked ~ .product-info .content-toggle {
        display: block !important;
        pointer-events: auto;
    }
    
    /* Ensure the inputs inside the popup are editable */
    .content-toggle input,
    .content-toggle select,
    .content-toggle button {
        pointer-events: auto !important;
    }
    
    /* Popup layer close button */
    .close-popup-btn {
        position: absolute !important;
        top: 12px !important;
        right: 12px !important;
        width: 36px !important;
        height: 36px !important;
        min-width: 36px !important;
        min-height: 36px !important;
        max-width: 36px !important;
        max-height: 36px !important;
        background: #F3F4F6 !important;
        color: #6B7280 !important;
        border-radius: 50% !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        font-size: 20px !important;
        cursor: pointer !important;
        z-index: 1001 !important;
        flex-shrink: 0 !important;
        padding: 0 !important;
        border: none !important;
        box-sizing: border-box !important;
        margin: 0 !important;
        transition: all 0.2s ease !important;
    }
    
    .close-popup-btn:hover {
        background: #E5E7EB !important;
        color: #374151 !important;
    }
    
    /* Remove the outer box info and product info frames */
    .box-info,
    .product-info {
        background: transparent !important;
        border: none !important;
        box-shadow: none !important;
        padding: 0 !important;
        margin-bottom: 8px !important;
    }
    
    /* Label styles - clickable effect */
    .box-info .info-label,
    .product-info .info-label {
        display: block;
        cursor: pointer;
        padding: 12px 16px;
        background: linear-gradient(135deg, #EEF2FF, #E0E7FF);
        border-radius: 8px;
        border: 1px solid #C7D2FE;
        transition: all 0.3s ease;
        margin-bottom: 8px;
        width: fit-content;
        margin-left: auto;
    }
    
    .box-info .info-label:hover,
    .product-info .info-label:hover {
        background: linear-gradient(135deg, #E0E7FF, #C7D2FE);
    }
    
    /* Add an expand arrow */
    .box-info .info-label h2::after,
    .product-info .info-label h2::after {
        content: '▶';
        font-size: 10px;
        margin-left: 8px;
        color: #4338CA;
        transition: transform 0.3s ease;
        display: inline-block;
    }
    
    /* Original styles */
    .container {
        flex-direction: row-reverse;
    }

    .panel {
        width: 85%;
        min-width: 280px;
        max-width: 340px;
        height: 100%;
        padding: 12px;
        font-size: 11px;
    }

    .output-section {
        height: 100%;
    }

    /* ===== Commented out - legacy panel collapse functionality ===== */
    /* Show the collapse toggle button */
    /*
    .toggle-panel-btn {
        display: flex;
        align-items: center;
        justify-content: center;
    }
    */

    .input-group .row {
        flex-wrap: wrap;
        gap: 4px;
    }

    .input-group label {
        width: 100%;
        min-width: auto;
        font-size: 11px;
    }

    .input-group input,
    .input-group .box-weight,
    .input-group .box-volume {
        width: calc(50% - 3px);
        min-width: 80px;
        font-size: 10px;
    }

    .panel h2 {
        font-size: 13px;
        margin-bottom: 0;
        color: #374151;
    }

    .product-table th, .product-table td {
        white-space: nowrap;
        padding: 5px 6px;
        font-size: 9px;
    }

    .product-table th {
        font-size: 8px;
    }

    .product-table input[type="number"] {
        width: 38px;
        padding: 3px 2px;
        font-size: 9px;
    }

    #calculateBtn {
        width: fit-content;
        padding: 10px 24px;
        font-size: 13px;
        margin-left: auto;
        display: block;
    }

    .volume-hint {
        bottom: 80px;
        left: 50%;
        transform: translateX(-50%);
        padding: 10px 16px;
        font-size: 12px;
        min-width: 160px;
        max-width: calc(100% - 40px);
    }

    #remaining-space {
        bottom: 70px;
        left: 50%;
        transform: translateX(-50%);
        padding: 12px 16px;
        min-width: 150px;
        max-width: calc(100% - 40px);
        height: auto;
    }

    #remaining-space h3 {
        font-size: 10px;
    }

    #remaining-space p {
        font-size: 9px;
    }

    .conversion-row {
        flex-wrap: wrap;
        gap: 8px;
        justify-content: flex-start;
    }
}

/* ===== Desktop styles (>= 640px) - centered popup ===== */
@media (min-width: 640px) {
    /* Hide the checkbox */
    .section-toggle {
        display: none;
    }
    
    /* Desktop centered popup styles */
    .content-toggle {
        display: none;
        position: fixed;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 80%;
        max-width: 600px;
        max-height: 80vh;
        background: rgba(255, 255, 255, 0.98);
        z-index: 1000;
        padding: 30px 20px;
        padding-top: 50px;
        overflow-y: auto;
        box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
        pointer-events: none;
        box-sizing: border-box;
        border-radius: 16px;
        animation: fadeInScale 0.3s ease;
    }
    
    /* Show the centered popup when selected */
    #box-toggle:checked ~ .box-info .content-toggle,
    #product-toggle:checked ~ .product-info .content-toggle {
        display: block !important;
        pointer-events: auto;
    }
    
    /* Show the close button */
    .close-popup-btn {
        position: absolute !important;
        top: 12px !important;
        right: 12px !important;
        width: 36px !important;
        height: 36px !important;
        min-width: 36px !important;
        min-height: 36px !important;
        max-width: 36px !important;
        max-height: 36px !important;
        background: #F3F4F6 !important;
        color: #6B7280 !important;
        border-radius: 50% !important;
        display: flex !important;
        align-items: center !important;
        justify-content: center !important;
        font-size: 20px !important;
        cursor: pointer !important;
        border: none !important;
        z-index: 1001 !important;
        flex-shrink: 0 !important;
        margin: 0 !important;
        transition: all 0.2s ease !important;
    }
    
    .close-popup-btn:hover {
        background: #E5E7EB !important;
        color: #374151 !important;
    }
    
    /* Display the popup layer title */
    .content-toggle h2 {
        display: block;
        margin-bottom: 16px;
        font-size: 18px;
        color: #333;
    }
    
    /* Labels act as clickable buttons */
    .box-info .info-label,
    .product-info .info-label {
        display: inline-block;
        cursor: pointer;
        padding: 12px 16px;
        background: linear-gradient(135deg, #EEF2FF, #E0E7FF);
        border-radius: 8px;
        margin-bottom: 12px;
        border: 1px solid #C7D2FE;
        transition: all 0.2s ease;
        float: right;
    }
    
    /* Label hover effect */
    .box-info .info-label:hover,
    .product-info .info-label:hover {
        background: linear-gradient(135deg, #E0E7FF, #C7D2FE);
    }
    
    /* Add an arrow */
    .box-info .info-label h2::after,
    .product-info .info-label h2::after {
        content: '▶';
        font-size: 10px;
        margin-left: 8px;
    }
    
    /* Remove the outer div frames of the outer box info and product info */
    .box-info,
    .product-info {
        background: transparent !important;
        border: none !important;
        box-shadow: none !important;
        padding: 0 !important;
        margin-bottom: 8px !important;
    }
}

/* ===== Responsive layout - mobile landscape / small tablet (640px - 1023px) ===== */
@media (min-width: 640px) and (max-width: 1023px) {
    .input-section {
        width: 25%;
        min-width: 180px;
        padding: 12px;
        font-size: 11px;
    }

    .product-table th, .product-table td {
        padding: 5px 5px;
        font-size: 10px;
    }

    .product-table input[type="number"] {
        width: 40px;
        font-size: 10px;
    }

    #calculateBtn {
        padding: 10px 20px;
        font-size: 12px;
    }
}

/* ===== Responsive layout - iPad / small desktop (1024px - 1439px) ===== */
@media (min-width: 1024px) and (max-width: 1439px) {
    .input-section {
        width: 22%;
        min-width: 200px;
        padding: 14px;
    }

    .product-table th, .product-table td {
        padding: 7px 6px;
        font-size: 11px;
    }

    .product-table input[type="number"] {
        width: 45px;
        font-size: 11px;
    }
}

/* ===== Responsive layout - standard desktop (1440px - 1919px) ===== */
@media (min-width: 1440px) and (max-width: 1919px) {
    .input-section {
        width: 20%;
        min-width: 220px;
        padding: 16px;
    }

    .product-table th, .product-table td {
        padding: 8px 7px;
        font-size: 12px;
    }

    .product-table input[type="number"] {
        width: 50px;
        font-size: 12px;
    }
}

/* ===== Responsive layout - large screen / external monitor (>= 1920px) ===== */
@media (min-width: 1920px) {
    .input-section {
        width: 18%;
        min-width: 250px;
        max-width: 450px;
        padding: 20px;
        font-size: 14px;
    }

    .product-table th, .product-table td {
        padding: 9px 8px;
        font-size: 13px;
    }

    .product-table input[type="number"] {
        width: 55px;
        font-size: 13px;
        padding: 6px 4px;
    }

    .input-section h2 {
        font-size: 15px;
    }

    .input-group input,
    .input-group .box-weight,
    .input-group .box-volume {
        font-size: 13px;
        padding: 7px 8px;
        width: 85px;
    }

    #calculateBtn {
        padding: 12px 30px;
        font-size: 15px;
    }

    .volume-hint {
        padding: 12px 18px;
        font-size: 14px;
    }

    #remaining-space {
        padding: 14px 18px;
        min-width: 180px;
    }

    #remaining-space h3 {
        font-size: 13px;
    }

    #remaining-space p {
        font-size: 12px;
    }
}

/* ===== Mobile touch optimization ===== */
@media (hover: none) {
    #calculateBtn:active {
        transform: scale(0.98);
    }

    .product-table tbody tr:active td {
        background: #F3F4F6;
    }
}

/* ===== High-DPI screen optimization ===== */
@media (-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi) {
    .product-table input[type="number"] {
        border-width: 2px;
    }
    
    #calculateBtn {
        box-shadow: 0 4px 18px rgba(99, 102, 241, 0.4);
    }
}